add russian openbsd.ru wiki
[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                 ") {\n";
39
40         
41         $ret.=<<"EOF";
42                         /* Trusted user.
43                          * Consume all stdin before exiting, as git may
44                          * otherwise be unhappy. */
45                         char buf[256];
46                         while (read(0, &buf, 256) != 0) {}
47                         exit(0);
48                 }
49                 asprintf(&s, "CALLER_UID=%i", u);
50                 newenviron[i++]=s;
51         }
52 EOF
53         return $ret;
54 }
55
56 sub test () {
57         exit 0 if trusted();
58
59         IkiWiki::lockwiki();
60         IkiWiki::loadindex();
61
62         # Dummy up a cgi environment to use when calling check_canedit
63         # and friends.
64         eval q{use CGI};
65         error($@) if $@;
66         my $cgi=CGI->new;
67
68         # And dummy up a session object.
69         require IkiWiki::CGI;
70         my $session=IkiWiki::cgi_getsession($cgi);
71         $session->param("name", getuser());
72         # Make sure whatever user was authed is in the
73         # userinfo db.
74         require IkiWiki::UserInfo;
75         if (! IkiWiki::userinfo_get($session->param("name"), "regdate")) {
76                 IkiWiki::userinfo_setall($session->param("name"), {
77                         email => "",
78                         password => "",
79                         regdate => time,
80                 }) || error("failed adding user");
81         }
82
83         IkiWiki::check_canchange(
84                 cgi => $cgi,
85                 session => $session,
86                 changes => [IkiWiki::rcs_receive()]
87         );
88         exit 0;
89 }
90
91 1