]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Wrapper.pm
Make pagestats output more deterministic.
[git.ikiwiki.info.git] / IkiWiki / Wrapper.pm
1 #!/usr/bin/perl
3 package IkiWiki;
5 use warnings;
6 use strict;
7 use File::Spec;
8 use Data::Dumper;
9 use IkiWiki;
11 sub gen_wrappers () {
12         debug(gettext("generating wrappers.."));
13         my %origconfig=(%config);
14         foreach my $wrapper (@{$config{wrappers}}) {
15                 %config=(%origconfig, %{$wrapper});
16                 $config{verbose}=$config{setupverbose}
17                         if exists $config{setupverbose};
18                 $config{syslog}=$config{setupsyslog}
19                         if exists $config{setupsyslog};
20                 delete @config{qw(setupsyslog setupverbose wrappers genwrappers rebuild)};
21                 checkconfig();
22                 if (! $config{cgi} && ! $config{post_commit} &&
23                     ! $config{test_receive}) {
24                         $config{post_commit}=1;
25                 }
26                 gen_wrapper();
27         }
28         %config=(%origconfig);
29 }
31 our $program_to_wrap = $0;
32 sub gen_wrapper () {
33         $config{srcdir}=File::Spec->rel2abs($config{srcdir});
34         $config{destdir}=File::Spec->rel2abs($config{destdir});
35         my $this=File::Spec->rel2abs($program_to_wrap);
36         if (! -x $this) {
37                 error(sprintf(gettext("%s doesn't seem to be executable"), $this));
38         }
40         if ($config{setup}) {
41                 error(gettext("cannot create a wrapper that uses a setup file"));
42         }
43         my $wrapper=possibly_foolish_untaint($config{wrapper});
44         if (! defined $wrapper || ! length $wrapper) {
45                 error(gettext("wrapper filename not specified"));
46         }
47         delete $config{wrapper};
48         
49         my @envsave;
50         push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
51                        CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
52                        HTTP_COOKIE REMOTE_USER HTTPS REDIRECT_STATUS
53                        HTTP_HOST SERVER_PORT HTTPS HTTP_ACCEPT
54                        REDIRECT_URL} if $config{cgi};
55         my $envsave="";
56         foreach my $var (@envsave) {
57                 $envsave.=<<"EOF";
58         if ((s=getenv("$var")))
59                 addenv("$var", s);
60 EOF
61         }
62         if (ref $config{ENV} eq 'HASH') {
63                 foreach my $key (keys %{$config{ENV}}) {
64                         my $val=$config{ENV}{$key};
65                         utf8::encode($val) if utf8::is_utf8($val);
66                         $val =~ s/([^A-Za-z0-9])/sprintf '""\\x%02x""', ord($1)/ge;
67                         $envsave.=<<"EOF";
68         addenv("$key", "$val");
69 EOF
70                 }
71                 delete $config{ENV};
72         }
73         
74         my @wrapper_hooks;
75         run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
77         my $check_commit_hook="";
78         my $pre_exec="";
79         if ($config{post_commit}) {
80                 # Optimise checking !commit_hook_enabled() , 
81                 # so that ikiwiki does not have to be started if the
82                 # hook is disabled.
83                 #
84                 # Note that perl's flock may be implemented using fcntl
85                 # or lockf on some systems. If so, and if there is no
86                 # interop between the locking systems, the true C flock will
87                 # always succeed, and this optimisation won't work.
88                 # The perl code will later correctly check the lock,
89                 # so the right thing will still happen, though without
90                 # the benefit of this optimisation.
91                 $check_commit_hook=<<"EOF";
92         {
93                 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
94                 if (fd != -1) {
95                         if (flock(fd, LOCK_SH | LOCK_NB) != 0)
96                                 exit(0);
97                         close(fd);
98                 }
99         }
100 EOF
101         }
102         elsif ($config{cgi}) {
103                 # Avoid more than one ikiwiki cgi running at a time by
104                 # taking a cgi lock. Since ikiwiki uses several MB of
105                 # memory, a pile up of processes could cause thrashing
106                 # otherwise. The fd of the lock is stored in
107                 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
108                 #
109                 # A lot of cgi wrapper processes can potentially build
110                 # up and clog an otherwise unloaded web server. To
111                 # partially avoid this, when a GET comes in and the lock
112                 # is already held, rather than blocking a html page is
113                 # constructed that retries. This is enabled by setting
114                 # cgi_overload_delay.
115                 if (defined $config{cgi_overload_delay} &&
116                     $config{cgi_overload_delay} =~/^[0-9]+/) {
117                         my $i=int($config{cgi_overload_delay});
118                         $pre_exec.="#define CGI_OVERLOAD_DELAY $i\n"
119                                 if $i > 0;
120                         my $msg=gettext("Please wait");
121                         $msg=~s/"/\\"/g;
122                         $pre_exec.='#define CGI_PLEASE_WAIT_TITLE "'.$msg."\"\n";
123                         if (defined $config{cgi_overload_message} && length $config{cgi_overload_message}) {
124                                 $msg=$config{cgi_overload_message};
125                                 $msg=~s/"/\\"/g;
126                         }
127                         $pre_exec.='#define CGI_PLEASE_WAIT_BODY "'.$msg."\"\n";
128                 }
129                 $pre_exec.=<<"EOF";
130         lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
131         if (lockfd != -1) {
132 #ifdef CGI_OVERLOAD_DELAY
133                 char *request_method = getenv("REQUEST_METHOD");
134                 if (request_method && strcmp(request_method, "GET") == 0) {
135                         if (lockf(lockfd, F_TLOCK, 0) == 0) {
136                                 set_cgilock_fd(lockfd);
137                         }
138                         else {
139                                 printf("Content-Type: text/html\\nRefresh: %i; URL=%s\\n\\n<html><head><title>%s</title><head><body><p>%s</p></body></html>",
140                                         CGI_OVERLOAD_DELAY,
141                                         getenv("REQUEST_URI"),
142                                         CGI_PLEASE_WAIT_TITLE,
143                                         CGI_PLEASE_WAIT_BODY);
144                                 exit(0);
145                         }
146                 }
147                 else if (lockf(lockfd, F_LOCK, 0) == 0) {
148                         set_cgilock_fd(lockfd);
149                 }
150 #else
151                 if (lockf(lockfd, F_LOCK, 0) == 0) {
152                         set_cgilock_fd(lockfd);
153                 }
154 #endif
155         }
156 EOF
157         }
159         my $set_background_command='';
160         if (defined $config{wrapper_background_command} &&
161             length $config{wrapper_background_command}) {
162                 my $background_command=delete $config{wrapper_background_command};
163                 $set_background_command=~s/"/\\"/g;
164                 $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
165         }
167         $Data::Dumper::Indent=0; # no newlines
168         my $configstring=Data::Dumper->Dump([\%config], ['*config']);
169         $configstring=~s/\\/\\\\/g;
170         $configstring=~s/"/\\"/g;
171         $configstring=~s/\n/\\n/g;
172         
173         writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
174 /* A wrapper for ikiwiki, can be safely made suid. */
175 #include <stdio.h>
176 #include <sys/types.h>
177 #include <sys/stat.h>
178 #include <fcntl.h>
179 #include <unistd.h>
180 #include <stdlib.h>
181 #include <string.h>
182 #include <sys/file.h>
184 extern char **environ;
185 int newenvironlen=0;
186 /* Array of length newenvironlen+1 (+1 for NULL) */
187 char **newenviron=NULL;
189 void addenv(char *var, char *val) {
190         char *s;
192         if (newenviron) {
193                 newenviron=realloc(newenviron, (newenvironlen+2) * sizeof(char *));
194         }
195         else {
196                 newenviron=calloc(newenvironlen+2, sizeof(char *));
197         }
199         if (!newenviron) {
200                 perror("realloc");
201                 exit(1);
202         }
204         s=malloc(strlen(var)+1+strlen(val)+1);
205         if (!s) {
206                 perror("malloc");
207                 exit(1);
208         }
209         else {
210                 sprintf(s, "%s=%s", var, val);
211                 newenviron[newenvironlen++]=s;
212         }
215 void set_cgilock_fd (int lockfd) {
216         char fd_s[12];
217         sprintf(fd_s, "%i", lockfd);
218         if (setenv("IKIWIKI_CGILOCK_FD", fd_s, 1) != 0) {
219                 perror("setenv");
220                 exit(1);
221         }
224 int main (int argc, char **argv) {
225         int lockfd=-1;
226         char *s;
228 $check_commit_hook
229 @wrapper_hooks
230 $envsave
231         addenv("HOME", "$ENV{HOME}");
232         addenv("PATH", "$ENV{PATH}");
233         addenv("WRAPPED_OPTIONS", "$configstring");
235 #ifdef __TINYC__
236         /* old tcc versions do not support modifying environ directly */
237         if (clearenv() != 0) {
238                 perror("clearenv");
239                 exit(1);
240         }
241         for (; newenvironlen>0; newenvironlen--)
242                 putenv(newenviron[newenvironlen-1]);
243 #else
244         newenviron[newenvironlen]=NULL;
245         environ=newenviron;
246 #endif
248         if (setregid(getegid(), -1) != 0 &&
249             setregid(getegid(), -1) != 0) {
250                 perror("failed to drop real gid");
251                 exit(1);
252         }
253         if (setreuid(geteuid(), -1) != 0 &&
254             setreuid(geteuid(), -1) != 0) {
255                 perror("failed to drop real uid");
256                 exit(1);
257         }
259 $pre_exec
261 $set_background_command
262 #ifdef BACKGROUND_COMMAND
263         if (lockfd != -1) {
264                 close(lockfd);
265         }
267         pid_t pid=fork();
268         if (pid == -1) {
269                 perror("fork");
270                 exit(1);
271         }
272         else if (pid == 0) {
273                 execl("$this", "$this", NULL);
274                 perror("exec $this");
275                 exit(1);                
276         }
277         else {
278                 waitpid(pid, NULL, 0);
280                 if (daemon(1, 0) == 0) {
281                         system(BACKGROUND_COMMAND);
282                         exit(0);
283                 }
284                 else {
285                         perror("daemon");
286                         exit(1);
287                 }
288         }
289 #else
290         execl("$this", "$this", NULL);
291         perror("exec $this");
292         exit(1);
293 #endif
295 EOF
297         my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
298         push @cc, split(' ', possibly_foolish_untaint($ENV{CFLAGS})) if exists $ENV{CFLAGS};
299         if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
300                 #translators: The parameter is a C filename.
301                 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
302         }
303         unlink("$wrapper.c");
304         if (defined $config{wrappergroup}) {
305                 my $gid=(getgrnam($config{wrappergroup}))[2];
306                 if (! defined $gid) {
307                         error(sprintf("bad wrappergroup"));
308                 }
309                 if (! chown(-1, $gid, "$wrapper.new")) {
310                         error("chown $wrapper.new: $!");
311                 }
312         }
313         if (defined $config{wrappermode} &&
314             ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
315                 error("chmod $wrapper.new: $!");
316         }
317         if (! rename("$wrapper.new", $wrapper)) {
318                 error("rename $wrapper.new $wrapper: $!");
319         }
320         #translators: The parameter is a filename.
321         debug(sprintf(gettext("successfully generated %s"), $wrapper));