069c185c |
1 | /* -*-c-*- |
2 | * |
12ed8a1f |
3 | * $Id$ |
069c185c |
4 | * |
5 | * Passphrase pixie for Catacomb |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
069c185c |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include "config.h" |
33 | |
34 | #include <assert.h> |
35 | #include <ctype.h> |
36 | #include <errno.h> |
37 | #include <signal.h> |
38 | #include <stdarg.h> |
39 | #include <stddef.h> |
40 | #include <stdio.h> |
41 | #include <stdlib.h> |
42 | #include <string.h> |
43 | #include <time.h> |
44 | |
45 | #include <sys/types.h> |
46 | #include <sys/time.h> |
47 | #include <unistd.h> |
48 | #include <sys/stat.h> |
49 | #include <sys/wait.h> |
50 | #include <pwd.h> |
51 | #include <fcntl.h> |
52 | #include <sys/ioctl.h> |
53 | #include <termios.h> |
54 | #include <syslog.h> |
55 | |
56 | #include <sys/socket.h> |
57 | #include <sys/un.h> |
58 | |
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> |
65 | #include <mLib/sel.h> |
66 | #include <mLib/selbuf.h> |
67 | #include <mLib/sig.h> |
68 | #include <mLib/str.h> |
69 | #include <mLib/sub.h> |
70 | #include <mLib/tv.h> |
71 | |
6a2d21e4 |
72 | #include "arena.h" |
069c185c |
73 | #include "lmem.h" |
74 | #include "passphrase.h" |
75 | #include "pixie.h" |
76 | |
77 | /*----- Static variables --------------------------------------------------*/ |
78 | |
4cb04adb |
79 | static unsigned long timeout = 900; |
069c185c |
80 | static sel_state sel; |
81 | static unsigned verbose = 1; |
82 | static const char *command = 0; |
83 | static lmem lm; |
84 | static unsigned flags = 0; |
85 | |
16efd15b |
86 | #define F_SYSLOG 1u |
87 | #define F_FETCH 2u |
069c185c |
88 | |
89 | /*----- Event logging -----------------------------------------------------*/ |
90 | |
91 | /* --- @log@ --- * |
92 | * |
93 | * Arguments: @const char *p@ = @printf@-style format string |
94 | * @...@ = extra arguments to fill in |
95 | * |
96 | * Returns: --- |
97 | * |
98 | * Use: Writes out a timestamped log message. |
99 | */ |
100 | |
101 | static void log(const char *p, ...) |
102 | { |
103 | dstr d = DSTR_INIT; |
104 | va_list ap; |
105 | |
106 | if (!(flags & F_SYSLOG)) { |
107 | time_t t = time(0); |
108 | struct tm *tm = localtime(&t); |
109 | DENSURE(&d, 64); |
110 | d.len += strftime(d.buf, d.sz, "%Y-%m-%d %H:%M:%S ", tm); |
111 | } |
112 | va_start(ap, p); |
5cf2e636 |
113 | dstr_vputf(&d, p, &ap); |
069c185c |
114 | va_end(ap); |
115 | |
116 | if (flags & F_SYSLOG) |
117 | syslog(LOG_NOTICE, "%s", d.buf); |
118 | else { |
119 | DPUTC(&d, '\n'); |
120 | dstr_write(&d, stderr); |
121 | } |
122 | DDESTROY(&d); |
123 | } |
124 | |
125 | /*----- Passphrase management ---------------------------------------------*/ |
126 | |
127 | /* --- Data structures --- */ |
128 | |
129 | typedef struct phrase { |
130 | struct phrase *next; |
131 | struct phrase *prev; |
132 | char *tag; |
133 | char *p; |
134 | unsigned long t; |
135 | sel_timer timer; |
136 | unsigned f; |
137 | } phrase; |
138 | |
139 | /* --- Variables --- */ |
140 | |
141 | #define P_ROOT ((phrase *)&p_root) |
142 | static struct { phrase *next; phrase *prev; } p_root = { P_ROOT, P_ROOT }; |
143 | |
144 | /* --- @p_free@ --- * |
145 | * |
146 | * Arguments: @phrase *p@ = pointer to phrase block |
147 | * |
148 | * Returns: --- |
149 | * |
150 | * Use: Frees a phrase block. |
151 | */ |
152 | |
153 | static void p_free(phrase *p) |
154 | { |
155 | if (p->t) |
156 | sel_rmtimer(&p->timer); |
12ed8a1f |
157 | xfree(p->tag); |
069c185c |
158 | l_free(&lm, p->p); |
159 | p->next->prev = p->prev; |
160 | p->prev->next = p->next; |
161 | DESTROY(p); |
162 | } |
163 | |
164 | /* --- @p_timer@ --- * |
165 | * |
166 | * Arguments: @struct timeval *tv@ = current time |
167 | * @void *p@ = pointer to phrase |
168 | * |
169 | * Returns: --- |
170 | * |
171 | * Use: Expires a passphrase. |
172 | */ |
173 | |
174 | static void p_timer(struct timeval *tv, void *p) |
175 | { |
176 | phrase *pp = p; |
177 | if (verbose) |
178 | log("expiring passphrase `%s'", pp->tag); |
179 | p_free(pp); |
180 | } |
181 | |
182 | /* --- @p_alloc@ --- * |
183 | * |
184 | * Arguments: @size_t sz@ = amount of memory required |
185 | * |
186 | * Returns: Pointer to allocated memory, or null. |
187 | * |
188 | * Use: Allocates some locked memory, flushing old passphrases if |
189 | * there's not enough space. |
190 | */ |
191 | |
192 | static void *p_alloc(size_t sz) |
193 | { |
194 | for (;;) { |
195 | char *p; |
196 | if ((p = l_alloc(&lm, sz)) != 0) |
197 | return (p); |
198 | if (P_ROOT->next == P_ROOT) |
199 | return (0); |
200 | if (verbose) { |
201 | log("flushing passphrase `%s' to free up needed space", |
202 | P_ROOT->next->tag); |
203 | } |
204 | p_free(P_ROOT->next); |
205 | } |
206 | } |
207 | |
208 | /* --- @p_find@ --- * |
209 | * |
210 | * Arguments: @const char *tag@ = pointer to tag to find |
211 | * |
212 | * Returns: Pointer to passphrase block, or null. |
213 | * |
214 | * Use: Finds a passphrase with a given tag. |
215 | */ |
216 | |
217 | static phrase *p_find(const char *tag) |
218 | { |
219 | phrase *p; |
220 | |
221 | for (p = P_ROOT->next; p != P_ROOT; p = p->next) { |
222 | if (strcmp(p->tag, tag) == 0) { |
223 | if (p->t) { |
224 | struct timeval tv; |
225 | sel_rmtimer(&p->timer); |
226 | gettimeofday(&tv, 0); |
227 | tv.tv_sec += p->t; |
228 | sel_addtimer(&sel, &p->timer, &tv, p_timer, p); |
229 | } |
230 | p->next->prev = p->prev; |
231 | p->prev->next = p->next; |
232 | p->next = P_ROOT; |
233 | p->prev = P_ROOT->prev; |
234 | P_ROOT->prev->next = p; |
235 | P_ROOT->prev = p; |
236 | return (p); |
237 | } |
238 | } |
239 | return (0); |
240 | } |
241 | |
242 | /* --- @p_add@ --- * |
243 | * |
244 | * Arguments: @const char *tag@ = pointer to tag string |
245 | * @const char *p@ = pointer to passphrase |
246 | * @unsigned long t@ = expiry timeout |
247 | * |
248 | * Returns: Pointer to newly-added passphrase. |
249 | * |
250 | * Use: Adds a new passphrase. The tag must not already exist. |
251 | */ |
252 | |
253 | static phrase *p_add(const char *tag, const char *p, unsigned long t) |
254 | { |
255 | size_t sz = strlen(p) + 1; |
256 | char *l = p_alloc(sz); |
257 | phrase *pp; |
258 | |
259 | /* --- Make sure the locked memory was allocated --- */ |
260 | |
261 | if (!l) |
262 | return (0); |
263 | |
264 | /* --- Fill in some other bits of the block --- */ |
265 | |
266 | pp = CREATE(phrase); |
267 | memcpy(l, p, sz); |
268 | pp->p = l; |
269 | pp->tag = xstrdup(tag); |
270 | pp->f = 0; |
271 | |
272 | /* --- Set the timer --- */ |
273 | |
274 | pp->t = t; |
275 | if (t) { |
276 | struct timeval tv; |
277 | gettimeofday(&tv, 0); |
278 | tv.tv_sec += t; |
279 | sel_addtimer(&sel, &pp->timer, &tv, p_timer, pp); |
280 | } |
281 | |
282 | /* --- Link the block into the chain --- */ |
283 | |
284 | pp->next = P_ROOT; |
285 | pp->prev = P_ROOT->prev; |
286 | P_ROOT->prev->next = pp; |
287 | P_ROOT->prev = pp; |
288 | return (pp); |
289 | } |
290 | |
291 | /* --- @p_flush@ --- * |
292 | * |
293 | * Arguments: @const char *tag@ = pointer to tag string, or zero for all |
294 | * |
295 | * Returns: --- |
296 | * |
297 | * Use: Immediately flushes either a single phrase or all of them. |
298 | */ |
299 | |
300 | static void p_flush(const char *tag) |
301 | { |
302 | phrase *p; |
303 | |
304 | if (!tag && verbose > 1) |
305 | log("flushing all passphrases"); |
52b8f75d |
306 | p = P_ROOT->next; |
307 | while (p != P_ROOT) { |
308 | phrase *pp = p->next; |
069c185c |
309 | if (!tag) |
310 | p_free(p); |
311 | else if (strcmp(p->tag, tag) == 0) { |
312 | if (verbose > 1) |
313 | log("flushing passphrase `%s'", tag); |
314 | p_free(p); |
315 | break; |
316 | } |
52b8f75d |
317 | p = pp; |
069c185c |
318 | } |
319 | } |
320 | |
321 | /*----- Reading passphrases -----------------------------------------------*/ |
322 | |
323 | /* --- @p_request@ --- * |
324 | * |
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 |
329 | * |
330 | * Returns: Zero if all went well, nonzero otherwise. |
331 | * |
332 | * Use: Requests a passphrase from the user. |
333 | */ |
334 | |
335 | static int p_request(const char *msg, const char *tag, char *buf, size_t sz) |
336 | { |
337 | /* --- If there's a passphrase-fetching command, run it --- */ |
338 | |
339 | if (command) { |
340 | dstr d = DSTR_INIT; |
341 | const char *p; |
342 | int fd[2]; |
343 | pid_t kid; |
344 | int r; |
4cb04adb |
345 | int rc; |
069c185c |
346 | |
347 | /* --- Substitute the prompt string into the command --- */ |
348 | |
349 | p = command; |
350 | for (;;) { |
351 | const char *q = strchr(p, '%'); |
352 | if (!q || !q[1]) { |
353 | DPUTS(&d, p); |
354 | break; |
355 | } |
356 | DPUTM(&d, p, q - p); |
357 | p = q + 1; |
358 | switch (*p) { |
359 | case 'm': |
360 | DPUTS(&d, msg); |
361 | break; |
362 | case 't': |
363 | DPUTS(&d, tag); |
364 | break; |
365 | default: |
366 | DPUTC(&d, '%'); |
367 | DPUTC(&d, *p); |
368 | break; |
369 | } |
370 | p++; |
371 | } |
372 | DPUTZ(&d); |
373 | |
374 | /* --- Create a pipe and start a child process --- */ |
375 | |
376 | if (pipe(fd)) |
377 | goto fail_1; |
378 | if ((kid = fork()) < 0) |
379 | goto fail_2; |
380 | |
381 | /* --- Child process --- */ |
382 | |
383 | fflush(0); |
384 | if (kid == 0) { |
385 | if (dup2(fd[1], STDOUT_FILENO) < 0) |
386 | _exit(127); |
387 | close(fd[0]); |
113e872f |
388 | execl("/bin/sh", "sh", "-c", d.buf, (char *)0); |
069c185c |
389 | _exit(127); |
390 | } |
391 | |
392 | /* --- Read the data back into my buffer --- */ |
393 | |
394 | close(fd[1]); |
395 | if ((r = read(fd[0], buf, sz - 1)) >= 0) { |
396 | char *q = memchr(buf, '\n', r); |
397 | if (!q) |
398 | q = buf + r; |
399 | *q = 0; |
400 | } |
401 | close(fd[0]); |
4cb04adb |
402 | waitpid(kid, &rc, 0); |
069c185c |
403 | dstr_destroy(&d); |
4cb04adb |
404 | if (r < 0 || rc != 0) |
069c185c |
405 | goto fail_0; |
6a2d21e4 |
406 | goto ok; |
069c185c |
407 | |
408 | /* --- Tidy up when things go wrong --- */ |
409 | |
410 | fail_2: |
411 | close(fd[0]); |
412 | close(fd[1]); |
413 | fail_1: |
414 | dstr_destroy(&d); |
415 | fail_0: |
416 | return (-1); |
417 | } |
418 | |
419 | /* --- Read a passphrase from the terminal --- * |
420 | * |
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. |
424 | */ |
425 | |
426 | { |
427 | dstr d = DSTR_INIT; |
428 | int rc; |
429 | dstr_putf(&d, "%s %s: ", msg, tag); |
430 | rc = pixie_getpass(d.buf, buf, sz); |
431 | dstr_destroy(&d); |
6a2d21e4 |
432 | if (rc) |
433 | return (rc); |
434 | goto ok; |
069c185c |
435 | } |
6a2d21e4 |
436 | |
437 | /* --- Sort out the buffer --- * |
438 | * |
439 | * Strip leading spaces. |
440 | */ |
441 | |
442 | ok: { |
443 | char *p = buf; |
444 | size_t len; |
445 | while (isspace((unsigned char)*p)) |
446 | p++; |
447 | len = strlen(p); |
448 | memmove(buf, p, len); |
449 | p[len] = 0; |
450 | } |
451 | |
452 | /* --- Done --- */ |
453 | |
454 | return (0); |
069c185c |
455 | } |
456 | |
457 | /* --- @p_get@ --- * |
458 | * |
6a2d21e4 |
459 | * Arguments: @const char **q@ = where to store the result |
460 | * @const char *tag@ = pointer to tag string |
069c185c |
461 | * @unsigned mode@ = reading mode (verify?) |
462 | * @time_t exp@ = expiry time suggestion |
463 | * |
6a2d21e4 |
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 |
467 | * @PMODE_VERIFY@. |
069c185c |
468 | * |
469 | * Use: Reads a passphrase from somewhere. |
470 | */ |
471 | |
6a2d21e4 |
472 | static int p_get(const char **q, const char *tag, unsigned mode, time_t exp) |
069c185c |
473 | { |
474 | #define LBUFSZ 1024 |
475 | |
476 | phrase *p; |
477 | char *pp = 0; |
478 | |
479 | /* --- Write a log message --- */ |
480 | |
481 | if (verbose > 1) |
482 | log("passphrase `%s' requested", tag); |
483 | |
6a2d21e4 |
484 | /* --- If there is no fetcher, life is simpler --- */ |
485 | |
486 | if (!(flags & F_FETCH)) { |
487 | if (mode == PMODE_VERIFY) |
488 | return (+1); |
489 | if ((p = p_find(tag)) == 0) |
490 | return (+1); |
491 | *q = p->p; |
492 | return (0); |
493 | } |
494 | |
069c185c |
495 | /* --- Try to find the phrase --- */ |
496 | |
6a2d21e4 |
497 | if (mode == PMODE_VERIFY) |
498 | p_flush(tag); |
499 | if (mode == PMODE_VERIFY || (p = p_find(tag)) == 0) { |
069c185c |
500 | if ((pp = p_alloc(LBUFSZ)) == 0) |
501 | goto fail; |
6a2d21e4 |
502 | if (p_request(mode == PMODE_READ ? "Passphrase" : "New passphrase", |
503 | tag, pp, LBUFSZ) < 0) |
069c185c |
504 | goto fail; |
505 | p = p_add(tag, pp, exp); |
506 | if (!p) |
507 | goto fail; |
508 | } |
509 | |
510 | /* --- If verification is requested, verify the passphrase --- */ |
511 | |
512 | if (mode == PMODE_VERIFY) { |
513 | if (!pp && (pp = p_alloc(LBUFSZ)) == 0) |
514 | goto fail; |
515 | if (p_request("Verify passphrase", tag, pp, LBUFSZ) < 0) |
516 | goto fail; |
517 | if (strcmp(pp, p->p) != 0) { |
518 | if (verbose) |
519 | log("passphrases for `%s' don't match", tag); |
520 | p_free(p); |
521 | goto fail; |
522 | } |
523 | } |
524 | |
525 | /* --- Tidy up and return the passphrase --- */ |
526 | |
527 | if (pp) { |
528 | memset(pp, 0, LBUFSZ); |
529 | l_free(&lm, pp); |
530 | } |
6a2d21e4 |
531 | *q = p->p; |
532 | return (0); |
069c185c |
533 | |
534 | /* --- Tidy up if things went wrong --- */ |
535 | |
536 | fail: |
537 | if (pp) { |
538 | memset(pp, 0, LBUFSZ); |
539 | l_free(&lm, pp); |
540 | } |
4cb04adb |
541 | return (-1); |
069c185c |
542 | |
543 | #undef LBUFSZ |
544 | } |
545 | |
546 | /*----- Server command parsing --------------------------------------------*/ |
547 | |
548 | /* --- Data structures --- */ |
549 | |
550 | typedef struct pixserv { |
551 | selbuf b; |
552 | int fd; |
553 | sel_timer timer; |
6a2d21e4 |
554 | unsigned f; |
069c185c |
555 | } pixserv; |
556 | |
9d23d982 |
557 | #define px_stdin 1u |
6a2d21e4 |
558 | |
559 | #define PIXSERV_TIMEOUT 30 |
069c185c |
560 | |
561 | /* --- @pixserv_expire@ --- * |
562 | * |
563 | * Arguments: @struct timeval *tv@ = pointer to current time |
564 | * @void *p@ = pointer to server block |
565 | * |
566 | * Returns: --- |
567 | * |
568 | * Use: Expires a pixie connection if the remote end decides he's not |
569 | * interested any more. |
570 | */ |
571 | |
572 | static void pixserv_expire(struct timeval *tv, void *p) |
573 | { |
574 | pixserv *px = p; |
575 | if (px->fd != px->b.reader.fd) |
576 | close(px->fd); |
6a2d21e4 |
577 | selbuf_destroy(&px->b); |
069c185c |
578 | close(px->b.reader.fd); |
579 | DESTROY(px); |
580 | } |
581 | |
582 | /* --- @pixserv_write@ --- * |
583 | * |
584 | * Arguments: @pixserv *px@ = pointer to server block |
585 | * @const char *p@ = pointer to skeleton string |
586 | * @...@ = other arguments to fill in |
587 | * |
588 | * Returns: --- |
589 | * |
590 | * Use: Formats a string and emits it to the output file. |
591 | */ |
592 | |
593 | static void pixserv_write(pixserv *px, const char *p, ...) |
594 | { |
595 | dstr d = DSTR_INIT; |
596 | va_list ap; |
597 | |
598 | va_start(ap, p); |
5cf2e636 |
599 | dstr_vputf(&d, p, &ap); |
069c185c |
600 | write(px->fd, d.buf, d.len); |
601 | va_end(ap); |
602 | dstr_destroy(&d); |
603 | } |
604 | |
605 | /* --- @pixserv_timeout@ --- * |
606 | * |
607 | * Arguments: @const char *p@ = pointer to timeout string |
608 | * |
609 | * Returns: Timeout in seconds. |
610 | * |
611 | * Use: Translates a string to a timeout value in seconds. |
612 | */ |
613 | |
614 | static unsigned long pixserv_timeout(const char *p) |
615 | { |
616 | unsigned long t; |
617 | char *q; |
618 | |
619 | if (!p) |
620 | return (timeout); |
621 | |
622 | t = strtoul(p, &q, 0); |
623 | switch (*q) { |
624 | case 'd': t *= 24; |
625 | case 'h': t *= 60; |
626 | case 'm': t *= 60; |
627 | case 's': if (q[1] != 0) |
628 | default: t = 0; |
629 | case 0: break; |
630 | } |
631 | return (t); |
632 | } |
633 | |
634 | /* --- @pixserv_line@ --- * |
635 | * |
636 | * Arguments: @char *s@ = pointer to the line read |
5cf2e636 |
637 | * @size_t len@ = length of the line |
069c185c |
638 | * @void *p@ = pointer to server block |
639 | * |
640 | * Returns: --- |
641 | * |
642 | * Use: Handles a line read from the client. |
643 | */ |
644 | |
5cf2e636 |
645 | static void pixserv_line(char *s, size_t len, void *p) |
069c185c |
646 | { |
647 | pixserv *px = p; |
648 | char *q, *qq; |
649 | unsigned mode; |
650 | |
651 | /* --- Handle an end-of-file --- */ |
652 | |
6a2d21e4 |
653 | if (!(px->f & px_stdin)) |
654 | sel_rmtimer(&px->timer); |
069c185c |
655 | if (!s) { |
656 | if (px->fd != px->b.reader.fd) |
657 | close(px->fd); |
6a2d21e4 |
658 | selbuf_destroy(&px->b); |
069c185c |
659 | close(px->b.reader.fd); |
660 | return; |
661 | } |
662 | |
663 | /* --- Fiddle the timeout --- */ |
664 | |
6a2d21e4 |
665 | if (!(px->f & px_stdin)) { |
069c185c |
666 | struct timeval tv; |
667 | gettimeofday(&tv, 0); |
668 | tv.tv_sec += PIXSERV_TIMEOUT; |
669 | sel_addtimer(&sel, &px->timer, &tv, pixserv_expire, px); |
670 | } |
671 | |
672 | /* --- Scan out the first word --- */ |
673 | |
674 | if ((q = str_getword(&s)) == 0) |
675 | return; |
676 | for (qq = q; *qq; qq++) |
677 | *qq = tolower((unsigned char)*qq); |
678 | |
679 | /* --- Handle a help request --- */ |
680 | |
681 | if (strcmp(q, "help") == 0) { |
682 | pixserv_write(px, "\ |
683 | INFO Commands supported:\n\ |
684 | INFO HELP\n\ |
685 | INFO LIST\n\ |
686 | INFO PASS tag [expire]\n\ |
687 | INFO VERIFY tag [expire]\n\ |
688 | INFO FLUSH [tag]\n\ |
6a2d21e4 |
689 | INFO SET tag [expire] -- phrase\n\ |
069c185c |
690 | INFO QUIT\n\ |
691 | OK\n\ |
692 | "); |
693 | } |
694 | |
695 | /* --- List the passphrases --- */ |
696 | |
697 | else if (strcmp(q, "list") == 0) { |
698 | phrase *p; |
699 | |
700 | for (p = P_ROOT->next; p != P_ROOT; p = p->next) { |
701 | if (!p->t) |
702 | pixserv_write(px, "ITEM %s no-expire\n", p->tag); |
703 | else { |
704 | struct timeval tv; |
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); |
708 | } |
709 | } |
710 | pixserv_write(px, "OK\n"); |
711 | } |
712 | |
713 | /* --- Request a passphrase --- */ |
714 | |
715 | else if ((mode = PMODE_READ, strcmp(q, "pass") == 0) || |
716 | (mode = PMODE_VERIFY, strcmp(q, "verify") == 0)) { |
717 | unsigned long t; |
718 | const char *p; |
6a2d21e4 |
719 | int rc; |
069c185c |
720 | |
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"); |
6a2d21e4 |
725 | else { |
726 | rc = p_get(&p, q, mode, t > timeout ? timeout : t); |
727 | switch (rc) { |
728 | case 0: |
729 | pixserv_write(px, "OK %s\n", p); |
730 | break; |
731 | case -1: |
732 | pixserv_write(px, "FAIL error reading passphrase\n"); |
733 | break; |
734 | case +1: |
735 | pixserv_write(px, "MISSING\n"); |
736 | break; |
737 | } |
738 | } |
069c185c |
739 | } |
740 | |
741 | /* --- Flush existing passphrases --- */ |
742 | |
743 | else if (strcmp(q, "flush") == 0) { |
744 | q = str_getword(&s); |
745 | p_flush(q); |
746 | pixserv_write(px, "OK\n"); |
747 | } |
748 | |
6a2d21e4 |
749 | /* --- Set a passphrase --- */ |
750 | |
751 | else if (strcmp(q, "set") == 0) { |
752 | char *tag; |
753 | unsigned long t; |
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"); |
758 | else { |
759 | if (strcmp(q, "--") != 0) { |
760 | t = pixserv_timeout(q); |
761 | q = str_getword(&s); |
762 | } else |
763 | t = pixserv_timeout(0); |
764 | if (!q) |
765 | pixserv_write(px, "FAIL no passphrase\n"); |
766 | else if (strcmp(q, "--") != 0) |
767 | pixserv_write(px, "FAIL rubbish found before passphrase\n"); |
768 | else { |
769 | p_flush(tag); |
770 | p_add(tag, s, t); |
771 | pixserv_write(px, "OK\n"); |
772 | } |
773 | } |
774 | } |
775 | |
069c185c |
776 | /* --- Shut the server down --- */ |
777 | |
778 | else if (strcmp(q, "quit") == 0) { |
779 | if (verbose) |
6a2d21e4 |
780 | log("%s client requested shutdown", |
781 | px->f & px_stdin ? "local" : "remote"); |
069c185c |
782 | pixserv_write(px, "OK\n"); |
783 | exit(0); |
784 | } |
785 | |
786 | /* --- Report an error for other commands --- */ |
787 | |
788 | else |
789 | pixserv_write(px, "FAIL unknown command `%s'\n", q); |
790 | } |
791 | |
792 | /* --- @pixserv_create@ --- * |
793 | * |
794 | * Arguments: @int fd@ = file descriptor to read from |
795 | * @int ofd@ = file descriptor to write to |
796 | * |
6a2d21e4 |
797 | * Returns: Pointer to the new connection. |
069c185c |
798 | * |
799 | * Use: Creates a new Pixie server instance for a new connection. |
800 | */ |
801 | |
6a2d21e4 |
802 | static pixserv *pixserv_create(int fd, int ofd) |
069c185c |
803 | { |
804 | pixserv *px = CREATE(pixserv); |
805 | struct timeval tv; |
806 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
807 | if (ofd != fd) |
808 | fdflags(ofd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
809 | px->fd = ofd; |
810 | selbuf_init(&px->b, &sel, fd, pixserv_line, px); |
6a2d21e4 |
811 | px->b.b.a = arena_secure; |
812 | selbuf_setsize(&px->b, 1024); |
069c185c |
813 | gettimeofday(&tv, 0); |
814 | tv.tv_sec += PIXSERV_TIMEOUT; |
815 | sel_addtimer(&sel, &px->timer, &tv, pixserv_expire, px); |
6a2d21e4 |
816 | px->f = 0; |
817 | return (px); |
069c185c |
818 | } |
819 | |
820 | /* --- @pixserv_accept@ --- * |
821 | * |
822 | * Arguments: @int fd@ = file descriptor |
823 | * @unsigned mode@ = what's happened |
824 | * @void *p@ = an uninteresting argument |
825 | * |
826 | * Returns: --- |
827 | * |
828 | * Use: Accepts a new connection. |
829 | */ |
830 | |
831 | static void pixserv_accept(int fd, unsigned mode, void *p) |
832 | { |
833 | int nfd; |
834 | struct sockaddr_un sun; |
4933a180 |
835 | size_t sunsz = sizeof(sun); |
069c185c |
836 | |
837 | if (mode != SEL_READ) |
838 | return; |
839 | if ((nfd = accept(fd, (struct sockaddr *)&sun, &sunsz)) < 0) { |
962dd332 |
840 | if (verbose && errno != EAGAIN && errno != EWOULDBLOCK && |
841 | errno != ECONNABORTED && errno != EPROTO && errno != EINTR) |
069c185c |
842 | log("new connection failed: %s", strerror(errno)); |
843 | return; |
844 | } |
845 | pixserv_create(nfd, nfd); |
846 | } |
847 | |
848 | /*----- Setting up the server ---------------------------------------------*/ |
849 | |
850 | /* --- @unlinksocket@ --- * |
851 | * |
852 | * Arguments: --- |
853 | * |
854 | * Returns: --- |
855 | * |
856 | * Use: Tidies up the socket when it's finished with. |
857 | */ |
858 | |
859 | static char *sockpath; |
860 | |
861 | static void unlinksocket(void) |
862 | { |
863 | unlink(sockpath); |
864 | l_purge(&lm); |
865 | } |
866 | |
867 | /* --- @pix_sigdie@ --- * |
868 | * |
869 | * Arguments: @int sig@ = signal number |
870 | * @void *p@ = uninteresting argument |
871 | * |
872 | * Returns: --- |
873 | * |
874 | * Use: Shuts down the program after a fatal signal. |
875 | */ |
876 | |
877 | static void pix_sigdie(int sig, void *p) |
878 | { |
879 | if (verbose) { |
880 | char *p; |
881 | char buf[20]; |
882 | |
883 | switch (sig) { |
884 | case SIGTERM: p = "SIGTERM"; break; |
885 | case SIGINT: p = "SIGINT"; break; |
886 | default: |
887 | sprintf(buf, "signal %i", sig); |
888 | p = buf; |
889 | break; |
890 | } |
891 | log("shutting down on %s", p); |
892 | } |
893 | exit(0); |
894 | } |
895 | |
896 | /* --- @pix_sigflush@ --- * |
897 | * |
898 | * Arguments: @int sig@ = signal number |
899 | * @void *p@ = uninteresting argument |
900 | * |
901 | * Returns: --- |
902 | * |
903 | * Use: Flushes the passphrase cache on receipt of a signal. |
904 | */ |
905 | |
906 | static void pix_sigflush(int sig, void *p) |
907 | { |
908 | if (verbose) { |
909 | char *p; |
910 | char buf[20]; |
911 | |
912 | switch (sig) { |
913 | case SIGHUP: p = "SIGHUP"; break; |
914 | case SIGQUIT: p = "SIGQUIT"; break; |
915 | default: |
916 | sprintf(buf, "signal %i", sig); |
917 | p = buf; |
918 | break; |
919 | } |
920 | log("received %s; flushing passphrases", p); |
921 | } |
922 | p_flush(0); |
923 | } |
924 | |
925 | /* --- @pix_setup@ --- * |
926 | * |
927 | * Arguments: @struct sockaddr_un *sun@ = pointer to address to use |
928 | * @size_t sz@ = size of socket address |
929 | * |
930 | * Returns: --- |
931 | * |
932 | * Use: Sets up the pixie's Unix-domain socket. |
933 | */ |
934 | |
935 | static void pix_setup(struct sockaddr_un *sun, size_t sz) |
936 | { |
937 | int fd; |
938 | |
939 | /* --- Set up the parent directory --- */ |
940 | |
941 | { |
069c185c |
942 | char *p = sun->sun_path; |
943 | char *q = strrchr(p, '/'); |
944 | |
945 | if (q) { |
4cb04adb |
946 | dstr d = DSTR_INIT; |
069c185c |
947 | struct stat st; |
948 | |
949 | DPUTM(&d, p, q - p); |
950 | DPUTZ(&d); |
951 | |
952 | mkdir(d.buf, 0700); |
953 | if (stat(d.buf, &st)) |
954 | die(1, "couldn't stat `%s': %s", d.buf, strerror(errno)); |
4cb04adb |
955 | if (!S_ISDIR(st.st_mode)) |
956 | die(1, "object `%s' isn't a directory", d.buf); |
069c185c |
957 | if (st.st_mode & 0077) |
958 | die(1, "parent directory `%s' has group or world access", d.buf); |
4cb04adb |
959 | dstr_destroy(&d); |
069c185c |
960 | } |
961 | } |
962 | |
963 | /* --- Initialize the socket --- */ |
964 | |
965 | { |
966 | int n = 5; |
967 | int e; |
968 | |
969 | umask(0077); |
970 | again: |
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) { |
974 | e = errno; |
975 | if (errno != EADDRINUSE) |
976 | die(1, "couldn't bind to address: %s", strerror(e)); |
977 | if (!n) |
978 | die(1, "too many retries; giving up"); |
979 | n--; |
980 | if (connect(fd, (struct sockaddr *)sun, sz)) { |
4cb04adb |
981 | struct stat st; |
069c185c |
982 | if (errno != ECONNREFUSED) |
983 | die(1, "couldn't bind to address: %s", strerror(e)); |
4cb04adb |
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); |
069c185c |
988 | if (verbose) |
989 | log("stale socket found; removing it"); |
990 | unlink(sun->sun_path); |
991 | close(fd); |
992 | } else { |
993 | if (verbose) |
994 | log("server already running; shutting it down"); |
995 | write(fd, "QUIT\n", 5); |
996 | sleep(1); |
997 | close(fd); |
998 | } |
999 | goto again; |
1000 | } |
1001 | chmod(sun->sun_path, 0600); |
1002 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
1003 | if (listen(fd, 5)) |
1004 | die(1, "couldn't listen on socket: %s", strerror(errno)); |
1005 | } |
1006 | |
1007 | /* --- Set up the rest of the server --- */ |
1008 | |
1009 | { |
1010 | static sel_file serv; |
1011 | sockpath = sun->sun_path; |
1012 | atexit(unlinksocket); |
1013 | sel_initfile(&sel, &serv, fd, SEL_READ, pixserv_accept, 0); |
1014 | sel_addfile(&serv); |
1015 | } |
1016 | } |
1017 | |
1018 | /*----- Client support code -----------------------------------------------*/ |
1019 | |
1020 | /* --- Variables --- */ |
1021 | |
6a2d21e4 |
1022 | static selbuf c_server, c_client; |
4933a180 |
1023 | static unsigned c_flags = 0; |
9d23d982 |
1024 | |
1025 | #define cf_uclose 1u |
1026 | #define cf_sclose 2u |
069c185c |
1027 | |
1028 | /* --- Line handler functions --- */ |
1029 | |
5cf2e636 |
1030 | static void c_uline(char *s, size_t len, void *p) |
069c185c |
1031 | { |
6a2d21e4 |
1032 | if (!s) { |
1033 | selbuf_destroy(&c_client); |
1034 | shutdown(c_server.reader.fd, 1); |
1035 | c_flags |= cf_uclose; |
1036 | } else { |
5cf2e636 |
1037 | s[len++] = '\n'; |
1038 | write(c_server.reader.fd, s, len); |
6a2d21e4 |
1039 | } |
069c185c |
1040 | } |
1041 | |
5cf2e636 |
1042 | static void c_sline(char *s, size_t len, void *p) |
069c185c |
1043 | { |
1044 | if (!s) { |
6a2d21e4 |
1045 | selbuf_destroy(&c_server); |
1046 | if (!(c_flags & cf_uclose)) { |
069c185c |
1047 | moan("server closed the connection"); |
6a2d21e4 |
1048 | selbuf_destroy(&c_client); |
1049 | } |
069c185c |
1050 | exit(0); |
81b5dd60 |
1051 | } |
1052 | puts(s); |
069c185c |
1053 | } |
1054 | |
1055 | /* --- @pix_client@ --- * |
1056 | * |
1057 | * Arguments: @struct sockaddr_un *sun@ = pointer to socket address |
1058 | * @size_t sz@ = size of socket address |
1059 | * @char *argv[]@ = pointer to arguments to send |
1060 | * |
1061 | * Returns: --- |
1062 | * |
1063 | * Use: Performs client-side actions for the passphrase pixie. |
1064 | */ |
1065 | |
1066 | static void pix_client(struct sockaddr_un *sun, size_t sz, char *argv[]) |
1067 | { |
1068 | int fd; |
1069 | |
4cb04adb |
1070 | /* --- Dispose of locked memory --- */ |
1071 | |
1072 | l_destroy(&lm); |
1073 | |
069c185c |
1074 | /* --- Open the socket --- */ |
1075 | |
1076 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
1077 | die(1, "couldn't create socket: %s", strerror(errno)); |
1078 | if (connect(fd, (struct sockaddr *)sun, sz)) |
1079 | die(1, "couldn't connect to server: %s", strerror(errno)); |
1080 | selbuf_init(&c_server, &sel, fd, c_sline, 0); |
1081 | |
1082 | /* --- If there are any arguments, turn them into a string --- */ |
1083 | |
1084 | if (!*argv) |
1085 | selbuf_init(&c_client, &sel, STDIN_FILENO, c_uline, 0); |
1086 | else { |
1087 | dstr d = DSTR_INIT; |
1088 | DPUTS(&d, *argv++); |
1089 | while (*argv) { |
1090 | DPUTC(&d, ' '); |
1091 | DPUTS(&d, *argv++); |
1092 | } |
1093 | DPUTC(&d, '\n'); |
1094 | write(fd, d.buf, d.len); |
1095 | shutdown(fd, 1); |
4cb04adb |
1096 | c_flags |= cf_uclose; |
069c185c |
1097 | dstr_destroy(&d); |
1098 | } |
1099 | |
1100 | /* --- And repeat --- */ |
1101 | |
81b5dd60 |
1102 | for (;;) { |
1103 | if (sel_select(&sel)) |
1104 | die(EXIT_FAILURE, "select error: %s", strerror(errno)); |
1105 | } |
069c185c |
1106 | } |
1107 | |
1108 | /*----- Main code ---------------------------------------------------------*/ |
1109 | |
1110 | /* --- @help@, @version@, @usage@ --- * |
1111 | * |
1112 | * Arguments: @FILE *fp@ = stream to write on |
1113 | * |
1114 | * Returns: --- |
1115 | * |
1116 | * Use: Emit helpful messages. |
1117 | */ |
1118 | |
1119 | static void usage(FILE *fp) |
1120 | { |
1121 | pquis(fp, "\ |
1122 | Usage:\n\ |
4cb04adb |
1123 | $ [-qvfidl] [-c command] [-t timeout] [-s socket]\n\ |
069c185c |
1124 | $ [-s socket] -C [command args...]\n\ |
1125 | "); |
1126 | } |
1127 | |
1128 | static void version(FILE *fp) |
1129 | { |
1130 | pquis(fp, "$, Catacomb version " VERSION "\n"); |
1131 | } |
1132 | |
1133 | static void help(FILE *fp) |
1134 | { |
1135 | version(fp); |
1136 | fputc('\n', fp); |
1137 | usage(fp); |
1138 | pquis(fp, "\n\ |
1139 | The Catacomb passphrase pixie collects and caches passphrases used to\n\ |
1140 | protect important keys. Options provided:\n\ |
1141 | \n\ |
1142 | -h, --help Show this help text.\n\ |
1143 | -V, --version Show the program's version number.\n\ |
1144 | -u, --usage Show a (very) terse usage summary.\n\ |
1145 | \n\ |
1146 | -C, --client Connect to a running pixie as a client.\n\ |
1147 | \n\ |
1148 | -q, --quiet Emit fewer log messages.\n\ |
1149 | -v, --version Emit more log messages.\n\ |
1150 | -s, --socket=FILE Name the pixie's socket.\n\ |
1151 | -c, --command=COMMAND Shell command to read a passphrase.\n\ |
6a2d21e4 |
1152 | -f, --fetch Fetch passphrases from the terminal.\n\ |
069c185c |
1153 | -t, --timeout=TIMEOUT Length of time to retain a passphrase in memory.\n\ |
1154 | -i, --interactive Allow commands to be typed interactively.\n\ |
6a2d21e4 |
1155 | -d, --daemon Fork into the background after initialization.\n\ |
069c185c |
1156 | -l, --syslog Emit log messages to the system log.\n\ |
1157 | \n\ |
1158 | The COMMAND may contain `%m' and `%t' markers which are replaced by a\n\ |
1159 | prompt message and the passphrase tag respectively. The TIMEOUT is an\n\ |
1160 | integer, optionally followed by `d', `h', `m' or `s' to specify units of\n\ |
1161 | days, hours, minutes or seconds respectively.\n\ |
1162 | \n\ |
1163 | In client mode, if a command is specified on the command line, it is sent\n\ |
1164 | to the running server; otherwise the program reads requests from stdin.\n\ |
1165 | Responses from the pixie are written to stdout. Send a HELP request for\n\ |
1166 | a quick summary of the pixie communication protocol.\n\ |
1167 | "); |
1168 | } |
1169 | |
1170 | /* --- @main@ --- * |
1171 | * |
1172 | * Arguments: @int argc@ = number of arguments |
1173 | * @char *argv[]@ = vector of argument values |
1174 | * |
1175 | * Returns: Zero if OK. |
1176 | * |
1177 | * Use: Main program. Listens on a socket and responds with a PGP |
1178 | * passphrase when asked. |
1179 | */ |
1180 | |
1181 | int main(int argc, char *argv[]) |
1182 | { |
1183 | char *path = 0; |
1184 | struct sockaddr_un *sun; |
1185 | size_t sz; |
1186 | unsigned f = 0; |
1187 | |
16efd15b |
1188 | #define f_bogus 1u |
1189 | #define f_client 2u |
1190 | #define f_stdin 4u |
1191 | #define f_daemon 8u |
1192 | #define f_syslog 16u |
069c185c |
1193 | |
1194 | /* --- Initialize libraries --- */ |
1195 | |
1196 | ego(argv[0]); |
1197 | sub_init(); |
1198 | |
1199 | /* --- Set up the locked memory area --- */ |
1200 | |
6a2d21e4 |
1201 | l_init(&lm, 16384); |
069c185c |
1202 | setuid(getuid()); |
1203 | |
1204 | /* --- Parse command line arguments --- */ |
1205 | |
1206 | for (;;) { |
1207 | static struct option opts[] = { |
1208 | |
1209 | /* --- Standard GNUy help options --- */ |
1210 | |
1211 | { "help", 0, 0, 'h' }, |
1212 | { "version", 0, 0, 'V' }, |
1213 | { "usage", 0, 0, 'u' }, |
1214 | |
1215 | /* --- Other options --- */ |
1216 | |
1217 | { "quiet", 0, 0, 'q' }, |
1218 | { "verbose", 0, 0, 'v' }, |
1219 | { "client", 0, 0, 'C' }, |
1220 | { "socket", OPTF_ARGREQ, 0, 's' }, |
1221 | { "command", OPTF_ARGREQ, 0, 'c' }, |
6a2d21e4 |
1222 | { "fetch", 0, 0, 'f' }, |
069c185c |
1223 | { "timeout", OPTF_ARGREQ, 0, 't' }, |
1224 | { "interactive", 0, 0, 'i' }, |
1225 | { "stdin", 0, 0, 'i' }, |
1226 | { "daemon", 0, 0, 'd' }, |
1227 | { "log", 0, 0, 'l' }, |
1228 | { "syslog", 0, 0, 'l' }, |
1229 | |
1230 | /* --- Magic terminator --- */ |
1231 | |
1232 | { 0, 0, 0, 0 } |
1233 | }; |
1234 | |
6a2d21e4 |
1235 | int i = mdwopt(argc, argv, "hVuqvCs:c:ft:idl", opts, 0, 0, 0); |
069c185c |
1236 | if (i < 0) |
1237 | break; |
1238 | |
1239 | switch (i) { |
1240 | |
1241 | /* --- GNUy help options --- */ |
1242 | |
1243 | case 'h': |
1244 | help(stdout); |
1245 | exit(0); |
1246 | case 'V': |
1247 | version(stdout); |
1248 | exit(0); |
1249 | case 'u': |
1250 | usage(stdout); |
1251 | exit(0); |
1252 | |
1253 | /* --- Other interesting things --- */ |
1254 | |
1255 | case 'q': |
1256 | if (verbose) |
1257 | verbose--; |
1258 | break; |
1259 | case 'v': |
1260 | verbose++; |
1261 | break; |
1262 | case 'C': |
1263 | f |= f_client; |
1264 | break; |
1265 | case 's': |
1266 | path = optarg; |
1267 | break; |
1268 | case 't': |
1269 | if ((timeout = pixserv_timeout(optarg)) == 0) |
1270 | die(1, "bad timeout `%s'", optarg); |
1271 | break; |
1272 | case 'c': |
1273 | command = optarg; |
6a2d21e4 |
1274 | flags |= F_FETCH; |
1275 | break; |
1276 | case 'f': |
1277 | flags |= F_FETCH; |
069c185c |
1278 | break; |
1279 | case 'i': |
1280 | f |= f_stdin; |
1281 | break; |
1282 | case 'd': |
1283 | f |= f_daemon; |
1284 | break; |
1285 | case 'l': |
1286 | f |= f_syslog; |
1287 | break; |
1288 | |
1289 | /* --- Something else --- */ |
1290 | |
1291 | default: |
1292 | f |= f_bogus; |
1293 | break; |
1294 | } |
1295 | } |
1296 | |
1297 | if (f & f_bogus || (optind < argc && !(f & f_client))) { |
1298 | usage(stderr); |
1299 | exit(1); |
1300 | } |
1301 | |
1302 | /* --- Set up the socket address --- */ |
1303 | |
1304 | sun = pixie_address(path, &sz); |
1305 | |
1306 | /* --- Initialize selectory --- */ |
1307 | |
1308 | sel_init(&sel); |
1309 | signal(SIGPIPE, SIG_IGN); |
1310 | |
1311 | /* --- Be a client if a client's wanted --- */ |
1312 | |
1313 | if (f & f_client) |
1314 | pix_client(sun, sz, argv + optind); |
1315 | |
1316 | /* --- Open the syslog if requested --- */ |
1317 | |
1318 | if (f & f_syslog) { |
1319 | flags |= F_SYSLOG; |
1320 | openlog(QUIS, 0, LOG_DAEMON); |
1321 | } |
1322 | |
1323 | /* --- Check on the locked memory area --- */ |
1324 | |
1325 | { |
1326 | dstr d = DSTR_INIT; |
1327 | int rc = l_report(&lm, &d); |
1328 | if (rc < 0) |
1329 | die(EXIT_FAILURE, d.buf); |
1330 | else if (rc && verbose) { |
1331 | log(d.buf); |
1332 | log("couldn't lock passphrase buffer"); |
1333 | } |
1334 | dstr_destroy(&d); |
6a2d21e4 |
1335 | arena_setsecure(&lm.a); |
069c185c |
1336 | } |
1337 | |
1338 | /* --- Set signal behaviours --- */ |
1339 | |
1340 | { |
1341 | static sig sigint, sigterm, sigquit, sighup; |
2c41d513 |
1342 | struct sigaction sa; |
069c185c |
1343 | sig_init(&sel); |
2c41d513 |
1344 | sigaction(SIGINT, 0, &sa); |
1345 | if (sa.sa_handler != SIG_IGN) |
1346 | sig_add(&sigint, SIGINT, pix_sigdie, 0); |
069c185c |
1347 | sig_add(&sigterm, SIGTERM, pix_sigdie, 0); |
1348 | sig_add(&sigquit, SIGQUIT, pix_sigflush, 0); |
1349 | sig_add(&sighup, SIGHUP, pix_sigflush, 0); |
1350 | } |
1351 | |
1352 | /* --- Set up the server --- */ |
1353 | |
1354 | pix_setup(sun, sz); |
6a2d21e4 |
1355 | if (f & f_stdin) { |
1356 | pixserv *px = pixserv_create(STDIN_FILENO, STDOUT_FILENO); |
1357 | sel_rmtimer(&px->timer); |
1358 | px->f |= px_stdin; |
1359 | } |
069c185c |
1360 | |
1361 | /* --- Fork into the background if requested --- */ |
1362 | |
1363 | if (f & f_daemon) { |
1364 | pid_t kid; |
1365 | |
1366 | if (((f & f_stdin) && |
1367 | (isatty(STDIN_FILENO) || isatty(STDOUT_FILENO))) || |
6a2d21e4 |
1368 | (!command && (flags & F_FETCH))) |
069c185c |
1369 | die(1, "can't become a daemon if terminal required"); |
1370 | |
1371 | if ((kid = fork()) < 0) |
1372 | die(1, "fork failed: %s", strerror(errno)); |
1373 | if (kid) |
1374 | _exit(0); |
1375 | #ifdef TIOCNOTTY |
1376 | { |
1377 | int fd; |
1378 | if ((fd = open("/dev/tty", O_RDONLY)) >= 0) { |
1379 | ioctl(fd, TIOCNOTTY); |
1380 | close(fd); |
1381 | } |
1382 | } |
1383 | #endif |
4cb04adb |
1384 | chdir("/"); |
069c185c |
1385 | setsid(); |
1386 | |
3884054b |
1387 | if (fork() != 0) |
069c185c |
1388 | _exit(0); |
1389 | } |
1390 | |
2b2d07ca |
1391 | if (verbose) |
1392 | log("initialized ok"); |
2c41d513 |
1393 | |
1394 | { |
1395 | int selerr = 0; |
1396 | for (;;) { |
1397 | if (!sel_select(&sel)) |
1398 | selerr = 0; |
1399 | else if (errno != EINTR && errno != EAGAIN) { |
1400 | log("error from select: %s", strerror(errno)); |
1401 | selerr++; |
1402 | if (selerr > 8) { |
1403 | log("too many consecutive select errors: bailing out"); |
1404 | exit(EXIT_FAILURE); |
1405 | } |
1406 | } |
1407 | } |
1408 | } |
069c185c |
1409 | return (0); |
1410 | } |
1411 | |
1412 | /*----- That's all, folks -------------------------------------------------*/ |