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);
32 $config{srcdir}=File::Spec->rel2abs($config{srcdir});
33 $config{destdir}=File::Spec->rel2abs($config{destdir});
34 my $this=File::Spec->rel2abs($0);
36 error(sprintf(gettext("%s doesn't seem to be executable"), $this));
40 error(gettext("cannot create a wrapper that uses a setup file"));
42 my $wrapper=possibly_foolish_untaint($config{wrapper});
43 if (! defined $wrapper || ! length $wrapper) {
44 error(gettext("wrapper filename not specified"));
46 delete $config{wrapper};
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};
55 foreach my $var (@envsave) {
57 if ((s=getenv("$var")))
63 run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
65 my $check_commit_hook="";
67 if ($config{post_commit}) {
68 # Optimise checking !commit_hook_enabled() ,
69 # so that ikiwiki does not have to be started if the
72 # Note that perl's flock may be implemented using fcntl
73 # or lockf on some systems. If so, and if there is no
74 # interop between the locking systems, the true C flock will
75 # always succeed, and this optimisation won't work.
76 # The perl code will later correctly check the lock,
77 # so the right thing will still happen, though without
78 # the benefit of this optimisation.
79 $check_commit_hook=<<"EOF";
81 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
83 if (flock(fd, LOCK_SH | LOCK_NB) != 0)
90 elsif ($config{cgi}) {
91 # Avoid more than one ikiwiki cgi running at a time by
92 # taking a cgi lock. Since ikiwiki uses several MB of
93 # memory, a pile up of processes could cause thrashing
94 # otherwise. The fd of the lock is stored in
95 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
97 # A lot of cgi wrapper processes can potentially build
98 # up and clog an otherwise unloaded web server. To
99 # partially avoid this, when a GET comes in and the lock
100 # is already held, rather than blocking a html page is
101 # constructed that retries. This is enabled by setting
102 # cgi_overload_delay.
103 if (defined $config{cgi_overload_delay} &&
104 $config{cgi_overload_delay} =~/^[0-9]+/) {
105 my $i=int($config{cgi_overload_delay});
106 $pre_exec.="#define CGI_OVERLOAD_DELAY $i\n"
110 lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
112 #ifdef CGI_OVERLOAD_DELAY
113 char *request_method = getenv("REQUEST_METHOD");
114 if (request_method && strcmp(request_method, "GET") == 0) {
115 if (lockf(lockfd, F_TLOCK, 0) == 0) {
116 set_cgilock_fd(lockfd);
119 printf("Content-Type: text/html\\nRefresh: %i; URL=%s\\n\\n<html><head><title>please wait...</title><head><body><p>Please wait ...</p></body></html>",
121 getenv("REQUEST_URI"));
125 else if (lockf(lockfd, F_LOCK, 0) == 0) {
126 set_cgilock_fd(lockfd);
129 if (lockf(lockfd, F_LOCK, 0) == 0) {
130 set_cgilock_fd(lockfd);
137 my $set_background_command='';
138 if (defined $config{wrapper_background_command} &&
139 length $config{wrapper_background_command}) {
140 my $background_command=delete $config{wrapper_background_command};
141 $set_background_command=~s/"/\\"/g;
142 $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
145 $Data::Dumper::Indent=0; # no newlines
146 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
147 $configstring=~s/\\/\\\\/g;
148 $configstring=~s/"/\\"/g;
149 $configstring=~s/\n/\\n/g;
151 writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
152 /* A wrapper for ikiwiki, can be safely made suid. */
154 #include <sys/types.h>
155 #include <sys/stat.h>
160 #include <sys/file.h>
162 extern char **environ;
163 char *newenviron[$#envsave+7];
166 void addenv(char *var, char *val) {
167 char *s=malloc(strlen(var)+1+strlen(val)+1);
170 sprintf(s, "%s=%s", var, val);
174 set_cgilock_fd (int lockfd) {
175 char *fd_s=malloc(8);
176 sprintf(fd_s, "%i", lockfd);
177 setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
180 int main (int argc, char **argv) {
187 newenviron[i++]="HOME=$ENV{HOME}";
188 newenviron[i++]="PATH=$ENV{PATH}";
189 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
192 /* old tcc versions do not support modifying environ directly */
193 if (clearenv() != 0) {
198 putenv(newenviron[i-1]);
204 if (setregid(getegid(), -1) != 0 &&
205 setregid(getegid(), -1) != 0) {
206 perror("failed to drop real gid");
209 if (setreuid(geteuid(), -1) != 0 &&
210 setreuid(geteuid(), -1) != 0) {
211 perror("failed to drop real uid");
217 $set_background_command
218 #ifdef BACKGROUND_COMMAND
229 execl("$this", "$this", NULL);
230 perror("exec $this");
234 waitpid(pid, NULL, 0);
236 if (daemon(1, 0) == 0) {
237 system(BACKGROUND_COMMAND);
246 execl("$this", "$this", NULL);
247 perror("exec $this");
253 my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
254 push @cc, split(' ', possibly_foolish_untaint($ENV{CFLAGS})) if exists $ENV{CFLAGS};
255 if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
256 #translators: The parameter is a C filename.
257 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
259 unlink("$wrapper.c");
260 if (defined $config{wrappergroup}) {
261 my $gid=(getgrnam($config{wrappergroup}))[2];
262 if (! defined $gid) {
263 error(sprintf("bad wrappergroup"));
265 if (! chown(-1, $gid, "$wrapper.new")) {
266 error("chown $wrapper.new: $!");
269 if (defined $config{wrappermode} &&
270 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
271 error("chmod $wrapper.new: $!");
273 if (! rename("$wrapper.new", $wrapper)) {
274 error("rename $wrapper.new $wrapper: $!");
276 #translators: The parameter is a filename.
277 debug(sprintf(gettext("successfully generated %s"), $wrapper));