| 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 | { 's', T_KEYSET, "symmetric keyset management" }, |
| 42 | { 'x', T_KEYEXCH, "key exchange" }, |
| 43 | { 'm', T_KEYMGMT, "key management" }, |
| 44 | { 'l', T_CHAL, "challenge management" }, |
| 45 | { 'p', T_PACKET, "packet contents" }, |
| 46 | { 'c', T_CRYPTO, "crypto details" }, |
| 47 | { 'A', T_ALL, "all of the above" }, |
| 48 | { 0, 0, 0 } |
| 49 | }; |
| 50 | |
| 51 | unsigned tr_flags = 0; |
| 52 | #endif |
| 53 | |
| 54 | static const trace_opt w_opts[] = { |
| 55 | { 't', AF_TRACE, "trace messages" }, |
| 56 | { 'n', AF_NOTE, "asynchronous notifications" }, |
| 57 | { 'w', AF_WARN, "warnings" }, |
| 58 | { 'A', AF_ALLMSGS, "all of the above" }, |
| 59 | { 0, 0, 0 } |
| 60 | }; |
| 61 | |
| 62 | /*----- Static variables --------------------------------------------------*/ |
| 63 | |
| 64 | static admin *admins; |
| 65 | static sel_file sock; |
| 66 | static const char *sockname; |
| 67 | static unsigned flags = 0; |
| 68 | static admin *a_stdin = 0; |
| 69 | static sig s_term, s_int, s_hup; |
| 70 | |
| 71 | #define F_DAEMON 1u |
| 72 | #define F_INIT 2u |
| 73 | |
| 74 | #define T_RESOLVE SEC(30) |
| 75 | #define T_PING SEC(5) |
| 76 | |
| 77 | static void a_destroy(admin */*a*/); |
| 78 | static void a_lock(admin */*a*/); |
| 79 | static void a_unlock(admin */*a*/); |
| 80 | |
| 81 | #define BOOL(x) ((x) ? "t" : "nil") |
| 82 | |
| 83 | /*----- Output functions --------------------------------------------------*/ |
| 84 | |
| 85 | /* --- @trywrite@ --- * |
| 86 | * |
| 87 | * Arguments: @admin *a@ = pointer to an admin block |
| 88 | * @const char *p@ = pointer to buffer to write |
| 89 | * @size_t sz@ = size of data to write |
| 90 | * |
| 91 | * Returns: The number of bytes written, or less than zero on error. |
| 92 | * |
| 93 | * Use: Attempts to write data to a client. |
| 94 | */ |
| 95 | |
| 96 | static ssize_t trywrite(admin *a, const char *p, size_t sz) |
| 97 | { |
| 98 | ssize_t n, done = 0; |
| 99 | |
| 100 | again: |
| 101 | if (!sz) |
| 102 | return (done); |
| 103 | n = write(a->w.fd, p, sz); |
| 104 | if (n > 0) { |
| 105 | done += n; |
| 106 | p += n; |
| 107 | sz -= n; |
| 108 | goto again; |
| 109 | } |
| 110 | if (n < 0) { |
| 111 | if (errno == EINTR) |
| 112 | goto again; |
| 113 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 114 | a_destroy(a); |
| 115 | a_warn("ADMIN client-read-error -- %s", strerror(errno)); |
| 116 | return (-1); |
| 117 | } |
| 118 | } |
| 119 | return (done); |
| 120 | } |
| 121 | |
| 122 | /* --- @doqueue@ -- * |
| 123 | * |
| 124 | * Arguments: @oqueue *q@ = pointer to output queue |
| 125 | * @const char *p@ = pointer to buffer to write |
| 126 | * @size_t sz@ = size of buffer |
| 127 | * |
| 128 | * Returns: Nonzero if the queue was previously empty. |
| 129 | * |
| 130 | * Use: Queues data to be written later. |
| 131 | */ |
| 132 | |
| 133 | static int doqueue(oqueue *q, const char *p, size_t sz) |
| 134 | { |
| 135 | obuf *o; |
| 136 | int rc = 0; |
| 137 | size_t n; |
| 138 | |
| 139 | o = q->tl; |
| 140 | if (!o) |
| 141 | rc = 1; |
| 142 | else if (o->p_in < o->buf + OBUFSZ) |
| 143 | goto noalloc; |
| 144 | |
| 145 | do { |
| 146 | o = xmalloc(sizeof(obuf)); |
| 147 | o->next = 0; |
| 148 | o->p_in = o->p_out = o->buf; |
| 149 | if (q->tl) |
| 150 | q->tl->next = o; |
| 151 | else |
| 152 | q->hd = o; |
| 153 | q->tl = o; |
| 154 | |
| 155 | noalloc: |
| 156 | n = o->buf + OBUFSZ - o->p_in; |
| 157 | if (n > sz) |
| 158 | n = sz; |
| 159 | memcpy(o->p_in, p, n); |
| 160 | o->p_in += n; |
| 161 | p += n; |
| 162 | sz -= n; |
| 163 | } while (sz); |
| 164 | |
| 165 | return (rc); |
| 166 | } |
| 167 | |
| 168 | /* --- @dosend@ --- * |
| 169 | * |
| 170 | * Arguemnts: @admin *a@ = pointer to an admin block |
| 171 | * @const char *p@ = pointer to buffer to write |
| 172 | * @size_t sz@ = size of data to write |
| 173 | * |
| 174 | * Returns: --- |
| 175 | * |
| 176 | * Use: Sends data to an admin client. |
| 177 | */ |
| 178 | |
| 179 | static void dosend(admin *a, const char *p, size_t sz) |
| 180 | { |
| 181 | ssize_t n; |
| 182 | |
| 183 | if (a->f & AF_DEAD) |
| 184 | return; |
| 185 | if (!a->out.hd) { |
| 186 | if ((n = trywrite(a, p, sz)) < 0) |
| 187 | return; |
| 188 | p += n; |
| 189 | sz -= n; |
| 190 | if (!sz) |
| 191 | return; |
| 192 | } |
| 193 | if (doqueue(&a->out, p, sz)) |
| 194 | sel_addfile(&a->w); |
| 195 | } |
| 196 | |
| 197 | /* --- @a_flush@ --- * |
| 198 | * |
| 199 | * Arguments: @int fd@ = file descriptor |
| 200 | * @unsigned mode@ = what's happening |
| 201 | * @void *v@ = pointer to my admin block |
| 202 | * |
| 203 | * Returns: --- |
| 204 | * |
| 205 | * Use: Flushes buffers when a client is ready to read again. |
| 206 | */ |
| 207 | |
| 208 | static void a_flush(int fd, unsigned mode, void *v) |
| 209 | { |
| 210 | admin *a = v; |
| 211 | obuf *o, *oo; |
| 212 | ssize_t n; |
| 213 | |
| 214 | o = a->out.hd; |
| 215 | while (o) { |
| 216 | if ((n = trywrite(a, o->p_out, o->p_in - o->p_out)) < 0) |
| 217 | return; |
| 218 | o->p_out += n; |
| 219 | if (o->p_in < o->p_out) |
| 220 | break; |
| 221 | oo = o; |
| 222 | o = o->next; |
| 223 | xfree(oo); |
| 224 | } |
| 225 | a->out.hd = o; |
| 226 | if (!o) { |
| 227 | a->out.tl = 0; |
| 228 | sel_rmfile(&a->w); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /*----- Utility functions -------------------------------------------------*/ |
| 233 | |
| 234 | /* --- @a_write@, @a_vwrite@ --- * |
| 235 | * |
| 236 | * Arguments: @admin *a@ = admin connection to write to |
| 237 | * @const char *status@ = status code to report |
| 238 | * @const char *tag@ = tag string, or null |
| 239 | * @const char *fmt@ = pointer to format string |
| 240 | * @va_list ap@ = arguments in list |
| 241 | * @...@ = other arguments |
| 242 | * |
| 243 | * Returns: --- |
| 244 | * |
| 245 | * Use: Sends a message to an admin connection. |
| 246 | */ |
| 247 | |
| 248 | static void a_vwrite(admin *a, const char *status, const char *tag, |
| 249 | const char *fmt, va_list ap) |
| 250 | { |
| 251 | dstr d = DSTR_INIT; |
| 252 | |
| 253 | if (tag) dstr_puts(&d, "BG"); |
| 254 | dstr_puts(&d, status); |
| 255 | if (tag) { |
| 256 | dstr_putc(&d, ' '); |
| 257 | dstr_puts(&d, tag); |
| 258 | } |
| 259 | if (fmt) { |
| 260 | dstr_putc(&d, ' '); |
| 261 | dstr_vputf(&d, fmt, &ap); |
| 262 | } |
| 263 | dstr_putc(&d, '\n'); |
| 264 | dosend(a, d.buf, d.len); |
| 265 | dstr_destroy(&d); |
| 266 | } |
| 267 | |
| 268 | static void a_write(admin *a, const char *status, const char *tag, |
| 269 | const char *fmt, ...) |
| 270 | { |
| 271 | va_list ap; |
| 272 | va_start(ap, fmt); |
| 273 | a_vwrite(a, status, tag, fmt, ap); |
| 274 | va_end(ap); |
| 275 | } |
| 276 | |
| 277 | /* --- @a_ok@, @a_info@, @a_fail@ --- * |
| 278 | * |
| 279 | * Arguments: @admin *a@ = connection |
| 280 | * @const char *fmt@ = format string |
| 281 | * @...@ = other arguments |
| 282 | * |
| 283 | * Returns: --- |
| 284 | * |
| 285 | * Use: Convenience functions for @a_write@. |
| 286 | */ |
| 287 | |
| 288 | static void a_ok(admin *a) { a_write(a, "OK", 0, 0); } |
| 289 | |
| 290 | static void a_info(admin *a, const char *fmt, ...) |
| 291 | { |
| 292 | va_list ap; |
| 293 | va_start(ap, fmt); |
| 294 | a_vwrite(a, "INFO", 0, fmt, ap); |
| 295 | va_end(ap); |
| 296 | } |
| 297 | |
| 298 | static void a_fail(admin *a, const char *fmt, ...) |
| 299 | { |
| 300 | va_list ap; |
| 301 | va_start(ap, fmt); |
| 302 | a_vwrite(a, "FAIL", 0, fmt, ap); |
| 303 | va_end(ap); |
| 304 | } |
| 305 | |
| 306 | /* --- @a_alert@, @a_valert@, @a_rawalert@ --- * |
| 307 | * |
| 308 | * Arguments: @unsigned f_and, f_eq@ = filter for connections |
| 309 | * @const char *status@ = status string |
| 310 | * @const char *fmt@ = pointer to format string |
| 311 | * @const char *p@ = pointer to raw string |
| 312 | * @size_t sz@ = size of raw string |
| 313 | * @va_list ap@ = arguments in list |
| 314 | * @...@ = other arguments |
| 315 | * |
| 316 | * Returns: --- |
| 317 | * |
| 318 | * Use: Write a message to all admin connections matched by the given |
| 319 | * filter. |
| 320 | */ |
| 321 | |
| 322 | static void a_rawalert(unsigned f_and, unsigned f_eq, const char *status, |
| 323 | const char *p, size_t sz) |
| 324 | { |
| 325 | admin *a, *aa; |
| 326 | dstr d = DSTR_INIT; |
| 327 | |
| 328 | if (!(flags & F_INIT)) |
| 329 | return; |
| 330 | dstr_puts(&d, status); |
| 331 | if (p) { |
| 332 | dstr_putc(&d, ' '); |
| 333 | dstr_putm(&d, p, sz); |
| 334 | } |
| 335 | dstr_putc(&d, '\n'); |
| 336 | p = d.buf; |
| 337 | sz = d.len; |
| 338 | for (a = admins; a; a = aa) { |
| 339 | aa = a->next; |
| 340 | if ((a->f & f_and) == f_eq) |
| 341 | dosend(a, d.buf, d.len); |
| 342 | } |
| 343 | dstr_destroy(&d); |
| 344 | } |
| 345 | |
| 346 | static void a_valert(unsigned f_and, unsigned f_eq, const char *tag, |
| 347 | const char *fmt, va_list ap) |
| 348 | { |
| 349 | dstr d = DSTR_INIT; |
| 350 | |
| 351 | if (!(flags & F_INIT)) |
| 352 | return; |
| 353 | if (fmt) |
| 354 | dstr_vputf(&d, fmt, &ap); |
| 355 | a_rawalert(f_and, f_eq, tag, fmt ? d.buf : 0, fmt ? d.len : 0); |
| 356 | dstr_destroy(&d); |
| 357 | } |
| 358 | |
| 359 | #if 0 /*unused*/ |
| 360 | static void a_alert(unsigned f_and, unsigned f_eq, const char *tag, |
| 361 | const char *fmt, ...) |
| 362 | { |
| 363 | va_list ap; |
| 364 | va_start(ap, fmt); |
| 365 | a_valert(f_and, f_eq, tag, fmt, ap); |
| 366 | va_end(ap); |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | /* --- @a_warn@ --- * |
| 371 | * |
| 372 | * Arguments: @const char *fmt@ = pointer to format string |
| 373 | * @...@ = other arguments |
| 374 | * |
| 375 | * Returns: --- |
| 376 | * |
| 377 | * Use: Informs all admin connections of a warning. |
| 378 | */ |
| 379 | |
| 380 | void a_warn(const char *fmt, ...) |
| 381 | { |
| 382 | va_list ap; |
| 383 | |
| 384 | va_start(ap, fmt); |
| 385 | if (flags & F_INIT) |
| 386 | a_valert(0, 0, "WARN", fmt, ap); |
| 387 | else { |
| 388 | fprintf(stderr, "%s: ", QUIS); |
| 389 | vfprintf(stderr, fmt, ap); |
| 390 | fputc('\n', stderr); |
| 391 | } |
| 392 | va_end(ap); |
| 393 | } |
| 394 | |
| 395 | /* --- @a_trace@ --- * |
| 396 | * |
| 397 | * Arguments: @const char *p@ = pointer to a buffer |
| 398 | * @size_t sz@ = size of the buffer |
| 399 | * @void *v@ = uninteresting pointer |
| 400 | * |
| 401 | * Returns: --- |
| 402 | * |
| 403 | * Use: Custom trace output handler. Sends trace messages to |
| 404 | * interested admin connections. |
| 405 | */ |
| 406 | |
| 407 | #ifndef NTRACE |
| 408 | static void a_trace(const char *p, size_t sz, void *v) |
| 409 | { a_rawalert(AF_TRACE, AF_TRACE, "TRACE", p, sz); } |
| 410 | #endif |
| 411 | |
| 412 | /* --- @a_notify@ --- * |
| 413 | * |
| 414 | * Arguments: @const char *fmt@ = pointer to format string |
| 415 | * @...@ = other arguments |
| 416 | * |
| 417 | * Returns: --- |
| 418 | * |
| 419 | * Use: Sends a notification to interested admin connections. |
| 420 | */ |
| 421 | |
| 422 | void a_notify(const char *fmt, ...) |
| 423 | { |
| 424 | va_list ap; |
| 425 | |
| 426 | va_start(ap, fmt); |
| 427 | a_valert(AF_NOTE, AF_NOTE, "NOTE", fmt, ap); |
| 428 | va_end(ap); |
| 429 | } |
| 430 | |
| 431 | /* --- @a_quit@ --- * |
| 432 | * |
| 433 | * Arguments: --- |
| 434 | * |
| 435 | * Returns: --- |
| 436 | * |
| 437 | * Use: Shuts things down nicely. |
| 438 | */ |
| 439 | |
| 440 | void a_quit(void) |
| 441 | { |
| 442 | peer *p; |
| 443 | |
| 444 | close(sock.fd); |
| 445 | unlink(sockname); |
| 446 | while ((p = p_first()) != 0) |
| 447 | p_destroy(p); |
| 448 | exit(0); |
| 449 | } |
| 450 | |
| 451 | /* --- @a_sigdie@ --- * |
| 452 | * |
| 453 | * Arguments: @int sig@ = signal number |
| 454 | * @void *v@ = an uninteresting argument |
| 455 | * |
| 456 | * Returns: --- |
| 457 | * |
| 458 | * Use Shuts down on receipt of a fatal signal. |
| 459 | */ |
| 460 | |
| 461 | static void a_sigdie(int sig, void *v) |
| 462 | { |
| 463 | char *p; |
| 464 | char buf[20]; |
| 465 | |
| 466 | switch (sig) { |
| 467 | case SIGTERM: p = "SIGTERM"; break; |
| 468 | case SIGINT: p = "SIGINT"; break; |
| 469 | default: |
| 470 | sprintf(buf, "%i", sig); |
| 471 | p = buf; |
| 472 | break; |
| 473 | } |
| 474 | a_warn("SERVER quit signal %s", p); |
| 475 | a_quit(); |
| 476 | } |
| 477 | |
| 478 | /* --- @a_sighup@ --- * |
| 479 | * |
| 480 | * Arguments: @int sig@ = signal number |
| 481 | * @void *v@ = an uninteresting argument |
| 482 | * |
| 483 | * Returns: --- |
| 484 | * |
| 485 | * Use Logs a message about SIGHUP not being useful. |
| 486 | */ |
| 487 | |
| 488 | static void a_sighup(int sig, void *v) |
| 489 | { |
| 490 | a_warn("SERVER ignore signal SIGHUP"); |
| 491 | } |
| 492 | |
| 493 | /* --- @a_parsetime@ --- * |
| 494 | * |
| 495 | * Arguments; @const char *p@ = time string to parse |
| 496 | * |
| 497 | * Returns: Time in seconds, or @< 0@ on error. |
| 498 | */ |
| 499 | |
| 500 | static long a_parsetime(const char *p) |
| 501 | { |
| 502 | char *q; |
| 503 | long t = strtol(p, &q, 0); |
| 504 | |
| 505 | switch (*q) { |
| 506 | case 'd': t *= 24; |
| 507 | case 'h': t *= 60; |
| 508 | case 'm': t *= 60; |
| 509 | case 's': if (q[1] != 0) |
| 510 | default: t = -1; |
| 511 | case 0: break; |
| 512 | } |
| 513 | return (t); |
| 514 | } |
| 515 | |
| 516 | /* --- @a_findpeer@ --- * |
| 517 | * |
| 518 | * Arguments: @admin *a@ = admin connection |
| 519 | * @const char *pn@ = peer name |
| 520 | * |
| 521 | * Returns: The peer, or null if not there. |
| 522 | * |
| 523 | * Use: Finds a peer, reporting an error if it failed. |
| 524 | */ |
| 525 | |
| 526 | static peer *a_findpeer(admin *a, const char *pn) |
| 527 | { |
| 528 | peer *p; |
| 529 | |
| 530 | if ((p = p_find(pn)) == 0) |
| 531 | a_fail(a, "unknown-peer %s", pn); |
| 532 | return (p); |
| 533 | } |
| 534 | |
| 535 | /*----- Backgrounded operations -------------------------------------------*/ |
| 536 | |
| 537 | #define BGTAG(bg) \ |
| 538 | (((admin_bgop *)(bg))->tag ? ((admin_bgop *)(bg))->tag : "<foreground>") |
| 539 | |
| 540 | /* --- @a_bgrelease@ --- * |
| 541 | * |
| 542 | * Arguments: @admin_bgop *bg@ = backgrounded operation |
| 543 | * |
| 544 | * Returns: --- |
| 545 | * |
| 546 | * Use: Removes a backgrounded operation from the queue, since |
| 547 | * (presumably) it's done. |
| 548 | */ |
| 549 | |
| 550 | static void a_bgrelease(admin_bgop *bg) |
| 551 | { |
| 552 | admin *a = bg->a; |
| 553 | |
| 554 | T( trace(T_ADMIN, "admin: release bgop %s", BGTAG(bg)); ) |
| 555 | if (bg->tag) xfree(bg->tag); |
| 556 | else selbuf_enable(&a->b); |
| 557 | if (bg->next) bg->next->prev = bg->prev; |
| 558 | if (bg->prev) bg->prev->next = bg->next; |
| 559 | else a->bg = bg->next; |
| 560 | xfree(bg); |
| 561 | if (a->f & AF_CLOSE) a_destroy(a); |
| 562 | a_unlock(a); |
| 563 | } |
| 564 | |
| 565 | /* --- @a_bgok@, @a_bginfo@, @a_bgfail@ --- * |
| 566 | * |
| 567 | * Arguments: @admin_bgop *bg@ = backgrounded operation |
| 568 | * @const char *fmt@ = format string |
| 569 | * @...@ = other arguments |
| 570 | * |
| 571 | * Returns: --- |
| 572 | * |
| 573 | * Use: Convenience functions for @a_write@. |
| 574 | */ |
| 575 | |
| 576 | static void a_bgok(admin_bgop *bg) |
| 577 | { a_write(bg->a, "OK", bg->tag, 0); } |
| 578 | |
| 579 | static void a_bginfo(admin_bgop *bg, const char *fmt, ...) |
| 580 | { |
| 581 | va_list ap; |
| 582 | va_start(ap, fmt); |
| 583 | a_vwrite(bg->a, "INFO", bg->tag, fmt, ap); |
| 584 | va_end(ap); |
| 585 | } |
| 586 | |
| 587 | static void a_bgfail(admin_bgop *bg, const char *fmt, ...) |
| 588 | { |
| 589 | va_list ap; |
| 590 | va_start(ap, fmt); |
| 591 | a_vwrite(bg->a, "FAIL", bg->tag, fmt, ap); |
| 592 | va_end(ap); |
| 593 | } |
| 594 | |
| 595 | /* --- @a_bgadd@ --- * |
| 596 | * |
| 597 | * Arguments: @admin *a@ = administration connection |
| 598 | * @admin_bgop *bg@ = pointer to background operation |
| 599 | * @const char *tag@ = background tag, or null for foreground |
| 600 | * @void (*cancel)(admin_bgop *)@ = cancel function |
| 601 | * |
| 602 | * Returns: --- |
| 603 | * |
| 604 | * Use: Links a background job into the list. |
| 605 | */ |
| 606 | |
| 607 | static void a_bgadd(admin *a, admin_bgop *bg, const char *tag, |
| 608 | void (*cancel)(admin_bgop *)) |
| 609 | { |
| 610 | if (tag) |
| 611 | bg->tag = xstrdup(tag); |
| 612 | else { |
| 613 | bg->tag = 0; |
| 614 | selbuf_disable(&a->b); |
| 615 | } |
| 616 | bg->a = a; |
| 617 | bg->cancel = cancel; |
| 618 | bg->next = a->bg; |
| 619 | bg->prev = 0; |
| 620 | if (a->bg) a->bg->prev = bg; |
| 621 | a->bg = bg; |
| 622 | a_lock(a); |
| 623 | T( trace(T_ADMIN, "admin: add bgop %s", BGTAG(bg)); ) |
| 624 | if (tag) a_write(a, "DETACH", tag, 0); |
| 625 | } |
| 626 | |
| 627 | /*----- Name resolution operations ----------------------------------------*/ |
| 628 | |
| 629 | /* --- @a_resolved@ --- * |
| 630 | * |
| 631 | * Arguments: @struct hostent *h@ = pointer to resolved hostname |
| 632 | * @void *v@ = pointer to resolver operation |
| 633 | * |
| 634 | * Returns: --- |
| 635 | * |
| 636 | * Use: Handles a completed name resolution. |
| 637 | */ |
| 638 | |
| 639 | static void a_resolved(struct hostent *h, void *v) |
| 640 | { |
| 641 | admin_resop *r = v; |
| 642 | |
| 643 | T( trace(T_ADMIN, "admin: resop %s resolved", BGTAG(r)); ) |
| 644 | TIMER; |
| 645 | if (!h) { |
| 646 | a_bgfail(&r->bg, "resolve-error %s", r->addr); |
| 647 | r->func(r, ARES_FAIL); |
| 648 | } else { |
| 649 | memcpy(&r->sa.sin.sin_addr, h->h_addr, sizeof(struct in_addr)); |
| 650 | r->func(r, ARES_OK); |
| 651 | } |
| 652 | sel_rmtimer(&r->t); |
| 653 | xfree(r->addr); |
| 654 | a_bgrelease(&r->bg); |
| 655 | } |
| 656 | |
| 657 | /* --- @a_restimer@ --- * |
| 658 | * |
| 659 | * Arguments: @struct timeval *tv@ = timer |
| 660 | * @void *v@ = pointer to resolver operation |
| 661 | * |
| 662 | * Returns: --- |
| 663 | * |
| 664 | * Use: Times out a resolver. |
| 665 | */ |
| 666 | |
| 667 | static void a_restimer(struct timeval *tv, void *v) |
| 668 | { |
| 669 | admin_resop *r = v; |
| 670 | |
| 671 | T( trace(T_ADMIN, "admin: resop %s timeout", BGTAG(r)); ) |
| 672 | a_bgfail(&r->bg, "resolver-timeout %s\n", r->addr); |
| 673 | r->func(r, ARES_FAIL); |
| 674 | bres_abort(&r->r); |
| 675 | xfree(r->addr); |
| 676 | a_bgrelease(&r->bg); |
| 677 | } |
| 678 | |
| 679 | /* --- @a_rescancel@ --- * |
| 680 | * |
| 681 | * Arguments: @admin_bgop *bg@ = background operation |
| 682 | * |
| 683 | * Returns: --- |
| 684 | * |
| 685 | * Use: Cancels an add operation. |
| 686 | */ |
| 687 | |
| 688 | static void a_rescancel(admin_bgop *bg) |
| 689 | { |
| 690 | admin_resop *r = (admin_resop *)bg; |
| 691 | |
| 692 | T( trace(T_ADMIN, "admin: cancel resop %s", BGTAG(r)); ) |
| 693 | r->func(r, ARES_FAIL); |
| 694 | sel_rmtimer(&r->t); |
| 695 | xfree(r->addr); |
| 696 | bres_abort(&r->r); |
| 697 | } |
| 698 | |
| 699 | /* --- @a_resolve@ --- * |
| 700 | * |
| 701 | * Arguments: @admin *a@ = administration connection |
| 702 | * @admin_resop *r@ = resolver operation to run |
| 703 | * @const char *tag@ = background operation tag |
| 704 | * @void (*func)(struct admin_resop *, int@ = handler function |
| 705 | * @unsigned ac@ = number of remaining arguments |
| 706 | * @char *av[]@ = pointer to remaining arguments |
| 707 | * |
| 708 | * Returns: --- |
| 709 | * |
| 710 | * Use: Cranks up a resolver job. |
| 711 | */ |
| 712 | |
| 713 | static void a_resolve(admin *a, admin_resop *r, const char *tag, |
| 714 | void (*func)(struct admin_resop *, int), |
| 715 | unsigned ac, char *av[]) |
| 716 | { |
| 717 | struct timeval tv; |
| 718 | unsigned long pt; |
| 719 | char *p; |
| 720 | int i = 0; |
| 721 | |
| 722 | /* --- Fill in the easy bits of address --- */ |
| 723 | |
| 724 | r->addr = 0; |
| 725 | r->func = func; |
| 726 | if (mystrieq(av[i], "inet")) i++; |
| 727 | if (ac - i != 2) { |
| 728 | a_fail(a, "bad-addr-syntax [inet] ADDRESS PORT"); |
| 729 | goto fail; |
| 730 | } |
| 731 | r->sa.sin.sin_family = AF_INET; |
| 732 | r->sasz = sizeof(r->sa.sin); |
| 733 | r->addr = xstrdup(av[i]); |
| 734 | pt = strtoul(av[i + 1], &p, 0); |
| 735 | if (*p) { |
| 736 | struct servent *s = getservbyname(av[i + 1], "udp"); |
| 737 | if (!s) { |
| 738 | a_fail(a, "unknown-service %s", av[i + 1]); |
| 739 | goto fail; |
| 740 | } |
| 741 | pt = ntohs(s->s_port); |
| 742 | } |
| 743 | if (pt == 0 || pt >= 65536) { |
| 744 | a_fail(a, "invalid-port %lu", pt); |
| 745 | goto fail; |
| 746 | } |
| 747 | r->sa.sin.sin_port = htons(pt); |
| 748 | |
| 749 | /* --- Report backgrounding --- * |
| 750 | * |
| 751 | * Do this for consistency of interface, even if we're going to get the |
| 752 | * answer straight away. |
| 753 | */ |
| 754 | |
| 755 | a_bgadd(a, &r->bg, tag, a_rescancel); |
| 756 | T( trace(T_ADMIN, "admin: %u, resop %s, hostname `%s'", |
| 757 | a->seq, BGTAG(r), r->addr); ) |
| 758 | |
| 759 | /* --- If the name is numeric, do it the easy way --- */ |
| 760 | |
| 761 | if (inet_aton(av[i], &r->sa.sin.sin_addr)) { |
| 762 | T( trace(T_ADMIN, "admin: resop %s done the easy way", BGTAG(r)); ) |
| 763 | func(r, ARES_OK); |
| 764 | xfree(r->addr); |
| 765 | a_bgrelease(&r->bg); |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | /* --- Store everything for later and crank up the resolver --- */ |
| 770 | |
| 771 | gettimeofday(&tv, 0); |
| 772 | tv.tv_sec += T_RESOLVE; |
| 773 | sel_addtimer(&sel, &r->t, &tv, a_restimer, r); |
| 774 | bres_byname(&r->r, r->addr, a_resolved, r); |
| 775 | return; |
| 776 | |
| 777 | fail: |
| 778 | func(r, ARES_FAIL); |
| 779 | if (r->addr) xfree(r->addr); |
| 780 | xfree(r); |
| 781 | } |
| 782 | |
| 783 | /*----- Adding peers ------------------------------------------------------*/ |
| 784 | |
| 785 | /* --- @a_doadd@ --- * |
| 786 | * |
| 787 | * Arguments: @admin_resop *r@ = resolver operation |
| 788 | * @int rc@ = how it worked |
| 789 | * |
| 790 | * Returns: --- |
| 791 | * |
| 792 | * Use: Handles a completed resolution. |
| 793 | */ |
| 794 | |
| 795 | static void a_doadd(admin_resop *r, int rc) |
| 796 | { |
| 797 | admin_addop *add = (admin_addop *)r; |
| 798 | |
| 799 | T( trace(T_ADMIN, "admin: done add op %s", BGTAG(add)); ) |
| 800 | |
| 801 | if (rc == ARES_OK) { |
| 802 | add->peer.sasz = add->r.sasz; |
| 803 | add->peer.sa = add->r.sa; |
| 804 | if (p_find(add->peer.name)) |
| 805 | a_bgfail(&add->r.bg, "peer-exists %s", add->peer.name); |
| 806 | else if (!p_create(&add->peer)) |
| 807 | a_bgfail(&add->r.bg, "peer-create-fail %s", add->peer.name); |
| 808 | else |
| 809 | a_bgok(&add->r.bg); |
| 810 | } |
| 811 | |
| 812 | xfree(add->peer.name); |
| 813 | } |
| 814 | |
| 815 | /* --- @acmd_add@ --- * |
| 816 | * |
| 817 | * Arguments: @admin *a@ = connection which requested the addition |
| 818 | * @unsigned ac@ = argument count |
| 819 | * @char *av[]@ = pointer to the argument list |
| 820 | * |
| 821 | * Returns: --- |
| 822 | * |
| 823 | * Use: Adds a new peer. |
| 824 | */ |
| 825 | |
| 826 | static void acmd_add(admin *a, unsigned ac, char *av[]) |
| 827 | { |
| 828 | unsigned i, j; |
| 829 | const char *tag = 0; |
| 830 | admin_addop *add = 0; |
| 831 | |
| 832 | /* --- Set stuff up --- */ |
| 833 | |
| 834 | add = xmalloc(sizeof(*add)); |
| 835 | add->peer.name = xstrdup(av[0]); |
| 836 | add->peer.t_ka = 0; |
| 837 | add->peer.tops = tun_default; |
| 838 | |
| 839 | /* --- Make sure someone's not got there already --- */ |
| 840 | |
| 841 | if (p_find(av[0])) { |
| 842 | a_fail(a, "peer-exists %s", av[0]); |
| 843 | goto fail; |
| 844 | } |
| 845 | |
| 846 | /* --- Parse options --- */ |
| 847 | |
| 848 | i = 1; |
| 849 | for (;;) { |
| 850 | if (!av[i]) |
| 851 | goto bad_syntax; |
| 852 | if (mystrieq(av[i], "-background")) { |
| 853 | if (!av[++i]) goto bad_syntax; |
| 854 | tag = av[i]; |
| 855 | } else if (mystrieq(av[i], "-tunnel")) { |
| 856 | if (!av[++i]) goto bad_syntax; |
| 857 | for (j = 0;; j++) { |
| 858 | if (!tunnels[j]) { |
| 859 | a_fail(a, "unknown-tunnel %s", av[i]); |
| 860 | goto fail; |
| 861 | } |
| 862 | if (mystrieq(av[i], tunnels[j]->name)) { |
| 863 | add->peer.tops = tunnels[j]; |
| 864 | break; |
| 865 | } |
| 866 | } |
| 867 | } else if (mystrieq(av[i], "-keepalive")) { |
| 868 | long t; |
| 869 | if (!av[++i]) goto bad_syntax; |
| 870 | if ((t = a_parsetime(av[i])) < 0) { |
| 871 | a_fail(a, "bad-time-spec %s", av[i]); |
| 872 | goto fail; |
| 873 | } |
| 874 | add->peer.t_ka = t; |
| 875 | } else if (mystrieq(av[i], "--")) { |
| 876 | i++; |
| 877 | break; |
| 878 | } else |
| 879 | break; |
| 880 | i++; |
| 881 | } |
| 882 | |
| 883 | /* --- Crank up the resolver --- */ |
| 884 | |
| 885 | a_resolve(a, &add->r, tag, a_doadd, ac - i, av + i); |
| 886 | return; |
| 887 | |
| 888 | /* --- Clearing up --- */ |
| 889 | |
| 890 | bad_syntax: |
| 891 | a_fail(a, "bad-syntax -- add PEER [OPTIONS] ADDR ..."); |
| 892 | fail: |
| 893 | xfree(add->peer.name); |
| 894 | xfree(add); |
| 895 | return; |
| 896 | } |
| 897 | |
| 898 | /*----- Ping --------------------------------------------------------------*/ |
| 899 | |
| 900 | /* --- @a_pingcancel@ --- * |
| 901 | * |
| 902 | * Arguments: @admin_bgop *bg@ = background operation block |
| 903 | * |
| 904 | * Returns: --- |
| 905 | * |
| 906 | * Use: Cancels a running ping. |
| 907 | */ |
| 908 | |
| 909 | static void a_pingcancel(admin_bgop *bg) |
| 910 | { |
| 911 | admin_pingop *pg = (admin_pingop *)bg; |
| 912 | T( trace(T_ADMIN, "admin: cancel ping op %s", BGTAG(pg)); ) |
| 913 | p_pingdone(&pg->ping, PING_NONOTIFY); |
| 914 | } |
| 915 | |
| 916 | /* --- @a_pong@ --- * |
| 917 | * |
| 918 | * Arguments: @int rc@ = return code |
| 919 | * @void *v@ = ping operation block |
| 920 | * |
| 921 | * Returns: --- |
| 922 | * |
| 923 | * Use: Collects what happened to a ping message. |
| 924 | */ |
| 925 | |
| 926 | static void a_pong(int rc, void *v) |
| 927 | { |
| 928 | admin_pingop *pg = v; |
| 929 | struct timeval tv; |
| 930 | double millis; |
| 931 | |
| 932 | switch (rc) { |
| 933 | case PING_OK: |
| 934 | gettimeofday(&tv, 0); |
| 935 | tv_sub(&tv, &tv, &pg->pingtime); |
| 936 | millis = (double)tv.tv_sec * 1000 + (double)tv.tv_usec/1000; |
| 937 | a_bginfo(&pg->bg, "ping-ok %.1f", millis); |
| 938 | a_bgok(&pg->bg); |
| 939 | break; |
| 940 | case PING_TIMEOUT: |
| 941 | a_bginfo(&pg->bg, "ping-timeout"); |
| 942 | a_bgok(&pg->bg); |
| 943 | break; |
| 944 | case PING_PEERDIED: |
| 945 | a_bginfo(&pg->bg, "ping-peer-died"); |
| 946 | a_bgok(&pg->bg); |
| 947 | break; |
| 948 | default: |
| 949 | abort(); |
| 950 | } |
| 951 | T( trace(T_ADMIN, "admin: ponged ping op %s", BGTAG(pg)); ) |
| 952 | a_bgrelease(&pg->bg); |
| 953 | } |
| 954 | |
| 955 | /* --- @acmd_ping@, @acmd_eping@ --- * |
| 956 | * |
| 957 | * Arguments: @admin *a@ = connection which requested the ping |
| 958 | * @unsigned ac@ = argument count |
| 959 | * @char *av[]@ = pointer to the argument list |
| 960 | * |
| 961 | * Returns: --- |
| 962 | * |
| 963 | * Use: Pings a peer. |
| 964 | */ |
| 965 | |
| 966 | static void a_ping(admin *a, unsigned ac, char *av[], |
| 967 | const char *cmd, unsigned msg) |
| 968 | { |
| 969 | long t = T_PING; |
| 970 | int i; |
| 971 | peer *p; |
| 972 | admin_pingop *pg = 0; |
| 973 | const char *tag = 0; |
| 974 | |
| 975 | i = 0; |
| 976 | for (;;) { |
| 977 | if (!av[i]) |
| 978 | goto bad_syntax; |
| 979 | if (mystrieq(av[i], "-background")) { |
| 980 | if (!av[++i]) goto bad_syntax; |
| 981 | tag = av[i]; |
| 982 | } else if (mystrieq(av[i], "-timeout")) { |
| 983 | if (!av[++i]) goto bad_syntax; |
| 984 | if ((t = a_parsetime(av[i])) < 0) { |
| 985 | a_fail(a, "bad-time-spec %s", av[i]); |
| 986 | return; |
| 987 | } |
| 988 | } else if (mystrieq(av[i], "--")) { |
| 989 | i++; |
| 990 | break; |
| 991 | } else |
| 992 | break; |
| 993 | i++; |
| 994 | } |
| 995 | |
| 996 | if (!av[i]) goto bad_syntax; |
| 997 | if ((p = a_findpeer(a, av[i])) == 0) |
| 998 | return; |
| 999 | pg = xmalloc(sizeof(*pg)); |
| 1000 | gettimeofday(&pg->pingtime, 0); |
| 1001 | a_bgadd(a, &pg->bg, tag, a_pingcancel); |
| 1002 | T( trace(T_ADMIN, "admin: ping op %s: %s to %s", |
| 1003 | BGTAG(pg), cmd, p_name(p)); ) |
| 1004 | if (p_pingsend(p, &pg->ping, msg, t, a_pong, pg)) { |
| 1005 | a_bgfail(&pg->bg, "ping-send-failed"); |
| 1006 | a_bgrelease(&pg->bg); |
| 1007 | } |
| 1008 | return; |
| 1009 | |
| 1010 | bad_syntax: |
| 1011 | a_fail(a, "bad-syntax -- %s [OPTIONS] PEER", cmd); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | static void acmd_ping(admin *a, unsigned ac, char *av[]) |
| 1016 | { a_ping(a, ac, av, "ping", MISC_PING); } |
| 1017 | static void acmd_eping(admin *a, unsigned ac, char *av[]) |
| 1018 | { a_ping(a, ac, av, "eping", MISC_EPING); } |
| 1019 | |
| 1020 | /*----- Administration commands -------------------------------------------*/ |
| 1021 | |
| 1022 | /* --- Miscellaneous commands --- */ |
| 1023 | |
| 1024 | /* --- @traceish@ --- * |
| 1025 | * |
| 1026 | * Arguments: @admin *a@ = connection to complain on |
| 1027 | * @unsigned ac@ = number of arguments |
| 1028 | * @char *av[]@ = vector of arguments |
| 1029 | * @const char *what@ = what we're messing with |
| 1030 | * @const trace_opt *tt@ = options table |
| 1031 | * @unsigned *ff@ = where the flags are |
| 1032 | * |
| 1033 | * Returns: Nonzero if anything changed. |
| 1034 | * |
| 1035 | * Use: Guts of trace-ish commands like `trace' and `watch'. |
| 1036 | */ |
| 1037 | |
| 1038 | static int traceish(admin *a, unsigned ac, char *av[], |
| 1039 | const char *what, const trace_opt *tt, unsigned *ff) |
| 1040 | { |
| 1041 | int ch = 0; |
| 1042 | |
| 1043 | if (!ac || strcmp(av[0], "?") == 0) { |
| 1044 | const trace_opt *t; |
| 1045 | for (t = tt; t->ch; t++) { |
| 1046 | a_info(a, "%c%c %s", |
| 1047 | t->ch, (*ff & t->f) == t->f ? '+' : ' ', t->help); |
| 1048 | } |
| 1049 | } else { |
| 1050 | unsigned sense = 1; |
| 1051 | unsigned f = *ff; |
| 1052 | const trace_opt *t; |
| 1053 | char *p = av[0]; |
| 1054 | |
| 1055 | while (*p) { |
| 1056 | switch (*p) { |
| 1057 | case '+': sense = 1; break; |
| 1058 | case '-': sense = 0; break; |
| 1059 | default: |
| 1060 | for (t = tt; t->ch; t++) { |
| 1061 | if (t->ch == *p) { |
| 1062 | if (sense) f |= t->f; |
| 1063 | else f &= ~t->f; |
| 1064 | goto tropt_ok; |
| 1065 | } |
| 1066 | } |
| 1067 | a_fail(a, "bad-%s-option %c", what, *p); |
| 1068 | return (0); |
| 1069 | tropt_ok:; |
| 1070 | break; |
| 1071 | } |
| 1072 | p++; |
| 1073 | } |
| 1074 | *ff = f; |
| 1075 | ch = 1; |
| 1076 | } |
| 1077 | a_ok(a); |
| 1078 | return (ch); |
| 1079 | } |
| 1080 | |
| 1081 | #ifndef NTRACE |
| 1082 | |
| 1083 | static void acmd_trace(admin *a, unsigned ac, char *av[]) |
| 1084 | { |
| 1085 | if (traceish(a, ac, av, "trace", tr_opts, &tr_flags)) |
| 1086 | trace_level(tr_flags); |
| 1087 | } |
| 1088 | |
| 1089 | #endif |
| 1090 | |
| 1091 | static void acmd_watch(admin *a, unsigned ac, char *av[]) |
| 1092 | { |
| 1093 | traceish(a, ac, av, "watch", w_opts, &a->f); |
| 1094 | } |
| 1095 | |
| 1096 | static void quotify(dstr *d, const char *p) |
| 1097 | { |
| 1098 | if (d->len) |
| 1099 | dstr_putc(d, ' '); |
| 1100 | if (*p && !p[strcspn(p, "\"' \t\n\v")]) |
| 1101 | dstr_puts(d, p); |
| 1102 | else { |
| 1103 | dstr_putc(d, '\"'); |
| 1104 | while (*p) { |
| 1105 | if (*p == '\\' || *p == '\"') |
| 1106 | dstr_putc(d, '\\'); |
| 1107 | dstr_putc(d, *p++); |
| 1108 | } |
| 1109 | dstr_putc(d, '\"'); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | static void alertcmd(admin *a, unsigned f_and, unsigned f_eq, |
| 1114 | const char *tag, unsigned ac, char *av[]) |
| 1115 | { |
| 1116 | dstr d = DSTR_INIT; |
| 1117 | unsigned i; |
| 1118 | |
| 1119 | dstr_puts(&d, "USER"); |
| 1120 | for (i = 0; i < ac; i++) |
| 1121 | quotify(&d, av[i]); |
| 1122 | dstr_putz(&d); |
| 1123 | a_rawalert(f_and, f_eq, tag, d.buf, d.len); |
| 1124 | dstr_destroy(&d); |
| 1125 | a_ok(a); |
| 1126 | } |
| 1127 | |
| 1128 | static void acmd_notify(admin *a, unsigned ac, char *av[]) |
| 1129 | { alertcmd(a, AF_NOTE, AF_NOTE, "NOTE", ac, av); } |
| 1130 | static void acmd_warn(admin *a, unsigned ac, char *av[]) |
| 1131 | { alertcmd(a, AF_WARN, AF_WARN, "WARN", ac, av); } |
| 1132 | |
| 1133 | static void acmd_port(admin *a, unsigned ac, char *av[]) |
| 1134 | { |
| 1135 | a_info(a, "%u", p_port()); |
| 1136 | a_ok(a); |
| 1137 | } |
| 1138 | |
| 1139 | static void acmd_daemon(admin *a, unsigned ac, char *av[]) |
| 1140 | { |
| 1141 | if (flags & F_DAEMON) |
| 1142 | a_fail(a, "already-daemon"); |
| 1143 | else { |
| 1144 | a_notify("DAEMON"); |
| 1145 | if (a_stdin) |
| 1146 | a_destroy(a_stdin); |
| 1147 | if (u_daemon()) |
| 1148 | a_fail(a, "daemon-error -- %s", strerror(errno)); |
| 1149 | else { |
| 1150 | flags |= F_DAEMON; |
| 1151 | a_ok(a); |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | static void acmd_list(admin *a, unsigned ac, char *av[]) |
| 1157 | { |
| 1158 | peer *p; |
| 1159 | for (p = p_first(); p; p = p_next(p)) |
| 1160 | a_info(a, "%s", p_name(p)); |
| 1161 | a_ok(a); |
| 1162 | } |
| 1163 | |
| 1164 | static void acmd_ifname(admin *a, unsigned ac, char *av[]) |
| 1165 | { |
| 1166 | peer *p; |
| 1167 | |
| 1168 | if ((p = a_findpeer(a, av[0])) != 0) { |
| 1169 | a_info(a, "%s", p_ifname(p)); |
| 1170 | a_ok(a); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | static void acmd_getchal(admin *a, unsigned ac, char *av[]) |
| 1175 | { |
| 1176 | buf b; |
| 1177 | |
| 1178 | buf_init(&b, buf_i, PKBUFSZ); |
| 1179 | c_new(&b); |
| 1180 | a_info(a, "%s", b64_encode(BBASE(&b), BLEN(&b))); |
| 1181 | a_ok(a); |
| 1182 | } |
| 1183 | |
| 1184 | static void acmd_checkchal(admin *a, unsigned ac, char *av[]) |
| 1185 | { |
| 1186 | base64_ctx b64; |
| 1187 | buf b; |
| 1188 | dstr d = DSTR_INIT; |
| 1189 | |
| 1190 | base64_init(&b64); |
| 1191 | base64_decode(&b64, av[0], strlen(av[0]), &d); |
| 1192 | base64_decode(&b64, 0, 0, &d); |
| 1193 | buf_init(&b, d.buf, d.len); |
| 1194 | if (c_check(&b) || BBAD(&b) || BLEFT(&b)) |
| 1195 | a_fail(a, "invalid-challenge"); |
| 1196 | else |
| 1197 | a_ok(a); |
| 1198 | dstr_destroy(&d); |
| 1199 | } |
| 1200 | |
| 1201 | static void acmd_greet(admin *a, unsigned ac, char *av[]) |
| 1202 | { |
| 1203 | peer *p; |
| 1204 | base64_ctx b64; |
| 1205 | dstr d = DSTR_INIT; |
| 1206 | |
| 1207 | if ((p = a_findpeer(a, av[0])) != 0) { |
| 1208 | base64_init(&b64); |
| 1209 | base64_decode(&b64, av[1], strlen(av[1]), &d); |
| 1210 | base64_decode(&b64, 0, 0, &d); |
| 1211 | p_greet(p, d.buf, d.len); |
| 1212 | dstr_destroy(&d); |
| 1213 | a_ok(a); |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | static void acmd_addr(admin *a, unsigned ac, char *av[]) |
| 1218 | { |
| 1219 | peer *p; |
| 1220 | const addr *ad; |
| 1221 | |
| 1222 | if ((p = a_findpeer(a, av[0])) != 0) { |
| 1223 | ad = p_addr(p); |
| 1224 | assert(ad->sa.sa_family == AF_INET); |
| 1225 | a_info(a, "INET %s %u", |
| 1226 | inet_ntoa(ad->sin.sin_addr), |
| 1227 | (unsigned)ntohs(ad->sin.sin_port)); |
| 1228 | a_ok(a); |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | static void acmd_peerinfo(admin *a, unsigned ac, char *av[]) |
| 1233 | { |
| 1234 | peer *p; |
| 1235 | const peerspec *ps; |
| 1236 | |
| 1237 | if ((p = a_findpeer(a, av[0])) != 0) { |
| 1238 | ps = p_spec(p); |
| 1239 | a_info(a, "tunnel=%s", ps->tops->name); |
| 1240 | a_info(a, "keepalive=%lu", ps->t_ka); |
| 1241 | a_ok(a); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | static void acmd_servinfo(admin *a, unsigned ac, char *av[]) |
| 1246 | { |
| 1247 | a_info(a, "implementation=edgeware-tripe"); |
| 1248 | a_info(a, "version=%s", VERSION); |
| 1249 | a_info(a, "daemon=%s", BOOL(flags & F_DAEMON)); |
| 1250 | a_ok(a); |
| 1251 | } |
| 1252 | |
| 1253 | static void acmd_stats(admin *a, unsigned ac, char *av[]) |
| 1254 | { |
| 1255 | peer *p; |
| 1256 | stats *st; |
| 1257 | |
| 1258 | if ((p = a_findpeer(a, av[0])) == 0) |
| 1259 | return; |
| 1260 | |
| 1261 | st = p_stats(p); |
| 1262 | a_info(a, "start-time=%s", timestr(st->t_start)); |
| 1263 | a_info(a, "last-packet-time=%s", timestr(st->t_last)); |
| 1264 | a_info(a, "last-keyexch-time=%s", timestr(st->t_kx)); |
| 1265 | a_info(a, "packets-in=%lu bytes-in=%lu", st->n_in, st->sz_in); |
| 1266 | a_info(a, "packets-out=%lu bytes-out=%lu", |
| 1267 | st->n_out, st->sz_out); |
| 1268 | a_info(a, "keyexch-packets-in=%lu keyexch-bytes-in=%lu", |
| 1269 | st->n_kxin, st->sz_kxin); |
| 1270 | a_info(a, "keyexch-packets-out=%lu keyexch-bytes-out=%lu", |
| 1271 | st->n_kxout, st->sz_kxout); |
| 1272 | a_info(a, "ip-packets-in=%lu ip-bytes-in=%lu", |
| 1273 | st->n_ipin, st->sz_ipin); |
| 1274 | a_info(a, "ip-packets-out=%lu ip-bytes-out=%lu", |
| 1275 | st->n_ipout, st->sz_ipout); |
| 1276 | a_info(a, "rejected-packets=%lu", st->n_reject); |
| 1277 | a_ok(a); |
| 1278 | } |
| 1279 | |
| 1280 | static void acmd_kill(admin *a, unsigned ac, char *av[]) |
| 1281 | { |
| 1282 | peer *p; |
| 1283 | if ((p = a_findpeer(a, av[0])) != 0) { |
| 1284 | p_destroy(p); |
| 1285 | a_ok(a); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | static void acmd_forcekx(admin *a, unsigned ac, char *av[]) |
| 1290 | { |
| 1291 | peer *p; |
| 1292 | if ((p = a_findpeer(a, av[0])) != 0) { |
| 1293 | kx_start(&p->kx, 1); |
| 1294 | a_ok(a); |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | static void acmd_reload(admin *a, unsigned ac, char *av[]) |
| 1299 | { p_keyreload(); a_ok(a); } |
| 1300 | |
| 1301 | static void acmd_quit(admin *a, unsigned ac, char *av[]) |
| 1302 | { |
| 1303 | a_warn("SERVER quit admin-request"); |
| 1304 | a_ok(a); |
| 1305 | a_unlock(a); |
| 1306 | a_quit(); |
| 1307 | } |
| 1308 | |
| 1309 | static void acmd_version(admin *a, unsigned ac, char *av[]) |
| 1310 | { |
| 1311 | a_info(a, "%s %s", PACKAGE, VERSION); |
| 1312 | a_ok(a); |
| 1313 | } |
| 1314 | |
| 1315 | static void acmd_tunnels(admin *a, unsigned ac, char *av[]) |
| 1316 | { |
| 1317 | int i; |
| 1318 | for (i = 0; tunnels[i]; i++) |
| 1319 | a_info(a, "%s", tunnels[i]->name); |
| 1320 | a_ok(a); |
| 1321 | } |
| 1322 | |
| 1323 | /* --- The command table and help --- */ |
| 1324 | |
| 1325 | typedef struct acmd { |
| 1326 | const char *name; |
| 1327 | const char *help; |
| 1328 | unsigned argmin, argmax; |
| 1329 | void (*func)(admin */*a*/, unsigned /*ac*/, char */*av*/[]); |
| 1330 | } acmd; |
| 1331 | |
| 1332 | static void acmd_help(admin */*a*/, unsigned /*ac*/, char */*av*/[]); |
| 1333 | |
| 1334 | static const acmd acmdtab[] = { |
| 1335 | { "add", "add PEER [OPTIONS] ADDR ...", |
| 1336 | 2, 0xffff, acmd_add }, |
| 1337 | { "addr", "addr PEER", 1, 1, acmd_addr }, |
| 1338 | { "checkchal", "checkchal CHAL", 1, 1, acmd_checkchal }, |
| 1339 | { "daemon", "daemon", 0, 0, acmd_daemon }, |
| 1340 | { "eping", "eping [OPTIONS] PEER", 1, 0xffff, acmd_eping }, |
| 1341 | { "forcekx", "forcekx PEER", 1, 1, acmd_forcekx }, |
| 1342 | { "getchal", "getchal", 0, 0, acmd_getchal }, |
| 1343 | { "greet", "greet PEER CHAL", 2, 2, acmd_greet }, |
| 1344 | { "help", "help", 0, 0, acmd_help }, |
| 1345 | { "ifname", "ifname PEER", 1, 1, acmd_ifname }, |
| 1346 | { "kill", "kill PEER", 1, 1, acmd_kill }, |
| 1347 | { "list", "list", 0, 0, acmd_list }, |
| 1348 | { "notify", "notify MESSAGE ...", 1, 0xffff, acmd_notify }, |
| 1349 | { "peerinfo", "peerinfo PEER", 1, 1, acmd_peerinfo }, |
| 1350 | { "ping", "ping [OPTIONS] PEER", 1, 0xffff, acmd_ping }, |
| 1351 | { "port", "port", 0, 0, acmd_port }, |
| 1352 | { "quit", "quit", 0, 0, acmd_quit }, |
| 1353 | { "reload", "reload", 0, 0, acmd_reload }, |
| 1354 | { "servinfo", "servinfo", 0, 0, acmd_servinfo }, |
| 1355 | { "stats", "stats PEER", 1, 1, acmd_stats }, |
| 1356 | #ifndef NTRACE |
| 1357 | { "trace", "trace [OPTIONS]", 0, 1, acmd_trace }, |
| 1358 | #endif |
| 1359 | { "tunnels", "tunnels", 0, 0, acmd_tunnels }, |
| 1360 | { "version", "version", 0, 0, acmd_version }, |
| 1361 | { "warn", "warn MESSAGE ...", 1, 0xffff, acmd_warn }, |
| 1362 | { "watch", "watch [OPTIONS]", 0, 1, acmd_watch }, |
| 1363 | { 0, 0, 0, 0, 0 } |
| 1364 | }; |
| 1365 | |
| 1366 | static void acmd_help(admin *a, unsigned ac, char *av[]) |
| 1367 | { |
| 1368 | const acmd *c; |
| 1369 | for (c = acmdtab; c->name; c++) |
| 1370 | a_info(a, "%s", c->help); |
| 1371 | a_ok(a); |
| 1372 | } |
| 1373 | |
| 1374 | /*----- Connection handling -----------------------------------------------*/ |
| 1375 | |
| 1376 | /* --- @a_lock@ --- * |
| 1377 | * |
| 1378 | * Arguments: @admin *a@ = pointer to an admin block |
| 1379 | * |
| 1380 | * Returns: --- |
| 1381 | * |
| 1382 | * Use: Locks an admin block so that it won't be destroyed |
| 1383 | * immediately. |
| 1384 | */ |
| 1385 | |
| 1386 | static void a_lock(admin *a) { a->ref++; } |
| 1387 | |
| 1388 | /* --- @a_dodestroy@ --- * |
| 1389 | * |
| 1390 | * Arguments: @admin *a@ = pointer to an admin block |
| 1391 | * |
| 1392 | * Returns: --- |
| 1393 | * |
| 1394 | * Use: Actually does the legwork of destroying an admin block. |
| 1395 | */ |
| 1396 | |
| 1397 | static void a_dodestroy(admin *a) |
| 1398 | { |
| 1399 | admin_bgop *bg, *bbg; |
| 1400 | |
| 1401 | T( trace(T_ADMIN, "admin: completing destruction of connection %u", |
| 1402 | a->seq); ) |
| 1403 | |
| 1404 | selbuf_destroy(&a->b); |
| 1405 | for (bg = a->bg; bg; bg = bbg) { |
| 1406 | bbg = bg->next; |
| 1407 | bg->cancel(bg); |
| 1408 | if (bg->tag) xfree(bg->tag); |
| 1409 | xfree(bg); |
| 1410 | } |
| 1411 | if (a->b.reader.fd != a->w.fd) close(a->b.reader.fd); |
| 1412 | close(a->w.fd); |
| 1413 | |
| 1414 | if (a_stdin == a) |
| 1415 | a_stdin = 0; |
| 1416 | if (a->next) |
| 1417 | a->next->prev = a->prev; |
| 1418 | if (a->prev) |
| 1419 | a->prev->next = a->next; |
| 1420 | else |
| 1421 | admins = a->next; |
| 1422 | DESTROY(a); |
| 1423 | } |
| 1424 | |
| 1425 | /* --- @a_unlock@ --- * |
| 1426 | * |
| 1427 | * Arguments: @admin *a@ = pointer to an admin block |
| 1428 | * |
| 1429 | * Returns: --- |
| 1430 | * |
| 1431 | * Use: Unlocks an admin block, allowing its destruction. This is |
| 1432 | * also the second half of @a_destroy@. |
| 1433 | */ |
| 1434 | |
| 1435 | static void a_unlock(admin *a) |
| 1436 | { |
| 1437 | assert(a->ref); |
| 1438 | if (!--a->ref && (a->f & AF_DEAD)) |
| 1439 | a_dodestroy(a); |
| 1440 | } |
| 1441 | |
| 1442 | /* --- @a_destroy@ --- * |
| 1443 | * |
| 1444 | * Arguments: @admin *a@ = pointer to an admin block |
| 1445 | * |
| 1446 | * Returns: --- |
| 1447 | * |
| 1448 | * Use: Destroys an admin block. This requires a certain amount of |
| 1449 | * care. |
| 1450 | */ |
| 1451 | |
| 1452 | static void freequeue(oqueue *q) |
| 1453 | { |
| 1454 | obuf *o, *oo; |
| 1455 | |
| 1456 | for (o = q->hd; o; o = oo) { |
| 1457 | oo = o->next; |
| 1458 | xfree(o); |
| 1459 | } |
| 1460 | q->hd = q->tl = 0; |
| 1461 | } |
| 1462 | |
| 1463 | static void a_destroy(admin *a) |
| 1464 | { |
| 1465 | /* --- Don't multiply destroy admin blocks --- */ |
| 1466 | |
| 1467 | if (a->f & AF_DEAD) |
| 1468 | return; |
| 1469 | |
| 1470 | /* --- Make sure nobody expects it to work --- */ |
| 1471 | |
| 1472 | a->f |= AF_DEAD; |
| 1473 | T( trace(T_ADMIN, "admin: destroying connection %u", a->seq); ) |
| 1474 | |
| 1475 | /* --- Free the output buffers --- */ |
| 1476 | |
| 1477 | if (a->out.hd) |
| 1478 | sel_rmfile(&a->w); |
| 1479 | freequeue(&a->out); |
| 1480 | |
| 1481 | /* --- If the block is locked, that's all we can manage --- */ |
| 1482 | |
| 1483 | if (!a->ref) |
| 1484 | a_dodestroy(a); |
| 1485 | T( else |
| 1486 | trace(T_ADMIN, "admin: deferring destruction..."); ) |
| 1487 | } |
| 1488 | |
| 1489 | /* --- @a_line@ --- * |
| 1490 | * |
| 1491 | * Arguments: @char *p@ = pointer to the line read |
| 1492 | * @size_t len@ = length of the line |
| 1493 | * @void *vp@ = pointer to my admin block |
| 1494 | * |
| 1495 | * Returns: --- |
| 1496 | * |
| 1497 | * Use: Handles a line of input. |
| 1498 | */ |
| 1499 | |
| 1500 | static void a_line(char *p, size_t len, void *vp) |
| 1501 | { |
| 1502 | admin *a = vp; |
| 1503 | const acmd *c; |
| 1504 | char *av[16]; |
| 1505 | size_t ac; |
| 1506 | |
| 1507 | TIMER; |
| 1508 | if (a->f & AF_DEAD) |
| 1509 | return; |
| 1510 | if (!p) { |
| 1511 | if (!a->bg) |
| 1512 | a_destroy(a); |
| 1513 | else { |
| 1514 | a->f |= AF_CLOSE; |
| 1515 | selbuf_disable(&a->b); |
| 1516 | } |
| 1517 | return; |
| 1518 | } |
| 1519 | ac = str_qsplit(p, av, 16, 0, STRF_QUOTE); |
| 1520 | if (!ac) |
| 1521 | return; |
| 1522 | for (c = acmdtab; c->name; c++) { |
| 1523 | if (mystrieq(av[0], c->name)) { |
| 1524 | ac--; |
| 1525 | if (c->argmin > ac || ac > c->argmax) |
| 1526 | a_fail(a, "bad-syntax -- %s", c->help); |
| 1527 | else { |
| 1528 | a_lock(a); |
| 1529 | c->func(a, ac, av + 1); |
| 1530 | a_unlock(a); |
| 1531 | } |
| 1532 | return; |
| 1533 | } |
| 1534 | } |
| 1535 | a_fail(a, "unknown-command %s", av[0]); |
| 1536 | } |
| 1537 | |
| 1538 | /* --- @a_create@ --- * |
| 1539 | * |
| 1540 | * Arguments: @int fd_in, fd_out@ = file descriptors to use |
| 1541 | * @unsigned f@ = initial flags to set |
| 1542 | * |
| 1543 | * Returns: --- |
| 1544 | * |
| 1545 | * Use: Creates a new admin connection. |
| 1546 | */ |
| 1547 | |
| 1548 | void a_create(int fd_in, int fd_out, unsigned f) |
| 1549 | { |
| 1550 | admin *a = CREATE(admin); |
| 1551 | |
| 1552 | T( static unsigned seq = 0; |
| 1553 | a->seq = seq++; ) |
| 1554 | T( trace(T_ADMIN, "admin: accepted connection %u", a->seq); ) |
| 1555 | a->bg = 0; |
| 1556 | a->f = f; |
| 1557 | if (fd_in == STDIN_FILENO) a_stdin = a; |
| 1558 | fdflags(fd_in, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
| 1559 | if (fd_out != fd_in) |
| 1560 | fdflags(fd_out, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
| 1561 | selbuf_init(&a->b, &sel, fd_in, a_line, a); |
| 1562 | sel_initfile(&sel, &a->w, fd_out, SEL_WRITE, a_flush, a); |
| 1563 | a->out.hd = a->out.tl = 0; |
| 1564 | a->next = admins; |
| 1565 | a->prev = 0; |
| 1566 | if (admins) admins->prev = a; |
| 1567 | admins = a; |
| 1568 | } |
| 1569 | |
| 1570 | /* --- @a_accept@ --- * |
| 1571 | * |
| 1572 | * Arguments: @int fd@ = file descriptor to accept |
| 1573 | * @unsigned mode@ = what to do |
| 1574 | * @void *v@ = uninteresting pointer |
| 1575 | * |
| 1576 | * Returns: --- |
| 1577 | * |
| 1578 | * Use: Accepts a new admin connection. |
| 1579 | */ |
| 1580 | |
| 1581 | static void a_accept(int fd, unsigned mode, void *v) |
| 1582 | { |
| 1583 | int nfd; |
| 1584 | struct sockaddr_un sun; |
| 1585 | size_t sz = sizeof(sun); |
| 1586 | |
| 1587 | if ((nfd = accept(fd, (struct sockaddr *)&sun, &sz)) < 0) { |
| 1588 | if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK && |
| 1589 | errno != ECONNABORTED && errno != EPROTO) |
| 1590 | a_warn("ADMIN accept-error -- %s", strerror(errno)); |
| 1591 | return; |
| 1592 | } |
| 1593 | a_create(nfd, nfd, 0); |
| 1594 | } |
| 1595 | |
| 1596 | /* --- @a_daemon@ --- * |
| 1597 | * |
| 1598 | * Arguments: --- |
| 1599 | * |
| 1600 | * Returns: --- |
| 1601 | * |
| 1602 | * Use: Informs the admin module that it's a daemon. |
| 1603 | */ |
| 1604 | |
| 1605 | void a_daemon(void) |
| 1606 | { |
| 1607 | flags |= F_DAEMON; |
| 1608 | } |
| 1609 | |
| 1610 | /* --- @a_init@ --- * |
| 1611 | * |
| 1612 | * Arguments: @const char *name@ = socket name to create |
| 1613 | * |
| 1614 | * Returns: --- |
| 1615 | * |
| 1616 | * Use: Creates the admin listening socket. |
| 1617 | */ |
| 1618 | |
| 1619 | void a_init(const char *name) |
| 1620 | { |
| 1621 | int fd; |
| 1622 | int n = 5; |
| 1623 | struct sockaddr_un sun; |
| 1624 | struct sigaction sa; |
| 1625 | size_t sz; |
| 1626 | |
| 1627 | /* --- Set up the socket address --- */ |
| 1628 | |
| 1629 | sz = strlen(name) + 1; |
| 1630 | if (sz > sizeof(sun.sun_path)) |
| 1631 | die(EXIT_FAILURE, "socket name `%s' too long", name); |
| 1632 | BURN(sun); |
| 1633 | sun.sun_family = AF_UNIX; |
| 1634 | memcpy(sun.sun_path, name, sz); |
| 1635 | sz += offsetof(struct sockaddr_un, sun_path); |
| 1636 | |
| 1637 | /* --- Attempt to bind to the socket --- */ |
| 1638 | |
| 1639 | umask(0077); |
| 1640 | again: |
| 1641 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
| 1642 | die(EXIT_FAILURE, "couldn't create socket: %s", strerror(errno)); |
| 1643 | if (bind(fd, (struct sockaddr *)&sun, sz) < 0) { |
| 1644 | struct stat st; |
| 1645 | int e = errno; |
| 1646 | if (errno != EADDRINUSE) { |
| 1647 | die(EXIT_FAILURE, "couldn't bind to address `%s': %s", |
| 1648 | sun.sun_path, strerror(e)); |
| 1649 | } |
| 1650 | if (!n) |
| 1651 | die(EXIT_FAILURE, "too many retries; giving up"); |
| 1652 | n--; |
| 1653 | if (!connect(fd, (struct sockaddr *)&sun, sz)) { |
| 1654 | die(EXIT_FAILURE, "server already listening on admin socket `%s'", |
| 1655 | sun.sun_path); |
| 1656 | } |
| 1657 | if (errno != ECONNREFUSED) |
| 1658 | die(EXIT_FAILURE, "couldn't bind to address: %s", strerror(e)); |
| 1659 | if (stat(sun.sun_path, &st)) { |
| 1660 | die(EXIT_FAILURE, "couldn't stat `%s': %s", |
| 1661 | sun.sun_path, strerror(errno)); |
| 1662 | } |
| 1663 | if (!S_ISSOCK(st.st_mode)) |
| 1664 | die(EXIT_FAILURE, "object `%s' isn't a socket", sun.sun_path); |
| 1665 | T( trace(T_ADMIN, "admin: stale socket found; removing it"); ) |
| 1666 | unlink(sun.sun_path); |
| 1667 | close(fd); |
| 1668 | goto again; |
| 1669 | } |
| 1670 | chmod(sun.sun_path, 0600); |
| 1671 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
| 1672 | if (listen(fd, 5)) |
| 1673 | die(EXIT_FAILURE, "couldn't listen on socket: %s", strerror(errno)); |
| 1674 | |
| 1675 | /* --- Listen to the socket --- */ |
| 1676 | |
| 1677 | sel_initfile(&sel, &sock, fd, SEL_READ, a_accept, 0); |
| 1678 | sel_addfile(&sock); |
| 1679 | sockname = name; |
| 1680 | bres_init(&sel); |
| 1681 | T( trace_custom(a_trace, 0); |
| 1682 | trace(T_ADMIN, "admin: enabled custom tracing"); ) |
| 1683 | flags |= F_INIT; |
| 1684 | |
| 1685 | /* --- Set up signal handlers --- */ |
| 1686 | |
| 1687 | sig_add(&s_term, SIGTERM, a_sigdie, 0); |
| 1688 | sig_add(&s_hup, SIGHUP, a_sighup, 0); |
| 1689 | signal(SIGPIPE, SIG_IGN); |
| 1690 | sigaction(SIGINT, 0, &sa); |
| 1691 | if (sa.sa_handler != SIG_IGN) |
| 1692 | sig_add(&s_int, SIGINT, a_sigdie, 0); |
| 1693 | } |
| 1694 | |
| 1695 | /*----- That's all, folks -------------------------------------------------*/ |