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 $Data::Dumper::Indent=0; # no newlines
47 my $configstring=Data::Dumper->Dump([\%config], ['*config']);
48 $configstring=~s/\\/\\\\/g;
49 $configstring=~s/"/\\"/g;
50 $configstring=~s/\n/\\n/g;
52 #translators: The first parameter is a filename, and the second is
53 #translators: a (probably not translated) error message.
54 open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
56 /* A wrapper for ikiwiki, can be safely made suid. */
58 #include <sys/types.h>
63 extern char **environ;
64 char *newenviron[$#envsave+6];
67 addenv(char *var, char *val) {
68 char *s=malloc(strlen(var)+1+strlen(val)+1);
71 sprintf(s, "%s=%s", var, val);
75 int main (int argc, char **argv) {
80 newenviron[i++]="HOME=$ENV{HOME}";
81 newenviron[i++]="WRAPPED_OPTIONS=$configstring";
85 if (setregid(getegid(), -1) != 0 &&
86 setregid(getegid(), -1) != 0) {
87 perror("failed to drop real gid");
90 if (setreuid(geteuid(), -1) != 0 &&
91 setreuid(geteuid(), -1) != 0) {
92 perror("failed to drop real uid");
96 execl("$this", "$this", NULL);
103 my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
104 if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
105 #translators: The parameter is a C filename.
106 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
108 unlink("$wrapper.c");
109 if (defined $config{wrappergroup}) {
110 my $gid=(getgrnam($config{wrappergroup}))[2];
111 if (! defined $gid) {
112 error(sprintf("bad wrappergroup"));
114 if (! chown(-1, $gid, "$wrapper.new")) {
115 error("chown $wrapper.new: $!");
118 if (defined $config{wrappermode} &&
119 ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
120 error("chmod $wrapper.new: $!");
122 if (! rename("$wrapper.new", $wrapper)) {
123 error("rename $wrapper.new $wrapper: $!");
125 #translators: The parameter is a filename.
126 printf(gettext("successfully generated %s"), $wrapper);