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