xref: /plan9/sys/src/cmd/unix/drawterm/libauth/auth_getuserpasswd.c (revision ec59a3ddbfceee0efe34584c2c9981a5e5ff1ec4)
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include "authlocal.h"
5 
6 enum {
7 	ARgiveup = 100,
8 };
9 
10 static int
dorpc(AuthRpc * rpc,char * verb,char * val,int len,AuthGetkey * getkey)11 dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
12 {
13 	int ret;
14 
15 	for(;;){
16 		if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
17 			return ret;
18 		if(getkey == 0)
19 			return ARgiveup;	/* don't know how */
20 		if((*getkey)(rpc->arg) < 0)
21 			return ARgiveup;	/* user punted */
22 	}
23 }
24 
25 UserPasswd*
auth_getuserpasswd(AuthGetkey * getkey,char * fmt,...)26 auth_getuserpasswd(AuthGetkey *getkey, char *fmt, ...)
27 {
28 	AuthRpc *rpc;
29 	char *f[3], *p, *params;
30 	int fd;
31 	va_list arg;
32 	UserPasswd *up;
33 
34 	up = nil;
35 	rpc = nil;
36 	params = nil;
37 
38 	fd = open("/mnt/factotum/rpc", ORDWR);
39 	if(fd < 0)
40 		goto out;
41 	rpc = auth_allocrpc(fd);
42 	if(rpc == nil)
43 		goto out;
44 	quotefmtinstall();	/* just in case */
45 	va_start(arg, fmt);
46 	params = vsmprint(fmt, arg);
47 	va_end(arg);
48 	if(params == nil)
49 		goto out;
50 
51 	if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok
52 	|| dorpc(rpc, "read", nil, 0, getkey) != ARok)
53 		goto out;
54 
55 	rpc->arg[rpc->narg] = '\0';
56 	if(tokenize(rpc->arg, f, 2) != 2){
57 		werrstr("bad answer from factotum");
58 		goto out;
59 	}
60 	up = malloc(sizeof(*up)+rpc->narg+1);
61 	if(up == nil)
62 		goto out;
63 	p = (char*)&up[1];
64 	strcpy(p, f[0]);
65 	up->user = p;
66 	p += strlen(p)+1;
67 	strcpy(p, f[1]);
68 	up->passwd = p;
69 
70 out:
71 	free(params);
72 	auth_freerpc(rpc);
73 	close(fd);
74 	return up;
75 }
76