]> git.donarmstrong.com Git - debhelper.git/blob - dh_suidregister
r320: Fixed a rather esoteric bug: If a file had multiple hard links, and was
[debhelper.git] / dh_suidregister
1 #!/usr/bin/perl -w
2 #
3 # If no parameters are given, and no debian/suid files exists, scan for 
4 # suid/sgid files and suidregister them. 
5 #
6 # If there are parameters, or there is a debian/suid, register the files
7 # listed there.
8
9 BEGIN { push @INC, "debian", "/usr/share/debhelper" }
10 use Dh_Lib;
11 init();
12
13 foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
14         $TMP=tmpdir($PACKAGE);
15         $suid=pkgfile($PACKAGE,"suid");
16
17         @files=();
18         if ($suid) {
19                 @files=filearray($suid);
20         }
21
22         if (($PACKAGE eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
23                 push @files, @ARGV;
24         }
25
26         if (! @files && ! $suid) {
27                 # No files specified (and no empty debian/suid file), so
28                 # guess what files to process.
29                 @files=split(/\n/,`find $TMP -type f -perm +6000`);
30
31                 # We will strip the debian working directory off of the
32                 # filenames.
33                 $tostrip="$TMP/";
34         }
35         else {
36                 # We will strip leading /'s, so the user can feed this
37                 # program either absolute filenames, or relative filenames,
38                 # and it will do the right thing either way.
39                 $tostrip="/";
40         }
41
42         # Register files with suidregister.
43         foreach $file (@files) {
44                 # Strip leading $tostrip from $file.
45                 $file=~s/^$tostrip//;
46
47                 if (! -e "$TMP/$file") {
48                         error("\"$TMP/$file\" does not exist.");
49                 }
50                 
51                 # Create the sed string that will be used to 
52                 # fill in the blanks in the autoscript files.
53                 # Fill with the owner, group, and perms of the file.
54                 (undef,undef,$mode,undef,$uid,$gid,undef) = stat("$TMP/$file");
55                 # Now come up with the user and group names for the uid and gid.
56                 $user=getpwuid($uid);
57                 if (! defined $user) {
58                         warning("$file has odd uid $uid, not in /etc/passwd");
59                         $user=$uid;
60                 }
61                 $group=getgrgid($gid);
62                 if (! defined $group) {
63                         warning("$file has odd gid $gid not in /etc/group");
64                         $group=$gid;
65                 }
66                 # Note that I have to print mode in ocal, stripping file type.
67                 $sedstr=sprintf("s:#FILE#:$file:;s/#PACKAGE#/$PACKAGE/;s/#OWNER#/$user/;s/#GROUP#/$group/;s/#PERMS#/%#o/",
68                                 $mode & 07777);
69
70                 autoscript($PACKAGE,"postinst","postinst-suid",$sedstr);
71                 autoscript($PACKAGE,"postrm","postrm-suid","$sedstr");
72         }
73
74         # Remove suid bits from files. This is delayed to this point, because
75         # of a situation with hard linked files if it is done earlier.
76         # See changelog for 2.0.77.
77         foreach $file (@files) {
78                 if ( -e "$TMP/$file") {
79                         doit("chmod","a-s","$TMP/$file");
80                 }
81         }
82 }