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")))
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+6];
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";
75 asprintf(&s, "CALLER_UID=%i", getuid());
80 if (setregid(getegid(), -1) != 0 &&
81 setregid(getegid(), -1) != 0) {
82 perror("failed to drop real gid");
85 if (setreuid(geteuid(), -1) != 0 &&
86 setreuid(geteuid(), -1) != 0) {
87 perror("failed to drop real uid");
91 execl("$this", "$this", NULL);
98 my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
99 if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
100 #translators: The parameter is a C filename.
101 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
103 unlink("$wrapper.c");
104 if (defined $config{wrappergroup}) {
105 my $gid=(getgrnam($config{wrappergroup}))[2];
106 if (! defined $gid) {
107 error(sprintf("bad wrappergroup"));
109 if (! chown(-1, $gid, "$wrapper.new")) {
110 error("chown $wrapper.new: $!");
113 if (defined $config{wrappermode} &&
114 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
115 error("chmod $wrapper.new: $!");
117 if (! rename("$wrapper.new", $wrapper)) {
118 error("rename $wrapper.new $wrapper: $!");
120 #translators: The parameter is a filename.
121 printf(gettext("successfully generated %s"), $wrapper);