3 * $Id: pixie.c,v 1.6 2000/10/08 12:06:46 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.6 2000/10/08 12:06:46 mdw
34 * Change size passed to socket function to be a @size_t@. Insert missing
35 * type name for flag declaration.
37 * Revision 1.5 2000/07/29 22:05:22 mdw
38 * Miscellaneous tidyings:
40 * * Change the timeout to something more appropriate for real use.
42 * * Check assumptions about object types when binding the socket. In
43 * particular, don't zap the socket if it's really something else.
45 * * In @p_request@, return a failure if the shell command returned
46 * nonzero. Fix a bug in @p_get@ which incorrectly passes on a success
47 * code when this happens.
49 * * Dispose of the locked memory in client mode to avoid being
52 * * Also in client mode, don't report closure from the server if we're
53 * running noninteractively.
55 * * Insert a missing option letter into the usage string.
57 * * Change to the root directory after forking in daemon mode.
59 * Revision 1.4 2000/06/17 11:50:53 mdw
60 * New pixie protocol allowing application to request passphrases and send
61 * them to the pixie. Use the secure arena interface for the input
62 * buffer. Extend the input buffer. Other minor fixes.
64 * Revision 1.3 1999/12/22 22:14:40 mdw
65 * Only produce initialization message if verbose.
67 * Revision 1.2 1999/12/22 22:13:42 mdw
68 * Fix bug in passphrase flushing loop.
70 * Revision 1.1 1999/12/22 15:58:41 mdw
71 * Passphrase pixie support.
75 /*----- Header files ------------------------------------------------------*/
90 #include <sys/types.h>
97 #include <sys/ioctl.h>
101 #include <sys/socket.h>
104 #include <mLib/alloc.h>
105 #include <mLib/dstr.h>
106 #include <mLib/fdflags.h>
107 #include <mLib/mdwopt.h>
108 #include <mLib/quis.h>
109 #include <mLib/report.h>
110 #include <mLib/sel.h>
111 #include <mLib/selbuf.h>
112 #include <mLib/sig.h>
113 #include <mLib/str.h>
114 #include <mLib/sub.h>
119 #include "passphrase.h"
122 /*----- Static variables --------------------------------------------------*/
124 static unsigned long timeout
= 900;
125 static sel_state sel
;
126 static unsigned verbose
= 1;
127 static const char *command
= 0;
129 static unsigned flags
= 0;
136 /*----- Event logging -----------------------------------------------------*/
140 * Arguments: @const char *p@ = @printf@-style format string
141 * @...@ = extra arguments to fill in
145 * Use: Writes out a timestamped log message.
148 static void log(const char *p
, ...)
153 if (!(flags
& F_SYSLOG
)) {
155 struct tm
*tm
= localtime(&t
);
157 d
.len
+= strftime(d
.buf
, d
.sz
, "%Y-%m-%d %H:%M:%S ", tm
);
160 dstr_vputf(&d
, p
, ap
);
163 if (flags
& F_SYSLOG
)
164 syslog(LOG_NOTICE
, "%s", d
.buf
);
167 dstr_write(&d
, stderr
);
172 /*----- Passphrase management ---------------------------------------------*/
174 /* --- Data structures --- */
176 typedef struct phrase
{
186 /* --- Variables --- */
188 #define P_ROOT ((phrase *)&p_root)
189 static struct { phrase
*next
; phrase
*prev
; } p_root
= { P_ROOT
, P_ROOT
};
191 /* --- @p_free@ --- *
193 * Arguments: @phrase *p@ = pointer to phrase block
197 * Use: Frees a phrase block.
200 static void p_free(phrase
*p
)
203 sel_rmtimer(&p
->timer
);
206 p
->next
->prev
= p
->prev
;
207 p
->prev
->next
= p
->next
;
211 /* --- @p_timer@ --- *
213 * Arguments: @struct timeval *tv@ = current time
214 * @void *p@ = pointer to phrase
218 * Use: Expires a passphrase.
221 static void p_timer(struct timeval
*tv
, void *p
)
225 log("expiring passphrase `%s'", pp
->tag
);
229 /* --- @p_alloc@ --- *
231 * Arguments: @size_t sz@ = amount of memory required
233 * Returns: Pointer to allocated memory, or null.
235 * Use: Allocates some locked memory, flushing old passphrases if
236 * there's not enough space.
239 static void *p_alloc(size_t sz
)
243 if ((p
= l_alloc(&lm
, sz
)) != 0)
245 if (P_ROOT
->next
== P_ROOT
)
248 log("flushing passphrase `%s' to free up needed space",
251 p_free(P_ROOT
->next
);
255 /* --- @p_find@ --- *
257 * Arguments: @const char *tag@ = pointer to tag to find
259 * Returns: Pointer to passphrase block, or null.
261 * Use: Finds a passphrase with a given tag.
264 static phrase
*p_find(const char *tag
)
268 for (p
= P_ROOT
->next
; p
!= P_ROOT
; p
= p
->next
) {
269 if (strcmp(p
->tag
, tag
) == 0) {
272 sel_rmtimer(&p
->timer
);
273 gettimeofday(&tv
, 0);
275 sel_addtimer(&sel
, &p
->timer
, &tv
, p_timer
, p
);
277 p
->next
->prev
= p
->prev
;
278 p
->prev
->next
= p
->next
;
280 p
->prev
= P_ROOT
->prev
;
281 P_ROOT
->prev
->next
= p
;
291 * Arguments: @const char *tag@ = pointer to tag string
292 * @const char *p@ = pointer to passphrase
293 * @unsigned long t@ = expiry timeout
295 * Returns: Pointer to newly-added passphrase.
297 * Use: Adds a new passphrase. The tag must not already exist.
300 static phrase
*p_add(const char *tag
, const char *p
, unsigned long t
)
302 size_t sz
= strlen(p
) + 1;
303 char *l
= p_alloc(sz
);
306 /* --- Make sure the locked memory was allocated --- */
311 /* --- Fill in some other bits of the block --- */
316 pp
->tag
= xstrdup(tag
);
319 /* --- Set the timer --- */
324 gettimeofday(&tv
, 0);
326 sel_addtimer(&sel
, &pp
->timer
, &tv
, p_timer
, pp
);
329 /* --- Link the block into the chain --- */
332 pp
->prev
= P_ROOT
->prev
;
333 P_ROOT
->prev
->next
= pp
;
338 /* --- @p_flush@ --- *
340 * Arguments: @const char *tag@ = pointer to tag string, or zero for all
344 * Use: Immediately flushes either a single phrase or all of them.
347 static void p_flush(const char *tag
)
351 if (!tag
&& verbose
> 1)
352 log("flushing all passphrases");
354 while (p
!= P_ROOT
) {
355 phrase
*pp
= p
->next
;
358 else if (strcmp(p
->tag
, tag
) == 0) {
360 log("flushing passphrase `%s'", tag
);
368 /*----- Reading passphrases -----------------------------------------------*/
370 /* --- @p_request@ --- *
372 * Arguments: @const char *msg@ = message string
373 * @const char *tag@ = pointer to tag string
374 * @char *buf@ = pointer to (locked) buffer
375 * @size_t sz@ = size of buffer
377 * Returns: Zero if all went well, nonzero otherwise.
379 * Use: Requests a passphrase from the user.
382 static int p_request(const char *msg
, const char *tag
, char *buf
, size_t sz
)
384 /* --- If there's a passphrase-fetching command, run it --- */
394 /* --- Substitute the prompt string into the command --- */
398 const char *q
= strchr(p
, '%');
421 /* --- Create a pipe and start a child process --- */
425 if ((kid
= fork()) < 0)
428 /* --- Child process --- */
432 if (dup2(fd
[1], STDOUT_FILENO
) < 0)
435 execl("/bin/sh", "sh", "-c", d
.buf
, (void *)0);
439 /* --- Read the data back into my buffer --- */
442 if ((r
= read(fd
[0], buf
, sz
- 1)) >= 0) {
443 char *q
= memchr(buf
, '\n', r
);
449 waitpid(kid
, &rc
, 0);
451 if (r
< 0 || rc
!= 0)
455 /* --- Tidy up when things go wrong --- */
466 /* --- Read a passphrase from the terminal --- *
468 * Use the standard Catacomb passphrase-reading function, so it'll read the
469 * passphrase from a file descriptor or something if the appropriate
470 * environment variable is set.
476 dstr_putf(&d
, "%s %s: ", msg
, tag
);
477 rc
= pixie_getpass(d
.buf
, buf
, sz
);
484 /* --- Sort out the buffer --- *
486 * Strip leading spaces.
492 while (isspace((unsigned char)*p
))
495 memmove(buf
, p
, len
);
506 * Arguments: @const char **q@ = where to store the result
507 * @const char *tag@ = pointer to tag string
508 * @unsigned mode@ = reading mode (verify?)
509 * @time_t exp@ = expiry time suggestion
511 * Returns: Zero if successful, @-1@ on a read failure, or @+1@ if the
512 * passphrase is missing and there is no fetcher. (This will
513 * always happen if there is no fetcher and @mode@ is
516 * Use: Reads a passphrase from somewhere.
519 static int p_get(const char **q
, const char *tag
, unsigned mode
, time_t exp
)
526 /* --- Write a log message --- */
529 log("passphrase `%s' requested", tag
);
531 /* --- If there is no fetcher, life is simpler --- */
533 if (!(flags
& F_FETCH
)) {
534 if (mode
== PMODE_VERIFY
)
536 if ((p
= p_find(tag
)) == 0)
542 /* --- Try to find the phrase --- */
544 if (mode
== PMODE_VERIFY
)
546 if (mode
== PMODE_VERIFY
|| (p
= p_find(tag
)) == 0) {
547 if ((pp
= p_alloc(LBUFSZ
)) == 0)
549 if (p_request(mode
== PMODE_READ ?
"Passphrase" : "New passphrase",
550 tag
, pp
, LBUFSZ
) < 0)
552 p
= p_add(tag
, pp
, exp
);
557 /* --- If verification is requested, verify the passphrase --- */
559 if (mode
== PMODE_VERIFY
) {
560 if (!pp
&& (pp
= p_alloc(LBUFSZ
)) == 0)
562 if (p_request("Verify passphrase", tag
, pp
, LBUFSZ
) < 0)
564 if (strcmp(pp
, p
->p
) != 0) {
566 log("passphrases for `%s' don't match", tag
);
572 /* --- Tidy up and return the passphrase --- */
575 memset(pp
, 0, LBUFSZ
);
581 /* --- Tidy up if things went wrong --- */
585 memset(pp
, 0, LBUFSZ
);
593 /*----- Server command parsing --------------------------------------------*/
595 /* --- Data structures --- */
597 typedef struct pixserv
{
604 enum { px_stdin
= 1 };
606 #define PIXSERV_TIMEOUT 30
608 /* --- @pixserv_expire@ --- *
610 * Arguments: @struct timeval *tv@ = pointer to current time
611 * @void *p@ = pointer to server block
615 * Use: Expires a pixie connection if the remote end decides he's not
616 * interested any more.
619 static void pixserv_expire(struct timeval
*tv
, void *p
)
622 if (px
->fd
!= px
->b
.reader
.fd
)
624 selbuf_destroy(&px
->b
);
625 close(px
->b
.reader
.fd
);
629 /* --- @pixserv_write@ --- *
631 * Arguments: @pixserv *px@ = pointer to server block
632 * @const char *p@ = pointer to skeleton string
633 * @...@ = other arguments to fill in
637 * Use: Formats a string and emits it to the output file.
640 static void pixserv_write(pixserv
*px
, const char *p
, ...)
646 dstr_vputf(&d
, p
, ap
);
647 write(px
->fd
, d
.buf
, d
.len
);
652 /* --- @pixserv_timeout@ --- *
654 * Arguments: @const char *p@ = pointer to timeout string
656 * Returns: Timeout in seconds.
658 * Use: Translates a string to a timeout value in seconds.
661 static unsigned long pixserv_timeout(const char *p
)
669 t
= strtoul(p
, &q
, 0);
674 case 's': if (q
[1] != 0)
681 /* --- @pixserv_line@ --- *
683 * Arguments: @char *s@ = pointer to the line read
684 * @void *p@ = pointer to server block
688 * Use: Handles a line read from the client.
691 static void pixserv_line(char *s
, void *p
)
697 /* --- Handle an end-of-file --- */
699 if (!(px
->f
& px_stdin
))
700 sel_rmtimer(&px
->timer
);
702 if (px
->fd
!= px
->b
.reader
.fd
)
704 selbuf_destroy(&px
->b
);
705 close(px
->b
.reader
.fd
);
709 /* --- Fiddle the timeout --- */
711 if (!(px
->f
& px_stdin
)) {
713 gettimeofday(&tv
, 0);
714 tv
.tv_sec
+= PIXSERV_TIMEOUT
;
715 sel_addtimer(&sel
, &px
->timer
, &tv
, pixserv_expire
, px
);
718 /* --- Scan out the first word --- */
720 if ((q
= str_getword(&s
)) == 0)
722 for (qq
= q
; *qq
; qq
++)
723 *qq
= tolower((unsigned char)*qq
);
725 /* --- Handle a help request --- */
727 if (strcmp(q
, "help") == 0) {
729 INFO Commands supported:\n\
732 INFO PASS tag [expire]\n\
733 INFO VERIFY tag [expire]\n\
735 INFO SET tag [expire] -- phrase\n\
741 /* --- List the passphrases --- */
743 else if (strcmp(q
, "list") == 0) {
746 for (p
= P_ROOT
->next
; p
!= P_ROOT
; p
= p
->next
) {
748 pixserv_write(px
, "ITEM %s no-expire\n", p
->tag
);
751 gettimeofday(&tv
, 0);
752 TV_SUB(&tv
, &p
->timer
.tv
, &tv
);
753 pixserv_write(px
, "ITEM %s %i\n", p
->tag
, tv
.tv_sec
);
756 pixserv_write(px
, "OK\n");
759 /* --- Request a passphrase --- */
761 else if ((mode
= PMODE_READ
, strcmp(q
, "pass") == 0) ||
762 (mode
= PMODE_VERIFY
, strcmp(q
, "verify") == 0)) {
767 if ((q
= str_getword(&s
)) == 0)
768 pixserv_write(px
, "FAIL missing tag\n");
769 else if ((t
= pixserv_timeout(s
)) == 0)
770 pixserv_write(px
, "FAIL bad timeout\n");
772 rc
= p_get(&p
, q
, mode
, t
> timeout ? timeout
: t
);
775 pixserv_write(px
, "OK %s\n", p
);
778 pixserv_write(px
, "FAIL error reading passphrase\n");
781 pixserv_write(px
, "MISSING\n");
787 /* --- Flush existing passphrases --- */
789 else if (strcmp(q
, "flush") == 0) {
792 pixserv_write(px
, "OK\n");
795 /* --- Set a passphrase --- */
797 else if (strcmp(q
, "set") == 0) {
800 if ((tag
= str_getword(&s
)) == 0)
801 pixserv_write(px
, "FAIL missing tag\n");
802 else if ((q
= str_getword(&s
)) == 0)
803 pixserv_write(px
, "FAIL no passphrase\n");
805 if (strcmp(q
, "--") != 0) {
806 t
= pixserv_timeout(q
);
809 t
= pixserv_timeout(0);
811 pixserv_write(px
, "FAIL no passphrase\n");
812 else if (strcmp(q
, "--") != 0)
813 pixserv_write(px
, "FAIL rubbish found before passphrase\n");
817 pixserv_write(px
, "OK\n");
822 /* --- Shut the server down --- */
824 else if (strcmp(q
, "quit") == 0) {
826 log("%s client requested shutdown",
827 px
->f
& px_stdin ?
"local" : "remote");
828 pixserv_write(px
, "OK\n");
832 /* --- Report an error for other commands --- */
835 pixserv_write(px
, "FAIL unknown command `%s'\n", q
);
838 /* --- @pixserv_create@ --- *
840 * Arguments: @int fd@ = file descriptor to read from
841 * @int ofd@ = file descriptor to write to
843 * Returns: Pointer to the new connection.
845 * Use: Creates a new Pixie server instance for a new connection.
848 static pixserv
*pixserv_create(int fd
, int ofd
)
850 pixserv
*px
= CREATE(pixserv
);
852 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
854 fdflags(ofd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
856 selbuf_init(&px
->b
, &sel
, fd
, pixserv_line
, px
);
857 px
->b
.b
.a
= arena_secure
;
858 selbuf_setsize(&px
->b
, 1024);
859 gettimeofday(&tv
, 0);
860 tv
.tv_sec
+= PIXSERV_TIMEOUT
;
861 sel_addtimer(&sel
, &px
->timer
, &tv
, pixserv_expire
, px
);
866 /* --- @pixserv_accept@ --- *
868 * Arguments: @int fd@ = file descriptor
869 * @unsigned mode@ = what's happened
870 * @void *p@ = an uninteresting argument
874 * Use: Accepts a new connection.
877 static void pixserv_accept(int fd
, unsigned mode
, void *p
)
880 struct sockaddr_un sun
;
881 size_t sunsz
= sizeof(sun
);
883 if (mode
!= SEL_READ
)
885 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &sunsz
)) < 0) {
887 log("new connection failed: %s", strerror(errno
));
890 pixserv_create(nfd
, nfd
);
893 /*----- Setting up the server ---------------------------------------------*/
895 /* --- @unlinksocket@ --- *
901 * Use: Tidies up the socket when it's finished with.
904 static char *sockpath
;
906 static void unlinksocket(void)
912 /* --- @pix_sigdie@ --- *
914 * Arguments: @int sig@ = signal number
915 * @void *p@ = uninteresting argument
919 * Use: Shuts down the program after a fatal signal.
922 static void pix_sigdie(int sig
, void *p
)
929 case SIGTERM
: p
= "SIGTERM"; break;
930 case SIGINT
: p
= "SIGINT"; break;
932 sprintf(buf
, "signal %i", sig
);
936 log("shutting down on %s", p
);
941 /* --- @pix_sigflush@ --- *
943 * Arguments: @int sig@ = signal number
944 * @void *p@ = uninteresting argument
948 * Use: Flushes the passphrase cache on receipt of a signal.
951 static void pix_sigflush(int sig
, void *p
)
958 case SIGHUP
: p
= "SIGHUP"; break;
959 case SIGQUIT
: p
= "SIGQUIT"; break;
961 sprintf(buf
, "signal %i", sig
);
965 log("received %s; flushing passphrases", p
);
970 /* --- @pix_setup@ --- *
972 * Arguments: @struct sockaddr_un *sun@ = pointer to address to use
973 * @size_t sz@ = size of socket address
977 * Use: Sets up the pixie's Unix-domain socket.
980 static void pix_setup(struct sockaddr_un
*sun
, size_t sz
)
984 /* --- Set up the parent directory --- */
987 char *p
= sun
->sun_path
;
988 char *q
= strrchr(p
, '/');
998 if (stat(d
.buf
, &st
))
999 die(1, "couldn't stat `%s': %s", d
.buf
, strerror(errno
));
1000 if (!S_ISDIR(st
.st_mode
))
1001 die(1, "object `%s' isn't a directory", d
.buf
);
1002 if (st
.st_mode
& 0077)
1003 die(1, "parent directory `%s' has group or world access", d
.buf
);
1008 /* --- Initialize the socket --- */
1016 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
1017 die(1, "couldn't create socket: %s", strerror(errno
));
1018 if (bind(fd
, (struct sockaddr
*)sun
, sz
) < 0) {
1020 if (errno
!= EADDRINUSE
)
1021 die(1, "couldn't bind to address: %s", strerror(e
));
1023 die(1, "too many retries; giving up");
1025 if (connect(fd
, (struct sockaddr
*)sun
, sz
)) {
1027 if (errno
!= ECONNREFUSED
)
1028 die(1, "couldn't bind to address: %s", strerror(e
));
1029 if (stat(sun
->sun_path
, &st
))
1030 die(1, "couldn't stat `%s': %s", sun
->sun_path
, strerror(errno
));
1031 if (!S_ISSOCK(st
.st_mode
))
1032 die(1, "object `%s' isn't a socket", sun
->sun_path
);
1034 log("stale socket found; removing it");
1035 unlink(sun
->sun_path
);
1039 log("server already running; shutting it down");
1040 write(fd
, "QUIT\n", 5);
1046 chmod(sun
->sun_path
, 0600);
1047 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
1049 die(1, "couldn't listen on socket: %s", strerror(errno
));
1052 /* --- Set up the rest of the server --- */
1055 static sel_file serv
;
1056 sockpath
= sun
->sun_path
;
1057 atexit(unlinksocket
);
1058 sel_initfile(&sel
, &serv
, fd
, SEL_READ
, pixserv_accept
, 0);
1063 /*----- Client support code -----------------------------------------------*/
1065 /* --- Variables --- */
1067 static selbuf c_server
, c_client
;
1068 static unsigned c_flags
= 0;
1069 enum { cf_uclose
= 1, cf_sclose
= 2 };
1071 /* --- Line handler functions --- */
1073 static void c_uline(char *s
, void *p
)
1077 selbuf_destroy(&c_client
);
1078 shutdown(c_server
.reader
.fd
, 1);
1079 c_flags
|= cf_uclose
;
1083 write(c_server
.reader
.fd
, s
, sz
);
1087 static void c_sline(char *s
, void *p
)
1090 selbuf_destroy(&c_server
);
1091 if (!(c_flags
& cf_uclose
)) {
1092 moan("server closed the connection");
1093 selbuf_destroy(&c_client
);
1100 /* --- @pix_client@ --- *
1102 * Arguments: @struct sockaddr_un *sun@ = pointer to socket address
1103 * @size_t sz@ = size of socket address
1104 * @char *argv[]@ = pointer to arguments to send
1108 * Use: Performs client-side actions for the passphrase pixie.
1111 static void pix_client(struct sockaddr_un
*sun
, size_t sz
, char *argv
[])
1115 /* --- Dispose of locked memory --- */
1119 /* --- Open the socket --- */
1121 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
1122 die(1, "couldn't create socket: %s", strerror(errno
));
1123 if (connect(fd
, (struct sockaddr
*)sun
, sz
))
1124 die(1, "couldn't connect to server: %s", strerror(errno
));
1125 selbuf_init(&c_server
, &sel
, fd
, c_sline
, 0);
1127 /* --- If there are any arguments, turn them into a string --- */
1130 selbuf_init(&c_client
, &sel
, STDIN_FILENO
, c_uline
, 0);
1139 write(fd
, d
.buf
, d
.len
);
1141 c_flags
|= cf_uclose
;
1145 /* --- And repeat --- */
1151 /*----- Main code ---------------------------------------------------------*/
1153 /* --- @help@, @version@, @usage@ --- *
1155 * Arguments: @FILE *fp@ = stream to write on
1159 * Use: Emit helpful messages.
1162 static void usage(FILE *fp
)
1166 $ [-qvfidl] [-c command] [-t timeout] [-s socket]\n\
1167 $ [-s socket] -C [command args...]\n\
1171 static void version(FILE *fp
)
1173 pquis(fp
, "$, Catacomb version " VERSION
"\n");
1176 static void help(FILE *fp
)
1182 The Catacomb passphrase pixie collects and caches passphrases used to\n\
1183 protect important keys. Options provided:\n\
1185 -h, --help Show this help text.\n\
1186 -V, --version Show the program's version number.\n\
1187 -u, --usage Show a (very) terse usage summary.\n\
1189 -C, --client Connect to a running pixie as a client.\n\
1191 -q, --quiet Emit fewer log messages.\n\
1192 -v, --version Emit more log messages.\n\
1193 -s, --socket=FILE Name the pixie's socket.\n\
1194 -c, --command=COMMAND Shell command to read a passphrase.\n\
1195 -f, --fetch Fetch passphrases from the terminal.\n\
1196 -t, --timeout=TIMEOUT Length of time to retain a passphrase in memory.\n\
1197 -i, --interactive Allow commands to be typed interactively.\n\
1198 -d, --daemon Fork into the background after initialization.\n\
1199 -l, --syslog Emit log messages to the system log.\n\
1201 The COMMAND may contain `%m' and `%t' markers which are replaced by a\n\
1202 prompt message and the passphrase tag respectively. The TIMEOUT is an\n\
1203 integer, optionally followed by `d', `h', `m' or `s' to specify units of\n\
1204 days, hours, minutes or seconds respectively.\n\
1206 In client mode, if a command is specified on the command line, it is sent\n\
1207 to the running server; otherwise the program reads requests from stdin.\n\
1208 Responses from the pixie are written to stdout. Send a HELP request for\n\
1209 a quick summary of the pixie communication protocol.\n\
1215 * Arguments: @int argc@ = number of arguments
1216 * @char *argv[]@ = vector of argument values
1218 * Returns: Zero if OK.
1220 * Use: Main program. Listens on a socket and responds with a PGP
1221 * passphrase when asked.
1224 int main(int argc
, char *argv
[])
1227 struct sockaddr_un
*sun
;
1239 /* --- Initialize libraries --- */
1244 /* --- Set up the locked memory area --- */
1249 /* --- Parse command line arguments --- */
1252 static struct option opts
[] = {
1254 /* --- Standard GNUy help options --- */
1256 { "help", 0, 0, 'h' },
1257 { "version", 0, 0, 'V' },
1258 { "usage", 0, 0, 'u' },
1260 /* --- Other options --- */
1262 { "quiet", 0, 0, 'q' },
1263 { "verbose", 0, 0, 'v' },
1264 { "client", 0, 0, 'C' },
1265 { "socket", OPTF_ARGREQ
, 0, 's' },
1266 { "command", OPTF_ARGREQ
, 0, 'c' },
1267 { "fetch", 0, 0, 'f' },
1268 { "timeout", OPTF_ARGREQ
, 0, 't' },
1269 { "interactive", 0, 0, 'i' },
1270 { "stdin", 0, 0, 'i' },
1271 { "daemon", 0, 0, 'd' },
1272 { "log", 0, 0, 'l' },
1273 { "syslog", 0, 0, 'l' },
1275 /* --- Magic terminator --- */
1280 int i
= mdwopt(argc
, argv
, "hVuqvCs:c:ft:idl", opts
, 0, 0, 0);
1286 /* --- GNUy help options --- */
1298 /* --- Other interesting things --- */
1314 if ((timeout
= pixserv_timeout(optarg
)) == 0)
1315 die(1, "bad timeout `%s'", optarg
);
1334 /* --- Something else --- */
1342 if (f
& f_bogus
|| (optind
< argc
&& !(f
& f_client
))) {
1347 /* --- Set up the socket address --- */
1349 sun
= pixie_address(path
, &sz
);
1351 /* --- Initialize selectory --- */
1354 signal(SIGPIPE
, SIG_IGN
);
1356 /* --- Be a client if a client's wanted --- */
1359 pix_client(sun
, sz
, argv
+ optind
);
1361 /* --- Open the syslog if requested --- */
1365 openlog(QUIS
, 0, LOG_DAEMON
);
1368 /* --- Check on the locked memory area --- */
1372 int rc
= l_report(&lm
, &d
);
1374 die(EXIT_FAILURE
, d
.buf
);
1375 else if (rc
&& verbose
) {
1377 log("couldn't lock passphrase buffer");
1380 arena_setsecure(&lm
.a
);
1383 /* --- Set signal behaviours --- */
1386 static sig sigint
, sigterm
, sigquit
, sighup
;
1388 sig_add(&sigint
, SIGINT
, pix_sigdie
, 0);
1389 sig_add(&sigterm
, SIGTERM
, pix_sigdie
, 0);
1390 sig_add(&sigquit
, SIGQUIT
, pix_sigflush
, 0);
1391 sig_add(&sighup
, SIGHUP
, pix_sigflush
, 0);
1394 /* --- Set up the server --- */
1398 pixserv
*px
= pixserv_create(STDIN_FILENO
, STDOUT_FILENO
);
1399 sel_rmtimer(&px
->timer
);
1403 /* --- Fork into the background if requested --- */
1408 if (((f
& f_stdin
) &&
1409 (isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
))) ||
1410 (!command
&& (flags
& F_FETCH
)))
1411 die(1, "can't become a daemon if terminal required");
1413 if ((kid
= fork()) < 0)
1414 die(1, "fork failed: %s", strerror(errno
));
1420 if ((fd
= open("/dev/tty", O_RDONLY
)) >= 0) {
1421 ioctl(fd
, TIOCNOTTY
);
1434 log("initialized ok");
1440 /*----- That's all, folks -------------------------------------------------*/