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());
48 keypattern = EARGF(usage());
54 if(argc != 1 && argc != 2)
57 addr = netmkaddr(argv[0], "tcp", "9fs");
58 if((fd = dial(addr, nil, nil, nil)) < 0)
59 sysfatal("dial %s: %r", addr);
70 if(post9pservice(fd, service) < 0)
71 sysfatal("post9pservice: %r");
77 do9p(Fcall *tx, Fcall *rx)
79 static uchar buf[9000];
80 static char ebuf[200];
83 n = convS2M(tx, buf, sizeof buf);
85 werrstr("convS2M failed");
89 fprint(2, "<- %F\n", tx);
90 if(write(fd, buf, n) != n)
92 if((n = read9pmsg(fd, buf, sizeof buf)) < 0)
95 werrstr("unexpected eof");
98 if(convM2S(buf, n, rx) != n){
99 werrstr("convM2S failed");
103 fprint(2, "-> %F\n", rx);
104 if(rx->type != Rerror && rx->type != tx->type+1){
105 werrstr("unexpected type");
108 if(rx->tag != tx->tag){
109 werrstr("unexpected tag");
115 rerrstr(ebuf, sizeof ebuf);
129 tx.version = "9P2000";
132 if(rx.type == Rerror)
133 sysfatal("Tversion: %s", rx.ename);
139 tx.uname = getuser();
142 if(rx.type == Rerror){
143 fprint(2, "rx: %s\n", rx.ename);
148 if(xauth_proxy(auth_getkey, "proto=p9any role=client %s", keypattern) == nil)
149 sysfatal("authproxy: %r");
153 xread(void *buf, int n)
159 tx.fid = 0; /* afid above */
163 if(rx.type == Rerror){
164 werrstr("%s", rx.ename);
169 werrstr("too much data returned");
172 memmove(buf, rx.data, rx.count);
177 xwrite(void *buf, int n)
183 tx.fid = 0; /* afid above */
188 if(rx.type == Rerror){
189 werrstr("%s", rx.ename);
197 * changed to add -A below
201 post9pservice(int fd, char *name)
207 if((ns = getns()) == nil)
210 s = smprint("unix!%s/%s", ns, name);
223 execlp("9pserve", "9pserve", "-u",
225 smprint("%d", msize),
231 execlp("9pserve", "9pserve",
232 doauth < 0 ? "-nu" : "-u", s, (char*)0);
233 fprint(2, "exec 9pserve: %r\n");
241 if(w->msg && w->msg[0]){
243 werrstr("9pserve failed");
251 enum { ARgiveup = 100 };
253 dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
258 if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
261 return ARgiveup; /* don't know how */
262 if((*getkey)(rpc->arg) < 0)
263 return ARgiveup; /* user punted */
269 * this just proxies what the factotum tells it to.
272 xfauth_proxy(AuthRpc *rpc, AuthGetkey *getkey, char *params)
279 rerrstr(oerr, sizeof oerr);
280 werrstr("UNKNOWN AUTH ERROR");
282 if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok){
283 werrstr("fauth_proxy start: %r");
287 buf = malloc(AuthRpcMax);
291 switch(dorpc(rpc, "read", nil, 0, getkey)){
294 a = auth_getinfo(rpc);
295 errstr(oerr, sizeof oerr); /* no error, restore whatever was there */
298 if(xwrite(rpc->arg, rpc->narg) != rpc->narg){
299 werrstr("auth_proxy write fid: %r");
305 memset(buf, 0, AuthRpcMax);
306 while((ret = dorpc(rpc, "write", buf, n, getkey)) == ARtoosmall){
307 if(atoi(rpc->arg) > AuthRpcMax)
309 m = xread(buf+n, atoi(rpc->arg)-n);
312 werrstr("auth_proxy short read: %s", buf);
318 werrstr("auth_proxy rpc write: %s: %r", buf);
323 werrstr("auth_proxy rpc: %r");
333 xauth_proxy(AuthGetkey *getkey, char *fmt, ...)
340 quotefmtinstall(); /* just in case */
342 p = vsmprint(fmt, arg);
345 rpc = auth_allocrpc();
351 ai = xfauth_proxy(rpc, getkey, p);