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))
44 free_netgroup_data(struct netgrent
*ng
)
46 if (ng
== NULL
) return;
48 if (ng
->ng_host
!= NULL
) free(ng
->ng_host
);
49 if (ng
->ng_user
!= NULL
) free(ng
->ng_user
);
50 if (ng
->ng_domain
!= NULL
) free(ng
->ng_domain
);
54 free_netgroup(struct netgrent
*ng
)
56 if (ng
== NULL
) return;
57 free_netgroup_data(ng
);
62 free_lu_thread_info_netgroup(void *x
)
64 struct lu_thread_info
*tdata
;
66 if (x
== NULL
) return;
68 tdata
= (struct lu_thread_info
*)x
;
70 if (tdata
->lu_entry
!= NULL
)
72 free_netgroup((struct netgrent
*)tdata
->lu_entry
);
73 tdata
->lu_entry
= NULL
;
76 _lu_data_free_vm_xdr(tdata
);
81 static struct netgrent
*
82 extract_netgroup(XDR
*xdr
)
87 if (xdr
== NULL
) return NULL
;
93 if (!xdr_string(xdr
, &h
, LU_LONG_STRING_LENGTH
))
98 if (!xdr_string(xdr
, &u
, LU_LONG_STRING_LENGTH
))
104 if (!xdr_string(xdr
, &d
, LU_LONG_STRING_LENGTH
))
111 ng
= (struct netgrent
*)calloc(1, sizeof(struct netgrent
));
121 static struct netgrent
*
122 copy_netgroup(struct netgrent
*in
)
126 if (in
== NULL
) return NULL
;
128 ng
= (struct group
*)calloc(1, sizeof(struct netgrent
));
130 ng
->ng_host
= LU_COPY_STRING(in
->ng_host
);
131 ng
->ng_user
= LU_COPY_STRING(in
->ng_user
);
132 ng
->ng_domain
= LU_COPY_STRING(in
->ng_domain
);
139 recycle_netgroup(struct lu_thread_info
*tdata
, struct netgrent
*in
)
143 if (tdata
== NULL
) return;
144 ng
= (struct netgrent
*)tdata
->lu_entry
;
149 tdata
->lu_entry
= NULL
;
152 if (tdata
->lu_entry
== NULL
)
154 tdata
->lu_entry
= in
;
158 free_netgroup_data(ng
);
160 ng
->ng_host
= in
->ng_host
;
161 ng
->ng_user
= in
->ng_user
;
162 ng
->ng_domain
= in
->ng_domain
;
169 lu_innetgr(const char *group
, const char *host
, const char *user
,
174 char namebuf
[4*_LU_MAXLUSTRLEN
+ 3*BYTES_PER_XDR_UNIT
];
175 static int proc
= -1;
178 _lu_innetgr_args args
;
183 if (_lookup_link(_lu_port
, "innetgr", &proc
) != KERN_SUCCESS
)
189 args
.group
= (char *)group
;
190 args
.host
= FIX(host
);
191 args
.user
= FIX(user
);
192 args
.domain
= FIX(domain
);
194 xdrmem_create(&xdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
195 if (!xdr__lu_innetgr_args(&xdr
, &args
))
201 size
= xdr_getpos(&xdr
) / BYTES_PER_XDR_UNIT
;
207 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, size
, &lookup_buf
, &datalen
) != KERN_SUCCESS
)
212 datalen
*= BYTES_PER_XDR_UNIT
;
213 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
215 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
216 if (!xdr_int(&xdr
, &res
))
219 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
224 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
232 struct lu_thread_info
*tdata
;
234 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
235 _lu_data_free_vm_xdr(tdata
);
240 * This is different than the other setXXXent routines
241 * since this is really more like getnetgrbyname() than
245 lu_setnetgrent(const char *name
)
248 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
250 static int proc
= -1;
251 struct lu_thread_info
*tdata
;
253 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
256 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
257 _lu_data_set_key(_lu_data_key_netgroup
, tdata
);
264 if (_lookup_link(_lu_port
, "getnetgrent", &proc
) != KERN_SUCCESS
)
271 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
272 if (!xdr__lu_string(&outxdr
, (_lu_string
*)&name
))
274 xdr_destroy(&outxdr
);
279 datalen
= xdr_getpos(&outxdr
);
280 xdr_destroy(&outxdr
);
281 if (_lookup_all(_lu_port
, proc
, (unit
*)namebuf
, datalen
/ BYTES_PER_XDR_UNIT
, &(tdata
->lu_vm
), &(tdata
->lu_vm_length
)) != KERN_SUCCESS
)
287 /* mig stubs measure size in words (4 bytes) */
288 tdata
->lu_vm_length
*= 4;
290 if (tdata
->lu_xdr
!= NULL
)
292 xdr_destroy(tdata
->lu_xdr
);
295 tdata
->lu_xdr
= (XDR
*)calloc(1, sizeof(XDR
));
297 xdrmem_create(tdata
->lu_xdr
, tdata
->lu_vm
, tdata
->lu_vm_length
, XDR_DECODE
);
298 if (!xdr_int(tdata
->lu_xdr
, &tdata
->lu_vm_cursor
)) lu_endnetgrent();
306 struct lu_thread_info
*tdata
;
308 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
309 if (tdata
== NULL
) return NULL
;
311 if (tdata
->lu_vm_cursor
== 0)
317 ng
= extract_netgroup(tdata
->lu_xdr
);
324 tdata
->lu_vm_cursor
--;
330 innetgr(const char *group
, const char *host
, const char *user
,
333 if (_lu_running()) return (lu_innetgr(group
, host
, user
, domain
));
340 struct netgrent
*res
= NULL
;
341 struct lu_thread_info
*tdata
;
343 tdata
= _lu_data_create_key(_lu_data_key_netgroup
, free_lu_thread_info_netgroup
);
346 tdata
= (struct lu_thread_info
*)calloc(1, sizeof(struct lu_thread_info
));
347 _lu_data_set_key(_lu_data_key_netgroup
, tdata
);
351 if (_lu_running()) res
= lu_getnetgrent();
353 recycle_netgroup(tdata
, res
);
354 return (struct netgrent
*)tdata
->lu_entry
;
358 setnetgrent(const char *name
)
360 if (_lu_running()) lu_setnetgrent(name
);
366 if (_lu_running()) lu_endnetgrent();