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