Changes in v9fs broke pipesrv. Fix it.
[npfs.git] / libnpauth / p9any.c
blobbc97a48628d9dc2bd06e733aa8dfd3437fd8f394
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "npfs.h"
6 #include "npclient.h"
7 #include "npauth.h"
9 #include "npaimpl.h"
11 #ifdef _WIN32
12 #define EPROTONOSUPPORT 93 // XXX
13 #endif
15 struct authproto {
16 char *name;
17 int (*auth)(Npcfid *fid, Npuser *user, void *aux);
20 static struct authproto authprotos[] = {
21 { "p9sk1", authp9sk1 },
22 { 0, 0 },
25 int
26 srvp9any(struct npsrvauth *a, char *msg, int len, char *resp, int resplen)
28 char *proto, *dom;
29 int r;
31 switch(a->state) {
32 case 0:
33 if(len != 0
34 || resplen < strlen(a->dom) + 32)
35 return err("internal error", EINVAL);
36 sprintf(resp, "v.2 p9sk1@%s", a->dom);
37 a->state++;
38 return strlen(resp) + 1;
40 case 1:
41 if(len == 0
42 || msg[len-1] != '\0'
43 || strlen(msg) != len-1
44 || getWord(&msg, ' ', &proto) == -1
45 || getWord(&msg, 0, &dom) == -1
46 || strcmp(dom, a->dom) != 0)
47 return err("botch", EINVAL);
48 if(strcmp(proto, "p9sk1") != 0)
49 return err("unsupported", EPROTONOSUPPORT);
50 if(resplen < 32)
51 return err("internal error", EINVAL);
52 strcpy(resp, "OK");
53 a->state++;
54 return strlen(resp) + 1;
56 default:
57 a->state -= 1; // map state [2..] to [1..] (skip state 0)
58 r = srvp9sk1(a, msg, len, resp, resplen);
59 a->state += 1;
60 return r;
65 int
66 authp9any(Npcfid *afid, Npuser *user, void *aux)
68 char buf[128], *p, *word, *proto, *dom;
69 int (*found)(Npcfid *afid, Npuser *user, void *aux);
70 int v2, i;
72 if(getline0(afid, buf, sizeof buf) <= 0)
73 return err("botch", EINVAL);
75 p = buf;
76 found = 0;
77 v2 = 0;
78 if(strncmp(p, "v.2 ", 4) == 0) {
79 v2 = 1;
80 p += 4;
82 while(*p && !found) {
83 if(getWord(&p, ' ', &word) == -1
84 || getWord(&word, '@', &proto) == -1
85 || getWord(&word, 0, &dom) == -1)
86 return err("botch", EINVAL);
87 for(i = 0; authprotos[i].name; i++) {
88 if(strcmp(authprotos[i].name, "p9sk1") == 0) {
89 found = authprotos[i].auth;
90 break;
94 if(!found)
95 return err("unsupported", EPROTONOSUPPORT);
97 if(putline0(afid, "%s %s", proto, dom) <= 0)
98 return err("botch", EINVAL);
100 if(v2) {
101 if(getline0(afid, buf, sizeof buf) <= 0)
102 return err("botch", EINVAL);
103 if(strcmp(buf, "OK") != 0)
104 return err("botch", EINVAL);
106 return found(afid, user, aux);