chiark / gitweb /
server/admin.c: Use the more modern mLib `codec' classes for Base64.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 2 Sep 2017 18:55:09 +0000 (19:55 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 14 Jun 2018 09:34:25 +0000 (10:34 +0100)
The main effect, other than making the code slightly briefer, is that
decoding is now significantly stricter.

server/admin.c
server/tripe-admin.5.in
server/tripe.h

index 2d1658eee513b3cf700d7f8e29b231751f3b8082..b493a36b259c7e84b15036df9f9142eff417e860 100644 (file)
@@ -284,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++);
@@ -1789,35 +1786,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[])
index df87b70926e4d256c1bf7c874a4f4c6a708d9c89..c220b4b4c40dff2a55bf12912b82f44fc2eeeff8 100644 (file)
@@ -942,6 +942,10 @@ server is already running as a daemon.
 (For commands accepting socket addresses.)  The address couldn't be
 understood.
 .SP
+.BI "bad-base64 " message
+(For commands accepting Base64-encoded input.)  The Base64-encoded
+string was invalid.
+.SP
 .BI "bad-syntax " cmd " " message
 (For any command.)  The command couldn't be understood: e.g., the number
 of arguments was wrong.
index 0004f17ee4d4a84ed6325c0c2f3627cd68e63c40..6978bf285ed2974a28c8a2712be155f84cee9c7e 100644 (file)
@@ -66,6 +66,7 @@
 #include <mLib/arena.h>
 #include <mLib/base64.h>
 #include <mLib/bres.h>
+#include <mLib/codec.h>
 #include <mLib/daemonize.h>
 #include <mLib/dstr.h>
 #include <mLib/env.h>