ref: f616c63b399fed4f78121f387be9599e8e0c2b4c
parent: 8b6804717bb1ebbf858032cb5abb057ea8e2ab78
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Mar 12 14:50:27 EDT 2017
imap4d: simplify auth with encodefmt (backport from 9front)
--- a/sys/src/cmd/upas/imap4d/auth.c
+++ b/sys/src/cmd/upas/imap4d/auth.c
@@ -151,7 +151,6 @@
cramauth(void)
{
char *s, *t;
- int n;
AuthInfo *ai;
Chalstate *cs;
@@ -158,12 +157,7 @@
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 @@
{
char response[2*MD5dlen + 1];
uchar digest[MD5dlen];
- int i;
AuthInfo *ai;
Chalstate *cs;
@@ -229,8 +222,7 @@
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);
--- a/sys/src/cmd/upas/imap4d/imap4d.c
+++ b/sys/src/cmd/upas/imap4d/imap4d.c
@@ -223,6 +223,10 @@
fmtinstall('Y', Zfmt);
fmtinstall('Z', Zfmt);
+ /* for auth */
+ fmtinstall('H', encodefmt);
+ fmtinstall('[', encodefmt);
+
preauth = 0;
allowpass = 0;
allowcr = 0;
--- a/sys/src/cmd/upas/imap4d/msg.c
+++ b/sys/src/cmd/upas/imap4d/msg.c
@@ -463,36 +463,6 @@
}
/*
- * 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
*/
static int