]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nis/nis-ethers.c
Update.
[thirdparty/glibc.git] / nis / nss_nis / nis-ethers.c
CommitLineData
fc9f33e3 1/* Copyright (C) 1996-2000, 2001 Free Software Foundation, Inc.
6259ec0d 2 This file is part of the GNU C Library.
b85697f6 3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
6259ec0d
UD
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
6259ec0d
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
6259ec0d 14
41bdb6e2
AJ
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. */
6259ec0d
UD
19
20#include <nss.h>
21#include <ctype.h>
22#include <errno.h>
23#include <string.h>
5107cf1d 24#include <bits/libc-lock.h>
6259ec0d
UD
25#include <rpcsvc/yp.h>
26#include <rpcsvc/ypclnt.h>
6b083d46 27#include <netinet/ether.h>
6259ec0d
UD
28#include <netinet/if_ether.h>
29
30#include "nss-nis.h"
31
32/* Protect global state against multiple changers */
33__libc_lock_define_initialized (static, lock)
34
7e3be507
UD
35/* Get the declaration of the parser function. */
36#define ENTNAME etherent
6b083d46 37#define STRUCTURE etherent
7e3be507 38#define EXTERN_PARSER
cf29ffbe 39#include <nss/nss_files/files-parse.c>
7e3be507 40
f166d865
UD
41struct response
42{
43 char *val;
44 struct response *next;
45};
6259ec0d 46
fc9f33e3
UD
47static struct response *start;
48static struct response *next;
f166d865
UD
49
50static int
0d8733c4 51saveit (int instatus, char *inkey, int inkeylen, char *inval,
f166d865 52 int invallen, char *indata)
6259ec0d 53{
f166d865
UD
54 if (instatus != YP_TRUE)
55 return instatus;
56
57 if (inkey && inkeylen > 0 && inval && invallen > 0)
58 {
59 if (start == NULL)
60 {
61 start = malloc (sizeof (struct response));
3b965a7d
UD
62 if (start == NULL)
63 return YP_FALSE;
f166d865
UD
64 next = start;
65 }
66 else
67 {
68 next->next = malloc (sizeof (struct response));
3b965a7d
UD
69 if (next->next == NULL)
70 return YP_FALSE;
f166d865
UD
71 next = next->next;
72 }
73 next->next = NULL;
74 next->val = malloc (invallen + 1);
3b965a7d
UD
75 if (next->val == NULL)
76 return YP_FALSE;
f166d865
UD
77 strncpy (next->val, inval, invallen);
78 next->val[invallen] = '\0';
79 }
0d8733c4 80
f166d865
UD
81 return 0;
82}
6259ec0d 83
900bec85 84static enum nss_status
f166d865
UD
85internal_nis_setetherent (void)
86{
87 char *domainname;
88 struct ypall_callback ypcb;
0d8733c4
UD
89 enum nss_status status;
90
91 yp_get_default_domain (&domainname);
92
f166d865 93 while (start != NULL)
6259ec0d 94 {
f166d865
UD
95 if (start->val != NULL)
96 free (start->val);
97 next = start;
98 start = start->next;
99 free (next);
6259ec0d 100 }
f166d865 101 start = NULL;
6259ec0d 102
f166d865
UD
103 ypcb.foreach = saveit;
104 ypcb.data = NULL;
0d8733c4 105 status = yperr2nss (yp_all (domainname, "ethers.byname", &ypcb));
f166d865 106 next = start;
6259ec0d 107
0d8733c4 108 return status;
6259ec0d
UD
109}
110
f166d865 111enum nss_status
ed073f0e 112_nss_nis_setetherent (int stayopen)
f166d865
UD
113{
114 enum nss_status result;
115
116 __libc_lock_lock (lock);
117
118 result = internal_nis_setetherent ();
119
120 __libc_lock_unlock (lock);
121
122 return result;
123}
0d8733c4 124
6259ec0d
UD
125enum nss_status
126_nss_nis_endetherent (void)
127{
128 __libc_lock_lock (lock);
129
f166d865 130 while (start != NULL)
6259ec0d 131 {
f166d865
UD
132 if (start->val != NULL)
133 free (start->val);
134 next = start;
135 start = start->next;
136 free (next);
6259ec0d 137 }
f166d865
UD
138 start = NULL;
139 next = NULL;
0d8733c4 140
6259ec0d 141 __libc_lock_unlock (lock);
0d8733c4 142
6259ec0d
UD
143 return NSS_STATUS_SUCCESS;
144}
145
146static enum nss_status
6b083d46 147internal_nis_getetherent_r (struct etherent *eth, char *buffer, size_t buflen,
d71b808a 148 int *errnop)
6259ec0d 149{
7e3be507 150 struct parser_data *data = (void *) buffer;
f166d865 151 int parse_res;
6259ec0d 152
f166d865
UD
153 if (start == NULL)
154 internal_nis_setetherent ();
6259ec0d
UD
155
156 /* Get the next entry until we found a correct one. */
157 do
158 {
6259ec0d 159 char *p;
0d8733c4 160
f166d865 161 if (next == NULL)
0c6cee5d
UD
162 {
163 *errnop = ENOENT;
164 return NSS_STATUS_NOTFOUND;
165 }
9756dfe1 166 p = strncpy (buffer, next->val, buflen);
0d8733c4 167
6259ec0d
UD
168 while (isspace (*p))
169 ++p;
0d8733c4 170
d71b808a
UD
171 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
172 if (parse_res == -1)
60c96635
UD
173 return NSS_STATUS_TRYAGAIN;
174 next = next->next;
6259ec0d
UD
175 }
176 while (!parse_res);
d71b808a 177
6259ec0d
UD
178 return NSS_STATUS_SUCCESS;
179}
180
181enum nss_status
6b083d46 182_nss_nis_getetherent_r (struct etherent *result, char *buffer, size_t buflen,
d71b808a 183 int *errnop)
6259ec0d
UD
184{
185 int status;
186
187 __libc_lock_lock (lock);
188
d71b808a 189 status = internal_nis_getetherent_r (result, buffer, buflen, errnop);
6259ec0d
UD
190
191 __libc_lock_unlock (lock);
192
193 return status;
194}
195
196enum nss_status
6b083d46 197_nss_nis_gethostton_r (const char *name, struct etherent *eth,
d71b808a 198 char *buffer, size_t buflen, int *errnop)
6259ec0d 199{
7e3be507 200 struct parser_data *data = (void *) buffer;
6259ec0d
UD
201 enum nss_status retval;
202 char *domain, *result, *p;
203 int len, parse_res;
204
205 if (name == NULL)
206 {
ac9f45cf 207 *errnop = EINVAL;
6259ec0d
UD
208 return NSS_STATUS_UNAVAIL;
209 }
210
211 if (yp_get_default_domain (&domain))
212 return NSS_STATUS_UNAVAIL;
213
214 retval = yperr2nss (yp_match (domain, "ethers.byname", name,
215 strlen (name), &result, &len));
216
217 if (retval != NSS_STATUS_SUCCESS)
218 {
0c6cee5d
UD
219 if (retval == NSS_STATUS_NOTFOUND)
220 *errnop = ENOENT;
221 else if (retval == NSS_STATUS_TRYAGAIN)
d71b808a 222 *errnop = errno;
6259ec0d
UD
223 return retval;
224 }
225
f8b87ef0 226 if ((size_t) (len + 1) > buflen)
6259ec0d
UD
227 {
228 free (result);
d71b808a 229 *errnop = ERANGE;
6259ec0d
UD
230 return NSS_STATUS_TRYAGAIN;
231 }
232
233 p = strncpy (buffer, result, len);
234 buffer[len] = '\0';
235 while (isspace (*p))
236 ++p;
237 free (result);
238
d71b808a 239 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
ac9f45cf
UD
240 if (parse_res < 1)
241 {
242 if (parse_res == -1)
243 return NSS_STATUS_TRYAGAIN;
244 else
b85697f6
UD
245 {
246 *errnop = ENOENT;
247 return NSS_STATUS_NOTFOUND;
248 }
ac9f45cf
UD
249 }
250 return NSS_STATUS_SUCCESS;
6259ec0d
UD
251}
252
253enum nss_status
6b083d46 254_nss_nis_getntohost_r (const struct ether_addr *addr, struct etherent *eth,
d71b808a 255 char *buffer, size_t buflen, int *errnop)
6259ec0d 256{
7e3be507 257 struct parser_data *data = (void *) buffer;
6259ec0d
UD
258 enum nss_status retval;
259 char *domain, *result, *p;
260 int len, nlen, parse_res;
261 char buf[33];
262
263 if (addr == NULL)
264 {
ac9f45cf 265 *errnop = EINVAL;
6259ec0d
UD
266 return NSS_STATUS_UNAVAIL;
267 }
268
269 if (yp_get_default_domain (&domain))
270 return NSS_STATUS_UNAVAIL;
271
272 nlen = sprintf (buf, "%x:%x:%x:%x:%x:%x",
273 (int) addr->ether_addr_octet[0],
274 (int) addr->ether_addr_octet[1],
275 (int) addr->ether_addr_octet[2],
276 (int) addr->ether_addr_octet[3],
277 (int) addr->ether_addr_octet[4],
278 (int) addr->ether_addr_octet[5]);
279
280 retval = yperr2nss (yp_match (domain, "ethers.byaddr", buf,
281 nlen, &result, &len));
282
283 if (retval != NSS_STATUS_SUCCESS)
284 {
285 if (retval == NSS_STATUS_TRYAGAIN)
d71b808a 286 *errnop = errno;
6259ec0d
UD
287 return retval;
288 }
289
f8b87ef0 290 if ((size_t) (len + 1) > buflen)
6259ec0d
UD
291 {
292 free (result);
d71b808a 293 *errnop = ERANGE;
6259ec0d
UD
294 return NSS_STATUS_TRYAGAIN;
295 }
296
297 p = strncpy (buffer, result, len);
298 buffer[len] = '\0';
299 while (isspace (*p))
300 ++p;
301 free (result);
302
d71b808a 303 parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop);
ac9f45cf
UD
304 if (parse_res < 1)
305 {
306 if (parse_res == -1)
307 return NSS_STATUS_TRYAGAIN;
308 else
0c6cee5d
UD
309 {
310 *errnop = ENOENT;
311 return NSS_STATUS_NOTFOUND;
312 }
ac9f45cf
UD
313 }
314 return NSS_STATUS_SUCCESS;
6259ec0d 315}