]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - ikiwiki-mass-rebuild
inline: The optimisation in 2.41 broke nested inlines. Detect those and avoid overopt...
[git.ikiwiki.info.git] / ikiwiki-mass-rebuild
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
5 sub supplemental_groups {
6         my $user=shift;
8         my @list;
9         while (my @fields=getgrent()) {
10                 if (grep { $_ eq $user } split(' ', $fields[3])) {
11                         push @list, $fields[2];
12                 }
13         }
15         return @list;
16 }
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                 if (($)=$grouplist) ne $grouplist) {
38                         die "failed to set egid $grouplist: $!";
39                 }
40                 $(=$ugid;
41                 $<=$uuid;
42                 $>=$uuid;
43                 if ($< != $uuid || $> != $uuid || $( != $ugid) {
44                         die "failed to drop permissions to $user";
45                 }
46                 %ENV=(
47                         PATH => $ENV{PATH},
48                         HOME => (getpwnam($user))[7],
49                 );
50                 exec("ikiwiki", "-setup", $setup, @ARGV);
51                 die "failed to run ikiwiki: $!";
52         }
53         waitpid($pid,0);
54         if ($?) {
55                 print STDERR "Processing $setup as user $user failed with code $?\n";
56         }
57 }
59 sub processlist {
60         my $file=shift;
61         my $forceuser=shift;
63         my $list;
64         open ($list, "<$file") || die "$file: $!";
65         while (<$list>) {
66                 chomp;
67                 s/^\s+//;
68                 s/\s+$//;
69                 next if /^#/ || ! length;
71                 if (/^([^\s]+)\s+([^\s]+)$/) {
72                         my $user=$1;
73                         my $setup=$2;
74                         if (defined $forceuser && $forceuser ne $user) {
75                                 print STDERR "warning: in $file line $., attempt to set user to $user, but user forced to $forceuser. Skipping\n";
76                         }
77                         processline($user, $setup);
78                 }
79                 elsif (/^([^\s]+)$/) {
80                         my $user=$1;
81                         my $home=(getpwnam($user))[7];
82                         if (defined $home && -d $home) {
83                                 my $dotfile="$home/.ikiwiki/wikilist";
84                                 if (-e $dotfile) {
85                                         processlist($dotfile, $user);
86                                 }
87                         }
88                 }
89         }
90         close $list;
91 }
93 my $wikilist="/etc/ikiwiki/wikilist";
95 if (-e $wikilist) {
96         processlist($wikilist);
97 }