xref: /onnv-gate/usr/src/lib/libresolv2/common/irs/getnetgrent_r.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
3*11038SRao.Shoaib@Sun.COM  * Copyright (C) 1998, 1999, 2001, 2003  Internet Software Consortium.
40Sstevel@tonic-gate  *
5*11038SRao.Shoaib@Sun.COM  * Permission to use, copy, modify, and/or distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*11038SRao.Shoaib@Sun.COM  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*11038SRao.Shoaib@Sun.COM  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*11038SRao.Shoaib@Sun.COM  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*11038SRao.Shoaib@Sun.COM  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*11038SRao.Shoaib@Sun.COM  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*11038SRao.Shoaib@Sun.COM  * PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: getnetgrent_r.c,v 1.14 2008/11/14 02:36:51 marka Exp $";
200Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #include <port_before.h>
230Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
240Sstevel@tonic-gate 	static int getnetgrent_r_not_required = 0;
250Sstevel@tonic-gate #else
260Sstevel@tonic-gate #include <errno.h>
270Sstevel@tonic-gate #include <string.h>
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <netinet/in.h>
310Sstevel@tonic-gate #include <netdb.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate #include <port_after.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #ifdef NGR_R_RETURN
36*11038SRao.Shoaib@Sun.COM #ifndef NGR_R_PRIVATE
37*11038SRao.Shoaib@Sun.COM #define NGR_R_PRIVATE 0
38*11038SRao.Shoaib@Sun.COM #endif
390Sstevel@tonic-gate 
40*11038SRao.Shoaib@Sun.COM static NGR_R_RETURN
41*11038SRao.Shoaib@Sun.COM copy_protoent(NGR_R_CONST char **, NGR_R_CONST char **, NGR_R_CONST char **,
42*11038SRao.Shoaib@Sun.COM 	      const char *, const char *, const char *, NGR_R_COPY_ARGS);
430Sstevel@tonic-gate 
440Sstevel@tonic-gate NGR_R_RETURN
innetgr_r(const char * netgroup,const char * host,const char * user,const char * domain)450Sstevel@tonic-gate innetgr_r(const char *netgroup, const char *host, const char *user,
460Sstevel@tonic-gate 	  const char *domain) {
470Sstevel@tonic-gate 	char *ng, *ho, *us, *dom;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	DE_CONST(netgroup, ng);
500Sstevel@tonic-gate 	DE_CONST(host, ho);
510Sstevel@tonic-gate 	DE_CONST(user, us);
520Sstevel@tonic-gate 	DE_CONST(domain, dom);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 	return (innetgr(ng, ho, us, dom));
550Sstevel@tonic-gate }
560Sstevel@tonic-gate 
57*11038SRao.Shoaib@Sun.COM /*%
580Sstevel@tonic-gate  *	These assume a single context is in operation per thread.
590Sstevel@tonic-gate  *	If this is not the case we will need to call irs directly
600Sstevel@tonic-gate  *	rather than through the base functions.
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate NGR_R_RETURN
getnetgrent_r(NGR_R_CONST char ** machinep,NGR_R_CONST char ** userp,NGR_R_CONST char ** domainp,NGR_R_ARGS)64*11038SRao.Shoaib@Sun.COM getnetgrent_r(NGR_R_CONST char **machinep, NGR_R_CONST char **userp,
65*11038SRao.Shoaib@Sun.COM 	      NGR_R_CONST char **domainp, NGR_R_ARGS)
66*11038SRao.Shoaib@Sun.COM {
67*11038SRao.Shoaib@Sun.COM 	NGR_R_CONST char *mp, *up, *dp;
680Sstevel@tonic-gate 	int res = getnetgrent(&mp, &up, &dp);
690Sstevel@tonic-gate 
70*11038SRao.Shoaib@Sun.COM 	if (res != 1)
710Sstevel@tonic-gate 		return (res);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	return (copy_protoent(machinep, userp, domainp,
740Sstevel@tonic-gate 				mp, up, dp, NGR_R_COPY));
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
77*11038SRao.Shoaib@Sun.COM #if NGR_R_PRIVATE == 2
78*11038SRao.Shoaib@Sun.COM struct private {
79*11038SRao.Shoaib@Sun.COM 	char *buf;
80*11038SRao.Shoaib@Sun.COM };
81*11038SRao.Shoaib@Sun.COM 
82*11038SRao.Shoaib@Sun.COM #endif
830Sstevel@tonic-gate NGR_R_SET_RETURN
84*11038SRao.Shoaib@Sun.COM #ifdef NGR_R_SET_ARGS
setnetgrent_r(NGR_R_SET_CONST char * netgroup,NGR_R_SET_ARGS)85*11038SRao.Shoaib@Sun.COM setnetgrent_r(NGR_R_SET_CONST char *netgroup, NGR_R_SET_ARGS)
860Sstevel@tonic-gate #else
87*11038SRao.Shoaib@Sun.COM setnetgrent_r(NGR_R_SET_CONST char *netgroup)
880Sstevel@tonic-gate #endif
890Sstevel@tonic-gate {
90*11038SRao.Shoaib@Sun.COM #if NGR_R_PRIVATE == 2
91*11038SRao.Shoaib@Sun.COM 	struct private *p;
92*11038SRao.Shoaib@Sun.COM #endif
93*11038SRao.Shoaib@Sun.COM 	char *tmp;
94*11038SRao.Shoaib@Sun.COM #if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0
95*11038SRao.Shoaib@Sun.COM 	UNUSED(buf);
96*11038SRao.Shoaib@Sun.COM 	UNUSED(buflen);
97*11038SRao.Shoaib@Sun.COM #endif
98*11038SRao.Shoaib@Sun.COM 
99*11038SRao.Shoaib@Sun.COM 	DE_CONST(netgroup, tmp);
100*11038SRao.Shoaib@Sun.COM 	setnetgrent(tmp);
101*11038SRao.Shoaib@Sun.COM 
102*11038SRao.Shoaib@Sun.COM #if NGR_R_PRIVATE == 1
1030Sstevel@tonic-gate 	*buf = NULL;
104*11038SRao.Shoaib@Sun.COM #elif NGR_R_PRIVATE == 2
105*11038SRao.Shoaib@Sun.COM 	*buf = p = malloc(sizeof(struct private));
106*11038SRao.Shoaib@Sun.COM 	if (p == NULL)
107*11038SRao.Shoaib@Sun.COM #ifdef NGR_R_SET_RESULT
108*11038SRao.Shoaib@Sun.COM 		return (NGR_R_BAD);
109*11038SRao.Shoaib@Sun.COM #else
110*11038SRao.Shoaib@Sun.COM 		return;
111*11038SRao.Shoaib@Sun.COM #endif
112*11038SRao.Shoaib@Sun.COM 	p->buf = NULL;
1130Sstevel@tonic-gate #endif
1140Sstevel@tonic-gate #ifdef NGR_R_SET_RESULT
1150Sstevel@tonic-gate 	return (NGR_R_SET_RESULT);
1160Sstevel@tonic-gate #endif
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate NGR_R_END_RETURN
120*11038SRao.Shoaib@Sun.COM #ifdef NGR_R_END_ARGS
endnetgrent_r(NGR_R_END_ARGS)121*11038SRao.Shoaib@Sun.COM endnetgrent_r(NGR_R_END_ARGS)
1220Sstevel@tonic-gate #else
1230Sstevel@tonic-gate endnetgrent_r(void)
1240Sstevel@tonic-gate #endif
1250Sstevel@tonic-gate {
126*11038SRao.Shoaib@Sun.COM #if NGR_R_PRIVATE == 2
127*11038SRao.Shoaib@Sun.COM 	struct private *p = buf;
128*11038SRao.Shoaib@Sun.COM #endif
129*11038SRao.Shoaib@Sun.COM #if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0
130*11038SRao.Shoaib@Sun.COM 	UNUSED(buf);
131*11038SRao.Shoaib@Sun.COM 	UNUSED(buflen);
132*11038SRao.Shoaib@Sun.COM #endif
133*11038SRao.Shoaib@Sun.COM 
1340Sstevel@tonic-gate 	endnetgrent();
135*11038SRao.Shoaib@Sun.COM #if NGR_R_PRIVATE == 1
1360Sstevel@tonic-gate 	if (*buf != NULL)
1370Sstevel@tonic-gate 		free(*buf);
1380Sstevel@tonic-gate 	*buf = NULL;
139*11038SRao.Shoaib@Sun.COM #elif NGR_R_PRIVATE == 2
140*11038SRao.Shoaib@Sun.COM 	if (p->buf != NULL)
141*11038SRao.Shoaib@Sun.COM 		free(p->buf);
142*11038SRao.Shoaib@Sun.COM 	free(p);
1430Sstevel@tonic-gate #endif
1440Sstevel@tonic-gate 	NGR_R_END_RESULT(NGR_R_OK);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /* Private */
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static int
copy_protoent(NGR_R_CONST char ** machinep,NGR_R_CONST char ** userp,NGR_R_CONST char ** domainp,const char * mp,const char * up,const char * dp,NGR_R_COPY_ARGS)150*11038SRao.Shoaib@Sun.COM copy_protoent(NGR_R_CONST char **machinep, NGR_R_CONST char **userp,
151*11038SRao.Shoaib@Sun.COM 	      NGR_R_CONST char **domainp, const char *mp, const char *up,
152*11038SRao.Shoaib@Sun.COM 	      const char *dp, NGR_R_COPY_ARGS)
153*11038SRao.Shoaib@Sun.COM {
154*11038SRao.Shoaib@Sun.COM #if NGR_R_PRIVATE == 2
155*11038SRao.Shoaib@Sun.COM 	struct private *p = buf;
156*11038SRao.Shoaib@Sun.COM #endif
1570Sstevel@tonic-gate 	char *cp;
1580Sstevel@tonic-gate 	int n;
1590Sstevel@tonic-gate 	int len;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/* Find out the amount of space required to store the answer. */
1620Sstevel@tonic-gate 	len = 0;
1630Sstevel@tonic-gate 	if (mp != NULL) len += strlen(mp) + 1;
1640Sstevel@tonic-gate 	if (up != NULL) len += strlen(up) + 1;
1650Sstevel@tonic-gate 	if (dp != NULL) len += strlen(dp) + 1;
166*11038SRao.Shoaib@Sun.COM 
167*11038SRao.Shoaib@Sun.COM #if NGR_R_PRIVATE == 1
168*11038SRao.Shoaib@Sun.COM 	if (*buf != NULL)
169*11038SRao.Shoaib@Sun.COM 		free(*buf);
1700Sstevel@tonic-gate 	*buf = malloc(len);
1710Sstevel@tonic-gate 	if (*buf == NULL)
1720Sstevel@tonic-gate 		return(NGR_R_BAD);
1730Sstevel@tonic-gate 	cp = *buf;
174*11038SRao.Shoaib@Sun.COM #elif NGR_R_PRIVATE == 2
175*11038SRao.Shoaib@Sun.COM 	if (p->buf)
176*11038SRao.Shoaib@Sun.COM 		free(p->buf);
177*11038SRao.Shoaib@Sun.COM 	p->buf = malloc(len);
178*11038SRao.Shoaib@Sun.COM 	if (p->buf == NULL)
179*11038SRao.Shoaib@Sun.COM 		return(NGR_R_BAD);
180*11038SRao.Shoaib@Sun.COM 	cp = p->buf;
1810Sstevel@tonic-gate #else
1820Sstevel@tonic-gate 	if (len > (int)buflen) {
1830Sstevel@tonic-gate 		errno = ERANGE;
1840Sstevel@tonic-gate 		return (NGR_R_BAD);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	cp = buf;
1870Sstevel@tonic-gate #endif
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (mp != NULL) {
1900Sstevel@tonic-gate 		n = strlen(mp) + 1;
1910Sstevel@tonic-gate 		strcpy(cp, mp);
1920Sstevel@tonic-gate 		*machinep = cp;
1930Sstevel@tonic-gate 		cp += n;
1940Sstevel@tonic-gate 	} else
1950Sstevel@tonic-gate 		*machinep = NULL;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	if (up != NULL) {
1980Sstevel@tonic-gate 		n = strlen(up) + 1;
1990Sstevel@tonic-gate 		strcpy(cp, up);
2000Sstevel@tonic-gate 		*userp = cp;
2010Sstevel@tonic-gate 		cp += n;
2020Sstevel@tonic-gate 	} else
2030Sstevel@tonic-gate 		*userp = NULL;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	if (dp != NULL) {
2060Sstevel@tonic-gate 		n = strlen(dp) + 1;
2070Sstevel@tonic-gate 		strcpy(cp, dp);
2080Sstevel@tonic-gate 		*domainp = cp;
2090Sstevel@tonic-gate 		cp += n;
2100Sstevel@tonic-gate 	} else
2110Sstevel@tonic-gate 		*domainp = NULL;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	return (NGR_R_OK);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate #else /* NGR_R_RETURN */
2160Sstevel@tonic-gate 	static int getnetgrent_r_unknown_system = 0;
2170Sstevel@tonic-gate #endif /* NGR_R_RETURN */
2180Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
219*11038SRao.Shoaib@Sun.COM /*! \file */
220