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