response
[ikiwiki.git] / IkiWiki / Wrapper.pm
blob84cc7540bbfa5e63b8d9d85d21cc0b0d63bbb478
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;
26 gen_wrapper();
28 %config=(%origconfig);
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));
39 if ($config{setup}) {
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};
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
53 REDIRECT_URL} if $config{cgi};
54 my $envsave="";
55 foreach my $var (@envsave) {
56 $envsave.=<<"EOF";
57 if ((s=getenv("$var")))
58 addenv("$var", s);
59 EOF
62 my @wrapper_hooks;
63 run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
65 my $check_commit_hook="";
66 my $pre_exec="";
67 if ($config{post_commit}) {
68 # Optimise checking !commit_hook_enabled() ,
69 # so that ikiwiki does not have to be started if the
70 # hook is disabled.
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);
82 if (fd != -1) {
83 if (flock(fd, LOCK_SH | LOCK_NB) != 0)
84 exit(0);
85 close(fd);
88 EOF
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.
96 $pre_exec=<<"EOF";
97 lockfd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
98 if (lockfd != -1 && flock(lockfd, LOCK_EX) == 0) {
99 char *fd_s=malloc(8);
100 sprintf(fd_s, "%i", lockfd);
101 setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
106 my $set_background_command='';
107 if (defined $config{wrapper_background_command} &&
108 length $config{wrapper_background_command}) {
109 my $background_command=delete $config{wrapper_background_command};
110 $set_background_command=~s/"/\\"/g;
111 $set_background_command='#define BACKGROUND_COMMAND "'.$background_command.'"';
114 $Data::Dumper::Indent=0; # no newlines
115 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
116 $configstring=~s/\\/\\\\/g;
117 $configstring=~s/"/\\"/g;
118 $configstring=~s/\n/\\n/g;
120 writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
121 /* A wrapper for ikiwiki, can be safely made suid. */
122 #include <stdio.h>
123 #include <sys/types.h>
124 #include <sys/stat.h>
125 #include <fcntl.h>
126 #include <unistd.h>
127 #include <stdlib.h>
128 #include <string.h>
129 #include <sys/file.h>
131 extern char **environ;
132 char *newenviron[$#envsave+7];
133 int i=0;
135 void addenv(char *var, char *val) {
136 char *s=malloc(strlen(var)+1+strlen(val)+1);
137 if (!s)
138 perror("malloc");
139 sprintf(s, "%s=%s", var, val);
140 newenviron[i++]=s;
143 int main (int argc, char **argv) {
144 int lockfd=-1;
145 char *s;
147 $check_commit_hook
148 @wrapper_hooks
149 $envsave
150 newenviron[i++]="HOME=$ENV{HOME}";
151 newenviron[i++]="PATH=$ENV{PATH}";
152 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
154 #ifdef __TINYC__
155 /* old tcc versions do not support modifying environ directly */
156 if (clearenv() != 0) {
157 perror("clearenv");
158 exit(1);
160 for (; i>0; i--)
161 putenv(newenviron[i-1]);
162 #else
163 newenviron[i]=NULL;
164 environ=newenviron;
165 #endif
167 if (setregid(getegid(), -1) != 0 &&
168 setregid(getegid(), -1) != 0) {
169 perror("failed to drop real gid");
170 exit(1);
172 if (setreuid(geteuid(), -1) != 0 &&
173 setreuid(geteuid(), -1) != 0) {
174 perror("failed to drop real uid");
175 exit(1);
178 $pre_exec
180 $set_background_command
181 #ifdef BACKGROUND_COMMAND
182 if (lockfd != -1) {
183 close(lockfd);
186 pid_t pid=fork();
187 if (pid == -1) {
188 perror("fork");
189 exit(1);
191 else if (pid == 0) {
192 execl("$this", "$this", NULL);
193 perror("exec $this");
194 exit(1);
196 else {
197 waitpid(pid, NULL, 0);
199 if (daemon(1, 0) == 0) {
200 system(BACKGROUND_COMMAND);
201 exit(0);
203 else {
204 perror("daemon");
205 exit(1);
208 #else
209 execl("$this", "$this", NULL);
210 perror("exec $this");
211 exit(1);
212 #endif
216 my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
217 push @cc, possibly_foolish_untaint($ENV{CFLAGS}) if exists $ENV{CFLAGS};
218 if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
219 #translators: The parameter is a C filename.
220 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
222 unlink("$wrapper.c");
223 if (defined $config{wrappergroup}) {
224 my $gid=(getgrnam($config{wrappergroup}))[2];
225 if (! defined $gid) {
226 error(sprintf("bad wrappergroup"));
228 if (! chown(-1, $gid, "$wrapper.new")) {
229 error("chown $wrapper.new: $!");
232 if (defined $config{wrappermode} &&
233 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
234 error("chmod $wrapper.new: $!");
236 if (! rename("$wrapper.new", $wrapper)) {
237 error("rename $wrapper.new $wrapper: $!");
239 #translators: The parameter is a filename.
240 printf(gettext("successfully generated %s"), $wrapper);
241 print "\n";