2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Copyright (C) 1989 by NeXT, Inc.
30 #include <mach/mach.h>
34 #include <rpc/types.h>
37 #include "_lu_types.h"
40 #include "lu_overrides.h"
42 #define FIX(x) ((x == NULL) ? NULL : (_lu_string *)&(x))
45 free_netgroup_data(struct netgrent
*ng
)
47 if (ng
== NULL
) return;
49 if (ng
->ng_host
!= NULL
) free(ng
->ng_host
);
50 if (ng
->ng_user
!= NULL
) free(ng
->ng_user
);
51 if (ng
->ng_domain
!= NULL
) free(ng
->ng_domain
);
55 free_netgroup(struct netgrent
*ng
)
57 if (ng
== NULL
) return;
58 free_netgroup_data(ng
);
63 free_lu_thread_info_netgroup(void *x
)
65 struct lu_thread_info
*tdata
;
67 if (x
== NULL
) return;
69 tdata
= (struct lu_thread_info
*)x
;
71 if (tdata
->lu_entry
!= NULL
)
73 free_netgroup((struct netgrent
*)tdata
->lu_entry
);
74 tdata
->lu_entry
= NULL
;
77 _lu_data_free_vm_xdr(tdata
);
82 static struct netgrent
*
83 extract_netgroup(XDR
*xdr
)
88 if (xdr
== NULL
) return NULL
;
94 if (!xdr_string(xdr
, &h
, LU_LONG_STRING_LENGTH
))
99 if (!xdr_string(xdr
, &u
, LU_LONG_STRING_LENGTH
))
105 if (!xdr_string(xdr
, &d
, LU_LONG_STRING_LENGTH
))
112 ng
= (struct netgrent
*)calloc(1, sizeof(struct netgrent
));
122 static struct netgrent
*
123 copy_netgroup(struct netgrent
*in
)
127 if (in
== NULL
) return NULL
;
129 ng
= (struct group
*)calloc(1, sizeof(struct netgrent
));
131 ng
->ng_host
= LU_COPY_STRING(in
->ng_host
);
132 ng
->ng_user
= LU_COPY_STRING(in
->ng_user
);
133 ng
->ng_domain
= LU_COPY_STRING(in
->ng_domain
);
140 recycle_netgroup(struct lu_thread_info
*tdata
, struct netgrent
*in
)
144 if (tdata
== NULL
) return;
145 ng
= (struct netgrent
*)tdata
->lu_entry
;
150 tdata
->lu_entry
= NULL
;
153 if (tdata
->lu_entry
== NULL
)
155 tdata
->lu_entry
= in
;
159 free_netgroup_data(ng
);
161 ng
->ng_host
= in
->ng_host
;
162 ng
->ng_user
= in
->ng_user
;
163 ng
->ng_domain
= in
->ng_domain
;
170 lu_innetgr(const char *group
, const char *host
, const char *user
,
175 char namebuf
[4*_LU_MAXLUSTRLEN
+ 3*BYTES_PER_XDR_UNIT
];
176 static int proc
= -1;
179 _lu_innetgr_args args
;
184 if (_lookup_link(_lu_port
, "innetgr", &proc
) != KERN_SUCCESS
)
190 args
.group
= (char *)group
;
191 args
.host
= FIX(host
);
192 args
.user
= FIX(user
);
193 args
.domain
= FIX(domain
);
195 xdrmem_create(&xdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
196 if (!xdr__lu_innetgr_args(&xdr
, &args
))
202 size
= xdr_getpos(&xdr
) / BYTES_PER_XDR_UNIT
;
208 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, size
, &lookup_buf
, &datalen
) != KERN_SUCCESS
)
213 datalen
*= BYTES_PER_XDR_UNIT
;
214 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
216 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
217 if (!xdr_int(&xdr
, &res
))
220 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
225 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
233 struct lu_thread_info
*tdata
;
235 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
236 _lu_data_free_vm_xdr(tdata
);
241 * This is different than the other setXXXent routines
242 * since this is really more like getnetgrbyname() than
246 lu_setnetgrent(const char *name
)
249 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
251 static int proc
= -1;
252 struct lu_thread_info
*tdata
;
254 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
257 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
258 _lu_data_set_key(_lu_data_key_netgroup
, tdata
);
265 if (_lookup_link(_lu_port
, "getnetgrent", &proc
) != KERN_SUCCESS
)
272 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
273 if (!xdr__lu_string(&outxdr
, (_lu_string
*)&name
))
275 xdr_destroy(&outxdr
);
280 datalen
= xdr_getpos(&outxdr
);
281 xdr_destroy(&outxdr
);
282 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, datalen
/ BYTES_PER_XDR_UNIT
, &(tdata
->lu_vm
), &(tdata
->lu_vm_length
)) != KERN_SUCCESS
)
288 /* mig stubs measure size in words (4 bytes) */
289 tdata
->lu_vm_length
*= 4;
291 if (tdata
->lu_xdr
!= NULL
)
293 xdr_destroy(tdata
->lu_xdr
);
296 tdata
->lu_xdr
= (XDR
*)calloc(1, sizeof(XDR
));
298 xdrmem_create(tdata
->lu_xdr
, tdata
->lu_vm
, tdata
->lu_vm_length
, XDR_DECODE
);
299 if (!xdr_int(tdata
->lu_xdr
, &tdata
->lu_vm_cursor
)) lu_endnetgrent();
307 struct lu_thread_info
*tdata
;
309 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
310 if (tdata
== NULL
) return NULL
;
312 if (tdata
->lu_vm_cursor
== 0)
318 ng
= extract_netgroup(tdata
->lu_xdr
);
325 tdata
->lu_vm_cursor
--;
331 innetgr(const char *group
, const char *host
, const char *user
,
334 if (_lu_running()) return (lu_innetgr(group
, host
, user
, domain
));
341 struct netgrent
*res
= NULL
;
342 struct lu_thread_info
*tdata
;
344 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
347 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
348 _lu_data_set_key(_lu_data_key_netgroup
, tdata
);
352 if (_lu_running()) res
= lu_getnetgrent();
354 recycle_netgroup(tdata
, res
);
355 return (struct netgrent
*)tdata
->lu_entry
;
359 setnetgrent(const char *name
)
361 if (_lu_running()) lu_setnetgrent(name
);
367 if (_lu_running()) lu_endnetgrent();