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};
55 my $envsize=$#envsave;
57 foreach my $var (@envsave) {
59 if ((s=getenv("$var")))
63 if (ref $config{ENV} eq 'HASH') {
64 foreach my $key (keys %{$config{ENV}}) {
65 my $val=$config{ENV}{$key};
66 $val =~ s/([\\"])/\\$1/g;
69 addenv("$key", "$val");
76 run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
78 my $check_commit_hook="";
80 if ($config{post_commit}) {
81 # Optimise checking !commit_hook_enabled() ,
82 # so that ikiwiki does not have to be started if the
85 # Note that perl's flock may be implemented using fcntl
86 # or lockf on some systems. If so, and if there is no
87 # interop between the locking systems, the true C flock will
88 # always succeed, and this optimisation won't work.
89 # The perl code will later correctly check the lock,
90 # so the right thing will still happen, though without
91 # the benefit of this optimisation.
92 $check_commit_hook=<<"EOF";
94 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
96 if (flock(fd, LOCK_SH | LOCK_NB) != 0)
103 elsif ($config{cgi}) {
104 # Avoid more than one ikiwiki cgi running at a time by
105 # taking a cgi lock. Since ikiwiki uses several MB of
106 # memory, a pile up of processes could cause thrashing
107 # otherwise. The fd of the lock is stored in
108 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
110 # A lot of cgi wrapper processes can potentially build
111 # up and clog an otherwise unloaded web server. To
112 # partially avoid this, when a GET comes in and the lock
113 # is already held, rather than blocking a html page is
114 # constructed that retries. This is enabled by setting
115 # cgi_overload_delay.
116 if (defined $config{cgi_overload_delay} &&
117 $config{cgi_overload_delay} =~/^[0-9]+/) {
118 my $i=int($config{cgi_overload_delay});
119 $pre_exec.="#define CGI_OVERLOAD_DELAY $i\n"
121 my $msg=gettext("Please wait");
123 $pre_exec.='#define CGI_PLEASE_WAIT_TITLE "'.$msg."\"\n";
124 if (defined $config{cgi_overload_message} && length $config{cgi_overload_message}) {
125 $msg=$config{cgi_overload_message};
128 $pre_exec.='#define CGI_PLEASE_WAIT_BODY "'.$msg."\"\n";
131 lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
133 #ifdef CGI_OVERLOAD_DELAY
134 char *request_method = getenv("REQUEST_METHOD");
135 if (request_method && strcmp(request_method, "GET") == 0) {
136 if (lockf(lockfd, F_TLOCK, 0) == 0) {
137 set_cgilock_fd(lockfd);
140 printf("Content-Type: text/html\\nRefresh: %i; URL=%s\\n\\n<html><head><title>%s</title><head><body><p>%s</p></body></html>",
142 getenv("REQUEST_URI"),
143 CGI_PLEASE_WAIT_TITLE,
144 CGI_PLEASE_WAIT_BODY);
148 else if (lockf(lockfd, F_LOCK, 0) == 0) {
149 set_cgilock_fd(lockfd);
152 if (lockf(lockfd, F_LOCK, 0) == 0) {
153 set_cgilock_fd(lockfd);
160 my $set_background_command='';
161 if (defined $config{wrapper_background_command} &&
162 length $config{wrapper_background_command}) {
163 my $background_command=delete $config{wrapper_background_command};
164 $set_background_command=~s/"/\\"/g;
165 $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
168 $Data::Dumper::Indent=0; # no newlines
169 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
170 $configstring=~s/\\/\\\\/g;
171 $configstring=~s/"/\\"/g;
172 $configstring=~s/\n/\\n/g;
174 writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
175 /* A wrapper for ikiwiki, can be safely made suid. */
177 #include <sys/types.h>
178 #include <sys/stat.h>
183 #include <sys/file.h>
185 extern char **environ;
186 char *newenviron[$envsize+7];
189 void addenv(char *var, char *val) {
190 char *s=malloc(strlen(var)+1+strlen(val)+1);
193 sprintf(s, "%s=%s", var, val);
197 void set_cgilock_fd (int lockfd) {
198 char *fd_s=malloc(8);
199 sprintf(fd_s, "%i", lockfd);
200 setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
203 int main (int argc, char **argv) {
210 newenviron[i++]="HOME=$ENV{HOME}";
211 newenviron[i++]="PATH=$ENV{PATH}";
212 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
215 /* old tcc versions do not support modifying environ directly */
216 if (clearenv() != 0) {
221 putenv(newenviron[i-1]);
227 if (setregid(getegid(), -1) != 0 &&
228 setregid(getegid(), -1) != 0) {
229 perror("failed to drop real gid");
232 if (setreuid(geteuid(), -1) != 0 &&
233 setreuid(geteuid(), -1) != 0) {
234 perror("failed to drop real uid");
240 $set_background_command
241 #ifdef BACKGROUND_COMMAND
252 execl("$this", "$this", NULL);
253 perror("exec $this");
257 waitpid(pid, NULL, 0);
259 if (daemon(1, 0) == 0) {
260 system(BACKGROUND_COMMAND);
269 execl("$this", "$this", NULL);
270 perror("exec $this");
276 my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
277 push @cc, split(' ', possibly_foolish_untaint($ENV{CFLAGS})) if exists $ENV{CFLAGS};
278 if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
279 #translators: The parameter is a C filename.
280 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
282 unlink("$wrapper.c");
283 if (defined $config{wrappergroup}) {
284 my $gid=(getgrnam($config{wrappergroup}))[2];
285 if (! defined $gid) {
286 error(sprintf("bad wrappergroup"));
288 if (! chown(-1, $gid, "$wrapper.new")) {
289 error("chown $wrapper.new: $!");
292 if (defined $config{wrappermode} &&
293 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
294 error("chmod $wrapper.new: $!");
296 if (! rename("$wrapper.new", $wrapper)) {
297 error("rename $wrapper.new $wrapper: $!");
299 #translators: The parameter is a filename.
300 debug(sprintf(gettext("successfully generated %s"), $wrapper));