summaryrefslogtreecommitdiff
path: root/IkiWiki/Receive.pm
blob: 88fb4972571e7013480f943483a73a11e5b9a93d (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(exists $ENV{CALLER_UID} ? $ENV{CALLER_UID} : $<))[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 genwrapper () {
  19. # Test for commits from untrusted committers in the wrapper, to
  20. # avoid starting ikiwiki proper at all for trusted commits.
  21. my $ret=<<"EOF";
  22. {
  23. int u=getuid();
  24. EOF
  25. $ret.="\t\tif ( ".
  26. join("&&", map {
  27. my $uid=getpwnam($_);
  28. if (! defined $uid) {
  29. error(sprintf(gettext("cannot determine id of untrusted committer %s"), $_));
  30. }
  31. "u != $uid";
  32. } @{$config{untrusted_committers}}).
  33. ") exit(0);\n";
  34. $ret.=<<"EOF";
  35. asprintf(&s, "CALLER_UID=%i", u);
  36. newenviron[i++]=s;
  37. }
  38. EOF
  39. return $ret;
  40. }
  41. sub test () {
  42. exit 0 if trusted();
  43. IkiWiki::lockwiki();
  44. IkiWiki::loadindex();
  45. # Dummy up a cgi environment to use when calling check_canedit
  46. # and friends.
  47. eval q{use CGI};
  48. error($@) if $@;
  49. my $cgi=CGI->new;
  50. # And dummy up a session object.
  51. require IkiWiki::CGI;
  52. my $session=IkiWiki::cgi_getsession($cgi);
  53. $session->param("name", getuser());
  54. # Make sure whatever user was authed is in the
  55. # userinfo db.
  56. require IkiWiki::UserInfo;
  57. if (! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
  58. IkiWiki::userinfo_setall($session->param("name"), {
  59. email => "",
  60. password => "",
  61. regdate => time,
  62. }) || error("failed adding user");
  63. }
  64. test_changes(cgi => $cgi,
  65. session => $session,
  66. changes => [IkiWiki::rcs_receive()]
  67. );
  68. exit 0;
  69. }
  70. sub test_changes {
  71. my %params = @_;
  72. my $cgi = $params{cgi};
  73. my $session = $params{session};
  74. my @changes = @{$params{changes}};
  75. my %newfiles;
  76. foreach my $change (@changes) {
  77. # This untaint is safe because we check file_pruned and
  78. # wiki_file_regexp.
  79. my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
  80. $file=IkiWiki::possibly_foolish_untaint($file);
  81. if (! defined $file || ! length $file ||
  82. IkiWiki::file_pruned($file)) {
  83. error(gettext("bad file name %s"), $file);
  84. }
  85. my $type=pagetype($file);
  86. my $page=pagename($file) if defined $type;
  87. if ($change->{action} eq 'add') {
  88. $newfiles{$file}=1;
  89. }
  90. if ($change->{action} eq 'change' ||
  91. $change->{action} eq 'add') {
  92. if (defined $page) {
  93. IkiWiki::check_canedit($page, $cgi, $session);
  94. next;
  95. }
  96. else {
  97. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  98. IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
  99. IkiWiki::check_canedit($file, $cgi, $session);
  100. next;
  101. }
  102. }
  103. }
  104. elsif ($change->{action} eq 'remove') {
  105. # check_canremove tests to see if the file is present
  106. # on disk. This will fail when a single commit adds a
  107. # file and then removes it again. Avoid the problem
  108. # by not testing the removal in such pairs of changes.
  109. # (The add is still tested, just to make sure that
  110. # no data is added to the repo that a web edit
  111. # could not add.)
  112. next if $newfiles{$file};
  113. if (IkiWiki::Plugin::remove->can("check_canremove")) {
  114. IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
  115. IkiWiki::check_canedit(defined $page ? $page : $file, $cgi, $session);
  116. next;
  117. }
  118. }
  119. else {
  120. error "unknown action ".$change->{action};
  121. }
  122. error sprintf(gettext("you are not allowed to change %s"), $file);
  123. }
  124. }
  125. 1