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