update from main archive 961211
[glibc/history.git] / nis / nss_nis / nis-ethers.c
blobae3ba76adfc354a2948d9584c8654414c3d99f80
1 /* Copyright (C) 1996 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 <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 char *e_name;
37 struct ether_addr e_addr;
40 static bool_t new_start = 1;
41 static char *oldkey = NULL;
42 static int oldkeylen = 0;
44 enum nss_status
45 _nss_nis_setetherent (void)
47 __libc_lock_lock (lock);
49 new_start = 1;
50 if (oldkey != NULL)
52 free (oldkey);
53 oldkey = NULL;
54 oldkeylen = 0;
57 __libc_lock_unlock (lock);
59 return NSS_STATUS_SUCCESS;
62 enum nss_status
63 _nss_nis_endetherent (void)
65 __libc_lock_lock (lock);
67 new_start = 1;
68 if (oldkey != NULL)
70 free (oldkey);
71 oldkey = NULL;
72 oldkeylen = 0;
75 __libc_lock_unlock (lock);
77 return NSS_STATUS_SUCCESS;
80 static enum nss_status
81 internal_nis_getetherent_r (struct ether *eth, char *buffer, size_t buflen)
83 char *domain, *result, *outkey;
84 int len, keylen, parse_res;
86 if (yp_get_default_domain (&domain))
87 return NSS_STATUS_UNAVAIL;
89 /* Get the next entry until we found a correct one. */
92 enum nss_status retval;
93 char *p;
95 if (new_start)
96 retval = yperr2nss (yp_first (domain, "ethers.byaddr",
97 &outkey, &keylen, &result, &len));
98 else
99 retval = yperr2nss ( yp_next (domain, "ethers.byaddr",
100 oldkey, oldkeylen,
101 &outkey, &keylen, &result, &len));
103 if (retval != NSS_STATUS_SUCCESS)
105 if (retval == NSS_STATUS_TRYAGAIN)
106 __set_errno (EAGAIN);
107 return retval;
110 if ((size_t) (len + 1) > buflen)
112 free (result);
113 __set_errno (ERANGE);
114 return NSS_STATUS_TRYAGAIN;
117 p = strncpy (buffer, result, len);
118 buffer[len] = '\0';
119 while (isspace (*p))
120 ++p;
121 free (result);
123 parse_res = _nss_files_parse_etherent (p, eth, buffer, buflen);
124 if (!parse_res && errno == ERANGE)
125 return NSS_STATUS_TRYAGAIN;
127 free (oldkey);
128 oldkey = outkey;
129 oldkeylen = keylen;
130 new_start = 0;
132 while (!parse_res);
134 return NSS_STATUS_SUCCESS;
137 enum nss_status
138 _nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen)
140 int status;
142 __libc_lock_lock (lock);
144 status = internal_nis_getetherent_r (result, buffer, buflen);
146 __libc_lock_unlock (lock);
148 return status;
151 enum nss_status
152 _nss_nis_getethernam_r (const char *name, struct ether *eth,
153 char *buffer, size_t buflen)
155 enum nss_status retval;
156 char *domain, *result, *p;
157 int len, parse_res;
159 if (name == NULL)
161 __set_errno (EINVAL);
162 return NSS_STATUS_UNAVAIL;
165 if (yp_get_default_domain (&domain))
166 return NSS_STATUS_UNAVAIL;
168 retval = yperr2nss (yp_match (domain, "ethers.byname", name,
169 strlen (name), &result, &len));
171 if (retval != NSS_STATUS_SUCCESS)
173 if (retval == NSS_STATUS_TRYAGAIN)
174 __set_errno (EAGAIN);
175 return retval;
178 if ((size_t) (len + 1) > buflen)
180 free (result);
181 __set_errno (ERANGE);
182 return NSS_STATUS_TRYAGAIN;
185 p = strncpy (buffer, result, len);
186 buffer[len] = '\0';
187 while (isspace (*p))
188 ++p;
189 free (result);
191 parse_res = _nss_files_parse_etherent (p, eth, buffer, buflen);
193 if (!parse_res)
195 if (errno == ERANGE)
196 return NSS_STATUS_TRYAGAIN;
197 else
198 return NSS_STATUS_NOTFOUND;
200 else
201 return NSS_STATUS_SUCCESS;
204 enum nss_status
205 _nss_nis_getetherbyaddr_r (struct ether_addr *addr, struct ether *eth,
206 char *buffer, size_t buflen)
208 enum nss_status retval;
209 char *domain, *result, *p;
210 int len, nlen, parse_res;
211 char buf[33];
213 if (addr == NULL)
215 __set_errno (EINVAL);
216 return NSS_STATUS_UNAVAIL;
219 if (yp_get_default_domain (&domain))
220 return NSS_STATUS_UNAVAIL;
222 nlen = sprintf (buf, "%x:%x:%x:%x:%x:%x",
223 (int) addr->ether_addr_octet[0],
224 (int) addr->ether_addr_octet[1],
225 (int) addr->ether_addr_octet[2],
226 (int) addr->ether_addr_octet[3],
227 (int) addr->ether_addr_octet[4],
228 (int) addr->ether_addr_octet[5]);
230 retval = yperr2nss (yp_match (domain, "ethers.byaddr", buf,
231 nlen, &result, &len));
233 if (retval != NSS_STATUS_SUCCESS)
235 if (retval == NSS_STATUS_TRYAGAIN)
236 __set_errno (EAGAIN);
237 return retval;
240 if ((size_t) (len + 1) > buflen)
242 free (result);
243 __set_errno (ERANGE);
244 return NSS_STATUS_TRYAGAIN;
247 p = strncpy (buffer, result, len);
248 buffer[len] = '\0';
249 while (isspace (*p))
250 ++p;
251 free (result);
253 parse_res = _nss_files_parse_etherent (p, eth, buffer, buflen);
255 if (!parse_res)
257 if (errno == ERANGE)
258 return NSS_STATUS_TRYAGAIN;
259 else
260 return NSS_STATUS_NOTFOUND;
262 else
263 return NSS_STATUS_SUCCESS;