2002-09-30 Roland McGrath <roland@redhat.com>
[glibc.git] / nis / nss_nis / nis-ethers.c
blob54f10a5d0d578143fce3d59ba059cc093d6b1605
1 /* Copyright (C) 1996-2000, 2001, 2002 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 char *val;
44 struct response *next;
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 instatus;
57 if (inkey && inkeylen > 0 && inval && invallen > 0)
59 if (start == NULL)
61 start = malloc (sizeof (struct response));
62 if (start == NULL)
63 return YP_FALSE;
64 next = start;
66 else
68 next->next = malloc (sizeof (struct response));
69 if (next->next == NULL)
70 return YP_FALSE;
71 next = next->next;
73 next->next = NULL;
74 next->val = malloc (invallen + 1);
75 if (next->val == NULL)
76 return YP_FALSE;
77 strncpy (next->val, inval, invallen);
78 next->val[invallen] = '\0';
81 return 0;
84 static enum nss_status
85 internal_nis_setetherent (void)
87 char *domainname;
88 struct ypall_callback ypcb;
89 enum nss_status status;
91 yp_get_default_domain (&domainname);
93 while (start != NULL)
95 if (start->val != NULL)
96 free (start->val);
97 next = start;
98 start = start->next;
99 free (next);
101 start = NULL;
103 ypcb.foreach = saveit;
104 ypcb.data = NULL;
105 status = yperr2nss (yp_all (domainname, "ethers.byname", &ypcb));
106 next = start;
108 return status;
111 enum nss_status
112 _nss_nis_setetherent (int stayopen)
114 enum nss_status result;
116 __libc_lock_lock (lock);
118 result = internal_nis_setetherent ();
120 __libc_lock_unlock (lock);
122 return result;
125 enum nss_status
126 _nss_nis_endetherent (void)
128 __libc_lock_lock (lock);
130 while (start != NULL)
132 if (start->val != NULL)
133 free (start->val);
134 next = start;
135 start = start->next;
136 free (next);
138 start = NULL;
139 next = NULL;
141 __libc_lock_unlock (lock);
143 return NSS_STATUS_SUCCESS;
146 static enum nss_status
147 internal_nis_getetherent_r (struct etherent *eth, char *buffer, size_t buflen,
148 int *errnop)
150 struct parser_data *data = (void *) buffer;
151 int parse_res;
153 if (start == NULL)
154 internal_nis_setetherent ();
156 /* Get the next entry until we found a correct one. */
159 char *p;
161 if (next == NULL)
162 return NSS_STATUS_NOTFOUND;
164 p = strncpy (buffer, next->val, buflen);
166 while (isspace (*p))
167 ++p;
169 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
170 if (parse_res == -1)
171 return NSS_STATUS_TRYAGAIN;
172 next = next->next;
174 while (!parse_res);
176 return NSS_STATUS_SUCCESS;
179 enum nss_status
180 _nss_nis_getetherent_r (struct etherent *result, char *buffer, size_t buflen,
181 int *errnop)
183 int status;
185 __libc_lock_lock (lock);
187 status = internal_nis_getetherent_r (result, buffer, buflen, errnop);
189 __libc_lock_unlock (lock);
191 return status;
194 enum nss_status
195 _nss_nis_gethostton_r (const char *name, struct etherent *eth,
196 char *buffer, size_t buflen, int *errnop)
198 struct parser_data *data = (void *) buffer;
199 enum nss_status retval;
200 char *domain, *result, *p;
201 int len, parse_res;
203 if (name == NULL)
205 *errnop = EINVAL;
206 return NSS_STATUS_UNAVAIL;
209 if (yp_get_default_domain (&domain))
210 return NSS_STATUS_UNAVAIL;
212 retval = yperr2nss (yp_match (domain, "ethers.byname", name,
213 strlen (name), &result, &len));
215 if (retval != NSS_STATUS_SUCCESS)
217 if (retval == NSS_STATUS_TRYAGAIN)
218 *errnop = errno;
219 return retval;
222 if ((size_t) (len + 1) > buflen)
224 free (result);
225 *errnop = ERANGE;
226 return NSS_STATUS_TRYAGAIN;
229 p = strncpy (buffer, result, len);
230 buffer[len] = '\0';
231 while (isspace (*p))
232 ++p;
233 free (result);
235 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
236 if (parse_res < 1)
238 if (parse_res == -1)
239 return NSS_STATUS_TRYAGAIN;
240 else
241 return NSS_STATUS_NOTFOUND;
243 return NSS_STATUS_SUCCESS;
246 enum nss_status
247 _nss_nis_getntohost_r (const struct ether_addr *addr, struct etherent *eth,
248 char *buffer, size_t buflen, int *errnop)
250 struct parser_data *data = (void *) buffer;
251 enum nss_status retval;
252 char *domain, *result, *p;
253 int len, nlen, parse_res;
254 char buf[33];
256 if (addr == NULL)
258 *errnop = EINVAL;
259 return NSS_STATUS_UNAVAIL;
262 if (yp_get_default_domain (&domain))
263 return NSS_STATUS_UNAVAIL;
265 nlen = sprintf (buf, "%x:%x:%x:%x:%x:%x",
266 (int) addr->ether_addr_octet[0],
267 (int) addr->ether_addr_octet[1],
268 (int) addr->ether_addr_octet[2],
269 (int) addr->ether_addr_octet[3],
270 (int) addr->ether_addr_octet[4],
271 (int) addr->ether_addr_octet[5]);
273 retval = yperr2nss (yp_match (domain, "ethers.byaddr", buf,
274 nlen, &result, &len));
276 if (retval != NSS_STATUS_SUCCESS)
278 if (retval == NSS_STATUS_TRYAGAIN)
279 *errnop = errno;
280 return retval;
283 if ((size_t) (len + 1) > buflen)
285 free (result);
286 *errnop = ERANGE;
287 return NSS_STATUS_TRYAGAIN;
290 p = strncpy (buffer, result, len);
291 buffer[len] = '\0';
292 while (isspace (*p))
293 ++p;
294 free (result);
296 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
297 if (parse_res < 1)
299 if (parse_res == -1)
300 return NSS_STATUS_TRYAGAIN;
301 else
302 return NSS_STATUS_NOTFOUND;
304 return NSS_STATUS_SUCCESS;