Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <fcall.h>
4 #include <auth.h>
5 #include <9pclient.h>
6 #include "authlocal.h"
8 enum {
9 ARgiveup = 100,
10 };
12 static uchar*
13 gstring(uchar *p, uchar *ep, char **s)
14 {
15 uint n;
17 if(p == nil)
18 return nil;
19 if(p+BIT16SZ > ep)
20 return nil;
21 n = GBIT16(p);
22 p += BIT16SZ;
23 if(p+n > ep)
24 return nil;
25 *s = malloc(n+1);
26 memmove((*s), p, n);
27 (*s)[n] = '\0';
28 p += n;
29 return p;
30 }
32 static uchar*
33 gcarray(uchar *p, uchar *ep, uchar **s, int *np)
34 {
35 uint n;
37 if(p == nil)
38 return nil;
39 if(p+BIT16SZ > ep)
40 return nil;
41 n = GBIT16(p);
42 p += BIT16SZ;
43 if(p+n > ep)
44 return nil;
45 *s = malloc(n);
46 if(*s == nil)
47 return nil;
48 memmove((*s), p, n);
49 *np = n;
50 p += n;
51 return p;
52 }
54 void
55 auth_freeAI(AuthInfo *ai)
56 {
57 if(ai == nil)
58 return;
59 free(ai->cuid);
60 free(ai->suid);
61 free(ai->cap);
62 free(ai->secret);
63 free(ai);
64 }
66 static uchar*
67 convM2AI(uchar *p, int n, AuthInfo **aip)
68 {
69 uchar *e = p+n;
70 AuthInfo *ai;
72 ai = mallocz(sizeof(*ai), 1);
73 if(ai == nil)
74 return nil;
76 p = gstring(p, e, &ai->cuid);
77 p = gstring(p, e, &ai->suid);
78 p = gstring(p, e, &ai->cap);
79 p = gcarray(p, e, &ai->secret, &ai->nsecret);
80 if(p == nil)
81 auth_freeAI(ai);
82 else
83 *aip = ai;
84 return p;
85 }
87 AuthInfo*
88 auth_getinfo(AuthRpc *rpc)
89 {
90 AuthInfo *a;
92 if(auth_rpc(rpc, "authinfo", nil, 0) != ARok)
93 return nil;
94 if(convM2AI((uchar*)rpc->arg, rpc->narg, &a) == nil){
95 werrstr("bad auth info from factotum");
96 return nil;
97 }
98 return a;
99 }
101 static int
102 dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
104 int ret;
106 for(;;){
107 if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
108 return ret;
109 if(getkey == nil)
110 return ARgiveup; /* don't know how */
111 if((*getkey)(rpc->arg) < 0)
112 return ARgiveup; /* user punted */
116 /*
117 * this just proxies what the factotum tells it to.
118 */
119 AuthInfo*
120 fauth_proxy(int fd, AuthRpc *rpc, AuthGetkey *getkey, char *params)
122 char *buf;
123 int m, n, ret;
124 AuthInfo *a;
125 char oerr[ERRMAX];
127 rerrstr(oerr, sizeof oerr);
128 werrstr("UNKNOWN AUTH ERROR");
130 if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok){
131 werrstr("fauth_proxy start: %r");
132 return nil;
135 buf = malloc(AuthRpcMax);
136 if(buf == nil)
137 return nil;
138 for(;;){
139 switch(dorpc(rpc, "read", nil, 0, getkey)){
140 case ARdone:
141 free(buf);
142 a = auth_getinfo(rpc);
143 errstr(oerr, sizeof oerr); /* no error, restore whatever was there */
144 return a;
145 case ARok:
146 if(write(fd, rpc->arg, rpc->narg) != rpc->narg){
147 werrstr("auth_proxy write fd: %r");
148 goto Error;
150 break;
151 case ARphase:
152 n = 0;
153 memset(buf, 0, AuthRpcMax);
154 while((ret = dorpc(rpc, "write", buf, n, getkey)) == ARtoosmall){
155 if(atoi(rpc->arg) > AuthRpcMax)
156 break;
157 m = read(fd, buf+n, atoi(rpc->arg)-n);
158 if(m <= 0){
159 if(m == 0)
160 werrstr("auth_proxy short read: %s", buf);
161 goto Error;
163 n += m;
165 if(ret != ARok){
166 werrstr("auth_proxy rpc write: %s: %r", buf);
167 goto Error;
169 break;
170 default:
171 werrstr("auth_proxy rpc: %r");
172 goto Error;
175 Error:
176 free(buf);
177 return nil;
180 AuthInfo*
181 auth_proxy(int fd, AuthGetkey *getkey, char *fmt, ...)
183 char *p;
184 va_list arg;
185 AuthInfo *ai;
186 AuthRpc *rpc;
188 quotefmtinstall(); /* just in case */
189 va_start(arg, fmt);
190 p = vsmprint(fmt, arg);
191 va_end(arg);
193 rpc = auth_allocrpc();
194 if(rpc == nil){
195 free(p);
196 return nil;
199 ai = fauth_proxy(fd, rpc, getkey, p);
200 free(p);
201 auth_freerpc(rpc);
202 return ai;
205 /*
206 * this just proxies what the factotum tells it to.
207 */
208 AuthInfo*
209 fsfauth_proxy(CFid *fid, AuthRpc *rpc, AuthGetkey *getkey, char *params)
211 char *buf;
212 int m, n, ret;
213 AuthInfo *a;
214 char oerr[ERRMAX];
216 rerrstr(oerr, sizeof oerr);
217 werrstr("UNKNOWN AUTH ERROR");
219 if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok){
220 werrstr("fauth_proxy start: %r");
221 return nil;
224 buf = malloc(AuthRpcMax);
225 if(buf == nil)
226 return nil;
227 for(;;){
228 switch(dorpc(rpc, "read", nil, 0, getkey)){
229 case ARdone:
230 free(buf);
231 a = auth_getinfo(rpc);
232 errstr(oerr, sizeof oerr); /* no error, restore whatever was there */
233 return a;
234 case ARok:
235 if(fswrite(fid, rpc->arg, rpc->narg) != rpc->narg){
236 werrstr("auth_proxy write fid: %r");
237 goto Error;
239 break;
240 case ARphase:
241 n = 0;
242 memset(buf, 0, AuthRpcMax);
243 while((ret = dorpc(rpc, "write", buf, n, getkey)) == ARtoosmall){
244 if(atoi(rpc->arg) > AuthRpcMax)
245 break;
246 m = fsread(fid, buf+n, atoi(rpc->arg)-n);
247 if(m <= 0){
248 if(m == 0)
249 werrstr("auth_proxy short read: %s", buf);
250 goto Error;
252 n += m;
254 if(ret != ARok){
255 werrstr("auth_proxy rpc write: %s: %r", buf);
256 goto Error;
258 break;
259 default:
260 werrstr("auth_proxy rpc: %r");
261 goto Error;
264 Error:
265 free(buf);
266 return nil;
269 AuthInfo*
270 fsauth_proxy(CFid *fid, AuthGetkey *getkey, char *fmt, ...)
272 char *p;
273 va_list arg;
274 AuthInfo *ai;
275 AuthRpc *rpc;
277 quotefmtinstall(); /* just in case */
278 va_start(arg, fmt);
279 p = vsmprint(fmt, arg);
280 va_end(arg);
282 rpc = auth_allocrpc();
283 if(rpc == nil){
284 free(p);
285 return nil;
288 ai = fsfauth_proxy(fid, rpc, getkey, p);
289 free(p);
290 auth_freerpc(rpc);
291 return ai;