chiark / gitweb /
cgi-fcgi-interp: Make ident_sc global, and introduce ident_addstring (nfc)
[chiark-utils.git] / cprogs / cgi-fcgi-interp.c
index ef48cfda3b80af8b02e6375639943f07558a44cf..7a6323e267e786b08c3253ca7695d46ec48dae98 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
@@ -77,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.
  *
@@ -151,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) {
@@ -178,6 +180,11 @@ 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[]= {
@@ -191,6 +198,7 @@ static const struct cmdinfo cmdinfos[]= {
 
 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 +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;
 }
@@ -265,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]);
@@ -287,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);
@@ -488,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]=='-' &&