]> sourceware.org Git - glibc.git/blame_incremental - nis/nss_nis/nis-ethers.c
Update to 2.1.x development version
[glibc.git] / nis / nss_nis / nis-ethers.c
... / ...
CommitLineData
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.
4
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.
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
13 Library General Public License for more details.
14
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. */
19
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>
28
29#include "nss-nis.h"
30
31/* Protect global state against multiple changers */
32__libc_lock_define_initialized (static, lock)
33
34struct ether
35{
36 const char *e_name;
37 struct ether_addr e_addr;
38};
39
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"
45
46static bool_t new_start = 1;
47static char *oldkey = NULL;
48static int oldkeylen = 0;
49
50enum nss_status
51_nss_nis_setetherent (void)
52{
53 __libc_lock_lock (lock);
54
55 new_start = 1;
56 if (oldkey != NULL)
57 {
58 free (oldkey);
59 oldkey = NULL;
60 oldkeylen = 0;
61 }
62
63 __libc_lock_unlock (lock);
64
65 return NSS_STATUS_SUCCESS;
66}
67
68enum nss_status
69_nss_nis_endetherent (void)
70{
71 __libc_lock_lock (lock);
72
73 new_start = 1;
74 if (oldkey != NULL)
75 {
76 free (oldkey);
77 oldkey = NULL;
78 oldkeylen = 0;
79 }
80
81 __libc_lock_unlock (lock);
82
83 return NSS_STATUS_SUCCESS;
84}
85
86static enum nss_status
87internal_nis_getetherent_r (struct ether *eth, char *buffer, size_t buflen)
88{
89 struct parser_data *data = (void *) buffer;
90 char *domain, *result, *outkey;
91 int len, keylen, parse_res;
92
93 if (yp_get_default_domain (&domain))
94 return NSS_STATUS_UNAVAIL;
95
96 /* Get the next entry until we found a correct one. */
97 do
98 {
99 enum nss_status retval;
100 char *p;
101
102 if (new_start)
103 retval = yperr2nss (yp_first (domain, "ethers.byaddr",
104 &outkey, &keylen, &result, &len));
105 else
106 retval = yperr2nss ( yp_next (domain, "ethers.byaddr",
107 oldkey, oldkeylen,
108 &outkey, &keylen, &result, &len));
109
110 if (retval != NSS_STATUS_SUCCESS)
111 {
112 if (retval == NSS_STATUS_TRYAGAIN)
113 __set_errno (EAGAIN);
114 return retval;
115 }
116
117 if ((size_t) (len + 1) > buflen)
118 {
119 free (result);
120 __set_errno (ERANGE);
121 return NSS_STATUS_TRYAGAIN;
122 }
123
124 p = strncpy (buffer, result, len);
125 buffer[len] = '\0';
126 while (isspace (*p))
127 ++p;
128 free (result);
129
130 parse_res = _nss_files_parse_etherent (p, eth, data, buflen);
131 if (!parse_res && errno == ERANGE)
132 return NSS_STATUS_TRYAGAIN;
133
134 free (oldkey);
135 oldkey = outkey;
136 oldkeylen = keylen;
137 new_start = 0;
138 }
139 while (!parse_res);
140
141 return NSS_STATUS_SUCCESS;
142}
143
144enum nss_status
145_nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen)
146{
147 int status;
148
149 __libc_lock_lock (lock);
150
151 status = internal_nis_getetherent_r (result, buffer, buflen);
152
153 __libc_lock_unlock (lock);
154
155 return status;
156}
157
158enum nss_status
159_nss_nis_getethernam_r (const char *name, struct ether *eth,
160 char *buffer, size_t buflen)
161{
162 struct parser_data *data = (void *) buffer;
163 enum nss_status retval;
164 char *domain, *result, *p;
165 int len, parse_res;
166
167 if (name == NULL)
168 {
169 __set_errno (EINVAL);
170 return NSS_STATUS_UNAVAIL;
171 }
172
173 if (yp_get_default_domain (&domain))
174 return NSS_STATUS_UNAVAIL;
175
176 retval = yperr2nss (yp_match (domain, "ethers.byname", name,
177 strlen (name), &result, &len));
178
179 if (retval != NSS_STATUS_SUCCESS)
180 {
181 if (retval == NSS_STATUS_TRYAGAIN)
182 __set_errno (EAGAIN);
183 return retval;
184 }
185
186 if ((size_t) (len + 1) > buflen)
187 {
188 free (result);
189 __set_errno (ERANGE);
190 return NSS_STATUS_TRYAGAIN;
191 }
192
193 p = strncpy (buffer, result, len);
194 buffer[len] = '\0';
195 while (isspace (*p))
196 ++p;
197 free (result);
198
199 parse_res = _nss_files_parse_etherent (p, eth, data, buflen);
200
201 if (!parse_res)
202 {
203 if (errno == ERANGE)
204 return NSS_STATUS_TRYAGAIN;
205 else
206 return NSS_STATUS_NOTFOUND;
207 }
208 else
209 return NSS_STATUS_SUCCESS;
210}
211
212enum nss_status
213_nss_nis_getetherbyaddr_r (struct ether_addr *addr, struct ether *eth,
214 char *buffer, size_t buflen)
215{
216 struct parser_data *data = (void *) buffer;
217 enum nss_status retval;
218 char *domain, *result, *p;
219 int len, nlen, parse_res;
220 char buf[33];
221
222 if (addr == NULL)
223 {
224 __set_errno (EINVAL);
225 return NSS_STATUS_UNAVAIL;
226 }
227
228 if (yp_get_default_domain (&domain))
229 return NSS_STATUS_UNAVAIL;
230
231 nlen = sprintf (buf, "%x:%x:%x:%x:%x:%x",
232 (int) addr->ether_addr_octet[0],
233 (int) addr->ether_addr_octet[1],
234 (int) addr->ether_addr_octet[2],
235 (int) addr->ether_addr_octet[3],
236 (int) addr->ether_addr_octet[4],
237 (int) addr->ether_addr_octet[5]);
238
239 retval = yperr2nss (yp_match (domain, "ethers.byaddr", buf,
240 nlen, &result, &len));
241
242 if (retval != NSS_STATUS_SUCCESS)
243 {
244 if (retval == NSS_STATUS_TRYAGAIN)
245 __set_errno (EAGAIN);
246 return retval;
247 }
248
249 if ((size_t) (len + 1) > buflen)
250 {
251 free (result);
252 __set_errno (ERANGE);
253 return NSS_STATUS_TRYAGAIN;
254 }
255
256 p = strncpy (buffer, result, len);
257 buffer[len] = '\0';
258 while (isspace (*p))
259 ++p;
260 free (result);
261
262 parse_res = _nss_files_parse_etherent (p, eth, data, buflen);
263
264 if (!parse_res)
265 {
266 if (errno == ERANGE)
267 return NSS_STATUS_TRYAGAIN;
268 else
269 return NSS_STATUS_NOTFOUND;
270 }
271 else
272 return NSS_STATUS_SUCCESS;
273}
This page took 0.028997 seconds and 5 git commands to generate.