imap4d: simplify auth with encodefmt (backport from 9front)

This commit is contained in:
cinap_lenrek 2017-03-12 18:50:27 +01:00
parent 8b6804717b
commit f616c63b39
3 changed files with 6 additions and 40 deletions

View file

@ -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);

View file

@ -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;

View file

@ -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
*/