410c8acf |
1 | /* -*-c-*- |
2 | * |
37075862 |
3 | * $Id: admin.c,v 1.8 2003/04/06 10:25:17 mdw Exp $ |
410c8acf |
4 | * |
5 | * Admin interface for configuration |
6 | * |
7 | * (c) 2001 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Trivial IP Encryption (TrIPE). |
13 | * |
14 | * TrIPE is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * TrIPE is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with TrIPE; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: admin.c,v $ |
37075862 |
32 | * Revision 1.8 2003/04/06 10:25:17 mdw |
33 | * Support Linux TUN/TAP device. Fix some bugs. |
34 | * |
3008eabf |
35 | * Revision 1.7 2002/01/13 14:57:33 mdw |
36 | * Track @lbuf@ and @dstr_vputf@ changes in mLib. |
37 | * |
fd3cf232 |
38 | * Revision 1.6 2001/02/19 19:11:09 mdw |
39 | * Output buffering on admin connections. |
40 | * |
c7053da9 |
41 | * Revision 1.5 2001/02/16 21:22:51 mdw |
42 | * Support for displaying statistics. Make client connections blocking, so |
43 | * that things don't get dropped. (This might change again if I add |
44 | * buffering.) |
45 | * |
7fadfa0d |
46 | * Revision 1.4 2001/02/06 09:34:53 mdw |
47 | * Change ERR response to FAIL for consistency with other programs. |
48 | * |
93bee2c4 |
49 | * Revision 1.3 2001/02/04 01:17:12 mdw |
50 | * The `DAEMON' notification to stdout is replaced by a warning. The |
51 | * `DAEMON' and `QUIT' command send `OK' on successful completion. Put |
52 | * assignment of sequence number in a T(...) guard. |
53 | * |
8d0c7a83 |
54 | * Revision 1.2 2001/02/03 22:40:29 mdw |
55 | * Put timer information into the entropy pool when packets are received |
56 | * and on similar events. Reseed the generator on the interval timer. |
57 | * |
410c8acf |
58 | * Revision 1.1 2001/02/03 20:26:37 mdw |
59 | * Initial checkin. |
60 | * |
61 | */ |
62 | |
63 | /*----- Header files ------------------------------------------------------*/ |
64 | |
65 | #include "tripe.h" |
66 | |
67 | /*----- Global variables --------------------------------------------------*/ |
68 | |
69 | #ifndef NTRACE |
70 | |
71 | const trace_opt tr_opts[] = { |
72 | { 't', T_TUNNEL, "tunnel events" }, |
73 | { 'r', T_PEER, "peer events" }, |
74 | { 'a', T_ADMIN, "admin interface" }, |
75 | { 'p', T_PACKET, "packet contents" }, |
76 | { 'c', T_CRYPTO, "crypto details" }, |
77 | { 's', T_KEYSET, "symmetric keyset management" }, |
78 | { 'x', T_KEYEXCH, "key exchange" }, |
79 | { 'm', T_KEYMGMT, "key management" }, |
80 | { 'A', T_ALL, "all of the above" }, |
81 | { 0, 0, 0 } |
82 | }; |
83 | |
84 | unsigned tr_flags = 0; |
85 | #endif |
86 | |
87 | /*----- Static variables --------------------------------------------------*/ |
88 | |
89 | static admin *admins; |
90 | static sel_file sock; |
91 | static const char *sockname; |
92 | static unsigned flags = 0; |
93 | static admin *a_stdin = 0; |
94 | static sig s_term, s_int, s_hup; |
95 | |
96 | #define F_DAEMON 1u |
97 | #define F_INIT 2u |
98 | |
99 | #define T_RESOLVE SEC(30) |
100 | |
fd3cf232 |
101 | static void a_destroy(admin */*a*/); |
102 | static void a_lock(admin */*a*/); |
103 | static void a_unlock(admin */*a*/); |
104 | |
105 | /*----- Output functions --------------------------------------------------*/ |
106 | |
107 | /* --- @trywrite@ --- * |
108 | * |
109 | * Arguments: @admin *a@ = pointer to an admin block |
110 | * @const char *p@ = pointer to buffer to write |
111 | * @size_t sz@ = size of data to write |
112 | * |
113 | * Returns: The number of bytes written, or less than zero on error. |
114 | * |
115 | * Use: Attempts to write data to a client. |
116 | */ |
117 | |
118 | static ssize_t trywrite(admin *a, const char *p, size_t sz) |
119 | { |
120 | ssize_t n, done = 0; |
121 | |
122 | again: |
123 | if (!sz) |
124 | return (done); |
125 | n = write(a->w.fd, p, sz); |
126 | if (n > 0) { |
127 | done += n; |
128 | p += n; |
129 | sz -= n; |
130 | goto again; |
131 | } |
132 | if (n < 0) { |
133 | if (errno == EINTR) |
134 | goto again; |
135 | if (errno != EAGAIN && errno != EWOULDBLOCK) { |
136 | a_destroy(a); |
137 | a_warn("disconnecting admin client due to write errors: %s", |
138 | strerror(errno)); |
139 | return (-1); |
140 | } |
141 | } |
142 | return (done); |
143 | } |
144 | |
145 | /* --- @dosend@ --- * |
146 | * |
147 | * Arguemnts: @admin *a@ = pointer to an admin block |
148 | * @const char *p@ = pointer to buffer to write |
149 | * @size_t sz@ = size of data to write |
150 | * |
151 | * Returns: --- |
152 | * |
153 | * Use: Sends data to an admin client. |
154 | */ |
155 | |
156 | static void dosend(admin *a, const char *p, size_t sz) |
157 | { |
158 | ssize_t n; |
159 | obuf *o; |
160 | |
161 | if (a->f & AF_DEAD) |
162 | return; |
163 | |
164 | /* --- Try to send the data immediately --- */ |
165 | |
166 | if (!a->o_head) { |
167 | if ((n = trywrite(a, p, sz)) < 0) |
168 | return; |
169 | p += n; |
170 | sz -= n; |
171 | if (!sz) |
172 | return; |
173 | } |
174 | |
175 | /* --- Fill buffers with the data until it's all gone --- */ |
176 | |
177 | o = a->o_tail; |
178 | if (!o) |
179 | sel_addfile(&a->w); |
180 | else if (o->p_in < o->buf + OBUFSZ) |
181 | goto noalloc; |
182 | |
183 | do { |
184 | o = xmalloc(sizeof(obuf)); |
185 | o->next = 0; |
186 | o->p_in = o->p_out = o->buf; |
187 | if (a->o_tail) |
188 | a->o_tail->next = o; |
189 | else |
190 | a->o_head = o; |
191 | a->o_tail = o; |
192 | |
193 | noalloc: |
194 | n = o->buf + OBUFSZ - o->p_in; |
195 | if (n > sz) |
196 | n = sz; |
197 | memcpy(o->p_in, p, n); |
198 | o->p_in += n; |
199 | p += n; |
200 | sz -= n; |
201 | } while (sz); |
202 | } |
203 | |
204 | /* --- @a_flush@ --- * |
205 | * |
206 | * Arguments: @int fd@ = file descriptor |
207 | * @unsigned mode@ = what's happening |
208 | * @void *v@ = pointer to my admin block |
209 | * |
210 | * Returns: --- |
211 | * |
212 | * Use: Flushes buffers when a client is ready to read again. |
213 | */ |
214 | |
215 | static void a_flush(int fd, unsigned mode, void *v) |
216 | { |
217 | admin *a = v; |
218 | obuf *o, *oo; |
219 | ssize_t n; |
220 | |
221 | o = a->o_head; |
222 | while (o) { |
223 | if ((n = trywrite(a, o->p_out, o->p_in - o->p_out)) < 0) |
224 | return; |
225 | o->p_out += n; |
226 | if (o->p_in < o->p_out) |
227 | break; |
228 | oo = o; |
229 | o = o->next; |
230 | xfree(oo); |
231 | } |
232 | a->o_head = o; |
233 | if (!o) { |
234 | a->o_tail = 0; |
235 | sel_rmfile(&a->w); |
236 | } |
237 | } |
238 | |
410c8acf |
239 | /*----- Utility functions -------------------------------------------------*/ |
240 | |
241 | /* --- @a_write@ --- * |
242 | * |
243 | * Arguments: @admin *a@ = admin connection to write to |
244 | * @const char *fmt@ = pointer to format string |
245 | * @...@ = other arguments |
246 | * |
247 | * Returns: --- |
248 | * |
249 | * Use: Sends a message to an admin connection. |
250 | */ |
251 | |
252 | static void a_write(admin *a, const char *fmt, ...) |
253 | { |
254 | va_list ap; |
255 | dstr d = DSTR_INIT; |
256 | va_start(ap, fmt); |
3008eabf |
257 | dstr_vputf(&d, fmt, &ap); |
410c8acf |
258 | va_end(ap); |
fd3cf232 |
259 | dosend(a, d.buf, d.len); |
410c8acf |
260 | dstr_destroy(&d); |
261 | } |
262 | |
263 | /* --- @a_warn@ --- * |
264 | * |
265 | * Arguments: @const char *fmt@ = pointer to format string |
266 | * @...@ = other arguments |
267 | * |
268 | * Returns: --- |
269 | * |
270 | * Use: Informs all admin connections of a warning. |
271 | */ |
272 | |
273 | void a_warn(const char *fmt, ...) |
274 | { |
275 | va_list ap; |
fd3cf232 |
276 | admin *a, *aa; |
410c8acf |
277 | dstr d = DSTR_INIT; |
278 | |
279 | if (flags & F_INIT) |
280 | dstr_puts(&d, "WARN "); |
281 | va_start(ap, fmt); |
37075862 |
282 | dstr_vputf(&d, fmt, &ap); |
410c8acf |
283 | va_end(ap); |
284 | if (!(flags & F_INIT)) |
285 | moan("%s", d.buf); |
286 | else { |
287 | dstr_putc(&d, '\n'); |
fd3cf232 |
288 | for (a = admins; a; a = aa) { |
289 | aa = a->next; |
290 | dosend(a, d.buf, d.len); |
291 | } |
410c8acf |
292 | } |
293 | dstr_destroy(&d); |
294 | } |
295 | |
296 | /* --- @a_trace@ --- * |
297 | * |
298 | * Arguments: @const char *p@ = pointer to a buffer |
299 | * @size_t sz@ = size of the buffer |
300 | * @void *v@ = uninteresting pointer |
301 | * |
302 | * Returns: --- |
303 | * |
304 | * Use: Custom trace output handler. |
305 | */ |
306 | |
307 | #ifndef NTRACE |
308 | static void a_trace(const char *p, size_t sz, void *v) |
309 | { |
310 | dstr d = DSTR_INIT; |
fd3cf232 |
311 | admin *a, *aa; |
410c8acf |
312 | |
313 | dstr_puts(&d, "TRACE "); |
314 | dstr_putm(&d, p, sz); |
315 | dstr_putc(&d, '\n'); |
fd3cf232 |
316 | for (a = admins; a; a = aa) { |
317 | aa = a->next; |
318 | dosend(a, d.buf, d.len); |
319 | } |
410c8acf |
320 | dstr_destroy(&d); |
321 | } |
322 | #endif |
323 | |
324 | /* --- @a_quit@ --- * |
325 | * |
326 | * Arguments: --- |
327 | * |
328 | * Returns: --- |
329 | * |
330 | * Use: Shuts things down nicely. |
331 | */ |
332 | |
333 | void a_quit(void) |
334 | { |
335 | close(sock.fd); |
336 | unlink(sockname); |
337 | exit(0); |
338 | } |
339 | |
340 | /* --- @a_sigdie@ --- * |
341 | * |
342 | * Arguments: @int sig@ = signal number |
343 | * @void *v@ = an uninteresting argument |
344 | * |
345 | * Returns: --- |
346 | * |
347 | * Use Shuts down on receipt of a fatal signal. |
348 | */ |
349 | |
350 | static void a_sigdie(int sig, void *v) |
351 | { |
352 | char *p; |
353 | char buf[20]; |
354 | |
355 | switch (sig) { |
356 | case SIGTERM: p = "SIGTERM"; break; |
357 | case SIGINT: p = "SIGINT"; break; |
358 | default: |
359 | sprintf(buf, "signal %i", sig); |
360 | p = buf; |
361 | break; |
362 | } |
363 | a_warn("shutting down on %s", p); |
364 | a_quit(); |
365 | } |
366 | |
367 | /* --- @a_sighup@ --- * |
368 | * |
369 | * Arguments: @int sig@ = signal number |
370 | * @void *v@ = an uninteresting argument |
371 | * |
372 | * Returns: --- |
373 | * |
374 | * Use Logs a message about SIGHUP not being useful. |
375 | */ |
376 | |
377 | static void a_sighup(int sig, void *v) |
378 | { |
379 | a_warn("received SIGHUP: ignoring"); |
380 | } |
381 | |
382 | /*----- Adding peers ------------------------------------------------------*/ |
383 | |
384 | /* --- @a_resolve@ --- * |
385 | * |
386 | * Arguments: @struct hostent *h@ = pointer to resolved hostname |
387 | * @void *v@ = pointer to admin block |
388 | * |
389 | * Returns: --- |
390 | * |
391 | * Use: Handles a completed name resolution. |
392 | */ |
393 | |
394 | static void a_resolve(struct hostent *h, void *v) |
395 | { |
396 | admin *a = v; |
fd3cf232 |
397 | |
398 | a_lock(a); |
410c8acf |
399 | T( trace(T_ADMIN, "admin: %u resolved", a->seq); ) |
8d0c7a83 |
400 | TIMER; |
410c8acf |
401 | sel_rmtimer(&a->t); |
402 | if (!h) |
7fadfa0d |
403 | a_write(a, "FAIL couldn't resolve hostname `%s'\n", a->paddr); |
410c8acf |
404 | else if (p_find(a->pname)) |
7fadfa0d |
405 | a_write(a, "FAIL peer `%s' already registered\n", a->pname); |
410c8acf |
406 | else { |
407 | memcpy(&a->peer.sin.sin_addr, h->h_addr, sizeof(struct in_addr)); |
408 | if (!p_create(a->pname, &a->peer.sa, a->sasz)) |
7fadfa0d |
409 | a_write(a, "FAIL couldn't create peer\n"); |
410c8acf |
410 | else |
411 | a_write(a, "OK\n"); |
412 | } |
413 | xfree(a->pname); |
414 | xfree(a->paddr); |
415 | a->pname = 0; |
416 | selbuf_enable(&a->b); |
fd3cf232 |
417 | a_unlock(a); |
410c8acf |
418 | } |
419 | |
420 | /* --- @a_timer@ --- * |
421 | * |
422 | * Arguments: @struct timeval *tv@ = timer |
423 | * @void *v@ = pointer to admin block |
424 | * |
425 | * Returns: --- |
426 | * |
427 | * Use: Times out a resolver. |
428 | */ |
429 | |
430 | static void a_timer(struct timeval *tv, void *v) |
431 | { |
432 | admin *a = v; |
fd3cf232 |
433 | |
434 | a_lock(a); |
410c8acf |
435 | T( trace(T_ADMIN, "admin: %u resolver timeout", a->seq); ) |
436 | bres_abort(&a->r); |
7fadfa0d |
437 | a_write(a, "FAIL timeout resolving `%s'\n", a->paddr); |
410c8acf |
438 | xfree(a->pname); |
439 | xfree(a->paddr); |
440 | a->pname = 0; |
441 | selbuf_enable(&a->b); |
fd3cf232 |
442 | a_unlock(a); |
410c8acf |
443 | } |
444 | |
445 | /* --- @acmd_add@ --- * |
446 | * |
447 | * Arguments: @admin *a@ = connection which requested the addition |
448 | * @unsigned ac@ = argument count |
449 | * @char *av[]@ = pointer to the argument list |
450 | * |
451 | * Returns: --- |
452 | * |
453 | * Use: Adds a new peer. |
454 | */ |
455 | |
456 | static void acmd_add(admin *a, unsigned ac, char *av[]) |
457 | { |
458 | unsigned long pt; |
459 | struct timeval tv; |
460 | char *p; |
461 | |
462 | /* --- Make sure someone's not got there already --- */ |
463 | |
464 | if (p_find(av[0])) { |
7fadfa0d |
465 | a_write(a, "FAIL peer `%s' already registered\n", av[0]); |
410c8acf |
466 | return; |
467 | } |
468 | |
469 | /* --- Fill in the easy bits of address --- */ |
470 | |
471 | BURN(a->peer); |
472 | a->peer.sin.sin_family = AF_INET; |
473 | a->sasz = sizeof(a->peer.sin); |
474 | pt = strtoul(av[2], &p, 0); |
475 | if (*p) { |
476 | struct servent *s = getservbyname(av[2], "udp"); |
477 | if (!s) { |
7fadfa0d |
478 | a_write(a, "FAIL service `%s' not known\n", av[2]); |
410c8acf |
479 | return; |
480 | } |
481 | pt = ntohs(s->s_port); |
482 | } |
483 | if (pt == 0 || pt >= 65536) { |
7fadfa0d |
484 | a_write(a, "FAIL bad port number %lu\n", pt); |
410c8acf |
485 | return; |
486 | } |
487 | a->peer.sin.sin_port = htons(pt); |
488 | |
489 | /* --- If the name is numeric, do it the easy way --- */ |
490 | |
491 | if (inet_aton(av[1], &a->peer.sin.sin_addr)) { |
492 | if (!p_create(av[0], &a->peer.sa, a->sasz)) |
7fadfa0d |
493 | a_write(a, "FAIL couldn't create peer\n"); |
410c8acf |
494 | else |
495 | a_write(a, "OK\n"); |
496 | return; |
497 | } |
498 | |
499 | /* --- Store everything for later and crank up the resolver --- * |
500 | * |
501 | * We disable the line buffer until the resolver completes (or times out). |
502 | * This prevents other commands on the same connection (though the rest of |
503 | * the system continues regardless), but makes life simpler for the client. |
504 | */ |
505 | |
506 | a->pname = xstrdup(av[0]); |
507 | a->paddr = xstrdup(av[1]); |
508 | selbuf_disable(&a->b); |
509 | gettimeofday(&tv, 0); |
510 | tv.tv_sec += T_RESOLVE; |
511 | sel_addtimer(&sel, &a->t, &tv, a_timer, a); |
512 | bres_byname(&a->r, a->paddr, a_resolve, a); |
513 | T( trace(T_ADMIN, "admin: %u resolving hostname `%s'", |
514 | a->seq, a->paddr); ) |
515 | } |
516 | |
517 | /*----- Administration commands -------------------------------------------*/ |
518 | |
519 | /* --- Miscellaneous commands --- */ |
520 | |
521 | #ifndef NTRACE |
522 | |
523 | static void acmd_trace(admin *a, unsigned ac, char *av[]) |
524 | { |
525 | if (!ac || strcmp(av[0], "?") == 0) { |
526 | const trace_opt *t; |
527 | a_write(a, "INFO Trace options:\n"); |
528 | for (t = tr_opts; t->ch; t++) { |
529 | a_write(a, "INFO %c %c %s\n", |
530 | t->ch, (tr_flags & t->f) == t->f ? '*' : ' ', t->help); |
531 | } |
532 | } else { |
533 | unsigned sense = 1; |
534 | unsigned f = tr_flags; |
535 | const trace_opt *tt; |
536 | char *p = av[0]; |
537 | |
538 | while (*p) { |
539 | switch (*p) { |
540 | case '+': sense = 1; break; |
541 | case '-': sense = 0; break; |
542 | default: |
543 | for (tt = tr_opts; tt->ch; tt++) { |
544 | if (tt->ch == *p) { |
545 | if (sense) f |= tt->f; |
546 | else f &= ~tt->f; |
547 | goto tropt_ok; |
548 | } |
549 | } |
7fadfa0d |
550 | a_write(a, "FAIL unknown trace option `%c'\n", *p); |
410c8acf |
551 | return; |
552 | tropt_ok:; |
553 | break; |
554 | } |
555 | p++; |
556 | } |
557 | tr_flags = f; |
558 | trace_level(tr_flags); |
559 | } |
560 | a_write(a, "OK\n"); |
561 | } |
562 | |
563 | #endif |
564 | |
565 | static void acmd_port(admin *a, unsigned ac, char *av[]) |
566 | { |
567 | a_write(a, "INFO %u\nOK\n", p_port()); |
568 | } |
569 | |
410c8acf |
570 | static void acmd_daemon(admin *a, unsigned ac, char *av[]) |
571 | { |
572 | if (flags & F_DAEMON) |
7fadfa0d |
573 | a_write(a, "FAIL already running as a daemon\n"); |
410c8acf |
574 | else { |
93bee2c4 |
575 | a_warn("becoming a daemon"); |
576 | if (a_stdin) |
410c8acf |
577 | a_destroy(a_stdin); |
410c8acf |
578 | if (u_daemon()) |
7fadfa0d |
579 | a_write(a, "FAIL error becoming a daemon: %s", strerror(errno)); |
93bee2c4 |
580 | else { |
410c8acf |
581 | flags |= F_DAEMON; |
93bee2c4 |
582 | a_write(a, "OK\n"); |
583 | } |
410c8acf |
584 | } |
585 | } |
586 | |
587 | static void acmd_list(admin *a, unsigned ac, char *av[]) |
588 | { |
589 | peer *p; |
590 | for (p = p_first(); p; p = p_next(p)) |
591 | a_write(a, "INFO %s\n", p_name(p)); |
592 | a_write(a, "OK\n"); |
593 | } |
594 | |
595 | static void acmd_ifname(admin *a, unsigned ac, char *av[]) |
596 | { |
597 | peer *p; |
598 | |
599 | if ((p = p_find(av[0])) == 0) |
7fadfa0d |
600 | a_write(a, "FAIL peer `%s' not found\n", av[0]); |
410c8acf |
601 | else |
602 | a_write(a, "INFO %s\nOK\n", p_ifname(p)); |
603 | } |
604 | |
605 | static void acmd_addr(admin *a, unsigned ac, char *av[]) |
606 | { |
607 | peer *p; |
608 | const addr *ad; |
609 | |
610 | if ((p = p_find(av[0])) == 0) |
7fadfa0d |
611 | a_write(a, "FAIL peer `%s' not found\n", av[0]); |
410c8acf |
612 | else { |
613 | ad = p_addr(p); |
614 | assert(ad->sa.sa_family == AF_INET); |
615 | a_write(a, "INFO %s %u\nOK\n", |
616 | inet_ntoa(ad->sin.sin_addr), |
617 | (unsigned)ntohs(ad->sin.sin_port)); |
618 | } |
619 | } |
620 | |
c7053da9 |
621 | static void acmd_stats(admin *a, unsigned ac, char *av[]) |
622 | { |
623 | peer *p; |
624 | stats *st; |
625 | |
626 | if ((p = p_find(av[0])) == 0) |
627 | a_write(a, "FAIL peer `%s' not found\n", av[0]); |
628 | else { |
629 | st = p_stats(p); |
630 | a_write(a, "INFO start-time=%s\n", timestr(st->t_start)); |
631 | a_write(a, "INFO last-packet-time=%s\n", timestr(st->t_last)); |
632 | a_write(a, "INFO packets-in=%lu bytes-in=%lu\n", st->n_in, st->sz_in); |
633 | a_write(a, "INFO packets-out=%lu bytes-out=%lu\n", |
634 | st->n_out, st->sz_out); |
635 | a_write(a, "INFO keyexch-packets-in=%lu keyexch-bytes-in=%lu\n", |
636 | st->n_kxin, st->sz_kxin); |
637 | a_write(a, "INFO keyexch-packets-out=%lu keyexch-bytes-out=%lu\n", |
638 | st->n_kxout, st->sz_kxout); |
639 | a_write(a, "INFO ip-packets-in=%lu ip-bytes-in=%lu\n", |
640 | st->n_ipin, st->sz_ipin); |
641 | a_write(a, "INFO ip-packets-out=%lu ip-bytes-out=%lu\n", |
642 | st->n_ipout, st->sz_ipout); |
643 | a_write(a, "INFO rejected-packets=%lu\n", st->n_reject); |
644 | a_write(a, "OK\n"); |
645 | } |
646 | } |
647 | |
410c8acf |
648 | static void acmd_kill(admin *a, unsigned ac, char *av[]) |
649 | { |
650 | peer *p; |
651 | if ((p = p_find(av[0])) == 0) |
7fadfa0d |
652 | a_write(a, "FAIL peer `%s' not found\n", av[0]); |
410c8acf |
653 | else { |
654 | p_destroy(p); |
655 | a_write(a, "OK\n"); |
656 | } |
657 | } |
658 | |
659 | static void acmd_quit(admin *a, unsigned ac, char *av[]) |
660 | { |
661 | a_warn("closing down on admin request"); |
93bee2c4 |
662 | a_write(a, "OK\n"); |
410c8acf |
663 | a_quit(); |
664 | } |
665 | |
666 | /* --- The command table and help --- */ |
667 | |
668 | typedef struct acmd { |
669 | const char *name; |
670 | const char *help; |
671 | unsigned argmin, argmax; |
672 | void (*func)(admin */*a*/, unsigned /*ac*/, char */*av*/[]); |
673 | } acmd; |
674 | |
675 | static void acmd_help(admin */*a*/, unsigned /*ac*/, char */*av*/[]); |
676 | |
677 | static const acmd acmdtab[] = { |
678 | { "help", "HELP", 0, 0, acmd_help }, |
679 | #ifndef NTRACE |
680 | { "trace", "TRACE [options]", 0, 1, acmd_trace }, |
681 | #endif |
682 | { "port", "PORT", 0, 0, acmd_port }, |
683 | { "daemon", "DAEMON", 0, 0, acmd_daemon }, |
684 | { "list", "LIST", 0, 0, acmd_list }, |
685 | { "ifname", "IFNAME peer", 1, 1, acmd_ifname }, |
686 | { "addr", "ADDR peer", 1, 1, acmd_addr }, |
c7053da9 |
687 | { "stats", "STATS peer", 1, 1, acmd_stats }, |
410c8acf |
688 | { "kill", "KILL peer", 1, 1, acmd_kill }, |
689 | { "add", "ADD peer addr port", 3, 3, acmd_add }, |
690 | { "quit", "QUIT", 0, 0, acmd_quit }, |
691 | { 0, 0, 0, 0, 0 } |
692 | }; |
693 | |
694 | static void acmd_help(admin *a, unsigned ac, char *av[]) |
695 | { |
696 | const acmd *c; |
697 | for (c = acmdtab; c->name; c++) |
698 | a_write(a, "INFO %s\n", c->help); |
699 | a_write(a, "OK\n"); |
700 | } |
701 | |
702 | /*----- Connection handling -----------------------------------------------*/ |
703 | |
fd3cf232 |
704 | /* --- @a_lock@ --- * |
410c8acf |
705 | * |
706 | * Arguments: @admin *a@ = pointer to an admin block |
707 | * |
708 | * Returns: --- |
709 | * |
fd3cf232 |
710 | * Use: Locks an admin block so that it won't be destroyed |
711 | * immediately. |
410c8acf |
712 | */ |
713 | |
fd3cf232 |
714 | static void a_lock(admin *a) { assert(!(a->f & AF_LOCK)); a->f |= AF_LOCK; } |
715 | |
716 | /* --- @a_unlock@ --- * |
717 | * |
718 | * Arguments: @admin *a@ = pointer to an admin block |
719 | * |
720 | * Returns: --- |
721 | * |
722 | * Use: Unlocks an admin block, allowing its destruction. This is |
723 | * also the second half of @a_destroy@. |
724 | */ |
725 | |
726 | static void a_unlock(admin *a) |
410c8acf |
727 | { |
fd3cf232 |
728 | assert(a->f & AF_LOCK); |
729 | if (!(a->f & AF_DEAD)) { |
730 | a->f &= ~AF_LOCK; |
731 | return; |
732 | } |
733 | |
734 | T( trace(T_ADMIN, "admin: completing destruction of connection %u", |
735 | a->seq); ) |
736 | |
410c8acf |
737 | selbuf_destroy(&a->b); |
410c8acf |
738 | if (a->pname) { |
739 | xfree(a->pname); |
740 | xfree(a->paddr); |
741 | bres_abort(&a->r); |
742 | sel_rmtimer(&a->t); |
743 | } |
fd3cf232 |
744 | if (a->b.reader.fd != a->w.fd) |
745 | close(a->b.reader.fd); |
746 | close(a->w.fd); |
747 | |
748 | if (a_stdin == a) |
749 | a_stdin = 0; |
410c8acf |
750 | if (a->next) |
751 | a->next->prev = a->prev; |
752 | if (a->prev) |
753 | a->prev->next = a->next; |
754 | else |
755 | admins = a->next; |
410c8acf |
756 | DESTROY(a); |
757 | } |
758 | |
fd3cf232 |
759 | /* --- @a_destroy@ --- * |
760 | * |
761 | * Arguments: @admin *a@ = pointer to an admin block |
762 | * |
763 | * Returns: --- |
764 | * |
765 | * Use: Destroys an admin block. This requires a certain amount of |
766 | * care. |
767 | */ |
768 | |
769 | static void a_destroy(admin *a) |
770 | { |
771 | /* --- Don't multiply destroy admin blocks --- */ |
772 | |
773 | if (a->f & AF_DEAD) |
774 | return; |
775 | |
776 | /* --- Make sure nobody expects it to work --- */ |
777 | |
778 | a->f |= AF_DEAD; |
779 | T( trace(T_ADMIN, "admin: destroying connection %u", a->seq); ) |
780 | |
781 | /* --- Free the output buffers --- */ |
782 | |
783 | if (a->o_head) { |
784 | obuf *o, *oo; |
785 | sel_rmfile(&a->w); |
786 | for (o = a->o_head; o; o = oo) { |
787 | oo = o->next; |
788 | xfree(o); |
789 | } |
790 | a->o_head = 0; |
791 | } |
792 | |
793 | /* --- If the block is locked, that's all we can manage --- */ |
794 | |
795 | if (a->f & AF_LOCK) { |
796 | T( trace(T_ADMIN, "admin: deferring destruction..."); ) |
797 | return; |
798 | } |
799 | a->f |= AF_LOCK; |
800 | a_unlock(a); |
801 | } |
802 | |
410c8acf |
803 | /* --- @a_line@ --- * |
804 | * |
805 | * Arguments: @char *p@ = pointer to the line read |
3008eabf |
806 | * @size_t len@ = length of the line |
410c8acf |
807 | * @void *vp@ = pointer to my admin block |
808 | * |
809 | * Returns: --- |
810 | * |
811 | * Use: Handles a line of input. |
812 | */ |
813 | |
3008eabf |
814 | static void a_line(char *p, size_t len, void *vp) |
410c8acf |
815 | { |
816 | admin *a = vp; |
817 | const acmd *c; |
818 | char *av[4]; |
819 | size_t ac; |
820 | |
8d0c7a83 |
821 | TIMER; |
fd3cf232 |
822 | if (a->f & AF_DEAD) |
823 | return; |
410c8acf |
824 | if (!p) { |
825 | a_destroy(a); |
826 | return; |
827 | } |
828 | ac = str_qsplit(p, av, 4, 0, STRF_QUOTE); |
829 | if (!ac) |
830 | return; |
831 | for (p = av[0]; *p; p++) *p = tolower((unsigned char)*p); |
832 | for (c = acmdtab; c->name; c++) { |
833 | if (strcmp(av[0], c->name) == 0) { |
834 | ac--; |
835 | if (c->argmin > ac || ac > c->argmax) |
7fadfa0d |
836 | a_write(a, "FAIL syntax: %s\n", c->help); |
fd3cf232 |
837 | else { |
838 | a_lock(a); |
410c8acf |
839 | c->func(a, ac, av + 1); |
fd3cf232 |
840 | a_unlock(a); |
841 | } |
410c8acf |
842 | return; |
843 | } |
844 | } |
7fadfa0d |
845 | a_write(a, "FAIL unknown command `%s'\n", av[0]); |
410c8acf |
846 | } |
847 | |
848 | /* --- @a_create@ --- * |
849 | * |
850 | * Arguments: @int fd_in, fd_out@ = file descriptors to use |
851 | * |
852 | * Returns: --- |
853 | * |
854 | * Use: Creates a new admin connection. |
855 | */ |
856 | |
857 | void a_create(int fd_in, int fd_out) |
858 | { |
859 | admin *a = CREATE(admin); |
93bee2c4 |
860 | T( static unsigned seq = 0; |
861 | a->seq = seq++; ) |
410c8acf |
862 | T( trace(T_ADMIN, "admin: accepted connection %u", a->seq); ) |
863 | a->pname = 0; |
fd3cf232 |
864 | a->f = 0; |
410c8acf |
865 | if (fd_in == STDIN_FILENO) |
866 | a_stdin = a; |
fd3cf232 |
867 | fdflags(fd_in, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
410c8acf |
868 | if (fd_out != fd_in) |
fd3cf232 |
869 | fdflags(fd_out, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
410c8acf |
870 | selbuf_init(&a->b, &sel, fd_in, a_line, a); |
fd3cf232 |
871 | sel_initfile(&sel, &a->w, fd_out, SEL_WRITE, a_flush, a); |
872 | a->o_head = 0; |
873 | a->o_tail = 0; |
410c8acf |
874 | a->next = admins; |
875 | a->prev = 0; |
876 | if (admins) |
877 | admins->prev = a; |
878 | admins = a; |
879 | } |
880 | |
881 | /* --- @a_accept@ --- * |
882 | * |
883 | * Arguments: @int fd@ = file descriptor to accept |
884 | * @unsigned mode@ = what to do |
885 | * @void *v@ = uninteresting pointer |
886 | * |
887 | * Returns: --- |
888 | * |
889 | * Use: Accepts a new admin connection. |
890 | */ |
891 | |
892 | static void a_accept(int fd, unsigned mode, void *v) |
893 | { |
894 | int nfd; |
895 | struct sockaddr_un sun; |
896 | size_t sz = sizeof(sun); |
897 | |
898 | if ((nfd = accept(fd, (struct sockaddr *)&sun, &sz)) < 0) { |
899 | a_warn("accept admin connection failed: %s", strerror(errno)); |
900 | return; |
901 | } |
902 | a_create(nfd, nfd); |
903 | } |
904 | |
905 | /* --- @a_daemon@ --- * |
906 | * |
907 | * Arguments: --- |
908 | * |
909 | * Returns: --- |
910 | * |
911 | * Use: Informs the admin module that it's a daemon. |
912 | */ |
913 | |
914 | void a_daemon(void) |
915 | { |
916 | flags |= F_DAEMON; |
917 | } |
918 | |
919 | /* --- @a_init@ --- * |
920 | * |
921 | * Arguments: @const char *name@ = socket name to create |
922 | * |
923 | * Returns: --- |
924 | * |
925 | * Use: Creates the admin listening socket. |
926 | */ |
927 | |
928 | void a_init(const char *name) |
929 | { |
930 | int fd; |
931 | int n = 5; |
932 | struct sockaddr_un sun; |
933 | struct sigaction sa; |
934 | size_t sz; |
935 | |
936 | /* --- Set up the socket address --- */ |
937 | |
938 | sz = strlen(name) + 1; |
939 | if (sz > sizeof(sun.sun_path)) |
940 | die(EXIT_FAILURE, "socket name `%s' too long", name); |
941 | BURN(sun); |
942 | sun.sun_family = AF_UNIX; |
943 | memcpy(sun.sun_path, name, sz); |
944 | sz += offsetof(struct sockaddr_un, sun_path); |
945 | |
946 | /* --- Attempt to bind to the socket --- */ |
947 | |
948 | umask(0077); |
949 | again: |
950 | if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) |
951 | die(EXIT_FAILURE, "couldn't create socket: %s", strerror(errno)); |
952 | if (bind(fd, (struct sockaddr *)&sun, sz) < 0) { |
953 | struct stat st; |
954 | int e = errno; |
955 | if (errno != EADDRINUSE) { |
956 | die(EXIT_FAILURE, "couldn't bind to address `%s': %s", |
957 | sun.sun_path, strerror(e)); |
958 | } |
959 | if (!n) |
960 | die(EXIT_FAILURE, "too many retries; giving up"); |
961 | n--; |
962 | if (!connect(fd, (struct sockaddr *)&sun, sz)) { |
963 | die(EXIT_FAILURE, "server already listening on admin socket `%s'", |
964 | sun.sun_path); |
965 | } |
966 | if (errno != ECONNREFUSED) |
967 | die(EXIT_FAILURE, "couldn't bind to address: %s", strerror(e)); |
968 | if (stat(sun.sun_path, &st)) { |
969 | die(EXIT_FAILURE, "couldn't stat `%s': %s", |
970 | sun.sun_path, strerror(errno)); |
971 | } |
972 | if (!S_ISSOCK(st.st_mode)) |
973 | die(EXIT_FAILURE, "object `%s' isn't a socket", sun.sun_path); |
c7053da9 |
974 | T( trace(T_ADMIN, "admin: stale socket found; removing it"); ) |
410c8acf |
975 | unlink(sun.sun_path); |
976 | close(fd); |
977 | goto again; |
978 | } |
979 | chmod(sun.sun_path, 0600); |
980 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
981 | if (listen(fd, 5)) |
982 | die(EXIT_FAILURE, "couldn't listen on socket: %s", strerror(errno)); |
983 | |
984 | /* --- Listen to the socket --- */ |
985 | |
986 | sel_initfile(&sel, &sock, fd, SEL_READ, a_accept, 0); |
987 | sel_addfile(&sock); |
988 | sockname = name; |
989 | bres_init(&sel); |
990 | T( trace_custom(a_trace, 0); |
991 | trace(T_ADMIN, "admin: enabled custom tracing"); ) |
992 | flags |= F_INIT; |
993 | |
994 | /* --- Set up signal handlers --- */ |
995 | |
996 | sig_add(&s_term, SIGTERM, a_sigdie, 0); |
997 | sig_add(&s_hup, SIGHUP, a_sighup, 0); |
998 | sigaction(SIGINT, 0, &sa); |
999 | if (sa.sa_handler != SIG_IGN) |
1000 | sig_add(&s_int, SIGINT, a_sigdie, 0); |
1001 | } |
1002 | |
1003 | /*----- That's all, folks -------------------------------------------------*/ |