11 sub gen_wrapper () { #{{{
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} if $config{cgi};
33 foreach my $var (@envsave) {
35 if ((s=getenv("$var")))
41 if ($config{test_receive}) {
42 require IkiWiki::Receive;
43 $test_receive=IkiWiki::Receive::gen_wrapper();
46 my $check_commit_hook="";
48 if ($config{post_commit}) {
49 # Optimise checking !commit_hook_enabled() ,
50 # so that ikiwiki does not have to be started if the
53 # Note that perl's flock may be implemented using fcntl
54 # or lockf on some systems. If so, and if there is no
55 # interop between the locking systems, the true C flock will
56 # always succeed, and this optimisation won't work.
57 # The perl code will later correctly check the lock,
58 # so the right thing will still happen, though without
59 # the benefit of this optimisation.
60 $check_commit_hook=<<"EOF";
62 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
64 if (flock(fd, LOCK_SH | LOCK_NB) != 0)
71 elsif ($config{cgi}) {
72 # Avoid more than one ikiwiki cgi running at a time by
73 # taking a cgi lock. Since ikiwiki uses several MB of
74 # memory, a pile up of processes could cause thrashing
78 int fd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
85 $Data::Dumper::Indent=0; # no newlines
86 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
87 $configstring=~s/\\/\\\\/g;
88 $configstring=~s/"/\\"/g;
89 $configstring=~s/\n/\\n/g;
91 #translators: The first parameter is a filename, and the second is
92 #translators: a (probably not translated) error message.
93 open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
95 /* A wrapper for ikiwiki, can be safely made suid. */
97 #include <sys/types.h>
103 #include <sys/file.h>
105 extern char **environ;
106 char *newenviron[$#envsave+6];
109 addenv(char *var, char *val) {
110 char *s=malloc(strlen(var)+1+strlen(val)+1);
113 sprintf(s, "%s=%s", var, val);
117 int main (int argc, char **argv) {
123 newenviron[i++]="HOME=$ENV{HOME}";
124 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
128 if (setregid(getegid(), -1) != 0 &&
129 setregid(getegid(), -1) != 0) {
130 perror("failed to drop real gid");
133 if (setreuid(geteuid(), -1) != 0 &&
134 setreuid(geteuid(), -1) != 0) {
135 perror("failed to drop real uid");
140 execl("$this", "$this", NULL);
141 perror("exec $this");
147 my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
148 if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
149 #translators: The parameter is a C filename.
150 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
152 unlink("$wrapper.c");
153 if (defined $config{wrappergroup}) {
154 my $gid=(getgrnam($config{wrappergroup}))[2];
155 if (! defined $gid) {
156 error(sprintf("bad wrappergroup"));
158 if (! chown(-1, $gid, "$wrapper.new")) {
159 error("chown $wrapper.new: $!");
162 if (defined $config{wrappermode} &&
163 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
164 error("chmod $wrapper.new: $!");
166 if (! rename("$wrapper.new", $wrapper)) {
167 error("rename $wrapper.new $wrapper: $!");
169 #translators: The parameter is a filename.
170 printf(gettext("successfully generated %s"), $wrapper);