X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/blobdiff_plain/d167fc1b2599ab06f857e984fc203fd7f64f4c0a..010e6f63edc4be4e127c18fc6c2c2a138efc93d2:/server/admin.c diff --git a/server/admin.c b/server/admin.c index 797b4786..73c71070 100644 --- a/server/admin.c +++ b/server/admin.c @@ -1,13 +1,11 @@ /* -*-c-*- * - * $Id$ - * * Admin interface for configuration * * (c) 2001 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Trivial IP Encryption (TrIPE). * @@ -15,12 +13,12 @@ * 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 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. @@ -231,34 +229,6 @@ static void a_flush(int fd, unsigned mode, void *v) /*----- Utility functions -------------------------------------------------*/ -/* --- @quotify@ --- * - * - * Arguments: @dstr *d@ = where to write the answer - * @const char *p@ = string to quotify - * - * Returns: --- - * - * Use: Quotes the given string if necessary, according to our - * quoting rules. - */ - -static void quotify(dstr *d, const char *p) -{ - if (d->len) - dstr_putc(d, ' '); - if (*p && !p[strcspn(p, "\"' \t\n\v")]) - dstr_puts(d, p); - else { - dstr_putc(d, '\"'); - while (*p) { - if (*p == '\\' || *p == '\"') - dstr_putc(d, '\\'); - dstr_putc(d, *p++); - } - dstr_putc(d, '\"'); - } -} - /* --- @a_vformat@ --- * * * Arguments: @dstr *d@ = where to leave the formatted message @@ -283,8 +253,8 @@ static void a_vformat(dstr *d, const char *fmt, va_list ap) const addr *a = va_arg(ap, const addr *); switch (a->sa.sa_family) { case AF_INET: - quotify(d, "INET"); - quotify(d, inet_ntoa(a->sin.sin_addr)); + u_quotify(d, "INET"); + u_quotify(d, inet_ntoa(a->sin.sin_addr)); dstr_putf(d, " %u", (unsigned)ntohs(a->sin.sin_port)); break; default: @@ -303,19 +273,19 @@ static void a_vformat(dstr *d, const char *fmt, va_list ap) while (d->len && d->buf[d->len - 1] == '=') d->len--; } else if (strcmp(fmt, "?TOKENS") == 0) { const char *const *av = va_arg(ap, const char *const *); - while (*av) quotify(d, *av++); + while (*av) u_quotify(d, *av++); } else if (strcmp(fmt, "?PEER") == 0) - quotify(d, p_name(va_arg(ap, peer *))); + u_quotify(d, p_name(va_arg(ap, peer *))); else if (strcmp(fmt, "?ERRNO") == 0) { dstr_putf(d, " E%d", errno); - quotify(d, strerror(errno)); + u_quotify(d, strerror(errno)); } else abort(); } else { if (*fmt == '!') fmt++; DRESET(&dd); dstr_vputf(&dd, fmt, &ap); - quotify(d, dd.buf); + u_quotify(d, dd.buf); } fmt = va_arg(ap, const char *); } @@ -344,7 +314,7 @@ static void a_vwrite(admin *a, const char *status, const char *tag, if (tag) dstr_puts(&d, "BG"); dstr_puts(&d, status); - if (tag) quotify(&d, tag); + if (tag) u_quotify(&d, tag); a_vformat(&d, fmt, ap); dstr_putc(&d, '\n'); dosend(a, d.buf, d.len); @@ -413,7 +383,7 @@ static void a_rawalert(unsigned f_and, unsigned f_eq, const char *status, { admin *a, *aa; dstr d = DSTR_INIT; - + if (!(flags & F_INIT)) return; dstr_puts(&d, status); @@ -595,10 +565,10 @@ static long a_parsetime(const char *p) case 'h': t *= 60; case 'm': t *= 60; case 's': if (q[1] != 0) - default: t = -1; + default: t = -1; case 0: break; } - return (t); + return (t); } /* --- @a_findpeer@ --- * @@ -1014,7 +984,7 @@ static void a_resolved(struct hostent *h, void *v) sel_rmtimer(&r->t); xfree(r->addr); a_bgrelease(&r->bg); -} +} /* --- @a_restimer@ --- * * @@ -1087,21 +1057,25 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag, r->addr = 0; r->func = func; if (mystrieq(av[i], "inet")) i++; - if (ac - i != 2) { - a_fail(a, "bad-addr-syntax", "[inet] ADDRESS PORT", A_END); + if (ac - i != 1 && ac - i != 2) { + a_fail(a, "bad-addr-syntax", "[inet] ADDRESS [PORT]", A_END); goto fail; } r->sa.sin.sin_family = AF_INET; r->sasz = sizeof(r->sa.sin); r->addr = xstrdup(av[i]); - pt = strtoul(av[i + 1], &p, 0); - if (*p) { - struct servent *s = getservbyname(av[i + 1], "udp"); - if (!s) { - a_fail(a, "unknown-service", "%s", av[i + 1], A_END); - goto fail; + if (!av[i + i]) + pt = TRIPE_PORT; + else { + pt = strtoul(av[i + 1], &p, 0); + if (*p) { + struct servent *s = getservbyname(av[i + 1], "udp"); + if (!s) { + a_fail(a, "unknown-service", "%s", av[i + 1], A_END); + goto fail; + } + pt = ntohs(s->s_port); } - pt = ntohs(s->s_port); } if (pt == 0 || pt >= 65536) { a_fail(a, "invalid-port", "%lu", pt, A_END); @@ -1121,7 +1095,7 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag, a->seq, BGTAG(r), r->addr); ) /* --- If the name is numeric, do it the easy way --- */ - + if (inet_aton(av[i], &r->sa.sin.sin_addr)) { T( trace(T_ADMIN, "admin: resop %s done the easy way", BGTAG(r)); ) func(r, ARES_OK); @@ -1236,6 +1210,7 @@ static void acmd_add(admin *a, unsigned ac, char *av[]) add->peer.name = 0; add->peer.t_ka = 0; add->peer.tops = tun_default; + add->peer.kxf = 0; /* --- Parse options --- */ @@ -1255,11 +1230,12 @@ static void acmd_add(admin *a, unsigned ac, char *av[]) } }) OPTTIME("-keepalive", t, { add->peer.t_ka = t; }) + OPT("-cork", { add->peer.kxf |= KXF_CORK; }) }); /* --- Make sure someone's not got there already --- */ - if (!*av) + if (!av[0] || !av[1]) goto bad_syntax; if (p_find(*av)) { a_fail(a, "peer-exists", "%s", *av, A_END); @@ -1377,7 +1353,7 @@ static void a_ping(admin *a, unsigned ac, char *av[], a_bgrelease(&pg->bg); } return; - + bad_syntax: a_fail(a, "bad-syntax", "%s", cmd, "[OPTIONS] PEER", cmd, A_END); fail: @@ -1593,7 +1569,7 @@ static int traceish(admin *a, unsigned ac, char *av[], } a_fail(a, "bad-%s-option", what, "%c", *p, A_END); return (0); - tropt_ok:; + tropt_ok:; break; } p++; @@ -1637,7 +1613,7 @@ static void acmd_daemon(admin *a, unsigned ac, char *av[]) a_notify("DAEMON", A_END); if (a_stdin) a_destroy(a_stdin); - if (u_daemon()) + if (daemonize()) a_fail(a, "daemon-error", "?ERRNO", A_END); else { flags |= F_DAEMON; @@ -1697,7 +1673,7 @@ static void acmd_setifname(admin *a, unsigned ac, char *av[]) a_notify("NEWIFNAME", "?PEER", p, "%s", p_ifname(p), "%s", av[1], A_END); p_setifname(p, av[1]); a_ok(a); - } + } } static void acmd_getchal(admin *a, unsigned ac, char *av[]) @@ -1807,7 +1783,7 @@ static void acmd_stats(admin *a, unsigned ac, char *av[]) "ip-bytes-in=%lu", st->sz_ipin, A_END); a_info(a, - "ip-packets-out=%lu", st->n_ipout, + "ip-packets-out=%lu", st->n_ipout, "ip-bytes-out=%lu", st->sz_ipout, A_END); a_info(a, "rejected-packets=%lu", st->n_reject, A_END); @@ -1910,7 +1886,7 @@ static const acmd acmdtab[] = { { "tunnels", 0, 0, 0, acmd_tunnels }, { "version", 0, 0, 0, acmd_version }, { "warn", "MESSAGE ...", 1, 0xffff, acmd_warn }, - { "watch", "[OPTIONS]", 0, 1, acmd_watch }, + { "watch", "[OPTIONS]", 0, 1, acmd_watch }, { 0, 0, 0, 0, 0 } }; @@ -1921,7 +1897,7 @@ static void acmd_help(admin *a, unsigned ac, char *av[]) for (c = acmdtab; c->name; c++) { if (c->help) a_info(a, "%s", c->name, "*%s", c->help, A_END); - else + else a_info(a, "%s", c->name, A_END); } a_ok(a); @@ -2068,7 +2044,7 @@ static void a_line(char *p, size_t len, void *vp) if (c->argmin > ac || ac > c->argmax) { if (!c->help) a_fail(a, "bad-syntax", "%s", c->name, "", A_END); - else + else a_fail(a, "bad-syntax", "%s", c->name, "%s", c->help, A_END); } else c->func(a, ac, av + 1);