xref: /illumos-gate/usr/src/lib/libc/port/gen/getgrnam.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma weak _getgrnam	= getgrnam
31 #pragma weak _getgrgid	= getgrgid
32 
33 #include "lint.h"
34 #include <sys/types.h>
35 #include <grp.h>
36 #include <nss_dbdefs.h>
37 #include <stdio.h>
38 #include "tsd.h"
39 
40 #ifdef	NSS_INCLUDE_UNSAFE
41 
42 extern size_t _nss_get_bufsizes(int arg);
43 
44 /*
45  * Ye olde non-reentrant interface (MT-unsafe, caveat utor)
46  */
47 
48 static void
free_grbuf(void * arg)49 free_grbuf(void *arg)
50 {
51 	nss_XbyY_buf_t **buffer = arg;
52 
53 	NSS_XbyY_FREE(buffer);
54 }
55 
56 static nss_XbyY_buf_t *
get_grbuf(int max_buf)57 get_grbuf(int max_buf)
58 {
59 	nss_XbyY_buf_t **buffer =
60 	    tsdalloc(_T_GRBUF, sizeof (nss_XbyY_buf_t *), free_grbuf);
61 	nss_XbyY_buf_t *b;
62 	size_t	blen;
63 
64 	if (buffer == NULL)
65 		return (NULL);
66 	if (max_buf == 0)
67 		blen = _nss_get_bufsizes(0);		/* default size */
68 	else
69 		blen = sysconf(_SC_GETGR_R_SIZE_MAX);	/* max size */
70 	if (*buffer) {
71 		if ((*buffer)->buflen >= blen)	/* existing size fits */
72 			return (*buffer);
73 		NSS_XbyY_FREE(buffer);		/* existing is too small */
74 	}
75 	b = NSS_XbyY_ALLOC(buffer, sizeof (struct group), blen);
76 	return (b);
77 }
78 
79 struct group *
getgrgid(gid_t gid)80 getgrgid(gid_t gid)
81 {
82 	nss_XbyY_buf_t	*b = get_grbuf(0);
83 	struct group *ret;
84 
85 	if (b == NULL)
86 		return (NULL);
87 
88 	ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
89 	if (ret == NULL && errno == ERANGE) {
90 		b = get_grbuf(1);
91 		if (b == NULL)
92 			return (NULL);
93 		ret = getgrgid_r(gid, b->result, b->buffer, b->buflen);
94 	}
95 	return (ret);
96 }
97 
98 struct group *
getgrnam(const char * nam)99 getgrnam(const char *nam)
100 {
101 	nss_XbyY_buf_t	*b = get_grbuf(0);
102 	struct group *ret;
103 
104 	if (b == NULL)
105 		return (NULL);
106 
107 	ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
108 	if (ret == NULL && errno == ERANGE && nam != NULL) {
109 		b = get_grbuf(1);
110 		if (b == NULL)
111 			return (NULL);
112 		ret = getgrnam_r(nam, b->result, b->buffer, b->buflen);
113 	}
114 	return (ret);
115 }
116 
117 struct group *
getgrent(void)118 getgrent(void)
119 {
120 	nss_XbyY_buf_t	*b = get_grbuf(1);
121 
122 	return (b == NULL ? NULL :
123 	    getgrent_r(b->result, b->buffer, b->buflen));
124 }
125 
126 struct group *
fgetgrent(FILE * f)127 fgetgrent(FILE *f)
128 {
129 	nss_XbyY_buf_t	*b = get_grbuf(1);
130 
131 	return (b == NULL ? NULL :
132 	    fgetgrent_r(f, b->result, b->buffer, b->buflen));
133 }
134 
135 #endif	/* NSS_INCLUDE_UNSAFE */
136