Update.
[glibc.git] / nis / nss_nis / nis-ethers.c
blobc95f766469e354a173ef9b80072a10e51c143abe
1 /* Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 <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/if_ether.h>
29 #include "nss-nis.h"
31 /* Protect global state against multiple changers */
32 __libc_lock_define_initialized (static, lock)
34 struct ether
36 const char *e_name;
37 struct ether_addr e_addr;
40 /* Get the declaration of the parser function. */
41 #define ENTNAME etherent
42 #define STRUCTURE ether
43 #define EXTERN_PARSER
44 #include <nss/nss_files/files-parse.c>
46 struct response
48 char *val;
49 struct response *next;
52 static struct response *start = NULL;
53 static struct response *next = NULL;
55 static int
56 saveit (int instatus, char *inkey, int inkeylen, char *inval,
57 int invallen, char *indata)
59 if (instatus != YP_TRUE)
60 return instatus;
62 if (inkey && inkeylen > 0 && inval && invallen > 0)
64 if (start == NULL)
66 start = malloc (sizeof (struct response));
67 next = start;
69 else
71 next->next = malloc (sizeof (struct response));
72 next = next->next;
74 next->next = NULL;
75 next->val = malloc (invallen + 1);
76 strncpy (next->val, inval, invallen);
77 next->val[invallen] = '\0';
80 return 0;
83 static enum nss_status
84 internal_nis_setetherent (void)
86 char *domainname;
87 struct ypall_callback ypcb;
88 enum nss_status status;
90 yp_get_default_domain (&domainname);
92 while (start != NULL)
94 if (start->val != NULL)
95 free (start->val);
96 next = start;
97 start = start->next;
98 free (next);
100 start = NULL;
102 ypcb.foreach = saveit;
103 ypcb.data = NULL;
104 status = yperr2nss (yp_all (domainname, "ethers.byname", &ypcb));
105 next = start;
107 return status;
110 enum nss_status
111 _nss_nis_setetherent (void)
113 enum nss_status result;
115 __libc_lock_lock (lock);
117 result = internal_nis_setetherent ();
119 __libc_lock_unlock (lock);
121 return result;
124 enum nss_status
125 _nss_nis_endetherent (void)
127 __libc_lock_lock (lock);
129 while (start != NULL)
131 if (start->val != NULL)
132 free (start->val);
133 next = start;
134 start = start->next;
135 free (next);
137 start = NULL;
138 next = NULL;
140 __libc_lock_unlock (lock);
142 return NSS_STATUS_SUCCESS;
145 static enum nss_status
146 internal_nis_getetherent_r (struct ether *eth, char *buffer, size_t buflen,
147 int *errnop)
149 struct parser_data *data = (void *) buffer;
150 int parse_res;
152 if (start == NULL)
153 internal_nis_setetherent ();
155 /* Get the next entry until we found a correct one. */
158 char *p;
160 if (next == NULL)
162 *errnop = ENOENT;
163 return NSS_STATUS_NOTFOUND;
165 p = strncpy (buffer, next->val, buflen);
167 while (isspace (*p))
168 ++p;
170 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
171 if (parse_res == -1)
172 return NSS_STATUS_TRYAGAIN;
173 next = next->next;
175 while (!parse_res);
177 return NSS_STATUS_SUCCESS;
180 enum nss_status
181 _nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen,
182 int *errnop)
184 int status;
186 __libc_lock_lock (lock);
188 status = internal_nis_getetherent_r (result, buffer, buflen, errnop);
190 __libc_lock_unlock (lock);
192 return status;
195 enum nss_status
196 _nss_nis_gethostton_r (const char *name, struct ether *eth,
197 char *buffer, size_t buflen, int *errnop)
199 struct parser_data *data = (void *) buffer;
200 enum nss_status retval;
201 char *domain, *result, *p;
202 int len, parse_res;
204 if (name == NULL)
206 *errnop = EINVAL;
207 return NSS_STATUS_UNAVAIL;
210 if (yp_get_default_domain (&domain))
211 return NSS_STATUS_UNAVAIL;
213 retval = yperr2nss (yp_match (domain, "ethers.byname", name,
214 strlen (name), &result, &len));
216 if (retval != NSS_STATUS_SUCCESS)
218 if (retval == NSS_STATUS_NOTFOUND)
219 *errnop = ENOENT;
220 else if (retval == NSS_STATUS_TRYAGAIN)
221 *errnop = errno;
222 return retval;
225 if ((size_t) (len + 1) > buflen)
227 free (result);
228 *errnop = ERANGE;
229 return NSS_STATUS_TRYAGAIN;
232 p = strncpy (buffer, result, len);
233 buffer[len] = '\0';
234 while (isspace (*p))
235 ++p;
236 free (result);
238 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
239 if (parse_res < 1)
241 if (parse_res == -1)
242 return NSS_STATUS_TRYAGAIN;
243 else
244 return NSS_STATUS_NOTFOUND;
246 return NSS_STATUS_SUCCESS;
249 enum nss_status
250 _nss_nis_getntohost_r (struct ether_addr *addr, struct ether *eth,
251 char *buffer, size_t buflen, int *errnop)
253 struct parser_data *data = (void *) buffer;
254 enum nss_status retval;
255 char *domain, *result, *p;
256 int len, nlen, parse_res;
257 char buf[33];
259 if (addr == NULL)
261 *errnop = EINVAL;
262 return NSS_STATUS_UNAVAIL;
265 if (yp_get_default_domain (&domain))
266 return NSS_STATUS_UNAVAIL;
268 nlen = sprintf (buf, "%x:%x:%x:%x:%x:%x",
269 (int) addr->ether_addr_octet[0],
270 (int) addr->ether_addr_octet[1],
271 (int) addr->ether_addr_octet[2],
272 (int) addr->ether_addr_octet[3],
273 (int) addr->ether_addr_octet[4],
274 (int) addr->ether_addr_octet[5]);
276 retval = yperr2nss (yp_match (domain, "ethers.byaddr", buf,
277 nlen, &result, &len));
279 if (retval != NSS_STATUS_SUCCESS)
281 if (retval == NSS_STATUS_TRYAGAIN)
282 *errnop = errno;
283 return retval;
286 if ((size_t) (len + 1) > buflen)
288 free (result);
289 *errnop = ERANGE;
290 return NSS_STATUS_TRYAGAIN;
293 p = strncpy (buffer, result, len);
294 buffer[len] = '\0';
295 while (isspace (*p))
296 ++p;
297 free (result);
299 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
300 if (parse_res < 1)
302 if (parse_res == -1)
303 return NSS_STATUS_TRYAGAIN;
304 else
306 *errnop = ENOENT;
307 return NSS_STATUS_NOTFOUND;
310 return NSS_STATUS_SUCCESS;