Libinfo-78.tar.gz
[apple/libinfo.git] / lookup.subproj / lu_netgroup.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 * Netgroup lookup
26 * Copyright (C) 1989 by NeXT, Inc.
27 */
28 #include <netgr.h>
29 #include <mach/mach.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <rpc/types.h>
34 #include <rpc/xdr.h>
35
36 #include "_lu_types.h"
37 #include "lookup.h"
38 #include "lu_utils.h"
39 #include "lu_overrides.h"
40
41 #define FIX(x) ((x == NULL) ? NULL : &(x))
42
43 static struct netgrent global_netgr;
44 static int global_free = 1;
45 static char *netgr_data = NULL;
46 static unsigned netgr_datalen;
47 static int netgr_nentries = 0;
48 static int netgr_start = 1;
49 static XDR netgr_xdr;
50
51 static void
52 freeold(void)
53 {
54 if (global_free == 1) return;
55
56 free(global_netgr.ng_host);
57 free(global_netgr.ng_user);
58 free(global_netgr.ng_domain);
59
60 global_free = 1;
61 }
62
63 static void
64 convert_netgr(_lu_netgrent *lu_netgr)
65 {
66 freeold();
67
68 global_netgr.ng_host = strdup(lu_netgr->ng_host);
69 global_netgr.ng_user = strdup(lu_netgr->ng_user);
70 global_netgr.ng_domain = strdup(lu_netgr->ng_domain);
71
72 global_free = 0;
73 }
74
75
76 static int
77 lu_innetgr(const char *group, const char *host, const char *user,
78 const char *domain)
79 {
80 unsigned datalen;
81 XDR xdr;
82 char namebuf[4*_LU_MAXLUSTRLEN + 3*BYTES_PER_XDR_UNIT];
83 static int proc = -1;
84 int size;
85 int res;
86 _lu_innetgr_args args;
87 unit lookup_buf[MAX_INLINE_UNITS];
88
89 if (proc < 0)
90 {
91 if (_lookup_link(_lu_port, "innetgr", &proc) != KERN_SUCCESS)
92 {
93 return (0);
94 }
95 }
96
97 args.group = (char *)group;
98 args.host = FIX(host);
99 args.user = FIX(user);
100 args.domain = FIX(domain);
101
102 xdrmem_create(&xdr, namebuf, sizeof(namebuf), XDR_ENCODE);
103 if (!xdr__lu_innetgr_args(&xdr, &args))
104 {
105 xdr_destroy(&xdr);
106 return (0);
107 }
108
109 size = xdr_getpos(&xdr) / BYTES_PER_XDR_UNIT;
110 xdr_destroy(&xdr);
111
112 datalen = MAX_INLINE_UNITS;
113 if (_lookup_one(_lu_port, proc, (unit *)namebuf, size, lookup_buf,
114 &datalen) != KERN_SUCCESS)
115 {
116 return (0);
117 }
118
119 datalen *= BYTES_PER_XDR_UNIT;
120 xdrmem_create(&xdr, lookup_buf, datalen, XDR_DECODE);
121 if (!xdr_int(&xdr, &res))
122 {
123 xdr_destroy(&xdr);
124 return (0);
125 }
126
127 xdr_destroy(&xdr);
128 return (res);
129 }
130
131 static void
132 lu_endnetgrent(void)
133 {
134 netgr_nentries = 0;
135 if (netgr_data != NULL)
136 {
137 freeold();
138 vm_deallocate(mach_task_self(), (vm_address_t)netgr_data, netgr_datalen);
139 netgr_data = NULL;
140 }
141 }
142
143 /*
144 * This is different than the other setXXXent routines
145 * since this is really more like getnetgrbyname() than
146 * getnetgrent().
147 */
148 static void
149 lu_setnetgrent(const char *group)
150 {
151 unsigned datalen;
152 char namebuf[_LU_MAXLUSTRLEN + BYTES_PER_XDR_UNIT];
153 XDR outxdr;
154 static int proc = -1;
155
156 lu_endnetgrent();
157
158 if (proc < 0)
159 {
160 if (_lookup_link(_lu_port, "getnetgrent", &proc) != KERN_SUCCESS)
161 {
162 lu_endnetgrent();
163 return;
164 }
165 }
166
167 xdrmem_create(&outxdr, namebuf, sizeof(namebuf), XDR_ENCODE);
168 if (!xdr__lu_string(&outxdr, &group))
169 {
170 xdr_destroy(&outxdr);
171 lu_endnetgrent();
172 return;
173 }
174
175 datalen = MAX_INLINE_UNITS;
176 if (_lookup_all(_lu_port, proc, (unit *)namebuf,
177 xdr_getpos(&outxdr) / BYTES_PER_XDR_UNIT,
178 &netgr_data, &netgr_datalen) != KERN_SUCCESS)
179 {
180 xdr_destroy(&outxdr);
181 lu_endnetgrent();
182 return;
183 }
184
185 xdr_destroy(&outxdr);
186
187 #ifdef NOTDEF
188 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
189 netgr_datalen *= BYTES_PER_XDR_UNIT;
190 #endif
191
192 xdrmem_create(&netgr_xdr, netgr_data,
193 netgr_datalen, XDR_DECODE);
194 if (!xdr_int(&netgr_xdr, &netgr_nentries))
195 {
196 xdr_destroy(&netgr_xdr);
197 lu_endnetgrent();
198 }
199 }
200
201
202 struct netgrent *
203 lu_getnetgrent(void)
204 {
205 _lu_netgrent lu_netgr;
206
207 if (netgr_nentries == 0)
208 {
209 xdr_destroy(&netgr_xdr);
210 lu_endnetgrent();
211 return (NULL);
212 }
213
214 bzero(&lu_netgr, sizeof(lu_netgr));
215 if (!xdr__lu_netgrent(&netgr_xdr, &lu_netgr))
216 {
217 xdr_destroy(&netgr_xdr);
218 lu_endnetgrent();
219 return (NULL);
220 }
221
222 netgr_nentries--;
223 convert_netgr(&lu_netgr);
224 xdr_free(xdr__lu_netgrent, &lu_netgr);
225 return (&global_netgr);
226 }
227
228 int
229 innetgr(const char *group, const char *host, const char *user,
230 const char *domain)
231 {
232 if (_lu_running()) return (lu_innetgr(group, host, user, domain));
233 // return (_old_innetgr(group, host, user, domain));
234 return (0);
235 }
236
237 struct netgrent *
238 getnetgrent(void)
239 {
240 if (_lu_running()) return (lu_getnetgrent());
241 // return (_old_getnetgrent());
242 return (NULL);
243 }
244
245 void
246 setnetgrent(const char *group)
247 {
248 if (_lu_running()) lu_setnetgrent(group);
249 // else _old_setnetgrent(group);
250 }
251
252 void
253 endnetgrent(void)
254 {
255 if (_lu_running()) lu_endnetgrent();
256 // else _old_endnetgrent();
257 }