17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
56dd8413djl * Common Development and Distribution License (the "License").
66dd8413djl * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217257d1braf
227c478bdstevel@tonic-gate/*
236dd8413djl * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307257d1braf#pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bdstevel@tonic-gate
327257d1braf#pragma weak _getgrnam	= getgrnam
337257d1braf#pragma weak _getgrgid	= getgrgid
347c478bdstevel@tonic-gate
357257d1braf#include "lint.h"
367c478bdstevel@tonic-gate#include <sys/types.h>
377c478bdstevel@tonic-gate#include <grp.h>
387c478bdstevel@tonic-gate#include <nss_dbdefs.h>
397c478bdstevel@tonic-gate#include <stdio.h>
407c478bdstevel@tonic-gate#include "tsd.h"
417c478bdstevel@tonic-gate
427c478bdstevel@tonic-gate#ifdef	NSS_INCLUDE_UNSAFE
437c478bdstevel@tonic-gate
446dd8413djlextern size_t _nss_get_bufsizes(int arg);
456dd8413djl
467c478bdstevel@tonic-gate/*
477c478bdstevel@tonic-gate * Ye olde non-reentrant interface (MT-unsafe, caveat utor)
487c478bdstevel@tonic-gate */
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gatestatic void
517c478bdstevel@tonic-gatefree_grbuf(void *arg)
527c478bdstevel@tonic-gate{
537c478bdstevel@tonic-gate	nss_XbyY_buf_t **buffer = arg;
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate	NSS_XbyY_FREE(buffer);
567c478bdstevel@tonic-gate}
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gatestatic nss_XbyY_buf_t *
596dd8413djlget_grbuf(int max_buf)
607c478bdstevel@tonic-gate{
617c478bdstevel@tonic-gate	nss_XbyY_buf_t **buffer =
627c478bdstevel@tonic-gate	    tsdalloc(_T_GRBUF, sizeof (nss_XbyY_buf_t *), free_grbuf);
637c478bdstevel@tonic-gate	nss_XbyY_buf_t *b;
646dd8413djl	size_t	blen;
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate	if (buffer == NULL)
677c478bdstevel@tonic-gate		return (NULL);
686dd8413djl	if (max_buf == 0)
696dd8413djl		blen = _nss_get_bufsizes(0);		/* default size */
706dd8413djl	else
716dd8413djl		blen = sysconf(_SC_GETGR_R_SIZE_MAX);	/* max size */
726dd8413djl	if (*buffer) {
736dd8413djl		if ((*buffer)->buflen >= blen)	/* existing size fits */
746dd8413djl			return (*buffer);
756dd8413djl		NSS_XbyY_FREE(buffer);		/* existing is too small */
766dd8413djl	}
776dd8413djl	b = NSS_XbyY_ALLOC(buffer, sizeof (struct group), blen);
787c478bdstevel@tonic-gate	return (b);
797c478bdstevel@tonic-gate}
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gatestruct group *
827c478bdstevel@tonic-gategetgrgid(gid_t gid)
837c478bdstevel@tonic-gate{
846dd8413djl	nss_XbyY_buf_t	*b = get_grbuf(0);
856dd8413djl	struct group *ret;
867c478bdstevel@tonic-gate
876dd8413djl	if (b == NULL)
886dd8413djl		return (NULL);
896dd8413djl
906dd8413djl	ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
916dd8413djl	if (ret == NULL && errno == ERANGE) {
926dd8413djl		b = get_grbuf(1);
936dd8413djl		if (b == NULL)
946dd8413djl			return (NULL);
956dd8413djl		ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
966dd8413djl	}
976dd8413djl	return (ret);
987c478bdstevel@tonic-gate}
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gatestruct group *
1017c478bdstevel@tonic-gategetgrnam(const char *nam)
1027c478bdstevel@tonic-gate{
1036dd8413djl	nss_XbyY_buf_t	*b = get_grbuf(0);
1046dd8413djl	struct group *ret;
1057c478bdstevel@tonic-gate
1066dd8413djl	if (b == NULL)
1076dd8413djl		return (NULL);
1086dd8413djl
1096dd8413djl	ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
1106dd8413djl	if (ret == NULL && errno == ERANGE && nam != NULL) {
1116dd8413djl		b = get_grbuf(1);
1126dd8413djl		if (b == NULL)
1136dd8413djl			return (NULL);
1146dd8413djl		ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
1156dd8413djl	}
1166dd8413djl	return (ret);
1177c478bdstevel@tonic-gate}
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gatestruct group *
1207c478bdstevel@tonic-gategetgrent(void)
1217c478bdstevel@tonic-gate{
1226dd8413djl	nss_XbyY_buf_t	*b = get_grbuf(1);
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate	return (b == NULL ? NULL :
1257c478bdstevel@tonic-gate	    getgrent_r(b->result, b->buffer, b->buflen));
1267c478bdstevel@tonic-gate}
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gatestruct group *
1297c478bdstevel@tonic-gatefgetgrent(FILE *f)
1307c478bdstevel@tonic-gate{
1316dd8413djl	nss_XbyY_buf_t	*b = get_grbuf(1);
1327c478bdstevel@tonic-gate
1337c478bdstevel@tonic-gate	return (b == NULL ? NULL :
1347c478bdstevel@tonic-gate	    fgetgrent_r(f, b->result, b->buffer, b->buflen));
1357c478bdstevel@tonic-gate}
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gate#endif	/* NSS_INCLUDE_UNSAFE */
138