Added a comment
[ikiwiki] / IkiWiki / Wrapper.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use File::Spec;
8 use Data::Dumper;
9 use IkiWiki;
10
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 }
30
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         }
38
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                        REDIRECT_URL} if $config{cgi};
53         my $envsave="";
54         foreach my $var (@envsave) {
55                 $envsave.=<<"EOF";
56         if ((s=getenv("$var")))
57                 addenv("$var", s);
58 EOF
59         }
60         
61         my @wrapper_hooks;
62         run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
63
64         my $check_commit_hook="";
65         my $pre_exec="";
66         if ($config{post_commit}) {
67                 # Optimise checking !commit_hook_enabled() , 
68                 # so that ikiwiki does not have to be started if the
69                 # hook is disabled.
70                 #
71                 # Note that perl's flock may be implemented using fcntl
72                 # or lockf on some systems. If so, and if there is no
73                 # interop between the locking systems, the true C flock will
74                 # always succeed, and this optimisation won't work.
75                 # The perl code will later correctly check the lock,
76                 # so the right thing will still happen, though without
77                 # the benefit of this optimisation.
78                 $check_commit_hook=<<"EOF";
79         {
80                 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
81                 if (fd != -1) {
82                         if (flock(fd, LOCK_SH | LOCK_NB) != 0)
83                                 exit(0);
84                         close(fd);
85                 }
86         }
87 EOF
88         }
89         elsif ($config{cgi}) {
90                 # Avoid more than one ikiwiki cgi running at a time by
91                 # taking a cgi lock. Since ikiwiki uses several MB of
92                 # memory, a pile up of processes could cause thrashing
93                 # otherwise. The fd of the lock is stored in
94                 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
95                 $pre_exec=<<"EOF";
96         lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
97         if (lockfd != -1 && flock(lockfd, LOCK_EX) == 0) {
98                 char *fd_s=malloc(8);
99                 sprintf(fd_s, "%i", lockfd);
100                 setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
101         }
102 EOF
103         }
104
105         my $set_background_command='';
106         if (defined $config{wrapper_background_command} &&
107             length $config{wrapper_background_command}) {
108                 my $background_command=delete $config{wrapper_background_command};
109                 $set_background_command=~s/"/\\"/g;
110                 $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
111         }
112
113         $Data::Dumper::Indent=0; # no newlines
114         my $configstring=Data::Dumper->Dump([\%config], ['*config']);
115         $configstring=~s/\\/\\\\/g;
116         $configstring=~s/"/\\"/g;
117         $configstring=~s/\n/\\n/g;
118         
119         writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
120 /* A wrapper for ikiwiki, can be safely made suid. */
121 #include <stdio.h>
122 #include <sys/types.h>
123 #include <sys/stat.h>
124 #include <fcntl.h>
125 #include <unistd.h>
126 #include <stdlib.h>
127 #include <string.h>
128 #include <sys/file.h>
129
130 extern char **environ;
131 char *newenviron[$#envsave+6];
132 int i=0;
133
134 void addenv(char *var, char *val) {
135         char *s=malloc(strlen(var)+1+strlen(val)+1);
136         if (!s)
137                 perror("malloc");
138         sprintf(s, "%s=%s", var, val);
139         newenviron[i++]=s;
140 }
141
142 int main (int argc, char **argv) {
143         int lockfd=-1;
144         char *s;
145
146 $check_commit_hook
147 @wrapper_hooks
148 $envsave
149         newenviron[i++]="HOME=$ENV{HOME}";
150         newenviron[i++]="WRAPPED_OPTIONS=$configstring";
151
152 #ifdef __TINYC__
153         /* old tcc versions do not support modifying environ directly */
154         if (clearenv() != 0) {
155                 perror("clearenv");
156                 exit(1);
157         }
158         for (; i>0; i--)
159                 putenv(newenviron[i-1]);
160 #else
161         newenviron[i]=NULL;
162         environ=newenviron;
163 #endif
164
165         if (setregid(getegid(), -1) != 0 &&
166             setregid(getegid(), -1) != 0) {
167                 perror("failed to drop real gid");
168                 exit(1);
169         }
170         if (setreuid(geteuid(), -1) != 0 &&
171             setreuid(geteuid(), -1) != 0) {
172                 perror("failed to drop real uid");
173                 exit(1);
174         }
175
176 $pre_exec
177
178 $set_background_command
179 #ifdef BACKGROUND_COMMAND
180         if (lockfd != -1) {
181                 close(lockfd);
182         }
183
184         pid_t pid=fork();
185         if (pid == -1) {
186                 perror("fork");
187                 exit(1);
188         }
189         else if (pid == 0) {
190                 execl("$this", "$this", NULL);
191                 perror("exec $this");
192                 exit(1);                
193         }
194         else {
195                 waitpid(pid, NULL, 0);
196
197                 if (daemon(1, 0) == 0) {
198                         system(BACKGROUND_COMMAND);
199                         exit(0);
200                 }
201                 else {
202                         perror("daemon");
203                         exit(1);
204                 }
205         }
206 #else
207         execl("$this", "$this", NULL);
208         perror("exec $this");
209         exit(1);
210 #endif
211 }
212 EOF
213
214         my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
215         push @cc, possibly_foolish_untaint($ENV{CFLAGS}) if exists $ENV{CFLAGS};
216         if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
217                 #translators: The parameter is a C filename.
218                 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
219         }
220         unlink("$wrapper.c");
221         if (defined $config{wrappergroup}) {
222                 my $gid=(getgrnam($config{wrappergroup}))[2];
223                 if (! defined $gid) {
224                         error(sprintf("bad wrappergroup"));
225                 }
226                 if (! chown(-1, $gid, "$wrapper.new")) {
227                         error("chown $wrapper.new: $!");
228                 }
229         }
230         if (defined $config{wrappermode} &&
231             ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
232                 error("chmod $wrapper.new: $!");
233         }
234         if (! rename("$wrapper.new", $wrapper)) {
235                 error("rename $wrapper.new $wrapper: $!");
236         }
237         #translators: The parameter is a filename.
238         printf(gettext("successfully generated %s"), $wrapper);
239         print "\n";
240 }
241
242 1