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 if ($config{test_receive}) {
43 require IkiWiki::Receive;
44 $test_receive=IkiWiki::Receive::gen_wrapper();
47 my $check_cvs_add_dir="";
48 # XXX conditionalize on $config{rcs} eq 'cvs'
49 $check_cvs_add_dir=<<"EOF";
52 for (j = 1; j < argc; j++)
53 if (strcmp(argv[j], "New directory") == 0)
58 my $check_commit_hook="";
60 if ($config{post_commit}) {
61 # Optimise checking !commit_hook_enabled() ,
62 # so that ikiwiki does not have to be started if the
65 # Note that perl's flock may be implemented using fcntl
66 # or lockf on some systems. If so, and if there is no
67 # interop between the locking systems, the true C flock will
68 # always succeed, and this optimisation won't work.
69 # The perl code will later correctly check the lock,
70 # so the right thing will still happen, though without
71 # the benefit of this optimisation.
72 $check_commit_hook=<<"EOF";
74 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
76 if (flock(fd, LOCK_SH | LOCK_NB) != 0)
83 elsif ($config{cgi}) {
84 # Avoid more than one ikiwiki cgi running at a time by
85 # taking a cgi lock. Since ikiwiki uses several MB of
86 # memory, a pile up of processes could cause thrashing
87 # otherwise. The fd of the lock is stored in
88 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
91 int fd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
92 if (fd != -1 && flock(fd, LOCK_EX) == 0) {
94 asprintf(&fd_s, "%i", fd);
95 setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
101 $Data::Dumper::Indent=0; # no newlines
102 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
103 $configstring=~s/\\/\\\\/g;
104 $configstring=~s/"/\\"/g;
105 $configstring=~s/\n/\\n/g;
107 writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
108 /* A wrapper for ikiwiki, can be safely made suid. */
110 #include <sys/types.h>
111 #include <sys/stat.h>
116 #include <sys/file.h>
118 extern char **environ;
119 char *newenviron[$#envsave+6];
122 addenv(char *var, char *val) {
123 char *s=malloc(strlen(var)+1+strlen(val)+1);
126 sprintf(s, "%s=%s", var, val);
130 int main (int argc, char **argv) {
137 newenviron[i++]="HOME=$ENV{HOME}";
138 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
142 if (setregid(getegid(), -1) != 0 &&
143 setregid(getegid(), -1) != 0) {
144 perror("failed to drop real gid");
147 if (setreuid(geteuid(), -1) != 0 &&
148 setreuid(geteuid(), -1) != 0) {
149 perror("failed to drop real uid");
154 execl("$this", "$this", NULL);
155 perror("exec $this");
161 my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
162 if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
163 #translators: The parameter is a C filename.
164 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
166 unlink("$wrapper.c");
167 if (defined $config{wrappergroup}) {
168 my $gid=(getgrnam($config{wrappergroup}))[2];
169 if (! defined $gid) {
170 error(sprintf("bad wrappergroup"));
172 if (! chown(-1, $gid, "$wrapper.new")) {
173 error("chown $wrapper.new: $!");
176 if (defined $config{wrappermode} &&
177 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
178 error("chmod $wrapper.new: $!");
180 if (! rename("$wrapper.new", $wrapper)) {
181 error("rename $wrapper.new $wrapper: $!");
183 #translators: The parameter is a filename.
184 printf(gettext("successfully generated %s"), $wrapper);