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