5 * Passphrase pixie for Catacomb
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
45 #include <sys/types.h>
52 #include <sys/ioctl.h>
56 #include <sys/socket.h>
59 #include <mLib/alloc.h>
60 #include <mLib/dstr.h>
61 #include <mLib/fdflags.h>
62 #include <mLib/mdwopt.h>
63 #include <mLib/quis.h>
64 #include <mLib/report.h>
66 #include <mLib/selbuf.h>
74 #include "passphrase.h"
77 /*----- Static variables --------------------------------------------------*/
79 static unsigned long timeout = 900;
81 static unsigned verbose = 1;
82 static const char *command = 0;
84 static unsigned flags = 0;
89 /*----- Event logging -----------------------------------------------------*/
93 * Arguments: @const char *p@ = @printf@-style format string
94 * @...@ = extra arguments to fill in
98 * Use: Writes out a timestamped log message.
101 static void log(const char *p, ...)
106 if (!(flags & F_SYSLOG)) {
108 struct tm *tm = localtime(&t);
110 d.len += strftime(d.buf, d.sz, "%Y-%m-%d %H:%M:%S ", tm);
113 dstr_vputf(&d, p, &ap);
116 if (flags & F_SYSLOG)
117 syslog(LOG_NOTICE, "%s", d.buf);
120 dstr_write(&d, stderr);
125 /*----- Passphrase management ---------------------------------------------*/
127 /* --- Data structures --- */
129 typedef struct phrase {
139 /* --- Variables --- */
141 #define P_ROOT ((phrase *)&p_root)
142 static struct { phrase *next; phrase *prev; } p_root = { P_ROOT, P_ROOT };
144 /* --- @p_free@ --- *
146 * Arguments: @phrase *p@ = pointer to phrase block
150 * Use: Frees a phrase block.
153 static void p_free(phrase *p)
156 sel_rmtimer(&p->timer);
159 p->next->prev = p->prev;
160 p->prev->next = p->next;
164 /* --- @p_timer@ --- *
166 * Arguments: @struct timeval *tv@ = current time
167 * @void *p@ = pointer to phrase
171 * Use: Expires a passphrase.
174 static void p_timer(struct timeval *tv, void *p)
178 log("expiring passphrase `%s'", pp->tag);
182 /* --- @p_alloc@ --- *
184 * Arguments: @size_t sz@ = amount of memory required
186 * Returns: Pointer to allocated memory, or null.
188 * Use: Allocates some locked memory, flushing old passphrases if
189 * there's not enough space.
192 static void *p_alloc(size_t sz)
196 if ((p = l_alloc(&lm, sz)) != 0)
198 if (P_ROOT->next == P_ROOT)
201 log("flushing passphrase `%s' to free up needed space",
204 p_free(P_ROOT->next);
208 /* --- @p_find@ --- *
210 * Arguments: @const char *tag@ = pointer to tag to find
212 * Returns: Pointer to passphrase block, or null.
214 * Use: Finds a passphrase with a given tag.
217 static phrase *p_find(const char *tag)
221 for (p = P_ROOT->next; p != P_ROOT; p = p->next) {
222 if (strcmp(p->tag, tag) == 0) {
225 sel_rmtimer(&p->timer);
226 gettimeofday(&tv, 0);
228 sel_addtimer(&sel, &p->timer, &tv, p_timer, p);
230 p->next->prev = p->prev;
231 p->prev->next = p->next;
233 p->prev = P_ROOT->prev;
234 P_ROOT->prev->next = p;
244 * Arguments: @const char *tag@ = pointer to tag string
245 * @const char *p@ = pointer to passphrase
246 * @unsigned long t@ = expiry timeout
248 * Returns: Pointer to newly-added passphrase.
250 * Use: Adds a new passphrase. The tag must not already exist.
253 static phrase *p_add(const char *tag, const char *p, unsigned long t)
255 size_t sz = strlen(p) + 1;
256 char *l = p_alloc(sz);
259 /* --- Make sure the locked memory was allocated --- */
264 /* --- Fill in some other bits of the block --- */
269 pp->tag = xstrdup(tag);
272 /* --- Set the timer --- */
277 gettimeofday(&tv, 0);
279 sel_addtimer(&sel, &pp->timer, &tv, p_timer, pp);
282 /* --- Link the block into the chain --- */
285 pp->prev = P_ROOT->prev;
286 P_ROOT->prev->next = pp;
291 /* --- @p_flush@ --- *
293 * Arguments: @const char *tag@ = pointer to tag string, or zero for all
297 * Use: Immediately flushes either a single phrase or all of them.
300 static void p_flush(const char *tag)
304 if (!tag && verbose > 1)
305 log("flushing all passphrases");
307 while (p != P_ROOT) {
308 phrase *pp = p->next;
311 else if (strcmp(p->tag, tag) == 0) {
313 log("flushing passphrase `%s'", tag);
321 /*----- Reading passphrases -----------------------------------------------*/
323 /* --- @p_request@ --- *
325 * Arguments: @const char *msg@ = message string
326 * @const char *tag@ = pointer to tag string
327 * @char *buf@ = pointer to (locked) buffer
328 * @size_t sz@ = size of buffer
330 * Returns: Zero if all went well, nonzero otherwise.
332 * Use: Requests a passphrase from the user.
335 static int p_request(const char *msg, const char *tag, char *buf, size_t sz)
337 /* --- If there's a passphrase-fetching command, run it --- */
347 /* --- Substitute the prompt string into the command --- */
351 const char *q = strchr(p, '%');
374 /* --- Create a pipe and start a child process --- */
378 if ((kid = fork()) < 0)
381 /* --- Child process --- */
385 if (dup2(fd[1], STDOUT_FILENO) < 0)
388 execl("/bin/sh", "sh", "-c", d.buf, (char *)0);
392 /* --- Read the data back into my buffer --- */
395 if ((r = read(fd[0], buf, sz - 1)) >= 0) {
396 char *q = memchr(buf, '\n', r);
402 waitpid(kid, &rc, 0);
404 if (r < 0 || rc != 0)
408 /* --- Tidy up when things go wrong --- */
419 /* --- Read a passphrase from the terminal --- *
421 * Use the standard Catacomb passphrase-reading function, so it'll read the
422 * passphrase from a file descriptor or something if the appropriate
423 * environment variable is set.
429 dstr_putf(&d, "%s %s: ", msg, tag);
430 rc = pixie_getpass(d.buf, buf, sz);
437 /* --- Sort out the buffer --- *
439 * Strip leading spaces.
445 while (isspace((unsigned char)*p))
448 memmove(buf, p, len);
459 * Arguments: @const char **q@ = where to store the result
460 * @const char *tag@ = pointer to tag string
461 * @unsigned mode@ = reading mode (verify?)
462 * @time_t exp@ = expiry time suggestion
464 * Returns: Zero if successful, @-1@ on a read failure, or @+1@ if the
465 * passphrase is missing and there is no fetcher. (This will
466 * always happen if there is no fetcher and @mode@ is
469 * Use: Reads a passphrase from somewhere.
472 static int p_get(const char **q, const char *tag, unsigned mode, time_t exp)
479 /* --- Write a log message --- */
482 log("passphrase `%s' requested", tag);
484 /* --- If there is no fetcher, life is simpler --- */
486 if (!(flags & F_FETCH)) {
487 if (mode == PMODE_VERIFY)
489 if ((p = p_find(tag)) == 0)
495 /* --- Try to find the phrase --- */
497 if (mode == PMODE_VERIFY)
499 if (mode == PMODE_VERIFY || (p = p_find(tag)) == 0) {
500 if ((pp = p_alloc(LBUFSZ)) == 0)
502 if (p_request(mode == PMODE_READ ? "Passphrase" : "New passphrase",
503 tag, pp, LBUFSZ) < 0)
505 p = p_add(tag, pp, exp);
510 /* --- If verification is requested, verify the passphrase --- */
512 if (mode == PMODE_VERIFY) {
513 if (!pp && (pp = p_alloc(LBUFSZ)) == 0)
515 if (p_request("Verify passphrase", tag, pp, LBUFSZ) < 0)
517 if (strcmp(pp, p->p) != 0) {
519 log("passphrases for `%s' don't match", tag);
525 /* --- Tidy up and return the passphrase --- */
528 memset(pp, 0, LBUFSZ);
534 /* --- Tidy up if things went wrong --- */
538 memset(pp, 0, LBUFSZ);
546 /*----- Server command parsing --------------------------------------------*/
548 /* --- Data structures --- */
550 typedef struct pixserv {
559 #define PIXSERV_TIMEOUT 30
561 /* --- @pixserv_expire@ --- *
563 * Arguments: @struct timeval *tv@ = pointer to current time
564 * @void *p@ = pointer to server block
568 * Use: Expires a pixie connection if the remote end decides he's not
569 * interested any more.
572 static void pixserv_expire(struct timeval *tv, void *p)
575 if (px->fd != px->b.reader.fd)
577 selbuf_destroy(&px->b);
578 close(px->b.reader.fd);
582 /* --- @pixserv_write@ --- *
584 * Arguments: @pixserv *px@ = pointer to server block
585 * @const char *p@ = pointer to skeleton string
586 * @...@ = other arguments to fill in
590 * Use: Formats a string and emits it to the output file.
593 static void pixserv_write(pixserv *px, const char *p, ...)
599 dstr_vputf(&d, p, &ap);
600 write(px->fd, d.buf, d.len);
605 /* --- @pixserv_timeout@ --- *
607 * Arguments: @const char *p@ = pointer to timeout string
609 * Returns: Timeout in seconds.
611 * Use: Translates a string to a timeout value in seconds.
614 static unsigned long pixserv_timeout(const char *p)
622 t = strtoul(p, &q, 0);
627 case 's': if (q[1] != 0)
634 /* --- @pixserv_line@ --- *
636 * Arguments: @char *s@ = pointer to the line read
637 * @size_t len@ = length of the line
638 * @void *p@ = pointer to server block
642 * Use: Handles a line read from the client.
645 static void pixserv_line(char *s, size_t len, void *p)
651 /* --- Handle an end-of-file --- */
653 if (!(px->f & px_stdin))
654 sel_rmtimer(&px->timer);
656 if (px->fd != px->b.reader.fd)
658 selbuf_destroy(&px->b);
659 close(px->b.reader.fd);
663 /* --- Fiddle the timeout --- */
665 if (!(px->f & px_stdin)) {
667 gettimeofday(&tv, 0);
668 tv.tv_sec += PIXSERV_TIMEOUT;
669 sel_addtimer(&sel, &px->timer, &tv, pixserv_expire, px);
672 /* --- Scan out the first word --- */
674 if ((q = str_getword(&s)) == 0)
676 for (qq = q; *qq; qq++)
677 *qq = tolower((unsigned char)*qq);
679 /* --- Handle a help request --- */
681 if (strcmp(q, "help") == 0) {
683 INFO Commands supported:\n\
686 INFO PASS tag [expire]\n\
687 INFO VERIFY tag [expire]\n\
689 INFO SET tag [expire] -- phrase\n\
695 /* --- List the passphrases --- */
697 else if (strcmp(q, "list") == 0) {
700 for (p = P_ROOT->next; p != P_ROOT; p = p->next) {
702 pixserv_write(px, "ITEM %s no-expire\n", p->tag);
705 gettimeofday(&tv, 0);
706 TV_SUB(&tv, &p->timer.tv, &tv);
707 pixserv_write(px, "ITEM %s %i\n", p->tag, tv.tv_sec);
710 pixserv_write(px, "OK\n");
713 /* --- Request a passphrase --- */
715 else if ((mode = PMODE_READ, strcmp(q, "pass") == 0) ||
716 (mode = PMODE_VERIFY, strcmp(q, "verify") == 0)) {
721 if ((q = str_getword(&s)) == 0)
722 pixserv_write(px, "FAIL missing tag\n");
723 else if ((t = pixserv_timeout(s)) == 0)
724 pixserv_write(px, "FAIL bad timeout\n");
726 rc = p_get(&p, q, mode, t > timeout ? timeout : t);
729 pixserv_write(px, "OK %s\n", p);
732 pixserv_write(px, "FAIL error reading passphrase\n");
735 pixserv_write(px, "MISSING\n");
741 /* --- Flush existing passphrases --- */
743 else if (strcmp(q, "flush") == 0) {
746 pixserv_write(px, "OK\n");
749 /* --- Set a passphrase --- */
751 else if (strcmp(q, "set") == 0) {
754 if ((tag = str_getword(&s)) == 0)
755 pixserv_write(px, "FAIL missing tag\n");
756 else if ((q = str_getword(&s)) == 0)
757 pixserv_write(px, "FAIL no passphrase\n");
759 if (strcmp(q, "--") != 0) {
760 t = pixserv_timeout(q);
763 t = pixserv_timeout(0);
765 pixserv_write(px, "FAIL no passphrase\n");
766 else if (strcmp(q, "--") != 0)
767 pixserv_write(px, "FAIL rubbish found before passphrase\n");
771 pixserv_write(px, "OK\n");
776 /* --- Shut the server down --- */
778 else if (strcmp(q, "quit") == 0) {
780 log("%s client requested shutdown",
781 px->f & px_stdin ? "local" : "remote");
782 pixserv_write(px, "OK\n");
786 /* --- Report an error for other commands --- */
789 pixserv_write(px, "FAIL unknown command `%s'\n", q);
792 /* --- @pixserv_create@ --- *
794 * Arguments: @int fd@ = file descriptor to read from
795 * @int ofd@ = file descriptor to write to
797 * Returns: Pointer to the new connection.
799 * Use: Creates a new Pixie server instance for a new connection.
802 static pixserv *pixserv_create(int fd, int ofd)
804 pixserv *px = CREATE(pixserv);
806 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
808 fdflags(ofd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
810 selbuf_init(&px->b, &sel, fd, pixserv_line, px);
811 px->b.b.a = arena_secure;
812 selbuf_setsize(&px->b, 1024);
813 gettimeofday(&tv, 0);
814 tv.tv_sec += PIXSERV_TIMEOUT;
815 sel_addtimer(&sel, &px->timer, &tv, pixserv_expire, px);
820 /* --- @pixserv_accept@ --- *
822 * Arguments: @int fd@ = file descriptor
823 * @unsigned mode@ = what's happened
824 * @void *p@ = an uninteresting argument
828 * Use: Accepts a new connection.
831 static void pixserv_accept(int fd, unsigned mode, void *p)
834 struct sockaddr_un sun;
835 size_t sunsz = sizeof(sun);
837 if (mode != SEL_READ)
839 if ((nfd = accept(fd, (struct sockaddr *)&sun, &sunsz)) < 0) {
840 if (verbose && errno != EAGAIN && errno != EWOULDBLOCK &&
841 errno != ECONNABORTED && errno != EPROTO && errno != EINTR)
842 log("new connection failed: %s", strerror(errno));
845 pixserv_create(nfd, nfd);
848 /*----- Setting up the server ---------------------------------------------*/
850 /* --- @unlinksocket@ --- *
856 * Use: Tidies up the socket when it's finished with.
859 static char *sockpath;
861 static void unlinksocket(void)
867 /* --- @pix_sigdie@ --- *
869 * Arguments: @int sig@ = signal number
870 * @void *p@ = uninteresting argument
874 * Use: Shuts down the program after a fatal signal.
877 static void pix_sigdie(int sig, void *p)
884 case SIGTERM: p = "SIGTERM"; break;
885 case SIGINT: p = "SIGINT"; break;
887 sprintf(buf, "signal %i", sig);
891 log("shutting down on %s", p);
896 /* --- @pix_sigflush@ --- *
898 * Arguments: @int sig@ = signal number
899 * @void *p@ = uninteresting argument
903 * Use: Flushes the passphrase cache on receipt of a signal.
906 static void pix_sigflush(int sig, void *p)
913 case SIGHUP: p = "SIGHUP"; break;
914 case SIGQUIT: p = "SIGQUIT"; break;
916 sprintf(buf, "signal %i", sig);
920 log("received %s; flushing passphrases", p);
925 /* --- @pix_setup@ --- *
927 * Arguments: @struct sockaddr_un *sun@ = pointer to address to use
928 * @size_t sz@ = size of socket address
932 * Use: Sets up the pixie's Unix-domain socket.
935 static void pix_setup(struct sockaddr_un *sun, size_t sz)
939 /* --- Set up the parent directory --- */
942 char *p = sun->sun_path;
943 char *q = strrchr(p, '/');
953 if (stat(d.buf, &st))
954 die(1, "couldn't stat `%s': %s", d.buf, strerror(errno));
955 if (!S_ISDIR(st.st_mode))
956 die(1, "object `%s' isn't a directory", d.buf);
957 if (st.st_mode & 0077)
958 die(1, "parent directory `%s' has group or world access", d.buf);
963 /* --- Initialize the socket --- */
971 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
972 die(1, "couldn't create socket: %s", strerror(errno));
973 if (bind(fd, (struct sockaddr *)sun, sz) < 0) {
975 if (errno != EADDRINUSE)
976 die(1, "couldn't bind to address: %s", strerror(e));
978 die(1, "too many retries; giving up");
980 if (connect(fd, (struct sockaddr *)sun, sz)) {
982 if (errno != ECONNREFUSED)
983 die(1, "couldn't bind to address: %s", strerror(e));
984 if (stat(sun->sun_path, &st))
985 die(1, "couldn't stat `%s': %s", sun->sun_path, strerror(errno));
986 if (!S_ISSOCK(st.st_mode))
987 die(1, "object `%s' isn't a socket", sun->sun_path);
989 log("stale socket found; removing it");
990 unlink(sun->sun_path);
994 log("server already running; shutting it down");
995 write(fd, "QUIT\n", 5);
1001 chmod(sun->sun_path, 0600);
1002 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
1004 die(1, "couldn't listen on socket: %s", strerror(errno));
1007 /* --- Set up the rest of the server --- */
1010 static sel_file serv;
1011 sockpath = sun->sun_path;
1012 atexit(unlinksocket);
1013 sel_initfile(&sel, &serv, fd, SEL_READ, pixserv_accept, 0);
1018 /*----- Client support code -----------------------------------------------*/
1020 /* --- Variables --- */
1022 static selbuf c_server, c_client;
1023 static unsigned c_flags = 0;
1025 #define cf_uclose 1u
1026 #define cf_sclose 2u
1027 #define cf_cooked 4u
1029 /* --- Line handler functions --- */
1031 static void c_uline(char *s, size_t len, void *p)
1034 selbuf_destroy(&c_client);
1035 shutdown(c_server.reader.fd, 1);
1036 c_flags |= cf_uclose;
1039 write(c_server.reader.fd, s, len);
1043 static void c_sline(char *s, size_t len, void *p)
1046 selbuf_destroy(&c_server);
1047 if (!(c_flags & cf_uclose)) {
1048 moan("server closed the connection");
1049 selbuf_destroy(&c_client);
1053 if (!(c_flags & cf_cooked))
1056 char *q = str_getword(&s);
1057 if (strcmp(q, "FAIL") == 0)
1059 else if (strcmp(q, "INFO") == 0 ||
1060 strcmp(q, "ITEM") == 0)
1062 else if (strcmp(q, "OK") == 0) {
1063 if (s && *s) puts(s);
1064 } else if (strcmp(q, "MISSING") == 0)
1067 moan("unexpected output: %s %s", q, s);
1071 /* --- @pix_client@ --- *
1073 * Arguments: @struct sockaddr_un *sun@ = pointer to socket address
1074 * @size_t sz@ = size of socket address
1075 * @char *argv[]@ = pointer to arguments to send
1079 * Use: Performs client-side actions for the passphrase pixie.
1082 static void pix_client(struct sockaddr_un *sun, size_t sz, char *argv[])
1086 /* --- Dispose of locked memory --- */
1090 /* --- Open the socket --- */
1092 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
1093 die(1, "couldn't create socket: %s", strerror(errno));
1094 if (connect(fd, (struct sockaddr *)sun, sz))
1095 die(1, "couldn't connect to server: %s", strerror(errno));
1096 selbuf_init(&c_server, &sel, fd, c_sline, 0);
1098 /* --- If there are any arguments, turn them into a string --- */
1101 selbuf_init(&c_client, &sel, STDIN_FILENO, c_uline, 0);
1110 write(fd, d.buf, d.len);
1112 c_flags |= cf_uclose | cf_cooked;
1116 /* --- And repeat --- */
1119 if (sel_select(&sel))
1120 die(EXIT_FAILURE, "select error: %s", strerror(errno));
1124 /*----- Main code ---------------------------------------------------------*/
1126 /* --- @help@, @version@, @usage@ --- *
1128 * Arguments: @FILE *fp@ = stream to write on
1132 * Use: Emit helpful messages.
1135 static void usage(FILE *fp)
1139 $ [-qvfidl] [-c COMMAND] [-t TIMEOUT] [-s SOCKET]\n\
1140 $ [-s SOCKET] -C [COMMAND ARGS...]\n\
1141 $ [-s SOCKET] -P[P] TAG\n\
1145 static void version(FILE *fp)
1147 pquis(fp, "$, Catacomb version " VERSION "\n");
1150 static void help(FILE *fp)
1156 The Catacomb passphrase pixie collects and caches passphrases used to\n\
1157 protect important keys. Options provided:\n\
1159 -h, --help Show this help text.\n\
1160 -V, --version Show the program's version number.\n\
1161 -u, --usage Show a (very) terse usage summary.\n\
1163 -C, --client Connect to a running pixie as a client.\n\
1164 -P, --passphrase Request passphrase TAG and print to stdout.\n\
1165 -PP, --verify-passphrase\n\
1166 Verify passphrase TAG and print to stdout.\n\
1168 -q, --quiet Emit fewer log messages.\n\
1169 -v, --version Emit more log messages.\n\
1170 -s, --socket=FILE Name the pixie's socket.\n\
1171 -c, --command=COMMAND Shell command to read a passphrase.\n\
1172 -f, --fetch Fetch passphrases from the terminal.\n\
1173 -t, --timeout=TIMEOUT Length of time to retain a passphrase in memory.\n\
1174 -i, --interactive Allow commands to be typed interactively.\n\
1175 -d, --daemon Fork into the background after initialization.\n\
1176 -l, --syslog Emit log messages to the system log.\n\
1178 The COMMAND may contain `%m' and `%t' markers which are replaced by a\n\
1179 prompt message and the passphrase tag respectively. The TIMEOUT is an\n\
1180 integer, optionally followed by `d', `h', `m' or `s' to specify units of\n\
1181 days, hours, minutes or seconds respectively.\n\
1183 In client mode, if a command is specified on the command line, it is sent\n\
1184 to the running server; otherwise the program reads requests from stdin.\n\
1185 Responses from the pixie are written to stdout. Send a HELP request for\n\
1186 a quick summary of the pixie communication protocol.\n\
1192 * Arguments: @int argc@ = number of arguments
1193 * @char *argv[]@ = vector of argument values
1195 * Returns: Zero if OK.
1197 * Use: Main program. Listens on a socket and responds with a PGP
1198 * passphrase when asked.
1201 int main(int argc, char *argv[])
1204 struct sockaddr_un *sun;
1212 #define f_syslog 16u
1214 #define f_verify 64u
1216 /* --- Initialize libraries --- */
1221 /* --- Set up the locked memory area --- */
1226 /* --- Parse command line arguments --- */
1229 static struct option opts[] = {
1231 /* --- Standard GNUy help options --- */
1233 { "help", 0, 0, 'h' },
1234 { "version", 0, 0, 'V' },
1235 { "usage", 0, 0, 'u' },
1237 /* --- Other options --- */
1239 { "quiet", 0, 0, 'q' },
1240 { "verbose", 0, 0, 'v' },
1241 { "client", 0, 0, 'C' },
1242 { "passphrase", 0, 0, 'P' },
1243 { "verify-passphrase", 0, 0, '+' },
1244 { "socket", OPTF_ARGREQ, 0, 's' },
1245 { "command", OPTF_ARGREQ, 0, 'c' },
1246 { "fetch", 0, 0, 'f' },
1247 { "timeout", OPTF_ARGREQ, 0, 't' },
1248 { "interactive", 0, 0, 'i' },
1249 { "stdin", 0, 0, 'i' },
1250 { "daemon", 0, 0, 'd' },
1251 { "log", 0, 0, 'l' },
1252 { "syslog", 0, 0, 'l' },
1254 /* --- Magic terminator --- */
1259 int i = mdwopt(argc, argv, "hVuqvCPs:c:ft:idl", opts, 0, 0, 0);
1265 /* --- GNUy help options --- */
1277 /* --- Other interesting things --- */
1297 f |= f_fetch | f_verify;
1304 if ((timeout = pixserv_timeout(optarg)) == 0)
1305 die(1, "bad timeout `%s'", optarg);
1324 /* --- Something else --- */
1333 (optind < argc && !(f & (f_client|f_fetch))) ||
1334 ((f & f_fetch) && optind != argc - 1)) {
1339 /* --- Handle request for a passphrase --- */
1342 char *buf = l_alloc(&lm, 1024);
1343 passphrase_connect(path);
1344 if (passphrase_read(argv[optind],
1345 (f & f_verify) ? PMODE_VERIFY : PMODE_READ,
1347 die(1, "failed to read passphrase: %s", strerror(errno));
1352 /* --- Set up the socket address --- */
1354 sun = pixie_address(path, &sz);
1356 /* --- Initialize selectory --- */
1359 signal(SIGPIPE, SIG_IGN);
1361 /* --- Be a client if a client's wanted --- */
1364 pix_client(sun, sz, argv + optind);
1366 /* --- Open the syslog if requested --- */
1370 openlog(QUIS, 0, LOG_DAEMON);
1373 /* --- Check on the locked memory area --- */
1377 int rc = l_report(&lm, &d);
1379 die(EXIT_FAILURE, d.buf);
1380 else if (rc && verbose) {
1382 log("couldn't lock passphrase buffer");
1385 arena_setsecure(&lm.a);
1388 /* --- Set signal behaviours --- */
1391 static sig sigint, sigterm, sigquit, sighup;
1392 struct sigaction sa;
1394 sigaction(SIGINT, 0, &sa);
1395 if (sa.sa_handler != SIG_IGN)
1396 sig_add(&sigint, SIGINT, pix_sigdie, 0);
1397 sig_add(&sigterm, SIGTERM, pix_sigdie, 0);
1398 sig_add(&sigquit, SIGQUIT, pix_sigflush, 0);
1399 sig_add(&sighup, SIGHUP, pix_sigflush, 0);
1402 /* --- Set up the server --- */
1406 pixserv *px = pixserv_create(STDIN_FILENO, STDOUT_FILENO);
1407 sel_rmtimer(&px->timer);
1411 /* --- Fork into the background if requested --- */
1416 if (((f & f_stdin) &&
1417 (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO))) ||
1418 (!command && (flags & F_FETCH)))
1419 die(1, "can't become a daemon if terminal required");
1421 if ((kid = fork()) < 0)
1422 die(1, "fork failed: %s", strerror(errno));
1428 if ((fd = open("/dev/tty", O_RDONLY)) >= 0) {
1429 ioctl(fd, TIOCNOTTY);
1442 log("initialized ok");
1447 if (!sel_select(&sel))
1449 else if (errno != EINTR && errno != EAGAIN) {
1450 log("error from select: %s", strerror(errno));
1453 log("too many consecutive select errors: bailing out");
1462 /*----- That's all, folks -------------------------------------------------*/