IkiWiki/Plugin/meta.pm <redir>: Support for specifying anchors.
[ikiwiki.git] / ikiwiki-mass-rebuild
blob8cc6f45c1d9f6fbd21b40e403bc3d5561bb93d3e
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];
15 return @list;
18 sub processline {
19 my $user=shift;
20 my $setup=shift;
22 if (! getpwnam("$user")) {
23 print STDERR "warning: user $user does not exist\n";
24 return
26 if (! -f "$setup") {
27 print STDERR "warning: $setup does not exist, skipping\n";
28 return;
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: $!";
41 $(=$ugid;
42 $<=$uuid;
43 $>=$uuid;
44 if ($< != $uuid || $> != $uuid || $( != $ugid) {
45 die "failed to drop permissions to $user";
47 %ENV=(
48 PATH => $ENV{PATH},
49 HOME => (getpwnam($user))[7],
51 exec("ikiwiki", "-setup", $setup, @ARGV);
52 die "failed to run ikiwiki: $!";
54 waitpid($pid,0);
55 if ($?) {
56 print STDERR "Processing $setup as user $user failed with code $?\n";
60 sub processlist {
61 my $file=shift;
62 my $forceuser=shift;
64 my $list;
65 open ($list, "<$file") || die "$file: $!";
66 while (<$list>) {
67 chomp;
68 s/^\s+//;
69 s/\s+$//;
70 next if /^#/ || ! length;
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";
78 processline($user, $setup);
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);
91 close $list;
94 my $wikilist="/etc/ikiwiki/wikilist";
96 if (-e $wikilist) {
97 processlist($wikilist);