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