11 sub gen_wrapper () { #{{{
12 $config{srcdir}=abs_path($config{srcdir});
13 $config{destdir}=abs_path($config{destdir});
14 my $this=abs_path($0);
16 error("$this doesn't seem to be executable");
20 error("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("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} if $config{cgi};
33 foreach my $var (@envsave) {
35 if ((s=getenv("$var")))
36 asprintf(&newenviron[i++], "%s=%s", "$var", s);
39 if ($config{rcs} eq "svn" && $config{notify}) {
40 # Support running directly as hooks/post-commit by passing
41 # $2 in REV in the environment.
44 asprintf(&newenviron[i++], "REV=%s", argv[2]);
45 else if ((s=getenv("REV")))
46 asprintf(&newenviron[i++], "%s=%s", "REV", s);
50 $Data::Dumper::Indent=0; # no newlines
51 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
52 $configstring=~s/\\/\\\\/g;
53 $configstring=~s/"/\\"/g;
54 $configstring=~s/\n/\\\n/g;
56 open(OUT, ">$wrapper.c") || error("failed to write $wrapper.c: $!");;
58 /* A wrapper for ikiwiki, can be safely made suid. */
61 #include <sys/types.h>
66 extern char **environ;
68 int main (int argc, char **argv) {
69 /* Sanitize environment. */
71 char *newenviron[$#envsave+5];
74 newenviron[i++]="HOME=$ENV{HOME}";
75 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
79 if (setregid(getegid(), -1) != 0 || setreuid(geteuid(), -1) != 0) {
80 perror("failed to drop real uid/gid");
84 execl("$this", "$this", NULL);
85 perror("failed to run $this");
90 if (system("gcc", "$wrapper.c", "-o", $wrapper) != 0) {
91 error("failed to compile $wrapper.c");
94 if (defined $config{wrappermode} &&
95 ! chmod(oct($config{wrappermode}), $wrapper)) {
96 error("chmod $wrapper: $!");
98 print "successfully generated $wrapper\n";