]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Wrapper.pm
Installing ikiwiki on a shared-hosting server, there may be no access to
[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 $envsize=$#envsave;
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                         $val =~ s/([\\"])/\\$1/g;
66                         $envsize += 1;
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                 $pre_exec=<<"EOF";
109         lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
110         if (lockfd != -1 && lockf(lockfd, F_LOCK, 0) == 0) {
111                 char *fd_s=malloc(8);
112                 sprintf(fd_s, "%i", lockfd);
113                 setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
114         }
115 EOF
116         }
118         my $set_background_command='';
119         if (defined $config{wrapper_background_command} &&
120             length $config{wrapper_background_command}) {
121                 my $background_command=delete $config{wrapper_background_command};
122                 $set_background_command=~s/"/\\"/g;
123                 $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
124         }
126         $Data::Dumper::Indent=0; # no newlines
127         my $configstring=Data::Dumper->Dump([\%config], ['*config']);
128         $configstring=~s/\\/\\\\/g;
129         $configstring=~s/"/\\"/g;
130         $configstring=~s/\n/\\n/g;
131         
132         writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
133 /* A wrapper for ikiwiki, can be safely made suid. */
134 #include <stdio.h>
135 #include <sys/types.h>
136 #include <sys/stat.h>
137 #include <fcntl.h>
138 #include <unistd.h>
139 #include <stdlib.h>
140 #include <string.h>
141 #include <sys/file.h>
143 extern char **environ;
144 char *newenviron[$envsize+7];
145 int i=0;
147 void addenv(char *var, char *val) {
148         char *s=malloc(strlen(var)+1+strlen(val)+1);
149         if (!s)
150                 perror("malloc");
151         sprintf(s, "%s=%s", var, val);
152         newenviron[i++]=s;
155 int main (int argc, char **argv) {
156         int lockfd=-1;
157         char *s;
159 $check_commit_hook
160 @wrapper_hooks
161 $envsave
162         newenviron[i++]="HOME=$ENV{HOME}";
163         newenviron[i++]="PATH=$ENV{PATH}";
164         newenviron[i++]="WRAPPED_OPTIONS=$configstring";
166 #ifdef __TINYC__
167         /* old tcc versions do not support modifying environ directly */
168         if (clearenv() != 0) {
169                 perror("clearenv");
170                 exit(1);
171         }
172         for (; i>0; i--)
173                 putenv(newenviron[i-1]);
174 #else
175         newenviron[i]=NULL;
176         environ=newenviron;
177 #endif
179         if (setregid(getegid(), -1) != 0 &&
180             setregid(getegid(), -1) != 0) {
181                 perror("failed to drop real gid");
182                 exit(1);
183         }
184         if (setreuid(geteuid(), -1) != 0 &&
185             setreuid(geteuid(), -1) != 0) {
186                 perror("failed to drop real uid");
187                 exit(1);
188         }
190 $pre_exec
192 $set_background_command
193 #ifdef BACKGROUND_COMMAND
194         if (lockfd != -1) {
195                 close(lockfd);
196         }
198         pid_t pid=fork();
199         if (pid == -1) {
200                 perror("fork");
201                 exit(1);
202         }
203         else if (pid == 0) {
204                 execl("$this", "$this", NULL);
205                 perror("exec $this");
206                 exit(1);                
207         }
208         else {
209                 waitpid(pid, NULL, 0);
211                 if (daemon(1, 0) == 0) {
212                         system(BACKGROUND_COMMAND);
213                         exit(0);
214                 }
215                 else {
216                         perror("daemon");
217                         exit(1);
218                 }
219         }
220 #else
221         execl("$this", "$this", NULL);
222         perror("exec $this");
223         exit(1);
224 #endif
226 EOF
228         my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
229         push @cc, split(' ', possibly_foolish_untaint($ENV{CFLAGS})) if exists $ENV{CFLAGS};
230         if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
231                 #translators: The parameter is a C filename.
232                 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
233         }
234         unlink("$wrapper.c");
235         if (defined $config{wrappergroup}) {
236                 my $gid=(getgrnam($config{wrappergroup}))[2];
237                 if (! defined $gid) {
238                         error(sprintf("bad wrappergroup"));
239                 }
240                 if (! chown(-1, $gid, "$wrapper.new")) {
241                         error("chown $wrapper.new: $!");
242                 }
243         }
244         if (defined $config{wrappermode} &&
245             ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
246                 error("chmod $wrapper.new: $!");
247         }
248         if (! rename("$wrapper.new", $wrapper)) {
249                 error("rename $wrapper.new $wrapper: $!");
250         }
251         #translators: The parameter is a filename.
252         debug(sprintf(gettext("successfully generated %s"), $wrapper));