summaryrefslogtreecommitdiff
path: root/IkiWiki/Receive.pm
blob: 37b6f2a62667600c0ad181eb448d4629504bdd4c (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 gen_wrapper () {
  19. # Test for commits from untrusted committers in the wrapper, to
  20. # avoid loading ikiwiki 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. $ENV{REMOTE_ADDR}='unknown' unless exists $ENV{REMOTE_ADDR};
  51. # And dummy up a session object.
  52. require IkiWiki::CGI;
  53. my $session=IkiWiki::cgi_getsession($cgi);
  54. $session->param("name", getuser());
  55. # Make sure whatever user was authed is in the
  56. # userinfo db.
  57. require IkiWiki::UserInfo;
  58. if (! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
  59. IkiWiki::userinfo_setall($session->param("name"), {
  60. email => "",
  61. password => "",
  62. regdate => time,
  63. }) || error("failed adding user");
  64. }
  65. my %newfiles;
  66. foreach my $change (IkiWiki::rcs_receive()) {
  67. # This untaint is safe because we check file_pruned and
  68. # wiki_file_regexp.
  69. my ($file)=$change->{file}=~/$config{wiki_file_regexp}/;
  70. $file=IkiWiki::possibly_foolish_untaint($file);
  71. if (! defined $file || ! length $file ||
  72. IkiWiki::file_pruned($file, $config{srcdir})) {
  73. error(gettext("bad file name %s"), $file);
  74. }
  75. my $type=pagetype($file);
  76. my $page=pagename($file) if defined $type;
  77. if ($change->{action} eq 'add') {
  78. $newfiles{$file}=1;
  79. }
  80. if ($change->{action} eq 'change' ||
  81. $change->{action} eq 'add') {
  82. if (defined $page) {
  83. if (IkiWiki->can("check_canedit")) {
  84. IkiWiki::check_canedit($page, $cgi, $session);
  85. next;
  86. }
  87. }
  88. else {
  89. if (IkiWiki::Plugin::attachment->can("check_canattach")) {
  90. IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
  91. next;
  92. }
  93. }
  94. }
  95. elsif ($change->{action} eq 'remove') {
  96. # check_canremove tests to see if the file is present
  97. # on disk. This will fail is a single commit adds a
  98. # file and then removes it again. Avoid the problem
  99. # by not testing the removal in such pairs of changes.
  100. # (The add is still tested, just to make sure that
  101. # no data is added to the repo that a web edit
  102. # could add.)
  103. next if $newfiles{$file};
  104. if (IkiWiki::Plugin::remove->can("check_canremove")) {
  105. IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
  106. next;
  107. }
  108. }
  109. else {
  110. error "unknown action ".$change->{action};
  111. }
  112. error sprintf(gettext("you are not allowed to change %s"), $file);
  113. }
  114. exit 0;
  115. }
  116. 1