dpkg-statoverride.pl 5.9 KB

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