Blob


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