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
56279Sdjl * Common Development and Distribution License (the "License").
66279Sdjl * 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 */
21*6812Sraf
220Sstevel@tonic-gate /*
236279Sdjl * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*6812Sraf #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
32*6812Sraf #pragma weak _getgrnam = getgrnam
33*6812Sraf #pragma weak _getgrgid = getgrgid
340Sstevel@tonic-gate
35*6812Sraf #include "lint.h"
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <grp.h>
380Sstevel@tonic-gate #include <nss_dbdefs.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include "tsd.h"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #ifdef NSS_INCLUDE_UNSAFE
430Sstevel@tonic-gate
446279Sdjl extern size_t _nss_get_bufsizes(int arg);
456279Sdjl
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate * Ye olde non-reentrant interface (MT-unsafe, caveat utor)
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate static void
free_grbuf(void * arg)510Sstevel@tonic-gate free_grbuf(void *arg)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate nss_XbyY_buf_t **buffer = arg;
540Sstevel@tonic-gate
550Sstevel@tonic-gate NSS_XbyY_FREE(buffer);
560Sstevel@tonic-gate }
570Sstevel@tonic-gate
580Sstevel@tonic-gate static nss_XbyY_buf_t *
get_grbuf(int max_buf)596279Sdjl get_grbuf(int max_buf)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate nss_XbyY_buf_t **buffer =
620Sstevel@tonic-gate tsdalloc(_T_GRBUF, sizeof (nss_XbyY_buf_t *), free_grbuf);
630Sstevel@tonic-gate nss_XbyY_buf_t *b;
646279Sdjl size_t blen;
650Sstevel@tonic-gate
660Sstevel@tonic-gate if (buffer == NULL)
670Sstevel@tonic-gate return (NULL);
686279Sdjl if (max_buf == 0)
696279Sdjl blen = _nss_get_bufsizes(0); /* default size */
706279Sdjl else
716279Sdjl blen = sysconf(_SC_GETGR_R_SIZE_MAX); /* max size */
726279Sdjl if (*buffer) {
736279Sdjl if ((*buffer)->buflen >= blen) /* existing size fits */
746279Sdjl return (*buffer);
756279Sdjl NSS_XbyY_FREE(buffer); /* existing is too small */
766279Sdjl }
776279Sdjl b = NSS_XbyY_ALLOC(buffer, sizeof (struct group), blen);
780Sstevel@tonic-gate return (b);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate struct group *
getgrgid(gid_t gid)820Sstevel@tonic-gate getgrgid(gid_t gid)
830Sstevel@tonic-gate {
846279Sdjl nss_XbyY_buf_t *b = get_grbuf(0);
856279Sdjl struct group *ret;
866279Sdjl
876279Sdjl if (b == NULL)
886279Sdjl return (NULL);
890Sstevel@tonic-gate
906279Sdjl ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
916279Sdjl if (ret == NULL && errno == ERANGE) {
926279Sdjl b = get_grbuf(1);
936279Sdjl if (b == NULL)
946279Sdjl return (NULL);
956279Sdjl ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
966279Sdjl }
976279Sdjl return (ret);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate
1000Sstevel@tonic-gate struct group *
getgrnam(const char * nam)1010Sstevel@tonic-gate getgrnam(const char *nam)
1020Sstevel@tonic-gate {
1036279Sdjl nss_XbyY_buf_t *b = get_grbuf(0);
1046279Sdjl struct group *ret;
1056279Sdjl
1066279Sdjl if (b == NULL)
1076279Sdjl return (NULL);
1080Sstevel@tonic-gate
1096279Sdjl ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
1106279Sdjl if (ret == NULL && errno == ERANGE && nam != NULL) {
1116279Sdjl b = get_grbuf(1);
1126279Sdjl if (b == NULL)
1136279Sdjl return (NULL);
1146279Sdjl ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
1156279Sdjl }
1166279Sdjl return (ret);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate struct group *
getgrent(void)1200Sstevel@tonic-gate getgrent(void)
1210Sstevel@tonic-gate {
1226279Sdjl nss_XbyY_buf_t *b = get_grbuf(1);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate return (b == NULL ? NULL :
1250Sstevel@tonic-gate getgrent_r(b->result, b->buffer, b->buflen));
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate struct group *
fgetgrent(FILE * f)1290Sstevel@tonic-gate fgetgrent(FILE *f)
1300Sstevel@tonic-gate {
1316279Sdjl nss_XbyY_buf_t *b = get_grbuf(1);
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate return (b == NULL ? NULL :
1340Sstevel@tonic-gate fgetgrent_r(f, b->result, b->buffer, b->buflen));
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate #endif /* NSS_INCLUDE_UNSAFE */
138