1 be8b315d 2004-06-17 devnull #include <u.h>
2 be8b315d 2004-06-17 devnull #include <libc.h>
3 be8b315d 2004-06-17 devnull #include <auth.h>
4 648bb6f7 2005-02-08 devnull #include <9pclient.h>
5 be8b315d 2004-06-17 devnull #include "authlocal.h"
7 be8b315d 2004-06-17 devnull static struct {
8 be8b315d 2004-06-17 devnull char *verb;
10 be8b315d 2004-06-17 devnull } tab[] = {
11 be8b315d 2004-06-17 devnull "ok", ARok,
12 be8b315d 2004-06-17 devnull "done", ARdone,
13 be8b315d 2004-06-17 devnull "error", ARerror,
14 be8b315d 2004-06-17 devnull "needkey", ARneedkey,
15 be8b315d 2004-06-17 devnull "badkey", ARbadkey,
16 be8b315d 2004-06-17 devnull "phase", ARphase,
17 be8b315d 2004-06-17 devnull "toosmall", ARtoosmall,
18 be8b315d 2004-06-17 devnull "error", ARerror,
21 648bb6f7 2005-02-08 devnull static long
22 648bb6f7 2005-02-08 devnull rpcread(AuthRpc *rpc, void *buf, int buflen)
24 648bb6f7 2005-02-08 devnull if (rpc->afd >= 0)
25 648bb6f7 2005-02-08 devnull return read(rpc->afd, buf, buflen);
27 648bb6f7 2005-02-08 devnull return fsread(rpc->afid, buf, buflen);
30 648bb6f7 2005-02-08 devnull static long
31 648bb6f7 2005-02-08 devnull rpcwrite(AuthRpc *rpc, void *buf, int buflen)
33 648bb6f7 2005-02-08 devnull if (rpc->afd >= 0)
34 648bb6f7 2005-02-08 devnull return write(rpc->afd, buf, buflen);
36 648bb6f7 2005-02-08 devnull return fswrite(rpc->afid, buf, buflen);
39 be8b315d 2004-06-17 devnull static int
40 be8b315d 2004-06-17 devnull classify(char *buf, uint n, AuthRpc *rpc)
42 be8b315d 2004-06-17 devnull int i, len;
44 be8b315d 2004-06-17 devnull for(i=0; i<nelem(tab); i++){
45 be8b315d 2004-06-17 devnull len = strlen(tab[i].verb);
46 be8b315d 2004-06-17 devnull if(n >= len && memcmp(buf, tab[i].verb, len) == 0 && (n==len || buf[len]==' ')){
47 be8b315d 2004-06-17 devnull if(n==len){
48 be8b315d 2004-06-17 devnull rpc->narg = 0;
49 be8b315d 2004-06-17 devnull rpc->arg = "";
51 be8b315d 2004-06-17 devnull rpc->narg = n - (len+1);
52 be8b315d 2004-06-17 devnull rpc->arg = (char*)buf+len+1;
54 be8b315d 2004-06-17 devnull return tab[i].val;
57 be8b315d 2004-06-17 devnull werrstr("malformed rpc response: %s", buf);
58 be8b315d 2004-06-17 devnull return ARrpcfailure;
62 648bb6f7 2005-02-08 devnull auth_allocrpc(void)
64 be8b315d 2004-06-17 devnull AuthRpc *rpc;
66 be8b315d 2004-06-17 devnull rpc = mallocz(sizeof(*rpc), 1);
67 be8b315d 2004-06-17 devnull if(rpc == nil)
68 be8b315d 2004-06-17 devnull return nil;
69 648bb6f7 2005-02-08 devnull rpc->afd = open("/mnt/factotum/rpc", ORDWR);
70 648bb6f7 2005-02-08 devnull if(rpc->afd < 0){
71 ba285d2b 2005-02-11 devnull rpc->afid = nsopen("factotum", nil, "rpc", ORDWR);
72 648bb6f7 2005-02-08 devnull if(rpc->afid == nil){
73 648bb6f7 2005-02-08 devnull free(rpc);
74 648bb6f7 2005-02-08 devnull return nil;
77 be8b315d 2004-06-17 devnull return rpc;
81 be8b315d 2004-06-17 devnull auth_freerpc(AuthRpc *rpc)
83 22157b7a 2006-01-27 devnull if(rpc == nil)
85 648bb6f7 2005-02-08 devnull if(rpc->afd >= 0)
86 648bb6f7 2005-02-08 devnull close(rpc->afd);
87 0c891f28 2005-02-15 devnull if(rpc->afid != nil)
88 648bb6f7 2005-02-08 devnull fsclose(rpc->afid);
89 be8b315d 2004-06-17 devnull free(rpc);
93 be8b315d 2004-06-17 devnull auth_rpc(AuthRpc *rpc, char *verb, void *a, int na)
95 be8b315d 2004-06-17 devnull int l, n, type;
96 be8b315d 2004-06-17 devnull char *f[4];
98 be8b315d 2004-06-17 devnull l = strlen(verb);
99 be8b315d 2004-06-17 devnull if(na+l+1 > AuthRpcMax){
100 be8b315d 2004-06-17 devnull werrstr("rpc too big");
101 be8b315d 2004-06-17 devnull return ARtoobig;
104 be8b315d 2004-06-17 devnull memmove(rpc->obuf, verb, l);
105 be8b315d 2004-06-17 devnull rpc->obuf[l] = ' ';
106 be8b315d 2004-06-17 devnull memmove(rpc->obuf+l+1, a, na);
107 648bb6f7 2005-02-08 devnull if((n=rpcwrite(rpc, rpc->obuf, l+1+na)) != l+1+na){
108 be8b315d 2004-06-17 devnull if(n >= 0)
109 be8b315d 2004-06-17 devnull werrstr("auth_rpc short write");
110 be8b315d 2004-06-17 devnull return ARrpcfailure;
113 648bb6f7 2005-02-08 devnull if((n=rpcread(rpc, rpc->ibuf, AuthRpcMax)) < 0)
114 be8b315d 2004-06-17 devnull return ARrpcfailure;
115 be8b315d 2004-06-17 devnull rpc->ibuf[n] = '\0';
118 be8b315d 2004-06-17 devnull * Set error string for good default behavior.
120 be8b315d 2004-06-17 devnull switch(type = classify(rpc->ibuf, n, rpc)){
121 be8b315d 2004-06-17 devnull default:
122 be8b315d 2004-06-17 devnull werrstr("unknown rpc type %d (bug in auth_rpc.c)", type);
124 be8b315d 2004-06-17 devnull case ARok:
126 be8b315d 2004-06-17 devnull case ARrpcfailure:
128 be8b315d 2004-06-17 devnull case ARerror:
129 be8b315d 2004-06-17 devnull if(rpc->narg == 0)
130 be8b315d 2004-06-17 devnull werrstr("unspecified rpc error");
132 be8b315d 2004-06-17 devnull werrstr("%s", rpc->arg);
134 be8b315d 2004-06-17 devnull case ARneedkey:
135 be8b315d 2004-06-17 devnull werrstr("needkey %s", rpc->arg);
137 be8b315d 2004-06-17 devnull case ARbadkey:
138 be8b315d 2004-06-17 devnull if(getfields(rpc->arg, f, nelem(f), 0, "\n") < 2)
139 be8b315d 2004-06-17 devnull werrstr("badkey %s", rpc->arg);
141 be8b315d 2004-06-17 devnull werrstr("badkey %s", f[1]);
143 be8b315d 2004-06-17 devnull case ARphase:
144 be8b315d 2004-06-17 devnull werrstr("phase error %s", rpc->arg);
147 be8b315d 2004-06-17 devnull return type;