afsconf-leak-20060915
[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 #include <rx/rxkad.h>
52
53 /*
54   Get the appropriate type of ubik client structure out from the system.
55 */
56 afs_int32
57 ugen_ClientInit(int noAuthFlag, char *confDir, char *cellName, afs_int32 sauth,
58                struct ubik_client **uclientp, int (*secproc) (),
59                char *funcName, afs_int32 gen_rxkad_level, 
60                afs_int32 maxservers, char *serviceid, afs_int32 deadtime,
61                afs_uint32 server, afs_uint32 port, afs_int32 usrvid)
62 {
63     afs_int32 code, scIndex, i;
64     struct afsconf_cell info;
65     struct afsconf_dir *tdir;
66     struct ktc_principal sname;
67     struct ktc_token ttoken;
68     struct rx_securityClass *sc;
69     /* This must change if VLDB_MAXSERVERS becomes larger than MAXSERVERS */
70     static struct rx_connection *serverconns[MAXSERVERS];
71     char cellstr[64];
72
73     code = rx_Init(0);
74     if (code) {
75         fprintf(stderr, "%s: could not initialize rx.\n", funcName);
76         return code;
77     }
78     rx_SetRxDeadTime(deadtime);
79
80     if (sauth) {                /* -localauth */
81         tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
82         if (!tdir) {
83             fprintf(stderr,
84                     "%s: Could not process files in configuration directory (%s).\n",
85                     funcName, AFSDIR_SERVER_ETC_DIRPATH);
86             return -1;
87         }
88         code = afsconf_ClientAuth(tdir, &sc, &scIndex); /* sets sc,scIndex */
89         if (code) {
90             afsconf_Close(tdir);
91             fprintf(stderr,
92                     "%s: Could not get security object for -localAuth\n",
93                     funcName);
94             return -1;
95         }
96         code =
97             afsconf_GetCellInfo(tdir, tdir->cellName, serviceid,
98                                 &info);
99         if (code) {
100             afsconf_Close(tdir);
101             fprintf(stderr,
102                     "%s: can't find cell %s's hosts in %s/%s\n",
103                     funcName, cellName, AFSDIR_SERVER_ETC_DIRPATH,
104                     AFSDIR_CELLSERVDB_FILE);
105             exit(1);
106         }
107     } else {                    /* not -localauth */
108         tdir = afsconf_Open(confDir);
109         if (!tdir) {
110             fprintf(stderr,
111                     "%s: Could not process files in configuration directory (%s).\n",
112                     funcName, confDir);
113             return -1;
114         }
115
116         if (!cellName) {
117             code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
118             if (code) {
119                 fprintf(stderr,
120                         "%s: can't get local cellname, check %s/%s\n",
121                         funcName, confDir, AFSDIR_THISCELL_FILE);
122                 exit(1);
123             }
124             cellName = cellstr;
125         }
126
127         code =
128             afsconf_GetCellInfo(tdir, cellName, serviceid, &info);
129         if (code) {
130             fprintf(stderr,
131                     "%s: can't find cell %s's hosts in %s/%s\n",
132                     funcName, cellName, confDir, AFSDIR_CELLSERVDB_FILE);
133             exit(1);
134         }
135         if (noAuthFlag)         /* -noauth */
136             scIndex = 0;
137         else {                  /* not -noauth */
138             strcpy(sname.cell, info.name);
139             sname.instance[0] = 0;
140             strcpy(sname.name, "afs");
141             code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
142             if (code) {         /* did not get ticket */
143                 fprintf(stderr,
144                         "%s: Could not get afs tokens, running unauthenticated.\n",
145                         funcName);
146                 scIndex = 0;
147             } else {            /* got a ticket */
148                 scIndex = 2;
149                 if ((ttoken.kvno < 0) || (ttoken.kvno > 256)) {
150                     fprintf(stderr,
151                             "%s: funny kvno (%d) in ticket, proceeding\n",
152                             funcName, ttoken.kvno);
153                 }
154             }
155         }
156
157         switch (scIndex) {
158         case 0:
159             sc = rxnull_NewClientSecurityObject();
160             break;
161         case 2:
162             sc = rxkad_NewClientSecurityObject(gen_rxkad_level,
163                                                &ttoken.sessionKey,
164                                                ttoken.kvno, ttoken.ticketLen,
165                                                ttoken.ticket);
166             break;
167         default:
168             fprintf(stderr, "%s: unsupported security index %d\n",
169                     funcName, scIndex);
170             exit(1);
171             break;
172         }
173     }
174
175     afsconf_Close(tdir);
176
177     if (secproc)        /* tell UV module about default authentication */
178         (*secproc) (sc, scIndex);
179     if (server) {
180         serverconns[0] = rx_NewConnection(server, port,
181                                           usrvid, sc, scIndex);
182     } else {
183         if (info.numServers > maxservers) {
184             fprintf(stderr,
185                     "%s: info.numServers=%d (> maxservers=%d)\n",
186                     funcName, info.numServers, maxservers);
187             exit(1);
188         }
189         for (i = 0; i < info.numServers; i++) {
190             serverconns[i] =
191                 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
192                                  info.hostAddr[i].sin_port, usrvid,
193                                  sc, scIndex);
194         }
195     }
196     /* Are we just setting up connections, or is this really ubik stuff? */
197     if (uclientp) {
198         *uclientp = 0;
199         code = ubik_ClientInit(serverconns, uclientp);
200         if (code) {
201             fprintf(stderr, "%s: ubik client init failed.\n", funcName);
202             return code;
203         }
204     }
205     return 0;
206 }
207
208