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