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