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