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