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