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);
66 if(post9pservice(fd, service) < 0)
67 sysfatal("post9pservice: %r");
73 do9p(Fcall *tx, Fcall *rx)
75 static uchar buf[9000];
76 static char ebuf[200];
79 n = convS2M(tx, buf, sizeof buf);
81 werrstr("convS2M failed");
85 fprint(2, "<- %F\n", tx);
86 if(write(fd, buf, n) != n)
88 if((n = read9pmsg(fd, buf, sizeof buf)) < 0)
91 werrstr("unexpected eof");
94 if(convM2S(buf, n, rx) != n){
95 werrstr("convM2S failed");
99 fprint(2, "-> %F\n", rx);
100 if(rx->type != Rerror && rx->type != tx->type+1){
101 werrstr("unexpected type");
104 if(rx->tag != tx->tag){
105 werrstr("unexpected tag");
111 rerrstr(ebuf, sizeof ebuf);
125 tx.version = "9P2000";
128 if(rx.type == Rerror)
129 sysfatal("Tversion: %s", rx.ename);
135 tx.uname = getuser();
138 if(rx.type == Rerror){
139 fprint(2, "rx: %s\n", rx.ename);
144 if(xauth_proxy(auth_getkey, "proto=p9any role=client %s", keypattern) == nil)
145 sysfatal("authproxy: %r");
149 xread(void *buf, int n)
155 tx.fid = 0; /* afid above */
159 if(rx.type == Rerror){
160 werrstr("%s", rx.ename);
165 werrstr("too much data returned");
168 memmove(buf, rx.data, rx.count);
173 xwrite(void *buf, int n)
179 tx.fid = 0; /* afid above */
184 if(rx.type == Rerror){
185 werrstr("%s", rx.ename);
193 * changed to add -A below
197 post9pservice(int fd, char *name)
203 if((ns = getns()) == nil)
206 s = smprint("unix!%s/%s", ns, name);
219 execlp("9pserve", "9pserve", "-u",
221 smprint("%d", msize),
227 execlp("9pserve", "9pserve", "-u", s, (char*)0);
228 fprint(2, "exec 9pserve: %r\n");
236 if(w->msg && w->msg[0]){
238 werrstr("9pserve failed");
246 enum { ARgiveup = 100 };
248 dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
253 if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
256 return ARgiveup; /* don't know how */
257 if((*getkey)(rpc->arg) < 0)
258 return ARgiveup; /* user punted */
264 * this just proxies what the factotum tells it to.
267 xfauth_proxy(AuthRpc *rpc, AuthGetkey *getkey, char *params)
274 rerrstr(oerr, sizeof oerr);
275 werrstr("UNKNOWN AUTH ERROR");
277 if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok){
278 werrstr("fauth_proxy start: %r");
282 buf = malloc(AuthRpcMax);
286 switch(dorpc(rpc, "read", nil, 0, getkey)){
289 a = auth_getinfo(rpc);
290 errstr(oerr, sizeof oerr); /* no error, restore whatever was there */
293 if(xwrite(rpc->arg, rpc->narg) != rpc->narg){
294 werrstr("auth_proxy write fid: %r");
300 memset(buf, 0, AuthRpcMax);
301 while((ret = dorpc(rpc, "write", buf, n, getkey)) == ARtoosmall){
302 if(atoi(rpc->arg) > AuthRpcMax)
304 m = xread(buf+n, atoi(rpc->arg)-n);
307 werrstr("auth_proxy short read: %s", buf);
313 werrstr("auth_proxy rpc write: %s: %r", buf);
318 werrstr("auth_proxy rpc: %r");
328 xauth_proxy(AuthGetkey *getkey, char *fmt, ...)
335 quotefmtinstall(); /* just in case */
337 p = vsmprint(fmt, arg);
340 rpc = auth_allocrpc();
346 ai = xfauth_proxy(rpc, getkey, p);