summaryrefslogtreecommitdiff
path: root/IkiWiki/Receive.pm
blob: 81b67d9b47d5c527bfb538c706a4ea42aedf64ab (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Receive;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. sub getuser () { #{{{
  7. my $user=(getpwuid($<))[0];
  8. if (! defined $user) {
  9. error("cannot determine username for $<");
  10. }
  11. return $user;
  12. } #}}}
  13. sub trusted () { #{{{
  14. my $user=getuser();
  15. return ! ref $config{untrusted_committers} ||
  16. ! grep { $_ eq $user } @{$config{untrusted_committers}};
  17. } #}}}
  18. sub test () { #{{{
  19. exit 0 if trusted();
  20. # Dummy up a cgi environment to use when calling check_canedit
  21. # and friends.
  22. eval q{use CGI};
  23. error($@) if $@;
  24. my $cgi=CGI->new;
  25. require IkiWiki::CGI;
  26. my $session=IkiWiki::cgi_getsession($cgi);
  27. $session->param("name", getuser());
  28. $ENV{REMOTE_ADDR}='unknown' unless exists $ENV{REMOTE_ADDR};
  29. # Wiki is not locked because we lack permission to do so.
  30. # So, relying on atomic index file updates to avoid trouble.
  31. IkiWiki::loadindex();
  32. my %newfiles;
  33. foreach my $change (IkiWiki::rcs_receive()) {
  34. # This untaint is safe because we check file_pruned and
  35. # wiki_file_regexp.
  36. my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
  37. $file=IkiWiki::possibly_foolish_untaint($file);
  38. if (! defined $file || ! length $file ||
  39. IkiWiki::file_pruned($file, $config{srcdir})) {
  40. error(gettext("bad file name %s"), $file);
  41. }
  42. my $type=pagetype($file);
  43. my $page=pagename($file) if defined $type;
  44. if ($change->{action} eq 'add') {
  45. $newfiles{$file}=1;
  46. }
  47. if ($change->{action} eq 'change' ||
  48. $change->{action} eq 'add') {
  49. if (defined $page) {
  50. if (IkiWiki->can("check_canedit")) {
  51. IkiWiki::check_canedit($page, $cgi, $session);
  52. next;
  53. }
  54. }
  55. else {
  56. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  57. IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
  58. next;
  59. }
  60. }
  61. }
  62. elsif ($change->{action} eq 'remove') {
  63. # check_canremove tests to see if the file is present
  64. # on disk. This will fail is a single commit adds a
  65. # file and then removes it again. Avoid the problem
  66. # by not testing the removal in such pairs of changes.
  67. # (The add is still tested, just to make sure that
  68. # no data is added to the repo that a web edit
  69. # could add.)
  70. next if $newfiles{$file};
  71. if (IkiWiki::Plugin::remove->can("check_canremove")) {
  72. IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
  73. next;
  74. }
  75. }
  76. else {
  77. error "unknown action ".$change->{action};
  78. }
  79. error sprintf(gettext("you are not allowed to change %s"), $file);
  80. }
  81. exit 0;
  82. } #}}}
  83. 1