shithub: riscv

Download patch

ref: 757354dac6d55d12f28a3a94c601e625cf89c5ac
parent: 7e7cc2c8d78a1e5e61f6c71051f8039365314ec4
author: cinap_lenrek <[email protected]>
date: Thu Mar 12 13:29:17 EDT 2015

imap4d: simplify auth code using encodefmt(), use readn() to get multiple of 3*18 in base64 input buffer

--- a/sys/src/cmd/ip/imap4d/auth.c
+++ b/sys/src/cmd/ip/imap4d/auth.c
@@ -130,17 +130,11 @@
 	AuthInfo *ai;
 	Chalstate *cs;
 	char *s, *t;
-	int n;
 
 	if((cs = auth_challenge("proto=cram role=server")) == nil)
 		return "couldn't get cram challenge";
 
-	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();
 
@@ -172,7 +166,6 @@
 	Chalstate *cs;
 	uchar digest[MD5dlen];
 	char response[2*MD5dlen+1];
-	int i;
 
 	if((cs = auth_challenge("proto=cram role=server")) == nil)
 		return nil;
@@ -180,8 +173,7 @@
 	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 = user;
 	cs->resp = response;
--- a/sys/src/cmd/ip/imap4d/imap4d.c
+++ b/sys/src/cmd/ip/imap4d/imap4d.c
@@ -207,6 +207,10 @@
 	Binit(&bin, 0, OREAD);
 	Binit(&bout, 1, OWRITE);
 
+	/* for auth */
+	fmtinstall('H', encodefmt);
+	fmtinstall('[', encodefmt);
+
 	preauth = 0;
 	allowPass = 0;
 	allowCR = 0;
--- a/sys/src/cmd/ip/imap4d/msg.c
+++ b/sys/src/cmd/ip/imap4d/msg.c
@@ -596,17 +596,18 @@
 {
 	int m, mm, nn;
 
-	nn = 0;
-	for(; n > 0; n -= m){
+	for(nn = 0; n > 0; n -= m, nn += mm){
 		m = 18 * 3;
 		if(m > n)
 			m = n;
+		nn += 2;	/* \r\n */
+		assert(nn < lim);
 		mm = enc64(out, lim - nn, in, m);
+		assert(mm > 0);
 		in += m;
 		out += mm;
 		*out++ = '\r';
 		*out++ = '\n';
-		nn += mm + 2;
 	}
 	return nn;
 }
@@ -619,7 +620,7 @@
 	int m, n;
 
 	for(;;){
-		n = read(in, buf, sizeof(buf));
+		n = readn(in, buf, sizeof(buf));
 		if(n < 0)
 			return;
 		if(n == 0)