7 int post9p(int, char*);
10 char *keypattern = "";
15 extern char *post9parg; /* clumsy hack */
17 AuthInfo* xauth_proxy(AuthGetkey *getkey, char *fmt, ...);
22 fprint(2, "usage: srv [-a] [-A aname] [-k keypattern] addr [srvname]\n");
23 threadexitsall("usage");
27 threadmain(int argc, char **argv)
31 fmtinstall('F', fcallfmt);
32 fmtinstall('M', dirmodefmt);
39 /* BUG: should be able to repeat this and establish multiple afids */
40 aname = EARGF(usage());
49 keypattern = EARGF(usage());
55 if(argc != 1 && argc != 2)
58 addr = netmkaddr(argv[0], "tcp", "9fs");
59 if((fd = dial(addr, nil, nil, nil)) < 0)
60 sysfatal("dial %s: %r", addr);
71 if(post9p(fd, service) < 0)
72 sysfatal("post9p: %r");
78 do9p(Fcall *tx, Fcall *rx)
80 static uchar buf[9000];
81 static char ebuf[200];
84 n = convS2M(tx, buf, sizeof buf);
86 werrstr("convS2M failed");
90 fprint(2, "<- %F\n", tx);
91 if(write(fd, buf, n) != n)
93 if((n = read9pmsg(fd, buf, sizeof buf)) < 0)
96 werrstr("unexpected eof");
99 if(convM2S(buf, n, rx) != n){
100 werrstr("convM2S failed");
104 fprint(2, "-> %F\n", rx);
105 if(rx->type != Rerror && rx->type != tx->type+1){
106 werrstr("unexpected type");
109 if(rx->tag != tx->tag){
110 werrstr("unexpected tag");
116 rerrstr(ebuf, sizeof ebuf);
130 tx.version = "9P2000";
133 if(rx.type == Rerror)
134 sysfatal("Tversion: %s", rx.ename);
140 tx.uname = getuser();
143 if(rx.type == Rerror){
144 fprint(2, "rx: %s\n", rx.ename);
149 if(xauth_proxy(auth_getkey, "proto=p9any role=client %s", keypattern) == nil)
150 sysfatal("authproxy: %r");
154 xread(void *buf, int n)
160 tx.fid = 0; /* afid above */
164 if(rx.type == Rerror){
165 werrstr("%s", rx.ename);
170 werrstr("too much data returned");
173 memmove(buf, rx.data, rx.count);
178 xwrite(void *buf, int n)
184 tx.fid = 0; /* afid above */
189 if(rx.type == Rerror){
190 werrstr("%s", rx.ename);
198 * changed to add -A below
202 post9p(int fd, char *name)
208 if((ns = getns()) == nil)
211 s = smprint("unix!%s/%s", ns, name);
224 execlp("9pserve", "9pserve", "-u",
226 smprint("%d", msize),
232 execlp("9pserve", "9pserve",
233 doauth < 0 ? "-nu" : "-u", s, (char*)0);
234 fprint(2, "exec 9pserve: %r\n");
242 if(w->msg && w->msg[0]){
244 werrstr("9pserve failed");
252 enum { ARgiveup = 100 };
254 dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
259 if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
262 return ARgiveup; /* don't know how */
263 if((*getkey)(rpc->arg) < 0)
264 return ARgiveup; /* user punted */
270 * this just proxies what the factotum tells it to.
273 xfauth_proxy(AuthRpc *rpc, AuthGetkey *getkey, char *params)
280 rerrstr(oerr, sizeof oerr);
281 werrstr("UNKNOWN AUTH ERROR");
283 if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok){
284 werrstr("fauth_proxy start: %r");
288 buf = malloc(AuthRpcMax);
292 switch(dorpc(rpc, "read", nil, 0, getkey)){
295 a = auth_getinfo(rpc);
296 errstr(oerr, sizeof oerr); /* no error, restore whatever was there */
299 if(xwrite(rpc->arg, rpc->narg) != rpc->narg){
300 werrstr("auth_proxy write fid: %r");
306 memset(buf, 0, AuthRpcMax);
307 while((ret = dorpc(rpc, "write", buf, n, getkey)) == ARtoosmall){
308 if(atoi(rpc->arg) > AuthRpcMax)
310 m = xread(buf+n, atoi(rpc->arg)-n);
313 werrstr("auth_proxy short read: %s", buf);
319 werrstr("auth_proxy rpc write: %s: %r", buf);
324 werrstr("auth_proxy rpc: %r");
334 xauth_proxy(AuthGetkey *getkey, char *fmt, ...)
341 quotefmtinstall(); /* just in case */
343 p = vsmprint(fmt, arg);
346 rpc = auth_allocrpc();
352 ai = xfauth_proxy(rpc, getkey, p);