summaryrefslogtreecommitdiff
path: root/ikiwiki-mass-rebuild
blob: 1ec90b3c0d34e05dd6092e542523e0bf6d81646a (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. PATH => $ENV{PATH},
  30. HOME => (getpwnam($user))[7],
  31. );
  32. exec("ikiwiki", "-setup", $setup, @ARGV);
  33. die "failed to run ikiwiki: $!";
  34. }
  35. waitpid($pid,0);
  36. if ($?) {
  37. print STDERR "Processing $setup as user $user failed with code $?\n";
  38. }
  39. }
  40. sub processlist {
  41. my $file=shift;
  42. my $forceuser=shift;
  43. my $list;
  44. open ($list, "<$file") || die "$file: $!";
  45. while (<$list>) {
  46. chomp;
  47. s/^\s+//;
  48. s/\s+$//;
  49. next if /^#/ || ! length;
  50. if (/^([^\s]+)\s+([^\s]+)$/) {
  51. my $user=$1;
  52. my $setup=$2;
  53. if (defined $forceuser && $forceuser ne $user) {
  54. print STDERR "warning: in $file line $., attempt to set user to $user, but user forced to $forceuser. Skipping\n";
  55. }
  56. processline($user, $setup);
  57. }
  58. elsif (/^([^\s]+)$/) {
  59. my $user=$1;
  60. my $home=(getpwnam($user))[7];
  61. if (defined $home && -d $home) {
  62. my $dotfile="$home/.ikiwiki/wikilist";
  63. if (-e $dotfile) {
  64. processlist($dotfile, $user);
  65. }
  66. }
  67. }
  68. }
  69. close $list;
  70. }
  71. my $wikilist="/etc/ikiwiki/wikilist";
  72. if (-e $wikilist) {
  73. processlist($wikilist);
  74. }