summaryrefslogtreecommitdiff
path: root/IkiWiki/Receive.pm
blob: c69911a7cfbe0130ef7acf8e5b035e7aeaa880ee (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. IkiWiki::lockwiki();
  30. IkiWiki::loadindex();
  31. my %newfiles;
  32. foreach my $change (IkiWiki::rcs_receive()) {
  33. # This untaint is safe because we check file_pruned and
  34. # wiki_file_regexp.
  35. my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
  36. $file=IkiWiki::possibly_foolish_untaint($file);
  37. if (! defined $file || ! length $file ||
  38. IkiWiki::file_pruned($file, $config{srcdir})) {
  39. error(gettext("bad file name"));
  40. }
  41. my $type=pagetype($file);
  42. my $page=pagename($file) if defined $type;
  43. if ($change->{action} eq 'add') {
  44. $newfiles{$file}=1;
  45. }
  46. if ($change->{action} eq 'change' ||
  47. $change->{action} eq 'add') {
  48. if (defined $page) {
  49. if (IkiWiki->can("check_canedit")) {
  50. IkiWiki::check_canedit($page, $cgi, $session);
  51. next;
  52. }
  53. }
  54. else {
  55. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  56. IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
  57. next;
  58. }
  59. }
  60. }
  61. elsif ($change->{action} eq 'remove') {
  62. # check_canremove tests to see if the file is present
  63. # on disk. This will fail is a single commit adds a
  64. # file and then removes it again. Avoid the problem
  65. # by not testing the removal in such pairs of changes.
  66. # (The add is still tested, just to make sure that
  67. # no data is added to the repo that a web edit
  68. # could add.)
  69. next if $newfiles{$file};
  70. if (IkiWiki::Plugin::remove->can("check_canremove")) {
  71. IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
  72. next;
  73. }
  74. }
  75. else {
  76. error "unknown action ".$change->{action};
  77. }
  78. error sprintf(gettext("you are not allowed to change %s"), $file);
  79. }
  80. exit 0;
  81. } #}}}
  82. 1