23f44019dc94671139033cf8d90dc713e2374fb3
[openafs.git] / src / ubik / uinit.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #include <afs/stds.h>
17 #ifdef AFS_NT40_ENV
18 #include <fcntl.h>
19 #include <winsock2.h>
20 #else
21 #include <sys/types.h>
22 #include <sys/file.h>
23 #include <netdb.h>
24 #include <netinet/in.h>
25 #endif /* AFS_NT40_ENV */
26 #include <sys/stat.h>
27 #ifdef AFS_AIX_ENV
28 #include <sys/statfs.h>
29 #endif
30
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #else
34 #ifdef HAVE_STRINGS_H
35 #include <strings.h>
36 #endif
37 #endif
38
39 #include <afs/dirpath.h>
40 #include <errno.h>
41 #include <lock.h>
42 #include <rx/xdr.h>
43 #include <rx/rx.h>
44 #include <rx/rx_globals.h>
45 #include <afs/auth.h>
46 #include <afs/cellconfig.h>
47 #include <afs/keys.h>
48 #include <ubik.h>
49 #include <afs/afsint.h>
50 #include <afs/cmd.h>
51
52 /*
53   Get the appropriate type of ubik client structure out from the system.
54 */
55 afs_int32
56 ugen_ClientInit(int noAuthFlag, char *confDir, char *cellName, afs_int32 sauth,
57                struct ubik_client **uclientp, int (*secproc) (),
58                char *funcName, afs_int32 gen_rxkad_level, 
59                afs_int32 maxservers, char *serviceid, afs_int32 deadtime,
60                afs_uint32 server, afs_uint32 port, afs_int32 usrvid)
61 {
62     afs_int32 code, scIndex, i;
63     struct afsconf_cell info;
64     struct afsconf_dir *tdir;
65     struct ktc_principal sname;
66     struct ktc_token ttoken;
67     struct rx_securityClass *sc;
68     /* This must change if VLDB_MAXSERVERS becomes larger than MAXSERVERS */
69     static struct rx_connection *serverconns[MAXSERVERS];
70     char cellstr[64];
71
72     code = rx_Init(0);
73     if (code) {
74         fprintf(stderr, "%s: could not initialize rx.\n", funcName);
75         return code;
76     }
77     rx_SetRxDeadTime(deadtime);
78
79     if (sauth) {                /* -localauth */
80         tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
81         if (!tdir) {
82             fprintf(stderr,
83                     "%s: Could not process files in configuration directory (%s).\n",
84                     funcName, AFSDIR_SERVER_ETC_DIRPATH);
85             return -1;
86         }
87         code = afsconf_ClientAuth(tdir, &sc, &scIndex); /* sets sc,scIndex */
88         if (code) {
89             afsconf_Close(tdir);
90             fprintf(stderr,
91                     "%s: Could not get security object for -localAuth\n",
92                     funcName);
93             return -1;
94         }
95         code =
96             afsconf_GetCellInfo(tdir, tdir->cellName, serviceid,
97                                 &info);
98         if (code) {
99             afsconf_Close(tdir);
100             fprintf(stderr,
101                     "%s: can't find cell %s's hosts in %s/%s\n",
102                     funcName, cellName, AFSDIR_SERVER_ETC_DIRPATH,
103                     AFSDIR_CELLSERVDB_FILE);
104             exit(1);
105         }
106     } else {                    /* not -localauth */
107         tdir = afsconf_Open(confDir);
108         if (!tdir) {
109             fprintf(stderr,
110                     "%s: Could not process files in configuration directory (%s).\n",
111                     funcName, confDir);
112             return -1;
113         }
114
115         if (!cellName) {
116             code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
117             if (code) {
118                 fprintf(stderr,
119                         "%s: can't get local cellname, check %s/%s\n",
120                         funcName, confDir, AFSDIR_THISCELL_FILE);
121                 exit(1);
122             }
123             cellName = cellstr;
124         }
125
126         code =
127             afsconf_GetCellInfo(tdir, cellName, serviceid, &info);
128         if (code) {
129             fprintf(stderr,
130                     "%s: can't find cell %s's hosts in %s/%s\n",
131                     funcName, cellName, confDir, AFSDIR_CELLSERVDB_FILE);
132             exit(1);
133         }
134         if (noAuthFlag)         /* -noauth */
135             scIndex = 0;
136         else {                  /* not -noauth */
137             strcpy(sname.cell, info.name);
138             sname.instance[0] = 0;
139             strcpy(sname.name, "afs");
140             code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
141             if (code) {         /* did not get ticket */
142                 fprintf(stderr,
143                         "%s: Could not get afs tokens, running unauthenticated.\n",
144                         funcName);
145                 scIndex = 0;
146             } else {            /* got a ticket */
147                 scIndex = 2;
148                 if ((ttoken.kvno < 0) || (ttoken.kvno > 256)) {
149                     fprintf(stderr,
150                             "%s: funny kvno (%d) in ticket, proceeding\n",
151                             funcName, ttoken.kvno);
152                 }
153             }
154         }
155
156         switch (scIndex) {
157         case 0:
158             sc = rxnull_NewClientSecurityObject();
159             break;
160         case 2:
161             sc = rxkad_NewClientSecurityObject(gen_rxkad_level,
162                                                &ttoken.sessionKey,
163                                                ttoken.kvno, ttoken.ticketLen,
164                                                ttoken.ticket);
165             break;
166         default:
167             fprintf(stderr, "%s: unsupported security index %d\n",
168                     funcName, scIndex);
169             exit(1);
170             break;
171         }
172     }
173
174     afsconf_Close(tdir);
175
176     if (secproc)        /* tell UV module about default authentication */
177         (*secproc) (sc, scIndex);
178     if (server) {
179         serverconns[0] = rx_NewConnection(server, port,
180                                           usrvid, sc, scIndex);
181     } else {
182         if (info.numServers > maxservers) {
183             fprintf(stderr,
184                     "%s: info.numServers=%d (> maxservers=%d)\n",
185                     funcName, info.numServers, maxservers);
186             exit(1);
187         }
188         for (i = 0; i < info.numServers; i++) {
189             serverconns[i] =
190                 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
191                                  info.hostAddr[i].sin_port, usrvid,
192                                  sc, scIndex);
193         }
194     }
195     /* Are we just setting up connections, or is this really ubik stuff? */
196     if (uclientp) {
197         *uclientp = 0;
198         code = ubik_ClientInit(serverconns, uclientp);
199         if (code) {
200             fprintf(stderr, "%s: ubik client init failed.\n", funcName);
201             return code;
202         }
203     }
204     return 0;
205 }
206
207