2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (C) 1989 by NeXT, Inc.
29 #include <mach/mach.h>
33 #include <rpc/types.h>
36 #include "_lu_types.h"
39 #include "lu_overrides.h"
41 #define FIX(x) ((x == NULL) ? NULL : (_lu_string *)&(x))
51 free_netgroup_data(struct lu_netgrent
*ng
)
53 if (ng
== NULL
) return;
55 if (ng
->ng_host
!= NULL
) free(ng
->ng_host
);
56 if (ng
->ng_user
!= NULL
) free(ng
->ng_user
);
57 if (ng
->ng_domain
!= NULL
) free(ng
->ng_domain
);
61 free_netgroup(struct lu_netgrent
*ng
)
63 if (ng
== NULL
) return;
64 free_netgroup_data(ng
);
69 free_lu_thread_info_netgroup(void *x
)
71 struct lu_thread_info
*tdata
;
73 if (x
== NULL
) return;
75 tdata
= (struct lu_thread_info
*)x
;
77 if (tdata
->lu_entry
!= NULL
)
79 free_netgroup((struct lu_netgrent
*)tdata
->lu_entry
);
80 tdata
->lu_entry
= NULL
;
83 _lu_data_free_vm_xdr(tdata
);
88 static struct lu_netgrent
*
89 extract_netgroup(XDR
*xdr
)
92 struct lu_netgrent
*ng
;
94 if (xdr
== NULL
) return NULL
;
100 if (!xdr_string(xdr
, &h
, LU_LONG_STRING_LENGTH
))
105 if (!xdr_string(xdr
, &u
, LU_LONG_STRING_LENGTH
))
111 if (!xdr_string(xdr
, &d
, LU_LONG_STRING_LENGTH
))
118 ng
= (struct lu_netgrent
*)calloc(1, sizeof(struct lu_netgrent
));
128 static struct lu_netgrent
*
129 copy_netgroup(struct lu_netgrent
*in
)
131 struct lu_netgrent
*ng
;
133 if (in
== NULL
) return NULL
;
135 ng
= (struct group
*)calloc(1, sizeof(struct lu_netgrent
));
137 ng
->ng_host
= LU_COPY_STRING(in
->ng_host
);
138 ng
->ng_user
= LU_COPY_STRING(in
->ng_user
);
139 ng
->ng_domain
= LU_COPY_STRING(in
->ng_domain
);
146 recycle_netgroup(struct lu_thread_info
*tdata
, struct lu_netgrent
*in
)
148 struct lu_netgrent
*ng
;
150 if (tdata
== NULL
) return;
151 ng
= (struct lu_netgrent
*)tdata
->lu_entry
;
156 tdata
->lu_entry
= NULL
;
159 if (tdata
->lu_entry
== NULL
)
161 tdata
->lu_entry
= in
;
165 free_netgroup_data(ng
);
167 ng
->ng_host
= in
->ng_host
;
168 ng
->ng_user
= in
->ng_user
;
169 ng
->ng_domain
= in
->ng_domain
;
176 lu_innetgr(const char *group
, const char *host
, const char *user
,
181 char namebuf
[4*_LU_MAXLUSTRLEN
+ 3*BYTES_PER_XDR_UNIT
];
182 static int proc
= -1;
185 _lu_innetgr_args args
;
190 if (_lookup_link(_lu_port
, "innetgr", &proc
) != KERN_SUCCESS
)
196 args
.group
= (char *)group
;
197 args
.host
= FIX(host
);
198 args
.user
= FIX(user
);
199 args
.domain
= FIX(domain
);
201 xdrmem_create(&xdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
202 if (!xdr__lu_innetgr_args(&xdr
, &args
))
208 size
= xdr_getpos(&xdr
) / BYTES_PER_XDR_UNIT
;
214 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, size
, &lookup_buf
, &datalen
) != KERN_SUCCESS
)
219 datalen
*= BYTES_PER_XDR_UNIT
;
220 if ((lookup_buf
== NULL
) || (datalen
== 0)) return 0;
222 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
223 if (!xdr_int(&xdr
, &res
))
226 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
231 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
239 struct lu_thread_info
*tdata
;
241 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
242 _lu_data_free_vm_xdr(tdata
);
247 * This is different than the other setXXXent routines
248 * since this is really more like getnetgrbyname() than
252 lu_setnetgrent(const char *name
)
255 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
257 static int proc
= -1;
258 struct lu_thread_info
*tdata
;
260 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
263 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
264 _lu_data_set_key(_lu_data_key_netgroup
, tdata
);
271 if (_lookup_link(_lu_port
, "getnetgrent", &proc
) != KERN_SUCCESS
)
278 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
279 if (!xdr__lu_string(&outxdr
, (_lu_string
*)&name
))
281 xdr_destroy(&outxdr
);
286 datalen
= xdr_getpos(&outxdr
);
287 xdr_destroy(&outxdr
);
288 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, datalen
/ BYTES_PER_XDR_UNIT
, &(tdata
->lu_vm
), &(tdata
->lu_vm_length
)) != KERN_SUCCESS
)
294 /* mig stubs measure size in words (4 bytes) */
295 tdata
->lu_vm_length
*= 4;
297 if (tdata
->lu_xdr
!= NULL
)
299 xdr_destroy(tdata
->lu_xdr
);
302 tdata
->lu_xdr
= (XDR
*)calloc(1, sizeof(XDR
));
304 xdrmem_create(tdata
->lu_xdr
, tdata
->lu_vm
, tdata
->lu_vm_length
, XDR_DECODE
);
305 if (!xdr_int(tdata
->lu_xdr
, &tdata
->lu_vm_cursor
)) lu_endnetgrent();
309 static struct lu_netgrent
*
312 struct lu_netgrent
*ng
;
313 struct lu_thread_info
*tdata
;
315 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
316 if (tdata
== NULL
) return NULL
;
318 if (tdata
->lu_vm_cursor
== 0)
324 ng
= extract_netgroup(tdata
->lu_xdr
);
331 tdata
->lu_vm_cursor
--;
337 innetgr(const char *group
, const char *host
, const char *user
,
340 if (_lu_running()) return (lu_innetgr(group
, host
, user
, domain
));
345 getnetgrent(char **host
, char **user
, char **domain
)
347 struct lu_netgrent
*res
= NULL
;
348 struct lu_thread_info
*tdata
;
350 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
353 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
354 _lu_data_set_key(_lu_data_key_netgroup
, tdata
);
358 if (_lu_running()) res
= lu_getnetgrent();
360 recycle_netgroup(tdata
, res
);
361 if (res
== NULL
) return 0;
363 if (host
!= NULL
) *host
= res
->ng_host
;
364 if (user
!= NULL
) *user
= res
->ng_user
;
365 if (domain
!= NULL
) *domain
= res
->ng_domain
;
371 setnetgrent(const char *name
)
373 if (_lu_running()) lu_setnetgrent(name
);
379 if (_lu_running()) lu_endnetgrent();