xref: /illumos-gate/usr/src/lib/libc/port/gen/getgrnam.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
56dd84139Sdjl  * Common Development and Distribution License (the "License").
66dd84139Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*7257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
236dd84139Sdjl  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
30*7257d1b4Sraf #pragma weak _getgrnam	= getgrnam
31*7257d1b4Sraf #pragma weak _getgrgid	= getgrgid
327c478bd9Sstevel@tonic-gate 
33*7257d1b4Sraf #include "lint.h"
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <grp.h>
367c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include "tsd.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifdef	NSS_INCLUDE_UNSAFE
417c478bd9Sstevel@tonic-gate 
426dd84139Sdjl extern size_t _nss_get_bufsizes(int arg);
436dd84139Sdjl 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Ye olde non-reentrant interface (MT-unsafe, caveat utor)
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static void
free_grbuf(void * arg)497c478bd9Sstevel@tonic-gate free_grbuf(void *arg)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t **buffer = arg;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	NSS_XbyY_FREE(buffer);
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *
get_grbuf(int max_buf)576dd84139Sdjl get_grbuf(int max_buf)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t **buffer =
607c478bd9Sstevel@tonic-gate 	    tsdalloc(_T_GRBUF, sizeof (nss_XbyY_buf_t *), free_grbuf);
617c478bd9Sstevel@tonic-gate 	nss_XbyY_buf_t *b;
626dd84139Sdjl 	size_t	blen;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (buffer == NULL)
657c478bd9Sstevel@tonic-gate 		return (NULL);
666dd84139Sdjl 	if (max_buf == 0)
676dd84139Sdjl 		blen = _nss_get_bufsizes(0);		/* default size */
686dd84139Sdjl 	else
696dd84139Sdjl 		blen = sysconf(_SC_GETGR_R_SIZE_MAX);	/* max size */
706dd84139Sdjl 	if (*buffer) {
716dd84139Sdjl 		if ((*buffer)->buflen >= blen)	/* existing size fits */
726dd84139Sdjl 			return (*buffer);
736dd84139Sdjl 		NSS_XbyY_FREE(buffer);		/* existing is too small */
746dd84139Sdjl 	}
756dd84139Sdjl 	b = NSS_XbyY_ALLOC(buffer, sizeof (struct group), blen);
767c478bd9Sstevel@tonic-gate 	return (b);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate struct group *
getgrgid(gid_t gid)807c478bd9Sstevel@tonic-gate getgrgid(gid_t gid)
817c478bd9Sstevel@tonic-gate {
826dd84139Sdjl 	nss_XbyY_buf_t	*b = get_grbuf(0);
836dd84139Sdjl 	struct group *ret;
847c478bd9Sstevel@tonic-gate 
856dd84139Sdjl 	if (b == NULL)
866dd84139Sdjl 		return (NULL);
876dd84139Sdjl 
886dd84139Sdjl 	ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
896dd84139Sdjl 	if (ret == NULL && errno == ERANGE) {
906dd84139Sdjl 		b = get_grbuf(1);
916dd84139Sdjl 		if (b == NULL)
926dd84139Sdjl 			return (NULL);
936dd84139Sdjl 		ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
946dd84139Sdjl 	}
956dd84139Sdjl 	return (ret);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate struct group *
getgrnam(const char * nam)997c478bd9Sstevel@tonic-gate getgrnam(const char *nam)
1007c478bd9Sstevel@tonic-gate {
1016dd84139Sdjl 	nss_XbyY_buf_t	*b = get_grbuf(0);
1026dd84139Sdjl 	struct group *ret;
1037c478bd9Sstevel@tonic-gate 
1046dd84139Sdjl 	if (b == NULL)
1056dd84139Sdjl 		return (NULL);
1066dd84139Sdjl 
1076dd84139Sdjl 	ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
1086dd84139Sdjl 	if (ret == NULL && errno == ERANGE && nam != NULL) {
1096dd84139Sdjl 		b = get_grbuf(1);
1106dd84139Sdjl 		if (b == NULL)
1116dd84139Sdjl 			return (NULL);
1126dd84139Sdjl 		ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
1136dd84139Sdjl 	}
1146dd84139Sdjl 	return (ret);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate struct group *
getgrent(void)1187c478bd9Sstevel@tonic-gate getgrent(void)
1197c478bd9Sstevel@tonic-gate {
1206dd84139Sdjl 	nss_XbyY_buf_t	*b = get_grbuf(1);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	return (b == NULL ? NULL :
1237c478bd9Sstevel@tonic-gate 	    getgrent_r(b->result, b->buffer, b->buflen));
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate struct group *
fgetgrent(FILE * f)1277c478bd9Sstevel@tonic-gate fgetgrent(FILE *f)
1287c478bd9Sstevel@tonic-gate {
1296dd84139Sdjl 	nss_XbyY_buf_t	*b = get_grbuf(1);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	return (b == NULL ? NULL :
1327c478bd9Sstevel@tonic-gate 	    fgetgrent_r(f, b->result, b->buffer, b->buflen));
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #endif	/* NSS_INCLUDE_UNSAFE */
136