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