14 extern char *post9parg; /* clumsy hack */
16 AuthInfo* xauth_proxy(AuthGetkey *getkey, char *fmt, ...);
21 fprint(2, "usage: srv [-a] [-A aname] [-k keypattern] addr [srvname]\n");
22 threadexitsall("usage");
26 threadmain(int argc, char **argv)
30 fmtinstall('F', fcallfmt);
31 fmtinstall('M', dirmodefmt);
38 /* BUG: should be able to repeat this and establish multiple afids */
39 aname = EARGF(usage());
45 keypattern = EARGF(usage());
51 if(argc != 1 && argc != 2)
54 addr = netmkaddr(argv[0], "tcp", "9fs");
55 if((fd = dial(addr, nil, nil, nil)) < 0)
56 sysfatal("dial %s: %r", addr);
67 if(post9pservice(fd, service) < 0)
68 sysfatal("post9pservice: %r");
74 do9p(Fcall *tx, Fcall *rx)
76 static uchar buf[9000];
77 static char ebuf[200];
80 n = convS2M(tx, buf, sizeof buf);
82 werrstr("convS2M failed");
86 fprint(2, "<- %F\n", tx);
87 if(write(fd, buf, n) != n)
89 if((n = read9pmsg(fd, buf, sizeof buf)) < 0)
92 werrstr("unexpected eof");
95 if(convM2S(buf, n, rx) != n){
96 werrstr("convM2S failed");
100 fprint(2, "-> %F\n", rx);
101 if(rx->type != Rerror && rx->type != tx->type+1){
102 werrstr("unexpected type");
105 if(rx->tag != tx->tag){
106 werrstr("unexpected tag");
112 rerrstr(ebuf, sizeof ebuf);
126 tx.version = "9P2000";
129 if(rx.type == Rerror)
130 sysfatal("Tversion: %s", rx.ename);
136 tx.uname = getuser();
139 if(rx.type == Rerror){
140 fprint(2, "rx: %s\n", rx.ename);
145 if(xauth_proxy(auth_getkey, "proto=p9any role=client %s", keypattern) == nil)
146 sysfatal("authproxy: %r");
150 xread(void *buf, int n)
156 tx.fid = 0; /* afid above */
160 if(rx.type == Rerror){
161 werrstr("%s", rx.ename);
166 werrstr("too much data returned");
169 memmove(buf, rx.data, rx.count);
174 xwrite(void *buf, int n)
180 tx.fid = 0; /* afid above */
185 if(rx.type == Rerror){
186 werrstr("%s", rx.ename);
194 * changed to add -A below
198 post9pservice(int fd, char *name)
204 if((ns = getns()) == nil)
207 s = smprint("unix!%s/%s", ns, name);
220 execlp("9pserve", "9pserve", "-u",
222 smprint("%d", msize),
228 execlp("9pserve", "9pserve", "-u", s, (char*)0);
229 fprint(2, "exec 9pserve: %r\n");
237 if(w->msg && w->msg[0]){
239 werrstr("9pserve failed");
247 enum { ARgiveup = 100 };
249 dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
254 if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
257 return ARgiveup; /* don't know how */
258 if((*getkey)(rpc->arg) < 0)
259 return ARgiveup; /* user punted */
265 * this just proxies what the factotum tells it to.
268 xfauth_proxy(AuthRpc *rpc, AuthGetkey *getkey, char *params)
275 rerrstr(oerr, sizeof oerr);
276 werrstr("UNKNOWN AUTH ERROR");
278 if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok){
279 werrstr("fauth_proxy start: %r");
283 buf = malloc(AuthRpcMax);
287 switch(dorpc(rpc, "read", nil, 0, getkey)){
290 a = auth_getinfo(rpc);
291 errstr(oerr, sizeof oerr); /* no error, restore whatever was there */
294 if(xwrite(rpc->arg, rpc->narg) != rpc->narg){
295 werrstr("auth_proxy write fid: %r");
301 memset(buf, 0, AuthRpcMax);
302 while((ret = dorpc(rpc, "write", buf, n, getkey)) == ARtoosmall){
303 if(atoi(rpc->arg) > AuthRpcMax)
305 m = xread(buf+n, atoi(rpc->arg)-n);
308 werrstr("auth_proxy short read: %s", buf);
314 werrstr("auth_proxy rpc write: %s: %r", buf);
319 werrstr("auth_proxy rpc: %r");
329 xauth_proxy(AuthGetkey *getkey, char *fmt, ...)
336 quotefmtinstall(); /* just in case */
338 p = vsmprint(fmt, arg);
341 rpc = auth_allocrpc();
347 ai = xfauth_proxy(rpc, getkey, p);