Update copyright notices with scripts/update-copyrights
[glibc.git] / nis / nss_nis / nis-ethers.c
bloba803c27ac6dd4810a4f947e47ed1ed2c1ed98d63
1 /* Copyright (C) 1996-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <nss.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <bits/libc-lock.h>
24 #include <rpcsvc/yp.h>
25 #include <rpcsvc/ypclnt.h>
26 #include <netinet/ether.h>
27 #include <netinet/if_ether.h>
29 #include "nss-nis.h"
31 /* Protect global state against multiple changers */
32 __libc_lock_define_initialized (static, lock)
34 /* Get the declaration of the parser function. */
35 #define ENTNAME etherent
36 #define STRUCTURE etherent
37 #define EXTERN_PARSER
38 #include <nss/nss_files/files-parse.c>
40 struct response
42 struct response *next;
43 char val[0];
46 static struct response *start;
47 static struct response *next;
49 static int
50 saveit (int instatus, char *inkey, int inkeylen, char *inval,
51 int invallen, char *indata)
53 if (instatus != YP_TRUE)
54 return 1;
56 if (inkey && inkeylen > 0 && inval && invallen > 0)
58 struct response *newp = malloc (sizeof (struct response) + invallen + 1);
59 if (newp == NULL)
60 return 1; /* We have no error code for out of memory */
62 if (start == NULL)
63 start = newp;
64 else
65 next->next = newp;
66 next = newp;
68 newp->next = NULL;
69 *((char *) mempcpy (newp->val, inval, invallen)) = '\0';
72 return 0;
75 static void
76 internal_nis_endetherent (void)
78 while (start != NULL)
80 next = start;
81 start = start->next;
82 free (next);
86 enum nss_status
87 _nss_nis_endetherent (void)
89 __libc_lock_lock (lock);
91 internal_nis_endetherent ();
92 next = NULL;
94 __libc_lock_unlock (lock);
96 return NSS_STATUS_SUCCESS;
99 static enum nss_status
100 internal_nis_setetherent (void)
102 char *domainname;
103 struct ypall_callback ypcb;
104 enum nss_status status;
106 yp_get_default_domain (&domainname);
108 internal_nis_endetherent ();
110 ypcb.foreach = saveit;
111 ypcb.data = NULL;
112 status = yperr2nss (yp_all (domainname, "ethers.byname", &ypcb));
113 next = start;
115 return status;
118 enum nss_status
119 _nss_nis_setetherent (int stayopen)
121 enum nss_status result;
123 __libc_lock_lock (lock);
125 result = internal_nis_setetherent ();
127 __libc_lock_unlock (lock);
129 return result;
132 static enum nss_status
133 internal_nis_getetherent_r (struct etherent *eth, char *buffer, size_t buflen,
134 int *errnop)
136 struct parser_data *data = (void *) buffer;
137 int parse_res;
139 if (start == NULL)
140 internal_nis_setetherent ();
142 /* Get the next entry until we found a correct one. */
145 char *p;
147 if (next == NULL)
148 return NSS_STATUS_NOTFOUND;
150 p = strncpy (buffer, next->val, buflen);
152 while (isspace (*p))
153 ++p;
155 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
156 if (parse_res == -1)
157 return NSS_STATUS_TRYAGAIN;
158 next = next->next;
160 while (!parse_res);
162 return NSS_STATUS_SUCCESS;
165 enum nss_status
166 _nss_nis_getetherent_r (struct etherent *result, char *buffer, size_t buflen,
167 int *errnop)
169 int status;
171 __libc_lock_lock (lock);
173 status = internal_nis_getetherent_r (result, buffer, buflen, errnop);
175 __libc_lock_unlock (lock);
177 return status;
180 enum nss_status
181 _nss_nis_gethostton_r (const char *name, struct etherent *eth,
182 char *buffer, size_t buflen, int *errnop)
184 if (name == NULL)
186 *errnop = EINVAL;
187 return NSS_STATUS_UNAVAIL;
190 char *domain;
191 if (__builtin_expect (yp_get_default_domain (&domain), 0))
192 return NSS_STATUS_UNAVAIL;
194 char *result;
195 int len;
196 int yperr = yp_match (domain, "ethers.byname", name, strlen (name), &result,
197 &len);
199 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
201 enum nss_status retval = yperr2nss (yperr);
203 if (retval == NSS_STATUS_TRYAGAIN)
204 *errnop = errno;
205 return retval;
208 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
210 free (result);
211 *errnop = ERANGE;
212 return NSS_STATUS_TRYAGAIN;
215 char *p = strncpy (buffer, result, len);
216 buffer[len] = '\0';
217 while (isspace (*p))
218 ++p;
219 free (result);
221 int parse_res = _nss_files_parse_etherent (p, eth, (void *) buffer, buflen,
222 errnop);
223 if (__builtin_expect (parse_res < 1, 0))
225 if (parse_res == -1)
226 return NSS_STATUS_TRYAGAIN;
227 else
228 return NSS_STATUS_NOTFOUND;
230 return NSS_STATUS_SUCCESS;
233 enum nss_status
234 _nss_nis_getntohost_r (const struct ether_addr *addr, struct etherent *eth,
235 char *buffer, size_t buflen, int *errnop)
237 if (addr == NULL)
239 *errnop = EINVAL;
240 return NSS_STATUS_UNAVAIL;
243 char *domain;
244 if (__builtin_expect (yp_get_default_domain (&domain), 0))
245 return NSS_STATUS_UNAVAIL;
247 char buf[33];
248 int nlen = snprintf (buf, sizeof (buf), "%x:%x:%x:%x:%x:%x",
249 (int) addr->ether_addr_octet[0],
250 (int) addr->ether_addr_octet[1],
251 (int) addr->ether_addr_octet[2],
252 (int) addr->ether_addr_octet[3],
253 (int) addr->ether_addr_octet[4],
254 (int) addr->ether_addr_octet[5]);
256 char *result;
257 int len;
258 int yperr = yp_match (domain, "ethers.byaddr", buf, nlen, &result, &len);
260 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
262 enum nss_status retval = yperr2nss (yperr);
264 if (retval == NSS_STATUS_TRYAGAIN)
265 *errnop = errno;
266 return retval;
269 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
271 free (result);
272 *errnop = ERANGE;
273 return NSS_STATUS_TRYAGAIN;
276 char *p = strncpy (buffer, result, len);
277 buffer[len] = '\0';
278 while (isspace (*p))
279 ++p;
280 free (result);
282 int parse_res = _nss_files_parse_etherent (p, eth, (void *) buffer, buflen,
283 errnop);
284 if (__builtin_expect (parse_res < 1, 0))
286 if (parse_res == -1)
287 return NSS_STATUS_TRYAGAIN;
288 else
289 return NSS_STATUS_NOTFOUND;
291 return NSS_STATUS_SUCCESS;