Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / kauth / kalocalcell.c
CommitLineData
805e021f
CE
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/* This is a utility routine that many parts of kauth use but it invokes the
25 afsconf package so its best to have it in a separate .o file to make the
26 linker happy. */
27
28static struct afsconf_dir *conf = 0;
29static char cell_name[MAXCELLCHARS];
30
31int
32ka_CellConfig(const char *dir)
33{
34 int code;
35
36 LOCK_GLOBAL_MUTEX;
37 if (conf)
38 afsconf_Close(conf);
39 conf = afsconf_Open(dir);
40 if (!conf) {
41 UNLOCK_GLOBAL_MUTEX;
42 return KANOCELLS;
43 }
44 code = afsconf_GetLocalCell(conf, cell_name, sizeof(cell_name));
45 UNLOCK_GLOBAL_MUTEX;
46 return code;
47}
48
49char *
50ka_LocalCell(void)
51{
52 int code = 0;
53
54 LOCK_GLOBAL_MUTEX;
55 if (conf) {
56 UNLOCK_GLOBAL_MUTEX;
57 return cell_name;
58 }
59 if ((conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
60 code = afsconf_GetLocalCell(conf, cell_name, sizeof(cell_name));
61/* leave conf open so we can lookup other cells */
62/* afsconf_Close (conf); */
63 }
64 if (!conf || code) {
65 printf("** Can't determine local cell name!\n");
66 conf = 0;
67 UNLOCK_GLOBAL_MUTEX;
68 return 0;
69 }
70 UNLOCK_GLOBAL_MUTEX;
71 return cell_name;
72}
73
74int
75ka_ExpandCell(char *cell, char *fullCell, int *alocal)
76{
77 int local = 0;
78 int code;
79 char cellname[MAXKTCREALMLEN];
80 struct afsconf_cell cellinfo; /* storage for cell info */
81
82 LOCK_GLOBAL_MUTEX;
83 ka_LocalCell(); /* initialize things */
84 if (!conf) {
85 UNLOCK_GLOBAL_MUTEX;
86 return KANOCELLS;
87 }
88
89 if ((cell == 0) || (strlen(cell) == 0)) {
90 local = 1;
91 cell = cell_name;
92 } else {
93 cell = lcstring(cellname, cell, sizeof(cellname));
94 code = afsconf_GetCellInfo(conf, cell, 0, &cellinfo);
95 if (code) {
96 UNLOCK_GLOBAL_MUTEX;
97 return KANOCELL;
98 }
99 cell = cellinfo.name;
100 }
101 if (strcmp(cell, cell_name) == 0)
102 local = 1;
103
104 if (fullCell)
105 strcpy(fullCell, cell);
106 if (alocal)
107 *alocal = local;
108 UNLOCK_GLOBAL_MUTEX;
109 return 0;
110}
111
112int
113ka_CellToRealm(char *cell, char *realm, int *local)
114{
115 int code = 0;
116
117 LOCK_GLOBAL_MUTEX;
118 code = ka_ExpandCell(cell, realm, local);
119 ucstring(realm, realm, MAXKTCREALMLEN);
120 UNLOCK_GLOBAL_MUTEX;
121 return code;
122}