| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id$ |
| 4 | * |
| 5 | * Admin interface for configuration |
| 6 | * |
| 7 | * (c) 2001 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Trivial IP Encryption (TrIPE). |
| 13 | * |
| 14 | * TrIPE is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * TrIPE 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 General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with TrIPE; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Header files ------------------------------------------------------*/ |
| 30 | |
| 31 | #include "tripe.h" |
| 32 | |
| 33 | /*----- Global variables --------------------------------------------------*/ |
| 34 | |
| 35 | #ifndef NTRACE |
| 36 | |
| 37 | const trace_opt tr_opts[] = { |
| 38 | { 't', T_TUNNEL, "tunnel events" }, |
| 39 | { 'r', T_PEER, "peer events" }, |
| 40 | { 'a', T_ADMIN, "admin interface" }, |
| 41 | { 'p', T_PACKET, "packet contents" }, |
| 42 | { 'c', T_CRYPTO, "crypto details" }, |
| 43 | { 's', T_KEYSET, "symmetric keyset management" }, |
| 44 | { 'x', T_KEYEXCH, "key exchange" }, |
| 45 | { 'm', T_KEYMGMT, "key management" }, |
| 46 | { 'A', T_ALL, "all of the above" }, |
| 47 | { 0, 0, 0 } |
| 48 | }; |
| 49 | |
| 50 | unsigned tr_flags = 0; |
| 51 | #endif |
| 52 | |
| 53 | static const trace_opt w_opts[] = { |
| 54 | { 't', AF_TRACE, "trace messages" }, |
| 55 | { 'n', AF_NOTE, "asynchronous notifications" }, |
| 56 | { 'w', AF_WARN, "warnings" }, |
| 57 | { 'A', AF_ALLMSGS, "all of the above" }, |
| 58 | { 0, 0, 0 } |
| 59 | }; |
| 60 | |
| 61 | /*----- Static variables --------------------------------------------------*/ |
| 62 | |
| 63 | static admin *admins; |
| 64 | static sel_file sock; |
| 65 | static const char *sockname; |
| 66 | static unsigned flags = 0; |
| 67 | static admin *a_stdin = 0; |
| 68 | static sig s_term, s_int, s_hup; |
| 69 | |
| 70 | #define F_DAEMON 1u |
| 71 | #define F_INIT 2u |
| 72 | |
| 73 | #define T_RESOLVE SEC(30) |
| 74 | |
| 75 | static void a_destroy(admin */*a*/); |
| 76 | static void a_lock(admin */*a*/); |
| 77 | static void a_unlock(admin */*a*/); |
| 78 | |
| 79 | /*----- Output functions --------------------------------------------------*/ |
| 80 | |
| 81 | /* --- @trywrite@ --- * |
| 82 | * |
| 83 | * Arguments: @admin *a@ = pointer to an admin block |
| 84 | * @const char *p@ = pointer to buffer to write |
| 85 | * @size_t sz@ = size of data to write |
| 86 | * |
| 87 | * Returns: The number of bytes written, or less than zero on error. |
| 88 | * |
| 89 | * Use: Attempts to write data to a client. |
| 90 | */ |
| 91 | |
| 92 | static ssize_t trywrite(admin *a, const char *p, size_t sz) |
| 93 | { |
| 94 | ssize_t n, done = 0; |
| 95 | |
| 96 | again: |
| 97 | if (!sz) |
| 98 | return (done); |
| 99 | n = write(a->w.fd, p, sz); |
| 100 | if (n > 0) { |
| 101 | done += n; |
| 102 | p += n; |
| 103 | sz -= n; |
| 104 | goto again; |
| 105 | } |
| 106 | if (n < 0) { |
| 107 | if (errno == EINTR) |
| 108 | goto again; |
| 109 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 110 | a_destroy(a); |
| 111 | a_warn("ADMIN client-read-error -- %s", strerror(errno)); |
| 112 | return (-1); |
| 113 | } |
| 114 | } |
| 115 | return (done); |
| 116 | } |
| 117 | |
| 118 | /* --- @dosend@ --- * |
| 119 | * |
| 120 | * Arguemnts: @admin *a@ = pointer to an admin block |
| 121 | * @const char *p@ = pointer to buffer to write |
| 122 | * @size_t sz@ = size of data to write |
| 123 | * |
| 124 | * Returns: --- |
| 125 | * |
| 126 | * Use: Sends data to an admin client. |
| 127 | */ |
| 128 | |
| 129 | static void dosend(admin *a, const char *p, size_t sz) |
| 130 | { |
| 131 | ssize_t n; |
| 132 | obuf *o; |
| 133 | |
| 134 | if (a->f & AF_DEAD) |
| 135 | return; |
| 136 | |
| 137 | /* --- Try to send the data immediately --- */ |
| 138 | |
| 139 | if (!a->o_head) { |
| 140 | if ((n = trywrite(a, p, sz)) < 0) |
| 141 | return; |
| 142 | p += n; |
| 143 | sz -= n; |
| 144 | if (!sz) |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | /* --- Fill buffers with the data until it's all gone --- */ |
| 149 | |
| 150 | o = a->o_tail; |
| 151 | if (!o) |
| 152 | sel_addfile(&a->w); |
| 153 | else if (o->p_in < o->buf + OBUFSZ) |
| 154 | goto noalloc; |
| 155 | |
| 156 | do { |
| 157 | o = xmalloc(sizeof(obuf)); |
| 158 | o->next = 0; |
| 159 | o->p_in = o->p_out = o->buf; |
| 160 | if (a->o_tail) |
| 161 | a->o_tail->next = o; |
| 162 | else |
| 163 | a->o_head = o; |
| 164 | a->o_tail = o; |
| 165 | |
| 166 | noalloc: |
| 167 | n = o->buf + OBUFSZ - o->p_in; |
| 168 | if (n > sz) |
| 169 | n = sz; |
| 170 | memcpy(o->p_in, p, n); |
| 171 | o->p_in += n; |
| 172 | p += n; |
| 173 | sz -= n; |
| 174 | } while (sz); |
| 175 | } |
| 176 | |
| 177 | /* --- @a_flush@ --- * |
| 178 | * |
| 179 | * Arguments: @int fd@ = file descriptor |
| 180 | * @unsigned mode@ = what's happening |
| 181 | * @void *v@ = pointer to my admin block |
| 182 | * |
| 183 | * Returns: --- |
| 184 | * |
| 185 | * Use: Flushes buffers when a client is ready to read again. |
| 186 | */ |
| 187 | |
| 188 | static void a_flush(int fd, unsigned mode, void *v) |
| 189 | { |
| 190 | admin *a = v; |
| 191 | obuf *o, *oo; |
| 192 | ssize_t n; |
| 193 | |
| 194 | o = a->o_head; |
| 195 | while (o) { |
| 196 | if ((n = trywrite(a, o->p_out, o->p_in - o->p_out)) < 0) |
| 197 | return; |
| 198 | o->p_out += n; |
| 199 | if (o->p_in < o->p_out) |
| 200 | break; |
| 201 | oo = o; |
| 202 | o = o->next; |
| 203 | xfree(oo); |
| 204 | } |
| 205 | a->o_head = o; |
| 206 | if (!o) { |
| 207 | a->o_tail = 0; |
| 208 | sel_rmfile(&a->w); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /*----- Utility functions -------------------------------------------------*/ |
| 213 | |
| 214 | /* --- @a_write@, @a_vwrite@ --- * |
| 215 | * |
| 216 | * Arguments: @admin *a@ = admin connection to write to |
| 217 | * @const char *tag@ = tag prefix string, or null |
| 218 | * @const char *fmt@ = pointer to format string |
| 219 | * @va_list ap@ = arguments in list |
| 220 | * @...@ = other arguments |
| 221 | * |
| 222 | * Returns: --- |
| 223 | * |
| 224 | * Use: Sends a message to an admin connection. |
| 225 | */ |
| 226 | |
| 227 | static void a_vwrite(admin *a, const char *tag, const char *fmt, va_list ap) |
| 228 | { |
| 229 | dstr d = DSTR_INIT; |
| 230 | if (tag) { |
| 231 | dstr_puts(&d, tag); |
| 232 | if (fmt) |
| 233 | dstr_putc(&d, ' '); |
| 234 | } |
| 235 | if (fmt) |
| 236 | dstr_vputf(&d, fmt, &ap); |
| 237 | dstr_putc(&d, '\n'); |
| 238 | dosend(a, d.buf, d.len); |
| 239 | dstr_destroy(&d); |
| 240 | } |
| 241 | |
| 242 | static void a_write(admin *a, const char *tag, const char *fmt, ...) |
| 243 | { |
| 244 | va_list ap; |
| 245 | va_start(ap, fmt); |
| 246 | a_vwrite(a, tag, fmt, ap); |
| 247 | va_end(ap); |
| 248 | } |
| 249 | |
| 250 | /* --- @a_ok@, @a_info@, @a_fail@ --- * |
| 251 | * |
| 252 | * Arguments: @admin *a@ = connection |
| 253 | * @const char *fmt@ = format string |
| 254 | * @...@ = other arguments |
| 255 | * |
| 256 | * Returns: --- |
| 257 | * |
| 258 | * Use: Convenience functions for @a_write@. |
| 259 | */ |
| 260 | |
| 261 | static void a_ok(admin *a) { a_write(a, "OK", 0); } |
| 262 | |
| 263 | static void a_info(admin *a, const char *fmt, ...) |
| 264 | { |
| 265 | va_list ap; |
| 266 | va_start(ap, fmt); |
| 267 | a_vwrite(a, "INFO", fmt, ap); |
| 268 | va_end(ap); |
| 269 | } |
| 270 | |
| 271 | static void a_fail(admin *a, const char *fmt, ...) |
| 272 | { |
| 273 | va_list ap; |
| 274 | va_start(ap, fmt); |
| 275 | a_vwrite(a, "FAIL", fmt, ap); |
| 276 | va_end(ap); |
| 277 | } |
| 278 | |
| 279 | /* --- @a_alert@, @a_valert@, @a_rawalert@ --- * |
| 280 | * |
| 281 | * Arguments: @unsigned f_and, f_eq@ = filter for connections |
| 282 | * @const char *tag@ = tag prefix string |
| 283 | * @const char *fmt@ = pointer to format string |
| 284 | @ @const char *p@ = pointer to raw string |
| 285 | * @size_t sz@ = size of raw string |
| 286 | * @va_list ap@ = arguments in list |
| 287 | * @...@ = other arguments |
| 288 | * |
| 289 | * Returns: --- |
| 290 | * |
| 291 | * Use: Write a message to all admin connections matched by the given |
| 292 | * filter. |
| 293 | */ |
| 294 | |
| 295 | static void a_rawalert(unsigned f_and, unsigned f_eq, const char *tag, |
| 296 | const char *p, size_t sz) |
| 297 | { |
| 298 | admin *a, *aa; |
| 299 | dstr d = DSTR_INIT; |
| 300 | |
| 301 | if (!(flags & F_INIT)) |
| 302 | return; |
| 303 | if (tag) { |
| 304 | dstr_puts(&d, tag); |
| 305 | if (p) |
| 306 | dstr_putc(&d, ' '); |
| 307 | } |
| 308 | if (p) |
| 309 | dstr_putm(&d, p, sz); |
| 310 | dstr_putc(&d, '\n'); |
| 311 | p = d.buf; |
| 312 | sz = d.len; |
| 313 | for (a = admins; a; a = aa) { |
| 314 | aa = a->next; |
| 315 | if ((a->f & f_and) == f_eq) |
| 316 | dosend(a, d.buf, d.len); |
| 317 | } |
| 318 | dstr_destroy(&d); |
| 319 | } |
| 320 | |
| 321 | static void a_valert(unsigned f_and, unsigned f_eq, const char *tag, |
| 322 | const char *fmt, va_list ap) |
| 323 | { |
| 324 | dstr d = DSTR_INIT; |
| 325 | |
| 326 | if (!(flags & F_INIT)) |
| 327 | return; |
| 328 | if (fmt) |
| 329 | dstr_vputf(&d, fmt, &ap); |
| 330 | a_rawalert(f_and, f_eq, tag, fmt ? d.buf : 0, fmt ? d.len : 0); |
| 331 | dstr_destroy(&d); |
| 332 | } |
| 333 | |
| 334 | #if 0 /*unused*/ |
| 335 | static void a_alert(unsigned f_and, unsigned f_eq, const char *tag, |
| 336 | const char *fmt, ...) |
| 337 | { |
| 338 | va_list ap; |
| 339 | va_start(ap, fmt); |
| 340 | a_valert(f_and, f_eq, tag, fmt, ap); |
| 341 | va_end(ap); |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | /* --- @a_warn@ --- * |
| 346 | * |
| 347 | * Arguments: @const char *fmt@ = pointer to format string |
| 348 | * @...@ = other arguments |
| 349 | * |
| 350 | * Returns: --- |
| 351 | * |
| 352 | * Use: Informs all admin connections of a warning. |
| 353 | */ |
| 354 | |
| 355 | void a_warn(const char *fmt, ...) |
| 356 | { |
| 357 | va_list ap; |
| 358 | |
| 359 | va_start(ap, fmt); |
| 360 | if (flags & F_INIT) |
| 361 | a_valert(0, 0, "WARN", fmt, ap); |
| 362 | else { |
| 363 | fprintf(stderr, "%s: ", QUIS); |
| 364 | vfprintf(stderr, fmt, ap); |
| 365 | fputc('\n', stderr); |
| 366 | } |
| 367 | va_end(ap); |
| 368 | } |
| 369 | |
| 370 | /* --- @a_trace@ --- * |
| 371 | * |
| 372 | * Arguments: @const char *p@ = pointer to a buffer |
| 373 | * @size_t sz@ = size of the buffer |
| 374 | * @void *v@ = uninteresting pointer |
| 375 | * |
| 376 | * Returns: --- |
| 377 | * |
| 378 | * Use: Custom trace output handler. Sends trace messages to |
| 379 | * interested admin connections. |
| 380 | */ |
| 381 | |
| 382 | #ifndef NTRACE |
| 383 | static void a_trace(const char *p, size_t sz, void *v) |
| 384 | { |
| 385 | a_rawalert(AF_TRACE, AF_TRACE, "TRACE", p, sz); |
| 386 | } |
| 387 | #endif |
| 388 | |
| 389 | /* --- @a_notify@ --- * |
| 390 | * |
| 391 | * Arguments: @const char *fmt@ = pointer to format string |
| 392 | * @...@ = other arguments |
| 393 | * |
| 394 | * Returns: --- |
| 395 | * |
| 396 | * Use: Sends a notification to interested admin connections. |
| 397 | */ |
| 398 | |
| 399 | void a_notify(const char *fmt, ...) |
| 400 | { |
| 401 | va_list ap; |
| 402 | |
| 403 | va_start(ap, fmt); |
| 404 | a_valert(AF_NOTE, AF_NOTE, "NOTE", fmt, ap); |
| 405 | va_end(ap); |
| 406 | } |
| 407 | |
| 408 | /* --- @a_quit@ --- * |
| 409 | * |
| 410 | * Arguments: --- |
| 411 | * |
| 412 | * Returns: --- |
| 413 | * |
| 414 | * Use: Shuts things down nicely. |
| 415 | */ |
| 416 | |
| 417 | void a_quit(void) |
| 418 | { |
| 419 | peer *p; |
| 420 | |
| 421 | while ((p = p_first()) != 0) |
| 422 | p_destroy(p); |
| 423 | close(sock.fd); |
| 424 | unlink(sockname); |
| 425 | exit(0); |
| 426 | } |
| 427 | |
| 428 | /* --- @a_sigdie@ --- * |
| 429 | * |
| 430 | * Arguments: @int sig@ = signal number |
| 431 | * @void *v@ = an uninteresting argument |
| 432 | * |
| 433 | * Returns: --- |
| 434 | * |
| 435 | * Use Shuts down on receipt of a fatal signal. |
| 436 | */ |
| 437 | |
| 438 | static void a_sigdie(int sig, void *v) |
| 439 | { |
| 440 | char *p; |
| 441 | char buf[20]; |
| 442 | |
| 443 | switch (sig) { |
| 444 | case SIGTERM: p = "SIGTERM"; break; |
| 445 | case SIGINT: p = "SIGINT"; break; |
| 446 | default: |
| 447 | sprintf(buf, "%i", sig); |
| 448 | p = buf; |
| 449 | break; |
| 450 | } |
| 451 | a_warn("SERVER quit signal %s", p); |
| 452 | a_quit(); |
| 453 | } |
| 454 | |
| 455 | /* --- @a_sighup@ --- * |
| 456 | * |
| 457 | * Arguments: @int sig@ = signal number |
| 458 | * @void *v@ = an uninteresting argument |
| 459 | * |
| 460 | * Returns: --- |
| 461 | * |
| 462 | * Use Logs a message about SIGHUP not being useful. |
| 463 | */ |
| 464 | |
| 465 | static void a_sighup(int sig, void *v) |
| 466 | { |
| 467 | a_warn("SERVER ignore signal SIGHUP"); |
| 468 | } |
| 469 | |
| 470 | /*----- Adding peers ------------------------------------------------------*/ |
| 471 | |
| 472 | /* --- @a_resolve@ --- * |
| 473 | * |
| 474 | * Arguments: @struct hostent *h@ = pointer to resolved hostname |
| 475 | * @void *v@ = pointer to admin block |
| 476 | * |
| 477 | * Returns: --- |
| 478 | * |
| 479 | * Use: Handles a completed name resolution. |
| 480 | */ |
| 481 | |
| 482 | static void a_resolve(struct hostent *h, void *v) |
| 483 | { |
| 484 | admin *a = v; |
| 485 | |
| 486 | a_lock(a); |
| 487 | T( trace(T_ADMIN, "admin: %u resolved", a->seq); ) |
| 488 | TIMER; |
| 489 | sel_rmtimer(&a->t); |
| 490 | if (!h) |
| 491 | a_fail(a, "resolve-error %s", a->paddr); |
| 492 | else if (p_find(a->pname)) |
| 493 | a_fail(a, "peer-exists %s", a->pname); |
| 494 | else { |
| 495 | memcpy(&a->peer.sin.sin_addr, h->h_addr, sizeof(struct in_addr)); |
| 496 | if (!p_create(a->pname, a->tops, &a->peer.sa, a->sasz)) |
| 497 | a_fail(a, "peer-create-fail %s", a->pname); |
| 498 | else |
| 499 | a_ok(a); |
| 500 | } |
| 501 | xfree(a->pname); |
| 502 | xfree(a->paddr); |
| 503 | a->pname = 0; |
| 504 | selbuf_enable(&a->b); |
| 505 | a_unlock(a); |
| 506 | } |
| 507 | |
| 508 | /* --- @a_timer@ --- * |
| 509 | * |
| 510 | * Arguments: @struct timeval *tv@ = timer |
| 511 | * @void *v@ = pointer to admin block |
| 512 | * |
| 513 | * Returns: --- |
| 514 | * |
| 515 | * Use: Times out a resolver. |
| 516 | */ |
| 517 | |
| 518 | static void a_timer(struct timeval *tv, void *v) |
| 519 | { |
| 520 | admin *a = v; |
| 521 | |
| 522 | a_lock(a); |
| 523 | T( trace(T_ADMIN, "admin: %u resolver timeout", a->seq); ) |
| 524 | bres_abort(&a->r); |
| 525 | a_fail(a, "resolver-timeout %s\n", a->paddr); |
| 526 | xfree(a->pname); |
| 527 | xfree(a->paddr); |
| 528 | a->pname = 0; |
| 529 | selbuf_enable(&a->b); |
| 530 | a_unlock(a); |
| 531 | } |
| 532 | |
| 533 | /* --- @acmd_add@ --- * |
| 534 | * |
| 535 | * Arguments: @admin *a@ = connection which requested the addition |
| 536 | * @unsigned ac@ = argument count |
| 537 | * @char *av[]@ = pointer to the argument list |
| 538 | * |
| 539 | * Returns: --- |
| 540 | * |
| 541 | * Use: Adds a new peer. |
| 542 | */ |
| 543 | |
| 544 | static void acmd_add(admin *a, unsigned ac, char *av[]) |
| 545 | { |
| 546 | unsigned long pt; |
| 547 | struct timeval tv; |
| 548 | unsigned i, j; |
| 549 | const tunnel_ops *tops = tun_default; |
| 550 | char *p; |
| 551 | |
| 552 | /* --- Make sure someone's not got there already --- */ |
| 553 | |
| 554 | if (p_find(av[0])) { |
| 555 | a_fail(a, "peer-exists %s", av[0]); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | /* --- Parse options --- */ |
| 560 | |
| 561 | i = 1; |
| 562 | for (;;) { |
| 563 | if (!av[i]) |
| 564 | goto bad_syntax; |
| 565 | if (mystrieq(av[i], "-tunnel")) { |
| 566 | i++; |
| 567 | if (!av[i]) |
| 568 | goto bad_syntax; |
| 569 | for (j = 0;; j++) { |
| 570 | if (!tunnels[j]) { |
| 571 | a_fail(a, "unknown-tunnel %s", av[i]); |
| 572 | return; |
| 573 | } |
| 574 | if (mystrieq(av[i], tunnels[j]->name)) { |
| 575 | tops = tunnels[j]; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | i++; |
| 580 | } else if (mystrieq(av[i], "--")) { |
| 581 | i++; |
| 582 | break; |
| 583 | } else |
| 584 | break; |
| 585 | } |
| 586 | |
| 587 | /* --- Fill in the easy bits of address --- */ |
| 588 | |
| 589 | BURN(a->peer); |
| 590 | if (mystrieq(av[i], "inet")) i++; |
| 591 | if (ac - i != 2) { |
| 592 | a_fail(a, "bad-syntax -- add PEER [-tunnel TUN] [inet] ADDRESS PORT"); |
| 593 | return; |
| 594 | } |
| 595 | a->peer.sin.sin_family = AF_INET; |
| 596 | a->sasz = sizeof(a->peer.sin); |
| 597 | pt = strtoul(av[i + 1], &p, 0); |
| 598 | if (*p) { |
| 599 | struct servent *s = getservbyname(av[i + 1], "udp"); |
| 600 | if (!s) { |
| 601 | a_fail(a, "unknown-service %s", av[i + 1]); |
| 602 | return; |
| 603 | } |
| 604 | pt = ntohs(s->s_port); |
| 605 | } |
| 606 | if (pt == 0 || pt >= 65536) { |
| 607 | a_fail(a, "invalid-port %lu", pt); |
| 608 | return; |
| 609 | } |
| 610 | a->peer.sin.sin_port = htons(pt); |
| 611 | |
| 612 | /* --- If the name is numeric, do it the easy way --- */ |
| 613 | |
| 614 | if (inet_aton(av[i], &a->peer.sin.sin_addr)) { |
| 615 | if (!p_create(av[0], tops, &a->peer.sa, a->sasz)) |
| 616 | a_fail(a, "peer-create-fail %s", a->pname); |
| 617 | else |
| 618 | a_ok(a); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | /* --- Store everything for later and crank up the resolver --- * |
| 623 | * |
| 624 | * We disable the line buffer until the resolver completes (or times out). |
| 625 | * This prevents other commands on the same connection (though the rest of |
| 626 | * the system continues regardless), but makes life simpler for the client. |
| 627 | */ |
| 628 | |
| 629 | a->pname = xstrdup(av[0]); |
| 630 | a->paddr = xstrdup(av[i]); |
| 631 | a->tops = tops; |
| 632 | selbuf_disable(&a->b); |
| 633 | gettimeofday(&tv, 0); |
| 634 | tv.tv_sec += T_RESOLVE; |
| 635 | sel_addtimer(&sel, &a->t, &tv, a_timer, a); |
| 636 | bres_byname(&a->r, a->paddr, a_resolve, a); |
| 637 | T( trace(T_ADMIN, "admin: %u resolving hostname `%s'", |
| 638 | a->seq, a->paddr); ) |
| 639 | return; |
| 640 | |
| 641 | bad_syntax: |
| 642 | a_fail(a, "bad-syntax -- add PEER [-tunnel TUN] ADDR ..."); |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | /*----- Administration commands -------------------------------------------*/ |
| 647 | |
| 648 | /* --- Miscellaneous commands --- */ |
| 649 | |
| 650 | /* --- @traceish@ --- * |
| 651 | * |
| 652 | * Arguments: @admin *a@ = connection to complain on |
| 653 | * @unsigned ac@ = number of arguments |
| 654 | * @char *av[]@ = vector of arguments |
| 655 | * @const char *what@ = what we're messing with |
| 656 | * @const trace_opt *tt@ = options table |
| 657 | * @unsigned *ff@ = where the flags are |
| 658 | * |
| 659 | * Returns: Nonzero if anything changed. |
| 660 | * |
| 661 | * Use: Guts of trace-ish commands like `trace' and `watch'. |
| 662 | */ |
| 663 | |
| 664 | static int traceish(admin *a, unsigned ac, char *av[], |
| 665 | const char *what, const trace_opt *tt, unsigned *ff) |
| 666 | { |
| 667 | int ch = 0; |
| 668 | |
| 669 | if (!ac || strcmp(av[0], "?") == 0) { |
| 670 | const trace_opt *t; |
| 671 | a_info(a, "Current %s status:", what); |
| 672 | for (t = tt; t->ch; t++) { |
| 673 | a_info(a, "%c %c %s", |
| 674 | t->ch, (*ff & t->f) == t->f ? '*' : ' ', t->help); |
| 675 | } |
| 676 | } else { |
| 677 | unsigned sense = 1; |
| 678 | unsigned f = *ff; |
| 679 | const trace_opt *t; |
| 680 | char *p = av[0]; |
| 681 | |
| 682 | while (*p) { |
| 683 | switch (*p) { |
| 684 | case '+': sense = 1; break; |
| 685 | case '-': sense = 0; break; |
| 686 | default: |
| 687 | for (t = tt; t->ch; t++) { |
| 688 | if (t->ch == *p) { |
| 689 | if (sense) f |= t->f; |
| 690 | else f &= ~t->f; |
| 691 | goto tropt_ok; |
| 692 | } |
| 693 | } |
| 694 | a_fail(a, "bad-%s-option %c", what, *p); |
| 695 | return (0); |
| 696 | tropt_ok:; |
| 697 | break; |
| 698 | } |
| 699 | p++; |
| 700 | } |
| 701 | *ff = f; |
| 702 | ch = 1; |
| 703 | } |
| 704 | a_ok(a); |
| 705 | return (ch); |
| 706 | } |
| 707 | |
| 708 | #ifndef NTRACE |
| 709 | |
| 710 | static void acmd_trace(admin *a, unsigned ac, char *av[]) |
| 711 | { |
| 712 | if (traceish(a, ac, av, "trace", tr_opts, &tr_flags)) |
| 713 | trace_level(tr_flags); |
| 714 | } |
| 715 | |
| 716 | #endif |
| 717 | |
| 718 | static void acmd_watch(admin *a, unsigned ac, char *av[]) |
| 719 | { |
| 720 | traceish(a, ac, av, "watch", w_opts, &a->f); |
| 721 | } |
| 722 | |
| 723 | static void quotify(dstr *d, const char *p) |
| 724 | { |
| 725 | if (d->len) |
| 726 | dstr_putc(d, ' '); |
| 727 | if (*p && !p[strcspn(p, "\"' \t\n\v")]) |
| 728 | dstr_puts(d, p); |
| 729 | else { |
| 730 | dstr_putc(d, '\"'); |
| 731 | while (*p) { |
| 732 | if (*p == '\\' || *p == '\"') |
| 733 | dstr_putc(d, '\\'); |
| 734 | dstr_putc(d, *p++); |
| 735 | } |
| 736 | dstr_putc(d, '\"'); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | static void alertcmd(admin *a, unsigned f_and, unsigned f_eq, |
| 741 | const char *tag, unsigned ac, char *av[]) |
| 742 | { |
| 743 | dstr d = DSTR_INIT; |
| 744 | unsigned i; |
| 745 | |
| 746 | dstr_puts(&d, "USER"); |
| 747 | for (i = 0; i < ac; i++) |
| 748 | quotify(&d, av[i]); |
| 749 | dstr_putz(&d); |
| 750 | a_rawalert(f_and, f_eq, tag, d.buf, d.len); |
| 751 | dstr_destroy(&d); |
| 752 | a_ok(a); |
| 753 | } |
| 754 | |
| 755 | static void acmd_notify(admin *a, unsigned ac, char *av[]) |
| 756 | { alertcmd(a, AF_NOTE, AF_NOTE, "NOTE", ac, av); } |
| 757 | static void acmd_warn(admin *a, unsigned ac, char *av[]) |
| 758 | { alertcmd(a, AF_WARN, AF_WARN, "WARN", ac, av); } |
| 759 | |
| 760 | static void acmd_port(admin *a, unsigned ac, char *av[]) |
| 761 | { |
| 762 | a_info(a, "%u", p_port()); |
| 763 | a_ok(a); |
| 764 | } |
| 765 | |
| 766 | static void acmd_daemon(admin *a, unsigned ac, char *av[]) |
| 767 | { |
| 768 | if (flags & F_DAEMON) |
| 769 | a_fail(a, "already-daemon"); |
| 770 | else { |
| 771 | a_notify("DAEMON"); |
| 772 | if (a_stdin) |
| 773 | a_destroy(a_stdin); |
| 774 | if (u_daemon()) |
| 775 | a_fail(a, "daemon-error -- %s", strerror(errno)); |
| 776 | else { |
| 777 | flags |= F_DAEMON; |
| 778 | a_ok(a); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | static void acmd_list(admin *a, unsigned ac, char *av[]) |
| 784 | { |
| 785 | peer *p; |
| 786 | for (p = p_first(); p; p = p_next(p)) |
| 787 | a_info(a, "%s", p_name(p)); |
| 788 | a_ok(a); |
| 789 | } |
| 790 | |
| 791 | static void acmd_ifname(admin *a, unsigned ac, char *av[]) |
| 792 | { |
| 793 | peer *p; |
| 794 | |
| 795 | if ((p = p_find(av[0])) == 0) |
| 796 | a_fail(a, "unknown-peer %s", av[0]); |
| 797 | else { |
| 798 | a_info(a, "%s", p_ifname(p)); |
| 799 | a_ok(a); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | static void acmd_addr(admin *a, unsigned ac, char *av[]) |
| 804 | { |
| 805 | peer *p; |
| 806 | const addr *ad; |
| 807 | |
| 808 | if ((p = p_find(av[0])) == 0) |
| 809 | a_fail(a, "unknown-peer %s", av[0]); |
| 810 | else { |
| 811 | ad = p_addr(p); |
| 812 | assert(ad->sa.sa_family == AF_INET); |
| 813 | a_info(a, "INET %s %u", |
| 814 | inet_ntoa(ad->sin.sin_addr), |
| 815 | (unsigned)ntohs(ad->sin.sin_port)); |
| 816 | a_ok(a); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | static void acmd_stats(admin *a, unsigned ac, char *av[]) |
| 821 | { |
| 822 | peer *p; |
| 823 | stats *st; |
| 824 | |
| 825 | if ((p = p_find(av[0])) == 0) |
| 826 | a_fail(a, "unknown-peer %s", av[0]); |
| 827 | else { |
| 828 | st = p_stats(p); |
| 829 | a_info(a, "start-time=%s", timestr(st->t_start)); |
| 830 | a_info(a, "last-packet-time=%s", timestr(st->t_last)); |
| 831 | a_info(a, "last-keyexch-time=%s", timestr(st->t_kx)); |
| 832 | a_info(a, "packets-in=%lu bytes-in=%lu", st->n_in, st->sz_in); |
| 833 | a_info(a, "packets-out=%lu bytes-out=%lu", |
| 834 | st->n_out, st->sz_out); |
| 835 | a_info(a, "keyexch-packets-in=%lu keyexch-bytes-in=%lu", |
| 836 | st->n_kxin, st->sz_kxin); |
| 837 | a_info(a, "keyexch-packets-out=%lu keyexch-bytes-out=%lu", |
| 838 | st->n_kxout, st->sz_kxout); |
| 839 | a_info(a, "ip-packets-in=%lu ip-bytes-in=%lu", |
| 840 | st->n_ipin, st->sz_ipin); |
| 841 | a_info(a, "ip-packets-out=%lu ip-bytes-out=%lu", |
| 842 | st->n_ipout, st->sz_ipout); |
| 843 | a_info(a, "rejected-packets=%lu", st->n_reject); |
| 844 | a_ok(a); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | static void acmd_kill(admin *a, unsigned ac, char *av[]) |
| 849 | { |
| 850 | peer *p; |
| 851 | if ((p = p_find(av[0])) == 0) |
| 852 | a_fail(a, "unknown-peer %s", av[0]); |
| 853 | else { |
| 854 | p_destroy(p); |
| 855 | a_ok(a); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | static void acmd_quit(admin *a, unsigned ac, char *av[]) |
| 860 | { |
| 861 | a_warn("SERVER quit admin-request"); |
| 862 | a_ok(a); |
| 863 | a_quit(); |
| 864 | } |
| 865 | |
| 866 | static void acmd_version(admin *a, unsigned ac, char *av[]) |
| 867 | { |
| 868 | a_info(a, "%s %s", PACKAGE, VERSION); |
| 869 | a_ok(a); |
| 870 | } |
| 871 | |
| 872 | static void acmd_tunnels(admin *a, unsigned ac, char *av[]) |
| 873 | { |
| 874 | int i; |
| 875 | for (i = 0; tunnels[i]; i++) |
| 876 | a_info(a, "%s", tunnels[i]->name); |
| 877 | a_ok(a); |
| 878 | } |
| 879 | |
| 880 | /* --- The command table and help --- */ |
| 881 | |
| 882 | typedef struct acmd { |
| 883 | const char *name; |
| 884 | const char *help; |
| 885 | unsigned argmin, argmax; |
| 886 | void (*func)(admin */*a*/, unsigned /*ac*/, char */*av*/[]); |
| 887 | } acmd; |
| 888 | |
| 889 | static void acmd_help(admin */*a*/, unsigned /*ac*/, char */*av*/[]); |
| 890 | |
| 891 | static const acmd acmdtab[] = { |
| 892 | { "help", "help", 0, 0, acmd_help }, |
| 893 | { "version", "version", 0, 0, acmd_version }, |
| 894 | #ifndef NTRACE |
| 895 | { "trace", "trace [OPTIONS]", 0, 1, acmd_trace }, |
| 896 | #endif |
| 897 | { "watch", "watch [OPTIONS]", 0, 1, acmd_watch }, |
| 898 | { "notify", "notify MESSAGE ...", 1, 0xffff, acmd_notify }, |
| 899 | { "warn", "warn MESSAGE ...", 1, 0xffff, acmd_warn }, |
| 900 | { "port", "port", 0, 0, acmd_port }, |
| 901 | { "daemon", "daemon", 0, 0, acmd_daemon }, |
| 902 | { "list", "list", 0, 0, acmd_list }, |
| 903 | { "ifname", "ifname PEER", 1, 1, acmd_ifname }, |
| 904 | { "addr", "addr PEER", 1, 1, acmd_addr }, |
| 905 | { "stats", "stats PEER", 1, 1, acmd_stats }, |
| 906 | { "kill", "kill PEER", 1, 1, acmd_kill }, |
| 907 | { "add", "add PEER [-tunnel TUN] ADDR ...", |
| 908 | 2, 0xffff, acmd_add }, |
| 909 | { "tunnels", "tunnels", 0, 0, acmd_tunnels }, |
| 910 | { "quit", "quit", 0, 0, acmd_quit }, |
| 911 | { 0, 0, 0, 0, 0 } |
| 912 | }; |
| 913 | |
| 914 | static void acmd_help(admin *a, unsigned ac, char *av[]) |
| 915 | { |
| 916 | const acmd *c; |
| 917 | for (c = acmdtab; c->name; c++) |
| 918 | a_info(a, "%s", c->help); |
| 919 | a_ok(a); |
| 920 | } |
| 921 | |
| 922 | /*----- Connection handling -----------------------------------------------*/ |
| 923 | |
| 924 | /* --- @a_lock@ --- * |
| 925 | * |
| 926 | * Arguments: @admin *a@ = pointer to an admin block |
| 927 | * |
| 928 | * Returns: --- |
| 929 | * |
| 930 | * Use: Locks an admin block so that it won't be destroyed |
| 931 | * immediately. |
| 932 | */ |
| 933 | |
| 934 | static void a_lock(admin *a) { assert(!(a->f & AF_LOCK)); a->f |= AF_LOCK; } |
| 935 | |
| 936 | /* --- @a_unlock@ --- * |
| 937 | * |
| 938 | * Arguments: @admin *a@ = pointer to an admin block |
| 939 | * |
| 940 | * Returns: --- |
| 941 | * |
| 942 | * Use: Unlocks an admin block, allowing its destruction. This is |
| 943 | * also the second half of @a_destroy@. |
| 944 | */ |
| 945 | |
| 946 | static void a_unlock(admin *a) |
| 947 | { |
| 948 | assert(a->f & AF_LOCK); |
| 949 | if (!(a->f & AF_DEAD)) { |
| 950 | a->f &= ~AF_LOCK; |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | T( trace(T_ADMIN, "admin: completing destruction of connection %u", |
| 955 | a->seq); ) |
| 956 | |
| 957 | selbuf_destroy(&a->b); |
| 958 | if (a->pname) { |
| 959 | xfree(a->pname); |
| 960 | xfree(a->paddr); |
| 961 | bres_abort(&a->r); |
| 962 | sel_rmtimer(&a->t); |
| 963 | } |
| 964 | if (a->b.reader.fd != a->w.fd) |
| 965 | close(a->b.reader.fd); |
| 966 | close(a->w.fd); |
| 967 | |
| 968 | if (a_stdin == a) |
| 969 | a_stdin = 0; |
| 970 | if (a->next) |
| 971 | a->next->prev = a->prev; |
| 972 | if (a->prev) |
| 973 | a->prev->next = a->next; |
| 974 | else |
| 975 | admins = a->next; |
| 976 | DESTROY(a); |
| 977 | } |
| 978 | |
| 979 | /* --- @a_destroy@ --- * |
| 980 | * |
| 981 | * Arguments: @admin *a@ = pointer to an admin block |
| 982 | * |
| 983 | * Returns: --- |
| 984 | * |
| 985 | * Use: Destroys an admin block. This requires a certain amount of |
| 986 | * care. |
| 987 | */ |
| 988 | |
| 989 | static void a_destroy(admin *a) |
| 990 | { |
| 991 | /* --- Don't multiply destroy admin blocks --- */ |
| 992 | |
| 993 | if (a->f & AF_DEAD) |
| 994 | return; |
| 995 | |
| 996 | /* --- Make sure nobody expects it to work --- */ |
| 997 | |
| 998 | a->f |= AF_DEAD; |
| 999 | T( trace(T_ADMIN, "admin: destroying connection %u", a->seq); ) |
| 1000 | |
| 1001 | /* --- Free the output buffers --- */ |
| 1002 | |
| 1003 | if (a->o_head) { |
| 1004 | obuf *o, *oo; |
| 1005 | sel_rmfile(&a->w); |
| 1006 | for (o = a->o_head; o; o = oo) { |
| 1007 | oo = o->next; |
| 1008 | xfree(o); |
| 1009 | } |
| 1010 | a->o_head = 0; |
| 1011 | } |
| 1012 | |
| 1013 | /* --- If the block is locked, that's all we can manage --- */ |
| 1014 | |
| 1015 | if (a->f & AF_LOCK) { |
| 1016 | T( trace(T_ADMIN, "admin: deferring destruction..."); ) |
| 1017 | return; |
| 1018 | } |
| 1019 | a->f |= AF_LOCK; |
| 1020 | a_unlock(a); |
| 1021 | } |
| 1022 | |
| 1023 | /* --- @a_line@ --- * |
| 1024 | * |
| 1025 | * Arguments: @char *p@ = pointer to the line read |
| 1026 | * @size_t len@ = length of the line |
| 1027 | * @void *vp@ = pointer to my admin block |
| 1028 | * |
| 1029 | * Returns: --- |
| 1030 | * |
| 1031 | * Use: Handles a line of input. |
| 1032 | */ |
| 1033 | |
| 1034 | static void a_line(char *p, size_t len, void *vp) |
| 1035 | { |
| 1036 | admin *a = vp; |
| 1037 | const acmd *c; |
| 1038 | char *av[16]; |
| 1039 | size_t ac; |
| 1040 | |
| 1041 | TIMER; |
| 1042 | if (a->f & AF_DEAD) |
| 1043 | return; |
| 1044 | if (!p) { |
| 1045 | a_destroy(a); |
| 1046 | return; |
| 1047 | } |
| 1048 | ac = str_qsplit(p, av, 16, 0, STRF_QUOTE); |
| 1049 | if (!ac) |
| 1050 | return; |
| 1051 | for (c = acmdtab; c->name; c++) { |
| 1052 | if (mystrieq(av[0], c->name)) { |
| 1053 | ac--; |
| 1054 | if (c->argmin > ac || ac > c->argmax) |
| 1055 | a_fail(a, "bad-syntax -- %s", c->help); |
| 1056 | else { |
| 1057 | a_lock(a); |
| 1058 | c->func(a, ac, av + 1); |
| 1059 | a_unlock(a); |
| 1060 | } |
| 1061 | return; |
| 1062 | } |
| 1063 | } |
| 1064 | a_fail(a, "unknown-command %s", av[0]); |
| 1065 | } |
| 1066 | |
| 1067 | /* --- @a_create@ --- * |
| 1068 | * |
| 1069 | * Arguments: @int fd_in, fd_out@ = file descriptors to use |
| 1070 | * @unsigned f@ = initial flags to set |
| 1071 | * |
| 1072 | * Returns: --- |
| 1073 | * |
| 1074 | * Use: Creates a new admin connection. |
| 1075 | */ |
| 1076 | |
| 1077 | void a_create(int fd_in, int fd_out, unsigned f) |
| 1078 | { |
| 1079 | admin *a = CREATE(admin); |
| 1080 | |
| 1081 | T( static unsigned seq = 0; |
| 1082 | a->seq = seq++; ) |
| 1083 | T( trace(T_ADMIN, "admin: accepted connection %u", a->seq); ) |
| 1084 | a->pname = 0; |
| 1085 | a->f = f; |
| 1086 | if (fd_in == STDIN_FILENO) |
| 1087 | a_stdin = a; |
| 1088 | fdflags(fd_in, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
| 1089 | if (fd_out != fd_in) |
| 1090 | fdflags(fd_out, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
| 1091 | selbuf_init(&a->b, &sel, fd_in, a_line, a); |
| 1092 | sel_initfile(&sel, &a->w, fd_out, SEL_WRITE, a_flush, a); |
| 1093 | a->o_head = 0; |
| 1094 | a->o_tail = 0; |
| 1095 | a->next = admins; |
| 1096 | a->prev = 0; |
| 1097 | if (admins) |
| 1098 | admins->prev = a; |
| 1099 | admins = a; |
| 1100 | } |
| 1101 | |
| 1102 | /* --- @a_accept@ --- * |
| 1103 | * |
| 1104 | * Arguments: @int fd@ = file descriptor to accept |
| 1105 | * @unsigned mode@ = what to do |
| 1106 | * @void *v@ = uninteresting pointer |
| 1107 | * |
| 1108 | * Returns: --- |
| 1109 | * |
| 1110 | * Use: Accepts a new admin connection. |
| 1111 | */ |
| 1112 | |
| 1113 | static void a_accept(int fd, unsigned mode, void *v) |
| 1114 | { |
| 1115 | int nfd; |
| 1116 | struct sockaddr_un sun; |
| 1117 | size_t sz = sizeof(sun); |
| 1118 | |
| 1119 | if ((nfd = accept(fd, (struct sockaddr *)&sun, &sz)) < 0) { |
| 1120 | if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK && |
| 1121 | errno != ECONNABORTED && errno != EPROTO) |
| 1122 | a_warn("ADMIN accept-error -- %s", strerror(errno)); |
| 1123 | return; |
| 1124 | } |
| 1125 | a_create(nfd, nfd, 0); |
| 1126 | } |
| 1127 | |
| 1128 | /* --- @a_daemon@ --- * |
| 1129 | * |
| 1130 | * Arguments: --- |
| 1131 | * |
| 1132 | * Returns: --- |
| 1133 | * |
| 1134 | * Use: Informs the admin module that it's a daemon. |
| 1135 | */ |
| 1136 | |
| 1137 | void a_daemon(void) |
| 1138 | { |
| 1139 | flags |= F_DAEMON; |
| 1140 | } |
| 1141 | |
| 1142 | /* --- @a_init@ --- * |
| 1143 | * |
| 1144 | * Arguments: @const char *name@ = socket name to create |
| 1145 | * |
| 1146 | * Returns: --- |
| 1147 | * |
| 1148 | * Use: Creates the admin listening socket. |
| 1149 | */ |
| 1150 | |
| 1151 | void a_init(const char *name) |
| 1152 | { |
| 1153 | int fd; |
| 1154 | int n = 5; |
| 1155 | struct sockaddr_un sun; |
| 1156 | struct sigaction sa; |
| 1157 | size_t sz; |
| 1158 | |
| 1159 | /* --- Set up the socket address --- */ |
| 1160 | |
| 1161 | sz = strlen(name) + 1; |
| 1162 | if (sz > sizeof(sun.sun_path)) |
| 1163 | die(EXIT_FAILURE, "socket name `%s' too long", name); |
| 1164 | BURN(sun); |
| 1165 | sun.sun_family = AF_UNIX; |
| 1166 | memcpy(sun.sun_path, name, sz); |
| 1167 | sz += offsetof(struct sockaddr_un, sun_path); |
| 1168 | |
| 1169 | /* --- Attempt to bind to the socket --- */ |
| 1170 | |
| 1171 | umask(0077); |
| 1172 | again: |
| 1173 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
| 1174 | die(EXIT_FAILURE, "couldn't create socket: %s", strerror(errno)); |
| 1175 | if (bind(fd, (struct sockaddr *)&sun, sz) < 0) { |
| 1176 | struct stat st; |
| 1177 | int e = errno; |
| 1178 | if (errno != EADDRINUSE) { |
| 1179 | die(EXIT_FAILURE, "couldn't bind to address `%s': %s", |
| 1180 | sun.sun_path, strerror(e)); |
| 1181 | } |
| 1182 | if (!n) |
| 1183 | die(EXIT_FAILURE, "too many retries; giving up"); |
| 1184 | n--; |
| 1185 | if (!connect(fd, (struct sockaddr *)&sun, sz)) { |
| 1186 | die(EXIT_FAILURE, "server already listening on admin socket `%s'", |
| 1187 | sun.sun_path); |
| 1188 | } |
| 1189 | if (errno != ECONNREFUSED) |
| 1190 | die(EXIT_FAILURE, "couldn't bind to address: %s", strerror(e)); |
| 1191 | if (stat(sun.sun_path, &st)) { |
| 1192 | die(EXIT_FAILURE, "couldn't stat `%s': %s", |
| 1193 | sun.sun_path, strerror(errno)); |
| 1194 | } |
| 1195 | if (!S_ISSOCK(st.st_mode)) |
| 1196 | die(EXIT_FAILURE, "object `%s' isn't a socket", sun.sun_path); |
| 1197 | T( trace(T_ADMIN, "admin: stale socket found; removing it"); ) |
| 1198 | unlink(sun.sun_path); |
| 1199 | close(fd); |
| 1200 | goto again; |
| 1201 | } |
| 1202 | chmod(sun.sun_path, 0600); |
| 1203 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
| 1204 | if (listen(fd, 5)) |
| 1205 | die(EXIT_FAILURE, "couldn't listen on socket: %s", strerror(errno)); |
| 1206 | |
| 1207 | /* --- Listen to the socket --- */ |
| 1208 | |
| 1209 | sel_initfile(&sel, &sock, fd, SEL_READ, a_accept, 0); |
| 1210 | sel_addfile(&sock); |
| 1211 | sockname = name; |
| 1212 | bres_init(&sel); |
| 1213 | T( trace_custom(a_trace, 0); |
| 1214 | trace(T_ADMIN, "admin: enabled custom tracing"); ) |
| 1215 | flags |= F_INIT; |
| 1216 | |
| 1217 | /* --- Set up signal handlers --- */ |
| 1218 | |
| 1219 | sig_add(&s_term, SIGTERM, a_sigdie, 0); |
| 1220 | sig_add(&s_hup, SIGHUP, a_sighup, 0); |
| 1221 | signal(SIGPIPE, SIG_IGN); |
| 1222 | sigaction(SIGINT, 0, &sa); |
| 1223 | if (sa.sa_handler != SIG_IGN) |
| 1224 | sig_add(&s_int, SIGINT, a_sigdie, 0); |
| 1225 | } |
| 1226 | |
| 1227 | /*----- That's all, folks -------------------------------------------------*/ |