17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
3*9525b14bSRao Shoaib  * Copyright (C) 1998, 1999, 2001, 2003  Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
5*9525b14bSRao Shoaib  * Permission to use, copy, modify, and/or distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*9525b14bSRao Shoaib  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*9525b14bSRao Shoaib  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*9525b14bSRao Shoaib  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*9525b14bSRao Shoaib  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*9525b14bSRao Shoaib  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*9525b14bSRao Shoaib  * PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include <port_before.h>
197c478bd9Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
207c478bd9Sstevel@tonic-gate 	static int getnetgrent_r_not_required = 0;
217c478bd9Sstevel@tonic-gate #else
227c478bd9Sstevel@tonic-gate #include <errno.h>
237c478bd9Sstevel@tonic-gate #include <string.h>
247c478bd9Sstevel@tonic-gate #include <stdio.h>
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate #include <netdb.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <port_after.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifdef NGR_R_RETURN
32*9525b14bSRao Shoaib #ifndef NGR_R_PRIVATE
33*9525b14bSRao Shoaib #define NGR_R_PRIVATE 0
34*9525b14bSRao Shoaib #endif
357c478bd9Sstevel@tonic-gate 
36*9525b14bSRao Shoaib static NGR_R_RETURN
37*9525b14bSRao Shoaib copy_protoent(NGR_R_CONST char **, NGR_R_CONST char **, NGR_R_CONST char **,
38*9525b14bSRao Shoaib 	      const char *, const char *, const char *, NGR_R_COPY_ARGS);
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate NGR_R_RETURN
innetgr_r(const char * netgroup,const char * host,const char * user,const char * domain)417c478bd9Sstevel@tonic-gate innetgr_r(const char *netgroup, const char *host, const char *user,
427c478bd9Sstevel@tonic-gate 	  const char *domain) {
437c478bd9Sstevel@tonic-gate 	char *ng, *ho, *us, *dom;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	DE_CONST(netgroup, ng);
467c478bd9Sstevel@tonic-gate 	DE_CONST(host, ho);
477c478bd9Sstevel@tonic-gate 	DE_CONST(user, us);
487c478bd9Sstevel@tonic-gate 	DE_CONST(domain, dom);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	return (innetgr(ng, ho, us, dom));
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
53*9525b14bSRao Shoaib /*%
547c478bd9Sstevel@tonic-gate  *	These assume a single context is in operation per thread.
557c478bd9Sstevel@tonic-gate  *	If this is not the case we will need to call irs directly
567c478bd9Sstevel@tonic-gate  *	rather than through the base functions.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@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)60*9525b14bSRao Shoaib getnetgrent_r(NGR_R_CONST char **machinep, NGR_R_CONST char **userp,
61*9525b14bSRao Shoaib 	      NGR_R_CONST char **domainp, NGR_R_ARGS)
62*9525b14bSRao Shoaib {
63*9525b14bSRao Shoaib 	NGR_R_CONST char *mp, *up, *dp;
647c478bd9Sstevel@tonic-gate 	int res = getnetgrent(&mp, &up, &dp);
657c478bd9Sstevel@tonic-gate 
66*9525b14bSRao Shoaib 	if (res != 1)
677c478bd9Sstevel@tonic-gate 		return (res);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	return (copy_protoent(machinep, userp, domainp,
707c478bd9Sstevel@tonic-gate 				mp, up, dp, NGR_R_COPY));
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate 
73*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 2
74*9525b14bSRao Shoaib struct private {
75*9525b14bSRao Shoaib 	char *buf;
76*9525b14bSRao Shoaib };
77*9525b14bSRao Shoaib 
78*9525b14bSRao Shoaib #endif
797c478bd9Sstevel@tonic-gate NGR_R_SET_RETURN
80*9525b14bSRao Shoaib #ifdef NGR_R_SET_ARGS
setnetgrent_r(NGR_R_SET_CONST char * netgroup,NGR_R_SET_ARGS)81*9525b14bSRao Shoaib setnetgrent_r(NGR_R_SET_CONST char *netgroup, NGR_R_SET_ARGS)
827c478bd9Sstevel@tonic-gate #else
83*9525b14bSRao Shoaib setnetgrent_r(NGR_R_SET_CONST char *netgroup)
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate {
86*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 2
87*9525b14bSRao Shoaib 	struct private *p;
88*9525b14bSRao Shoaib #endif
89*9525b14bSRao Shoaib 	char *tmp;
90*9525b14bSRao Shoaib #if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0
91*9525b14bSRao Shoaib 	UNUSED(buf);
92*9525b14bSRao Shoaib 	UNUSED(buflen);
93*9525b14bSRao Shoaib #endif
94*9525b14bSRao Shoaib 
95*9525b14bSRao Shoaib 	DE_CONST(netgroup, tmp);
96*9525b14bSRao Shoaib 	setnetgrent(tmp);
97*9525b14bSRao Shoaib 
98*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 1
997c478bd9Sstevel@tonic-gate 	*buf = NULL;
100*9525b14bSRao Shoaib #elif NGR_R_PRIVATE == 2
101*9525b14bSRao Shoaib 	*buf = p = malloc(sizeof(struct private));
102*9525b14bSRao Shoaib 	if (p == NULL)
103*9525b14bSRao Shoaib #ifdef NGR_R_SET_RESULT
104*9525b14bSRao Shoaib 		return (NGR_R_BAD);
105*9525b14bSRao Shoaib #else
106*9525b14bSRao Shoaib 		return;
107*9525b14bSRao Shoaib #endif
108*9525b14bSRao Shoaib 	p->buf = NULL;
1097c478bd9Sstevel@tonic-gate #endif
1107c478bd9Sstevel@tonic-gate #ifdef NGR_R_SET_RESULT
1117c478bd9Sstevel@tonic-gate 	return (NGR_R_SET_RESULT);
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate NGR_R_END_RETURN
116*9525b14bSRao Shoaib #ifdef NGR_R_END_ARGS
endnetgrent_r(NGR_R_END_ARGS)117*9525b14bSRao Shoaib endnetgrent_r(NGR_R_END_ARGS)
1187c478bd9Sstevel@tonic-gate #else
1197c478bd9Sstevel@tonic-gate endnetgrent_r(void)
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate {
122*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 2
123*9525b14bSRao Shoaib 	struct private *p = buf;
124*9525b14bSRao Shoaib #endif
125*9525b14bSRao Shoaib #if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0
126*9525b14bSRao Shoaib 	UNUSED(buf);
127*9525b14bSRao Shoaib 	UNUSED(buflen);
128*9525b14bSRao Shoaib #endif
129*9525b14bSRao Shoaib 
1307c478bd9Sstevel@tonic-gate 	endnetgrent();
131*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 1
1327c478bd9Sstevel@tonic-gate 	if (*buf != NULL)
1337c478bd9Sstevel@tonic-gate 		free(*buf);
1347c478bd9Sstevel@tonic-gate 	*buf = NULL;
135*9525b14bSRao Shoaib #elif NGR_R_PRIVATE == 2
136*9525b14bSRao Shoaib 	if (p->buf != NULL)
137*9525b14bSRao Shoaib 		free(p->buf);
138*9525b14bSRao Shoaib 	free(p);
1397c478bd9Sstevel@tonic-gate #endif
1407c478bd9Sstevel@tonic-gate 	NGR_R_END_RESULT(NGR_R_OK);
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate /* Private */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@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)146*9525b14bSRao Shoaib copy_protoent(NGR_R_CONST char **machinep, NGR_R_CONST char **userp,
147*9525b14bSRao Shoaib 	      NGR_R_CONST char **domainp, const char *mp, const char *up,
148*9525b14bSRao Shoaib 	      const char *dp, NGR_R_COPY_ARGS)
149*9525b14bSRao Shoaib {
150*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 2
151*9525b14bSRao Shoaib 	struct private *p = buf;
152*9525b14bSRao Shoaib #endif
1537c478bd9Sstevel@tonic-gate 	char *cp;
1547c478bd9Sstevel@tonic-gate 	int n;
1557c478bd9Sstevel@tonic-gate 	int len;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/* Find out the amount of space required to store the answer. */
1587c478bd9Sstevel@tonic-gate 	len = 0;
1597c478bd9Sstevel@tonic-gate 	if (mp != NULL) len += strlen(mp) + 1;
1607c478bd9Sstevel@tonic-gate 	if (up != NULL) len += strlen(up) + 1;
1617c478bd9Sstevel@tonic-gate 	if (dp != NULL) len += strlen(dp) + 1;
162*9525b14bSRao Shoaib 
163*9525b14bSRao Shoaib #if NGR_R_PRIVATE == 1
164*9525b14bSRao Shoaib 	if (*buf != NULL)
165*9525b14bSRao Shoaib 		free(*buf);
1667c478bd9Sstevel@tonic-gate 	*buf = malloc(len);
1677c478bd9Sstevel@tonic-gate 	if (*buf == NULL)
1687c478bd9Sstevel@tonic-gate 		return(NGR_R_BAD);
1697c478bd9Sstevel@tonic-gate 	cp = *buf;
170*9525b14bSRao Shoaib #elif NGR_R_PRIVATE == 2
171*9525b14bSRao Shoaib 	if (p->buf)
172*9525b14bSRao Shoaib 		free(p->buf);
173*9525b14bSRao Shoaib 	p->buf = malloc(len);
174*9525b14bSRao Shoaib 	if (p->buf == NULL)
175*9525b14bSRao Shoaib 		return(NGR_R_BAD);
176*9525b14bSRao Shoaib 	cp = p->buf;
1777c478bd9Sstevel@tonic-gate #else
1787c478bd9Sstevel@tonic-gate 	if (len > (int)buflen) {
1797c478bd9Sstevel@tonic-gate 		errno = ERANGE;
1807c478bd9Sstevel@tonic-gate 		return (NGR_R_BAD);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 	cp = buf;
1837c478bd9Sstevel@tonic-gate #endif
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (mp != NULL) {
1867c478bd9Sstevel@tonic-gate 		n = strlen(mp) + 1;
1877c478bd9Sstevel@tonic-gate 		strcpy(cp, mp);
1887c478bd9Sstevel@tonic-gate 		*machinep = cp;
1897c478bd9Sstevel@tonic-gate 		cp += n;
1907c478bd9Sstevel@tonic-gate 	} else
1917c478bd9Sstevel@tonic-gate 		*machinep = NULL;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (up != NULL) {
1947c478bd9Sstevel@tonic-gate 		n = strlen(up) + 1;
1957c478bd9Sstevel@tonic-gate 		strcpy(cp, up);
1967c478bd9Sstevel@tonic-gate 		*userp = cp;
1977c478bd9Sstevel@tonic-gate 		cp += n;
1987c478bd9Sstevel@tonic-gate 	} else
1997c478bd9Sstevel@tonic-gate 		*userp = NULL;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (dp != NULL) {
2027c478bd9Sstevel@tonic-gate 		n = strlen(dp) + 1;
2037c478bd9Sstevel@tonic-gate 		strcpy(cp, dp);
2047c478bd9Sstevel@tonic-gate 		*domainp = cp;
2057c478bd9Sstevel@tonic-gate 		cp += n;
2067c478bd9Sstevel@tonic-gate 	} else
2077c478bd9Sstevel@tonic-gate 		*domainp = NULL;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	return (NGR_R_OK);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate #else /* NGR_R_RETURN */
2127c478bd9Sstevel@tonic-gate 	static int getnetgrent_r_unknown_system = 0;
2137c478bd9Sstevel@tonic-gate #endif /* NGR_R_RETURN */
2147c478bd9Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
215*9525b14bSRao Shoaib /*! \file */
216