5 * p9any - protocol negotiator
8 * S->C: v.2 proto@dom proto@dom proto@dom... NUL
10 * [negotiated proto continues]
13 static Proto* okproto[] =
20 rolecall(Role *r, char *name, Conv *c)
23 if(strcmp(r->name, name) == 0)
25 werrstr("unknown role");
30 hasnul(void *v, int n)
35 if(n > 0 && c[n-1] == '\0')
53 attr = delattr(copyattr(c->attr), "proto");
55 for(i=0; i<ring.nkey; i++){
57 for(j=0; okproto[j]; j++)
58 if(k->proto == okproto[j]
59 && (dom = strfindattr(k->attr, "dom")) != nil
60 && matchattr(attr, k->attr, k->privattr)){
61 s = estrappend(s, " %s@%s", k->proto->name, dom);
67 werrstr("no valid keys");
71 c->state = "write offer";
72 if(convwrite(c, s, strlen(s)+1) < 0)
77 c->state = "read choice";
78 if(convreadfn(c, hasnul, &s) < 0)
81 m = tokenize(s, tok, nelem(tok));
83 werrstr("bad protocol message");
87 for(i=0; okproto[i]; i++)
88 if(strcmp(okproto[i]->name, tok[0]) == 0)
91 werrstr("bad chosen protocol %q", tok[0]);
95 c->state = "write ok";
96 if(convwrite(c, "OK\0", 3) < 0)
99 c->state = "start choice";
100 attr = addattr(attr, "proto=%q dom=%q", tok[0], tok[1]);
104 c->proto = okproto[i];
106 if(rolecall(c->proto->roles, "server", c) < 0){
107 werrstr("%s: %r", tok[0]);
122 char *s, **f, *tok[20], ok[3], *q, *user, *dom;
123 int i, n, ret, version;
132 user = strfindattr(c->attr, "user");
133 dom = strfindattr(c->attr, "dom");
136 * if the user is the factotum owner, any key will do.
137 * if not, then if we have a speakfor key,
138 * we will only vouch for the user's local identity.
140 * this logic is duplicated in p9sk1.c
142 attr = delattr(copyattr(c->attr), "role");
143 attr = delattr(attr, "proto");
144 if(strcmp(c->sysuser, owner) == 0)
145 attr = addattr(attr, "role=client");
146 else if(user==nil || strcmp(c->sysuser, user)==0){
147 attr = delattr(attr, "user");
148 attr = addattr(attr, "role=speakfor");
150 werrstr("will not authenticate for %q as %q", c->sysuser, user);
154 c->state = "read offer";
155 if(convreadfn(c, hasnul, &s) < 0)
158 c->state = "look for keys";
159 n = tokenize(s, tok, nelem(tok));
162 if(n > 0 && memcmp(f[0], "v.", 2) == 0){
163 version = atoi(f[0]+2);
165 werrstr("unknown p9any version: %s", f[0]);
172 /* look for keys that don't need confirmation */
174 if((q = strchr(f[i], '@')) == nil)
176 if(dom && strcmp(q+1, dom) != 0)
179 if((k = keylookup("%A proto=%q dom=%q", attr, f[i], q))
180 && strfindattr(k->attr, "confirm") == nil)
185 /* look for any keys at all */
187 if((q = strchr(f[i], '@')) == nil)
189 if(dom && strcmp(q+1, dom) != 0)
192 if(k = keyfetch(c, "%A proto=%q dom=%q", attr, f[i], q))
197 /* ask for new keys */
198 c->state = "ask for keys";
200 if((q = strchr(f[i], '@')) == nil)
202 if(dom && strcmp(q+1, dom) != 0)
205 p = protolookup(f[i]);
206 if(p == nil || p->keyprompt == nil){
210 if(k = keyfetch(c, "%A proto=%q dom=%q %s", attr, f[i], q, p->keyprompt))
216 werrstr("unable to find common key");
220 /* f[i] is the chosen protocol, q the chosen domain */
221 attr = addattr(attr, "proto=%q dom=%q", f[i], q);
222 c->state = "write choice";
223 /* have a key: go for it */
224 if(convprint(c, "%q %q", f[i], q) < 0
225 || convwrite(c, "\0", 1) < 0)
229 c->state = "read ok";
230 if(convread(c, ok, 3) < 0 || memcmp(ok, "OK\0", 3) != 0)
234 c->state = "start choice";
235 c->proto = protolookup(f[i]);
240 if(rolecall(c->proto->roles, "client", c) < 0){
241 werrstr("%s: %r", c->proto->name);
257 "client", p9anyclient,
258 "server", p9anyserver,