Libinfo-78.tar.gz
[apple/libinfo.git] / lookup.subproj / lu_alias.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Alias lookup
26 * Copyright (C) 1989 by NeXT, Inc.
27 */
28
29 #include <stdlib.h>
30 #include <mach/mach.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <rpc/types.h>
34 #include <rpc/xdr.h>
35 #include <aliasdb.h>
36
37 #include "lookup.h"
38 #include "_lu_types.h"
39 #include "lu_utils.h"
40 #include "lu_overrides.h"
41
42 static lookup_state alias_state = LOOKUP_CACHE;
43 static struct aliasent global_aliasent;
44 static int global_free = 1;
45 static char *alias_data = NULL;
46 static unsigned alias_datalen;
47 static int alias_nentries = 0;
48 static int alias_start = 1;
49 static XDR alias_xdr;
50
51 static void
52 freeold(void)
53 {
54 int i, len;
55
56 if (global_free == 1) return;
57
58 free(global_aliasent.alias_name);
59
60 len = global_aliasent.alias_members_len;
61 for (i = 0; i < len; i++)
62 free(global_aliasent.alias_members[i]);
63
64 free(global_aliasent.alias_members);
65
66 global_free = 1;
67 }
68
69 static void
70 convert_aliasent(_lu_aliasent *lu_aliasent)
71 {
72 int i, len;
73
74 freeold();
75
76 global_aliasent.alias_name = strdup(lu_aliasent->alias_name);
77
78 len = lu_aliasent->alias_members.alias_members_len;
79 global_aliasent.alias_members_len = len;
80 global_aliasent.alias_members = (char **)malloc(len * sizeof(char *));
81
82 for (i = 0; i < len; i++)
83 {
84 global_aliasent.alias_members[i] =
85 strdup(lu_aliasent->alias_members.alias_members_val[i]);
86 }
87
88 global_aliasent.alias_local = lu_aliasent->alias_local;
89
90 global_free = 0;
91 }
92
93 static struct aliasent *
94 lu_alias_getbyname(const char *name)
95 {
96 unsigned datalen;
97 char namebuf[_LU_MAXLUSTRLEN + BYTES_PER_XDR_UNIT];
98 unit lookup_buf[MAX_INLINE_UNITS];
99 XDR outxdr;
100 XDR inxdr;
101 _lu_aliasent_ptr lu_aliasent;
102 static int proc = -1;
103
104 if (proc < 0)
105 {
106 if (_lookup_link(_lu_port, "alias_getbyname", &proc) != KERN_SUCCESS)
107 {
108 return (NULL);
109 }
110 }
111
112 xdrmem_create(&outxdr, namebuf, sizeof(namebuf), XDR_ENCODE);
113 if (!xdr__lu_string(&outxdr, &name))
114 {
115 xdr_destroy(&outxdr);
116 return (NULL);
117 }
118
119 datalen = MAX_INLINE_UNITS;
120 if (_lookup_one(_lu_port, proc, (unit *)namebuf,
121 xdr_getpos(&outxdr) / BYTES_PER_XDR_UNIT, lookup_buf, &datalen)
122 != KERN_SUCCESS)
123 {
124 xdr_destroy(&outxdr);
125 return (NULL);
126 }
127
128 xdr_destroy(&outxdr);
129
130 datalen *= BYTES_PER_XDR_UNIT;
131 xdrmem_create(&inxdr, lookup_buf, datalen,
132 XDR_DECODE);
133 lu_aliasent = NULL;
134 if (!xdr__lu_aliasent_ptr(&inxdr, &lu_aliasent) || (lu_aliasent == NULL))
135 {
136 xdr_destroy(&inxdr);
137 return (NULL);
138 }
139
140 xdr_destroy(&inxdr);
141
142 convert_aliasent(lu_aliasent);
143 xdr_free(xdr__lu_aliasent_ptr, &lu_aliasent);
144 return (&global_aliasent);
145 }
146
147 static void
148 lu_alias_endent(void)
149 {
150 alias_nentries = 0;
151 if (alias_data != NULL)
152 {
153 freeold();
154 vm_deallocate(mach_task_self(), (vm_address_t)alias_data, alias_datalen);
155 alias_data = NULL;
156 }
157 }
158
159 static void
160 lu_alias_setent(void)
161 {
162 lu_alias_endent();
163 alias_start = 1;
164 }
165
166 static struct aliasent *
167 lu_alias_getent(void)
168 {
169 static int proc = -1;
170 _lu_aliasent lu_aliasent;
171
172 if (alias_start == 1)
173 {
174 alias_start = 0;
175
176 if (proc < 0)
177 {
178 if (_lookup_link(_lu_port, "alias_getent", &proc) != KERN_SUCCESS)
179 {
180 lu_alias_endent();
181 return (NULL);
182 }
183 }
184
185 if (_lookup_all(_lu_port, proc, NULL, 0, &alias_data, &alias_datalen)
186 != KERN_SUCCESS)
187 {
188 lu_alias_endent();
189 return (NULL);
190 }
191
192 #ifdef NOTDEF
193 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
194 alias_datalen *= BYTES_PER_XDR_UNIT;
195 #endif
196 xdrmem_create(&alias_xdr, alias_data,
197 alias_datalen, XDR_DECODE);
198 if (!xdr_int(&alias_xdr, &alias_nentries))
199 {
200 xdr_destroy(&alias_xdr);
201 lu_alias_endent();
202 return (NULL);
203 }
204 }
205
206 if (alias_nentries == 0)
207 {
208 xdr_destroy(&alias_xdr);
209 lu_alias_endent();
210 return (NULL);
211 }
212
213 bzero(&lu_aliasent, sizeof(lu_aliasent));
214 if (!xdr__lu_aliasent(&alias_xdr, &lu_aliasent))
215 {
216 xdr_destroy(&alias_xdr);
217 lu_alias_endent();
218 return (NULL);
219 }
220
221 alias_nentries--;
222 convert_aliasent(&lu_aliasent);
223 xdr_free(xdr__lu_aliasent, &lu_aliasent);
224 return (&global_aliasent);
225 }
226
227 struct aliasent *
228 alias_getbyname(const char *name)
229 {
230 LOOKUP1(lu_alias_getbyname, _old_alias_getbyname, name, struct aliasent);
231 }
232
233 struct aliasent *
234 alias_getent(void)
235 {
236 GETENT(lu_alias_getent, _old_alias_getent, &alias_state, struct aliasent);
237 }
238
239 void
240 alias_setent(void)
241 {
242 SETSTATEVOID(lu_alias_setent, _old_alias_setent, &alias_state);
243 }
244
245 void
246 alias_endent(void)
247 {
248 UNSETSTATE(lu_alias_endent, _old_alias_endent, &alias_state);
249 }