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