]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libauth/auth_challenge.c
cc: fix wrong "useless or misleading comparison" warning
[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         if(c->dom){
78                 if(auth_rpc(c->rpc, "write", c->dom, strlen(c->dom)) != ARok){
79                         /*
80                          * if this fails we're out of phase with factotum.
81                          * give up.
82                          */
83                         goto Out;
84                 }
85         }
86
87         if(auth_rpc(c->rpc, "write", c->resp, c->nresp) != ARok){
88                 /*
89                  * don't close the connection -- maybe we'll try again.
90                  */
91                 return nil;
92         }
93
94         switch(ret = auth_rpc(c->rpc, "read", nil, 0)){
95         case ARok:
96         default:
97                 werrstr("factotum protocol botch %d %s", ret, c->rpc->ibuf);
98                 break;
99         case ARdone:
100                 ai = auth_getinfo(c->rpc);
101                 break;
102         }
103
104 Out:
105         close(c->afd);
106         auth_freerpc(c->rpc);
107         c->afd = -1;
108         c->rpc = nil;
109         return ai;
110 }
111
112 void
113 auth_freechal(Chalstate *c)
114 {
115         if(c == nil)
116                 return;
117
118         if(c->afd >= 0)
119                 close(c->afd);
120         if(c->rpc != nil)
121                 auth_freerpc(c->rpc);
122
123         memset(c, 0xBB, sizeof(*c));
124         free(c);
125 }