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