]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libauth/auth_challenge.c
vt: implement snarf support
[plan9front.git] / sys / src / libauth / auth_challenge.c
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include "authlocal.h"
5
6 Chalstate*
7 auth_challenge(char *fmt, ...)
8 {
9         char *p;
10         va_list arg;
11         Chalstate *c;
12
13         quotefmtinstall();      /* just in case */
14         va_start(arg, fmt);
15         p = vsmprint(fmt, arg);
16         va_end(arg);
17         if(p == nil)
18                 return nil;
19
20         c = mallocz(sizeof(*c), 1);
21         if(c == nil){
22                 free(p);
23                 return nil;
24         }
25
26         if((c->afd = open("/mnt/factotum/rpc", ORDWR)) < 0){
27         Error:
28                 auth_freechal(c);
29                 free(p);
30                 return nil;
31         }
32
33         if((c->rpc=auth_allocrpc(c->afd)) == nil
34         || auth_rpc(c->rpc, "start", p, strlen(p)) != ARok
35         || auth_rpc(c->rpc, "read", nil, 0) != ARok)
36                 goto Error;
37
38         if(c->rpc->narg > sizeof(c->chal)-1){
39                 werrstr("buffer too small for challenge");
40                 goto Error;
41         }
42         memmove(c->chal, c->rpc->arg, c->rpc->narg);
43         c->nchal = c->rpc->narg;
44         free(p);
45         return c;
46 }
47
48 AuthInfo*
49 auth_response(Chalstate *c)
50 {
51         int ret;
52         AuthInfo *ai;
53
54         ai = nil;
55         if(c->afd < 0){
56                 werrstr("auth_response: connection not open");
57                 return nil;
58         }
59         if(c->resp == nil){
60                 werrstr("auth_response: nil response");
61                 return nil;
62         }
63         if(c->nresp == 0){
64                 werrstr("auth_response: unspecified response length");
65                 return nil;
66         }
67
68         if(c->user){
69                 if(auth_rpc(c->rpc, "write", c->user, strlen(c->user)) != ARok){
70                         /*
71                          * if this fails we're out of phase with factotum.
72                          * give up.
73                          */
74                         goto Out;
75                 }
76         }
77
78         if(auth_rpc(c->rpc, "write", c->resp, c->nresp) != ARok){
79                 /*
80                  * don't close the connection -- maybe we'll try again.
81                  */
82                 return nil;
83         }
84
85         switch(ret = auth_rpc(c->rpc, "read", nil, 0)){
86         case ARok:
87         default:
88                 werrstr("factotum protocol botch %d %s", ret, c->rpc->ibuf);
89                 break;
90         case ARdone:
91                 ai = auth_getinfo(c->rpc);
92                 break;
93         }
94
95 Out:
96         close(c->afd);
97         auth_freerpc(c->rpc);
98         c->afd = -1;
99         c->rpc = nil;
100         return ai;
101 }
102
103 void
104 auth_freechal(Chalstate *c)
105 {
106         if(c == nil)
107                 return;
108
109         if(c->afd >= 0)
110                 close(c->afd);
111         if(c->rpc != nil)
112                 auth_freerpc(c->rpc);
113
114         memset(c, 0xBB, sizeof(*c));
115         free(c);
116 }