]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Wrapper.pm
remove xxx comment
[ikiwiki.git] / IkiWiki / Wrapper.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use File::Spec;
8 use Data::Dumper;
9 use IkiWiki;
10
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);
15         if (! -x $this) {
16                 error(sprintf(gettext("%s doesn't seem to be executable"), $this));
17         }
18
19         if ($config{setup}) {
20                 error(gettext("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(gettext("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 REMOTE_USER HTTPS} if $config{cgi};
32         my $envsave="";
33         foreach my $var (@envsave) {
34                 $envsave.=<<"EOF";
35         if ((s=getenv("$var")))
36                 addenv("$var", s);
37 EOF
38         }
39
40         my $test_receive="";
41         if ($config{test_receive}) {
42                 require IkiWiki::Receive;
43                 $test_receive=IkiWiki::Receive::gen_wrapper();
44         }
45
46         my $check_commit_hook="";
47         my $pre_exec="";
48         if ($config{post_commit}) {
49                 # Optimise checking !commit_hook_enabled() , 
50                 # so that ikiwiki does not have to be started if the
51                 # hook is disabled.
52                 #
53                 # Note that perl's flock may be implemented using fcntl
54                 # or lockf on some systems. If so, and if there is no
55                 # interop between the locking systems, the true C flock will
56                 # always succeed, and this optimisation won't work.
57                 # The perl code will later correctly check the lock,
58                 # so the right thing will still happen, though without
59                 # the benefit of this optimisation.
60                 $check_commit_hook=<<"EOF";
61         {
62                 int fd=open("$config{wikistatedir}/commitlock", O_CREAT | O_RDWR, 0666);
63                 if (fd != -1) {
64                         if (flock(fd, LOCK_SH | LOCK_NB) != 0)
65                                 exit(0);
66                         close(fd);
67                 }
68         }
69 EOF
70         }
71         elsif ($config{cgi}) {
72                 # Avoid more than one ikiwiki cgi running at a time by
73                 # taking a cgi lock. Since ikiwiki uses several MB of
74                 # memory, a pile up of processes could cause thrashing
75                 # otherwise. The fd of the lock is stored in
76                 # IKIWIKI_CGILOCK_FD so unlockwiki can close it.
77                 $pre_exec=<<"EOF";
78         {
79                 int fd=open("$config{wikistatedir}/cgilock", O_CREAT | O_RDWR, 0666);
80                 if (fd != -1 && flock(fd, LOCK_EX) == 0) {
81                         char *fd_s;
82                         asprintf(&fd_s, "%i", fd);
83                         setenv("IKIWIKI_CGILOCK_FD", fd_s, 1);
84                 }
85         }
86 EOF
87         }
88
89         $Data::Dumper::Indent=0; # no newlines
90         my $configstring=Data::Dumper->Dump([\%config], ['*config']);
91         $configstring=~s/\\/\\\\/g;
92         $configstring=~s/"/\\"/g;
93         $configstring=~s/\n/\\n/g;
94         
95         #translators: The first parameter is a filename, and the second is
96         #translators: a (probably not translated) error message.
97         open(OUT, ">$wrapper.c") || error(sprintf(gettext("failed to write %s: %s"), "$wrapper.c", $!));;
98         print OUT <<"EOF";
99 /* A wrapper for ikiwiki, can be safely made suid. */
100 #include <stdio.h>
101 #include <sys/types.h>
102 #include <sys/stat.h>
103 #include <fcntl.h>
104 #include <unistd.h>
105 #include <stdlib.h>
106 #include <string.h>
107 #include <sys/file.h>
108
109 extern char **environ;
110 char *newenviron[$#envsave+6];
111 int i=0;
112
113 addenv(char *var, char *val) {
114         char *s=malloc(strlen(var)+1+strlen(val)+1);
115         if (!s)
116                 perror("malloc");
117         sprintf(s, "%s=%s", var, val);
118         newenviron[i++]=s;
119 }
120
121 int main (int argc, char **argv) {
122         char *s;
123
124 $check_commit_hook
125 $test_receive
126 $envsave
127         newenviron[i++]="HOME=$ENV{HOME}";
128         newenviron[i++]="WRAPPED_OPTIONS=$configstring";
129         newenviron[i]=NULL;
130         environ=newenviron;
131
132         if (setregid(getegid(), -1) != 0 &&
133             setregid(getegid(), -1) != 0) {
134                 perror("failed to drop real gid");
135                 exit(1);
136         }
137         if (setreuid(geteuid(), -1) != 0 &&
138             setreuid(geteuid(), -1) != 0) {
139                 perror("failed to drop real uid");
140                 exit(1);
141         }
142
143 $pre_exec
144         execl("$this", "$this", NULL);
145         perror("exec $this");
146         exit(1);
147 }
148 EOF
149         close OUT;
150
151         my $cc=exists $ENV{CC} ? possibly_foolish_untaint($ENV{CC}) : 'cc';
152         if (system($cc, "$wrapper.c", "-o", "$wrapper.new") != 0) {
153                 #translators: The parameter is a C filename.
154                 error(sprintf(gettext("failed to compile %s"), "$wrapper.c"));
155         }
156         unlink("$wrapper.c");
157         if (defined $config{wrappergroup}) {
158                 my $gid=(getgrnam($config{wrappergroup}))[2];
159                 if (! defined $gid) {
160                         error(sprintf("bad wrappergroup"));
161                 }
162                 if (! chown(-1, $gid, "$wrapper.new")) {
163                         error("chown $wrapper.new: $!");
164                 }
165         }
166         if (defined $config{wrappermode} &&
167             ! chmod(oct($config{wrappermode}), "$wrapper.new")) {
168                 error("chmod $wrapper.new: $!");
169         }
170         if (! rename("$wrapper.new", $wrapper)) {
171                 error("rename $wrapper.new $wrapper: $!");
172         }
173         #translators: The parameter is a filename.
174         printf(gettext("successfully generated %s"), $wrapper);
175         print "\n";
176 }
177
178 1