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_args=" return 0;";
48 run_hooks(wrapperargcheck => sub { $check_args = shift->(); });
50 my $check_commit_hook="";
52 if ($config{post_commit}) {
53 # Optimise checking !commit_hook_enabled() ,
54 # so that ikiwiki does not have to be started if the
57 # Note that perl's flock may be implemented using fcntl
58 # or lockf on some systems. If so, and if there is no
59 # interop between the locking systems, the true C flock will
60 # always succeed, and this optimisation won't work.
61 # The perl code will later correctly check the lock,
62 # so the right thing will still happen, though without
63 # the benefit of this optimisation.
64 $check_commit_hook=<<"EOF";
66 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
68 if (flock(fd, LOCK_SH | LOCK_NB) != 0)
75 elsif ($config{cgi}) {
76 # Avoid more than one ikiwiki cgi running at a time by
77 # taking a cgi lock. Since ikiwiki uses several MB of
78 # memory, a pile up of processes could cause thrashing
79 # otherwise. The fd of the lock is stored in
80 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
83 int fd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
84 if (fd != -1 && flock(fd, LOCK_EX) == 0) {
86 asprintf(&fd_s, "%i", fd);
87 setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
93 $Data::Dumper::Indent=0; # no newlines
94 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
95 $configstring=~s/\\/\\\\/g;
96 $configstring=~s/"/\\"/g;
97 $configstring=~s/\n/\\n/g;
99 writefile(basename("$wrapper.c"), dirname($wrapper), <<"EOF");
100 /* A wrapper for ikiwiki, can be safely made suid. */
102 #include <sys/types.h>
103 #include <sys/stat.h>
108 #include <sys/file.h>
110 extern char **environ;
111 char *newenviron[$#envsave+6];
114 addenv(char *var, char *val) {
115 char *s=malloc(strlen(var)+1+strlen(val)+1);
118 sprintf(s, "%s=%s", var, val);
122 int checkargs(int argc, char **argv) {
126 int main (int argc, char **argv) {
129 if (!checkargs(argc, argv))
135 newenviron[i++]="HOME=$ENV{HOME}";
136 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
140 if (setregid(getegid(), -1) != 0 &&
141 setregid(getegid(), -1) != 0) {
142 perror("failed to drop real gid");
145 if (setreuid(geteuid(), -1) != 0 &&
146 setreuid(geteuid(), -1) != 0) {
147 perror("failed to drop real uid");
152 execl("$this", "$this", NULL);
153 perror("exec $this");
159 my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
160 if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
161 #translators: The parameter is a C filename.
162 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
164 unlink("$wrapper.c");
165 if (defined $config{wrappergroup}) {
166 my $gid=(getgrnam($config{wrappergroup}))[2];
167 if (! defined $gid) {
168 error(sprintf("bad wrappergroup"));
170 if (! chown(-1, $gid, "$wrapper.new")) {
171 error("chown $wrapper.new: $!");
174 if (defined $config{wrappermode} &&
175 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
176 error("chmod $wrapper.new: $!");
178 if (! rename("$wrapper.new", $wrapper)) {
179 error("rename $wrapper.new $wrapper: $!");
181 #translators: The parameter is a filename.
182 printf(gettext("successfully generated %s"), $wrapper);