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