]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libauth/auth_respond.c
bc9fd20347bfd65ce87f2038f1dcb0210b63b9a6
[plan9front.git] / sys / src / libauth / auth_respond.c
1 #include <u.h>
2 #include <libc.h>
3 #include <auth.h>
4 #include "authlocal.h"
5
6 enum {
7         ARgiveup = 100,
8 };
9
10 static int
11 dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
12 {
13         int ret;
14
15         for(;;){
16                 if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
17                         return ret;
18                 if(getkey == nil)
19                         return ARgiveup;        /* don't know how */
20                 if((*getkey)(rpc->arg) < 0)
21                         return ARgiveup;        /* user punted */
22         }
23 }
24
25 int
26 auth_respond(void *chal, uint nchal, char *user, uint nuser, void *resp, uint nresp, AuthGetkey *getkey, char *fmt, ...)
27 {
28         char *p, *s;
29         va_list arg;
30         int afd;
31         AuthRpc *rpc;
32         Attr *a;
33
34         if((afd = open("/mnt/factotum/rpc", ORDWR)) < 0)
35                 return -1;
36         
37         if((rpc = auth_allocrpc(afd)) == nil){
38                 close(afd);
39                 return -1;
40         }
41
42         quotefmtinstall();      /* just in case */
43         va_start(arg, fmt);
44         p = vsmprint(fmt, arg);
45         va_end(arg);
46
47         if(p==nil
48         || dorpc(rpc, "start", p, strlen(p), getkey) != ARok
49         || dorpc(rpc, "write", chal, nchal, getkey) != ARok
50         || dorpc(rpc, "read", nil, 0, getkey) != ARok){
51                 free(p);
52                 close(afd);
53                 auth_freerpc(rpc);
54                 return -1;
55         }
56         free(p);
57
58         if(rpc->narg < nresp)
59                 nresp = rpc->narg;
60         memmove(resp, rpc->arg, nresp);
61
62         if((a = auth_attr(rpc)) != nil
63         && (s = _strfindattr(a, "user")) != nil && strlen(s) < nuser)
64                 strcpy(user, s);
65         else if(nuser > 0)
66                 user[0] = '\0';
67
68         _freeattr(a);
69         close(afd);
70         auth_freerpc(rpc);
71         return nresp;   
72 }