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