xref: /plan9/sys/src/libauth/auth_userpasswd.c (revision b89146921ae046704254cb6244e6b53ef05722cd)
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include <authsrv.h>
5 #include "authlocal.h"
6 
7 /*
8  * compute the proper response.  We encrypt the ascii of
9  * challenge number, with trailing binary zero fill.
10  * This process was derived empirically.
11  * this was copied from inet's guard.
12  */
13 static void
netresp(char * key,long chal,char * answer)14 netresp(char *key, long chal, char *answer)
15 {
16 	uchar buf[8];
17 
18 	memset(buf, 0, sizeof buf);
19 	snprint((char *)buf, sizeof buf, "%lud", chal);
20 	if(encrypt(key, buf, 8) < 0)
21 		abort();
22 	sprint(answer, "%.8ux", buf[0]<<24 | buf[1]<<16 | buf[2]<<8 | buf[3]);
23 }
24 
25 AuthInfo*
auth_userpasswd(char * user,char * passwd)26 auth_userpasswd(char *user, char *passwd)
27 {
28 	char key[DESKEYLEN], resp[16];
29 	AuthInfo *ai;
30 	Chalstate *ch;
31 
32 	/*
33 	 * Probably we should have a factotum protocol
34 	 * to check a raw password.  For now, we use
35 	 * p9cr, which is simplest to speak.
36 	 */
37 	if((ch = auth_challenge("user=%q proto=p9cr role=server", user)) == nil)
38 		return nil;
39 
40 	passtokey(key, passwd);
41 	netresp(key, atol(ch->chal), resp);
42 	memset(key, 0, sizeof key);
43 
44 	ch->resp = resp;
45 	ch->nresp = strlen(resp);
46 	ai = auth_response(ch);
47 	auth_freechal(ch);
48 	return ai;
49 }
50