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)};
22 if (! $config{cgi} && ! $config{post_commit} &&
23 ! $config{test_receive}) {
24 $config{post_commit}=1;
28 %config=(%origconfig);
31 our $program_to_wrap = $0;
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);
37 error(sprintf(gettext("%s doesn't seem to be executable"), $this));
41 error(gettext("cannot create a wrapper that uses a setup file"));
43 my $wrapper=possibly_foolish_untaint($config{wrapper});
44 if (! defined $wrapper || ! length $wrapper) {
45 error(gettext("wrapper filename not specified"));
47 delete $config{wrapper};
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};
56 foreach my $var (@envsave) {
58 if ((s=getenv("$var")))
62 if (ref $config{ENV} eq 'HASH') {
63 foreach my $key (keys %{$config{ENV}}) {
64 my $val=$config{ENV}{$key};
65 utf8::encode($val) if utf8::is_utf8($val);
66 $val =~ s/([^A-Za-z0-9])/sprintf '""\\x%02x""', ord($1)/ge;
68 addenv("$key", "$val");
75 run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
77 my $check_commit_hook="";
79 if ($config{post_commit}) {
80 # Optimise checking !commit_hook_enabled() ,
81 # so that ikiwiki does not have to be started if the
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";
93 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
95 if (flock(fd, LOCK_SH | LOCK_NB) != 0)
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.
109 # A lot of cgi wrapper processes can potentially build
110 # up and clog an otherwise unloaded web server. To
111 # partially avoid this, when a GET comes in and the lock
112 # is already held, rather than blocking a html page is
113 # constructed that retries. This is enabled by setting
114 # cgi_overload_delay.
115 if (defined $config{cgi_overload_delay} &&
116 $config{cgi_overload_delay} =~/^[0-9]+/) {
117 my $i=int($config{cgi_overload_delay});
118 $pre_exec.="#define CGI_OVERLOAD_DELAY $i\n"
120 my $msg=gettext("Please wait");
122 $pre_exec.='#define CGI_PLEASE_WAIT_TITLE "'.$msg."\"\n";
123 if (defined $config{cgi_overload_message} && length $config{cgi_overload_message}) {
124 $msg=$config{cgi_overload_message};
127 $pre_exec.='#define CGI_PLEASE_WAIT_BODY "'.$msg."\"\n";
130 lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
132 #ifdef CGI_OVERLOAD_DELAY
133 char *request_method = getenv("REQUEST_METHOD");
134 if (request_method && strcmp(request_method, "GET") == 0) {
135 if (lockf(lockfd, F_TLOCK, 0) == 0) {
136 set_cgilock_fd(lockfd);
139 printf("Content-Type: text/html\\nRefresh: %i; URL=%s\\n\\n<html><head><title>%s</title><head><body><p>%s</p></body></html>",
141 getenv("REQUEST_URI"),
142 CGI_PLEASE_WAIT_TITLE,
143 CGI_PLEASE_WAIT_BODY);
147 else if (lockf(lockfd, F_LOCK, 0) == 0) {
148 set_cgilock_fd(lockfd);
151 if (lockf(lockfd, F_LOCK, 0) == 0) {
152 set_cgilock_fd(lockfd);
159 my $set_background_command='';
160 if (defined $config{wrapper_background_command} &&
161 length $config{wrapper_background_command}) {
162 my $background_command=delete $config{wrapper_background_command};
163 $set_background_command=~s/"/\\"/g;
164 $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
167 $Data::Dumper::Indent=0; # no newlines
168 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
169 $configstring=~s/\\/\\\\/g;
170 $configstring=~s/"/\\"/g;
171 $configstring=~s/\n/\\n/g;
173 writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
174 /* A wrapper for ikiwiki, can be safely made suid. */
176 #include <sys/types.h>
177 #include <sys/stat.h>
182 #include <sys/file.h>
184 extern char **environ;
186 /* Array of length newenvironlen+1 (+1 for NULL) */
187 char **newenviron=NULL;
189 void addenv(char *var, char *val) {
193 newenviron=realloc(newenviron, (newenvironlen+2) * sizeof(char *));
196 newenviron=calloc(newenvironlen+2, sizeof(char *));
204 s=malloc(strlen(var)+1+strlen(val)+1);
210 sprintf(s, "%s=%s", var, val);
211 newenviron[newenvironlen++]=s;
215 void set_cgilock_fd (int lockfd) {
217 sprintf(fd_s, "%i", lockfd);
218 if (setenv("IKIWIKI_CGILOCK_FD", fd_s, 1) != 0) {
224 int main (int argc, char **argv) {
231 addenv("HOME", "$ENV{HOME}");
232 addenv("PATH", "$ENV{PATH}");
233 addenv("WRAPPED_OPTIONS", "$configstring");
236 /* old tcc versions do not support modifying environ directly */
237 if (clearenv() != 0) {
241 for (; newenvironlen>0; newenvironlen--)
242 putenv(newenviron[newenvironlen-1]);
244 newenviron[newenvironlen]=NULL;
248 if (setregid(getegid(), -1) != 0 &&
249 setregid(getegid(), -1) != 0) {
250 perror("failed to drop real gid");
253 if (setreuid(geteuid(), -1) != 0 &&
254 setreuid(geteuid(), -1) != 0) {
255 perror("failed to drop real uid");
261 $set_background_command
262 #ifdef BACKGROUND_COMMAND
273 execl("$this", "$this", NULL);
274 perror("exec $this");
278 waitpid(pid, NULL, 0);
280 if (daemon(1, 0) == 0) {
281 system(BACKGROUND_COMMAND);
290 execl("$this", "$this", NULL);
291 perror("exec $this");
297 my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
298 push @cc, split(' ', possibly_foolish_untaint($ENV{CFLAGS})) if exists $ENV{CFLAGS};
299 if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
300 #translators: The parameter is a C filename.
301 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
303 unlink("$wrapper.c");
304 if (defined $config{wrappergroup}) {
305 my $gid=(getgrnam($config{wrappergroup}))[2];
306 if (! defined $gid) {
307 error(sprintf("bad wrappergroup"));
309 if (! chown(-1, $gid, "$wrapper.new")) {
310 error("chown $wrapper.new: $!");
313 if (defined $config{wrappermode} &&
314 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
315 error("chmod $wrapper.new: $!");
317 if (! rename("$wrapper.new", $wrapper)) {
318 error("rename $wrapper.new $wrapper: $!");
320 #translators: The parameter is a filename.
321 debug(sprintf(gettext("successfully generated %s"), $wrapper));