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>
7 * cgi-fcgi-interp.[ch] - C helpers common to the whole of chiark-utils
9 * Copyright 2016 Ian Jackson
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public
22 * License along with this file; if not, consult the Free Software
23 * Foundation's website at www.fsf.org, or the GNU Project website at
27 * The result is a program which looks, when executed via the #!
28 * line, like a CGI program. But the script inside will be executed
29 * via <interpreter> in an fcgi context.
34 * The real interpreter to use. Eg "perl". Need not
35 * be an absolute path; will be fed to execvp.
38 * Use <ident> rather than hex(sha256(<script>))
39 * as the basename of the leafname of the fcgi rendezvous
40 * socket. If <ident> contains only hex digit characters it
41 * ought to be no more than 32 characters. <ident> should
42 * not contain spaces or commas (see below).
45 * Start <numservers> instances of the program. This
46 * determines the maximum concurrency. (Note that unlike
47 * speedy, the specified number of servers is started
48 * right away.) The default is 4.
51 * Debug mode. Do not actually run program. Instead, print
52 * out what we would do.
54 * <options> and <interpreter> can be put into a single argument
55 * to cgi-fcgi-interp, separated by spaces or commas. <interpreter>
58 * cgi-fcgi-interp automatically expires old sockets, including
59 * ones where the named script is out of date.
63 * Uses one of two directories
64 * /var/run/user/<UID>/cgi-fcgi-interp/
65 * ~/.cgi-fcgi-interp/<node>/
66 * and inside there uses these paths
70 * If -M<ident> is not specified then an initial substricg of the
71 * lowercase hex of the sha256 of the <script> (ie, our argv[1]) is
72 * used. The substring is chosen so that the whole path is 10 bytes
73 * shorter than sizeof(sun_path). But always at least 33 characters.
75 * <node> is truncated at the first `.' and after the first 32
79 * - see if /var/run/user exists
80 * if so, lstat /var/run/user/<UID> and check that
81 * we own it and it's X700; if not, fail
82 * if it's ok then <base> is /var/run/user/<UID>
83 * otherwise, look for and maybe create ~/.cgi-fcgi-interp
84 * (where ~ is HOME or from getpwuid)
85 * and then <base> is ~/.cgi-fcgi-interp/<node>
86 * - calculate pathname (checking <ident> length is OK)
87 * - check for and maybe create <base>
88 * - stat and lstat the <script>
89 * - stat the socket and check its timestamp
90 * if it is too hold, rename it to g<inum> (where
91 * <inum> is in decimal)
92 * and run garbage collection
93 * - run cgi-fcgi -connect SOCKET SCRIPT
106 #include <sys/types.h>
107 #include <sys/stat.h>
108 #include <sys/utsname.h>
109 #include <sys/socket.h>
115 #include <nettle/sha.h>
119 #define die common_die
120 #define diee common_diee
122 #define MINHEXHASH 33
124 static const char *interp, *ident;
125 static int numservers, debugmode;
127 void diee(const char *m) {
128 err(127, "error: %s failed", m);
131 static void fusagemessage(FILE *f) {
132 fprintf(f, "usage: #!/usr/bin/cgi-fcgi-interp [<options>]\n");
135 void usagemessage(void) { fusagemessage(stderr); }
137 static void of_help(const struct cmdinfo *ci, const char *val) {
138 fusagemessage(stdout);
139 if (ferror(stdout)) diee("write usage message to stdout");
143 static void of_iassign(const struct cmdinfo *ci, const char *val) {
146 errno= 0; v= strtol(val,&ep,10);
147 if (!*val || *ep || errno || v<INT_MIN || v>INT_MAX)
148 badusage("bad integer argument `%s' for --%s",val,ci->olong);
154 static const struct cmdinfo cmdinfos[]= {
155 { "help", 0, .call= of_help },
156 { 0, 'g', 1, .sassignto= &ident },
157 { 0, 'M', 1, .call=of_iassign, .iassignto= &numservers },
158 { 0, 'D', 0, .iassignto= &debugmode, .arg= 1 },
163 static const char *run_base, *script, *socket_path;
165 static bool find_run_base_var_run(void) {
170 try = m_asprintf("%s/%lu", "/var/run/user", us);
171 r = lstat(try, &stab);
173 if (errno == ENOENT ||
177 return 0; /* oh well */
178 diee("stat /var/run/user/UID");
180 if (!S_ISDIR(stab.st_mode)) {
181 warnx("%s not a directory, falling back to ~\n", try);
184 if (stab.st_uid != us) {
185 warnx("%s not owned by uid %lu, falling back to ~\n", try,
189 if (stab.st_mode & 0077) {
190 warnx("%s writeable by group or other, falling back to ~\n", try);
193 run_base = m_asprintf("%s/%s", try, "cgi-fcgi-interp");
197 static bool find_run_base_home(void) {
203 pw = getpwuid(us); if (!pw) diee("getpwent(uid)");
205 r = uname(&ut); if (r) diee("uname(2)");
206 dot = strchr(ut.nodename, '.');
208 if (sizeof(ut.nodename) > 32)
211 try = m_asprintf("%s/%s/%s", pw->pw_dir, ".cgi-fcgi-interp", ut.nodename);
216 static void find_socket_path(void) {
217 struct sockaddr_un sun;
220 us = getuid(); if (us==(uid_t)-1) diee("getuid");
222 find_run_base_var_run() ||
223 find_run_base_home() ||
226 int maxidentlen = sizeof(sun.sun_path) - strlen(run_base) - 10 - 2;
229 if (maxidentlen < MINHEXHASH)
230 errx(127,"base directory `%s'"
231 " leaves only %d characters for id hash"
232 " which is too little (<%d)",
233 run_base, maxidentlen, MINHEXHASH);
235 int identlen = maxidentlen > 64 ? 64 : maxidentlen;
236 char *hexident = xmalloc(identlen + 2);
237 struct sha256_ctx sc;
238 unsigned char bbuf[32];
242 sha256_update(&sc,strlen(interp)+1,interp);
243 sha256_update(&sc,strlen(script)+1,script);
244 sha256_digest(&sc,sizeof(bbuf),bbuf);
246 for (i=0; i<identlen; i += 2)
247 sprintf(hexident+i, "%02x", bbuf[i/2]);
249 hexident[identlen] = 0;
253 if (strlen(ident) > maxidentlen)
254 errx(127, "base directory `%s' plus ident `%s' too long"
255 " (with spare) for socket (max ident %d)\n",
256 run_base, ident, maxidentlen);
258 r = mkdir(run_base, 0700);
260 if (!(errno == EEXIST))
261 err(127,"mkdir %s",run_base);
264 socket_path = m_asprintf("%s/g%s",run_base,ident);
267 static bool check_garbage(void) {
268 struct stat sock_stab, cmd_stab;
271 r = lstat(socket_path, &sock_stab);
273 if ((errno == ENOENT))
274 return 0; /* well, no garbage then */
275 err(127,"stat socket (%s)",socket_path);
278 r = lstat(script, &cmd_stab);
279 if (r) err(127,"lstat script (%s)",script);
284 static void shbang_opts(const char *const **argv_io,
285 const struct cmdinfo *cmdinfos) {
286 myopt(argv_io, cmdinfos);
288 interp = *(*argv_io)++;
289 if (!interp) errx(127,"need interpreter argument");
292 int main(int argc, const char *const *argv) {
293 const char *smashedopt;
296 (smashedopt = argv[1]) &&
297 smashedopt[0]=='-' &&
298 (strchr(smashedopt,' ') || strchr(smashedopt,','))) {
299 /* single argument containg all the options and <interp> */
300 argv += 2; /* eat argv[0] and smashedopt */
301 const char *split_args[MAX_OPTS+1];
303 split_args[split_argc++] = argv[0];
305 if (split_argc >= MAX_OPTS) errx(127,"too many options in combined arg");
306 split_args[split_argc++] = smashedopt;
307 if (smashedopt[0] != '-') /* never true on first iteration */
309 char *delim = strchr(smashedopt,' ');
310 if (!delim) delim = strchr(smashedopt,',');
312 errx(127,"combined arg lacks <interpreter>");
314 smashedopt = delim+1;
316 assert(split_argc <= MAX_OPTS);
317 split_args[split_argc++] = 0;
319 const char *const *split_argv = split_args;
321 shbang_opts(&split_argv, cmdinfos);
323 if (!split_argv) errx(127,"combined arg too many non-option arguments");
325 shbang_opts(&argv, cmdinfos);
329 if (!script) errx(127,"need script argument");
330 if (*argv) errx(127,"too many arguments");
337 printf("socket: %s\n",socket_path);
338 printf("interp: %s\n",interp);
339 printf("script: %s\n",script);