Handle DNS timeouts in old-style lookkup code.
[glibc.git] / nis / nss_nis / nis-ethers.c
bloba3064282ab36ae6788efa9982828f4ca75eae150
1 /* Copyright (C) 1996-2003, 2004, 2006 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <nss.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
27 #include <netinet/ether.h>
28 #include <netinet/if_ether.h>
30 #include "nss-nis.h"
32 /* Protect global state against multiple changers */
33 __libc_lock_define_initialized (static, lock)
35 /* Get the declaration of the parser function. */
36 #define ENTNAME etherent
37 #define STRUCTURE etherent
38 #define EXTERN_PARSER
39 #include <nss/nss_files/files-parse.c>
41 struct response
43 struct response *next;
44 char val[0];
47 static struct response *start;
48 static struct response *next;
50 static int
51 saveit (int instatus, char *inkey, int inkeylen, char *inval,
52 int invallen, char *indata)
54 if (instatus != YP_TRUE)
55 return 1;
57 if (inkey && inkeylen > 0 && inval && invallen > 0)
59 struct response *newp = malloc (sizeof (struct response) + invallen + 1);
60 if (newp == NULL)
61 return 1; /* We have no error code for out of memory */
63 if (start == NULL)
64 start = newp;
65 else
66 next->next = newp;
67 next = newp;
69 newp->next = NULL;
70 *((char *) mempcpy (newp->val, inval, invallen)) = '\0';
73 return 0;
76 static void
77 internal_nis_endetherent (void)
79 while (start != NULL)
81 next = start;
82 start = start->next;
83 free (next);
87 enum nss_status
88 _nss_nis_endetherent (void)
90 __libc_lock_lock (lock);
92 internal_nis_endetherent ();
93 next = NULL;
95 __libc_lock_unlock (lock);
97 return NSS_STATUS_SUCCESS;
100 static enum nss_status
101 internal_nis_setetherent (void)
103 char *domainname;
104 struct ypall_callback ypcb;
105 enum nss_status status;
107 yp_get_default_domain (&domainname);
109 internal_nis_endetherent ();
111 ypcb.foreach = saveit;
112 ypcb.data = NULL;
113 status = yperr2nss (yp_all (domainname, "ethers.byname", &ypcb));
114 next = start;
116 return status;
119 enum nss_status
120 _nss_nis_setetherent (int stayopen)
122 enum nss_status result;
124 __libc_lock_lock (lock);
126 result = internal_nis_setetherent ();
128 __libc_lock_unlock (lock);
130 return result;
133 static enum nss_status
134 internal_nis_getetherent_r (struct etherent *eth, char *buffer, size_t buflen,
135 int *errnop)
137 struct parser_data *data = (void *) buffer;
138 int parse_res;
140 if (start == NULL)
141 internal_nis_setetherent ();
143 /* Get the next entry until we found a correct one. */
146 char *p;
148 if (next == NULL)
149 return NSS_STATUS_NOTFOUND;
151 p = strncpy (buffer, next->val, buflen);
153 while (isspace (*p))
154 ++p;
156 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
157 if (parse_res == -1)
158 return NSS_STATUS_TRYAGAIN;
159 next = next->next;
161 while (!parse_res);
163 return NSS_STATUS_SUCCESS;
166 enum nss_status
167 _nss_nis_getetherent_r (struct etherent *result, char *buffer, size_t buflen,
168 int *errnop)
170 int status;
172 __libc_lock_lock (lock);
174 status = internal_nis_getetherent_r (result, buffer, buflen, errnop);
176 __libc_lock_unlock (lock);
178 return status;
181 enum nss_status
182 _nss_nis_gethostton_r (const char *name, struct etherent *eth,
183 char *buffer, size_t buflen, int *errnop)
185 if (name == NULL)
187 *errnop = EINVAL;
188 return NSS_STATUS_UNAVAIL;
191 char *domain;
192 if (__builtin_expect (yp_get_default_domain (&domain), 0))
193 return NSS_STATUS_UNAVAIL;
195 char *result;
196 int len;
197 int yperr = yp_match (domain, "ethers.byname", name, strlen (name), &result,
198 &len);
200 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
202 enum nss_status retval = yperr2nss (yperr);
204 if (retval == NSS_STATUS_TRYAGAIN)
205 *errnop = errno;
206 return retval;
209 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
211 free (result);
212 *errnop = ERANGE;
213 return NSS_STATUS_TRYAGAIN;
216 char *p = strncpy (buffer, result, len);
217 buffer[len] = '\0';
218 while (isspace (*p))
219 ++p;
220 free (result);
222 int parse_res = _nss_files_parse_etherent (p, eth, (void *) buffer, buflen,
223 errnop);
224 if (__builtin_expect (parse_res < 1, 0))
226 if (parse_res == -1)
227 return NSS_STATUS_TRYAGAIN;
228 else
229 return NSS_STATUS_NOTFOUND;
231 return NSS_STATUS_SUCCESS;
234 enum nss_status
235 _nss_nis_getntohost_r (const struct ether_addr *addr, struct etherent *eth,
236 char *buffer, size_t buflen, int *errnop)
238 if (addr == NULL)
240 *errnop = EINVAL;
241 return NSS_STATUS_UNAVAIL;
244 char *domain;
245 if (__builtin_expect (yp_get_default_domain (&domain), 0))
246 return NSS_STATUS_UNAVAIL;
248 char buf[33];
249 int nlen = snprintf (buf, sizeof (buf), "%x:%x:%x:%x:%x:%x",
250 (int) addr->ether_addr_octet[0],
251 (int) addr->ether_addr_octet[1],
252 (int) addr->ether_addr_octet[2],
253 (int) addr->ether_addr_octet[3],
254 (int) addr->ether_addr_octet[4],
255 (int) addr->ether_addr_octet[5]);
257 char *result;
258 int len;
259 int yperr = yp_match (domain, "ethers.byaddr", buf, nlen, &result, &len);
261 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
263 enum nss_status retval = yperr2nss (yperr);
265 if (retval == NSS_STATUS_TRYAGAIN)
266 *errnop = errno;
267 return retval;
270 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
272 free (result);
273 *errnop = ERANGE;
274 return NSS_STATUS_TRYAGAIN;
277 char *p = strncpy (buffer, result, len);
278 buffer[len] = '\0';
279 while (isspace (*p))
280 ++p;
281 free (result);
283 int parse_res = _nss_files_parse_etherent (p, eth, (void *) buffer, buflen,
284 errnop);
285 if (__builtin_expect (parse_res < 1, 0))
287 if (parse_res == -1)
288 return NSS_STATUS_TRYAGAIN;
289 else
290 return NSS_STATUS_NOTFOUND;
292 return NSS_STATUS_SUCCESS;