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

This commit is contained in:
cinap_lenrek 2015-03-12 17:29:17 +01:00
parent 7e7cc2c8d7
commit 757354dac6
3 changed files with 11 additions and 14 deletions

View file

@ -130,17 +130,11 @@ cramauth(void)
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 @@ passLogin(char *user, char *secret)
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 @@ passLogin(char *user, char *secret)
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;

View file

@ -207,6 +207,10 @@ main(int argc, char *argv[])
Binit(&bin, 0, OREAD);
Binit(&bout, 1, OWRITE);
/* for auth */
fmtinstall('H', encodefmt);
fmtinstall('[', encodefmt);
preauth = 0;
allowPass = 0;
allowCR = 0;

View file

@ -596,17 +596,18 @@ enc64x18(char *out, int lim, uchar *in, int n)
{
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 @@ body64(int in, int out)
int m, n;
for(;;){
n = read(in, buf, sizeof(buf));
n = readn(in, buf, sizeof(buf));
if(n < 0)
return;
if(n == 0)