X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/blobdiff_plain/5b9f3d3788bafcba79c893b1afc6a1c77bc77d20..813d2de98ea1bc45a68c3262c1df87f0834bd0ab:/server/admin.c diff --git a/server/admin.c b/server/admin.c index eb965c1c..e39658ef 100644 --- a/server/admin.c +++ b/server/admin.c @@ -9,19 +9,18 @@ * * This file is part of Trivial IP Encryption (TrIPE). * - * TrIPE is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * TrIPE is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your + * option) any later version. * - * TrIPE is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * TrIPE is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. * * You should have received a copy of the GNU General Public License - * along with TrIPE; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * along with TrIPE. If not, see . */ /*----- Header files ------------------------------------------------------*/ @@ -285,14 +284,11 @@ void a_vformat(dstr *d, const char *fmt, va_list *ap) } else if (strcmp(fmt, "?B64") == 0) { const octet *p = va_arg(*ap, const octet *); size_t n = va_arg(*ap, size_t); - base64_ctx b64; + codec *b64 = base64_class.encoder(CDCF_NOEQPAD, "", 0); dstr_putc(d, ' '); - base64_init(&b64); - b64.indent = ""; - b64.maxline = 0; - base64_encode(&b64, p, n, d); - base64_encode(&b64, 0, 0, d); - while (d->len && d->buf[d->len - 1] == '=') d->len--; + b64->ops->code(b64, p, n, d); + b64->ops->code(b64, 0, 0, d); + b64->ops->destroy(b64); } else if (strcmp(fmt, "?TOKENS") == 0) { const char *const *av = va_arg(*ap, const char *const *); while (*av) u_quotify(d, *av++); @@ -1024,12 +1020,14 @@ static void a_resolved(struct hostent *h, void *v) { admin_resop *r = v; - T( trace(T_ADMIN, "admin: resop %s resolved", BGTAG(r)); ) QUICKRAND; if (!h) { + T( trace(T_ADMIN, "admin: resop %s failed: %s", + BGTAG(r), hstrerror(h_errno)); ) a_bgfail(&r->bg, "resolve-error", "%s", r->addr, A_END); r->func(r, ARES_FAIL); } else { + T( trace(T_ADMIN, "admin: resop %s ok", BGTAG(r)); ) memcpy(&r->sa.sin.sin_addr, h->h_addr, sizeof(struct in_addr)); r->func(r, ARES_OK); } @@ -1114,7 +1112,6 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag, goto fail; } r->sa.sin.sin_family = AF_INET; - r->sasz = sizeof(r->sa.sin); r->addr = xstrdup(av[i]); if (!av[i + 1]) pt = TRIPE_PORT; @@ -1227,7 +1224,6 @@ static void a_doadd(admin_resop *r, int rc) T( trace(T_ADMIN, "admin: done add op %s", BGTAG(add)); ) if (rc == ARES_OK) { - add->peer.sasz = add->r.sasz; add->peer.sa = add->r.sa; if (p_findbyaddr(&add->r.sa)) a_bgfail(&add->r.bg, "peer-addr-exists", "?ADDR", &add->r.sa, A_END); @@ -1240,6 +1236,7 @@ static void a_doadd(admin_resop *r, int rc) } if (add->peer.tag) xfree(add->peer.tag); + if (add->peer.privtag) xfree(add->peer.privtag); xfree(add->peer.name); } @@ -1289,14 +1286,12 @@ static void acmd_add(admin *a, unsigned ac, char *av[]) OPTTIME("-keepalive", t, { add->peer.t_ka = t; }) OPT("-cork", { add->peer.f |= KXF_CORK; }) OPTARG("-key", arg, { - if (add->peer.tag) - xfree(add->peer.tag); + if (add->peer.tag) xfree(add->peer.tag); add->peer.tag = xstrdup(arg); }) OPT("-mobile", { add->peer.f |= PSF_MOBILE; }) OPTARG("-priv", arg, { - if (add->peer.privtag) - xfree(add->peer.privtag); + if (add->peer.privtag) xfree(add->peer.privtag); add->peer.privtag = xstrdup(arg); }) }); @@ -1673,7 +1668,24 @@ static void acmd_warn(admin *a, unsigned ac, char *av[]) { alertcmd(a, AF_WARN, AF_WARN, "WARN", av); } static void acmd_port(admin *a, unsigned ac, char *av[]) - { a_info(a, "%u", p_port(), A_END); a_ok(a); } +{ + int i; + + if (ac) { + for (i = 0; i < NADDRFAM; i++) + if (mystrieq(av[0], aftab[i].name)) goto found; + a_fail(a, "unknown-address-family", "%s", av[0], A_END); + return; + found: + assert(udpsock[i].fd >= 0); + } else { + for (i = 0; i < NADDRFAM; i++) + if (udpsock[i].fd >= 0) goto found; + abort(); + } + a_info(a, "%u", p_port(i), A_END); + a_ok(a); +} static void acmd_daemon(admin *a, unsigned ac, char *av[]) { @@ -1790,35 +1802,43 @@ static void acmd_getchal(admin *a, unsigned ac, char *av[]) static void acmd_checkchal(admin *a, unsigned ac, char *av[]) { - base64_ctx b64; + codec *b64 = base64_class.decoder(CDCF_NOEQPAD); + int err; buf b; dstr d = DSTR_INIT; - base64_init(&b64); - base64_decode(&b64, av[0], strlen(av[0]), &d); - base64_decode(&b64, 0, 0, &d); - buf_init(&b, d.buf, d.len); - if (c_check(&b) || BBAD(&b) || BLEFT(&b)) - a_fail(a, "invalid-challenge", A_END); - else - a_ok(a); + if ((err = b64->ops->code(b64, av[0], strlen(av[0]), &d)) != 0 || + (err = b64->ops->code(b64, 0, 0, &d)) != 0) + a_fail(a, "bad-base64", "%s", codec_strerror(err), A_END); + else { + buf_init(&b, d.buf, d.len); + if (c_check(&b) || BBAD(&b) || BLEFT(&b)) + a_fail(a, "invalid-challenge", A_END); + else + a_ok(a); + } + b64->ops->destroy(b64); dstr_destroy(&d); } static void acmd_greet(admin *a, unsigned ac, char *av[]) { peer *p; - base64_ctx b64; + int err; + codec *b64; dstr d = DSTR_INIT; - if ((p = a_findpeer(a, av[0])) != 0) { - base64_init(&b64); - base64_decode(&b64, av[1], strlen(av[1]), &d); - base64_decode(&b64, 0, 0, &d); + if ((p = a_findpeer(a, av[0])) == 0) return; + b64 = base64_class.decoder(CDCF_NOEQPAD); + if ((err = b64->ops->code(b64, av[1], strlen(av[1]), &d)) != 0 || + (err = b64->ops->code(b64, 0, 0, &d)) != 0) + a_fail(a, "bad-base64", "%s", codec_strerror(err), A_END); + else { p_greet(p, d.buf, d.len); - dstr_destroy(&d); a_ok(a); } + b64->ops->destroy(b64); + dstr_destroy(&d); } static void acmd_addr(admin *a, unsigned ac, char *av[]) @@ -1849,6 +1869,9 @@ static void acmd_peerinfo(admin *a, unsigned ac, char *av[]) a_info(a, "private-key=%s", ptag, "current-private-key=%s", p->kx.kpriv->tag, A_END); a_info(a, "keepalive=%lu", ps->t_ka, A_END); + a_info(a, "corked=%s", BOOL(p->kx.f&KXF_CORK), + "mobile=%s", BOOL(ps->f&PSF_MOBILE), + A_END); a_ok(a); } } @@ -1973,11 +1996,12 @@ static const acmd acmdtab[] = { { "notify", "MESSAGE ...", 1, 0xffff, acmd_notify }, { "peerinfo", "PEER", 1, 1, acmd_peerinfo }, { "ping", "[OPTIONS] PEER", 1, 0xffff, acmd_ping }, - { "port", 0, 0, 0, acmd_port }, + { "port", "[FAMILY]", 0, 1, acmd_port }, { "quit", 0, 0, 0, acmd_quit }, { "reload", 0, 0, 0, acmd_reload }, { "servinfo", 0, 0, 0, acmd_servinfo }, { "setifname", "PEER NEW-NAME", 2, 2, acmd_setifname }, + { "stats", "PEER", 1, 1, acmd_stats }, { "svcclaim", "SERVICE VERSION", 2, 2, acmd_svcclaim }, { "svcensure", "SERVICE [VERSION]", 1, 2, acmd_svcensure }, { "svcfail", "JOBID TOKENS...", 1, 0xffff, acmd_svcfail }, @@ -1988,7 +2012,6 @@ static const acmd acmdtab[] = { { "svcrelease", "SERVICE", 1, 1, acmd_svcrelease }, { "svcsubmit", "[OPTIONS] SERVICE TOKENS...", 2, 0xffff, acmd_svcsubmit }, - { "stats", "PEER", 1, 1, acmd_stats }, #ifndef NTRACE { "trace", "[OPTIONS]", 0, 1, acmd_trace }, #endif @@ -2357,7 +2380,6 @@ again: sig_add(&s_term, SIGTERM, a_sigdie, 0); sig_add(&s_hup, SIGHUP, a_sighup, 0); - signal(SIGPIPE, SIG_IGN); sigaction(SIGINT, 0, &sa); if (sa.sa_handler != SIG_IGN) sig_add(&s_int, SIGINT, a_sigdie, 0);