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