summaryrefslogtreecommitdiff
path: root/ikiwiki-mass-rebuild
blob: fa8bd913db1844e793062052379248e4612ef2fd (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. sub processline {
  5. my $user=shift;
  6. my $setup=shift;
  7. if (! getpwnam("$user")) {
  8. print STDERR "warning: user $user does not exist\n";
  9. return
  10. }
  11. if (! -f "$setup") {
  12. print STDERR "warning: $setup does not exist, skipping\n";
  13. return;
  14. }
  15. print "Processing $setup as user $user ...\n";
  16. # su is not used because it passes arguments through the shell,
  17. # which is not safe for untrusted setup file names.
  18. defined(my $pid = fork) or die "Can’t fork: $!";
  19. if (! $pid) {
  20. my ($uuid, $ugid) = (getpwnam($user))[2, 3];
  21. $)="$ugid $ugid";
  22. $(=$ugid;
  23. $<=$uuid;
  24. $>=$uuid;
  25. if ($< != $uuid || $> != $uuid || $( != $ugid || $) ne "$ugid $ugid") {
  26. die "failed to drop permissions to $user";
  27. }
  28. %ENV=();
  29. $ENV{HOME}=(getpwnam($user))[7];
  30. exec("ikiwiki", "-setup", $setup, @ARGV);
  31. die "failed to run ikiwiki: $!";
  32. }
  33. waitpid($pid,0);
  34. if ($?) {
  35. print STDERR "Processing $setup as user $user failed with code $?\n";
  36. }
  37. }
  38. sub processlist {
  39. my $file=shift;
  40. my $forceuser=shift;
  41. my $list;
  42. open ($list, "<$file") || die "$file: $!";
  43. while (<$list>) {
  44. chomp;
  45. s/^\s+//;
  46. s/\s+$//;
  47. next if /^#/ || ! length;
  48. if (/^([^\s]+)\s+([^\s]+)$/) {
  49. my $user=$1;
  50. my $setup=$2;
  51. if (defined $forceuser && $forceuser ne $user) {
  52. print STDERR "warning: in $file line $., attempt to set user to $user, but user forced to $forceuser. Skipping\n";
  53. }
  54. processline($user, $setup);
  55. }
  56. elsif (/^([^\s]+)$/) {
  57. my $user=$1;
  58. my $home=(getpwnam($user))[7];
  59. if (defined $home && -d $home) {
  60. my $dotfile="$home/.ikiwiki/wikilist";
  61. if (-e $dotfile) {
  62. processlist($dotfile, $user);
  63. }
  64. }
  65. }
  66. }
  67. close $list;
  68. }
  69. my $wikilist="/etc/ikiwiki/wikilist";
  70. if (-e $wikilist) {
  71. processlist($wikilist);
  72. }