summaryrefslogtreecommitdiff
path: root/dh_suidregister
blob: b7b3152fc1842fa0a9837429031fb1d5c4235047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl -w
#
# If no parameters are given, and no debian/suid files exists, scan for 
# suid/sgid files and suidregister them. 
#
# If there are parameters, or there is a debian/suid, register the files
# listed there.

BEGIN { push @INC, "debian", "/usr/share/debhelper" }
use Dh_Lib;
init();

foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
	$TMP=tmpdir($PACKAGE);
	$suid=pkgfile($PACKAGE,"suid");

	@files=();
	if ($suid) {
		@files=filearray($suid);
	}

	if (($PACKAGE eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
		push @files, @ARGV;
	}

	if (! @files && ! $suid) {
		# No files specified (and no empty debian/suid file), so
		# guess what files to process.
		@files=split(/\n/,`find $TMP -type f -perm +6000`);

		# We will strip the debian working directory off of the
		# filenames.
		$tostrip="$TMP/";
	}
	else {
		# We will strip leading /'s, so the user can feed this
		# program either absolute filenames, or relative filenames,
		# and it will do the right thing either way.
		$tostrip="/";
	}

	foreach $file (@files) {
		# Strip leading $tostrip from $file.
		$file=~s/^$tostrip//;

		if (! -e "$TMP/$file") {
			error("\"$TMP/$file\" does not exist.");
		}
		
		# Create the sed string that will be used to 
		# fill in the blanks in the autoscript files.
		# Fill with the owner, group, and perms of the file.
		(undef,undef,$mode,undef,$uid,$gid,undef) = stat("$TMP/$file");
		# Now come up with the user and group names for the uid and gid.
		$user=getpwuid($uid);
		if (! defined $user) {
			warning("$file has odd uid $uid, not in /etc/passwd");
			$user=$uid;
		}
		$group=getgrgid($gid);
		if (! defined $group) {
			warning("$file has odd gid $gid not in /etc/group");
			$group=$gid;
		}
		# Note that I have to print mode in ocal, stripping file type.
		$sedstr=sprintf("s:#FILE#:$file:;s/#PACKAGE#/$PACKAGE/;s/#OWNER#/$user/;s/#GROUP#/$group/;s/#PERMS#/%#o/",
			        $mode & 07777);

		autoscript($PACKAGE,"postinst","postinst-suid",$sedstr);
		autoscript($PACKAGE,"postrm","postrm-suid","$sedstr");
		
		if ( -e "$TMP/$file") {
			doit("chmod","a-s","$TMP/$file");
		}
	}
}