]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Receive.pm
Merge branch 'master' into po
[git.ikiwiki.info.git] / IkiWiki / Receive.pm
1 #!/usr/bin/perl
3 package IkiWiki::Receive;
5 use warnings;
6 use strict;
7 use IkiWiki;
9 sub getuser () {
10         my $user=(getpwuid(exists $ENV{CALLER_UID} ? $ENV{CALLER_UID} : $<))[0];
11         if (! defined $user) {
12                 error("cannot determine username for $<");
13         }
14         return $user;
15 }
17 sub trusted () {
18         my $user=getuser();
19         return ! ref $config{untrusted_committers} ||
20                 ! grep { $_ eq $user } @{$config{untrusted_committers}};
21 }
23 sub gen_wrapper () {
24         # Test for commits from untrusted committers in the wrapper, to
25         # avoid loading ikiwiki at all for trusted commits.
27         my $ret=<<"EOF";
28         {
29                 int u=getuid();
30 EOF
31         $ret.="\t\tif ( ".
32                 join("&&", map {
33                         my $uid=getpwnam($_);
34                         if (! defined $uid) {
35                                 error(sprintf(gettext("cannot determine id of untrusted committer %s"), $_));
36                         }
37                         "u != $uid";
38                 } @{$config{untrusted_committers}}).
39                 ") exit(0);\n";
40         $ret.=<<"EOF";
41                 asprintf(&s, "CALLER_UID=%i", u);
42                 newenviron[i++]=s;
43         }
44 EOF
45         return $ret;
46 }
48 sub test () {
49         exit 0 if trusted();
50         
51         IkiWiki::lockwiki();
52         IkiWiki::loadindex();
53         
54         # Dummy up a cgi environment to use when calling check_canedit
55         # and friends.
56         eval q{use CGI};
57         error($@) if $@;
58         my $cgi=CGI->new;
59         $ENV{REMOTE_ADDR}='unknown' unless exists $ENV{REMOTE_ADDR};
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;
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, $config{srcdir})) {
85                         error(gettext("bad file name %s"), $file);
86                 }
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                 }
95                 if ($change->{action} eq 'change' ||
96                     $change->{action} eq 'add') {
97                         if (defined $page) {
98                                 if (IkiWiki->can("check_canedit")) {
99                                         IkiWiki::check_canedit($page, $cgi, $session);
100                                         next;
101                                 }
102                         }
103                         else {
104                                 if (IkiWiki::Plugin::attachment->can("check_canattach")) {
105                                         IkiWiki::Plugin::attachment::check_canattach($session, $file, $change->{path});
106                                         next;
107                                 }
108                         }
109                 }
110                 elsif ($change->{action} eq 'remove') {
111                         # check_canremove tests to see if the file is present
112                         # on disk. This will fail is a single commit adds a
113                         # file and then removes it again. Avoid the problem
114                         # by not testing the removal in such pairs of changes.
115                         # (The add is still tested, just to make sure that
116                         # no data is added to the repo that a web edit
117                         # could add.)
118                         next if $newfiles{$file};
120                         if (IkiWiki::Plugin::remove->can("check_canremove")) {
121                                 IkiWiki::Plugin::remove::check_canremove(defined $page ? $page : $file, $cgi, $session);
122                                 next;
123                         }
124                 }
125                 else {
126                         error "unknown action ".$change->{action};
127                 }
128                 
129                 error sprintf(gettext("you are not allowed to change %s"), $file);
130         }
132         exit 0;