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")))
40 $Data::Dumper::Indent=0; # no newlines
41 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
42 $configstring=~s/\\/\\\\/g;
43 $configstring=~s/"/\\"/g;
44 $configstring=~s/\n/\\n/g;
46 #translators: The first parameter is a filename, and the second is
47 #translators: a (probably not translated) error message.
48 open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
50 /* A wrapper for ikiwiki, can be safely made suid. */
52 #include <sys/types.h>
57 extern char **environ;
58 char *newenviron[$#envsave+5];
61 addenv(char *var, char *val) {
62 char *s=malloc(strlen(var)+1+strlen(val)+1);
65 sprintf(s, "%s=%s", var, val);
69 int main (int argc, char **argv) {
70 /* Sanitize environment. */
73 newenviron[i++]="HOME=$ENV{HOME}";
74 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
78 if (setregid(getegid(), -1) != 0 &&
79 setregid(getegid(), -1) != 0) {
80 perror("failed to drop real gid");
83 if (setreuid(geteuid(), -1) != 0 &&
84 setreuid(geteuid(), -1) != 0) {
85 perror("failed to drop real uid");
89 execl("$this", "$this", NULL);
96 my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
97 if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
98 #translators: The parameter is a C filename.
99 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
101 unlink("$wrapper.c");
102 if (defined $config{wrappergroup}) {
103 my $gid=(getgrnam($config{wrappergroup}))[2];
104 if (! defined $gid) {
105 error(sprintf("bad wrappergroup"));
107 if (! chown(-1, $gid, "$wrapper.new")) {
108 error("chown $wrapper.new: $!");
111 if (defined $config{wrappermode} &&
112 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
113 error("chmod $wrapper.new: $!");
115 if (! rename("$wrapper.new", $wrapper)) {
116 error("rename $wrapper.new $wrapper: $!");
118 #translators: The parameter is a filename.
119 printf(gettext("successfully generated %s"), $wrapper);