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