fix a bug that doubled the search box
[ikiwiki] / IkiWiki / Wrapper.pm
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use Cwd q{abs_path};
6 use Data::Dumper;
7 use IkiWiki;
8
9 package IkiWiki;
10
11 sub gen_wrapper () { #{{{
12         $config{srcdir}=abs_path($config{srcdir});
13         $config{destdir}=abs_path($config{destdir});
14         my $this=abs_path($0);
15         if (! -x $this) {
16                 error("$this doesn't seem to be executable");
17         }
18
19         if ($config{setup}) {
20                 error("cannot create a wrapper that uses a setup file");
21         }
22         my $wrapper=possibly_foolish_untaint($config{wrapper});
23         if (! defined $wrapper || ! length $wrapper) {
24                 error("wrapper filename not specified");
25         }
26         delete $config{wrapper};
27         
28         my @envsave;
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};
32         my $envsave="";
33         foreach my $var (@envsave) {
34                 $envsave.=<<"EOF"
35         if ((s=getenv("$var")))
36                 asprintf(&newenviron[i++], "%s=%s", "$var", s);
37 EOF
38         }
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.
42                 $envsave.=<<"EOF"
43         if (argc == 3)
44                 asprintf(&newenviron[i++], "REV=%s", argv[2]);
45         else if ((s=getenv("REV")))
46                 asprintf(&newenviron[i++], "%s=%s", "REV", s);
47 EOF
48         }
49         
50         # This is only set by plugins, which append to it on startup, so
51         # avoid storing it in the wrapper.
52         $config{headercontent}="";
53         
54         $Data::Dumper::Indent=0; # no newlines
55         my $configstring=Data::Dumper->Dump([\%config], ['*config']);
56         $configstring=~s/\\/\\\\/g;
57         $configstring=~s/"/\\"/g;
58         $configstring=~s/\n/\\\n/g;
59         
60         open(OUT, ">$wrapper.c") || error("failed to write $wrapper.c: $!");;
61         print OUT <<"EOF";
62 /* A wrapper for ikiwiki, can be safely made suid. */
63 #define _GNU_SOURCE
64 #include <stdio.h>
65 #include <unistd.h>
66 #include <stdlib.h>
67 #include <string.h>
68
69 extern char **environ;
70
71 int main (int argc, char **argv) {
72         /* Sanitize environment. */
73         char *s;
74         char *newenviron[$#envsave+5];
75         int i=0;
76 $envsave
77         newenviron[i++]="HOME=$ENV{HOME}";
78         newenviron[i++]="WRAPPED_OPTIONS=$configstring";
79         newenviron[i]=NULL;
80         environ=newenviron;
81
82         execl("$this", "$this", NULL);
83         perror("failed to run $this");
84         exit(1);
85 }
86 EOF
87         close OUT;
88         if (system("gcc", "$wrapper.c", "-o", $wrapper) != 0) {
89                 error("failed to compile $wrapper.c");
90         }
91         unlink("$wrapper.c");
92         if (defined $config{wrappermode} &&
93             ! chmod(oct($config{wrappermode}), $wrapper)) {
94                 error("chmod $wrapper: $!");
95         }
96         print "successfully generated $wrapper\n";
97 } #}}}
98
99 1