xref: /onnv-gate/usr/src/lib/libc/port/gen/getgrnam.c (revision 6279:696954f6dab2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6279Sdjl  * Common Development and Distribution License (the "License").
6*6279Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*6279Sdjl  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*	3.0 SID #	1.2	*/
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #pragma weak getgrnam	= _getgrnam
350Sstevel@tonic-gate #pragma weak getgrgid	= _getgrgid
360Sstevel@tonic-gate #pragma weak getgrent	= _getgrent
370Sstevel@tonic-gate #pragma weak fgetgrent	= _fgetgrent
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include "synonyms.h"
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <grp.h>
420Sstevel@tonic-gate #include <nss_dbdefs.h>
430Sstevel@tonic-gate #include <stdio.h>
440Sstevel@tonic-gate #include "tsd.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #ifdef	NSS_INCLUDE_UNSAFE
470Sstevel@tonic-gate 
48*6279Sdjl extern size_t _nss_get_bufsizes(int arg);
49*6279Sdjl 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * Ye olde non-reentrant interface (MT-unsafe, caveat utor)
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static void
550Sstevel@tonic-gate free_grbuf(void *arg)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	nss_XbyY_buf_t **buffer = arg;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	NSS_XbyY_FREE(buffer);
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static nss_XbyY_buf_t *
63*6279Sdjl get_grbuf(int max_buf)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	nss_XbyY_buf_t **buffer =
660Sstevel@tonic-gate 	    tsdalloc(_T_GRBUF, sizeof (nss_XbyY_buf_t *), free_grbuf);
670Sstevel@tonic-gate 	nss_XbyY_buf_t *b;
68*6279Sdjl 	size_t	blen;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if (buffer == NULL)
710Sstevel@tonic-gate 		return (NULL);
72*6279Sdjl 	if (max_buf == 0)
73*6279Sdjl 		blen = _nss_get_bufsizes(0);		/* default size */
74*6279Sdjl 	else
75*6279Sdjl 		blen = sysconf(_SC_GETGR_R_SIZE_MAX);	/* max size */
76*6279Sdjl 	if (*buffer) {
77*6279Sdjl 		if ((*buffer)->buflen >= blen)	/* existing size fits */
78*6279Sdjl 			return (*buffer);
79*6279Sdjl 		NSS_XbyY_FREE(buffer);		/* existing is too small */
80*6279Sdjl 	}
81*6279Sdjl 	b = NSS_XbyY_ALLOC(buffer, sizeof (struct group), blen);
820Sstevel@tonic-gate 	return (b);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate 
850Sstevel@tonic-gate struct group *
860Sstevel@tonic-gate getgrgid(gid_t gid)
870Sstevel@tonic-gate {
88*6279Sdjl 	nss_XbyY_buf_t	*b = get_grbuf(0);
89*6279Sdjl 	struct group *ret;
90*6279Sdjl 
91*6279Sdjl 	if (b == NULL)
92*6279Sdjl 		return (NULL);
930Sstevel@tonic-gate 
94*6279Sdjl 	ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
95*6279Sdjl 	if (ret == NULL && errno == ERANGE) {
96*6279Sdjl 		b = get_grbuf(1);
97*6279Sdjl 		if (b == NULL)
98*6279Sdjl 			return (NULL);
99*6279Sdjl 		ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
100*6279Sdjl 	}
101*6279Sdjl 	return (ret);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate struct group *
1050Sstevel@tonic-gate getgrnam(const char *nam)
1060Sstevel@tonic-gate {
107*6279Sdjl 	nss_XbyY_buf_t	*b = get_grbuf(0);
108*6279Sdjl 	struct group *ret;
109*6279Sdjl 
110*6279Sdjl 	if (b == NULL)
111*6279Sdjl 		return (NULL);
1120Sstevel@tonic-gate 
113*6279Sdjl 	ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
114*6279Sdjl 	if (ret == NULL && errno == ERANGE && nam != NULL) {
115*6279Sdjl 		b = get_grbuf(1);
116*6279Sdjl 		if (b == NULL)
117*6279Sdjl 			return (NULL);
118*6279Sdjl 		ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
119*6279Sdjl 	}
120*6279Sdjl 	return (ret);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate struct group *
1240Sstevel@tonic-gate getgrent(void)
1250Sstevel@tonic-gate {
126*6279Sdjl 	nss_XbyY_buf_t	*b = get_grbuf(1);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	return (b == NULL ? NULL :
1290Sstevel@tonic-gate 	    getgrent_r(b->result, b->buffer, b->buflen));
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate struct group *
1330Sstevel@tonic-gate fgetgrent(FILE *f)
1340Sstevel@tonic-gate {
135*6279Sdjl 	nss_XbyY_buf_t	*b = get_grbuf(1);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	return (b == NULL ? NULL :
1380Sstevel@tonic-gate 	    fgetgrent_r(f, b->result, b->buffer, b->buflen));
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #endif	/* NSS_INCLUDE_UNSAFE */
142