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