From: mdw Date: Sat, 29 Jul 2000 22:05:22 +0000 (+0000) Subject: Miscellaneous tidyings: X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/4cb04adbd7653fc4d4674efce22293854d2d6e0b Miscellaneous tidyings: * Change the timeout to something more appropriate for real use. * Check assumptions about object types when binding the socket. In particular, don't zap the socket if it's really something else. * In @p_request@, return a failure if the shell command returned nonzero. Fix a bug in @p_get@ which incorrectly passes on a success code when this happens. * Dispose of the locked memory in client mode to avoid being antisocial. * Also in client mode, don't report closure from the server if we're running noninteractively. * Insert a missing option letter into the usage string. * Change to the root directory after forking in daemon mode. --- diff --git a/pixie.c b/pixie.c index 1ee1bab..441b732 100644 --- a/pixie.c +++ b/pixie.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: pixie.c,v 1.4 2000/06/17 11:50:53 mdw Exp $ + * $Id: pixie.c,v 1.5 2000/07/29 22:05:22 mdw Exp $ * * Passphrase pixie for Catacomb * @@ -30,6 +30,28 @@ /*----- Revision history --------------------------------------------------* * * $Log: pixie.c,v $ + * Revision 1.5 2000/07/29 22:05:22 mdw + * Miscellaneous tidyings: + * + * * Change the timeout to something more appropriate for real use. + * + * * Check assumptions about object types when binding the socket. In + * particular, don't zap the socket if it's really something else. + * + * * In @p_request@, return a failure if the shell command returned + * nonzero. Fix a bug in @p_get@ which incorrectly passes on a success + * code when this happens. + * + * * Dispose of the locked memory in client mode to avoid being + * antisocial. + * + * * Also in client mode, don't report closure from the server if we're + * running noninteractively. + * + * * Insert a missing option letter into the usage string. + * + * * Change to the root directory after forking in daemon mode. + * * Revision 1.4 2000/06/17 11:50:53 mdw * New pixie protocol allowing application to request passphrases and send * them to the pixie. Use the secure arena interface for the input @@ -95,7 +117,7 @@ /*----- Static variables --------------------------------------------------*/ -static unsigned long timeout = 300; +static unsigned long timeout = 900; static sel_state sel; static unsigned verbose = 1; static const char *command = 0; @@ -363,6 +385,7 @@ static int p_request(const char *msg, const char *tag, char *buf, size_t sz) int fd[2]; pid_t kid; int r; + int rc; /* --- Substitute the prompt string into the command --- */ @@ -419,9 +442,9 @@ static int p_request(const char *msg, const char *tag, char *buf, size_t sz) *q = 0; } close(fd[0]); - waitpid(kid, 0, 0); + waitpid(kid, &rc, 0); dstr_destroy(&d); - if (r < 0) + if (r < 0 || rc != 0) goto fail_0; goto ok; @@ -558,7 +581,7 @@ fail: memset(pp, 0, LBUFSZ); l_free(&lm, pp); } - return (0); + return (-1); #undef LBUFSZ } @@ -957,11 +980,11 @@ static void pix_setup(struct sockaddr_un *sun, size_t sz) /* --- Set up the parent directory --- */ { - dstr d = DSTR_INIT; char *p = sun->sun_path; char *q = strrchr(p, '/'); if (q) { + dstr d = DSTR_INIT; struct stat st; DPUTM(&d, p, q - p); @@ -970,8 +993,11 @@ static void pix_setup(struct sockaddr_un *sun, size_t sz) mkdir(d.buf, 0700); if (stat(d.buf, &st)) die(1, "couldn't stat `%s': %s", d.buf, strerror(errno)); + if (!S_ISDIR(st.st_mode)) + die(1, "object `%s' isn't a directory", d.buf); if (st.st_mode & 0077) die(1, "parent directory `%s' has group or world access", d.buf); + dstr_destroy(&d); } } @@ -993,8 +1019,13 @@ static void pix_setup(struct sockaddr_un *sun, size_t sz) die(1, "too many retries; giving up"); n--; if (connect(fd, (struct sockaddr *)sun, sz)) { + struct stat st; if (errno != ECONNREFUSED) die(1, "couldn't bind to address: %s", strerror(e)); + if (stat(sun->sun_path, &st)) + die(1, "couldn't stat `%s': %s", sun->sun_path, strerror(errno)); + if (!S_ISSOCK(st.st_mode)) + die(1, "object `%s' isn't a socket", sun->sun_path); if (verbose) log("stale socket found; removing it"); unlink(sun->sun_path); @@ -1077,6 +1108,10 @@ static void pix_client(struct sockaddr_un *sun, size_t sz, char *argv[]) { int fd; + /* --- Dispose of locked memory --- */ + + l_destroy(&lm); + /* --- Open the socket --- */ if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) @@ -1099,6 +1134,7 @@ static void pix_client(struct sockaddr_un *sun, size_t sz, char *argv[]) DPUTC(&d, '\n'); write(fd, d.buf, d.len); shutdown(fd, 1); + c_flags |= cf_uclose; dstr_destroy(&d); } @@ -1123,7 +1159,7 @@ static void usage(FILE *fp) { pquis(fp, "\ Usage:\n\ - $ [-qvidl] [-c command] [-t timeout] [-s socket]\n\ + $ [-qvfidl] [-c command] [-t timeout] [-s socket]\n\ $ [-s socket] -C [command args...]\n\ "); } @@ -1383,6 +1419,7 @@ int main(int argc, char *argv[]) } } #endif + chdir("/"); setsid(); if (fork() > 0)