initialize-variables-to-avoid-logic-issues-20081027
[openafs.git] / src / kauth / kalocalcell.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 #if defined(UKERNEL)
12 #include "afs/param.h"
13 #else
14 #include <afs/param.h>
15 #endif
16
17 RCSID
18     ("$Header$");
19
20 #if defined(UKERNEL)
21 #include "afs/pthread_glock.h"
22 #include "afs/sysincludes.h"
23 #include "afsincludes.h"
24 #include "afs_usrops.h"
25 #include "afs/cellconfig.h"
26 #include "afs/pthread_glock.h"
27 #include "rx/xdr.h"
28 #include "rx/rx.h"
29 #include "afs/kauth.h"
30 #include "afs/kautils.h"
31 #include "afs/afsutil.h"
32 #else /* defined(UKERNEL) */
33 #include <afs/pthread_glock.h>
34 #include <sys/types.h>
35 #ifdef AFS_NT40_ENV
36 #include <winsock2.h>
37 #else
38 #include <netinet/in.h>
39 #endif
40 #include <string.h>
41 #include <afs/cellconfig.h>
42 #include <rx/xdr.h>
43 #include <rx/rx.h>
44 #include "kauth.h"
45 #include "kautils.h"
46 #include <afs/afsutil.h>
47 #endif /* defined(UKERNEL) */
48
49 /* This is a utility routine that many parts of kauth use but it invokes the
50    afsconf package so its best to have it in a separate .o file to make the
51    linker happy. */
52
53 static struct afsconf_dir *conf = 0;
54 static char cell_name[MAXCELLCHARS];
55
56 int
57 ka_CellConfig(const char *dir)
58 {
59     int code;
60 #ifdef UKERNEL
61     conf = afs_cdir;
62     strcpy(cell_name, afs_LclCellName);
63     return 0;
64 #else /* UKERNEL */
65     LOCK_GLOBAL_MUTEX;
66     if (conf)
67         afsconf_Close(conf);
68     conf = afsconf_Open(dir);
69     if (!conf) {
70         UNLOCK_GLOBAL_MUTEX;
71         return KANOCELLS;
72     }
73     code = afsconf_GetLocalCell(conf, cell_name, sizeof(cell_name));
74     UNLOCK_GLOBAL_MUTEX;
75     return code;
76 #endif /* UKERNEL */
77 }
78
79 char *
80 ka_LocalCell(void)
81 {
82     int code = 0;
83
84     LOCK_GLOBAL_MUTEX;
85     if (conf) {
86         UNLOCK_GLOBAL_MUTEX;
87         return cell_name;
88     }
89 #ifdef UKERNEL
90     conf = afs_cdir;
91     strcpy(cell_name, afs_LclCellName);
92 #else /* UKERNEL */
93     if (conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH)) {
94         code = afsconf_GetLocalCell(conf, cell_name, sizeof(cell_name));
95 /* leave conf open so we can lookup other cells */
96 /* afsconf_Close (conf); */
97     }
98     if (!conf || code) {
99         printf("** Can't determine local cell name!\n");
100         conf = 0;
101         UNLOCK_GLOBAL_MUTEX;
102         return 0;
103     }
104 #endif /* UKERNEL */
105     UNLOCK_GLOBAL_MUTEX;
106     return cell_name;
107 }
108
109 int
110 ka_ExpandCell(char *cell, char *fullCell, int *alocal)
111 {
112     int local = 0;
113     int code;
114     char cellname[MAXKTCREALMLEN];
115     struct afsconf_cell cellinfo;       /* storage for cell info */
116
117     LOCK_GLOBAL_MUTEX;
118     ka_LocalCell();             /* initialize things */
119     if (!conf) {
120         UNLOCK_GLOBAL_MUTEX;
121         return KANOCELLS;
122     }
123
124     if ((cell == 0) || (strlen(cell) == 0)) {
125         local = 1;
126         cell = cell_name;
127     } else {
128         cell = lcstring(cellname, cell, sizeof(cellname));
129         code = afsconf_GetCellInfo(conf, cell, 0, &cellinfo);
130         if (code) {
131             UNLOCK_GLOBAL_MUTEX;
132             return KANOCELL;
133         }
134         cell = cellinfo.name;
135     }
136     if (strcmp(cell, cell_name) == 0)
137         local = 1;
138
139     if (fullCell)
140         strcpy(fullCell, cell);
141     if (alocal)
142         *alocal = local;
143     UNLOCK_GLOBAL_MUTEX;
144     return 0;
145 }
146
147 int
148 ka_CellToRealm(char *cell, char *realm, int *local)
149 {
150     int code;
151
152     LOCK_GLOBAL_MUTEX;
153     code = ka_ExpandCell(cell, realm, local);
154     ucstring(realm, realm, MAXKTCREALMLEN);
155     UNLOCK_GLOBAL_MUTEX;
156     return code;
157 }