chiark / gitweb /
server/tests.at (AWAIT_KXDONE): Ignore the correct server messages.
[tripe] / server / admin.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
410c8acf 3 * Admin interface for configuration
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
e04c2d50 16 *
410c8acf 17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
410c8acf 27/*----- Header files ------------------------------------------------------*/
28
29#include "tripe.h"
30
31/*----- Global variables --------------------------------------------------*/
32
33#ifndef NTRACE
34
35const trace_opt tr_opts[] = {
36 { 't', T_TUNNEL, "tunnel events" },
37 { 'r', T_PEER, "peer events" },
38 { 'a', T_ADMIN, "admin interface" },
410c8acf 39 { 's', T_KEYSET, "symmetric keyset management" },
40 { 'x', T_KEYEXCH, "key exchange" },
41 { 'm', T_KEYMGMT, "key management" },
37941236 42 { 'l', T_CHAL, "challenge management" },
388e0319 43 { 'v', T_PRIVSEP, "privilege separation" },
37941236 44 { 'p', T_PACKET, "packet contents" },
45 { 'c', T_CRYPTO, "crypto details" },
410c8acf 46 { 'A', T_ALL, "all of the above" },
47 { 0, 0, 0 }
48};
49
50unsigned tr_flags = 0;
51#endif
52
3cdc3f3a 53static const trace_opt w_opts[] = {
8ed35e02 54#ifndef NTRACE
3cdc3f3a 55 { 't', AF_TRACE, "trace messages" },
8ed35e02 56#endif
3cdc3f3a 57 { 'n', AF_NOTE, "asynchronous notifications" },
58 { 'w', AF_WARN, "warnings" },
59 { 'A', AF_ALLMSGS, "all of the above" },
60 { 0, 0, 0 }
61};
62
410c8acf 63/*----- Static variables --------------------------------------------------*/
64
65static admin *admins;
c511e1f9 66static admin *a_dead;
410c8acf 67static sel_file sock;
68static const char *sockname;
be6a1b7a 69static sym_table a_svcs;
410c8acf 70static unsigned flags = 0;
71static admin *a_stdin = 0;
72static sig s_term, s_int, s_hup;
73
74#define F_DAEMON 1u
75#define F_INIT 2u
46dde080 76#define F_FOREGROUND 4u
410c8acf 77
78#define T_RESOLVE SEC(30)
0ba8de86 79#define T_PING SEC(5)
410c8acf 80
fd3cf232 81static void a_destroy(admin */*a*/);
fd3cf232 82
060ca767 83#define BOOL(x) ((x) ? "t" : "nil")
84
fd3cf232 85/*----- Output functions --------------------------------------------------*/
86
87/* --- @trywrite@ --- *
88 *
89 * Arguments: @admin *a@ = pointer to an admin block
90 * @const char *p@ = pointer to buffer to write
91 * @size_t sz@ = size of data to write
92 *
93 * Returns: The number of bytes written, or less than zero on error.
94 *
95 * Use: Attempts to write data to a client.
96 */
97
98static ssize_t trywrite(admin *a, const char *p, size_t sz)
99{
100 ssize_t n, done = 0;
101
102again:
103 if (!sz)
104 return (done);
105 n = write(a->w.fd, p, sz);
106 if (n > 0) {
107 done += n;
108 p += n;
109 sz -= n;
110 goto again;
111 }
112 if (n < 0) {
113 if (errno == EINTR)
114 goto again;
115 if (errno != EAGAIN && errno != EWOULDBLOCK) {
116 a_destroy(a);
f43df819 117 a_warn("ADMIN", "client-write-error", "?ERRNO", A_END);
fd3cf232 118 return (-1);
119 }
120 }
121 return (done);
122}
123
de014da6 124/* --- @doqueue@ -- *
fd3cf232 125 *
de014da6 126 * Arguments: @oqueue *q@ = pointer to output queue
fd3cf232 127 * @const char *p@ = pointer to buffer to write
de014da6 128 * @size_t sz@ = size of buffer
fd3cf232 129 *
de014da6 130 * Returns: Nonzero if the queue was previously empty.
fd3cf232 131 *
de014da6 132 * Use: Queues data to be written later.
fd3cf232 133 */
134
de014da6 135static int doqueue(oqueue *q, const char *p, size_t sz)
fd3cf232 136{
fd3cf232 137 obuf *o;
de014da6 138 int rc = 0;
139 size_t n;
fd3cf232 140
de014da6 141 o = q->tl;
fd3cf232 142 if (!o)
de014da6 143 rc = 1;
fd3cf232 144 else if (o->p_in < o->buf + OBUFSZ)
145 goto noalloc;
146
147 do {
148 o = xmalloc(sizeof(obuf));
149 o->next = 0;
150 o->p_in = o->p_out = o->buf;
de014da6 151 if (q->tl)
152 q->tl->next = o;
fd3cf232 153 else
de014da6 154 q->hd = o;
155 q->tl = o;
fd3cf232 156
157 noalloc:
158 n = o->buf + OBUFSZ - o->p_in;
159 if (n > sz)
160 n = sz;
161 memcpy(o->p_in, p, n);
162 o->p_in += n;
163 p += n;
164 sz -= n;
165 } while (sz);
de014da6 166
167 return (rc);
168}
169
170/* --- @dosend@ --- *
171 *
172 * Arguemnts: @admin *a@ = pointer to an admin block
173 * @const char *p@ = pointer to buffer to write
174 * @size_t sz@ = size of data to write
175 *
176 * Returns: ---
177 *
178 * Use: Sends data to an admin client.
179 */
180
181static void dosend(admin *a, const char *p, size_t sz)
182{
183 ssize_t n;
184
185 if (a->f & AF_DEAD)
186 return;
187 if (!a->out.hd) {
188 if ((n = trywrite(a, p, sz)) < 0)
189 return;
190 p += n;
191 sz -= n;
192 if (!sz)
193 return;
194 }
195 if (doqueue(&a->out, p, sz))
196 sel_addfile(&a->w);
fd3cf232 197}
198
199/* --- @a_flush@ --- *
200 *
201 * Arguments: @int fd@ = file descriptor
202 * @unsigned mode@ = what's happening
203 * @void *v@ = pointer to my admin block
204 *
205 * Returns: ---
206 *
207 * Use: Flushes buffers when a client is ready to read again.
208 */
209
210static void a_flush(int fd, unsigned mode, void *v)
211{
212 admin *a = v;
213 obuf *o, *oo;
214 ssize_t n;
215
de014da6 216 o = a->out.hd;
fd3cf232 217 while (o) {
218 if ((n = trywrite(a, o->p_out, o->p_in - o->p_out)) < 0)
219 return;
220 o->p_out += n;
221 if (o->p_in < o->p_out)
222 break;
223 oo = o;
224 o = o->next;
225 xfree(oo);
226 }
de014da6 227 a->out.hd = o;
fd3cf232 228 if (!o) {
de014da6 229 a->out.tl = 0;
fd3cf232 230 sel_rmfile(&a->w);
231 }
232}
233
410c8acf 234/*----- Utility functions -------------------------------------------------*/
235
f43df819
MW
236/* --- @a_vformat@ --- *
237 *
238 * Arguments: @dstr *d@ = where to leave the formatted message
239 * @const char *fmt@ = pointer to format string
240 * @va_list ap@ = arguments in list
241 *
242 * Returns: ---
243 *
7f73baaf
MW
244 * Use: Main message token formatting driver. The arguments are
245 * interleaved formatting tokens and their parameters, finally
246 * terminated by an entry @A_END@.
247 *
248 * Tokens recognized:
249 *
250 * * "*..." ... -- pretokenized @dstr_putf@-like string
251 *
252 * * "?ADDR" SOCKADDR -- a socket address, to be converted
253 *
254 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
255 *
256 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
257 *
258 * * "?PEER" PEER -- peer's name
259 *
260 * * "?ERRNO" ERRNO -- system error code
261 *
262 * * "[!]..." ... -- @dstr_putf@-like string as single token
f43df819
MW
263 */
264
7f73baaf 265void a_vformat(dstr *d, const char *fmt, va_list ap)
f43df819
MW
266{
267 dstr dd = DSTR_INIT;
268
269 while (fmt) {
270 if (*fmt == '*') {
6ebbf072 271 if (d->len) dstr_putc(d, ' ');
f43df819
MW
272 dstr_vputf(d, fmt + 1, &ap);
273 } else if (*fmt == '?') {
274 if (strcmp(fmt, "?ADDR") == 0) {
275 const addr *a = va_arg(ap, const addr *);
276 switch (a->sa.sa_family) {
277 case AF_INET:
0ed0735f
MW
278 u_quotify(d, "INET");
279 u_quotify(d, inet_ntoa(a->sin.sin_addr));
f43df819
MW
280 dstr_putf(d, " %u", (unsigned)ntohs(a->sin.sin_port));
281 break;
282 default:
283 abort();
284 }
285 } else if (strcmp(fmt, "?B64") == 0) {
286 const octet *p = va_arg(ap, const octet *);
287 size_t n = va_arg(ap, size_t);
288 base64_ctx b64;
289 dstr_putc(d, ' ');
290 base64_init(&b64);
291 b64.indent = "";
292 b64.maxline = 0;
293 base64_encode(&b64, p, n, d);
294 base64_encode(&b64, 0, 0, d);
295 while (d->len && d->buf[d->len - 1] == '=') d->len--;
3331936b
MW
296 } else if (strcmp(fmt, "?TOKENS") == 0) {
297 const char *const *av = va_arg(ap, const char *const *);
0ed0735f 298 while (*av) u_quotify(d, *av++);
f43df819 299 } else if (strcmp(fmt, "?PEER") == 0)
0ed0735f 300 u_quotify(d, p_name(va_arg(ap, peer *)));
f43df819
MW
301 else if (strcmp(fmt, "?ERRNO") == 0) {
302 dstr_putf(d, " E%d", errno);
0ed0735f 303 u_quotify(d, strerror(errno));
f43df819
MW
304 } else
305 abort();
306 } else {
307 if (*fmt == '!') fmt++;
308 DRESET(&dd);
309 dstr_vputf(&dd, fmt, &ap);
0ed0735f 310 u_quotify(d, dd.buf);
f43df819
MW
311 }
312 fmt = va_arg(ap, const char *);
313 }
314
315 dstr_destroy(&dd);
316}
317
f241e36c
MW
318/* --- @a_format@ --- *
319 *
320 * Arguments: @dstr *d@ = where to leave the formatted message
321 * @const char *fmt@ = pointer to format string
322 *
323 * Returns: ---
324 *
325 * Use: Writes a tokenized message into a string, for later
326 * presentation.
327 */
328
329void a_format(dstr *d, const char *fmt, ...)
330{
331 va_list ap;
332
333 va_start(ap, fmt);
334 a_vformat(d, fmt, ap);
335 va_end(ap);
336}
337
3cdc3f3a 338/* --- @a_write@, @a_vwrite@ --- *
410c8acf 339 *
340 * Arguments: @admin *a@ = admin connection to write to
de014da6 341 * @const char *status@ = status code to report
342 * @const char *tag@ = tag string, or null
410c8acf 343 * @const char *fmt@ = pointer to format string
3cdc3f3a 344 * @va_list ap@ = arguments in list
410c8acf 345 * @...@ = other arguments
346 *
347 * Returns: ---
348 *
349 * Use: Sends a message to an admin connection.
350 */
351
de014da6 352static void a_vwrite(admin *a, const char *status, const char *tag,
353 const char *fmt, va_list ap)
410c8acf 354{
410c8acf 355 dstr d = DSTR_INIT;
f98df549 356
de014da6 357 if (tag) dstr_puts(&d, "BG");
358 dstr_puts(&d, status);
0ed0735f 359 if (tag) u_quotify(&d, tag);
f43df819 360 a_vformat(&d, fmt, ap);
3cdc3f3a 361 dstr_putc(&d, '\n');
362 dosend(a, d.buf, d.len);
363 dstr_destroy(&d);
364}
365
de014da6 366static void a_write(admin *a, const char *status, const char *tag,
367 const char *fmt, ...)
3cdc3f3a 368{
369 va_list ap;
f98df549 370
410c8acf 371 va_start(ap, fmt);
de014da6 372 a_vwrite(a, status, tag, fmt, ap);
410c8acf 373 va_end(ap);
3cdc3f3a 374}
375
376/* --- @a_ok@, @a_info@, @a_fail@ --- *
377 *
378 * Arguments: @admin *a@ = connection
379 * @const char *fmt@ = format string
380 * @...@ = other arguments
381 *
382 * Returns: ---
383 *
384 * Use: Convenience functions for @a_write@.
385 */
386
f43df819 387static void a_ok(admin *a) { a_write(a, "OK", 0, A_END); }
3cdc3f3a 388
389static void a_info(admin *a, const char *fmt, ...)
390{
391 va_list ap;
f98df549 392
3cdc3f3a 393 va_start(ap, fmt);
de014da6 394 a_vwrite(a, "INFO", 0, fmt, ap);
3cdc3f3a 395 va_end(ap);
396}
397
398static void a_fail(admin *a, const char *fmt, ...)
399{
400 va_list ap;
f98df549 401
3cdc3f3a 402 va_start(ap, fmt);
de014da6 403 a_vwrite(a, "FAIL", 0, fmt, ap);
3cdc3f3a 404 va_end(ap);
405}
406
407/* --- @a_alert@, @a_valert@, @a_rawalert@ --- *
408 *
409 * Arguments: @unsigned f_and, f_eq@ = filter for connections
de014da6 410 * @const char *status@ = status string
3cdc3f3a 411 * @const char *fmt@ = pointer to format string
de014da6 412 * @const char *p@ = pointer to raw string
3cdc3f3a 413 * @size_t sz@ = size of raw string
414 * @va_list ap@ = arguments in list
415 * @...@ = other arguments
416 *
417 * Returns: ---
418 *
419 * Use: Write a message to all admin connections matched by the given
420 * filter.
421 */
422
de014da6 423static void a_rawalert(unsigned f_and, unsigned f_eq, const char *status,
3cdc3f3a 424 const char *p, size_t sz)
425{
426 admin *a, *aa;
427 dstr d = DSTR_INIT;
e04c2d50 428
3cdc3f3a 429 if (!(flags & F_INIT))
430 return;
de014da6 431 dstr_puts(&d, status);
432 if (p) {
433 dstr_putc(&d, ' ');
3cdc3f3a 434 dstr_putm(&d, p, sz);
de014da6 435 }
3cdc3f3a 436 dstr_putc(&d, '\n');
437 p = d.buf;
438 sz = d.len;
439 for (a = admins; a; a = aa) {
440 aa = a->next;
441 if ((a->f & f_and) == f_eq)
442 dosend(a, d.buf, d.len);
443 }
410c8acf 444 dstr_destroy(&d);
445}
446
b9adb829 447static void a_valert(unsigned f_and, unsigned f_eq, const char *status,
3cdc3f3a 448 const char *fmt, va_list ap)
449{
450 dstr d = DSTR_INIT;
451
452 if (!(flags & F_INIT))
453 return;
f43df819 454 a_vformat(&d, fmt, ap);
b9adb829 455 a_rawalert(f_and, f_eq, status, fmt ? d.buf : 0, fmt ? d.len : 0);
3cdc3f3a 456 dstr_destroy(&d);
457}
458
b9adb829 459static void a_alert(unsigned f_and, unsigned f_eq, const char *status,
3cdc3f3a 460 const char *fmt, ...)
461{
462 va_list ap;
f98df549 463
3cdc3f3a 464 va_start(ap, fmt);
b9adb829 465 a_valert(f_and, f_eq, status, fmt, ap);
3cdc3f3a 466 va_end(ap);
467}
3cdc3f3a 468
410c8acf 469/* --- @a_warn@ --- *
470 *
471 * Arguments: @const char *fmt@ = pointer to format string
472 * @...@ = other arguments
473 *
474 * Returns: ---
475 *
476 * Use: Informs all admin connections of a warning.
477 */
478
479void a_warn(const char *fmt, ...)
480{
481 va_list ap;
410c8acf 482
410c8acf 483 va_start(ap, fmt);
3cdc3f3a 484 if (flags & F_INIT)
485 a_valert(0, 0, "WARN", fmt, ap);
410c8acf 486 else {
f43df819 487 dstr d = DSTR_INIT;
3cdc3f3a 488 fprintf(stderr, "%s: ", QUIS);
f43df819
MW
489 a_vformat(&d, fmt, ap);
490 dstr_putc(&d, '\n');
491 dstr_write(&d, stderr);
492 dstr_destroy(&d);
410c8acf 493 }
3cdc3f3a 494 va_end(ap);
410c8acf 495}
496
497/* --- @a_trace@ --- *
498 *
499 * Arguments: @const char *p@ = pointer to a buffer
500 * @size_t sz@ = size of the buffer
501 * @void *v@ = uninteresting pointer
502 *
503 * Returns: ---
504 *
3cdc3f3a 505 * Use: Custom trace output handler. Sends trace messages to
506 * interested admin connections.
410c8acf 507 */
508
509#ifndef NTRACE
510static void a_trace(const char *p, size_t sz, void *v)
de014da6 511 { a_rawalert(AF_TRACE, AF_TRACE, "TRACE", p, sz); }
410c8acf 512#endif
513
3cdc3f3a 514/* --- @a_notify@ --- *
515 *
516 * Arguments: @const char *fmt@ = pointer to format string
517 * @...@ = other arguments
518 *
519 * Returns: ---
520 *
521 * Use: Sends a notification to interested admin connections.
522 */
523
524void a_notify(const char *fmt, ...)
525{
526 va_list ap;
527
528 va_start(ap, fmt);
529 a_valert(AF_NOTE, AF_NOTE, "NOTE", fmt, ap);
530 va_end(ap);
531}
532
410c8acf 533/* --- @a_quit@ --- *
534 *
535 * Arguments: ---
536 *
537 * Returns: ---
538 *
539 * Use: Shuts things down nicely.
540 */
541
542void a_quit(void)
543{
544 close(sock.fd);
545 unlink(sockname);
c8e02c8a 546 FOREACH_PEER(p, { p_destroy(p); });
388e0319 547 ps_quit();
410c8acf 548 exit(0);
549}
550
551/* --- @a_sigdie@ --- *
552 *
553 * Arguments: @int sig@ = signal number
554 * @void *v@ = an uninteresting argument
555 *
556 * Returns: ---
557 *
558 * Use Shuts down on receipt of a fatal signal.
559 */
560
561static void a_sigdie(int sig, void *v)
562{
563 char *p;
564 char buf[20];
565
566 switch (sig) {
567 case SIGTERM: p = "SIGTERM"; break;
568 case SIGINT: p = "SIGINT"; break;
569 default:
3cdc3f3a 570 sprintf(buf, "%i", sig);
410c8acf 571 p = buf;
572 break;
573 }
f43df819 574 a_warn("SERVER", "quit", "signal", "%s", p, A_END);
410c8acf 575 a_quit();
576}
577
578/* --- @a_sighup@ --- *
579 *
580 * Arguments: @int sig@ = signal number
581 * @void *v@ = an uninteresting argument
582 *
583 * Returns: ---
584 *
585 * Use Logs a message about SIGHUP not being useful.
586 */
587
588static void a_sighup(int sig, void *v)
f98df549 589 { a_warn("SERVER", "ignore", "signal", "SIGHUP", A_END); }
410c8acf 590
0ba8de86 591/* --- @a_parsetime@ --- *
592 *
593 * Arguments; @const char *p@ = time string to parse
594 *
595 * Returns: Time in seconds, or @< 0@ on error.
596 */
597
598static long a_parsetime(const char *p)
599{
600 char *q;
601 long t = strtol(p, &q, 0);
602
603 switch (*q) {
604 case 'd': t *= 24;
605 case 'h': t *= 60;
606 case 'm': t *= 60;
607 case 's': if (q[1] != 0)
e04c2d50 608 default: t = -1;
0ba8de86 609 case 0: break;
610 }
e04c2d50 611 return (t);
0ba8de86 612}
613
37941236 614/* --- @a_findpeer@ --- *
615 *
616 * Arguments: @admin *a@ = admin connection
617 * @const char *pn@ = peer name
618 *
619 * Returns: The peer, or null if not there.
620 *
621 * Use: Finds a peer, reporting an error if it failed.
622 */
623
624static peer *a_findpeer(admin *a, const char *pn)
625{
626 peer *p;
627
628 if ((p = p_find(pn)) == 0)
f43df819 629 a_fail(a, "unknown-peer", "%s", pn, A_END);
37941236 630 return (p);
631}
632
de014da6 633/*----- Backgrounded operations -------------------------------------------*/
634
060ca767 635#define BGTAG(bg) \
636 (((admin_bgop *)(bg))->tag ? ((admin_bgop *)(bg))->tag : "<foreground>")
637
ff92ffd3
MW
638/* --- @a_bgfind@ --- *
639 *
640 * Arguments: @admin *a@ = a client block
641 * @const char *tag@ = the requested tag
642 *
643 * Returns: The requested background job, or null.
644 */
645
646static admin_bgop *a_bgfind(admin *a, const char *tag)
647{
648 admin_bgop *bg;
649
650 for (bg = a->bg; bg; bg = bg->next) {
651 if (bg->tag && strcmp(tag, bg->tag) == 0)
652 return (bg);
653 }
654 return (0);
655}
656
de014da6 657/* --- @a_bgrelease@ --- *
658 *
659 * Arguments: @admin_bgop *bg@ = backgrounded operation
660 *
661 * Returns: ---
662 *
663 * Use: Removes a backgrounded operation from the queue, since
664 * (presumably) it's done.
665 */
666
667static void a_bgrelease(admin_bgop *bg)
668{
669 admin *a = bg->a;
670
060ca767 671 T( trace(T_ADMIN, "admin: release bgop %s", BGTAG(bg)); )
672 if (bg->tag) xfree(bg->tag);
673 else selbuf_enable(&a->b);
674 if (bg->next) bg->next->prev = bg->prev;
675 if (bg->prev) bg->prev->next = bg->next;
676 else a->bg = bg->next;
de014da6 677 xfree(bg);
547a9c13 678 if (!a->bg && (a->f & AF_CLOSE)) a_destroy(a);
de014da6 679}
680
681/* --- @a_bgok@, @a_bginfo@, @a_bgfail@ --- *
682 *
683 * Arguments: @admin_bgop *bg@ = backgrounded operation
684 * @const char *fmt@ = format string
685 * @...@ = other arguments
686 *
687 * Returns: ---
688 *
689 * Use: Convenience functions for @a_write@.
690 */
691
692static void a_bgok(admin_bgop *bg)
f43df819 693 { a_write(bg->a, "OK", bg->tag, A_END); }
de014da6 694
695static void a_bginfo(admin_bgop *bg, const char *fmt, ...)
696{
697 va_list ap;
698 va_start(ap, fmt);
699 a_vwrite(bg->a, "INFO", bg->tag, fmt, ap);
700 va_end(ap);
701}
702
703static void a_bgfail(admin_bgop *bg, const char *fmt, ...)
704{
705 va_list ap;
706 va_start(ap, fmt);
707 a_vwrite(bg->a, "FAIL", bg->tag, fmt, ap);
708 va_end(ap);
709}
710
711/* --- @a_bgadd@ --- *
712 *
713 * Arguments: @admin *a@ = administration connection
714 * @admin_bgop *bg@ = pointer to background operation
715 * @const char *tag@ = background tag, or null for foreground
716 * @void (*cancel)(admin_bgop *)@ = cancel function
717 *
ff92ffd3 718 * Returns: Zero for success, nonzero on failure.
de014da6 719 *
720 * Use: Links a background job into the list.
721 */
722
ff92ffd3
MW
723static int a_bgadd(admin *a, admin_bgop *bg, const char *tag,
724 void (*cancel)(admin_bgop *))
de014da6 725{
ff92ffd3
MW
726 if (tag) {
727 if (a_bgfind(a, tag)) {
728 a_fail(a, "tag-exists", "%s", tag, A_END);
729 return (-1);
730 }
de014da6 731 bg->tag = xstrdup(tag);
ff92ffd3 732 }
de014da6 733 else {
734 bg->tag = 0;
735 selbuf_disable(&a->b);
736 }
737 bg->a = a;
738 bg->cancel = cancel;
739 bg->next = a->bg;
740 bg->prev = 0;
060ca767 741 if (a->bg) a->bg->prev = bg;
de014da6 742 a->bg = bg;
060ca767 743 T( trace(T_ADMIN, "admin: add bgop %s", BGTAG(bg)); )
f43df819 744 if (tag) a_write(a, "DETACH", tag, A_END);
ff92ffd3 745 return (0);
de014da6 746}
747
5d46c0f8
MW
748/*----- Job table manipulation --------------------------------------------*/
749
750#define JOB_SHIFT 16
751#define JOB_INDEXMASK ((1ul << JOB_SHIFT) - 1)
752#define JOB_SEQMASK ((1ul << (32 - JOB_SHIFT)) - 1)
753
754#define JOB_END 0xfffffffful
755
756static unsigned long a_joboffset;
757
758/* --- @a_jobidencode@ --- *
759 *
760 * Arguments: @admin_svcop *svc@ = pointer to a service operation
761 *
762 * Returns: A jobid for this job, in an internal static buffer.
763 *
764 * Use: Constructs a jobid. In order to dissuade people from
765 * predicting jobids, we obfuscate them.
766 *
767 * A `raw' jobid consists of two 16-bit fields. The low 16 bits
768 * are an index into a big array. The high 16 bits are a
769 * sequence number recording how many times that slot has been
770 * reused.
771 *
772 * This `raw' jobid is then obfuscated by adding a randomly-
773 * generated offset, and multiplying (mod %$2^{32}$%) by a fixed
774 * odd constant.
775 */
776
777static const char *a_jobidencode(admin_svcop *svc)
778{
779 admin_jobtable *j = &svc->prov->j;
780 static char buf[10];
781 unsigned long pre;
782 unsigned seq;
783
784 assert(svc->index <= JOB_INDEXMASK);
785 seq = j->v[svc->index].seq;
786 assert(seq <= JOB_SEQMASK);
787 pre = (unsigned long)svc->index | ((unsigned long)seq << JOB_SHIFT);
788 sprintf(buf, "J%08lx", ((pre + a_joboffset) * 0x0f87a7a3ul) & 0xffffffff);
789 return (buf);
790}
791
792/* --- @a_jobiddecode@ --- *
793 *
794 * Arguments: @admin_jobtable *j@ = pointer to a job table
795 * @const char *jid@ = pointer to a jobid string
796 *
797 * Returns: A pointer to the job's @svcop@ structure.
798 */
799
800static admin_svcop *a_jobiddecode(admin_jobtable *j, const char *jid)
801{
802 unsigned i;
803 unsigned long pre;
804
805 if (jid[0] != 'J')
806 return (0);
807 for (i = 1; i < 9; i++) {
808 if (!isxdigit((unsigned char)jid[i]))
809 return (0);
810 }
811 if (jid[9] != 0)
812 return (0);
813 pre = strtoul(jid + 1, 0, 16);
814 pre = ((pre * 0xbd11c40bul) - a_joboffset) & 0xffffffff;
815 i = pre & JOB_INDEXMASK;
816 if (i >= j->n || j->v[i].seq != (pre >> JOB_SHIFT))
817 return (0);
818 return (j->v[i].u.op);
819}
820
821/* --- @a_jobcreate@ --- *
822 *
823 * Arguments: @admin *a@ = pointer to administration client
824 *
825 * Returns: A pointer to a freshly-allocated @svcop@, or null.
826 *
827 * Use: Allocates a fresh @svcop@ and links it into a job table.
828 */
829
830static admin_svcop *a_jobcreate(admin *a)
831{
832 admin_svcop *svc;
833 unsigned i;
834 unsigned sz;
835 admin_jobtable *j = &a->j;
836
837 if (j->free != JOB_END) {
838 i = j->free;
839 j->free = j->v[i].u.next;
840 } else {
841 if (j->n == j->sz) {
842 if (j->sz > JOB_INDEXMASK)
843 return (0);
844 sz = j->sz;
845 if (!sz) {
846 j->sz = 16;
847 j->v = xmalloc(j->sz * sizeof(*j->v));
848 } else {
849 j->sz = sz << 1;
850 j->v = xrealloc(j->v, j->sz * sizeof(*j->v), sz * sizeof(*j->v));
851 }
852 }
853 i = j->n++;
854 j->v[i].seq = 0;
855 }
856 svc = xmalloc(sizeof(*svc));
857 svc->index = i;
858 svc->prov = a;
859 svc->next = j->active;
860 svc->prev = 0;
861 if (j->active) j->active->prev = svc;
862 j->active = svc;
863 j->v[i].u.op = svc;
864 IF_TRACING(T_ADMIN, {
865 trace(T_ADMIN, "admin: created job %s (%u)", a_jobidencode(svc), i);
866 })
867 return (svc);
868}
869
870/* --- @a_jobdestroy@ --- *
871 *
872 * Arguments: @admin_svcop *svc@ = pointer to job block
873 *
874 * Returns: ---
875 *
876 * Use: Frees up a completed (or cancelled) job.
877 */
878
879static void a_jobdestroy(admin_svcop *svc)
880{
881 admin *a = svc->prov;
882 admin_jobtable *j = &a->j;
883 unsigned i = svc->index;
884
885 IF_TRACING(T_ADMIN, {
886 trace(T_ADMIN, "admin: destroying job %s (%u)", a_jobidencode(svc), i);
887 })
888 assert(j->v[i].u.op = svc);
889 j->v[i].u.next = j->free;
890 j->v[i].seq++;
891 j->free = i;
892 if (svc->next) svc->next->prev = svc->prev;
893 if (svc->prev) svc->prev->next = svc->next;
894 else j->active = svc->next;
895}
896
897/* --- @a_jobtableinit@ --- *
898 *
899 * Arguments: @admin_jobtable *j@ = pointer to job table
900 *
901 * Returns: ---
902 *
903 * Use: Initializes a job table.
904 */
905
906static void a_jobtableinit(admin_jobtable *j)
907{
908 if (!a_joboffset)
909 a_joboffset = GR_RANGE(&rand_global, 0xffffffff) + 1;
910 j->n = j->sz = 0;
911 j->active = 0;
912 j->free = JOB_END;
913 j->v = 0;
914}
915
916/* --- @a_jobtablefinal@ --- *
917 *
918 * Arguments: @admin_jobtable *j@ = pointer to job table
919 *
920 * Returns: ---
921 *
922 * Use: Closes down a job table.
923 */
924
925static void a_jobtablefinal(admin_jobtable *j)
926{
927 admin_svcop *svc, *ssvc;
928
929 for (svc = j->active; svc; svc = ssvc) {
930 ssvc = svc->next;
931 a_bgfail(&svc->bg, "provider-failed", A_END);
932 a_bgrelease(&svc->bg);
933 }
934 if (j->v) xfree(j->v);
935}
936
be6a1b7a
MW
937/*----- Services infrastructure -------------------------------------------*/
938
939/* --- @a_svcfind@ --- *
940 *
941 * Arguments: @admin *a@ = the requesting client
942 * @const char *name@ = service name wanted
943 *
944 * Returns: The service requested, or null.
945 *
946 * Use: Finds a service; reports an error if the service couldn't be
947 * found.
948 */
949
950static admin_service *a_svcfind(admin *a, const char *name)
951{
952 admin_service *svc;
953
954 if ((svc = sym_find(&a_svcs, name, -1, 0, 0)) == 0) {
955 a_fail(a, "unknown-service", "%s", name, A_END);
956 return (0);
957 }
958 return (svc);
959}
960
961/* --- @a_svcunlink@ --- *
962 *
963 * Arguments: @admin_service *svc@ = pointer to service structure
964 *
965 * Returns: ---
966 *
967 * Use: Unlinks the service from its provider, without issuing a
968 * message or freeing the structure. The version string is
969 * freed, however.
970 */
971
972static void a_svcunlink(admin_service *svc)
973{
974 if (svc->next)
975 svc->next->prev = svc->prev;
976 if (svc->prev)
977 svc->prev->next = svc->next;
978 else
979 svc->prov->svcs = svc->next;
980 xfree(svc->version);
981}
982
983/* --- @a_svcrelease@ --- *
984 *
985 * Arguments: @admin_service *svc@ = pointer to service structure
986 *
987 * Returns: ---
988 *
989 * Use: Releases a service and frees its structure.
990 */
991
992static void a_svcrelease(admin_service *svc)
993{
994 a_notify("SVCRELEASE", "%s", SYM_NAME(svc), A_END);
995 a_svcunlink(svc);
996 sym_remove(&a_svcs, svc);
de014da6 997}
998
37941236 999/*----- Name resolution operations ----------------------------------------*/
de014da6 1000
37941236 1001/* --- @a_resolved@ --- *
de014da6 1002 *
37941236 1003 * Arguments: @struct hostent *h@ = pointer to resolved hostname
1004 * @void *v@ = pointer to resolver operation
de014da6 1005 *
1006 * Returns: ---
1007 *
37941236 1008 * Use: Handles a completed name resolution.
de014da6 1009 */
1010
37941236 1011static void a_resolved(struct hostent *h, void *v)
de014da6 1012{
37941236 1013 admin_resop *r = v;
1014
1015 T( trace(T_ADMIN, "admin: resop %s resolved", BGTAG(r)); )
1016 TIMER;
1017 if (!h) {
f43df819 1018 a_bgfail(&r->bg, "resolve-error", "%s", r->addr, A_END);
37941236 1019 r->func(r, ARES_FAIL);
1020 } else {
1021 memcpy(&r->sa.sin.sin_addr, h->h_addr, sizeof(struct in_addr));
1022 r->func(r, ARES_OK);
1023 }
1024 sel_rmtimer(&r->t);
1025 xfree(r->addr);
1026 a_bgrelease(&r->bg);
e04c2d50 1027}
de014da6 1028
37941236 1029/* --- @a_restimer@ --- *
de014da6 1030 *
37941236 1031 * Arguments: @struct timeval *tv@ = timer
1032 * @void *v@ = pointer to resolver operation
de014da6 1033 *
1034 * Returns: ---
1035 *
37941236 1036 * Use: Times out a resolver.
de014da6 1037 */
1038
37941236 1039static void a_restimer(struct timeval *tv, void *v)
de014da6 1040{
37941236 1041 admin_resop *r = v;
1042
1043 T( trace(T_ADMIN, "admin: resop %s timeout", BGTAG(r)); )
f43df819 1044 a_bgfail(&r->bg, "resolver-timeout", "%s", r->addr, A_END);
37941236 1045 r->func(r, ARES_FAIL);
1046 bres_abort(&r->r);
1047 xfree(r->addr);
1048 a_bgrelease(&r->bg);
de014da6 1049}
1050
37941236 1051/* --- @a_rescancel@ --- *
de014da6 1052 *
37941236 1053 * Arguments: @admin_bgop *bg@ = background operation
de014da6 1054 *
1055 * Returns: ---
1056 *
37941236 1057 * Use: Cancels an add operation.
de014da6 1058 */
1059
37941236 1060static void a_rescancel(admin_bgop *bg)
de014da6 1061{
37941236 1062 admin_resop *r = (admin_resop *)bg;
1063
1064 T( trace(T_ADMIN, "admin: cancel resop %s", BGTAG(r)); )
1065 r->func(r, ARES_FAIL);
1066 sel_rmtimer(&r->t);
1067 xfree(r->addr);
1068 bres_abort(&r->r);
de014da6 1069}
37941236 1070
1071/* --- @a_resolve@ --- *
410c8acf 1072 *
37941236 1073 * Arguments: @admin *a@ = administration connection
1074 * @admin_resop *r@ = resolver operation to run
1075 * @const char *tag@ = background operation tag
1076 * @void (*func)(struct admin_resop *, int@ = handler function
1077 * @unsigned ac@ = number of remaining arguments
1078 * @char *av[]@ = pointer to remaining arguments
410c8acf 1079 *
1080 * Returns: ---
1081 *
37941236 1082 * Use: Cranks up a resolver job.
410c8acf 1083 */
1084
37941236 1085static void a_resolve(admin *a, admin_resop *r, const char *tag,
1086 void (*func)(struct admin_resop *, int),
1087 unsigned ac, char *av[])
410c8acf 1088{
37941236 1089 struct timeval tv;
1090 unsigned long pt;
1091 char *p;
1092 int i = 0;
fd3cf232 1093
37941236 1094 /* --- Fill in the easy bits of address --- */
1095
452bf3f6 1096 r->bg.tag = "<starting>";
37941236 1097 r->addr = 0;
1098 r->func = func;
1099 if (mystrieq(av[i], "inet")) i++;
165efde7
MW
1100 if (ac - i != 1 && ac - i != 2) {
1101 a_fail(a, "bad-addr-syntax", "[inet] ADDRESS [PORT]", A_END);
37941236 1102 goto fail;
1103 }
1104 r->sa.sin.sin_family = AF_INET;
1105 r->sasz = sizeof(r->sa.sin);
1106 r->addr = xstrdup(av[i]);
6f88042a 1107 if (!av[i + 1])
165efde7
MW
1108 pt = TRIPE_PORT;
1109 else {
1110 pt = strtoul(av[i + 1], &p, 0);
1111 if (*p) {
1112 struct servent *s = getservbyname(av[i + 1], "udp");
1113 if (!s) {
1114 a_fail(a, "unknown-service", "%s", av[i + 1], A_END);
1115 goto fail;
1116 }
1117 pt = ntohs(s->s_port);
37941236 1118 }
410c8acf 1119 }
37941236 1120 if (pt == 0 || pt >= 65536) {
f43df819 1121 a_fail(a, "invalid-port", "%lu", pt, A_END);
37941236 1122 goto fail;
1123 }
1124 r->sa.sin.sin_port = htons(pt);
1125
1126 /* --- Report backgrounding --- *
1127 *
1128 * Do this for consistency of interface, even if we're going to get the
1129 * answer straight away.
1130 */
1131
ff92ffd3
MW
1132 if (a_bgadd(a, &r->bg, tag, a_rescancel))
1133 goto fail;
37941236 1134 T( trace(T_ADMIN, "admin: %u, resop %s, hostname `%s'",
1135 a->seq, BGTAG(r), r->addr); )
1136
1137 /* --- If the name is numeric, do it the easy way --- */
e04c2d50 1138
37941236 1139 if (inet_aton(av[i], &r->sa.sin.sin_addr)) {
1140 T( trace(T_ADMIN, "admin: resop %s done the easy way", BGTAG(r)); )
1141 func(r, ARES_OK);
1142 xfree(r->addr);
1143 a_bgrelease(&r->bg);
1144 return;
1145 }
1146
1147 /* --- Store everything for later and crank up the resolver --- */
1148
1149 gettimeofday(&tv, 0);
1150 tv.tv_sec += T_RESOLVE;
1151 sel_addtimer(&sel, &r->t, &tv, a_restimer, r);
1152 bres_byname(&r->r, r->addr, a_resolved, r);
1153 return;
1154
1155fail:
1156 func(r, ARES_FAIL);
1157 if (r->addr) xfree(r->addr);
1158 xfree(r);
410c8acf 1159}
1160
e7a4d919
MW
1161/*----- Option parsing ----------------------------------------------------*/
1162
1163#define OPTIONS(argc, argv, guts) do { \
1164 char **o_av = argv; \
1165 for (;; o_av++) { \
1166 if (!*o_av) \
1167 break; \
1168 if (mystrieq(*o_av, "--")) { \
1169 o_av++; \
1170 break; \
1171 } \
1172 guts \
1173 if (**o_av == '-') \
1174 goto bad_syntax; \
1175 break; \
1176 } \
1177 argc -= o_av - argv; \
1178 argv = o_av; \
1179} while (0)
1180
1181#define OPT(name, guts) if (mystrieq(*o_av, name)) { guts continue; }
1182
1183#define OPTARG(name, arg, guts) OPT(name, { \
1184 const char *arg; \
1185 arg = *++o_av; \
1186 if (!arg) goto bad_syntax; \
1187 guts \
1188})
1189
1190#define OPTTIME(name, arg, guts) OPTARG(name, o_arg, { \
1191 long arg; \
1192 if ((arg = a_parsetime(o_arg)) < 0) { \
1193 a_fail(a, "bad-time-spec", "%s", o_arg, A_END); \
1194 goto fail; \
1195 } \
1196 guts \
1197})
1198
37941236 1199/*----- Adding peers ------------------------------------------------------*/
1200
1201/* --- @a_doadd@ --- *
410c8acf 1202 *
37941236 1203 * Arguments: @admin_resop *r@ = resolver operation
1204 * @int rc@ = how it worked
410c8acf 1205 *
1206 * Returns: ---
1207 *
37941236 1208 * Use: Handles a completed resolution.
410c8acf 1209 */
1210
37941236 1211static void a_doadd(admin_resop *r, int rc)
410c8acf 1212{
37941236 1213 admin_addop *add = (admin_addop *)r;
1214
1215 T( trace(T_ADMIN, "admin: done add op %s", BGTAG(add)); )
1216
1217 if (rc == ARES_OK) {
1218 add->peer.sasz = add->r.sasz;
1219 add->peer.sa = add->r.sa;
c8e02c8a
MW
1220 if (p_findbyaddr(&add->r.sa))
1221 a_bgfail(&add->r.bg, "peer-addr-exists", "?ADDR", &add->r.sa, A_END);
1222 else if (p_find(add->peer.name))
f43df819 1223 a_bgfail(&add->r.bg, "peer-exists", "%s", add->peer.name, A_END);
37941236 1224 else if (!p_create(&add->peer))
f43df819 1225 a_bgfail(&add->r.bg, "peer-create-fail", "%s", add->peer.name, A_END);
37941236 1226 else
1227 a_bgok(&add->r.bg);
1228 }
de014da6 1229
48b84569 1230 if (add->peer.tag) xfree(add->peer.tag);
37941236 1231 xfree(add->peer.name);
410c8acf 1232}
1233
1234/* --- @acmd_add@ --- *
1235 *
1236 * Arguments: @admin *a@ = connection which requested the addition
1237 * @unsigned ac@ = argument count
1238 * @char *av[]@ = pointer to the argument list
1239 *
1240 * Returns: ---
1241 *
1242 * Use: Adds a new peer.
1243 */
1244
1245static void acmd_add(admin *a, unsigned ac, char *av[])
1246{
de014da6 1247 const char *tag = 0;
9986f0b5 1248 admin_addop *add;
410c8acf 1249
0ba8de86 1250 /* --- Set stuff up --- */
1251
de014da6 1252 add = xmalloc(sizeof(*add));
9986f0b5 1253 add->peer.name = 0;
48b84569 1254 add->peer.tag = 0;
fe2a5dcf 1255 add->peer.privtag = 0;
de014da6 1256 add->peer.t_ka = 0;
1257 add->peer.tops = tun_default;
8743c776 1258 add->peer.f = 0;
37941236 1259
42da2a58 1260 /* --- Parse options --- */
1261
e7a4d919
MW
1262 OPTIONS(ac, av, {
1263 OPTARG("-background", arg, { tag = arg; })
1264 OPTARG("-tunnel", arg, {
1265 unsigned i;
1266 for (i = 0;; i++) {
1267 if (!tunnels[i]) {
1268 a_fail(a, "unknown-tunnel", "%s", arg, A_END);
37941236 1269 goto fail;
42da2a58 1270 }
e7a4d919
MW
1271 if (mystrieq(arg, tunnels[i]->name)) {
1272 add->peer.tops = tunnels[i];
42da2a58 1273 break;
1274 }
1275 }
e7a4d919
MW
1276 })
1277 OPTTIME("-keepalive", t, { add->peer.t_ka = t; })
8743c776 1278 OPT("-cork", { add->peer.f |= KXF_CORK; })
48b84569
MW
1279 OPTARG("-key", arg, {
1280 if (add->peer.tag)
1281 xfree(add->peer.tag);
1282 add->peer.tag = xstrdup(arg);
1283 })
6411163d 1284 OPT("-mobile", { add->peer.f |= PSF_MOBILE; })
fe2a5dcf
MW
1285 OPTARG("-priv", arg, {
1286 if (add->peer.privtag)
1287 xfree(add->peer.privtag);
1288 add->peer.privtag = xstrdup(arg);
1289 })
e7a4d919 1290 });
42da2a58 1291
9986f0b5
MW
1292 /* --- Make sure someone's not got there already --- */
1293
bcf3764a 1294 if (!av[0] || !av[1])
9986f0b5 1295 goto bad_syntax;
e7a4d919
MW
1296 if (p_find(*av)) {
1297 a_fail(a, "peer-exists", "%s", *av, A_END);
9986f0b5 1298 goto fail;
42da2a58 1299 }
e7a4d919
MW
1300 add->peer.name = xstrdup(*av++);
1301 ac--;
42da2a58 1302
37941236 1303 /* --- Crank up the resolver --- */
de014da6 1304
e7a4d919 1305 a_resolve(a, &add->r, tag, a_doadd, ac, av);
42da2a58 1306 return;
1307
37941236 1308 /* --- Clearing up --- */
1309
42da2a58 1310bad_syntax:
9986f0b5 1311 a_fail(a, "bad-syntax", "add", "[OPTIONS] PEER ADDR ...", A_END);
de014da6 1312fail:
9986f0b5 1313 if (add->peer.name) xfree(add->peer.name);
48b84569 1314 if (add->peer.tag) xfree(add->peer.tag);
fe2a5dcf 1315 if (add->peer.privtag) xfree(add->peer.privtag);
37941236 1316 xfree(add);
0ba8de86 1317 return;
1318}
1319
1320/*----- Ping --------------------------------------------------------------*/
1321
de014da6 1322/* --- @a_pingcancel@ --- *
1323 *
1324 * Arguments: @admin_bgop *bg@ = background operation block
1325 *
1326 * Returns: ---
1327 *
1328 * Use: Cancels a running ping.
1329 */
1330
1331static void a_pingcancel(admin_bgop *bg)
1332{
1333 admin_pingop *pg = (admin_pingop *)bg;
060ca767 1334 T( trace(T_ADMIN, "admin: cancel ping op %s", BGTAG(pg)); )
de014da6 1335 p_pingdone(&pg->ping, PING_NONOTIFY);
1336}
1337
0ba8de86 1338/* --- @a_pong@ --- *
1339 *
1340 * Arguments: @int rc@ = return code
de014da6 1341 * @void *v@ = ping operation block
0ba8de86 1342 *
1343 * Returns: ---
1344 *
1345 * Use: Collects what happened to a ping message.
1346 */
1347
de014da6 1348static void a_pong(int rc, void *v)
0ba8de86 1349{
de014da6 1350 admin_pingop *pg = v;
0ba8de86 1351 struct timeval tv;
1352 double millis;
1353
0ba8de86 1354 switch (rc) {
1355 case PING_OK:
1356 gettimeofday(&tv, 0);
de014da6 1357 tv_sub(&tv, &tv, &pg->pingtime);
0ba8de86 1358 millis = (double)tv.tv_sec * 1000 + (double)tv.tv_usec/1000;
f43df819 1359 a_bginfo(&pg->bg, "ping-ok", "%.1f", millis, A_END);
de014da6 1360 a_bgok(&pg->bg);
0ba8de86 1361 break;
1362 case PING_TIMEOUT:
f43df819 1363 a_bginfo(&pg->bg, "ping-timeout", A_END);
de014da6 1364 a_bgok(&pg->bg);
0ba8de86 1365 break;
1366 case PING_PEERDIED:
f43df819 1367 a_bginfo(&pg->bg, "ping-peer-died", A_END);
de014da6 1368 a_bgok(&pg->bg);
0ba8de86 1369 break;
1370 default:
1371 abort();
1372 }
060ca767 1373 T( trace(T_ADMIN, "admin: ponged ping op %s", BGTAG(pg)); )
de014da6 1374 a_bgrelease(&pg->bg);
0ba8de86 1375}
1376
1377/* --- @acmd_ping@, @acmd_eping@ --- *
1378 *
1379 * Arguments: @admin *a@ = connection which requested the ping
1380 * @unsigned ac@ = argument count
1381 * @char *av[]@ = pointer to the argument list
1382 *
1383 * Returns: ---
1384 *
1385 * Use: Pings a peer.
1386 */
1387
1388static void a_ping(admin *a, unsigned ac, char *av[],
1389 const char *cmd, unsigned msg)
1390{
1391 long t = T_PING;
0ba8de86 1392 peer *p;
de014da6 1393 admin_pingop *pg = 0;
1394 const char *tag = 0;
0ba8de86 1395
e7a4d919
MW
1396 OPTIONS(ac, av, {
1397 OPTARG("-background", arg, { tag = arg; })
1398 OPTTIME("-timeout", arg, { t = arg; })
1399 });
1400 if (!*av || av[1]) goto bad_syntax;
1401 if ((p = a_findpeer(a, *av)) == 0)
0ba8de86 1402 return;
de014da6 1403 pg = xmalloc(sizeof(*pg));
1404 gettimeofday(&pg->pingtime, 0);
ff92ffd3
MW
1405 if (a_bgadd(a, &pg->bg, tag, a_pingcancel))
1406 goto fail;
060ca767 1407 T( trace(T_ADMIN, "admin: ping op %s: %s to %s",
1408 BGTAG(pg), cmd, p_name(p)); )
de014da6 1409 if (p_pingsend(p, &pg->ping, msg, t, a_pong, pg)) {
f43df819 1410 a_bgfail(&pg->bg, "ping-send-failed", A_END);
de014da6 1411 a_bgrelease(&pg->bg);
1412 }
0ba8de86 1413 return;
e04c2d50 1414
0ba8de86 1415bad_syntax:
f43df819 1416 a_fail(a, "bad-syntax", "%s", cmd, "[OPTIONS] PEER", cmd, A_END);
e7a4d919 1417fail:
ff92ffd3 1418 if (pg) xfree(pg);
42da2a58 1419 return;
410c8acf 1420}
1421
0ba8de86 1422static void acmd_ping(admin *a, unsigned ac, char *av[])
1423 { a_ping(a, ac, av, "ping", MISC_PING); }
1424static void acmd_eping(admin *a, unsigned ac, char *av[])
1425 { a_ping(a, ac, av, "eping", MISC_EPING); }
0ba8de86 1426
be6a1b7a
MW
1427/*----- Service commands --------------------------------------------------*/
1428
1429static void acmd_svcclaim(admin *a, unsigned ac, char *av[])
1430{
1431 admin_service *svc;
1432 unsigned f;
1433
1434 svc = sym_find(&a_svcs, av[0], -1, sizeof(*svc), &f);
1435 if (f) {
1436 if (versioncmp(av[1], svc->version) <= 0) {
1437 a_fail(a,
1438 "service-exists",
1439 "%s", SYM_NAME(svc),
1440 "%s", svc->version,
1441 A_END);
1442 return;
1443 }
1444 a_write(svc->prov, "SVCCLAIM", 0, "%s", av[0], "%s", av[1], A_END);
1445 a_svcunlink(svc);
1446 }
1447 svc->prov = a;
1448 svc->version = xstrdup(av[1]);
1449 svc->next = a->svcs;
1450 svc->prev = 0;
1451 if (a->svcs) a->svcs->prev = svc;
1452 a->svcs = svc;
1453 a_notify("SVCCLAIM", "%s", SYM_NAME(svc), "%s", svc->version, A_END);
1454 a_ok(a);
1455}
1456
1457static void acmd_svcrelease(admin *a, unsigned ac, char *av[])
1458{
1459 admin_service *svc;
1460
1461 if ((svc = a_svcfind(a, av[0])) == 0)
1462 return;
1463 if (svc->prov != a) {
1464 a_fail(a, "not-service-provider", "%s", SYM_NAME(svc), A_END);
1465 return;
1466 }
1467 a_svcrelease(svc);
1468 a_ok(a);
1469}
1470
1471static void acmd_svcensure(admin *a, unsigned ac, char *av[])
1472{
1473 admin_service *svc;
1474
1475 if ((svc = a_svcfind(a, av[0])) == 0)
1476 return;
1477 if (av[1] && versioncmp(svc->version, av[1]) < 0) {
1478 a_fail(a, "service-too-old",
1479 "%s", SYM_NAME(svc),
1480 "%s", svc->version,
1481 A_END);
1482 return;
1483 }
1484 a_ok(a);
1485}
1486
1487static void acmd_svcquery(admin *a, unsigned ac, char *av[])
1488{
1489 admin_service *svc;
1490
1491 if ((svc = a_svcfind(a, av[0])) != 0) {
1492 a_info(a, "name=%s", SYM_NAME(svc), "version=%s", svc->version, A_END);
1493 a_ok(a);
1494 }
1495}
1496
1497static void acmd_svclist(admin *a, unsigned ac, char *av[])
1498{
1499 admin_service *svc;
1500 sym_iter i;
1501
1502 for (sym_mkiter(&i, &a_svcs); (svc = sym_next(&i)) != 0; )
1503 a_info(a, "%s", SYM_NAME(svc), "%s", svc->version, A_END);
1504 a_ok(a);
1505}
1506
ef2f29c9
MW
1507static void a_canceljob(admin_bgop *bg)
1508{
1509 admin_svcop *svc = (admin_svcop *)bg;
1510
1511 a_write(svc->prov, "SVCCANCEL", 0, "%s", a_jobidencode(svc), A_END);
1512 a_jobdestroy(svc);
1513}
1514
1515static void acmd_svcsubmit(admin *a, unsigned ac, char *av[])
1516{
1517 const char *tag = 0;
1518 admin_service *svc;
1519 admin_svcop *svcop;
1520 const char *ver = 0;
1521 dstr d = DSTR_INIT;
1522
1523 OPTIONS(ac, av, {
1524 OPTARG("-background", arg, { tag = arg; })
1525 OPTARG("-version", arg, { ver = arg; })
1526 });
1527 if (!*av) goto bad_syntax;
1528 if ((svc = a_svcfind(a, *av)) == 0) goto fail;
1529 if (ver && versioncmp(svc->version, ver) < 0) {
1530 a_fail(a, "service-too-old",
1531 "%s", SYM_NAME(svc),
1532 "%s", svc->version,
1533 A_END);
1534 goto fail;
1535 }
1536 if ((svcop = a_jobcreate(svc->prov)) == 0) {
1537 a_fail(a, "provider-overloaded", A_END);
1538 goto fail;
1539 }
1540 if (a_bgadd(a, &svcop->bg, tag, a_canceljob)) {
1541 a_jobdestroy(svcop);
1542 goto fail;
1543 }
1544 a_write(svc->prov, "SVCJOB", 0,
1545 "%s", a_jobidencode(svcop),
1546 "?TOKENS", av,
1547 A_END);
1548 goto done;
1549
1550bad_syntax:
1551 a_fail(a, "bad-syntax", "svcsubmit", "[OPTIONS] SERVICE ARGS...", A_END);
1552fail:
1553done:
1554 dstr_destroy(&d);
1555 return;
1556}
1557
1558static void a_replycmd(admin *a, const char *status, int donep, char *av[])
1559{
1560 admin_svcop *svc;
1561
1562 if ((svc = a_jobiddecode(&a->j, av[0])) == 0) {
1563 a_fail(a, "unknown-jobid", av[0], A_END);
1564 return;
1565 }
1566 a_write(svc->bg.a, status, svc->bg.tag, "?TOKENS", av + 1, A_END);
1567 if (donep) {
1568 a_jobdestroy(svc);
1569 a_bgrelease(&svc->bg);
1570 }
1571 a_ok(a);
1572}
1573
1574static void acmd_svcok(admin *a, unsigned ac, char *av[])
1575 { a_replycmd(a, "OK", 1, av); }
1576static void acmd_svcinfo(admin *a, unsigned ac, char *av[])
1577 { a_replycmd(a, "INFO", 0, av); }
1578static void acmd_svcfail(admin *a, unsigned ac, char *av[])
1579 { a_replycmd(a, "FAIL", 1, av); }
1580
410c8acf 1581/*----- Administration commands -------------------------------------------*/
1582
1583/* --- Miscellaneous commands --- */
1584
3cdc3f3a 1585/* --- @traceish@ --- *
1586 *
1587 * Arguments: @admin *a@ = connection to complain on
1588 * @unsigned ac@ = number of arguments
1589 * @char *av[]@ = vector of arguments
1590 * @const char *what@ = what we're messing with
1591 * @const trace_opt *tt@ = options table
1592 * @unsigned *ff@ = where the flags are
1593 *
1594 * Returns: Nonzero if anything changed.
1595 *
1596 * Use: Guts of trace-ish commands like `trace' and `watch'.
1597 */
410c8acf 1598
3cdc3f3a 1599static int traceish(admin *a, unsigned ac, char *av[],
1600 const char *what, const trace_opt *tt, unsigned *ff)
410c8acf 1601{
3cdc3f3a 1602 int ch = 0;
1603
410c8acf 1604 if (!ac || strcmp(av[0], "?") == 0) {
1605 const trace_opt *t;
3cdc3f3a 1606 for (t = tt; t->ch; t++) {
f43df819
MW
1607 a_info(a, "*%c%c %s",
1608 t->ch, (*ff & t->f) == t->f ? '+' : ' ', t->help, A_END);
410c8acf 1609 }
1610 } else {
1611 unsigned sense = 1;
3cdc3f3a 1612 unsigned f = *ff;
1613 const trace_opt *t;
410c8acf 1614 char *p = av[0];
1615
1616 while (*p) {
1617 switch (*p) {
1618 case '+': sense = 1; break;
1619 case '-': sense = 0; break;
1620 default:
3cdc3f3a 1621 for (t = tt; t->ch; t++) {
1622 if (t->ch == *p) {
1623 if (sense) f |= t->f;
1624 else f &= ~t->f;
410c8acf 1625 goto tropt_ok;
1626 }
1627 }
f43df819 1628 a_fail(a, "bad-%s-option", what, "%c", *p, A_END);
3cdc3f3a 1629 return (0);
e04c2d50 1630 tropt_ok:;
410c8acf 1631 break;
1632 }
1633 p++;
1634 }
3cdc3f3a 1635 *ff = f;
1636 ch = 1;
410c8acf 1637 }
3cdc3f3a 1638 a_ok(a);
1639 return (ch);
1640}
1641
1642#ifndef NTRACE
1643
1644static void acmd_trace(admin *a, unsigned ac, char *av[])
1645{
1646 if (traceish(a, ac, av, "trace", tr_opts, &tr_flags))
1647 trace_level(tr_flags);
410c8acf 1648}
1649
1650#endif
1651
3cdc3f3a 1652static void acmd_watch(admin *a, unsigned ac, char *av[])
f98df549 1653 { traceish(a, ac, av, "watch", w_opts, &a->f); }
3cdc3f3a 1654
3cdc3f3a 1655static void alertcmd(admin *a, unsigned f_and, unsigned f_eq,
3331936b
MW
1656 const char *status, char *av[])
1657 { a_alert(f_and, f_eq, status, "USER", "?TOKENS", av, A_END); a_ok(a); }
3cdc3f3a 1658static void acmd_notify(admin *a, unsigned ac, char *av[])
3331936b 1659 { alertcmd(a, AF_NOTE, AF_NOTE, "NOTE", av); }
3cdc3f3a 1660static void acmd_warn(admin *a, unsigned ac, char *av[])
3331936b 1661 { alertcmd(a, AF_WARN, AF_WARN, "WARN", av); }
3cdc3f3a 1662
410c8acf 1663static void acmd_port(admin *a, unsigned ac, char *av[])
f98df549 1664 { a_info(a, "%u", p_port(), A_END); a_ok(a); }
410c8acf 1665
410c8acf 1666static void acmd_daemon(admin *a, unsigned ac, char *av[])
1667{
1668 if (flags & F_DAEMON)
f43df819 1669 a_fail(a, "already-daemon", A_END);
410c8acf 1670 else {
f43df819 1671 a_notify("DAEMON", A_END);
93bee2c4 1672 if (a_stdin)
410c8acf 1673 a_destroy(a_stdin);
19dd2531 1674 if (daemonize())
f43df819 1675 a_fail(a, "daemon-error", "?ERRNO", A_END);
93bee2c4 1676 else {
410c8acf 1677 flags |= F_DAEMON;
3cdc3f3a 1678 a_ok(a);
93bee2c4 1679 }
410c8acf 1680 }
1681}
1682
ff92ffd3
MW
1683static void acmd_jobs(admin *a, unsigned ac, char *av[])
1684{
1685 admin_bgop *bg;
1686
1687 for (bg = a->bg; bg; bg = bg->next) {
1688 assert(bg->tag);
1689 a_info(a, "%s", bg->tag, A_END);
1690 }
1691 a_ok(a);
1692}
1693
1694static void acmd_bgcancel(admin *a, unsigned ac, char *av[])
1695{
1696 admin_bgop *bg;
1697
1698 if ((bg = a_bgfind(a, av[0])) == 0)
1699 a_fail(a, "unknown-tag", "%s", av[0], A_END);
1700 else {
1701 bg->cancel(bg);
1702 a_bgrelease(bg);
1703 a_ok(a);
1704 }
1705}
1706
449991a3
MW
1707static void acmd_algs(admin *a, unsigned ac, char *av[])
1708{
35c8b547
MW
1709 peer *p;
1710 const kdata *kd;
1711 const group *g;
1712 const algswitch *algs;
1713
1714 if (!ac)
1715 kd = master;
1716 else {
1717 if ((p = a_findpeer(a, av[0])) == 0) return;
1718 kd = p->kx.kpriv;
1719 }
1720 g = kd->g;
1721 algs = &kd->algs;
1722
449991a3 1723 a_info(a,
35c8b547
MW
1724 "kx-group=%s", g->ops->name,
1725 "kx-group-order-bits=%lu", (unsigned long)mp_bits(g->r),
1726 "kx-group-elt-bits=%lu", (unsigned long)g->nbits,
449991a3
MW
1727 A_END);
1728 a_info(a,
35c8b547
MW
1729 "hash=%s", algs->h->name,
1730 "mgf=%s", algs->mgf->name,
1731 "hash-sz=%lu", (unsigned long)algs->h->hashsz,
449991a3
MW
1732 A_END);
1733 a_info(a,
a93aacce
MW
1734 "bulk-transform=%s", algs->bulk->name,
1735 "bulk-overhead=%lu", (unsigned long)algs->bulk->overhead(algs),
449991a3 1736 A_END);
a93aacce
MW
1737 if (algs->c) {
1738 a_info(a,
1739 "cipher=%s", algs->c->name,
1740 "cipher-keysz=%lu", (unsigned long)algs->cksz,
1741 "cipher-blksz=%lu", (unsigned long)algs->c->blksz,
1742 A_END);
1743 }
449991a3 1744 a_info(a,
35c8b547 1745 "cipher-data-limit=%lu", (unsigned long)algs->expsz,
449991a3 1746 A_END);
a93aacce
MW
1747 if (algs->m) {
1748 a_info(a,
1749 "mac=%s", algs->m->name,
1750 "mac-keysz=%lu", (unsigned long)algs->mksz,
1751 "mac-tagsz=%lu", (unsigned long)algs->tagsz,
1752 A_END);
1753 }
b87bffcb
MW
1754 if (algs->b) {
1755 a_info(a,
1756 "blkc=%.*s", strlen(algs->b->name) - 4, algs->b->name,
1757 "blkc-keysz=%lu", (unsigned long)algs->bksz,
1758 "blkc-blksz=%lu", (unsigned long)algs->b->blksz,
1759 A_END);
1760 }
449991a3
MW
1761 a_ok(a);
1762}
1763
410c8acf 1764static void acmd_list(admin *a, unsigned ac, char *av[])
1765{
c8e02c8a 1766 FOREACH_PEER(p, { a_info(a, "%s", p_name(p), A_END); });
3cdc3f3a 1767 a_ok(a);
410c8acf 1768}
1769
1770static void acmd_ifname(admin *a, unsigned ac, char *av[])
1771{
1772 peer *p;
1773
37941236 1774 if ((p = a_findpeer(a, av[0])) != 0) {
f43df819 1775 a_info(a, "%s", p_ifname(p), A_END);
3cdc3f3a 1776 a_ok(a);
1777 }
410c8acf 1778}
1779
64cf2223
MW
1780static void acmd_setifname(admin *a, unsigned ac, char *av[])
1781{
1782 peer *p;
1783
1784 if ((p = a_findpeer(a, av[0])) != 0) {
1785 a_notify("NEWIFNAME", "?PEER", p, "%s", p_ifname(p), "%s", av[1], A_END);
1786 p_setifname(p, av[1]);
1787 a_ok(a);
e04c2d50 1788 }
64cf2223
MW
1789}
1790
37941236 1791static void acmd_getchal(admin *a, unsigned ac, char *av[])
1792{
1793 buf b;
1794
1795 buf_init(&b, buf_i, PKBUFSZ);
1796 c_new(&b);
f43df819 1797 a_info(a, "?B64", BBASE(&b), (size_t)BLEN(&b), A_END);
37941236 1798 a_ok(a);
1799}
1800
1801static void acmd_checkchal(admin *a, unsigned ac, char *av[])
1802{
1803 base64_ctx b64;
1804 buf b;
1805 dstr d = DSTR_INIT;
1806
1807 base64_init(&b64);
1808 base64_decode(&b64, av[0], strlen(av[0]), &d);
1809 base64_decode(&b64, 0, 0, &d);
1810 buf_init(&b, d.buf, d.len);
1811 if (c_check(&b) || BBAD(&b) || BLEFT(&b))
f43df819 1812 a_fail(a, "invalid-challenge", A_END);
37941236 1813 else
1814 a_ok(a);
1815 dstr_destroy(&d);
1816}
1817
1818static void acmd_greet(admin *a, unsigned ac, char *av[])
1819{
1820 peer *p;
1821 base64_ctx b64;
1822 dstr d = DSTR_INIT;
1823
1824 if ((p = a_findpeer(a, av[0])) != 0) {
1825 base64_init(&b64);
1826 base64_decode(&b64, av[1], strlen(av[1]), &d);
1827 base64_decode(&b64, 0, 0, &d);
1828 p_greet(p, d.buf, d.len);
1829 dstr_destroy(&d);
1830 a_ok(a);
1831 }
1832}
1833
410c8acf 1834static void acmd_addr(admin *a, unsigned ac, char *av[])
1835{
1836 peer *p;
1837 const addr *ad;
1838
37941236 1839 if ((p = a_findpeer(a, av[0])) != 0) {
410c8acf 1840 ad = p_addr(p);
1841 assert(ad->sa.sa_family == AF_INET);
f43df819 1842 a_info(a, "?ADDR", ad, A_END);
3cdc3f3a 1843 a_ok(a);
410c8acf 1844 }
1845}
1846
060ca767 1847static void acmd_peerinfo(admin *a, unsigned ac, char *av[])
1848{
1849 peer *p;
1850 const peerspec *ps;
f3c6d21c 1851 const char *ptag;
060ca767 1852
37941236 1853 if ((p = a_findpeer(a, av[0])) != 0) {
1854 ps = p_spec(p);
f43df819 1855 a_info(a, "tunnel=%s", ps->tops->name, A_END);
fe2a5dcf
MW
1856 a_info(a, "key=%s", p_tag(p),
1857 "current-key=%s", p->kx.kpub->tag, A_END);
f3c6d21c
MW
1858 if ((ptag = p_privtag(p)) == 0) ptag = "(default)";
1859 a_info(a, "private-key=%s", ptag,
fe2a5dcf 1860 "current-private-key=%s", p->kx.kpriv->tag, A_END);
f43df819 1861 a_info(a, "keepalive=%lu", ps->t_ka, A_END);
37941236 1862 a_ok(a);
060ca767 1863 }
060ca767 1864}
1865
1866static void acmd_servinfo(admin *a, unsigned ac, char *av[])
1867{
f43df819
MW
1868 a_info(a, "implementation=edgeware-tripe", A_END);
1869 a_info(a, "version=%s", VERSION, A_END);
1870 a_info(a, "daemon=%s", BOOL(flags & F_DAEMON), A_END);
060ca767 1871 a_ok(a);
1872}
1873
c7053da9 1874static void acmd_stats(admin *a, unsigned ac, char *av[])
1875{
1876 peer *p;
1877 stats *st;
1878
37941236 1879 if ((p = a_findpeer(a, av[0])) == 0)
060ca767 1880 return;
060ca767 1881
1882 st = p_stats(p);
f43df819
MW
1883 a_info(a, "start-time=%s", timestr(st->t_start), A_END);
1884 a_info(a, "last-packet-time=%s", timestr(st->t_last), A_END);
1885 a_info(a, "last-keyexch-time=%s", timestr(st->t_kx), A_END);
18ad8913
MW
1886 a_info(a, "packets-in=%lu", st->n_in, "bytes-in=%lu", st->sz_in, A_END);
1887 a_info(a,
1888 "packets-out=%lu", st->n_out,
1889 "bytes-out=%lu", st->sz_out,
1890 A_END);
1891 a_info(a,
1892 "keyexch-packets-in=%lu", st->n_kxin,
1893 "keyexch-bytes-in=%lu", st->sz_kxin,
1894 A_END);
1895 a_info(a,
1896 "keyexch-packets-out=%lu", st->n_kxout,
1897 "keyexch-bytes-out=%lu", st->sz_kxout,
1898 A_END);
1899 a_info(a,
1900 "ip-packets-in=%lu", st->n_ipin,
1901 "ip-bytes-in=%lu", st->sz_ipin,
1902 A_END);
1903 a_info(a,
e04c2d50 1904 "ip-packets-out=%lu", st->n_ipout,
18ad8913
MW
1905 "ip-bytes-out=%lu", st->sz_ipout,
1906 A_END);
f43df819 1907 a_info(a, "rejected-packets=%lu", st->n_reject, A_END);
060ca767 1908 a_ok(a);
c7053da9 1909}
1910
410c8acf 1911static void acmd_kill(admin *a, unsigned ac, char *av[])
1912{
1913 peer *p;
f98df549 1914
37941236 1915 if ((p = a_findpeer(a, av[0])) != 0) {
410c8acf 1916 p_destroy(p);
3cdc3f3a 1917 a_ok(a);
410c8acf 1918 }
1919}
1920
0ba8de86 1921static void acmd_forcekx(admin *a, unsigned ac, char *av[])
1922{
1923 peer *p;
f98df549 1924
37941236 1925 if ((p = a_findpeer(a, av[0])) != 0) {
de014da6 1926 kx_start(&p->kx, 1);
0ba8de86 1927 a_ok(a);
1928 }
1929}
1930
de014da6 1931static void acmd_reload(admin *a, unsigned ac, char *av[])
1932 { p_keyreload(); a_ok(a); }
1933
410c8acf 1934static void acmd_quit(admin *a, unsigned ac, char *av[])
1935{
f43df819 1936 a_warn("SERVER", "quit", "admin-request", A_END);
3cdc3f3a 1937 a_ok(a);
410c8acf 1938 a_quit();
1939}
1940
3cdc3f3a 1941static void acmd_version(admin *a, unsigned ac, char *av[])
1942{
f43df819 1943 a_info(a, "%s", PACKAGE, "%s", VERSION, A_END);
3cdc3f3a 1944 a_ok(a);
1945}
1946
42da2a58 1947static void acmd_tunnels(admin *a, unsigned ac, char *av[])
1948{
1949 int i;
f98df549 1950
42da2a58 1951 for (i = 0; tunnels[i]; i++)
f43df819 1952 a_info(a, "%s", tunnels[i]->name, A_END);
42da2a58 1953 a_ok(a);
1954}
1955
410c8acf 1956/* --- The command table and help --- */
1957
1958typedef struct acmd {
1959 const char *name;
1960 const char *help;
1961 unsigned argmin, argmax;
1962 void (*func)(admin */*a*/, unsigned /*ac*/, char */*av*/[]);
1963} acmd;
1964
1965static void acmd_help(admin */*a*/, unsigned /*ac*/, char */*av*/[]);
1966
1967static const acmd acmdtab[] = {
9986f0b5 1968 { "add", "[OPTIONS] PEER ADDR ...", 2, 0xffff, acmd_add },
f43df819 1969 { "addr", "PEER", 1, 1, acmd_addr },
35c8b547 1970 { "algs", "[PEER]", 0, 1, acmd_algs },
ff92ffd3 1971 { "bgcancel", "TAG", 1, 1, acmd_bgcancel },
f43df819
MW
1972 { "checkchal", "CHAL", 1, 1, acmd_checkchal },
1973 { "daemon", 0, 0, 0, acmd_daemon },
1974 { "eping", "[OPTIONS] PEER", 1, 0xffff, acmd_eping },
1975 { "forcekx", "PEER", 1, 1, acmd_forcekx },
1976 { "getchal", 0, 0, 0, acmd_getchal },
1977 { "greet", "PEER CHAL", 2, 2, acmd_greet },
1978 { "help", 0, 0, 0, acmd_help },
1979 { "ifname", "PEER", 1, 1, acmd_ifname },
ff92ffd3 1980 { "jobs", 0, 0, 0, acmd_jobs },
f43df819
MW
1981 { "kill", "PEER", 1, 1, acmd_kill },
1982 { "list", 0, 0, 0, acmd_list },
1983 { "notify", "MESSAGE ...", 1, 0xffff, acmd_notify },
1984 { "peerinfo", "PEER", 1, 1, acmd_peerinfo },
1985 { "ping", "[OPTIONS] PEER", 1, 0xffff, acmd_ping },
1986 { "port", 0, 0, 0, acmd_port },
1987 { "quit", 0, 0, 0, acmd_quit },
1988 { "reload", 0, 0, 0, acmd_reload },
1989 { "servinfo", 0, 0, 0, acmd_servinfo },
64cf2223 1990 { "setifname", "PEER NEW-NAME", 2, 2, acmd_setifname },
be6a1b7a
MW
1991 { "svcclaim", "SERVICE VERSION", 2, 2, acmd_svcclaim },
1992 { "svcensure", "SERVICE [VERSION]", 1, 2, acmd_svcensure },
ef2f29c9
MW
1993 { "svcfail", "JOBID TOKENS...", 1, 0xffff, acmd_svcfail },
1994 { "svcinfo", "JOBID TOKENS...", 1, 0xffff, acmd_svcinfo },
be6a1b7a 1995 { "svclist", 0, 0, 0, acmd_svclist },
ef2f29c9 1996 { "svcok", "JOBID", 1, 1, acmd_svcok },
be6a1b7a
MW
1997 { "svcquery", "SERVICE", 1, 1, acmd_svcquery },
1998 { "svcrelease", "SERVICE", 1, 1, acmd_svcrelease },
ef2f29c9
MW
1999 { "svcsubmit", "[OPTIONS] SERVICE TOKENS...",
2000 2, 0xffff, acmd_svcsubmit },
f43df819 2001 { "stats", "PEER", 1, 1, acmd_stats },
de014da6 2002#ifndef NTRACE
f43df819 2003 { "trace", "[OPTIONS]", 0, 1, acmd_trace },
de014da6 2004#endif
f43df819
MW
2005 { "tunnels", 0, 0, 0, acmd_tunnels },
2006 { "version", 0, 0, 0, acmd_version },
2007 { "warn", "MESSAGE ...", 1, 0xffff, acmd_warn },
e04c2d50 2008 { "watch", "[OPTIONS]", 0, 1, acmd_watch },
410c8acf 2009 { 0, 0, 0, 0, 0 }
2010};
2011
2012static void acmd_help(admin *a, unsigned ac, char *av[])
2013{
2014 const acmd *c;
f98df549 2015
f43df819
MW
2016 for (c = acmdtab; c->name; c++) {
2017 if (c->help)
2018 a_info(a, "%s", c->name, "*%s", c->help, A_END);
e04c2d50 2019 else
f43df819
MW
2020 a_info(a, "%s", c->name, A_END);
2021 }
3cdc3f3a 2022 a_ok(a);
410c8acf 2023}
2024
2025/*----- Connection handling -----------------------------------------------*/
2026
c511e1f9 2027/* --- @a_destroypending@ --- *
410c8acf 2028 *
c511e1f9 2029 * Arguments: ---
fd3cf232 2030 *
2031 * Returns: ---
2032 *
c511e1f9 2033 * Use: Destroys pending admin connections at a safe time.
fd3cf232 2034 */
2035
c511e1f9 2036static void a_destroypending(void)
410c8acf 2037{
94593d1b 2038 admin *a, *aa, *head;
de014da6 2039 admin_bgop *bg, *bbg;
be6a1b7a 2040 admin_service *svc, *ssvc;
de014da6 2041
94593d1b
MW
2042 /* --- Destroy connections marked as pending --- *
2043 *
2044 * Slightly messy. Killing clients may cause others to finally die. Make
2045 * sure that they can be put on the list without clobbering anything or
2046 * getting lost.
2047 */
fd3cf232 2048
94593d1b
MW
2049 while (a_dead) {
2050 head = a_dead;
2051 a_dead = 0;
2052 for (a = head; a; a = aa) {
2053 aa = a->next;
2054 assert(a->f & AF_DEAD);
c511e1f9 2055
94593d1b 2056 /* --- Report what we're doing --- */
c511e1f9 2057
94593d1b
MW
2058 T( trace(T_ADMIN, "admin: completing destruction of connection %u",
2059 a->seq); )
c511e1f9 2060
94593d1b 2061 /* --- If this is the foreground client then shut down --- */
46dde080 2062
94593d1b
MW
2063 if (a->f & AF_FOREGROUND) {
2064 T( trace(T_ADMIN, "admin: foreground client quit: shutting down"); )
2065 a_warn("SERVER", "quit", "foreground-eof", A_END);
2066 a_quit();
2067 }
46dde080 2068
94593d1b 2069 /* --- Abort any background jobs in progress --- */
c511e1f9 2070
94593d1b
MW
2071 for (bg = a->bg; bg; bg = bbg) {
2072 bbg = bg->next;
2073 bg->cancel(bg);
2074 if (bg->tag) xfree(bg->tag);
2075 xfree(bg);
2076 }
c511e1f9 2077
94593d1b 2078 /* --- Release services I hold, and abort pending jobs --- */
be6a1b7a 2079
94593d1b
MW
2080 for (svc = a->svcs; svc; svc = ssvc) {
2081 ssvc = svc->next;
2082 a_svcrelease(svc);
2083 }
2084 a_jobtablefinal(&a->j);
be6a1b7a 2085
94593d1b 2086 /* --- Close file descriptors and selectory --- */
c511e1f9 2087
94593d1b
MW
2088 selbuf_destroy(&a->b);
2089 if (a->b.reader.fd != a->w.fd) close(a->b.reader.fd);
2090 close(a->w.fd);
2091 if (a_stdin == a) a_stdin = 0;
c511e1f9 2092
94593d1b 2093 /* --- Done --- */
c511e1f9 2094
94593d1b
MW
2095 DESTROY(a);
2096 }
410c8acf 2097 }
060ca767 2098}
2099
fd3cf232 2100/* --- @a_destroy@ --- *
2101 *
2102 * Arguments: @admin *a@ = pointer to an admin block
2103 *
2104 * Returns: ---
2105 *
2106 * Use: Destroys an admin block. This requires a certain amount of
2107 * care.
2108 */
2109
de014da6 2110static void freequeue(oqueue *q)
2111{
2112 obuf *o, *oo;
2113
2114 for (o = q->hd; o; o = oo) {
2115 oo = o->next;
2116 xfree(o);
2117 }
2118 q->hd = q->tl = 0;
2119}
2120
fd3cf232 2121static void a_destroy(admin *a)
2122{
fd3cf232 2123 if (a->f & AF_DEAD)
2124 return;
2125
c511e1f9
MW
2126 if (a->next) a->next->prev = a->prev;
2127 if (a->prev) a->prev->next = a->next;
2128 else admins = a->next;
fd3cf232 2129
c511e1f9 2130 if (a->out.hd) sel_rmfile(&a->w);
de014da6 2131 freequeue(&a->out);
fd3cf232 2132
c511e1f9
MW
2133 a->f |= AF_DEAD;
2134 a->next = a_dead;
2135 a_dead = a;
fd3cf232 2136
c511e1f9 2137 T( trace(T_ADMIN, "admin: killing connection %u", a->seq); )
fd3cf232 2138}
2139
410c8acf 2140/* --- @a_line@ --- *
2141 *
2142 * Arguments: @char *p@ = pointer to the line read
3008eabf 2143 * @size_t len@ = length of the line
410c8acf 2144 * @void *vp@ = pointer to my admin block
2145 *
2146 * Returns: ---
2147 *
2148 * Use: Handles a line of input.
2149 */
2150
3008eabf 2151static void a_line(char *p, size_t len, void *vp)
410c8acf 2152{
2153 admin *a = vp;
2154 const acmd *c;
6f88042a 2155 char *av[16 + 1];
410c8acf 2156 size_t ac;
2157
8d0c7a83 2158 TIMER;
fd3cf232 2159 if (a->f & AF_DEAD)
2160 return;
410c8acf 2161 if (!p) {
de014da6 2162 if (!a->bg)
2163 a_destroy(a);
2164 else {
2165 a->f |= AF_CLOSE;
2166 selbuf_disable(&a->b);
2167 }
410c8acf 2168 return;
2169 }
6f88042a 2170 ac = str_qsplit(p, av, N(av) - 1, 0, STRF_QUOTE);
410c8acf 2171 if (!ac)
2172 return;
6f88042a 2173 av[ac] = 0;
410c8acf 2174 for (c = acmdtab; c->name; c++) {
3cdc3f3a 2175 if (mystrieq(av[0], c->name)) {
410c8acf 2176 ac--;
f43df819
MW
2177 if (c->argmin > ac || ac > c->argmax) {
2178 if (!c->help)
2179 a_fail(a, "bad-syntax", "%s", c->name, "", A_END);
e04c2d50 2180 else
f43df819 2181 a_fail(a, "bad-syntax", "%s", c->name, "%s", c->help, A_END);
c511e1f9 2182 } else
410c8acf 2183 c->func(a, ac, av + 1);
2184 return;
2185 }
2186 }
f43df819 2187 a_fail(a, "unknown-command", "%s", av[0], A_END);
410c8acf 2188}
2189
2190/* --- @a_create@ --- *
2191 *
2192 * Arguments: @int fd_in, fd_out@ = file descriptors to use
3cdc3f3a 2193 * @unsigned f@ = initial flags to set
410c8acf 2194 *
2195 * Returns: ---
2196 *
2197 * Use: Creates a new admin connection.
2198 */
2199
3cdc3f3a 2200void a_create(int fd_in, int fd_out, unsigned f)
410c8acf 2201{
2202 admin *a = CREATE(admin);
3cdc3f3a 2203
93bee2c4 2204 T( static unsigned seq = 0;
2205 a->seq = seq++; )
410c8acf 2206 T( trace(T_ADMIN, "admin: accepted connection %u", a->seq); )
de014da6 2207 a->bg = 0;
042d5c20 2208 a->ref = 0;
be6a1b7a 2209 a->svcs = 0;
5d46c0f8 2210 a_jobtableinit(&a->j);
3cdc3f3a 2211 a->f = f;
de014da6 2212 if (fd_in == STDIN_FILENO) a_stdin = a;
fd3cf232 2213 fdflags(fd_in, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
410c8acf 2214 if (fd_out != fd_in)
fd3cf232 2215 fdflags(fd_out, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
410c8acf 2216 selbuf_init(&a->b, &sel, fd_in, a_line, a);
fd3cf232 2217 sel_initfile(&sel, &a->w, fd_out, SEL_WRITE, a_flush, a);
de014da6 2218 a->out.hd = a->out.tl = 0;
410c8acf 2219 a->next = admins;
2220 a->prev = 0;
de014da6 2221 if (admins) admins->prev = a;
410c8acf 2222 admins = a;
2223}
2224
2225/* --- @a_accept@ --- *
2226 *
2227 * Arguments: @int fd@ = file descriptor to accept
2228 * @unsigned mode@ = what to do
2229 * @void *v@ = uninteresting pointer
2230 *
2231 * Returns: ---
2232 *
2233 * Use: Accepts a new admin connection.
2234 */
2235
2236static void a_accept(int fd, unsigned mode, void *v)
2237{
2238 int nfd;
2239 struct sockaddr_un sun;
2240 size_t sz = sizeof(sun);
2241
2242 if ((nfd = accept(fd, (struct sockaddr *)&sun, &sz)) < 0) {
1ecee057 2243 if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK &&
2244 errno != ECONNABORTED && errno != EPROTO)
f43df819 2245 a_warn("ADMIN", "accept-error", "?ERRNO", A_END);
410c8acf 2246 return;
2247 }
3cdc3f3a 2248 a_create(nfd, nfd, 0);
410c8acf 2249}
2250
c511e1f9
MW
2251/* --- @a_preselect@ --- *
2252 *
2253 * Arguments: ---
2254 *
2255 * Returns: ---
2256 *
2257 * Use: Informs the admin module that we're about to select again,
2258 * and that it should do cleanup things it has delayed until a
2259 * `safe' time.
2260 */
2261
2262void a_preselect(void) { if (a_dead) a_destroypending(); }
2263
410c8acf 2264/* --- @a_daemon@ --- *
2265 *
2266 * Arguments: ---
2267 *
2268 * Returns: ---
2269 *
2270 * Use: Informs the admin module that it's a daemon.
2271 */
2272
f98df549 2273void a_daemon(void) { flags |= F_DAEMON; }
410c8acf 2274
2275/* --- @a_init@ --- *
2276 *
2277 * Arguments: @const char *name@ = socket name to create
aa2405e8
MW
2278 * @uid_t u@ = user to own the socket
2279 * @gid_t g@ = group to own the socket
a9279e37 2280 * @mode_t m@ = permissions to set on the socket
410c8acf 2281 *
2282 * Returns: ---
2283 *
2284 * Use: Creates the admin listening socket.
2285 */
2286
a9279e37 2287void a_init(const char *name, uid_t u, gid_t g, mode_t m)
410c8acf 2288{
2289 int fd;
2290 int n = 5;
2291 struct sockaddr_un sun;
2292 struct sigaction sa;
2293 size_t sz;
879483d7 2294 mode_t omask;
410c8acf 2295
be6a1b7a
MW
2296 /* --- Create services table --- */
2297
2298 sym_create(&a_svcs);
2299
410c8acf 2300 /* --- Set up the socket address --- */
2301
2302 sz = strlen(name) + 1;
2303 if (sz > sizeof(sun.sun_path))
2304 die(EXIT_FAILURE, "socket name `%s' too long", name);
2305 BURN(sun);
2306 sun.sun_family = AF_UNIX;
2307 memcpy(sun.sun_path, name, sz);
2308 sz += offsetof(struct sockaddr_un, sun_path);
2309
2310 /* --- Attempt to bind to the socket --- */
2311
879483d7 2312 omask = umask(0077);
410c8acf 2313again:
2314 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
2315 die(EXIT_FAILURE, "couldn't create socket: %s", strerror(errno));
2316 if (bind(fd, (struct sockaddr *)&sun, sz) < 0) {
2317 struct stat st;
2318 int e = errno;
2319 if (errno != EADDRINUSE) {
2320 die(EXIT_FAILURE, "couldn't bind to address `%s': %s",
2321 sun.sun_path, strerror(e));
2322 }
2323 if (!n)
2324 die(EXIT_FAILURE, "too many retries; giving up");
2325 n--;
2326 if (!connect(fd, (struct sockaddr *)&sun, sz)) {
2327 die(EXIT_FAILURE, "server already listening on admin socket `%s'",
2328 sun.sun_path);
2329 }
2330 if (errno != ECONNREFUSED)
2331 die(EXIT_FAILURE, "couldn't bind to address: %s", strerror(e));
2332 if (stat(sun.sun_path, &st)) {
2333 die(EXIT_FAILURE, "couldn't stat `%s': %s",
2334 sun.sun_path, strerror(errno));
2335 }
2336 if (!S_ISSOCK(st.st_mode))
2337 die(EXIT_FAILURE, "object `%s' isn't a socket", sun.sun_path);
c7053da9 2338 T( trace(T_ADMIN, "admin: stale socket found; removing it"); )
410c8acf 2339 unlink(sun.sun_path);
2340 close(fd);
2341 goto again;
2342 }
aa2405e8 2343 if (chown(sun.sun_path, u, g)) {
9e0161ce
MW
2344 die(EXIT_FAILURE, "failed to set socket owner: %s",
2345 strerror(errno));
aa2405e8 2346 }
a9279e37
MW
2347 if (chmod(sun.sun_path, m)) {
2348 die(EXIT_FAILURE, "failed to set socket permissions: %s",
2349 strerror(errno));
2350 }
879483d7 2351 umask(omask);
410c8acf 2352 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
2353 if (listen(fd, 5))
2354 die(EXIT_FAILURE, "couldn't listen on socket: %s", strerror(errno));
2355
2356 /* --- Listen to the socket --- */
2357
2358 sel_initfile(&sel, &sock, fd, SEL_READ, a_accept, 0);
2359 sel_addfile(&sock);
2360 sockname = name;
2361 bres_init(&sel);
2362 T( trace_custom(a_trace, 0);
2363 trace(T_ADMIN, "admin: enabled custom tracing"); )
2364 flags |= F_INIT;
2365
2366 /* --- Set up signal handlers --- */
2367
2368 sig_add(&s_term, SIGTERM, a_sigdie, 0);
2369 sig_add(&s_hup, SIGHUP, a_sighup, 0);
3cdc3f3a 2370 signal(SIGPIPE, SIG_IGN);
410c8acf 2371 sigaction(SIGINT, 0, &sa);
2372 if (sa.sa_handler != SIG_IGN)
2373 sig_add(&s_int, SIGINT, a_sigdie, 0);
2374}
2375
2376/*----- That's all, folks -------------------------------------------------*/