* sysdeps/m68k/bits/byteswap.h (__bswap_32): Add cast to avoid
[glibc/pb-stable.git] / nis / nss_nis / nis-proto.c
blob33286df53ac88944fee1e30157b6628b98a1d31c
1 /* Copyright (C) 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
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.
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.
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. */
20 #include <nss.h>
21 #include <netdb.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
29 #include "nss-nis.h"
31 /* Get the declaration of the parser function. */
32 #define ENTNAME protoent
33 #define EXTERN_PARSER
34 #include <nss/nss_files/files-parse.c>
36 __libc_lock_define_initialized (static, lock)
38 struct response
40 char *val;
41 struct response *next;
44 static struct response *start = NULL;
45 static struct response *next = NULL;
47 static int
48 saveit (int instatus, char *inkey, int inkeylen, char *inval,
49 int invallen, char *indata)
51 if (instatus != YP_TRUE)
52 return instatus;
54 if (inkey && inkeylen > 0 && inval && invallen > 0)
56 if (start == NULL)
58 start = malloc (sizeof (struct response));
59 if (start == NULL)
60 return YP_FALSE;
61 next = start;
63 else
65 next->next = malloc (sizeof (struct response));
66 if (next->next == NULL)
67 return YP_FALSE;
68 next = next->next;
70 next->next = NULL;
71 next->val = malloc (invallen + 1);
72 if (next->val == NULL)
73 return YP_FALSE;
74 strncpy (next->val, inval, invallen);
75 next->val[invallen] = '\0';
78 return 0;
81 static enum nss_status
82 internal_nis_setprotoent (void)
84 char *domainname;
85 struct ypall_callback ypcb;
86 enum nss_status status;
88 yp_get_default_domain (&domainname);
90 while (start != NULL)
92 if (start->val != NULL)
93 free (start->val);
94 next = start;
95 start = start->next;
96 free (next);
98 start = NULL;
100 ypcb.foreach = saveit;
101 ypcb.data = NULL;
102 status = yperr2nss (yp_all (domainname, "protocols.bynumber", &ypcb));
103 next = start;
105 return status;
108 enum nss_status
109 _nss_nis_setprotoent (int stayopen)
111 enum nss_status status;
113 __libc_lock_lock (lock);
115 status = internal_nis_setprotoent ();
117 __libc_lock_unlock (lock);
119 return status;
122 enum nss_status
123 _nss_nis_endprotoent (void)
125 __libc_lock_lock (lock);
127 while (start != NULL)
129 if (start->val != NULL)
130 free (start->val);
131 next = start;
132 start = start->next;
133 free (next);
135 start = NULL;
136 next = NULL;
138 __libc_lock_unlock (lock);
140 return NSS_STATUS_SUCCESS;
143 static enum nss_status
144 internal_nis_getprotoent_r (struct protoent *proto,
145 char *buffer, size_t buflen, int *errnop)
147 struct parser_data *data = (void *) buffer;
148 int parse_res;
150 if (start == NULL)
151 internal_nis_setprotoent ();
153 /* Get the next entry until we found a correct one. */
156 char *p;
158 if (next == NULL)
160 *errnop = ENOENT;
161 return NSS_STATUS_NOTFOUND;
163 p = strncpy (buffer, next->val, buflen);
165 while (isspace (*p))
166 ++p;
168 parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
169 if (parse_res == -1)
170 return NSS_STATUS_TRYAGAIN;
171 next = next->next;
173 while (!parse_res);
175 return NSS_STATUS_SUCCESS;
178 enum nss_status
179 _nss_nis_getprotoent_r (struct protoent *proto, char *buffer, size_t buflen,
180 int *errnop)
182 enum nss_status status;
184 __libc_lock_lock (lock);
186 status = internal_nis_getprotoent_r (proto, buffer, buflen, errnop);
188 __libc_lock_unlock (lock);
190 return status;
193 enum nss_status
194 _nss_nis_getprotobyname_r (const char *name, struct protoent *proto,
195 char *buffer, size_t buflen, int *errnop)
197 struct parser_data *data = (void *) buffer;
198 enum nss_status retval;
199 char *domain, *result, *p;
200 int len, parse_res;
202 if (name == NULL)
204 *errnop = EINVAL;
205 return NSS_STATUS_UNAVAIL;
208 if (yp_get_default_domain (&domain))
209 return NSS_STATUS_UNAVAIL;
211 retval = yperr2nss (yp_match (domain, "protocols.byname", name,
212 strlen (name), &result, &len));
214 if (retval != NSS_STATUS_SUCCESS)
216 if (retval == NSS_STATUS_NOTFOUND)
217 *errnop = ENOENT;
218 else if (retval == NSS_STATUS_TRYAGAIN)
219 *errnop = errno;
220 return retval;
223 if ((size_t) (len + 1) > buflen)
225 free (result);
226 *errnop = ERANGE;
227 return NSS_STATUS_TRYAGAIN;
230 p = strncpy (buffer, result, len);
231 buffer[len] = '\0';
232 while (isspace (*p))
233 ++p;
234 free (result);
236 parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
237 if (parse_res < 1)
239 if (parse_res == -1)
240 return NSS_STATUS_TRYAGAIN;
241 else
243 *errnop = ENOENT;
244 return NSS_STATUS_NOTFOUND;
247 return NSS_STATUS_SUCCESS;
250 enum nss_status
251 _nss_nis_getprotobynumber_r (int number, struct protoent *proto,
252 char *buffer, size_t buflen, int *errnop)
254 struct parser_data *data = (void *) buffer;
255 enum nss_status retval;
256 char *domain, *result, *p;
257 int len, nlen, parse_res;
258 char buf[32];
260 if (yp_get_default_domain (&domain))
261 return NSS_STATUS_UNAVAIL;
263 nlen = sprintf (buf, "%d", number);
265 retval = yperr2nss (yp_match (domain, "protocols.bynumber", buf,
266 nlen, &result, &len));
268 if (retval != NSS_STATUS_SUCCESS)
270 if (retval == NSS_STATUS_NOTFOUND)
271 *errnop = ENOENT;
272 else if (retval == NSS_STATUS_TRYAGAIN)
273 *errnop = errno;
274 return retval;
277 if ((size_t) (len + 1) > buflen)
279 free (result);
280 *errnop = ERANGE;
281 return NSS_STATUS_TRYAGAIN;
284 p = strncpy (buffer, result, len);
285 buffer[len] = '\0';
286 while (isspace (*p))
287 ++p;
288 free (result);
290 parse_res = _nss_files_parse_protoent (p, proto, data, buflen, errnop);
291 if (parse_res < 1)
293 if (parse_res == -1)
294 return NSS_STATUS_TRYAGAIN;
295 else
297 *errnop = ENOENT;
298 return NSS_STATUS_NOTFOUND;
301 return NSS_STATUS_SUCCESS;