From f616c63b399fed4f78121f387be9599e8e0c2b4c Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sun, 12 Mar 2017 18:50:27 +0100 Subject: [PATCH] imap4d: simplify auth with encodefmt (backport from 9front) --- sys/src/cmd/upas/imap4d/auth.c | 12 ++---------- sys/src/cmd/upas/imap4d/imap4d.c | 4 ++++ sys/src/cmd/upas/imap4d/msg.c | 30 ------------------------------ 3 files changed, 6 insertions(+), 40 deletions(-) diff --git a/sys/src/cmd/upas/imap4d/auth.c b/sys/src/cmd/upas/imap4d/auth.c index dad825bfd..748174f52 100644 --- a/sys/src/cmd/upas/imap4d/auth.c +++ b/sys/src/cmd/upas/imap4d/auth.c @@ -151,19 +151,13 @@ char* cramauth(void) { char *s, *t; - int n; AuthInfo *ai; Chalstate *cs; if((cs = auth_challenge("proto=cram role=server")) == nil) return Ebadch; - n = cs->nchal; - s = binalloc(&parsebin, n * 2, 0); - n = enc64(s, n * 2, (uchar*)cs->chal, n); - Bprint(&bout, "+ "); - Bwrite(&bout, s, n); - Bprint(&bout, "\r\n"); + Bprint(&bout, "+ %.*[\r\n", cs->nchal, cs->chal); if(Bflush(&bout) < 0) writeerr(); @@ -221,7 +215,6 @@ passauth(char *u, char *secret) { char response[2*MD5dlen + 1]; uchar digest[MD5dlen]; - int i; AuthInfo *ai; Chalstate *cs; @@ -229,8 +222,7 @@ passauth(char *u, char *secret) return Ebadch; hmac_md5((uchar*)cs->chal, strlen(cs->chal), (uchar*)secret, strlen(secret), digest, nil); - for(i = 0; i < MD5dlen; i++) - snprint(response + 2*i, sizeof response - 2*i, "%2.2ux", digest[i]); + snprint(response, sizeof(response), "%.*H", MD5dlen, digest); cs->user = u; cs->resp = response; cs->nresp = strlen(response); diff --git a/sys/src/cmd/upas/imap4d/imap4d.c b/sys/src/cmd/upas/imap4d/imap4d.c index 94fa0275e..3841bd3b0 100644 --- a/sys/src/cmd/upas/imap4d/imap4d.c +++ b/sys/src/cmd/upas/imap4d/imap4d.c @@ -223,6 +223,10 @@ main(int argc, char *argv[]) fmtinstall('Y', Zfmt); fmtinstall('Z', Zfmt); + /* for auth */ + fmtinstall('H', encodefmt); + fmtinstall('[', encodefmt); + preauth = 0; allowpass = 0; allowcr = 0; diff --git a/sys/src/cmd/upas/imap4d/msg.c b/sys/src/cmd/upas/imap4d/msg.c index fbd21d2ac..12900938f 100644 --- a/sys/src/cmd/upas/imap4d/msg.c +++ b/sys/src/cmd/upas/imap4d/msg.c @@ -462,36 +462,6 @@ msgstruct(Msg *m, int top) return 1; } -/* - * stolen from upas/marshal; base64 encodes from one fd to another. - * - * the size of buf is very important to enc64. Anything other than - * a multiple of 3 will cause enc64 to output a termination sequence. - * To ensure that a full buf corresponds to a multiple of complete lines, - * we make buf a multiple of 3*18 since that's how many enc64 sticks on - * a single line. This avoids short lines in the output which is pleasing - * but not necessary. - */ -static int -enc64x18(char *out, int lim, uchar *in, int n) -{ - int m, mm, nn; - - nn = 0; - for(; n > 0; n -= m){ - m = 18 * 3; - if(m > n) - m = n; - mm = enc64(out, lim - nn, in, m); - in += m; - out += mm; - *out++ = '\r'; - *out++ = '\n'; - nn += mm + 2; - } - return nn; -} - /* * read in the message body to count \n without a preceding \r */