]>
git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/lu_group.c
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.
30 #include <mach/mach.h>
34 #include <rpc/types.h>
37 #include <netinet/in.h>
38 #include <sys/param.h>
41 #include "_lu_types.h"
44 #include "lu_overrides.h"
46 #define GROUP_SENTINEL -99
48 static lookup_state gr_state
= LOOKUP_CACHE
;
49 static struct group global_gr
;
50 static int global_free
= 1;
52 static unsigned gr_datalen
= 0;
53 static int gr_nentries
= 0;
54 static int gr_start
= 1;
62 if (global_free
== 1) return;
64 free(global_gr
.gr_name
);
65 global_gr
.gr_name
= NULL
;
67 free(global_gr
.gr_passwd
);
68 global_gr
.gr_passwd
= NULL
;
70 mem
= global_gr
.gr_mem
;
73 while (*mem
!= NULL
) free(*mem
++);
74 free(global_gr
.gr_mem
);
75 global_gr
.gr_mem
= NULL
;
82 convert_gr(_lu_group
*lu_gr
)
88 global_gr
.gr_name
= strdup(lu_gr
->gr_name
);
89 global_gr
.gr_passwd
= strdup(lu_gr
->gr_passwd
);
90 global_gr
.gr_gid
= lu_gr
->gr_gid
;
92 len
= lu_gr
->gr_mem
.gr_mem_len
;
93 global_gr
.gr_mem
= (char **)malloc((len
+ 1) * sizeof(char *));
95 for (i
= 0; i
< len
; i
++)
97 global_gr
.gr_mem
[i
] = strdup(lu_gr
->gr_mem
.gr_mem_val
[i
]);
100 global_gr
.gr_mem
[len
] = NULL
;
105 static struct group
*
111 static int proc
= -1;
112 unit lookup_buf
[MAX_INLINE_UNITS
];
116 if (_lookup_link(_lu_port
, "getgrgid", &proc
) != KERN_SUCCESS
)
123 datalen
= MAX_INLINE_UNITS
;
125 if (_lookup_one(_lu_port
, proc
, (unit
*)&gid
, 1, lookup_buf
, &datalen
)
131 datalen
*= BYTES_PER_XDR_UNIT
;
132 xdrmem_create(&xdr
, lookup_buf
, datalen
, XDR_DECODE
);
135 if (!xdr__lu_group_ptr(&xdr
, &lu_gr
) || lu_gr
== NULL
)
144 xdr_free(xdr__lu_group_ptr
, &lu_gr
);
148 static struct group
*
149 lu_getgrnam(const char *name
)
152 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
156 static int proc
= -1;
157 unit lookup_buf
[MAX_INLINE_UNITS
];
161 if (_lookup_link(_lu_port
, "getgrnam", &proc
) != KERN_SUCCESS
)
167 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
169 if (!xdr__lu_string(&outxdr
, &name
))
171 xdr_destroy(&outxdr
);
175 datalen
= MAX_INLINE_UNITS
;
177 if (_lookup_one(_lu_port
, proc
, (unit
*)namebuf
,
178 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
184 xdr_destroy(&outxdr
);
186 datalen
*= BYTES_PER_XDR_UNIT
;
187 xdrmem_create(&inxdr
, lookup_buf
, datalen
,
191 if (!xdr__lu_group_ptr(&inxdr
, &lu_gr
) || (lu_gr
== NULL
))
200 xdr_free(xdr__lu_group_ptr
, &lu_gr
);
206 lu_initgroups(const char *name
, int basegid
)
211 static int proc
= -1;
212 unit lookup_buf
[MAX_INLINE_UNITS
];
213 char namebuf
[_LU_MAXLUSTRLEN
+ BYTES_PER_XDR_UNIT
];
223 if (_lookup_link(_lu_port
, "initgroups", &proc
) != KERN_SUCCESS
)
229 xdrmem_create(&outxdr
, namebuf
, sizeof(namebuf
), XDR_ENCODE
);
230 if (!xdr__lu_string(&outxdr
, &name
))
232 xdr_destroy(&outxdr
);
236 datalen
= MAX_INLINE_UNITS
;
237 if (_lookup_one(_lu_port
, proc
, (unit
*)namebuf
,
238 xdr_getpos(&outxdr
) / BYTES_PER_XDR_UNIT
, lookup_buf
, &datalen
)
241 xdr_destroy(&outxdr
);
245 xdr_destroy(&outxdr
);
247 datalen
*= BYTES_PER_XDR_UNIT
;
248 xdrmem_create(&inxdr
, lookup_buf
, datalen
,
251 while (xdr_int(&inxdr
, &a_group
))
253 if (a_group
== GROUP_SENTINEL
) break;
255 for (count
= 0; count
< ngroups
; count
++)
257 if (groups
[count
] == a_group
) break;
260 if (count
>= ngroups
) groups
[ngroups
++] = a_group
;
264 return setgroups(ngroups
, groups
);
274 vm_deallocate(mach_task_self(), (vm_address_t
)gr_data
, gr_datalen
);
287 static struct group
*
290 static int proc
= -1;
299 if (_lookup_link(_lu_port
, "getgrent", &proc
) != KERN_SUCCESS
)
306 if (_lookup_all(_lu_port
, proc
, NULL
, 0, &gr_data
, &gr_datalen
)
314 /* NOTDEF because OOL buffers are counted in bytes with untyped IPC */
315 gr_datalen
*= BYTES_PER_XDR_UNIT
;
317 xdrmem_create(&gr_xdr
, gr_data
, gr_datalen
,
319 if (!xdr_int(&gr_xdr
, &gr_nentries
))
321 xdr_destroy(&gr_xdr
);
327 if (gr_nentries
== 0)
329 xdr_destroy(&gr_xdr
);
334 bzero(&lu_gr
, sizeof(lu_gr
));
335 if (!xdr__lu_group(&gr_xdr
, &lu_gr
))
337 xdr_destroy(&gr_xdr
);
344 xdr_free(xdr__lu_group
, &lu_gr
);
351 LOOKUP1(lu_getgrgid
, _old_getgrgid
, gid
, struct group
);
355 getgrnam(const char *name
)
357 LOOKUP1(lu_getgrnam
, _old_getgrnam
, name
, struct group
);
361 initgroups(const char *name
, int basegid
)
365 if (name
== NULL
) return -1;
369 if ((res
= lu_initgroups(name
, basegid
)))
371 res
= _old_initgroups(name
, basegid
);
376 res
= _old_initgroups(name
, basegid
);
385 GETENT(lu_getgrent
, _old_getgrent
, &gr_state
, struct group
);
391 INTSETSTATEVOID(lu_setgrent
, _old_setgrent
, &gr_state
);
397 UNSETSTATE(lu_endgrent
, _old_endgrent
, &gr_state
);