summaryrefslogtreecommitdiff
path: root/IkiWiki/Receive.pm
blob: e77c477a98ad743b6db8d584067cff1cba992be3 (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. my %newfiles;
  65. foreach my $change (IkiWiki::rcs_receive()) {
  66. # This untaint is safe because we check file_pruned and
  67. # wiki_file_regexp.
  68. my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
  69. $file=IkiWiki::possibly_foolish_untaint($file);
  70. if (! defined $file || ! length $file ||
  71. IkiWiki::file_pruned($file)) {
  72. error(gettext("bad file name %s"), $file);
  73. }
  74. my $type=pagetype($file);
  75. my $page=pagename($file) if defined $type;
  76. if ($change->{action} eq 'add') {
  77. $newfiles{$file}=1;
  78. }
  79. if ($change->{action} eq 'change' ||
  80. $change->{action} eq 'add') {
  81. if (defined $page) {
  82. IkiWiki::check_canedit($page, $cgi, $session);
  83. next;
  84. }
  85. else {
  86. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  87. IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
  88. IkiWiki::check_canedit($file, $cgi, $session);
  89. next;
  90. }
  91. }
  92. }
  93. elsif ($change->{action} eq 'remove') {
  94. # check_canremove tests to see if the file is present
  95. # on disk. This will fail when a single commit adds a
  96. # file and then removes it again. Avoid the problem
  97. # by not testing the removal in such pairs of changes.
  98. # (The add is still tested, just to make sure that
  99. # no data is added to the repo that a web edit
  100. # could not add.)
  101. next if $newfiles{$file};
  102. if (IkiWiki::Plugin::remove->can("check_canremove")) {
  103. IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
  104. IkiWiki::check_canedit(defined $page ? $page : $file, $cgi, $session);
  105. next;
  106. }
  107. }
  108. else {
  109. error "unknown action ".$change->{action};
  110. }
  111. error sprintf(gettext("you are not allowed to change %s"), $file);
  112. }
  113. exit 0;
  114. }
  115. 1