]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/cwfs/auth.c
grep: error if sbrk fails
[plan9front.git] / sys / src / cmd / cwfs / auth.c
1 #include "all.h"
2 #include "io.h"
3 #include <auth.h>
4
5 char*
6 nvrgetconfig(void)
7 {
8         return conf.confdev;
9 }
10
11 int
12 nvrsetconfig(char* word)
13 {
14         /* config block is on device `word' */
15         USED(word);
16         return 0;
17 }
18
19 static char *keyspec = "proto=p9any role=server";
20
21 void*
22 authnew(void)
23 {
24         AuthRpc *rpc;
25         int fd;
26
27         if(access("/mnt/factotum", 0) < 0)
28                 if((fd = open("/srv/factotum", ORDWR)) >= 0)
29                         mount(fd, -1, "/mnt", MBEFORE, "");
30         if((fd = open("/mnt/factotum/rpc", ORDWR)) < 0)
31                 return nil;
32         if((rpc = auth_allocrpc(fd)) == nil){
33                 close(fd);
34                 return nil;
35         }
36         if(auth_rpc(rpc, "start", keyspec, strlen(keyspec)) != ARok){
37                 authfree(rpc);
38                 return nil;
39         }
40         return rpc;
41 }
42
43 void
44 authfree(void *auth)
45 {
46         AuthRpc *rpc;
47
48         if(rpc = auth){
49                 close(rpc->afd);
50                 auth_freerpc(rpc);
51         }
52 }
53
54 int
55 authread(File *file, uchar *data, int count)
56 {
57         AuthInfo *ai;
58         AuthRpc *rpc;
59         Chan *chan;
60
61         chan = file->cp;
62         if((rpc = file->auth) == nil){
63                 snprint(chan->err, sizeof(chan->err),
64                         "not an auth fid");
65                 return -1;
66         }
67
68         switch(auth_rpc(rpc, "read", nil, 0)){
69         default:
70                 snprint(chan->err, sizeof(chan->err),
71                         "authread: auth protocol not finished");
72                 return -1;
73         case ARdone:
74                 if((ai = auth_getinfo(rpc)) == nil)
75                         goto Phase;
76                 file->uid = strtouid(ai->cuid);
77                 if(file->uid < 0){
78                         snprint(chan->err, sizeof(chan->err),
79                                 "unknown user '%s'", ai->cuid);
80                         auth_freeAI(ai);
81                         return -1;
82                 }
83                 auth_freeAI(ai);
84                 return 0;
85         case ARok:
86                 if(count < rpc->narg){
87                         snprint(chan->err, sizeof(chan->err),
88                                 "not enough data in auth read");
89                         return -1;
90                 }
91                 memmove(data, rpc->arg, rpc->narg);
92                 return rpc->narg;
93         case ARphase:
94         Phase:
95                 rerrstr(chan->err, sizeof(chan->err));
96                 return -1;
97         }
98 }
99
100 int
101 authwrite(File *file, uchar *data, int count)
102 {
103         AuthRpc *rpc;
104         Chan *chan;
105
106         chan = file->cp;
107         if((rpc = file->auth) == nil){
108                 snprint(chan->err, sizeof(chan->err),
109                         "not an auth fid");
110                 return -1;
111         }
112         if(auth_rpc(rpc, "write", data, count) != ARok){
113                 rerrstr(chan->err, sizeof(chan->err));
114                 return -1;
115         }
116         return count;
117 }
118