xref: /onnv-gate/usr/src/lib/libpkg/common/ncgrpw.c (revision 9781:ccf49524d5dc)
1*9781SMoriah.Waterland@Sun.COM /*
2*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER START
3*9781SMoriah.Waterland@Sun.COM  *
4*9781SMoriah.Waterland@Sun.COM  * The contents of this file are subject to the terms of the
5*9781SMoriah.Waterland@Sun.COM  * Common Development and Distribution License (the "License").
6*9781SMoriah.Waterland@Sun.COM  * You may not use this file except in compliance with the License.
7*9781SMoriah.Waterland@Sun.COM  *
8*9781SMoriah.Waterland@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9781SMoriah.Waterland@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9781SMoriah.Waterland@Sun.COM  * See the License for the specific language governing permissions
11*9781SMoriah.Waterland@Sun.COM  * and limitations under the License.
12*9781SMoriah.Waterland@Sun.COM  *
13*9781SMoriah.Waterland@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9781SMoriah.Waterland@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9781SMoriah.Waterland@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9781SMoriah.Waterland@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9781SMoriah.Waterland@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9781SMoriah.Waterland@Sun.COM  *
19*9781SMoriah.Waterland@Sun.COM  * CDDL HEADER END
20*9781SMoriah.Waterland@Sun.COM  */
21*9781SMoriah.Waterland@Sun.COM 
22*9781SMoriah.Waterland@Sun.COM /*
23*9781SMoriah.Waterland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*9781SMoriah.Waterland@Sun.COM  * Use is subject to license terms.
25*9781SMoriah.Waterland@Sun.COM  */
26*9781SMoriah.Waterland@Sun.COM 
27*9781SMoriah.Waterland@Sun.COM 
28*9781SMoriah.Waterland@Sun.COM /*
29*9781SMoriah.Waterland@Sun.COM  * This module fetches group and passwd structs for the caller. It
30*9781SMoriah.Waterland@Sun.COM  * uses a hash table to speed up retrieval of repeated entries. If
31*9781SMoriah.Waterland@Sun.COM  * the attempts to initialize the hash tables fail, this just
32*9781SMoriah.Waterland@Sun.COM  * continues the slow way.
33*9781SMoriah.Waterland@Sun.COM  */
34*9781SMoriah.Waterland@Sun.COM 
35*9781SMoriah.Waterland@Sun.COM #include <pwd.h>
36*9781SMoriah.Waterland@Sun.COM #include <grp.h>
37*9781SMoriah.Waterland@Sun.COM #include <string.h>
38*9781SMoriah.Waterland@Sun.COM #include <stdio.h>
39*9781SMoriah.Waterland@Sun.COM #include <stdlib.h>
40*9781SMoriah.Waterland@Sun.COM #include <unistd.h>
41*9781SMoriah.Waterland@Sun.COM #include "pkglib.h"
42*9781SMoriah.Waterland@Sun.COM #include "pkglocale.h"
43*9781SMoriah.Waterland@Sun.COM #include "nhash.h"
44*9781SMoriah.Waterland@Sun.COM 
45*9781SMoriah.Waterland@Sun.COM #define	HASHSIZE	151
46*9781SMoriah.Waterland@Sun.COM #define	BSZ		4
47*9781SMoriah.Waterland@Sun.COM 
48*9781SMoriah.Waterland@Sun.COM #define	ERR_DUPFAIL	"%s: strdup(%s) failed.\n"
49*9781SMoriah.Waterland@Sun.COM #define	ERR_ADDFAIL	"%s: add_cache() failed.\n"
50*9781SMoriah.Waterland@Sun.COM #define	ERR_BADMEMB	"%s: %s in \"%s\" %s structure is invalid.\n"
51*9781SMoriah.Waterland@Sun.COM #define	ERR_NOGRP	"dup_gr_ent(): no group entry provided.\n"
52*9781SMoriah.Waterland@Sun.COM #define	ERR_NOPWD	"dup_pw_ent(): no passwd entry provided.\n"
53*9781SMoriah.Waterland@Sun.COM #define	ERR_NOINIT	"%s: init_cache() failed.\n"
54*9781SMoriah.Waterland@Sun.COM #define	ERR_MALLOC	"%s: malloc(%d) failed for %s.\n"
55*9781SMoriah.Waterland@Sun.COM 
56*9781SMoriah.Waterland@Sun.COM static Cache *pwnam_cache = (Cache *) NULL;
57*9781SMoriah.Waterland@Sun.COM static Cache *grnam_cache = (Cache *) NULL;
58*9781SMoriah.Waterland@Sun.COM static Cache *pwuid_cache = (Cache *) NULL;
59*9781SMoriah.Waterland@Sun.COM static Cache *grgid_cache = (Cache *) NULL;
60*9781SMoriah.Waterland@Sun.COM 
61*9781SMoriah.Waterland@Sun.COM static int dup_gr_ent(struct group *grp);
62*9781SMoriah.Waterland@Sun.COM static int dup_pw_ent(struct passwd *pwp);
63*9781SMoriah.Waterland@Sun.COM 
64*9781SMoriah.Waterland@Sun.COM /*
65*9781SMoriah.Waterland@Sun.COM  * These indicate whether the hash table has been initialized for the four
66*9781SMoriah.Waterland@Sun.COM  * categories.
67*9781SMoriah.Waterland@Sun.COM  */
68*9781SMoriah.Waterland@Sun.COM static int is_a_pwnam_cache;
69*9781SMoriah.Waterland@Sun.COM static int is_a_grnam_cache;
70*9781SMoriah.Waterland@Sun.COM static int is_a_pwuid_cache;
71*9781SMoriah.Waterland@Sun.COM static int is_a_grgid_cache;
72*9781SMoriah.Waterland@Sun.COM 
73*9781SMoriah.Waterland@Sun.COM extern char *get_install_root(void);
74*9781SMoriah.Waterland@Sun.COM 
75*9781SMoriah.Waterland@Sun.COM /*
76*9781SMoriah.Waterland@Sun.COM  * If there's a grnam cache, then update it with this new
77*9781SMoriah.Waterland@Sun.COM  * group, otherwise, skip it.
78*9781SMoriah.Waterland@Sun.COM  */
79*9781SMoriah.Waterland@Sun.COM static Item *
cache_alloc(char * fname,int len,size_t struct_size)80*9781SMoriah.Waterland@Sun.COM cache_alloc(char *fname, int len, size_t struct_size)
81*9781SMoriah.Waterland@Sun.COM {
82*9781SMoriah.Waterland@Sun.COM 	Item *itemp;
83*9781SMoriah.Waterland@Sun.COM 
84*9781SMoriah.Waterland@Sun.COM 	/*
85*9781SMoriah.Waterland@Sun.COM 	 * Allocate space for the Item pointer, key and data.
86*9781SMoriah.Waterland@Sun.COM 	 */
87*9781SMoriah.Waterland@Sun.COM 	if ((itemp = (Item *) malloc(sizeof (*itemp))) ==
88*9781SMoriah.Waterland@Sun.COM 	    Null_Item) {
89*9781SMoriah.Waterland@Sun.COM 		(void) fprintf(stderr,
90*9781SMoriah.Waterland@Sun.COM 		    pkg_gt(ERR_MALLOC), fname,
91*9781SMoriah.Waterland@Sun.COM 		    sizeof (*itemp), "itemp");
92*9781SMoriah.Waterland@Sun.COM 	} else if ((itemp->key = (char *)malloc(len)) == NULL) {
93*9781SMoriah.Waterland@Sun.COM 		(void) fprintf(stderr, pkg_gt(ERR_MALLOC), fname, len,
94*9781SMoriah.Waterland@Sun.COM 		    "itemp->key");
95*9781SMoriah.Waterland@Sun.COM 		free(itemp);
96*9781SMoriah.Waterland@Sun.COM 	} else if ((itemp->data = malloc(struct_size)) == NULL) {
97*9781SMoriah.Waterland@Sun.COM 		(void) fprintf(stderr, pkg_gt(ERR_MALLOC), fname,
98*9781SMoriah.Waterland@Sun.COM 		    struct_size, "itemp->data");
99*9781SMoriah.Waterland@Sun.COM 		free(itemp->key);
100*9781SMoriah.Waterland@Sun.COM 		free(itemp);
101*9781SMoriah.Waterland@Sun.COM 	} else {
102*9781SMoriah.Waterland@Sun.COM 		/* Set length parameters. */
103*9781SMoriah.Waterland@Sun.COM 		itemp->keyl = len;
104*9781SMoriah.Waterland@Sun.COM 		itemp->datal = struct_size;
105*9781SMoriah.Waterland@Sun.COM 
106*9781SMoriah.Waterland@Sun.COM 		return (itemp);
107*9781SMoriah.Waterland@Sun.COM 	}
108*9781SMoriah.Waterland@Sun.COM 
109*9781SMoriah.Waterland@Sun.COM 	return ((Item *) NULL);
110*9781SMoriah.Waterland@Sun.COM }
111*9781SMoriah.Waterland@Sun.COM 
112*9781SMoriah.Waterland@Sun.COM /* Get the required group structure based upon the group name. */
113*9781SMoriah.Waterland@Sun.COM struct group *
cgrnam(char * nam)114*9781SMoriah.Waterland@Sun.COM cgrnam(char *nam)
115*9781SMoriah.Waterland@Sun.COM {
116*9781SMoriah.Waterland@Sun.COM 	struct group *grp;
117*9781SMoriah.Waterland@Sun.COM 	Item *itemp;
118*9781SMoriah.Waterland@Sun.COM 	int len;
119*9781SMoriah.Waterland@Sun.COM 	static int cache_failed;
120*9781SMoriah.Waterland@Sun.COM 
121*9781SMoriah.Waterland@Sun.COM 	/* Attempt to initialize the grname cache. */
122*9781SMoriah.Waterland@Sun.COM 	if (!is_a_grnam_cache && !cache_failed) {
123*9781SMoriah.Waterland@Sun.COM 		if (init_cache(&grnam_cache, HASHSIZE, BSZ,
124*9781SMoriah.Waterland@Sun.COM 		    (int (*)())NULL, (int (*)())NULL) == -1) {
125*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr, pkg_gt(ERR_NOINIT), "cgrnam()");
126*9781SMoriah.Waterland@Sun.COM 			grnam_cache = (Cache *) NULL;
127*9781SMoriah.Waterland@Sun.COM 			cache_failed = 1;
128*9781SMoriah.Waterland@Sun.COM 		} else
129*9781SMoriah.Waterland@Sun.COM 			is_a_grnam_cache = 1;
130*9781SMoriah.Waterland@Sun.COM 	}
131*9781SMoriah.Waterland@Sun.COM 
132*9781SMoriah.Waterland@Sun.COM 	len = strlen(nam) + 1;
133*9781SMoriah.Waterland@Sun.COM 
134*9781SMoriah.Waterland@Sun.COM 	/* First look in the cache. Failing that, do it the hard way. */
135*9781SMoriah.Waterland@Sun.COM 	if ((itemp = lookup_cache(grnam_cache, nam, len)) == Null_Item) {
136*9781SMoriah.Waterland@Sun.COM 
137*9781SMoriah.Waterland@Sun.COM 		/* Get the group by name. */
138*9781SMoriah.Waterland@Sun.COM 		if ((grp = clgrnam(nam)) != NULL ||
139*9781SMoriah.Waterland@Sun.COM 				(grp = getgrnam(nam)) != NULL) {
140*9781SMoriah.Waterland@Sun.COM 			/* A group by that name exists on this machine. */
141*9781SMoriah.Waterland@Sun.COM 			if (dup_gr_ent(grp))
142*9781SMoriah.Waterland@Sun.COM 				/*
143*9781SMoriah.Waterland@Sun.COM 				 * Effectively no such group since struct
144*9781SMoriah.Waterland@Sun.COM 				 * couldn't be duplicated.
145*9781SMoriah.Waterland@Sun.COM 				 */
146*9781SMoriah.Waterland@Sun.COM 				grp = (struct group *)NULL;
147*9781SMoriah.Waterland@Sun.COM 			/*
148*9781SMoriah.Waterland@Sun.COM 			 * If there's a grnam cache, then update it with this
149*9781SMoriah.Waterland@Sun.COM 			 * new group, otherwise, skip it.
150*9781SMoriah.Waterland@Sun.COM 			 */
151*9781SMoriah.Waterland@Sun.COM 			else if (is_a_grnam_cache) {
152*9781SMoriah.Waterland@Sun.COM 				if ((itemp = cache_alloc("cgrnam()", len,
153*9781SMoriah.Waterland@Sun.COM 				    sizeof (struct group))) != Null_Item) {
154*9781SMoriah.Waterland@Sun.COM 					/*
155*9781SMoriah.Waterland@Sun.COM 					 * With that allocated, insert the
156*9781SMoriah.Waterland@Sun.COM 					 * group name as key and set the key
157*9781SMoriah.Waterland@Sun.COM 					 * length.
158*9781SMoriah.Waterland@Sun.COM 					 */
159*9781SMoriah.Waterland@Sun.COM 					(void) memmove(itemp->key, nam, len);
160*9781SMoriah.Waterland@Sun.COM 
161*9781SMoriah.Waterland@Sun.COM 					/*
162*9781SMoriah.Waterland@Sun.COM 					 * Insert the data associated with
163*9781SMoriah.Waterland@Sun.COM 					 * the key and the data length.
164*9781SMoriah.Waterland@Sun.COM 					 */
165*9781SMoriah.Waterland@Sun.COM 					(void) memmove(itemp->data, grp,
166*9781SMoriah.Waterland@Sun.COM 					    sizeof (struct group));
167*9781SMoriah.Waterland@Sun.COM 
168*9781SMoriah.Waterland@Sun.COM 					/* Insert the Item into the cache. */
169*9781SMoriah.Waterland@Sun.COM 					if (add_cache(grnam_cache, itemp) == -1)
170*9781SMoriah.Waterland@Sun.COM 						(void) fprintf(stderr,
171*9781SMoriah.Waterland@Sun.COM 						    pkg_gt(ERR_ADDFAIL),
172*9781SMoriah.Waterland@Sun.COM 						    "cgrnam()");
173*9781SMoriah.Waterland@Sun.COM 				}
174*9781SMoriah.Waterland@Sun.COM 			}
175*9781SMoriah.Waterland@Sun.COM 		}
176*9781SMoriah.Waterland@Sun.COM 		return (grp);
177*9781SMoriah.Waterland@Sun.COM 	} else	/* Found it in the cache. */
178*9781SMoriah.Waterland@Sun.COM 		return ((struct group *)itemp->data);
179*9781SMoriah.Waterland@Sun.COM }
180*9781SMoriah.Waterland@Sun.COM 
181*9781SMoriah.Waterland@Sun.COM struct passwd *
cpwnam(char * nam)182*9781SMoriah.Waterland@Sun.COM cpwnam(char *nam)
183*9781SMoriah.Waterland@Sun.COM {
184*9781SMoriah.Waterland@Sun.COM 	struct passwd *pwd;
185*9781SMoriah.Waterland@Sun.COM 	Item *itemp;
186*9781SMoriah.Waterland@Sun.COM 	int len;
187*9781SMoriah.Waterland@Sun.COM 	static int cache_failed;
188*9781SMoriah.Waterland@Sun.COM 
189*9781SMoriah.Waterland@Sun.COM 	if (!is_a_pwnam_cache && !cache_failed) {
190*9781SMoriah.Waterland@Sun.COM 		if (init_cache(&pwnam_cache, HASHSIZE, BSZ,
191*9781SMoriah.Waterland@Sun.COM 		    (int (*)())NULL, (int (*)())NULL) == -1) {
192*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr, pkg_gt(ERR_NOINIT), "cpwnam()");
193*9781SMoriah.Waterland@Sun.COM 			pwnam_cache = (Cache *) NULL;
194*9781SMoriah.Waterland@Sun.COM 			cache_failed = 1;
195*9781SMoriah.Waterland@Sun.COM 		} else
196*9781SMoriah.Waterland@Sun.COM 			is_a_pwnam_cache = 1;
197*9781SMoriah.Waterland@Sun.COM 	}
198*9781SMoriah.Waterland@Sun.COM 
199*9781SMoriah.Waterland@Sun.COM 	len = strlen(nam) + 1;
200*9781SMoriah.Waterland@Sun.COM 
201*9781SMoriah.Waterland@Sun.COM 	/* First look in the cache. Failing that, do it the hard way. */
202*9781SMoriah.Waterland@Sun.COM 	if ((itemp = lookup_cache(pwnam_cache, nam, len)) == Null_Item) {
203*9781SMoriah.Waterland@Sun.COM 
204*9781SMoriah.Waterland@Sun.COM 		/* Get the passwd by name. */
205*9781SMoriah.Waterland@Sun.COM 		if ((pwd = clpwnam(nam)) != NULL ||
206*9781SMoriah.Waterland@Sun.COM 				(pwd = getpwnam(nam)) != NULL) {
207*9781SMoriah.Waterland@Sun.COM 			/* A passwd by that name exists on this machine. */
208*9781SMoriah.Waterland@Sun.COM 			if (dup_pw_ent(pwd))
209*9781SMoriah.Waterland@Sun.COM 				/*
210*9781SMoriah.Waterland@Sun.COM 				 * Effectively no such passwd since struct
211*9781SMoriah.Waterland@Sun.COM 				 * couldn't be duplicated.
212*9781SMoriah.Waterland@Sun.COM 				 */
213*9781SMoriah.Waterland@Sun.COM 				pwd = (struct passwd *)NULL;
214*9781SMoriah.Waterland@Sun.COM 			/*
215*9781SMoriah.Waterland@Sun.COM 			 * If there's a pwnam cache, then update it with this
216*9781SMoriah.Waterland@Sun.COM 			 * new passwd, otherwise, skip it.
217*9781SMoriah.Waterland@Sun.COM 			 */
218*9781SMoriah.Waterland@Sun.COM 			else if (is_a_pwnam_cache) {
219*9781SMoriah.Waterland@Sun.COM 				/*
220*9781SMoriah.Waterland@Sun.COM 				 * Allocate space for the Item pointer, key
221*9781SMoriah.Waterland@Sun.COM 				 * and data.
222*9781SMoriah.Waterland@Sun.COM 				 */
223*9781SMoriah.Waterland@Sun.COM 				if ((itemp = cache_alloc("cpwnam()", len,
224*9781SMoriah.Waterland@Sun.COM 				    sizeof (struct passwd))) != Null_Item) {
225*9781SMoriah.Waterland@Sun.COM 					/*
226*9781SMoriah.Waterland@Sun.COM 					 * With that allocated, insert the
227*9781SMoriah.Waterland@Sun.COM 					 * group name as key and set the key
228*9781SMoriah.Waterland@Sun.COM 					 * length.
229*9781SMoriah.Waterland@Sun.COM 					 */
230*9781SMoriah.Waterland@Sun.COM 					(void) memmove(itemp->key, nam, len);
231*9781SMoriah.Waterland@Sun.COM 
232*9781SMoriah.Waterland@Sun.COM 					/*
233*9781SMoriah.Waterland@Sun.COM 					 * Insert the data associated with
234*9781SMoriah.Waterland@Sun.COM 					 * the key and the data length.
235*9781SMoriah.Waterland@Sun.COM 					 */
236*9781SMoriah.Waterland@Sun.COM 					(void) memmove(itemp->data, pwd,
237*9781SMoriah.Waterland@Sun.COM 					    sizeof (struct passwd));
238*9781SMoriah.Waterland@Sun.COM 
239*9781SMoriah.Waterland@Sun.COM 					if (add_cache(pwnam_cache, itemp) == -1)
240*9781SMoriah.Waterland@Sun.COM 						(void) fprintf(stderr,
241*9781SMoriah.Waterland@Sun.COM 						    pkg_gt(ERR_ADDFAIL),
242*9781SMoriah.Waterland@Sun.COM 						    "cpwnam()");
243*9781SMoriah.Waterland@Sun.COM 				}
244*9781SMoriah.Waterland@Sun.COM 			}
245*9781SMoriah.Waterland@Sun.COM 		}
246*9781SMoriah.Waterland@Sun.COM 		return (pwd);
247*9781SMoriah.Waterland@Sun.COM 	} else	/* Found it in the cache. */
248*9781SMoriah.Waterland@Sun.COM 		return ((struct passwd *)itemp->data);
249*9781SMoriah.Waterland@Sun.COM }
250*9781SMoriah.Waterland@Sun.COM 
251*9781SMoriah.Waterland@Sun.COM static int
uid_hash(void * datap,int datalen,int hsz)252*9781SMoriah.Waterland@Sun.COM uid_hash(void *datap, int datalen, int hsz)
253*9781SMoriah.Waterland@Sun.COM {
254*9781SMoriah.Waterland@Sun.COM #ifdef lint
255*9781SMoriah.Waterland@Sun.COM 	int i = datalen;
256*9781SMoriah.Waterland@Sun.COM 	datalen = i;
257*9781SMoriah.Waterland@Sun.COM #endif	/* lint */
258*9781SMoriah.Waterland@Sun.COM 
259*9781SMoriah.Waterland@Sun.COM 	return (*((uid_t *)datap) % hsz);
260*9781SMoriah.Waterland@Sun.COM }
261*9781SMoriah.Waterland@Sun.COM 
262*9781SMoriah.Waterland@Sun.COM static int
uid_comp(void * datap1,void * datap2,int datalen)263*9781SMoriah.Waterland@Sun.COM uid_comp(void *datap1, void *datap2, int datalen)
264*9781SMoriah.Waterland@Sun.COM {
265*9781SMoriah.Waterland@Sun.COM #ifdef lint
266*9781SMoriah.Waterland@Sun.COM 	int i = datalen;
267*9781SMoriah.Waterland@Sun.COM 	datalen = i;
268*9781SMoriah.Waterland@Sun.COM #endif	/* lint */
269*9781SMoriah.Waterland@Sun.COM 
270*9781SMoriah.Waterland@Sun.COM 	return (*((uid_t *)datap1) - *((uid_t *)datap2));
271*9781SMoriah.Waterland@Sun.COM }
272*9781SMoriah.Waterland@Sun.COM 
273*9781SMoriah.Waterland@Sun.COM struct group *
cgrgid(gid_t gid)274*9781SMoriah.Waterland@Sun.COM cgrgid(gid_t gid)
275*9781SMoriah.Waterland@Sun.COM {
276*9781SMoriah.Waterland@Sun.COM 	struct group *grp;
277*9781SMoriah.Waterland@Sun.COM 	Item *itemp;
278*9781SMoriah.Waterland@Sun.COM 	int len;
279*9781SMoriah.Waterland@Sun.COM 	static int cache_failed;
280*9781SMoriah.Waterland@Sun.COM 
281*9781SMoriah.Waterland@Sun.COM 	if (!is_a_grgid_cache && !cache_failed) {
282*9781SMoriah.Waterland@Sun.COM 		if (init_cache(&grgid_cache, HASHSIZE, BSZ,
283*9781SMoriah.Waterland@Sun.COM 		    uid_hash, uid_comp) == -1) {
284*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr, pkg_gt(ERR_NOINIT), "cgrgid()");
285*9781SMoriah.Waterland@Sun.COM 			grgid_cache = (Cache *) NULL;
286*9781SMoriah.Waterland@Sun.COM 			cache_failed = 1;
287*9781SMoriah.Waterland@Sun.COM 		} else
288*9781SMoriah.Waterland@Sun.COM 			is_a_grgid_cache = 1;
289*9781SMoriah.Waterland@Sun.COM 	}
290*9781SMoriah.Waterland@Sun.COM 
291*9781SMoriah.Waterland@Sun.COM 	len = sizeof (uid_t);
292*9781SMoriah.Waterland@Sun.COM 
293*9781SMoriah.Waterland@Sun.COM 	/* First look in the cache. Failing that, do it the hard way. */
294*9781SMoriah.Waterland@Sun.COM 	if ((itemp = lookup_cache(grgid_cache, &gid, len)) == Null_Item) {
295*9781SMoriah.Waterland@Sun.COM 		if ((grp = clgrgid(gid)) != NULL ||
296*9781SMoriah.Waterland@Sun.COM 				(grp = getgrgid(gid)) != NULL) {
297*9781SMoriah.Waterland@Sun.COM 			/* A group by that number exists on this machine. */
298*9781SMoriah.Waterland@Sun.COM 			if (dup_gr_ent(grp))
299*9781SMoriah.Waterland@Sun.COM 				/*
300*9781SMoriah.Waterland@Sun.COM 				 * Effectively no such group since struct
301*9781SMoriah.Waterland@Sun.COM 				 * couldn't be duplicated.
302*9781SMoriah.Waterland@Sun.COM 				 */
303*9781SMoriah.Waterland@Sun.COM 				grp = (struct group *)NULL;
304*9781SMoriah.Waterland@Sun.COM 			/*
305*9781SMoriah.Waterland@Sun.COM 			 * If there's a grnam cache, then update it with this
306*9781SMoriah.Waterland@Sun.COM 			 * new group, otherwise, skip it.
307*9781SMoriah.Waterland@Sun.COM 			 */
308*9781SMoriah.Waterland@Sun.COM 			else if (is_a_grgid_cache) {
309*9781SMoriah.Waterland@Sun.COM 				if ((itemp = cache_alloc("cgrgid()", len,
310*9781SMoriah.Waterland@Sun.COM 				    sizeof (struct group))) != Null_Item) {
311*9781SMoriah.Waterland@Sun.COM 					/*
312*9781SMoriah.Waterland@Sun.COM 					 * With that allocated, insert the
313*9781SMoriah.Waterland@Sun.COM 					 * group name as key and set the key
314*9781SMoriah.Waterland@Sun.COM 					 * length.
315*9781SMoriah.Waterland@Sun.COM 					 */
316*9781SMoriah.Waterland@Sun.COM 					(void) memmove(itemp->key, &gid, len);
317*9781SMoriah.Waterland@Sun.COM 
318*9781SMoriah.Waterland@Sun.COM 					/*
319*9781SMoriah.Waterland@Sun.COM 					 * Insert the data associated with
320*9781SMoriah.Waterland@Sun.COM 					 * the key and the data length.
321*9781SMoriah.Waterland@Sun.COM 					 */
322*9781SMoriah.Waterland@Sun.COM 					(void) memmove(itemp->data, grp,
323*9781SMoriah.Waterland@Sun.COM 					    sizeof (struct group));
324*9781SMoriah.Waterland@Sun.COM 
325*9781SMoriah.Waterland@Sun.COM 					if (add_cache(grgid_cache, itemp) == -1)
326*9781SMoriah.Waterland@Sun.COM 						(void) fprintf(stderr,
327*9781SMoriah.Waterland@Sun.COM 						    pkg_gt(ERR_ADDFAIL),
328*9781SMoriah.Waterland@Sun.COM 						    "cgrgid()");
329*9781SMoriah.Waterland@Sun.COM 				}
330*9781SMoriah.Waterland@Sun.COM 			}
331*9781SMoriah.Waterland@Sun.COM 		}
332*9781SMoriah.Waterland@Sun.COM 		return (grp);
333*9781SMoriah.Waterland@Sun.COM 	} else	/* Found it in the cache. */
334*9781SMoriah.Waterland@Sun.COM 		return ((struct group *)itemp->data);
335*9781SMoriah.Waterland@Sun.COM }
336*9781SMoriah.Waterland@Sun.COM 
337*9781SMoriah.Waterland@Sun.COM struct passwd *
cpwuid(uid_t uid)338*9781SMoriah.Waterland@Sun.COM cpwuid(uid_t uid)
339*9781SMoriah.Waterland@Sun.COM {
340*9781SMoriah.Waterland@Sun.COM 	struct passwd *pwd;
341*9781SMoriah.Waterland@Sun.COM 	Item *itemp;
342*9781SMoriah.Waterland@Sun.COM 	int len;
343*9781SMoriah.Waterland@Sun.COM 	static int cache_failed;
344*9781SMoriah.Waterland@Sun.COM 
345*9781SMoriah.Waterland@Sun.COM 	if (!is_a_pwuid_cache && !cache_failed) {
346*9781SMoriah.Waterland@Sun.COM 		if (init_cache(&pwuid_cache, HASHSIZE, BSZ,
347*9781SMoriah.Waterland@Sun.COM 		    uid_hash, uid_comp) == -1) {
348*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
349*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_NOINIT), "cpwuid()");
350*9781SMoriah.Waterland@Sun.COM 			pwuid_cache = (Cache *) NULL;
351*9781SMoriah.Waterland@Sun.COM 			cache_failed = 1;
352*9781SMoriah.Waterland@Sun.COM 		} else
353*9781SMoriah.Waterland@Sun.COM 			is_a_pwuid_cache = 1;
354*9781SMoriah.Waterland@Sun.COM 	}
355*9781SMoriah.Waterland@Sun.COM 
356*9781SMoriah.Waterland@Sun.COM 	len = sizeof (uid_t);
357*9781SMoriah.Waterland@Sun.COM 
358*9781SMoriah.Waterland@Sun.COM 	/* First look in the cache. Failing that, do it the hard way. */
359*9781SMoriah.Waterland@Sun.COM 	if ((itemp = lookup_cache(pwuid_cache, &uid, len)) == Null_Item) {
360*9781SMoriah.Waterland@Sun.COM 
361*9781SMoriah.Waterland@Sun.COM 		/* Get the passwd by number. */
362*9781SMoriah.Waterland@Sun.COM 		if ((pwd = clpwuid(uid)) != NULL ||
363*9781SMoriah.Waterland@Sun.COM 				(pwd = getpwuid(uid)) != NULL) {
364*9781SMoriah.Waterland@Sun.COM 			/* A passwd by that user ID exists on this machine. */
365*9781SMoriah.Waterland@Sun.COM 			if (dup_pw_ent(pwd))
366*9781SMoriah.Waterland@Sun.COM 				/*
367*9781SMoriah.Waterland@Sun.COM 				 * Effectively no such passwd since struct
368*9781SMoriah.Waterland@Sun.COM 				 * couldn't be duplicated.
369*9781SMoriah.Waterland@Sun.COM 				 */
370*9781SMoriah.Waterland@Sun.COM 				pwd = (struct passwd *)NULL;
371*9781SMoriah.Waterland@Sun.COM 			/*
372*9781SMoriah.Waterland@Sun.COM 			 * If there's a pwuid cache, then update it with this
373*9781SMoriah.Waterland@Sun.COM 			 * new passwd, otherwise, skip it.
374*9781SMoriah.Waterland@Sun.COM 			 */
375*9781SMoriah.Waterland@Sun.COM 			else if (is_a_pwuid_cache) {
376*9781SMoriah.Waterland@Sun.COM 				if ((itemp = cache_alloc("cpwuid()", len,
377*9781SMoriah.Waterland@Sun.COM 				    sizeof (struct passwd))) != Null_Item) {
378*9781SMoriah.Waterland@Sun.COM 					/*
379*9781SMoriah.Waterland@Sun.COM 					 * With that allocated, insert the
380*9781SMoriah.Waterland@Sun.COM 					 * group name as key and set the key
381*9781SMoriah.Waterland@Sun.COM 					 * length.
382*9781SMoriah.Waterland@Sun.COM 					 */
383*9781SMoriah.Waterland@Sun.COM 					(void) memmove(itemp->key, &uid, len);
384*9781SMoriah.Waterland@Sun.COM 
385*9781SMoriah.Waterland@Sun.COM 					/*
386*9781SMoriah.Waterland@Sun.COM 					 * Insert the data associated with
387*9781SMoriah.Waterland@Sun.COM 					 * the key and the data length.
388*9781SMoriah.Waterland@Sun.COM 					 */
389*9781SMoriah.Waterland@Sun.COM 					(void) memmove(itemp->data, pwd,
390*9781SMoriah.Waterland@Sun.COM 					    sizeof (struct passwd));
391*9781SMoriah.Waterland@Sun.COM 
392*9781SMoriah.Waterland@Sun.COM 					if (add_cache(pwuid_cache, itemp) == -1)
393*9781SMoriah.Waterland@Sun.COM 						(void) fprintf(stderr,
394*9781SMoriah.Waterland@Sun.COM 						    pkg_gt(ERR_ADDFAIL),
395*9781SMoriah.Waterland@Sun.COM 						    "cpwuid()");
396*9781SMoriah.Waterland@Sun.COM 				}
397*9781SMoriah.Waterland@Sun.COM 			}
398*9781SMoriah.Waterland@Sun.COM 		}
399*9781SMoriah.Waterland@Sun.COM 		return (pwd);
400*9781SMoriah.Waterland@Sun.COM 	} else	/* Found it in the cache. */
401*9781SMoriah.Waterland@Sun.COM 		return ((struct passwd *)itemp->data);
402*9781SMoriah.Waterland@Sun.COM }
403*9781SMoriah.Waterland@Sun.COM 
404*9781SMoriah.Waterland@Sun.COM /*
405*9781SMoriah.Waterland@Sun.COM  * This function duplicates the group structure provided from kernel static
406*9781SMoriah.Waterland@Sun.COM  * memory. There is a lot of defensive coding here because there have been
407*9781SMoriah.Waterland@Sun.COM  * problems with the getgr*() functions. They will sometimes provide NULL
408*9781SMoriah.Waterland@Sun.COM  * values instead of pointers to NULL values. There has been no explanation
409*9781SMoriah.Waterland@Sun.COM  * for the reason behind this; but, this function takes a NULL to be an
410*9781SMoriah.Waterland@Sun.COM  * invalid (char *) and returns an error.
411*9781SMoriah.Waterland@Sun.COM  */
412*9781SMoriah.Waterland@Sun.COM static int
dup_gr_ent(struct group * grp)413*9781SMoriah.Waterland@Sun.COM dup_gr_ent(struct group *grp)
414*9781SMoriah.Waterland@Sun.COM {
415*9781SMoriah.Waterland@Sun.COM 	char **tp = NULL;
416*9781SMoriah.Waterland@Sun.COM 	char **memp = NULL;
417*9781SMoriah.Waterland@Sun.COM 	int	nent = 0;	/* Number of entries in the member list. */
418*9781SMoriah.Waterland@Sun.COM 
419*9781SMoriah.Waterland@Sun.COM 	if (grp) {
420*9781SMoriah.Waterland@Sun.COM 		if (grp->gr_name == NULL) {
421*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
422*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_gr_ent()", "gr_name",
423*9781SMoriah.Waterland@Sun.COM 			    "unknown", "group");
424*9781SMoriah.Waterland@Sun.COM 			return (-1);
425*9781SMoriah.Waterland@Sun.COM 		} else if ((grp->gr_name = strdup(grp->gr_name)) == NULL) {
426*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
427*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_DUPFAIL), "dup_gr_ent()", "gr_name");
428*9781SMoriah.Waterland@Sun.COM 			return (-1);
429*9781SMoriah.Waterland@Sun.COM 		}
430*9781SMoriah.Waterland@Sun.COM 		if (grp->gr_passwd == NULL) {
431*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
432*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_gr_ent()", "gr_passwd",
433*9781SMoriah.Waterland@Sun.COM 			    grp->gr_name, "group");
434*9781SMoriah.Waterland@Sun.COM 			return (-1);
435*9781SMoriah.Waterland@Sun.COM 		} else if ((grp->gr_passwd = strdup(grp->gr_passwd)) == NULL) {
436*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
437*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_DUPFAIL), "dup_gr_ent()", "gr_passwd");
438*9781SMoriah.Waterland@Sun.COM 			return (-1);
439*9781SMoriah.Waterland@Sun.COM 		}
440*9781SMoriah.Waterland@Sun.COM 		/*
441*9781SMoriah.Waterland@Sun.COM 		 * Allocate space for the member list and move the members
442*9781SMoriah.Waterland@Sun.COM 		 * into it.
443*9781SMoriah.Waterland@Sun.COM 		 */
444*9781SMoriah.Waterland@Sun.COM 		if (grp->gr_mem) {
445*9781SMoriah.Waterland@Sun.COM 			/*
446*9781SMoriah.Waterland@Sun.COM 			 * First count the members. The nent variable will be
447*9781SMoriah.Waterland@Sun.COM 			 * the number of members + 1 for the terminator.
448*9781SMoriah.Waterland@Sun.COM 			 */
449*9781SMoriah.Waterland@Sun.COM 			for (tp = grp->gr_mem; *tp; nent++, tp++);
450*9781SMoriah.Waterland@Sun.COM 
451*9781SMoriah.Waterland@Sun.COM 			/* Now allocate room for the pointers. */
452*9781SMoriah.Waterland@Sun.COM 			memp = malloc(sizeof (char **)* (nent+1));
453*9781SMoriah.Waterland@Sun.COM 
454*9781SMoriah.Waterland@Sun.COM 			if (memp == NULL) {
455*9781SMoriah.Waterland@Sun.COM 				(void) fprintf(stderr,
456*9781SMoriah.Waterland@Sun.COM 				    pkg_gt(ERR_MALLOC), "dup_gr_ent()",
457*9781SMoriah.Waterland@Sun.COM 				    (sizeof (char **)* (nent+1)),
458*9781SMoriah.Waterland@Sun.COM 				    "memp");
459*9781SMoriah.Waterland@Sun.COM 				return (-1);
460*9781SMoriah.Waterland@Sun.COM 			}
461*9781SMoriah.Waterland@Sun.COM 
462*9781SMoriah.Waterland@Sun.COM 			/*
463*9781SMoriah.Waterland@Sun.COM 			 * Now copy over the pointers and entries. It should
464*9781SMoriah.Waterland@Sun.COM 			 * be noted that if the structure is messed up here,
465*9781SMoriah.Waterland@Sun.COM 			 * the resulting member list will be truncated at the
466*9781SMoriah.Waterland@Sun.COM 			 * NULL entry.
467*9781SMoriah.Waterland@Sun.COM 			 */
468*9781SMoriah.Waterland@Sun.COM 			for (nent = 0, tp = grp->gr_mem; *tp; tp++) {
469*9781SMoriah.Waterland@Sun.COM 				if ((memp[nent++] = strdup(*tp)) == NULL) {
470*9781SMoriah.Waterland@Sun.COM 					(void) fprintf(stderr,
471*9781SMoriah.Waterland@Sun.COM 					    pkg_gt(ERR_DUPFAIL), "dup_gr_ent()",
472*9781SMoriah.Waterland@Sun.COM 					    "gr_mem");
473*9781SMoriah.Waterland@Sun.COM 					return (-1);
474*9781SMoriah.Waterland@Sun.COM 				}
475*9781SMoriah.Waterland@Sun.COM 			}
476*9781SMoriah.Waterland@Sun.COM 		} else {
477*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
478*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_gr_ent()", "gr_mem",
479*9781SMoriah.Waterland@Sun.COM 			    grp->gr_name, "group");
480*9781SMoriah.Waterland@Sun.COM 			return (-1);
481*9781SMoriah.Waterland@Sun.COM 		}
482*9781SMoriah.Waterland@Sun.COM 	} else {
483*9781SMoriah.Waterland@Sun.COM 		(void) fprintf(stderr, pkg_gt(ERR_NOGRP));
484*9781SMoriah.Waterland@Sun.COM 		return (-1);
485*9781SMoriah.Waterland@Sun.COM 	}
486*9781SMoriah.Waterland@Sun.COM 	memp[nent++] = '\0';
487*9781SMoriah.Waterland@Sun.COM 	return (0);
488*9781SMoriah.Waterland@Sun.COM }
489*9781SMoriah.Waterland@Sun.COM 
490*9781SMoriah.Waterland@Sun.COM /*
491*9781SMoriah.Waterland@Sun.COM  * This function duplicates the passwd structure provided from kernel static
492*9781SMoriah.Waterland@Sun.COM  * memory. As in the above function, since there have been problems with the
493*9781SMoriah.Waterland@Sun.COM  * getpw*() functions, the structure provided is rigorously scrubbed. This
494*9781SMoriah.Waterland@Sun.COM  * function takes a NULL to be an invalid (char *) and returns an error if
495*9781SMoriah.Waterland@Sun.COM  * one is detected.
496*9781SMoriah.Waterland@Sun.COM  */
497*9781SMoriah.Waterland@Sun.COM static int
dup_pw_ent(struct passwd * pwd)498*9781SMoriah.Waterland@Sun.COM dup_pw_ent(struct passwd *pwd)
499*9781SMoriah.Waterland@Sun.COM {
500*9781SMoriah.Waterland@Sun.COM 	if (pwd) {
501*9781SMoriah.Waterland@Sun.COM 		if (pwd->pw_name == NULL) {
502*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
503*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_name",
504*9781SMoriah.Waterland@Sun.COM 			    "unknown", "passwd");
505*9781SMoriah.Waterland@Sun.COM 			return (-1);
506*9781SMoriah.Waterland@Sun.COM 		} else if ((pwd->pw_name = strdup(pwd->pw_name)) == NULL) {
507*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
508*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_name");
509*9781SMoriah.Waterland@Sun.COM 			return (-1);
510*9781SMoriah.Waterland@Sun.COM 		}
511*9781SMoriah.Waterland@Sun.COM 
512*9781SMoriah.Waterland@Sun.COM 		if (pwd->pw_passwd == NULL) {
513*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
514*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_passwd",
515*9781SMoriah.Waterland@Sun.COM 			    pwd->pw_name, "passwd");
516*9781SMoriah.Waterland@Sun.COM 			return (-1);
517*9781SMoriah.Waterland@Sun.COM 		} else if ((pwd->pw_passwd = strdup(pwd->pw_passwd)) == NULL) {
518*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
519*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_passwd");
520*9781SMoriah.Waterland@Sun.COM 			return (-1);
521*9781SMoriah.Waterland@Sun.COM 		}
522*9781SMoriah.Waterland@Sun.COM 
523*9781SMoriah.Waterland@Sun.COM 		if (pwd->pw_age == NULL) {
524*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
525*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_age",
526*9781SMoriah.Waterland@Sun.COM 			    pwd->pw_name, "passwd");
527*9781SMoriah.Waterland@Sun.COM 			return (-1);
528*9781SMoriah.Waterland@Sun.COM 		} else if ((pwd->pw_age = strdup(pwd->pw_age)) == NULL) {
529*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
530*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_age");
531*9781SMoriah.Waterland@Sun.COM 			return (-1);
532*9781SMoriah.Waterland@Sun.COM 		}
533*9781SMoriah.Waterland@Sun.COM 
534*9781SMoriah.Waterland@Sun.COM 		if (pwd->pw_comment == NULL) {
535*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
536*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_comment",
537*9781SMoriah.Waterland@Sun.COM 			    pwd->pw_name, "passwd");
538*9781SMoriah.Waterland@Sun.COM 			return (-1);
539*9781SMoriah.Waterland@Sun.COM 		} else if ((pwd->pw_comment = strdup(pwd->pw_comment)) ==
540*9781SMoriah.Waterland@Sun.COM 		    NULL) {
541*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
542*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_comment");
543*9781SMoriah.Waterland@Sun.COM 			return (-1);
544*9781SMoriah.Waterland@Sun.COM 		}
545*9781SMoriah.Waterland@Sun.COM 
546*9781SMoriah.Waterland@Sun.COM 		if (pwd->pw_gecos == NULL) {
547*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
548*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_gecos",
549*9781SMoriah.Waterland@Sun.COM 			    pwd->pw_name, "passwd");
550*9781SMoriah.Waterland@Sun.COM 			return (-1);
551*9781SMoriah.Waterland@Sun.COM 		} else if ((pwd->pw_gecos = strdup(pwd->pw_gecos)) == NULL) {
552*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
553*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_gecos");
554*9781SMoriah.Waterland@Sun.COM 			return (-1);
555*9781SMoriah.Waterland@Sun.COM 		}
556*9781SMoriah.Waterland@Sun.COM 
557*9781SMoriah.Waterland@Sun.COM 		if (pwd->pw_dir == NULL) {
558*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
559*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_dir",
560*9781SMoriah.Waterland@Sun.COM 			    pwd->pw_name, "passwd");
561*9781SMoriah.Waterland@Sun.COM 			return (-1);
562*9781SMoriah.Waterland@Sun.COM 		} else if ((pwd->pw_dir = strdup(pwd->pw_dir)) == NULL) {
563*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
564*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_dir");
565*9781SMoriah.Waterland@Sun.COM 			return (-1);
566*9781SMoriah.Waterland@Sun.COM 		}
567*9781SMoriah.Waterland@Sun.COM 
568*9781SMoriah.Waterland@Sun.COM 		if (pwd->pw_shell == NULL) {
569*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
570*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_BADMEMB), "dup_pw_ent()", "pw_shell",
571*9781SMoriah.Waterland@Sun.COM 			    pwd->pw_name, "passwd");
572*9781SMoriah.Waterland@Sun.COM 			return (-1);
573*9781SMoriah.Waterland@Sun.COM 		} else if ((pwd->pw_shell = strdup(pwd->pw_shell)) == NULL) {
574*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
575*9781SMoriah.Waterland@Sun.COM 			    pkg_gt(ERR_DUPFAIL), "dup_pw_ent()", "pw_shell");
576*9781SMoriah.Waterland@Sun.COM 			return (-1);
577*9781SMoriah.Waterland@Sun.COM 		}
578*9781SMoriah.Waterland@Sun.COM 	} else {
579*9781SMoriah.Waterland@Sun.COM 		(void) fprintf(stderr, pkg_gt(ERR_NOPWD));
580*9781SMoriah.Waterland@Sun.COM 		return (-1);
581*9781SMoriah.Waterland@Sun.COM 	}
582*9781SMoriah.Waterland@Sun.COM 
583*9781SMoriah.Waterland@Sun.COM 	return (0);
584*9781SMoriah.Waterland@Sun.COM }
585*9781SMoriah.Waterland@Sun.COM 
586*9781SMoriah.Waterland@Sun.COM /*
587*9781SMoriah.Waterland@Sun.COM  * Check the client's etc/group file for the group name
588*9781SMoriah.Waterland@Sun.COM  *
589*9781SMoriah.Waterland@Sun.COM  * returns a pointer to the group structure if the group is found
590*9781SMoriah.Waterland@Sun.COM  * returns NULL if not found
591*9781SMoriah.Waterland@Sun.COM  */
592*9781SMoriah.Waterland@Sun.COM struct group *
clgrnam(char * nam)593*9781SMoriah.Waterland@Sun.COM clgrnam(char *nam)
594*9781SMoriah.Waterland@Sun.COM {
595*9781SMoriah.Waterland@Sun.COM 	struct group *gr;
596*9781SMoriah.Waterland@Sun.COM 	char *instroot, *buf;
597*9781SMoriah.Waterland@Sun.COM 	FILE *gr_ptr;
598*9781SMoriah.Waterland@Sun.COM 
599*9781SMoriah.Waterland@Sun.COM 	if ((instroot = get_install_root()) != NULL) {
600*9781SMoriah.Waterland@Sun.COM 		if ((buf = (char *)malloc(strlen(instroot) +
601*9781SMoriah.Waterland@Sun.COM 			strlen(GROUP) + 1)) == NULL) {
602*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
603*9781SMoriah.Waterland@Sun.COM 				pkg_gt(ERR_MALLOC), "clgrnam()",
604*9781SMoriah.Waterland@Sun.COM 				strlen(instroot) + strlen(GROUP), "buf");
605*9781SMoriah.Waterland@Sun.COM 		}
606*9781SMoriah.Waterland@Sun.COM 		(void) sprintf(buf, "%s%s", instroot, GROUP);
607*9781SMoriah.Waterland@Sun.COM 		if ((gr_ptr = fopen(buf, "r")) == NULL) {
608*9781SMoriah.Waterland@Sun.COM 			free(buf);
609*9781SMoriah.Waterland@Sun.COM 			return (NULL);
610*9781SMoriah.Waterland@Sun.COM 		} else {
611*9781SMoriah.Waterland@Sun.COM 			while ((gr = fgetgrent(gr_ptr)) != NULL) {
612*9781SMoriah.Waterland@Sun.COM 				if (strcmp(gr->gr_name, nam) == 0) {
613*9781SMoriah.Waterland@Sun.COM 					break;
614*9781SMoriah.Waterland@Sun.COM 				}
615*9781SMoriah.Waterland@Sun.COM 			}
616*9781SMoriah.Waterland@Sun.COM 		}
617*9781SMoriah.Waterland@Sun.COM 		free(buf);
618*9781SMoriah.Waterland@Sun.COM 		(void) fclose(gr_ptr);
619*9781SMoriah.Waterland@Sun.COM 		return (gr);
620*9781SMoriah.Waterland@Sun.COM 	} else {
621*9781SMoriah.Waterland@Sun.COM 		return (NULL);
622*9781SMoriah.Waterland@Sun.COM 	}
623*9781SMoriah.Waterland@Sun.COM }
624*9781SMoriah.Waterland@Sun.COM 
625*9781SMoriah.Waterland@Sun.COM /*
626*9781SMoriah.Waterland@Sun.COM  * Check the client's etc/passwd file for the user name
627*9781SMoriah.Waterland@Sun.COM  *
628*9781SMoriah.Waterland@Sun.COM  * returns a pointer to the passwd structure if the passwd is found
629*9781SMoriah.Waterland@Sun.COM  * returns NULL if not found
630*9781SMoriah.Waterland@Sun.COM  */
631*9781SMoriah.Waterland@Sun.COM struct passwd *
clpwnam(char * nam)632*9781SMoriah.Waterland@Sun.COM clpwnam(char *nam)
633*9781SMoriah.Waterland@Sun.COM {
634*9781SMoriah.Waterland@Sun.COM 	struct passwd *pw;
635*9781SMoriah.Waterland@Sun.COM 	char *instroot, *buf;
636*9781SMoriah.Waterland@Sun.COM 	FILE *pw_ptr;
637*9781SMoriah.Waterland@Sun.COM 
638*9781SMoriah.Waterland@Sun.COM 	if ((instroot = get_install_root()) != NULL) {
639*9781SMoriah.Waterland@Sun.COM 		if ((buf = (char *)malloc(strlen(instroot) +
640*9781SMoriah.Waterland@Sun.COM 			strlen(PASSWD) + 1)) == NULL) {
641*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
642*9781SMoriah.Waterland@Sun.COM 				pkg_gt(ERR_MALLOC), "clpwnam()",
643*9781SMoriah.Waterland@Sun.COM 				strlen(instroot) + strlen(PASSWD), "buf");
644*9781SMoriah.Waterland@Sun.COM 		}
645*9781SMoriah.Waterland@Sun.COM 		(void) sprintf(buf, "%s%s", instroot, PASSWD);
646*9781SMoriah.Waterland@Sun.COM 		if ((pw_ptr = fopen(buf, "r")) == NULL) {
647*9781SMoriah.Waterland@Sun.COM 			free(buf);
648*9781SMoriah.Waterland@Sun.COM 			return (NULL);
649*9781SMoriah.Waterland@Sun.COM 		} else {
650*9781SMoriah.Waterland@Sun.COM 			while ((pw = fgetpwent(pw_ptr)) != NULL) {
651*9781SMoriah.Waterland@Sun.COM 				if (strcmp(pw->pw_name, nam) == 0) {
652*9781SMoriah.Waterland@Sun.COM 					break;
653*9781SMoriah.Waterland@Sun.COM 				}
654*9781SMoriah.Waterland@Sun.COM 			}
655*9781SMoriah.Waterland@Sun.COM 		}
656*9781SMoriah.Waterland@Sun.COM 		free(buf);
657*9781SMoriah.Waterland@Sun.COM 		(void) fclose(pw_ptr);
658*9781SMoriah.Waterland@Sun.COM 		return (pw);
659*9781SMoriah.Waterland@Sun.COM 	} else {
660*9781SMoriah.Waterland@Sun.COM 		return (NULL);
661*9781SMoriah.Waterland@Sun.COM 	}
662*9781SMoriah.Waterland@Sun.COM }
663*9781SMoriah.Waterland@Sun.COM 
664*9781SMoriah.Waterland@Sun.COM /*
665*9781SMoriah.Waterland@Sun.COM  * Check the client's etc/group file for the group id
666*9781SMoriah.Waterland@Sun.COM  *
667*9781SMoriah.Waterland@Sun.COM  * returns a pointer to the group structure if the group id is found
668*9781SMoriah.Waterland@Sun.COM  * returns NULL if not found
669*9781SMoriah.Waterland@Sun.COM  */
670*9781SMoriah.Waterland@Sun.COM struct group *
clgrgid(gid_t gid)671*9781SMoriah.Waterland@Sun.COM clgrgid(gid_t gid)
672*9781SMoriah.Waterland@Sun.COM {
673*9781SMoriah.Waterland@Sun.COM 	struct group *gr;
674*9781SMoriah.Waterland@Sun.COM 	char *instroot, *buf;
675*9781SMoriah.Waterland@Sun.COM 	FILE *gr_ptr;
676*9781SMoriah.Waterland@Sun.COM 
677*9781SMoriah.Waterland@Sun.COM 	if ((instroot = get_install_root()) != NULL) {
678*9781SMoriah.Waterland@Sun.COM 		if ((buf = (char *)malloc(strlen(instroot) +
679*9781SMoriah.Waterland@Sun.COM 			strlen(GROUP) + 1)) == NULL) {
680*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
681*9781SMoriah.Waterland@Sun.COM 				pkg_gt(ERR_MALLOC), "clgrgid()",
682*9781SMoriah.Waterland@Sun.COM 				strlen(instroot) + strlen(GROUP), "buf");
683*9781SMoriah.Waterland@Sun.COM 		}
684*9781SMoriah.Waterland@Sun.COM 		(void) sprintf(buf, "%s%s", instroot, GROUP);
685*9781SMoriah.Waterland@Sun.COM 		if ((gr_ptr = fopen(buf, "r")) == NULL) {
686*9781SMoriah.Waterland@Sun.COM 			free(buf);
687*9781SMoriah.Waterland@Sun.COM 			return (NULL);
688*9781SMoriah.Waterland@Sun.COM 		} else {
689*9781SMoriah.Waterland@Sun.COM 			while ((gr = fgetgrent(gr_ptr)) != NULL) {
690*9781SMoriah.Waterland@Sun.COM 				if (gr->gr_gid == gid) {
691*9781SMoriah.Waterland@Sun.COM 					break;
692*9781SMoriah.Waterland@Sun.COM 				}
693*9781SMoriah.Waterland@Sun.COM 			}
694*9781SMoriah.Waterland@Sun.COM 		}
695*9781SMoriah.Waterland@Sun.COM 		free(buf);
696*9781SMoriah.Waterland@Sun.COM 		(void) fclose(gr_ptr);
697*9781SMoriah.Waterland@Sun.COM 		return (gr);
698*9781SMoriah.Waterland@Sun.COM 	} else {
699*9781SMoriah.Waterland@Sun.COM 		return (NULL);
700*9781SMoriah.Waterland@Sun.COM 	}
701*9781SMoriah.Waterland@Sun.COM }
702*9781SMoriah.Waterland@Sun.COM 
703*9781SMoriah.Waterland@Sun.COM /*
704*9781SMoriah.Waterland@Sun.COM  * Check the client's etc/passwd file for the user id
705*9781SMoriah.Waterland@Sun.COM  *
706*9781SMoriah.Waterland@Sun.COM  * returns a pointer to the passwd structure if the user id is found
707*9781SMoriah.Waterland@Sun.COM  * returns NULL if not found
708*9781SMoriah.Waterland@Sun.COM  */
709*9781SMoriah.Waterland@Sun.COM struct passwd *
clpwuid(uid_t uid)710*9781SMoriah.Waterland@Sun.COM clpwuid(uid_t uid)
711*9781SMoriah.Waterland@Sun.COM {
712*9781SMoriah.Waterland@Sun.COM 	struct passwd *pw;
713*9781SMoriah.Waterland@Sun.COM 	char *instroot, *buf;
714*9781SMoriah.Waterland@Sun.COM 	FILE *pw_ptr;
715*9781SMoriah.Waterland@Sun.COM 
716*9781SMoriah.Waterland@Sun.COM 	if ((instroot = get_install_root()) != NULL) {
717*9781SMoriah.Waterland@Sun.COM 		if ((buf = (char *)malloc(strlen(instroot) +
718*9781SMoriah.Waterland@Sun.COM 			strlen(PASSWD) + 1)) == NULL) {
719*9781SMoriah.Waterland@Sun.COM 			(void) fprintf(stderr,
720*9781SMoriah.Waterland@Sun.COM 				pkg_gt(ERR_MALLOC), "clpwuid()",
721*9781SMoriah.Waterland@Sun.COM 				strlen(instroot) + strlen(PASSWD), "buf");
722*9781SMoriah.Waterland@Sun.COM 		}
723*9781SMoriah.Waterland@Sun.COM 		(void) sprintf(buf, "%s%s", instroot, PASSWD);
724*9781SMoriah.Waterland@Sun.COM 		if ((pw_ptr = fopen(buf, "r")) == NULL) {
725*9781SMoriah.Waterland@Sun.COM 			free(buf);
726*9781SMoriah.Waterland@Sun.COM 			return (NULL);
727*9781SMoriah.Waterland@Sun.COM 		} else {
728*9781SMoriah.Waterland@Sun.COM 			while ((pw = fgetpwent(pw_ptr)) != NULL) {
729*9781SMoriah.Waterland@Sun.COM 				if (pw->pw_uid == uid) {
730*9781SMoriah.Waterland@Sun.COM 					break;
731*9781SMoriah.Waterland@Sun.COM 				}
732*9781SMoriah.Waterland@Sun.COM 			}
733*9781SMoriah.Waterland@Sun.COM 		}
734*9781SMoriah.Waterland@Sun.COM 		free(buf);
735*9781SMoriah.Waterland@Sun.COM 		(void) fclose(pw_ptr);
736*9781SMoriah.Waterland@Sun.COM 		return (pw);
737*9781SMoriah.Waterland@Sun.COM 	} else {
738*9781SMoriah.Waterland@Sun.COM 		return (NULL);
739*9781SMoriah.Waterland@Sun.COM 	}
740*9781SMoriah.Waterland@Sun.COM }
741