web commit by http://edward.myopenid.com/: do i get an error?
[ikiwiki] / ikiwiki-mass-rebuild
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 sub supplemental_groups {
6         my $user=shift;
7
8         my @list;
9         while (my @fields=getgrent()) {
10                 if (grep { $_ eq $user } split(' ', $fields[3])) {
11                         push @list, $fields[2];
12                 }
13         }
14
15         return @list;
16 }
17
18 sub processline {
19         my $user=shift;
20         my $setup=shift;
21         
22         if (! getpwnam("$user")) {
23                 print STDERR "warning: user $user does not exist\n";
24                 return
25         }
26         if (! -f "$setup") {
27                 print STDERR "warning: $setup does not exist, skipping\n";
28                 return;
29         }
30         print "Processing $setup as user $user ...\n";
31         # su is not used because it passes arguments through the shell,
32         # which is not safe for untrusted setup file names.
33         defined(my $pid = fork) or die "Can’t fork: $!";
34         if (! $pid) {
35                 my ($uuid, $ugid) = (getpwnam($user))[2, 3];
36                 my $grouplist=join(" ", $ugid, $ugid, supplemental_groups($user));
37                 $)=$grouplist;
38                 if ($!) {
39                         die "failed to set egid $grouplist: $!";
40                 }
41                 $(=$ugid;
42                 $<=$uuid;
43                 $>=$uuid;
44                 if ($< != $uuid || $> != $uuid || $( != $ugid) {
45                         die "failed to drop permissions to $user";
46                 }
47                 %ENV=(
48                         PATH => $ENV{PATH},
49                         HOME => (getpwnam($user))[7],
50                 );
51                 exec("ikiwiki", "-setup", $setup, @ARGV);
52                 die "failed to run ikiwiki: $!";
53         }
54         waitpid($pid,0);
55         if ($?) {
56                 print STDERR "Processing $setup as user $user failed with code $?\n";
57         }
58 }
59
60 sub processlist {
61         my $file=shift;
62         my $forceuser=shift;
63
64         my $list;
65         open ($list, "<$file") || die "$file: $!";
66         while (<$list>) {
67                 chomp;
68                 s/^\s+//;
69                 s/\s+$//;
70                 next if /^#/ || ! length;
71
72                 if (/^([^\s]+)\s+([^\s]+)$/) {
73                         my $user=$1;
74                         my $setup=$2;
75                         if (defined $forceuser && $forceuser ne $user) {
76                                 print STDERR "warning: in $file line $., attempt to set user to $user, but user forced to $forceuser. Skipping\n";
77                         }
78                         processline($user, $setup);
79                 }
80                 elsif (/^([^\s]+)$/) {
81                         my $user=$1;
82                         my $home=(getpwnam($user))[7];
83                         if (defined $home && -d $home) {
84                                 my $dotfile="$home/.ikiwiki/wikilist";
85                                 if (-e $dotfile) {
86                                         processlist($dotfile, $user);
87                                 }
88                         }
89                 }
90         }
91         close $list;
92 }
93
94 my $wikilist="/etc/ikiwiki/wikilist";
95
96 if (-e $wikilist) {
97         processlist($wikilist);
98 }
99