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