1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*
7*0Sstevel@tonic-gate  * Copyright (c) 1998-1999 by Internet Software Consortium.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
10*0Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
11*0Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
12*0Sstevel@tonic-gate  *
13*0Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14*0Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15*0Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16*0Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17*0Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18*0Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19*0Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20*0Sstevel@tonic-gate  * SOFTWARE.
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
26*0Sstevel@tonic-gate static const char rcsid[] = "$Id: getnetgrent_r.c,v 8.7 2003/04/29 05:51:14 marka Exp $";
27*0Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <port_before.h>
30*0Sstevel@tonic-gate #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
31*0Sstevel@tonic-gate 	static int getnetgrent_r_not_required = 0;
32*0Sstevel@tonic-gate #else
33*0Sstevel@tonic-gate #include <errno.h>
34*0Sstevel@tonic-gate #include <string.h>
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate #include <netinet/in.h>
38*0Sstevel@tonic-gate #include <netdb.h>
39*0Sstevel@tonic-gate #include <netgroup.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <port_after.h>
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #ifdef NGR_R_RETURN
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate static NGR_R_RETURN
46*0Sstevel@tonic-gate copy_protoent(char **, char **, char **, const char *, const char *,
47*0Sstevel@tonic-gate 	      const char *, NGR_R_COPY_ARGS);
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate NGR_R_RETURN
50*0Sstevel@tonic-gate innetgr_r(const char *netgroup, const char *host, const char *user,
51*0Sstevel@tonic-gate 	  const char *domain) {
52*0Sstevel@tonic-gate 	char *ng, *ho, *us, *dom;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	DE_CONST(netgroup, ng);
55*0Sstevel@tonic-gate 	DE_CONST(host, ho);
56*0Sstevel@tonic-gate 	DE_CONST(user, us);
57*0Sstevel@tonic-gate 	DE_CONST(domain, dom);
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 	return (innetgr(ng, ho, us, dom));
60*0Sstevel@tonic-gate }
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  *	These assume a single context is in operation per thread.
64*0Sstevel@tonic-gate  *	If this is not the case we will need to call irs directly
65*0Sstevel@tonic-gate  *	rather than through the base functions.
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate NGR_R_RETURN
69*0Sstevel@tonic-gate getnetgrent_r(char **machinep, char **userp, char **domainp, NGR_R_ARGS) {
70*0Sstevel@tonic-gate 	char *mp, *up, *dp;
71*0Sstevel@tonic-gate 	int res = getnetgrent(&mp, &up, &dp);
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	if (res != 1)
74*0Sstevel@tonic-gate 		return (res);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	return (copy_protoent(machinep, userp, domainp,
77*0Sstevel@tonic-gate 				mp, up, dp, NGR_R_COPY));
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate NGR_R_SET_RETURN
81*0Sstevel@tonic-gate #ifdef NGR_R_ENT_ARGS
82*0Sstevel@tonic-gate setnetgrent_r(const char *netgroup, NGR_R_ENT_ARGS)
83*0Sstevel@tonic-gate #else
84*0Sstevel@tonic-gate setnetgrent_r(const char *netgroup)
85*0Sstevel@tonic-gate #endif
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	setnetgrent(netgroup);
88*0Sstevel@tonic-gate #ifdef NGR_R_PRIVATE
89*0Sstevel@tonic-gate 	*buf = NULL;
90*0Sstevel@tonic-gate #endif
91*0Sstevel@tonic-gate #ifdef NGR_R_SET_RESULT
92*0Sstevel@tonic-gate 	return (NGR_R_SET_RESULT);
93*0Sstevel@tonic-gate #endif
94*0Sstevel@tonic-gate }
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate NGR_R_END_RETURN
97*0Sstevel@tonic-gate #ifdef NGR_R_ENT_ARGS
98*0Sstevel@tonic-gate endnetgrent_r(NGR_R_ENT_ARGS)
99*0Sstevel@tonic-gate #else
100*0Sstevel@tonic-gate endnetgrent_r(void)
101*0Sstevel@tonic-gate #endif
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	endnetgrent();
104*0Sstevel@tonic-gate #ifdef NGR_R_PRIVATE
105*0Sstevel@tonic-gate 	if (*buf != NULL)
106*0Sstevel@tonic-gate 		free(*buf);
107*0Sstevel@tonic-gate 	*buf = NULL;
108*0Sstevel@tonic-gate #endif
109*0Sstevel@tonic-gate 	NGR_R_END_RESULT(NGR_R_OK);
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate /* Private */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate static int
115*0Sstevel@tonic-gate copy_protoent(char **machinep, char **userp, char **domainp,
116*0Sstevel@tonic-gate 	      const char *mp, const char *up, const char *dp,
117*0Sstevel@tonic-gate 	      NGR_R_COPY_ARGS) {
118*0Sstevel@tonic-gate 	char *cp;
119*0Sstevel@tonic-gate 	int n;
120*0Sstevel@tonic-gate 	int len;
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 	/* Find out the amount of space required to store the answer. */
123*0Sstevel@tonic-gate 	len = 0;
124*0Sstevel@tonic-gate 	if (mp != NULL) len += strlen(mp) + 1;
125*0Sstevel@tonic-gate 	if (up != NULL) len += strlen(up) + 1;
126*0Sstevel@tonic-gate 	if (dp != NULL) len += strlen(dp) + 1;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate #ifdef NGR_R_PRIVATE
129*0Sstevel@tonic-gate 	free(*buf);
130*0Sstevel@tonic-gate 	*buf = malloc(len);
131*0Sstevel@tonic-gate 	if (*buf == NULL)
132*0Sstevel@tonic-gate 		return(NGR_R_BAD);
133*0Sstevel@tonic-gate 	cp = *buf;
134*0Sstevel@tonic-gate #else
135*0Sstevel@tonic-gate 	if (len > (int)buflen) {
136*0Sstevel@tonic-gate 		errno = ERANGE;
137*0Sstevel@tonic-gate 		return (NGR_R_BAD);
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 	cp = buf;
140*0Sstevel@tonic-gate #endif
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if (mp != NULL) {
144*0Sstevel@tonic-gate 		n = strlen(mp) + 1;
145*0Sstevel@tonic-gate 		strcpy(cp, mp);
146*0Sstevel@tonic-gate 		*machinep = cp;
147*0Sstevel@tonic-gate 		cp += n;
148*0Sstevel@tonic-gate 	} else
149*0Sstevel@tonic-gate 		*machinep = NULL;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if (up != NULL) {
152*0Sstevel@tonic-gate 		n = strlen(up) + 1;
153*0Sstevel@tonic-gate 		strcpy(cp, up);
154*0Sstevel@tonic-gate 		*userp = cp;
155*0Sstevel@tonic-gate 		cp += n;
156*0Sstevel@tonic-gate 	} else
157*0Sstevel@tonic-gate 		*userp = NULL;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	if (dp != NULL) {
160*0Sstevel@tonic-gate 		n = strlen(dp) + 1;
161*0Sstevel@tonic-gate 		strcpy(cp, dp);
162*0Sstevel@tonic-gate 		*domainp = cp;
163*0Sstevel@tonic-gate 		cp += n;
164*0Sstevel@tonic-gate 	} else
165*0Sstevel@tonic-gate 		*domainp = NULL;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	return (NGR_R_OK);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate #else /* NGR_R_RETURN */
170*0Sstevel@tonic-gate 	static int getnetgrent_r_unknown_system = 0;
171*0Sstevel@tonic-gate #endif /* NGR_R_RETURN */
172*0Sstevel@tonic-gate #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
173