xref: /plan9/sys/src/libauth/auth_proxy.c (revision f366f900ff2cf070570ce4f3ef512f925d4caaac)
1 #include <u.h>
2 #include <libc.h>
3 #include <fcall.h>
4 #include <auth.h>
5 #include "authlocal.h"
6 
7 enum {
8 	ARgiveup = 100,
9 };
10 
11 static uchar*
gstring(uchar * p,uchar * ep,char ** s)12 gstring(uchar *p, uchar *ep, char **s)
13 {
14 	uint n;
15 
16 	if(p == nil)
17 		return nil;
18 	if(p+BIT16SZ > ep)
19 		return nil;
20 	n = GBIT16(p);
21 	p += BIT16SZ;
22 	if(p+n > ep)
23 		return nil;
24 	*s = malloc(n+1);
25 	memmove((*s), p, n);
26 	(*s)[n] = '\0';
27 	p += n;
28 	return p;
29 }
30 
31 static uchar*
gcarray(uchar * p,uchar * ep,uchar ** s,int * np)32 gcarray(uchar *p, uchar *ep, uchar **s, int *np)
33 {
34 	uint n;
35 
36 	if(p == nil)
37 		return nil;
38 	if(p+BIT16SZ > ep)
39 		return nil;
40 	n = GBIT16(p);
41 	p += BIT16SZ;
42 	if(p+n > ep)
43 		return nil;
44 	*s = malloc(n);
45 	if(*s == nil)
46 		return nil;
47 	memmove((*s), p, n);
48 	*np = n;
49 	p += n;
50 	return p;
51 }
52 
53 void
auth_freeAI(AuthInfo * ai)54 auth_freeAI(AuthInfo *ai)
55 {
56 	if(ai == nil)
57 		return;
58 	free(ai->cuid);
59 	free(ai->suid);
60 	free(ai->cap);
61 	free(ai->secret);
62 	free(ai);
63 }
64 
65 static uchar*
convM2AI(uchar * p,int n,AuthInfo ** aip)66 convM2AI(uchar *p, int n, AuthInfo **aip)
67 {
68 	uchar *e = p+n;
69 	AuthInfo *ai;
70 
71 	ai = mallocz(sizeof(*ai), 1);
72 	if(ai == nil)
73 		return nil;
74 
75 	p = gstring(p, e, &ai->cuid);
76 	p = gstring(p, e, &ai->suid);
77 	p = gstring(p, e, &ai->cap);
78 	p = gcarray(p, e, &ai->secret, &ai->nsecret);
79 	if(p == nil)
80 		auth_freeAI(ai);
81 	else
82 		*aip = ai;
83 	return p;
84 }
85 
86 AuthInfo*
auth_getinfo(AuthRpc * rpc)87 auth_getinfo(AuthRpc *rpc)
88 {
89 	AuthInfo *a;
90 
91 	if(auth_rpc(rpc, "authinfo", nil, 0) != ARok)
92 		return nil;
93 	if(convM2AI((uchar*)rpc->arg, rpc->narg, &a) == nil){
94 		werrstr("bad auth info from factotum");
95 		return nil;
96 	}
97 	return a;
98 }
99 
100 static int
dorpc(AuthRpc * rpc,char * verb,char * val,int len,AuthGetkey * getkey)101 dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
102 {
103 	int ret;
104 
105 	for(;;){
106 		if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
107 			return ret;
108 		if(getkey == nil)
109 			return ARgiveup;	/* don't know how */
110 		if((*getkey)(rpc->arg) < 0)
111 			return ARgiveup;	/* user punted */
112 	}
113 }
114 
115 /*
116  *  this just proxies what the factotum tells it to.
117  */
118 AuthInfo*
fauth_proxy(int fd,AuthRpc * rpc,AuthGetkey * getkey,char * params)119 fauth_proxy(int fd, AuthRpc *rpc, AuthGetkey *getkey, char *params)
120 {
121 	char *buf;
122 	int m, n, ret;
123 	AuthInfo *a;
124 	char oerr[ERRMAX];
125 
126 	if(rpc == nil){
127 		werrstr("fauth_proxy - no factotum");
128 		return nil;
129 	}
130 
131 	rerrstr(oerr, sizeof oerr);
132 	werrstr("UNKNOWN AUTH ERROR");
133 
134 	if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok){
135 		werrstr("fauth_proxy start: %r");
136 		return nil;
137 	}
138 
139 	buf = malloc(AuthRpcMax);
140 	if(buf == nil)
141 		return nil;
142 	for(;;){
143 		switch(dorpc(rpc, "read", nil, 0, getkey)){
144 		case ARdone:
145 			free(buf);
146 			a = auth_getinfo(rpc);
147 			/* no error, restore whatever was there */
148 			errstr(oerr, sizeof oerr);
149 			return a;
150 		case ARok:
151 			if(write(fd, rpc->arg, rpc->narg) != rpc->narg){
152 				werrstr("auth_proxy write fd: %r");
153 				goto Error;
154 			}
155 			break;
156 		case ARphase:
157 			n = 0;
158 			memset(buf, 0, AuthRpcMax);
159 			while((ret = dorpc(rpc, "write", buf, n, getkey)) == ARtoosmall){
160 				if(atoi(rpc->arg) > AuthRpcMax)
161 					break;
162 				m = read(fd, buf + n, atoi(rpc->arg) - n);
163 				if(m <= 0){
164 					if(m == 0)
165 						werrstr("auth_proxy short read: %s",
166 							buf);
167 					goto Error;
168 				}
169 				n += m;
170 			}
171 			if(ret != ARok){
172 				werrstr("auth_proxy rpc write: %s: %r", buf);
173 				goto Error;
174 			}
175 			break;
176 		default:
177 			werrstr("auth_proxy rpc: %r");
178 			goto Error;
179 		}
180 	}
181 Error:
182 	free(buf);
183 	return nil;
184 }
185 
186 AuthInfo*
auth_proxy(int fd,AuthGetkey * getkey,char * fmt,...)187 auth_proxy(int fd, AuthGetkey *getkey, char *fmt, ...)
188 {
189 	int afd;
190 	char *p;
191 	va_list arg;
192 	AuthInfo *ai;
193 	AuthRpc *rpc;
194 
195 	quotefmtinstall();	/* just in case */
196 	va_start(arg, fmt);
197 	p = vsmprint(fmt, arg);
198 	va_end(arg);
199 
200 	ai = nil;
201 	afd = open("/mnt/factotum/rpc", ORDWR);
202 	if(afd < 0){
203 		werrstr("opening /mnt/factotum/rpc: %r");
204 		free(p);
205 		return nil;
206 	}
207 
208 	rpc = auth_allocrpc(afd);
209 	if(rpc){
210 		ai = fauth_proxy(fd, rpc, getkey, p);
211 		auth_freerpc(rpc);
212 	}
213 	close(afd);
214 	free(p);
215 	return ai;
216 }
217