]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nis/nis-proto.c
Update.
[thirdparty/glibc.git] / nis / nss_nis / nis-proto.c
CommitLineData
f166d865 1/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
6259ec0d
UD
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20#include <nss.h>
21#include <netdb.h>
22#include <ctype.h>
23#include <errno.h>
24#include <string.h>
25#include <libc-lock.h>
26#include <rpcsvc/yp.h>
27#include <rpcsvc/ypclnt.h>
28
29#include "nss-nis.h"
30
7e3be507
UD
31/* Get the declaration of the parser function. */
32#define ENTNAME protoent
33#define EXTERN_PARSER
34#include "../nss/nss_files/files-parse.c"
35
6259ec0d
UD
36__libc_lock_define_initialized (static, lock)
37
f166d865
UD
38struct response
39{
40 char *val;
41 struct response *next;
42};
43
44static struct response *start = NULL;
45static struct response *next = NULL;
46
47static int
48saveit (int instatus, char *inkey, int inkeylen, char *inval,
49 int invallen, char *indata)
50{
51 if (instatus != YP_TRUE)
52 return instatus;
53
54 if (inkey && inkeylen > 0 && inval && invallen > 0)
55 {
56 if (start == NULL)
57 {
58 start = malloc (sizeof (struct response));
59 next = start;
60 }
61 else
62 {
63 next->next = malloc (sizeof (struct response));
64 next = next->next;
65 }
66 next->next = NULL;
67 next->val = malloc (invallen + 1);
68 strncpy (next->val, inval, invallen);
69 next->val[invallen] = '\0';
70 }
71
72 return 0;
73}
74
75enum nss_status
76internal_nis_setprotoent (void)
77{
78 char *domainname;
79 struct ypall_callback ypcb;
80
81 yp_get_default_domain (&domainname);
82
83 while (start != NULL)
84 {
85 if (start->val != NULL)
86 free (start->val);
87 next = start;
88 start = start->next;
89 free (next);
90 }
91 start = NULL;
92
93 ypcb.foreach = saveit;
94 ypcb.data = NULL;
95 yp_all (domainname, "protocols.bynumber", &ypcb);
96 next = start;
97
98 return NSS_STATUS_SUCCESS;
99}
6259ec0d
UD
100
101enum nss_status
102_nss_nis_setprotoent (void)
103{
f166d865
UD
104 enum nss_status status;
105
6259ec0d
UD
106 __libc_lock_lock (lock);
107
f166d865 108 status = internal_nis_setprotoent ();
6259ec0d
UD
109
110 __libc_lock_unlock (lock);
111
f166d865 112 return status;
6259ec0d
UD
113}
114
115enum nss_status
116_nss_nis_endprotoent (void)
117{
118 __libc_lock_lock (lock);
119
f166d865 120 while (start != NULL)
6259ec0d 121 {
f166d865
UD
122 if (start->val != NULL)
123 free (start->val);
124 next = start;
125 start = start->next;
126 free (next);
6259ec0d 127 }
f166d865
UD
128 start = NULL;
129 next = NULL;
130
6259ec0d 131 __libc_lock_unlock (lock);
f166d865 132
6259ec0d
UD
133 return NSS_STATUS_SUCCESS;
134}
135
136static enum nss_status
137internal_nis_getprotoent_r (struct protoent *proto,
138 char *buffer, size_t buflen)
139{
7e3be507 140 struct parser_data *data = (void *) buffer;
f166d865 141 int parse_res;
6259ec0d 142
f166d865
UD
143 if (start == NULL)
144 internal_nis_setprotoent ();
6259ec0d
UD
145
146 /* Get the next entry until we found a correct one. */
147 do
148 {
6259ec0d 149 char *p;
f166d865
UD
150
151 if (next == NULL)
152 return NSS_STATUS_NOTFOUND;
153 p = strcpy (buffer, next->val);
154 next = next->next;
155
6259ec0d
UD
156 while (isspace (*p))
157 ++p;
6259ec0d 158
7e3be507 159 parse_res = _nss_files_parse_protoent (p, proto, data, buflen);
6259ec0d
UD
160 if (!parse_res && errno == ERANGE)
161 return NSS_STATUS_TRYAGAIN;
6259ec0d
UD
162 }
163 while (!parse_res);
f166d865 164
6259ec0d
UD
165 return NSS_STATUS_SUCCESS;
166}
167
168enum nss_status
169_nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen)
170{
171 enum nss_status status;
172
173 __libc_lock_lock (lock);
174
175 status = internal_nis_getprotoent_r (proto, buffer, buflen);
176
177 __libc_lock_unlock (lock);
178
179 return status;
180}
181
182enum nss_status
183_nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
184 char *buffer, size_t buflen)
185{
7e3be507 186 struct parser_data *data = (void *) buffer;
6259ec0d
UD
187 enum nss_status retval;
188 char *domain, *result, *p;
189 int len, parse_res;
190
191 if (name == NULL)
192 {
193 __set_errno (EINVAL);
194 return NSS_STATUS_UNAVAIL;
195 }
196
197 if (yp_get_default_domain (&domain))
198 return NSS_STATUS_UNAVAIL;
199
200 retval = yperr2nss (yp_match (domain, "protocols.byname", name,
201 strlen (name), &result, &len));
202
203 if (retval != NSS_STATUS_SUCCESS)
204 {
205 if (retval == NSS_STATUS_TRYAGAIN)
206 __set_errno (EAGAIN);
207 return retval;
208 }
209
f8b87ef0 210 if ((size_t) (len + 1) > buflen)
6259ec0d
UD
211 {
212 free (result);
213 __set_errno (ERANGE);
214 return NSS_STATUS_TRYAGAIN;
215 }
216
217 p = strncpy (buffer, result, len);
218 buffer[len] = '\0';
219 while (isspace (*p))
220 ++p;
221 free (result);
222
7e3be507 223 parse_res = _nss_files_parse_protoent (p, proto, data, buflen);
6259ec0d
UD
224
225 if (!parse_res)
226 {
227 if (errno == ERANGE)
228 return NSS_STATUS_TRYAGAIN;
229 else
230 return NSS_STATUS_NOTFOUND;
231 }
232 else
233 return NSS_STATUS_SUCCESS;
234}
235
236enum nss_status
237_nss_nis_getprotobynumber_r (int number, struct protoent *proto,
238 char *buffer, size_t buflen)
239{
7e3be507 240 struct parser_data *data = (void *) buffer;
6259ec0d
UD
241 enum nss_status retval;
242 char *domain, *result, *p;
243 int len, nlen, parse_res;
244 char buf[32];
245
246 if (yp_get_default_domain (&domain))
247 return NSS_STATUS_UNAVAIL;
248
249 nlen = sprintf (buf, "%d", number);
250
251 retval = yperr2nss (yp_match (domain, "protocols.bynumber", buf,
252 nlen, &result, &len));
253
254 if (retval != NSS_STATUS_SUCCESS)
255 {
256 if (retval == NSS_STATUS_TRYAGAIN)
257 __set_errno (EAGAIN);
258 return retval;
259 }
260
f8b87ef0 261 if ((size_t) (len + 1) > buflen)
6259ec0d
UD
262 {
263 free (result);
264 __set_errno (ERANGE);
265 return NSS_STATUS_TRYAGAIN;
266 }
267
268 p = strncpy (buffer, result, len);
269 buffer[len] = '\0';
270 while (isspace (*p))
271 ++p;
272 free (result);
273
7e3be507 274 parse_res = _nss_files_parse_protoent (p, proto, data, buflen);
6259ec0d
UD
275
276 if (!parse_res)
277 {
278 if (errno == ERANGE)
279 return NSS_STATUS_TRYAGAIN;
280 else
281 return NSS_STATUS_NOTFOUND;
282 }
283 else
284 return NSS_STATUS_SUCCESS;
285}