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