12 $config{srcdir}=File::Spec->rel2abs($config{srcdir});
13 $config{destdir}=File::Spec->rel2abs($config{destdir});
14 my $this=File::Spec->rel2abs($0);
16 error(sprintf(gettext("%s doesn't seem to be executable"), $this));
20 error(gettext("cannot create a wrapper that uses a setup file"));
22 my $wrapper=possibly_foolish_untaint($config{wrapper});
23 if (! defined $wrapper || ! length $wrapper) {
24 error(gettext("wrapper filename not specified"));
26 delete $config{wrapper};
29 push @envsave, qw{REMOTE_ADDR QUERY_STRING REQUEST_METHOD REQUEST_URI
30 CONTENT_TYPE CONTENT_LENGTH GATEWAY_INTERFACE
31 HTTP_COOKIE REMOTE_USER HTTPS REDIRECT_STATUS
32 REDIRECT_URL} if $config{cgi};
34 foreach my $var (@envsave) {
36 if ((s=getenv("$var")))
42 run_hooks(genwrapper => sub { push @wrapper_hooks, shift->() });
44 my $check_commit_hook="";
46 if ($config{post_commit}) {
47 # Optimise checking !commit_hook_enabled() ,
48 # so that ikiwiki does not have to be started if the
51 # Note that perl's flock may be implemented using fcntl
52 # or lockf on some systems. If so, and if there is no
53 # interop between the locking systems, the true C flock will
54 # always succeed, and this optimisation won't work.
55 # The perl code will later correctly check the lock,
56 # so the right thing will still happen, though without
57 # the benefit of this optimisation.
58 $check_commit_hook=<<"EOF";
60 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
62 if (flock(fd, LOCK_SH | LOCK_NB) != 0)
69 elsif ($config{cgi}) {
70 # Avoid more than one ikiwiki cgi running at a time by
71 # taking a cgi lock. Since ikiwiki uses several MB of
72 # memory, a pile up of processes could cause thrashing
73 # otherwise. The fd of the lock is stored in
74 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
77 int fd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
78 if (fd != -1 && flock(fd, LOCK_EX) == 0) {
80 sprintf(fd_s, "%i", fd);
81 setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
87 $Data::Dumper::Indent=0; # no newlines
88 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
89 $configstring=~s/\\/\\\\/g;
90 $configstring=~s/"/\\"/g;
91 $configstring=~s/\n/\\n/g;
93 writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
94 /* A wrapper for ikiwiki, can be safely made suid. */
96 #include <sys/types.h>
102 #include <sys/file.h>
104 extern char **environ;
105 char *newenviron[$#envsave+6];
108 void addenv(char *var, char *val) {
109 char *s=malloc(strlen(var)+1+strlen(val)+1);
112 sprintf(s, "%s=%s", var, val);
116 int main (int argc, char **argv) {
122 newenviron[i++]="HOME=$ENV{HOME}";
123 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
126 /* old tcc versions do not support modifying environ directly */
127 if (clearenv() != 0) {
132 putenv(newenviron[i-1]);
138 if (setregid(getegid(), -1) != 0 &&
139 setregid(getegid(), -1) != 0) {
140 perror("failed to drop real gid");
143 if (setreuid(geteuid(), -1) != 0 &&
144 setreuid(geteuid(), -1) != 0) {
145 perror("failed to drop real uid");
150 execl("$this", "$this", NULL);
151 perror("exec $this");
156 my @cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
157 push @cc, possibly_foolish_untaint($ENV{CFLAGS}) if exists $ENV{CFLAGS};
158 if (system(@cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
159 #translators: The parameter is a C filename.
160 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
162 unlink("$wrapper.c");
163 if (defined $config{wrappergroup}) {
164 my $gid=(getgrnam($config{wrappergroup}))[2];
165 if (! defined $gid) {
166 error(sprintf("bad wrappergroup"));
168 if (! chown(-1, $gid, "$wrapper.new")) {
169 error("chown $wrapper.new: $!");
172 if (defined $config{wrappermode} &&
173 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
174 error("chmod $wrapper.new: $!");
176 if (! rename("$wrapper.new", $wrapper)) {
177 error("rename $wrapper.new $wrapper: $!");
179 #translators: The parameter is a filename.
180 printf(gettext("successfully generated %s"), $wrapper);