Updated to fedora-glibc-20050428T0846
[glibc.git] / glibc-compat / nss_nis / nis-ethers.c
blob54b99dcba9c27d2688cc0944f86b9ec4d845972f
1 /* Copyright (C) 1996, 1997 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_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 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)
148 struct parser_data *data = (void *) buffer;
149 int parse_res;
151 if (start == NULL)
152 internal_nis_setetherent ();
154 /* Get the next entry until we found a correct one. */
157 char *p;
159 if (next == NULL)
160 return NSS_STATUS_NOTFOUND;
161 p = strncpy (buffer, next->val, buflen);
162 next = next->next;
164 while (isspace (*p))
165 ++p;
167 parse_res = _nss_files_parse_etherent (p, eth, data, buflen);
168 if (parse_res == -1 && errno == ERANGE)
169 return NSS_STATUS_TRYAGAIN;
171 while (!parse_res);
173 return NSS_STATUS_SUCCESS;
176 enum nss_status
177 _nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen)
179 int status;
181 __libc_lock_lock (lock);
183 status = internal_nis_getetherent_r (result, buffer, buflen);
185 __libc_lock_unlock (lock);
187 return status;
190 enum nss_status
191 _nss_nis_gethostton_r (const char *name, struct ether *eth,
192 char *buffer, size_t buflen)
194 struct parser_data *data = (void *) buffer;
195 enum nss_status retval;
196 char *domain, *result, *p;
197 int len, parse_res;
199 if (name == NULL)
201 __set_errno (EINVAL);
202 return NSS_STATUS_UNAVAIL;
205 if (yp_get_default_domain (&domain))
206 return NSS_STATUS_UNAVAIL;
208 retval = yperr2nss (yp_match (domain, "ethers.byname", name,
209 strlen (name), &result, &len));
211 if (retval != NSS_STATUS_SUCCESS)
213 if (retval == NSS_STATUS_TRYAGAIN)
214 __set_errno (EAGAIN);
215 return retval;
218 if ((size_t) (len + 1) > buflen)
220 free (result);
221 __set_errno (ERANGE);
222 return NSS_STATUS_TRYAGAIN;
225 p = strncpy (buffer, result, len);
226 buffer[len] = '\0';
227 while (isspace (*p))
228 ++p;
229 free (result);
231 parse_res = _nss_files_parse_etherent (p, eth, data, buflen);
233 if (parse_res == -1 && errno == ERANGE)
234 return NSS_STATUS_TRYAGAIN;
235 else if (parse_res == 0)
236 return NSS_STATUS_NOTFOUND;
238 return NSS_STATUS_SUCCESS;
241 enum nss_status
242 _nss_nis_getntohost_r (struct ether_addr *addr, struct ether *eth,
243 char *buffer, size_t buflen)
245 struct parser_data *data = (void *) buffer;
246 enum nss_status retval;
247 char *domain, *result, *p;
248 int len, nlen, parse_res;
249 char buf[33];
251 if (addr == NULL)
253 __set_errno (EINVAL);
254 return NSS_STATUS_UNAVAIL;
257 if (yp_get_default_domain (&domain))
258 return NSS_STATUS_UNAVAIL;
260 nlen = sprintf (buf, "%x:%x:%x:%x:%x:%x",
261 (int) addr->ether_addr_octet[0],
262 (int) addr->ether_addr_octet[1],
263 (int) addr->ether_addr_octet[2],
264 (int) addr->ether_addr_octet[3],
265 (int) addr->ether_addr_octet[4],
266 (int) addr->ether_addr_octet[5]);
268 retval = yperr2nss (yp_match (domain, "ethers.byaddr", buf,
269 nlen, &result, &len));
271 if (retval != NSS_STATUS_SUCCESS)
273 if (retval == NSS_STATUS_TRYAGAIN)
274 __set_errno (EAGAIN);
275 return retval;
278 if ((size_t) (len + 1) > buflen)
280 free (result);
281 __set_errno (ERANGE);
282 return NSS_STATUS_TRYAGAIN;
285 p = strncpy (buffer, result, len);
286 buffer[len] = '\0';
287 while (isspace (*p))
288 ++p;
289 free (result);
291 parse_res = _nss_files_parse_etherent (p, eth, data, buflen);
293 if (parse_res == -1 && errno == ERANGE)
294 return NSS_STATUS_TRYAGAIN;
295 else if (parse_res == 0)
296 return NSS_STATUS_NOTFOUND;
298 return NSS_STATUS_SUCCESS;