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(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} if $config{cgi};
33 foreach my $var (@envsave) {
35 if ((s=getenv("$var")))
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 addenv("REV", argv[2]);
45 else if ((s=getenv("REV")))
49 if ($config{rcs} eq "tla" && $config{notify}) {
51 if ((s=getenv("ARCH_VERSION")))
52 addenv("ARCH_VERSION", s);
56 $Data::Dumper::Indent=0; # no newlines
57 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
58 $configstring=~s/\\/\\\\/g;
59 $configstring=~s/"/\\"/g;
60 $configstring=~s/\n/\\n/g;
62 #translators: The first parameter is a filename, and the second is
63 #translators: a (probably not translated) error message.
64 open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
66 /* A wrapper for ikiwiki, can be safely made suid. */
68 #include <sys/types.h>
73 extern char **environ;
74 char *newenviron[$#envsave+5];
77 addenv(char *var, char *val) {
78 char *s=malloc(strlen(var)+1+strlen(val)+1);
81 sprintf(s, "%s=%s", var, val);
85 int main (int argc, char **argv) {
86 /* Sanitize environment. */
89 newenviron[i++]="HOME=$ENV{HOME}";
90 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
94 if (setregid(getegid(), -1) != 0 &&
95 setregid(getegid(), -1) != 0) {
96 perror("failed to drop real gid");
99 if (setreuid(geteuid(), -1) != 0 &&
100 setreuid(geteuid(), -1) != 0) {
101 perror("failed to drop real uid");
105 execl("$this", "$this", NULL);
106 perror("exec $this");
112 my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
113 if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
114 #translators: The parameter is a C filename.
115 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
117 unlink("$wrapper.c");
118 if (defined $config{wrappergroup}) {
119 my $gid=(getgrnam($config{wrappergroup}))[2];
120 if (! defined $gid) {
121 error(sprintf("bad wrappergroup"));
123 if (! chown(-1, $gid, "$wrapper.new")) {
124 error("chown $wrapper.new: $!");
127 if (defined $config{wrappermode} &&
128 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
129 error("chmod $wrapper.new: $!");
131 if (! rename("$wrapper.new", $wrapper)) {
132 error("rename $wrapper.new $wrapper: $!");
134 #translators: The parameter is a filename.
135 printf(gettext("successfully generated %s"), $wrapper);