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