1*5bbd2a12Schristos /* $NetBSD: getnetgrent_r.c,v 1.1.1.2 2012/09/09 16:07:57 christos Exp $ */
2b5677b36Schristos
3b5677b36Schristos /*
4b5677b36Schristos * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos * Copyright (C) 1998, 1999, 2001, 2003 Internet Software Consortium.
6b5677b36Schristos *
7b5677b36Schristos * Permission to use, copy, modify, and/or distribute this software for any
8b5677b36Schristos * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos *
11b5677b36Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12b5677b36Schristos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13b5677b36Schristos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14b5677b36Schristos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15b5677b36Schristos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16b5677b36Schristos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17b5677b36Schristos * PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos */
19b5677b36Schristos
20b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
21b5677b36Schristos static const char rcsid[] = "Id: getnetgrent_r.c,v 1.14 2008/11/14 02:36:51 marka Exp ";
22b5677b36Schristos #endif /* LIBC_SCCS and not lint */
23b5677b36Schristos
24b5677b36Schristos #include <port_before.h>
25b5677b36Schristos #if !defined(_REENTRANT) || !defined(DO_PTHREADS)
26b5677b36Schristos static int getnetgrent_r_not_required = 0;
27b5677b36Schristos #else
28b5677b36Schristos #include <errno.h>
29b5677b36Schristos #include <string.h>
30b5677b36Schristos #include <stdio.h>
31b5677b36Schristos #include <sys/types.h>
32b5677b36Schristos #include <netinet/in.h>
33b5677b36Schristos #include <netdb.h>
34b5677b36Schristos #include <stdlib.h>
35b5677b36Schristos #include <port_after.h>
36b5677b36Schristos
37b5677b36Schristos #ifdef NGR_R_RETURN
38b5677b36Schristos #ifndef NGR_R_PRIVATE
39b5677b36Schristos #define NGR_R_PRIVATE 0
40b5677b36Schristos #endif
41b5677b36Schristos
42b5677b36Schristos static NGR_R_RETURN
43b5677b36Schristos copy_protoent(NGR_R_CONST char **, NGR_R_CONST char **, NGR_R_CONST char **,
44b5677b36Schristos const char *, const char *, const char *, NGR_R_COPY_ARGS);
45b5677b36Schristos
46b5677b36Schristos NGR_R_RETURN
innetgr_r(const char * netgroup,const char * host,const char * user,const char * domain)47b5677b36Schristos innetgr_r(const char *netgroup, const char *host, const char *user,
48b5677b36Schristos const char *domain) {
49b5677b36Schristos char *ng, *ho, *us, *dom;
50b5677b36Schristos
51b5677b36Schristos DE_CONST(netgroup, ng);
52b5677b36Schristos DE_CONST(host, ho);
53b5677b36Schristos DE_CONST(user, us);
54b5677b36Schristos DE_CONST(domain, dom);
55b5677b36Schristos
56b5677b36Schristos return (innetgr(ng, ho, us, dom));
57b5677b36Schristos }
58b5677b36Schristos
59b5677b36Schristos /*%
60b5677b36Schristos * These assume a single context is in operation per thread.
61b5677b36Schristos * If this is not the case we will need to call irs directly
62b5677b36Schristos * rather than through the base functions.
63b5677b36Schristos */
64b5677b36Schristos
65b5677b36Schristos NGR_R_RETURN
getnetgrent_r(NGR_R_CONST char ** machinep,NGR_R_CONST char ** userp,NGR_R_CONST char ** domainp,NGR_R_ARGS)66b5677b36Schristos getnetgrent_r(NGR_R_CONST char **machinep, NGR_R_CONST char **userp,
67b5677b36Schristos NGR_R_CONST char **domainp, NGR_R_ARGS)
68b5677b36Schristos {
69b5677b36Schristos NGR_R_CONST char *mp, *up, *dp;
70b5677b36Schristos int res = getnetgrent(&mp, &up, &dp);
71b5677b36Schristos
72b5677b36Schristos if (res != 1)
73b5677b36Schristos return (res);
74b5677b36Schristos
75b5677b36Schristos return (copy_protoent(machinep, userp, domainp,
76b5677b36Schristos mp, up, dp, NGR_R_COPY));
77b5677b36Schristos }
78b5677b36Schristos
79b5677b36Schristos #if NGR_R_PRIVATE == 2
80b5677b36Schristos struct private {
81b5677b36Schristos char *buf;
82b5677b36Schristos };
83b5677b36Schristos
84b5677b36Schristos #endif
85b5677b36Schristos NGR_R_SET_RETURN
86b5677b36Schristos #ifdef NGR_R_SET_ARGS
setnetgrent_r(NGR_R_SET_CONST char * netgroup,NGR_R_SET_ARGS)87b5677b36Schristos setnetgrent_r(NGR_R_SET_CONST char *netgroup, NGR_R_SET_ARGS)
88b5677b36Schristos #else
89b5677b36Schristos setnetgrent_r(NGR_R_SET_CONST char *netgroup)
90b5677b36Schristos #endif
91b5677b36Schristos {
92b5677b36Schristos #if NGR_R_PRIVATE == 2
93b5677b36Schristos struct private *p;
94b5677b36Schristos #endif
95b5677b36Schristos char *tmp;
96b5677b36Schristos #if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0
97b5677b36Schristos UNUSED(buf);
98b5677b36Schristos UNUSED(buflen);
99b5677b36Schristos #endif
100b5677b36Schristos
101b5677b36Schristos DE_CONST(netgroup, tmp);
102b5677b36Schristos setnetgrent(tmp);
103b5677b36Schristos
104b5677b36Schristos #if NGR_R_PRIVATE == 1
105b5677b36Schristos *buf = NULL;
106b5677b36Schristos #elif NGR_R_PRIVATE == 2
107b5677b36Schristos *buf = p = malloc(sizeof(struct private));
108b5677b36Schristos if (p == NULL)
109b5677b36Schristos #ifdef NGR_R_SET_RESULT
110b5677b36Schristos return (NGR_R_BAD);
111b5677b36Schristos #else
112b5677b36Schristos return;
113b5677b36Schristos #endif
114b5677b36Schristos p->buf = NULL;
115b5677b36Schristos #endif
116b5677b36Schristos #ifdef NGR_R_SET_RESULT
117b5677b36Schristos return (NGR_R_SET_RESULT);
118b5677b36Schristos #endif
119b5677b36Schristos }
120b5677b36Schristos
121b5677b36Schristos NGR_R_END_RETURN
122b5677b36Schristos #ifdef NGR_R_END_ARGS
endnetgrent_r(NGR_R_END_ARGS)123b5677b36Schristos endnetgrent_r(NGR_R_END_ARGS)
124b5677b36Schristos #else
125b5677b36Schristos endnetgrent_r(void)
126b5677b36Schristos #endif
127b5677b36Schristos {
128b5677b36Schristos #if NGR_R_PRIVATE == 2
129b5677b36Schristos struct private *p = buf;
130b5677b36Schristos #endif
131b5677b36Schristos #if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0
132b5677b36Schristos UNUSED(buf);
133b5677b36Schristos UNUSED(buflen);
134b5677b36Schristos #endif
135b5677b36Schristos
136b5677b36Schristos endnetgrent();
137b5677b36Schristos #if NGR_R_PRIVATE == 1
138b5677b36Schristos if (*buf != NULL)
139b5677b36Schristos free(*buf);
140b5677b36Schristos *buf = NULL;
141b5677b36Schristos #elif NGR_R_PRIVATE == 2
142b5677b36Schristos if (p->buf != NULL)
143b5677b36Schristos free(p->buf);
144b5677b36Schristos free(p);
145b5677b36Schristos #endif
146b5677b36Schristos NGR_R_END_RESULT(NGR_R_OK);
147b5677b36Schristos }
148b5677b36Schristos
149b5677b36Schristos /* Private */
150b5677b36Schristos
151b5677b36Schristos 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)152b5677b36Schristos copy_protoent(NGR_R_CONST char **machinep, NGR_R_CONST char **userp,
153b5677b36Schristos NGR_R_CONST char **domainp, const char *mp, const char *up,
154b5677b36Schristos const char *dp, NGR_R_COPY_ARGS)
155b5677b36Schristos {
156b5677b36Schristos #if NGR_R_PRIVATE == 2
157b5677b36Schristos struct private *p = buf;
158b5677b36Schristos #endif
159b5677b36Schristos char *cp;
160b5677b36Schristos int n;
161b5677b36Schristos int len;
162b5677b36Schristos
163b5677b36Schristos /* Find out the amount of space required to store the answer. */
164b5677b36Schristos len = 0;
165b5677b36Schristos if (mp != NULL) len += strlen(mp) + 1;
166b5677b36Schristos if (up != NULL) len += strlen(up) + 1;
167b5677b36Schristos if (dp != NULL) len += strlen(dp) + 1;
168b5677b36Schristos
169b5677b36Schristos #if NGR_R_PRIVATE == 1
170b5677b36Schristos if (*buf != NULL)
171b5677b36Schristos free(*buf);
172b5677b36Schristos *buf = malloc(len);
173b5677b36Schristos if (*buf == NULL)
174b5677b36Schristos return(NGR_R_BAD);
175b5677b36Schristos cp = *buf;
176b5677b36Schristos #elif NGR_R_PRIVATE == 2
177b5677b36Schristos if (p->buf)
178b5677b36Schristos free(p->buf);
179b5677b36Schristos p->buf = malloc(len);
180b5677b36Schristos if (p->buf == NULL)
181b5677b36Schristos return(NGR_R_BAD);
182b5677b36Schristos cp = p->buf;
183b5677b36Schristos #else
184b5677b36Schristos if (len > (int)buflen) {
185b5677b36Schristos errno = ERANGE;
186b5677b36Schristos return (NGR_R_BAD);
187b5677b36Schristos }
188b5677b36Schristos cp = buf;
189b5677b36Schristos #endif
190b5677b36Schristos
191b5677b36Schristos if (mp != NULL) {
192b5677b36Schristos n = strlen(mp) + 1;
193b5677b36Schristos strcpy(cp, mp);
194b5677b36Schristos *machinep = cp;
195b5677b36Schristos cp += n;
196b5677b36Schristos } else
197b5677b36Schristos *machinep = NULL;
198b5677b36Schristos
199b5677b36Schristos if (up != NULL) {
200b5677b36Schristos n = strlen(up) + 1;
201b5677b36Schristos strcpy(cp, up);
202b5677b36Schristos *userp = cp;
203b5677b36Schristos cp += n;
204b5677b36Schristos } else
205b5677b36Schristos *userp = NULL;
206b5677b36Schristos
207b5677b36Schristos if (dp != NULL) {
208b5677b36Schristos n = strlen(dp) + 1;
209b5677b36Schristos strcpy(cp, dp);
210b5677b36Schristos *domainp = cp;
211b5677b36Schristos cp += n;
212b5677b36Schristos } else
213b5677b36Schristos *domainp = NULL;
214b5677b36Schristos
215b5677b36Schristos return (NGR_R_OK);
216b5677b36Schristos }
217b5677b36Schristos #else /* NGR_R_RETURN */
218b5677b36Schristos static int getnetgrent_r_unknown_system = 0;
219b5677b36Schristos #endif /* NGR_R_RETURN */
220b5677b36Schristos #endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
221b5677b36Schristos /*! \file */
222