death to trailing whitespace
[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
14 #include <afs/stds.h>
15 #ifdef AFS_NT40_ENV
16 #include <fcntl.h>
17 #include <winsock2.h>
18 #else
19 #include <sys/types.h>
20 #include <sys/file.h>
21 #include <netdb.h>
22 #include <netinet/in.h>
23 #endif /* AFS_NT40_ENV */
24 #include <sys/stat.h>
25 #ifdef AFS_AIX_ENV
26 #include <sys/statfs.h>
27 #endif
28
29 #include <string.h>
30
31 #include <afs/dirpath.h>
32 #include <errno.h>
33 #include <lock.h>
34 #include <rx/xdr.h>
35 #include <rx/rx.h>
36 #include <rx/rx_globals.h>
37 #include <afs/auth.h>
38 #include <afs/cellconfig.h>
39 #include <afs/keys.h>
40 #include <ubik.h>
41 #include <afs/afsint.h>
42 #include <afs/cmd.h>
43
44 /*!
45  * \brief Get the appropriate type of ubik client structure out from the system.
46  */
47 afs_int32
48 ugen_ClientInit(int noAuthFlag, const char *confDir, char *cellName, afs_int32 sauth,
49                struct ubik_client **uclientp,
50                int (*secproc) (struct rx_securityClass *, afs_int32),
51                char *funcName, afs_int32 gen_rxkad_level,
52                afs_int32 maxservers, char *serviceid, afs_int32 deadtime,
53                afs_uint32 server, afs_uint32 port, afs_int32 usrvid)
54 {
55     afs_int32 code, secFlags, i;
56     afs_int32 scIndex;
57     struct afsconf_cell info;
58     struct afsconf_dir *tdir;
59     struct rx_securityClass *sc;
60     /* This must change if VLDB_MAXSERVERS becomes larger than MAXSERVERS */
61     static struct rx_connection *serverconns[MAXSERVERS];
62     const char *confdir;
63
64     code = rx_Init(0);
65     if (code) {
66         fprintf(stderr, "%s: could not initialize rx.\n", funcName);
67         return code;
68     }
69     rx_SetRxDeadTime(deadtime);
70
71     secFlags = AFSCONF_SECOPTS_FALLBACK_NULL;
72     if (sauth) {
73         secFlags |= AFSCONF_SECOPTS_LOCALAUTH;
74         confdir = AFSDIR_SERVER_ETC_DIRPATH;
75     } else {
76         confdir = AFSDIR_CLIENT_ETC_DIRPATH;
77     }
78
79     if (noAuthFlag) {
80         secFlags |= AFSCONF_SECOPTS_NOAUTH;
81     }
82
83     tdir = afsconf_Open(confdir);
84     if (!tdir) {
85         fprintf(stderr,
86                 "%s: Could not process files in configuration directory (%s).\n",
87                 funcName, confdir);
88         return -1;
89     }
90
91     if (sauth)
92         cellName = tdir->cellName;
93
94     code = afsconf_GetCellInfo(tdir, cellName, serviceid, &info);
95     if (code) {
96         afsconf_Close(tdir);
97         fprintf(stderr, "%s: can't find cell %s's hosts in %s/%s\n",
98                 funcName, cellName, confdir, AFSDIR_CELLSERVDB_FILE);
99         return -1;
100     }
101     code = afsconf_PickClientSecObj(tdir, secFlags, &info, cellName, &sc,
102                                     &scIndex, NULL);
103     if (code) {
104         fprintf(stderr, "%s: can't create client security object", funcName);
105         return -1;
106     }
107     if (scIndex == RX_SECIDX_NULL && !noAuthFlag) {
108         fprintf(stderr,
109                 "%s: Could not get afs tokens, running unauthenticated.\n",
110                 funcName);
111     }
112
113     afsconf_Close(tdir);
114
115     if (secproc)        /* tell UV module about default authentication */
116         (*secproc) (sc, scIndex);
117     if (server) {
118         serverconns[0] = rx_NewConnection(server, port,
119                                           usrvid, sc, scIndex);
120     } else {
121         if (info.numServers > maxservers) {
122             fprintf(stderr,
123                     "%s: info.numServers=%d (> maxservers=%d)\n",
124                     funcName, info.numServers, maxservers);
125             return -1;
126         }
127         for (i = 0; i < info.numServers; i++) {
128             serverconns[i] =
129                 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
130                                  info.hostAddr[i].sin_port, usrvid,
131                                  sc, scIndex);
132         }
133     }
134     /* Are we just setting up connections, or is this really ubik stuff? */
135     if (uclientp) {
136         *uclientp = 0;
137         code = ubik_ClientInit(serverconns, uclientp);
138         if (code) {
139             fprintf(stderr, "%s: ubik client init failed.\n", funcName);
140             return code;
141         }
142     }
143     return 0;
144 }
145
146