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 ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
32*7257d1b4Sraf #pragma weak _getgrnam = getgrnam
33*7257d1b4Sraf #pragma weak _getgrgid = getgrgid
347c478bd9Sstevel@tonic-gate
35*7257d1b4Sraf #include "lint.h"
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <grp.h>
387c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include "tsd.h"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #ifdef NSS_INCLUDE_UNSAFE
437c478bd9Sstevel@tonic-gate
446dd84139Sdjl extern size_t _nss_get_bufsizes(int arg);
456dd84139Sdjl
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * Ye olde non-reentrant interface (MT-unsafe, caveat utor)
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate static void
free_grbuf(void * arg)517c478bd9Sstevel@tonic-gate free_grbuf(void *arg)
527c478bd9Sstevel@tonic-gate {
537c478bd9Sstevel@tonic-gate nss_XbyY_buf_t **buffer = arg;
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate NSS_XbyY_FREE(buffer);
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *
get_grbuf(int max_buf)596dd84139Sdjl get_grbuf(int max_buf)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate nss_XbyY_buf_t **buffer =
627c478bd9Sstevel@tonic-gate tsdalloc(_T_GRBUF, sizeof (nss_XbyY_buf_t *), free_grbuf);
637c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *b;
646dd84139Sdjl size_t blen;
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate if (buffer == NULL)
677c478bd9Sstevel@tonic-gate return (NULL);
686dd84139Sdjl if (max_buf == 0)
696dd84139Sdjl blen = _nss_get_bufsizes(0); /* default size */
706dd84139Sdjl else
716dd84139Sdjl blen = sysconf(_SC_GETGR_R_SIZE_MAX); /* max size */
726dd84139Sdjl if (*buffer) {
736dd84139Sdjl if ((*buffer)->buflen >= blen) /* existing size fits */
746dd84139Sdjl return (*buffer);
756dd84139Sdjl NSS_XbyY_FREE(buffer); /* existing is too small */
766dd84139Sdjl }
776dd84139Sdjl b = NSS_XbyY_ALLOC(buffer, sizeof (struct group), blen);
787c478bd9Sstevel@tonic-gate return (b);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate struct group *
getgrgid(gid_t gid)827c478bd9Sstevel@tonic-gate getgrgid(gid_t gid)
837c478bd9Sstevel@tonic-gate {
846dd84139Sdjl nss_XbyY_buf_t *b = get_grbuf(0);
856dd84139Sdjl struct group *ret;
867c478bd9Sstevel@tonic-gate
876dd84139Sdjl if (b == NULL)
886dd84139Sdjl return (NULL);
896dd84139Sdjl
906dd84139Sdjl ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
916dd84139Sdjl if (ret == NULL && errno == ERANGE) {
926dd84139Sdjl b = get_grbuf(1);
936dd84139Sdjl if (b == NULL)
946dd84139Sdjl return (NULL);
956dd84139Sdjl ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
966dd84139Sdjl }
976dd84139Sdjl return (ret);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate struct group *
getgrnam(const char * nam)1017c478bd9Sstevel@tonic-gate getgrnam(const char *nam)
1027c478bd9Sstevel@tonic-gate {
1036dd84139Sdjl nss_XbyY_buf_t *b = get_grbuf(0);
1046dd84139Sdjl struct group *ret;
1057c478bd9Sstevel@tonic-gate
1066dd84139Sdjl if (b == NULL)
1076dd84139Sdjl return (NULL);
1086dd84139Sdjl
1096dd84139Sdjl ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
1106dd84139Sdjl if (ret == NULL && errno == ERANGE && nam != NULL) {
1116dd84139Sdjl b = get_grbuf(1);
1126dd84139Sdjl if (b == NULL)
1136dd84139Sdjl return (NULL);
1146dd84139Sdjl ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
1156dd84139Sdjl }
1166dd84139Sdjl return (ret);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate struct group *
getgrent(void)1207c478bd9Sstevel@tonic-gate getgrent(void)
1217c478bd9Sstevel@tonic-gate {
1226dd84139Sdjl nss_XbyY_buf_t *b = get_grbuf(1);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate return (b == NULL ? NULL :
1257c478bd9Sstevel@tonic-gate getgrent_r(b->result, b->buffer, b->buflen));
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate struct group *
fgetgrent(FILE * f)1297c478bd9Sstevel@tonic-gate fgetgrent(FILE *f)
1307c478bd9Sstevel@tonic-gate {
1316dd84139Sdjl nss_XbyY_buf_t *b = get_grbuf(1);
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate return (b == NULL ? NULL :
1347c478bd9Sstevel@tonic-gate fgetgrent_r(f, b->result, b->buffer, b->buflen));
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate #endif /* NSS_INCLUDE_UNSAFE */
138