]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Wrapper.pm
Add ikistrap plugin for ikistrap theme.
[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         push @envsave, qw{GIT_OBJECT_DIRECTORY GIT_QUARANTINE_PATH
56                        GIT_ALTERNATE_OBJECT_DIRECTORIES} if $config{test_receive};
57         my $envsave="";
58         foreach my $var (@envsave) {
59                 $envsave.=<<"EOF";
60         if ((s=getenv("$var")))
61                 addenv("$var", s);
62 EOF
63         }
64         if (ref $config{ENV} eq 'HASH') {
65                 foreach my $key (keys %{$config{ENV}}) {
66                         my $val=$config{ENV}{$key};
67                         utf8::encode($val) if utf8::is_utf8($val);
68                         $val =~ s/([^A-Za-z0-9])/sprintf '""\\x%02x""', ord($1)/ge;
69                         $envsave.=<<"EOF";
70         addenv("$key", "$val");
71 EOF
72                 }
73                 delete $config{ENV};
74         }
75         
76         my @wrapper_hooks;
77         run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
79         my $check_commit_hook="";
80         my $pre_exec="";
81         if ($config{post_commit}) {
82                 # Optimise checking !commit_hook_enabled() , 
83                 # so that ikiwiki does not have to be started if the
84                 # hook is disabled.
85                 #
86                 # Note that perl's flock may be implemented using fcntl
87                 # or lockf on some systems. If so, and if there is no
88                 # interop between the locking systems, the true C flock will
89                 # always succeed, and this optimisation won't work.
90                 # The perl code will later correctly check the lock,
91                 # so the right thing will still happen, though without
92                 # the benefit of this optimisation.
93                 $check_commit_hook=<<"EOF";
94         {
95                 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
96                 if (fd != -1) {
97                         if (flock(fd, LOCK_SH | LOCK_NB) != 0)
98                                 exit(0);
99                         close(fd);
100                 }
101         }
102 EOF
103         }
104         elsif ($config{cgi}) {
105                 # Avoid more than one ikiwiki cgi running at a time by
106                 # taking a cgi lock. Since ikiwiki uses several MB of
107                 # memory, a pile up of processes could cause thrashing
108                 # otherwise. The fd of the lock is stored in
109                 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
110                 #
111                 # A lot of cgi wrapper processes can potentially build
112                 # up and clog an otherwise unloaded web server. To
113                 # partially avoid this, when a GET comes in and the lock
114                 # is already held, rather than blocking a html page is
115                 # constructed that retries. This is enabled by setting
116                 # cgi_overload_delay.
117                 if (defined $config{cgi_overload_delay} &&
118                     $config{cgi_overload_delay} =~/^[0-9]+/) {
119                         my $i=int($config{cgi_overload_delay});
120                         $pre_exec.="#define CGI_OVERLOAD_DELAY $i\n"
121                                 if $i > 0;
122                         my $msg=gettext("Please wait");
123                         $msg=~s/"/\\"/g;
124                         $pre_exec.='#define CGI_PLEASE_WAIT_TITLE "'.$msg."\"\n";
125                         if (defined $config{cgi_overload_message} && length $config{cgi_overload_message}) {
126                                 $msg=$config{cgi_overload_message};
127                                 $msg=~s/"/\\"/g;
128                         }
129                         $pre_exec.='#define CGI_PLEASE_WAIT_BODY "'.$msg."\"\n";
130                 }
131                 $pre_exec.=<<"EOF";
132         lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
133         if (lockfd != -1) {
134 #ifdef CGI_OVERLOAD_DELAY
135                 char *request_method = getenv("REQUEST_METHOD");
136                 if (request_method && strcmp(request_method, "GET") == 0) {
137                         if (lockf(lockfd, F_TLOCK, 0) == 0) {
138                                 set_cgilock_fd(lockfd);
139                         }
140                         else {
141                                 printf("Content-Type: text/html\\nRefresh: %i; URL=%s\\n\\n<html><head><title>%s</title><head><body><p>%s</p></body></html>",
142                                         CGI_OVERLOAD_DELAY,
143                                         getenv("REQUEST_URI"),
144                                         CGI_PLEASE_WAIT_TITLE,
145                                         CGI_PLEASE_WAIT_BODY);
146                                 exit(0);
147                         }
148                 }
149                 else if (lockf(lockfd, F_LOCK, 0) == 0) {
150                         set_cgilock_fd(lockfd);
151                 }
152 #else
153                 if (lockf(lockfd, F_LOCK, 0) == 0) {
154                         set_cgilock_fd(lockfd);
155                 }
156 #endif
157         }
158 EOF
159         }
161         my $set_background_command='';
162         if (defined $config{wrapper_background_command} &&
163             length $config{wrapper_background_command}) {
164                 my $background_command=delete $config{wrapper_background_command};
165                 $background_command=~s/"/\\"/g;
166                 $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
167         }
169         $Data::Dumper::Indent=0; # no newlines
170         my $configstring=Data::Dumper->Dump([\%config], ['*config']);
171         $configstring=~s/\\/\\\\/g;
172         $configstring=~s/"/\\"/g;
173         $configstring=~s/\n/\\n/g;
174         
175         writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
176 /* A wrapper for ikiwiki, can be safely made suid. */
177 #include <stdio.h>
178 #include <sys/types.h>
179 #include <sys/stat.h>
180 #include <fcntl.h>
181 #include <unistd.h>
182 #include <stdlib.h>
183 #include <string.h>
184 #include <sys/file.h>
185 #include <sys/wait.h>
187 extern char **environ;
188 int newenvironlen=0;
189 /* Array of length newenvironlen+1 (+1 for NULL) */
190 char **newenviron=NULL;
192 void addenv(char *var, char *val) {
193         char *s;
195         if (newenviron) {
196                 newenviron=realloc(newenviron, (newenvironlen+2) * sizeof(char *));
197         }
198         else {
199                 newenviron=calloc(newenvironlen+2, sizeof(char *));
200         }
202         if (!newenviron) {
203                 perror("realloc");
204                 exit(1);
205         }
207         s=malloc(strlen(var)+1+strlen(val)+1);
208         if (!s) {
209                 perror("malloc");
210                 exit(1);
211         }
212         else {
213                 sprintf(s, "%s=%s", var, val);
214                 newenviron[newenvironlen++]=s;
215         }
218 void set_cgilock_fd (int lockfd) {
219         char fd_s[12];
220         sprintf(fd_s, "%i", lockfd);
221         if (setenv("IKIWIKI_CGILOCK_FD", fd_s, 1) != 0) {
222                 perror("setenv");
223                 exit(1);
224         }
227 int main (int argc, char **argv) {
228         int lockfd=-1;
229         char *s;
231 $check_commit_hook
232 @wrapper_hooks
233 $envsave
234         addenv("HOME", "$ENV{HOME}");
235         addenv("PATH", "$ENV{PATH}");
236         addenv("WRAPPED_OPTIONS", "$configstring");
238 #ifdef __TINYC__
239         /* old tcc versions do not support modifying environ directly */
240         if (clearenv() != 0) {
241                 perror("clearenv");
242                 exit(1);
243         }
244         for (; newenvironlen>0; newenvironlen--)
245                 putenv(newenviron[newenvironlen-1]);
246 #else
247         newenviron[newenvironlen]=NULL;
248         environ=newenviron;
249 #endif
251         if (setregid(getegid(), -1) != 0 &&
252             setregid(getegid(), -1) != 0) {
253                 perror("failed to drop real gid");
254                 exit(1);
255         }
256         if (setreuid(geteuid(), -1) != 0 &&
257             setreuid(geteuid(), -1) != 0) {
258                 perror("failed to drop real uid");
259                 exit(1);
260         }
262 $pre_exec
264 $set_background_command
265 #ifdef BACKGROUND_COMMAND
266         if (lockfd != -1) {
267                 close(lockfd);
268         }
270         pid_t pid=fork();
271         if (pid == -1) {
272                 perror("fork");
273                 exit(1);
274         }
275         else if (pid == 0) {
276                 execl("$this", "$this", NULL);
277                 perror("exec $this");
278                 exit(1);                
279         }
280         else {
281                 waitpid(pid, NULL, 0);
283                 if (daemon(1, 0) == 0) {
284                         system(BACKGROUND_COMMAND);
285                         exit(0);
286                 }
287                 else {
288                         perror("daemon");
289                         exit(1);
290                 }
291         }
292 #else
293         execl("$this", "$this", NULL);
294         perror("exec $this");
295         exit(1);
296 #endif
298 EOF
300         my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
301         push @cc, split(' ', possibly_foolish_untaint($ENV{CFLAGS})) if exists $ENV{CFLAGS};
302         if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
303                 #translators: The parameter is a C filename.
304                 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
305         }
306         unlink("$wrapper.c");
307         if (defined $config{wrappergroup}) {
308                 my $gid=(getgrnam($config{wrappergroup}))[2];
309                 if (! defined $gid) {
310                         error(sprintf("bad wrappergroup"));
311                 }
312                 if (! chown(-1, $gid, "$wrapper.new")) {
313                         error("chown $wrapper.new: $!");
314                 }
315         }
316         if (defined $config{wrappermode} &&
317             ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
318                 error("chmod $wrapper.new: $!");
319         }
320         if (! rename("$wrapper.new", $wrapper)) {
321                 error("rename $wrapper.new $wrapper: $!");
322         }
323         #translators: The parameter is a filename.
324         debug(sprintf(gettext("successfully generated %s"), $wrapper));