From 0ed0735f38c78e11cdf1c2b095ba754a92d2d201 Mon Sep 17 00:00:00 2001 Message-Id: <0ed0735f38c78e11cdf1c2b095ba754a92d2d201.1715298318.git.mdw@distorted.org.uk> From: Mark Wooding Date: Mon, 1 Jan 2007 15:46:36 +0000 Subject: [PATCH] tripectl: Quotify the arguments rather than hoping for the best. Organization: Straylight/Edgeware From: Mark Wooding To make this work, move quotify from admin to the shared utilities. --- client/tripectl.c | 7 ++----- common/util.c | 30 ++++++++++++++++++++++++++++++ common/util.h | 13 +++++++++++++ doc/tripectl.1 | 5 +++-- server/admin.c | 42 +++++++----------------------------------- 5 files changed, 55 insertions(+), 42 deletions(-) diff --git a/client/tripectl.c b/client/tripectl.c index 7386b304..ea8aadff 100644 --- a/client/tripectl.c +++ b/client/tripectl.c @@ -532,11 +532,8 @@ int main(int argc, char *argv[]) if (optind < argc) { dstr d = DSTR_INIT; setup((f & f_warn) ? "WATCH -A+w" : "WATCH -A"); - dstr_puts(&d, argv[optind++]); - while (optind < argc) { - dstr_putc(&d, ' '); - dstr_puts(&d, argv[optind++]); - } + while (optind < argc) + u_quotify(&d, argv[optind++]); dstr_putc(&d, '\n'); errno = EIO; if (write(fd, d.buf, d.len) != d.len || shutdown(fd, 1)) diff --git a/common/util.c b/common/util.c index ceeb0bd5..4b32985c 100644 --- a/common/util.c +++ b/common/util.c @@ -37,12 +37,42 @@ #include #include +#include + #include "util.h" #include /*----- Main code ---------------------------------------------------------*/ +/* --- @u_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. + */ + +void u_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, '\"'); + } +} + /* --- @u_detach@ --- * * * Arguments: --- diff --git a/common/util.h b/common/util.h index 675f0efe..0c135480 100644 --- a/common/util.h +++ b/common/util.h @@ -35,6 +35,19 @@ /*----- Functions provided ------------------------------------------------*/ +/* --- @u_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. + */ + +extern void u_quotify(dstr */*d*/, const char */*p*/); + /* --- @u_detach@ --- * * * Arguments: --- diff --git a/doc/tripectl.1 b/doc/tripectl.1 index c0f57a46..379fcda1 100644 --- a/doc/tripectl.1 +++ b/doc/tripectl.1 @@ -185,8 +185,9 @@ fatal signal or an end-of-file indication from the server. .SS "Use from scripts" If arguments are given to .BR tripectl , -they are concatenated with spaces between and submitted to the server -after connection. Any +they are quoted if necessary to protect spaces and other special +characters, concatenated with spaces between, and submitted to the +server after connection. Any .B INFO responses returned by the server are written to standard output (without the diff --git a/server/admin.c b/server/admin.c index 797b4786..4dbaf33e 100644 --- a/server/admin.c +++ b/server/admin.c @@ -231,34 +231,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 +255,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 +275,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 +316,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); -- [mdw]