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