chiark / gitweb /
cgi-fcgi-interp: Provide -E option.
[chiark-utils.git] / cprogs / cgi-fcgi-interp.c
index ef48cfda3b80af8b02e6375639943f07558a44cf..6b0585b54f4a9b46e0464d80458c28f4f88b28c7 100644 (file)
  *          The real interpreter to use.  Eg "perl".  Need not
  *          be an absolute path; will be fed to execvp.
  *
+ *  -G<ident-info>
+ *          Add <ident-info> to the unique identifying information for
+ *          this fcgi program.  May be repeated; order is significant.
+ *
+ *  -E<ident-info-env-var>
+ *          Look <ident-info-env-var> up in the environment and add
+ *          <ident-info-env-var>=<value> as if specified with -G.  If
+ *          the variable is unset in the environment, it is as if
+ *          -G<ident-info-env-var> was specified.
+ *
  *  -g<ident>
- *          Use <ident> rather than hex(sha256(<script>))
+ *          Use <ident> rather than hex(sha256(<interp>\0<script>\0))
  *          as the basename of the leafname of the fcgi rendezvous
  *          socket.  If <ident> contains only hex digit characters it
  *          ought to be no more than 32 characters.  <ident> should
@@ -77,8 +87,8 @@
  *   s<ident>
  *   l<ident>    used to lock around garbage collection
  *
- * If -M<ident> is not specified then an initial substricg of the
- * lowercase hex of the sha256 of the <script> (ie, our argv[1]) is
+ * If -M<ident> is not specified then an initial substring of the
+ * lowercase hex of the sha256 of <interp>\0<script>\0 is
  * used.  The substring is chosen so that the whole path is 10 bytes
  * shorter than sizeof(sun_path).  But always at least 33 characters.
  *
@@ -151,6 +161,8 @@ static const char *interp, *ident;
 static int numservers=4, debugmode;
 static int check_interval=300;
 
+static struct sha256_ctx identsc;
+
 const char *stage2;
 
 void diee(const char *m) {
@@ -178,19 +190,38 @@ static void of_iassign(const struct cmdinfo *ci, const char *val) {
   *ci->iassignto = v;
 }
 
+static void ident_addstring(const struct cmdinfo *ci, const char *string) {
+  /* ci may be 0 and is provided so this can be .call */
+  sha256_update(&identsc,strlen(string)+1,string);
+}
+
+static void off_ident_addenv(const struct cmdinfo *ci, const char *name) {
+  const char *val = getenv(name);
+  if (val) {
+    sha256_update(&identsc,strlen(name),name); /* no nul */
+    sha256_update(&identsc,1,"=");
+    ident_addstring(0,val);
+  } else {
+    ident_addstring(0,name);
+  }
+}
+
 #define MAX_OPTS 5
 
 static const struct cmdinfo cmdinfos[]= {
-  { "help",   0, .call= of_help               },
-  { 0, 'g',   1, .sassignto= &ident           },
-  { 0, 'M',   1, .call=of_iassign, .iassignto= &numservers      },
-  { 0, 'D',   0, .iassignto= &debugmode, .arg= 1 },
-  { 0, 'c',   1, .call=of_iassign, .iassignto= &check_interval  },
+  { "help",   0, .call=of_help                                         },
+  { 0, 'g',   1,                    .sassignto= &ident                 },
+  { 0, 'G',   1, .call= ident_addstring                                },
+  { 0, 'E',   1, .call= off_ident_addenv                               },
+  { 0, 'M',   1, .call=of_iassign,  .iassignto= &numservers            },
+  { 0, 'D',   0,                    .iassignto= &debugmode,    .arg= 1 },
+  { 0, 'c',   1, .call=of_iassign,  .iassignto= &check_interval        },
   { 0 }
 };
 
 static uid_t us;
 static const char *run_base, *script, *socket_path;
+static const char *run_base_mkdir_p;
 static int stderr_copy;
 
 static bool find_run_base_var_run(void) {
@@ -239,7 +270,8 @@ static bool find_run_base_home(void) {
   if (sizeof(ut.nodename) > 32)
     ut.nodename[32] = 0;
 
-  try = m_asprintf("%s/%s/%s", pw->pw_dir, ".cgi-fcgi-interp", ut.nodename);
+  run_base_mkdir_p = m_asprintf("%s/%s", pw->pw_dir, ".cgi-fcgi-interp");
+  try = m_asprintf("%/%s", run_base_mkdir_p, ut.nodename);
   run_base = try;
   return 1;
 }
@@ -265,14 +297,12 @@ static void find_socket_path(void) {
 
     int identlen = maxidentlen > 64 ? 64 : maxidentlen;
     char *hexident = xmalloc(identlen + 2);
-    struct sha256_ctx sc;
     unsigned char bbuf[32];
     int i;
 
-    sha256_init(&sc);
-    sha256_update(&sc,strlen(interp)+1,interp);
-    sha256_update(&sc,strlen(script)+1,script);
-    sha256_digest(&sc,sizeof(bbuf),bbuf);
+    ident_addstring(0,interp);
+    ident_addstring(0,script);
+    sha256_digest(&identsc,sizeof(bbuf),bbuf);
 
     for (i=0; i<identlen; i += 2)
       sprintf(hexident+i, "%02x", bbuf[i/2]);
@@ -287,6 +317,11 @@ static void find_socket_path(void) {
         run_base, ident, maxidentlen);
 
   r = mkdir(run_base, 0700);
+  if (r && errno==ENOENT && run_base_mkdir_p) {
+    r = mkdir(run_base_mkdir_p, 0700);
+    if (r) err(127,"mkdir %s (since %s was ENOENT)",run_base_mkdir_p,run_base);
+    r = mkdir(run_base, 0700);
+  }
   if (r) {
     if (!(errno == EEXIST))
       err(127,"mkdir %s",run_base);
@@ -488,6 +523,8 @@ int main(int argc, const char *const *argv) {
     else if (r!=1) errx(127,"open /dev/null for stdout gave bad fd %d",r);
   }
 
+  sha256_init(&identsc);
+
   if (argc>=2 &&
       (smashedopt = argv[1]) &&
       smashedopt[0]=='-' &&