dpkg-statoverride.pl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use POSIX;
  5. use POSIX qw(:errno_h :signal_h);
  6. use Dpkg;
  7. use Dpkg::Gettext;
  8. textdomain("dpkg");
  9. my $verbose = 1;
  10. my $doforce = 0;
  11. my $doupdate = 0;
  12. my $mode = "";
  13. my %owner;
  14. my %group;
  15. my %mode;
  16. sub version {
  17. printf _g("Debian %s version %s.\n"), $progname, $version;
  18. printf _g("
  19. Copyright (C) 2000 Wichert Akkerman.");
  20. printf _g("
  21. This is free software; see the GNU General Public Licence version 2 or
  22. later for copying conditions. There is NO warranty.
  23. ");
  24. }
  25. sub usage {
  26. printf _g(
  27. "Usage: %s [<option> ...] <command>
  28. Commands:
  29. --add <owner> <group> <mode> <file>
  30. add a new entry into the database.
  31. --remove <file> remove file from the database.
  32. --list [<glob-pattern>] list current overrides in the database.
  33. Options:
  34. --admindir <directory> set the directory with the statoverride file.
  35. --update immediately update file permissions.
  36. --force force an action even if a sanity check fails.
  37. --quiet quiet operation, minimal output.
  38. --help show this help message.
  39. --version show the version.
  40. "), $progname;
  41. }
  42. sub CheckModeConflict {
  43. return unless $mode;
  44. badusage(sprintf(_g("two commands specified: %s and --%s"), $_, $mode));
  45. }
  46. while (@ARGV) {
  47. $_=shift(@ARGV);
  48. last if m/^--$/;
  49. if (!m/^-/) {
  50. unshift(@ARGV,$_); last;
  51. } elsif (m/^--help$/) {
  52. &usage; exit(0);
  53. } elsif (m/^--version$/) {
  54. &version; exit(0);
  55. } elsif (m/^--update$/) {
  56. $doupdate=1;
  57. } elsif (m/^--quiet$/) {
  58. $verbose=0;
  59. } elsif (m/^--force$/) {
  60. $doforce=1;
  61. } elsif (m/^--admindir$/) {
  62. @ARGV || &badusage(sprintf(_g("--%s needs a <directory> argument"), "admindir"));
  63. $admindir= shift(@ARGV);
  64. } elsif (m/^--add$/) {
  65. &CheckModeConflict;
  66. $mode= 'add';
  67. } elsif (m/^--remove$/) {
  68. &CheckModeConflict;
  69. $mode= 'remove';
  70. } elsif (m/^--list$/) {
  71. &CheckModeConflict;
  72. $mode= 'list';
  73. } else {
  74. &badusage(sprintf(_g("unknown option \`%s'"), $_));
  75. }
  76. }
  77. my $dowrite = 0;
  78. my $exitcode = 0;
  79. &badusage(_g("no mode specified")) unless $mode;
  80. &ReadOverrides;
  81. if ($mode eq "add") {
  82. @ARGV==4 || &badusage(_g("--add needs four arguments"));
  83. my $user = $ARGV[0];
  84. my $uid = 0;
  85. my $gid = 0;
  86. if ($user =~ m/^#([0-9]+)$/) {
  87. $uid=$1;
  88. &badusage(sprintf(_g("illegal user %s"), $user)) if ($uid<0);
  89. } else {
  90. my ($name, $pw);
  91. (($name,$pw,$uid)=getpwnam($user)) || &badusage(sprintf(_g("non-existing user %s"), $user));
  92. }
  93. my $group = $ARGV[1];
  94. if ($group =~ m/^#([0-9]+)$/) {
  95. $gid=$1;
  96. &badusage(sprintf(_g("illegal group %s"), $group)) if ($gid<0);
  97. } else {
  98. my ($name, $pw);
  99. (($name,$pw,$gid)=getgrnam($group)) || &badusage(sprintf(_g("non-existing group %s"), $group));
  100. }
  101. my $mode = $ARGV[2];
  102. (($mode<0) or (oct($mode)>07777) or ($mode !~ m/\d+/)) && &badusage(sprintf(_g("illegal mode %s"), $mode));
  103. my $file = $ARGV[3];
  104. $file =~ m/\n/ && &badusage(_g("file may not contain newlines"));
  105. $file =~ s,/+$,, && print STDERR _g("stripping trailing /")."\n";
  106. if (defined $owner{$file}) {
  107. printf STDERR _g("An override for \"%s\" already exists, "), $file;
  108. if ($doforce) {
  109. print STDERR _g("but --force specified so will be ignored.")."\n";
  110. } else {
  111. print STDERR _g("aborting")."\n";
  112. exit(3);
  113. }
  114. }
  115. $owner{$file}=$user;
  116. $group{$file}=$group;
  117. $mode{$file}=$mode;
  118. $dowrite=1;
  119. if ($doupdate) {
  120. if (not -e $file) {
  121. printf STDERR _g("warning: --update given but %s does not exist")."\n", $file;
  122. } else {
  123. chown ($uid,$gid,$file) || warn sprintf(_g("failed to chown %s: %s"), $file, $!)."\n";
  124. chmod (oct($mode),$file) || warn sprintf(_g("failed to chmod %s: %s"), $file, $!)."\n";
  125. }
  126. }
  127. } elsif ($mode eq "remove") {
  128. @ARGV==1 || &badusage(sprintf(_g("--%s needs a single argument"), "remove"));
  129. my $file = $ARGV[0];
  130. $file =~ s,/+$,, && print STDERR _g("stripping trailing /")."\n";
  131. if (not defined $owner{$file}) {
  132. print STDERR _g("No override present.")."\n";
  133. exit(0) if ($doforce);
  134. exit(2);
  135. }
  136. delete $owner{$file};
  137. delete $group{$file};
  138. delete $mode{$file};
  139. $dowrite=1;
  140. print(STDERR _g("warning: --update is useless for --remove")."\n") if ($doupdate);
  141. } elsif ($mode eq "list") {
  142. my (@list, @ilist);
  143. @ilist= @ARGV ? @ARGV : ('*');
  144. while (defined($_=shift(@ilist))) {
  145. s/\W/\\$&/g;
  146. s/\\\?/./g;
  147. s/\\\*/.*/g;
  148. s,/+$,, && print STDERR _g("stripping trailing /")."\n";
  149. push(@list,"^$_\$");
  150. }
  151. my $pattern = join('|', @list);
  152. $exitcode=1;
  153. for my $file (keys %owner) {
  154. next unless ($file =~ m/$pattern/o);
  155. $exitcode=0;
  156. print "$owner{$file} $group{$file} $mode{$file} $file\n";
  157. }
  158. }
  159. &WriteOverrides if ($dowrite);
  160. exit($exitcode);
  161. sub ReadOverrides {
  162. open(SO,"$admindir/statoverride") || &quit(sprintf(_g("cannot open statoverride: %s"), $!));
  163. while (<SO>) {
  164. my ($owner,$group,$mode,$file);
  165. chomp;
  166. ($owner,$group,$mode,$file)=split(' ', $_, 4);
  167. die sprintf(_g("Multiple overrides for \"%s\", aborting"), $file)
  168. if defined $owner{$file};
  169. $owner{$file}=$owner;
  170. $group{$file}=$group;
  171. $mode{$file}=$mode;
  172. }
  173. close(SO);
  174. }
  175. sub WriteOverrides {
  176. my ($file);
  177. open(SO,">$admindir/statoverride-new") || &quit(sprintf(_g("cannot open new statoverride file: %s"), $!));
  178. foreach $file (keys %owner) {
  179. print SO "$owner{$file} $group{$file} $mode{$file} $file\n";
  180. }
  181. close(SO);
  182. chmod(0644, "$admindir/statoverride-new");
  183. unlink("$admindir/statoverride-old") ||
  184. $! == ENOENT || &quit(sprintf(_g("error removing statoverride-old: %s"), $!));
  185. link("$admindir/statoverride","$admindir/statoverride-old") ||
  186. $! == ENOENT || &quit(sprintf(_g("error creating new statoverride-old: %s"), $!));
  187. rename("$admindir/statoverride-new","$admindir/statoverride")
  188. || &quit(sprintf(_g("error installing new statoverride: %s"), $!));
  189. }
  190. sub quit
  191. {
  192. printf STDERR "%s: %s\n", $0, "@_";
  193. exit(2);
  194. }
  195. sub badusage
  196. {
  197. printf STDERR "%s: %s\n\n", $0, "@_";
  198. &usage;
  199. exit(2);
  200. }
  201. # vi: ts=8 sw=8 ai si cindent