chiark / gitweb /
cgi-fcgi-interp: Reformat option table (nfc)
[chiark-utils.git] / cprogs / cgi-fcgi-interp.c
index 5aeee720910b8eabb51e1148a935073999da90dc..65532e677da1d67368d58680a26ce70719a69da3 100644 (file)
@@ -38,7 +38,7 @@
  *          be an absolute path; will be fed to execvp.
  *
  *  -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
@@ -69,7 +69,6 @@
  * cgi-fcgi-interp automatically expires old sockets, including
  * ones where the named script is out of date.
  */
-
 /*
  * Uses one of two directories
  *   /var/run/user/<UID>/cgi-fcgi-interp/
@@ -78,8 +77,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.
  *
@@ -152,6 +151,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) {
@@ -179,19 +180,25 @@ 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);
+}
+
 #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, '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) {
@@ -240,7 +247,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;
 }
@@ -266,14 +274,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]);
@@ -288,6 +294,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);
@@ -489,6 +500,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]=='-' &&
@@ -549,6 +562,7 @@ int main(int argc, const char *const *argv) {
     execlp("cgi-fcgi",
           "cgi-fcgi", "-connect", socket_path,
           script,
+          m_asprintf("%d", numservers),
           (char*)0);
     err(127,"exec cgi-fcgi");