do no-op post_commit test in wrapper
[ikiwiki] / IkiWiki / Wrapper.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use File::Spec;
8 use Data::Dumper;
9 use IkiWiki;
10
11 sub gen_wrapper () { #{{{
12         $config{srcdir}=File::Spec->rel2abs($config{srcdir});
13         $config{destdir}=File::Spec->rel2abs($config{destdir});
14         my $this=File::Spec->rel2abs($0);
15         if (! -x $this) {
16                 error(sprintf(gettext("%s doesn't seem to be executable"), $this));
17         }
18
19         if ($config{setup}) {
20                 error(gettext("cannot create a wrapper that uses a setup file"));
21         }
22         my $wrapper=possibly_foolish_untaint($config{wrapper});
23         if (! defined $wrapper || ! length $wrapper) {
24                 error(gettext("wrapper filename not specified"));
25         }
26         delete $config{wrapper};
27         
28         my @envsave;
29         push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
30                        CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
31                        HTTP_COOKIE REMOTE_USER HTTPS} if $config{cgi};
32         my $envsave="";
33         foreach my $var (@envsave) {
34                 $envsave.=<<"EOF";
35         if ((s=getenv("$var")))
36                 addenv("$var", s);
37 EOF
38         }
39
40         my $test_receive="";
41         if ($config{test_receive}) {
42                 require IkiWiki::Receive;
43                 $test_receive=IkiWiki::Receive::gen_wrapper();
44         }
45
46         my $check_commit_hook="";
47         if ($config{post_commit}) {
48                 # Optimise checking !commit_hook_enabled() , 
49                 # so that ikiwiki does not have to be started if the
50                 # hook is disabled.
51                 #
52                 # Note that perl's flock may be implemented using fcntl
53                 # or lockf on some systems. If so, and if there is no
54                 # interop between the locking systems, the true C flock will
55                 # always succeed, and this optimisation won't work.
56                 # The perl code will later correctly check the lock,
57                 # so the right thing will still happen, though without
58                 # the benefit of this optimisation.
59                 $check_commit_hook=<<"EOF";
60         {
61                 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR);
62                 if (fd != -1) {
63                         if (flock(fd, LOCK_SH | LOCK_NB) != 0)
64                                 exit(0);
65                         close(fd);
66                 }
67         }
68 EOF
69         }
70
71         $Data::Dumper::Indent=0; # no newlines
72         my $configstring=Data::Dumper->Dump([\%config], ['*config']);
73         $configstring=~s/\\/\\\\/g;
74         $configstring=~s/"/\\"/g;
75         $configstring=~s/\n/\\n/g;
76         
77         #translators: The first parameter is a filename, and the second is
78         #translators: a (probably not translated) error message.
79         open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
80         print OUT <<"EOF";
81 /* A wrapper for ikiwiki, can be safely made suid. */
82 #include <stdio.h>
83 #include <sys/types.h>
84 #include <sys/stat.h>
85 #include <fcntl.h>
86 #include <unistd.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <sys/file.h>
90
91 extern char **environ;
92 char *newenviron[$#envsave+6];
93 int i=0;
94
95 addenv(char *var, char *val) {
96         char *s=malloc(strlen(var)+1+strlen(val)+1);
97         if (!s)
98                 perror("malloc");
99         sprintf(s, "%s=%s", var, val);
100         newenviron[i++]=s;
101 }
102
103 int main (int argc, char **argv) {
104         char *s;
105
106 $check_commit_hook
107 $test_receive
108 $envsave
109         newenviron[i++]="HOME=$ENV{HOME}";
110         newenviron[i++]="WRAPPED_OPTIONS=$configstring";
111         newenviron[i]=NULL;
112         environ=newenviron;
113
114         if (setregid(getegid(), -1) != 0 &&
115             setregid(getegid(), -1) != 0) {
116                 perror("failed to drop real gid");
117                 exit(1);
118         }
119         if (setreuid(geteuid(), -1) != 0 &&
120             setreuid(geteuid(), -1) != 0) {
121                 perror("failed to drop real uid");
122                 exit(1);
123         }
124
125         execl("$this", "$this", NULL);
126         perror("exec $this");
127         exit(1);
128 }
129 EOF
130         close OUT;
131
132         my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
133         if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
134                 #translators: The parameter is a C filename.
135                 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
136         }
137         unlink("$wrapper.c");
138         if (defined $config{wrappergroup}) {
139                 my $gid=(getgrnam($config{wrappergroup}))[2];
140                 if (! defined $gid) {
141                         error(sprintf("bad wrappergroup"));
142                 }
143                 if (! chown(-1, $gid, "$wrapper.new")) {
144                         error("chown $wrapper.new: $!");
145                 }
146         }
147         if (defined $config{wrappermode} &&
148             ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
149                 error("chmod $wrapper.new: $!");
150         }
151         if (! rename("$wrapper.new", $wrapper)) {
152                 error("rename $wrapper.new $wrapper: $!");
153         }
154         #translators: The parameter is a filename.
155         printf(gettext("successfully generated %s"), $wrapper);
156         print "\n";
157 } #}}}
158
159 1