]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libauthsrv/authdial.c
merge
[plan9front.git] / sys / src / libauthsrv / authdial.c
1 #include <u.h>
2 #include <libc.h>
3 #include <authsrv.h>
4 #include <bio.h>
5 #include <ndb.h>
6
7 int
8 authdial(char *netroot, char *dom)
9 {
10         Ndbtuple *t, *nt;
11         char *p;
12         int rv;
13
14         if(dom == nil)
15                 /* look for one relative to my machine */
16                 return dial(netmkaddr("$auth", netroot, "ticket"), 0, 0, 0);
17
18         /* look up an auth server in an authentication domain */
19         p = csgetvalue(netroot, "authdom", dom, "auth", &t);
20
21         /* if that didn't work, just try the IP domain */
22         if(p == nil)
23                 p = csgetvalue(netroot, "dom", dom, "auth", &t);
24
25         /*
26          * if that didn't work, try p9auth.$dom.  this is very helpful if
27          * you can't edit /lib/ndb.
28          */
29         if(p == nil) {
30                 p = smprint("p9auth.%s", dom);
31                 t = ndbnew("auth", p);
32         }
33         free(p);
34
35         /*
36          * allow multiple auth= attributes for backup auth servers,
37          * try each one in order.
38          */
39         rv = -1;
40         for(nt = t; nt != nil; nt = nt->entry) {
41                 if(strcmp(nt->attr, "auth") == 0) {
42                         p = netmkaddr(nt->val, netroot, "ticket");
43                         rv = dial(p, 0, 0, 0);
44                         if(rv >= 0)
45                                 break;
46                 }
47         }
48         ndbfree(t);
49
50         return rv;
51 }