chiark / gitweb /
27327743b0cc361ec4a77efe0148a56bb7f4203a
[chiark-utils.git] / cprogs / cgi-fcgi-interp.c
1 /*
2  * "Interpreter" that you can put in #! like this
3  *   #!/usr/bin/cgi-fcgi-interp [<options>] <interpreter>
4  *   #!/usr/bin/cgi-fcgi-interp [<options>],<interpreter>
5  */
6 /*
7  * cgi-fcgi-interp.[ch] - C helpers common to the whole of chiark-utils
8  *
9  * Copyright 2016 Ian Jackson
10  * Copyright 1982,1986,1993 The Regents of the University of California
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public
23  * License along with this file; if not, consult the Free Software
24  * Foundation's website at www.fsf.org, or the GNU Project website at
25  * www.gnu.org.
26  *
27  * See below for a BSD 3-clause notice regarding timespeccmp.
28  */
29 /*
30  * The result is a program which looks, when executed via the #!
31  * line, like a CGI program.  But the script inside will be executed
32  * via <interpreter> in an fcgi context.
33  *
34  * Options:
35  *
36  *  <interpreter>
37  *          The real interpreter to use.  Eg "perl".  Need not
38  *          be an absolute path; will be fed to execvp.
39  *
40  *  -g<ident>
41  *          Use <ident> rather than hex(sha256(<script>))
42  *          as the basename of the leafname of the fcgi rendezvous
43  *          socket.  If <ident> contains only hex digit characters it
44  *          ought to be no more than 32 characters.  <ident> should
45  *          not contain spaces or commas (see below).
46  *
47  *  -M<numservers>
48  *         Start <numservers> instances of the program.  This
49  *         determines the maximum concurrency.  (Note that unlike
50  *         speedy, the specified number of servers is started
51  *         right away.)  The default is 4.
52  *
53  *  -D
54  *         Debug mode.  Do not actually run program.  Instead, print
55  *         out what we would do.
56  *
57  * <options> and <interpreter> can be put into a single argument
58  * to cgi-fcgi-interp, separated by spaces or commas.  <interpreter>
59  * must come last.
60  *
61  * cgi-fcgi-interp automatically expires old sockets, including
62  * ones where the named script is out of date.
63  */
64
65 /*
66  * Uses one of two directories
67  *   /var/run/user/<UID>/cgi-fcgi-interp/
68  *   ~/.cgi-fcgi-interp/<node>/
69  * and inside there uses these paths
70  *   s<ident>
71  *   g<inum>
72  *
73  * If -M<ident> is not specified then an initial substricg of the
74  * lowercase hex of the sha256 of the <script> (ie, our argv[1]) is
75  * used.  The substring is chosen so that the whole path is 10 bytes
76  * shorter than sizeof(sun_path).  But always at least 33 characters.
77  *
78  * <node> is truncated at the first `.' and after the first 32
79  * characters.
80  *
81  * Algorithm:
82  *  - see if /var/run/user exists
83  *       if so, lstat /var/run/user/<UID> and check that
84  *         we own it and it's X700; if not, fail
85  *         if it's ok then <base> is /var/run/user/<UID>
86  *       otherwise, look for and maybe create ~/.cgi-fcgi-interp
87  *         (where ~ is HOME or from getpwuid)
88  *         and then <base> is ~/.cgi-fcgi-interp/<node>
89  *  - calculate pathname (checking <ident> length is OK)
90  *  - check for and maybe create <base>
91  *  - stat and lstat the <script>
92  *  - stat the socket and check its timestamp
93  *       if it is too old, rename it to g<inum>.<pid> (where
94  *       <inum> and <pid> are in decimal)
95  *       and run garbage collection
96  *  - run  cgi-fcgi -connect SOCKET SCRIPT
97  */
98
99 #include "common.h"
100
101 #include <stdio.h>
102 #include <stdlib.h>
103 #include <string.h>
104 #include <errno.h>
105 #include <stdbool.h>
106 #include <assert.h>
107 #include <limits.h>
108
109 #include <sys/types.h>
110 #include <sys/stat.h>
111 #include <sys/utsname.h>
112 #include <sys/socket.h>
113 #include <sys/un.h>
114 #include <unistd.h>
115 #include <pwd.h>
116 #include <err.h>
117
118 #include <nettle/sha.h>
119
120 #include "myopt.h"
121
122 #define die  common_die
123 #define diee common_diee
124
125 #define MINHEXHASH 33
126
127 static const char *interp, *ident;
128 static int numservers, debugmode;
129
130 void diee(const char *m) {
131   err(127, "error: %s failed", m);
132 }
133
134 static void fusagemessage(FILE *f) {
135   fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
136 }
137
138 void usagemessage(void) { fusagemessage(stderr); }
139
140 static void of_help(const struct cmdinfo *ci, const char *val) {
141   fusagemessage(stdout);
142   if (ferror(stdout)) diee("write usage message to stdout");
143   exit(0);
144 }
145
146 static void of_iassign(const struct cmdinfo *ci, const char *val) {
147   long v;
148   char *ep;
149   errno= 0; v= strtol(val,&ep,10);
150   if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
151     badusage("bad integer argument `%s' for --%s",val,ci->olong);
152   *ci->iassignto = v;
153 }
154
155 #define MAX_OPTS 5
156
157 static const struct cmdinfo cmdinfos[]= {
158   { "help",   0, .call= of_help               },
159   { 0, 'g',   1, .sassignto= &ident           },
160   { 0, 'M',   1, .call=of_iassign, .iassignto= &numservers      },
161   { 0, 'D',   0, .iassignto= &debugmode, .arg= 1 },
162   { 0 }
163 };
164
165 static uid_t us;
166 static const char *run_base, *script, *socket_path;
167 static struct stat sock_stab;
168
169 static bool find_run_base_var_run(void) {
170   struct stat stab;
171   char *try;
172   int r;
173
174   try = m_asprintf("%s/%lu", "/var/run/user", us);
175   r = lstat(try, &stab);
176   if (r<0) {
177     if (errno == ENOENT ||
178         errno == ENOTDIR ||
179         errno == EACCES ||
180         errno == EPERM)
181       return 0; /* oh well */
182     diee("stat /var/run/user/UID");
183   }
184   if (!S_ISDIR(stab.st_mode)) {
185     warnx("%s not a directory, falling back to ~\n", try);
186     return 0;
187   }
188   if (stab.st_uid != us) {
189     warnx("%s not owned by uid %lu, falling back to ~\n", try,
190           (unsigned long)us);
191     return 0;
192   }
193   if (stab.st_mode & 0077) {
194     warnx("%s writeable by group or other, falling back to ~\n", try);
195     return 0;
196   }
197   run_base = m_asprintf("%s/%s", try, "cgi-fcgi-interp");
198   return 1;
199 }
200
201 static bool find_run_base_home(void) {
202   struct passwd *pw;
203   struct utsname ut;
204   char *dot, *try;
205   int r;
206
207   pw = getpwuid(us);  if (!pw) diee("getpwent(uid)");
208
209   r = uname(&ut);   if (r) diee("uname(2)");
210   dot = strchr(ut.nodename, '.');
211   if (dot) *dot = 0;
212   if (sizeof(ut.nodename) > 32)
213     ut.nodename[32] = 0;
214
215   try = m_asprintf("%s/%s/%s", pw->pw_dir, ".cgi-fcgi-interp", ut.nodename);
216   run_base = try;
217   return 1;
218 }
219
220 static void find_socket_path(void) {
221   struct sockaddr_un sun;
222   int r;
223
224   us = getuid();  if (us==(uid_t)-1) diee("getuid");
225
226   find_run_base_var_run() ||
227     find_run_base_home() ||
228     (abort(),0);
229
230   int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
231
232   if (!ident) {
233     if (maxidentlen < MINHEXHASH)
234       errx(127,"base directory `%s'"
235            " leaves only %d characters for id hash"
236            " which is too little (<%d)",
237            run_base, maxidentlen, MINHEXHASH);
238
239     int identlen = maxidentlen > 64 ? 64 : maxidentlen;
240     char *hexident = xmalloc(identlen + 2);
241     struct sha256_ctx sc;
242     unsigned char bbuf[32];
243     int i;
244
245     sha256_init(&sc);
246     sha256_update(&sc,strlen(interp)+1,interp);
247     sha256_update(&sc,strlen(script)+1,script);
248     sha256_digest(&sc,sizeof(bbuf),bbuf);
249
250     for (i=0; i<identlen; i += 2)
251       sprintf(hexident+i, "%02x", bbuf[i/2]);
252
253     hexident[identlen] = 0;
254     ident = hexident;
255   }
256
257   if (strlen(ident) > maxidentlen)
258     errx(127, "base directory `%s' plus ident `%s' too long"
259          " (with spare) for socket (max ident %d)\n",
260          run_base, ident, maxidentlen);
261
262   r = mkdir(run_base, 0700);
263   if (r) {
264     if (!(errno == EEXIST))
265       err(127,"mkdir %s",run_base);
266   }
267
268   socket_path = m_asprintf("%s/g%s",run_base,ident);
269 }  
270
271 /*
272  * Regarding the macro timespeccmp:
273  *
274  * Copyright (c) 1982, 1986, 1993
275  *      The Regents of the University of California.  All rights reserved.
276  *
277  * Redistribution and use in source and binary forms, with or without
278  * modification, are permitted provided that the following conditions
279  * are met:
280  * 1. Redistributions of source code must retain the above copyright
281  *    notice, this list of conditions and the following disclaimer.
282  * 2. Redistributions in binary form must reproduce the above copyright
283  *    notice, this list of conditions and the following disclaimer in the
284  *    documentation and/or other materials provided with the distribution.
285  * 4. Neither the name of the University nor the names of its contributors
286  *    may be used to endorse or promote products derived from this software
287  *    without specific prior written permission.
288  *
289  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
290  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
291  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
292  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
293  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
295  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
296  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
297  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
298  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299  * SUCH DAMAGE.
300  *
301  *      @(#)time.h      8.5 (Berkeley) 5/4/95
302  * $FreeBSD: head/sys/sys/time.h 275985 2014-12-21 05:07:11Z imp $
303  */
304 #ifndef timespeccmp
305 #define timespeccmp(tvp, uvp, cmp)                                      \
306         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
307             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
308             ((tvp)->tv_sec cmp (uvp)->tv_sec))
309 #endif /*timespeccmp*/
310
311
312
313 static bool stab_isnewer(const struct stat *a, const struct stat *b) {
314 #ifdef st_mtime
315   return timespeccmp(&a->st_mtim, &b->st_mtim, >);
316 #else
317   return a->st_mtime > &b->st_mtime;
318 #endif
319 }
320
321 static bool check_garbage(void) {
322   struct stat script_stab;
323   int r;
324
325   r = lstat(script, &script_stab);
326   if (r) err(127,"lstat script (%s)",script);
327
328   r = lstat(socket_path, &sock_stab);
329   if (r) {
330     if ((errno == ENOENT))
331       return 0; /* well, no garbage then */
332     err(127,"stat socket (%s)",socket_path);
333   }
334
335   if (stab_isnewer(&script_stab, &sock_stab))
336     return 1;
337
338   if (S_ISLNK(script_stab.st_mode)) {
339     r = stat(script, &script_stab);
340     if (r) err(127,"stat script (%s0",script);
341
342     if (stab_isnewer(&script_stab, &sock_stab))
343       return 1;
344   }
345
346   return 0;
347 }
348
349 static void tidy_garbage(void) {
350   const char *this_garbage =
351     m_asprintf("%s/%lu.%lu", run_base,
352                (unsigned long)sock_stab.st_ino,
353                (unsigned long)getpid());
354
355   printf("this_garb: %s\n", this_garbage);
356 }
357
358 static void shbang_opts(const char *const **argv_io,
359                         const struct cmdinfo *cmdinfos) {
360   myopt(argv_io, cmdinfos);
361
362   interp = *(*argv_io)++;
363   if (!interp) errx(127,"need interpreter argument");
364 }
365
366 int main(int argc, const char *const *argv) {
367   const char *smashedopt;
368
369   if (argc>=2 &&
370       (smashedopt = argv[1]) &&
371       smashedopt[0]=='-' &&
372       (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
373     /* single argument containg all the options and <interp> */
374     argv += 2; /* eat argv[0] and smashedopt */
375     const char *split_args[MAX_OPTS+1];
376     int split_argc = 0;
377     split_args[split_argc++] = argv[0];
378     for (;;) {
379       if (split_argc >= MAX_OPTS) errx(127,"too many options in combined arg");
380       split_args[split_argc++] = smashedopt;
381       if (smashedopt[0] != '-') /* never true on first iteration */
382         break;
383       char *delim = strchr(smashedopt,' ');
384       if (!delim) delim = strchr(smashedopt,',');
385       if (!delim)
386         errx(127,"combined arg lacks <interpreter>");
387       *delim = 0;
388       smashedopt = delim+1;
389     }
390     assert(split_argc <= MAX_OPTS);
391     split_args[split_argc++] = 0;
392
393     const char *const *split_argv = split_args;
394
395     shbang_opts(&split_argv, cmdinfos);
396     /* sets interp */
397     if (!split_argv) errx(127,"combined arg too many non-option arguments");
398   } else {
399     shbang_opts(&argv, cmdinfos);
400   }
401
402   script = *argv++;
403   if (!script) errx(127,"need script argument");
404   if (*argv) errx(127,"too many arguments");
405
406   find_socket_path();
407
408   bool havegarbage = check_garbage();
409
410   if (debugmode) {
411     printf("socket: %s\n",socket_path);
412     printf("interp: %s\n",interp);
413     printf("script: %s\n",script);
414     printf("garbage: %d\n",havegarbage);
415     exit(0);
416   }
417
418   if (havegarbage)
419     tidy_garbage();
420
421   exit(0);
422 }