]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libauth/auth_respond.c
audiohda: fix syntax error
[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 static int
26 dorespond(void *chal, uint nchal, char *user, uint nuser, void *resp, uint nresp,
27         AuthInfo **ai, AuthGetkey *getkey, char *fmt, va_list arg)
28 {
29         char *p, *s;
30         int afd;
31         AuthRpc *rpc;
32         Attr *a;
33
34         if((afd = open("/mnt/factotum/rpc", ORDWR|OCEXEC)) < 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         
44         if((p = vsmprint(fmt, arg))==nil
45         || dorpc(rpc, "start", p, strlen(p), getkey) != ARok
46         || dorpc(rpc, "write", chal, nchal, getkey) != ARok
47         || dorpc(rpc, "read", nil, 0, getkey) != ARok){
48                 free(p);
49                 close(afd);
50                 auth_freerpc(rpc);
51                 return -1;
52         }
53         free(p);
54
55         if(rpc->narg < nresp)
56                 nresp = rpc->narg;
57         memmove(resp, rpc->arg, nresp);
58
59         if(ai != nil)
60                 *ai = auth_getinfo(rpc);
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 }
73
74 int
75 auth_respond(void *chal, uint nchal, char *user, uint nuser, void *resp, uint nresp,
76         AuthGetkey *getkey, char *fmt, ...)
77 {
78         va_list arg;
79         int ret;
80
81         va_start(arg, fmt);
82         ret = dorespond(chal, nchal, user, nuser, resp, nresp, nil, getkey, fmt, arg);
83         va_end(arg);
84         return ret;
85 }
86
87 int
88 auth_respondAI(void *chal, uint nchal, char *user, uint nuser, void *resp, uint nresp,
89         AuthInfo **ai, AuthGetkey *getkey, char *fmt, ...)
90 {
91         va_list arg;
92         int ret;
93
94         va_start(arg, fmt);
95         ret = dorespond(chal, nchal, user, nuser, resp, nresp, ai, getkey, fmt, arg);
96         va_end(arg);
97         return ret;
98 }