]>
Commit | Line | Data |
---|---|---|
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. | |
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 <bits/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 | ||
34 | struct 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 | ||
46 | struct response | |
47 | { | |
48 | char *val; | |
49 | struct response *next; | |
50 | }; | |
51 | ||
52 | static struct response *start = NULL; | |
53 | static struct response *next = NULL; | |
54 | ||
55 | static int | |
56 | saveit (int instatus, char *inkey, int inkeylen, char *inval, | |
57 | int invallen, char *indata) | |
58 | { | |
59 | if (instatus != YP_TRUE) | |
60 | return instatus; | |
61 | ||
62 | if (inkey && inkeylen > 0 && inval && invallen > 0) | |
63 | { | |
64 | if (start == NULL) | |
65 | { | |
66 | start = malloc (sizeof (struct response)); | |
67 | next = start; | |
68 | } | |
69 | else | |
70 | { | |
71 | next->next = malloc (sizeof (struct response)); | |
72 | next = next->next; | |
73 | } | |
74 | next->next = NULL; | |
75 | next->val = malloc (invallen + 1); | |
76 | strncpy (next->val, inval, invallen); | |
77 | next->val[invallen] = '\0'; | |
78 | } | |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
83 | static enum nss_status | |
84 | internal_nis_setetherent (void) | |
85 | { | |
86 | char *domainname; | |
87 | struct ypall_callback ypcb; | |
88 | enum nss_status status; | |
89 | ||
90 | yp_get_default_domain (&domainname); | |
91 | ||
92 | while (start != NULL) | |
93 | { | |
94 | if (start->val != NULL) | |
95 | free (start->val); | |
96 | next = start; | |
97 | start = start->next; | |
98 | free (next); | |
99 | } | |
100 | start = NULL; | |
101 | ||
102 | ypcb.foreach = saveit; | |
103 | ypcb.data = NULL; | |
104 | status = yperr2nss (yp_all (domainname, "ethers.byname", &ypcb)); | |
105 | next = start; | |
106 | ||
107 | return status; | |
108 | } | |
109 | ||
110 | enum nss_status | |
111 | _nss_nis_setetherent (void) | |
112 | { | |
113 | enum nss_status result; | |
114 | ||
115 | __libc_lock_lock (lock); | |
116 | ||
117 | result = internal_nis_setetherent (); | |
118 | ||
119 | __libc_lock_unlock (lock); | |
120 | ||
121 | return result; | |
122 | } | |
123 | ||
124 | enum nss_status | |
125 | _nss_nis_endetherent (void) | |
126 | { | |
127 | __libc_lock_lock (lock); | |
128 | ||
129 | while (start != NULL) | |
130 | { | |
131 | if (start->val != NULL) | |
132 | free (start->val); | |
133 | next = start; | |
134 | start = start->next; | |
135 | free (next); | |
136 | } | |
137 | start = NULL; | |
138 | next = NULL; | |
139 | ||
140 | __libc_lock_unlock (lock); | |
141 | ||
142 | return NSS_STATUS_SUCCESS; | |
143 | } | |
144 | ||
145 | static enum nss_status | |
146 | internal_nis_getetherent_r (struct ether *eth, char *buffer, size_t buflen, | |
147 | int *errnop) | |
148 | { | |
149 | struct parser_data *data = (void *) buffer; | |
150 | int parse_res; | |
151 | ||
152 | if (start == NULL) | |
153 | internal_nis_setetherent (); | |
154 | ||
155 | /* Get the next entry until we found a correct one. */ | |
156 | do | |
157 | { | |
158 | char *p; | |
159 | ||
160 | if (next == NULL) | |
161 | return NSS_STATUS_NOTFOUND; | |
162 | p = strncpy (buffer, next->val, buflen); | |
163 | ||
164 | while (isspace (*p)) | |
165 | ++p; | |
166 | ||
167 | parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop); | |
168 | if (parse_res == -1) | |
169 | return NSS_STATUS_TRYAGAIN; | |
170 | next = next->next; | |
171 | } | |
172 | while (!parse_res); | |
173 | ||
174 | return NSS_STATUS_SUCCESS; | |
175 | } | |
176 | ||
177 | enum nss_status | |
178 | _nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen, | |
179 | int *errnop) | |
180 | { | |
181 | int status; | |
182 | ||
183 | __libc_lock_lock (lock); | |
184 | ||
185 | status = internal_nis_getetherent_r (result, buffer, buflen, errnop); | |
186 | ||
187 | __libc_lock_unlock (lock); | |
188 | ||
189 | return status; | |
190 | } | |
191 | ||
192 | enum nss_status | |
193 | _nss_nis_gethostton_r (const char *name, struct ether *eth, | |
194 | char *buffer, size_t buflen, int *errnop) | |
195 | { | |
196 | struct parser_data *data = (void *) buffer; | |
197 | enum nss_status retval; | |
198 | char *domain, *result, *p; | |
199 | int len, parse_res; | |
200 | ||
201 | if (name == NULL) | |
202 | { | |
203 | __set_errno (EINVAL); | |
204 | return NSS_STATUS_UNAVAIL; | |
205 | } | |
206 | ||
207 | if (yp_get_default_domain (&domain)) | |
208 | return NSS_STATUS_UNAVAIL; | |
209 | ||
210 | retval = yperr2nss (yp_match (domain, "ethers.byname", name, | |
211 | strlen (name), &result, &len)); | |
212 | ||
213 | if (retval != NSS_STATUS_SUCCESS) | |
214 | { | |
215 | if (retval == NSS_STATUS_TRYAGAIN) | |
216 | *errnop = errno; | |
217 | return retval; | |
218 | } | |
219 | ||
220 | if ((size_t) (len + 1) > buflen) | |
221 | { | |
222 | free (result); | |
223 | *errnop = ERANGE; | |
224 | return NSS_STATUS_TRYAGAIN; | |
225 | } | |
226 | ||
227 | p = strncpy (buffer, result, len); | |
228 | buffer[len] = '\0'; | |
229 | while (isspace (*p)) | |
230 | ++p; | |
231 | free (result); | |
232 | ||
233 | parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop); | |
234 | if (parse_res == -1) | |
235 | return NSS_STATUS_TRYAGAIN; | |
236 | ||
237 | if (!parse_res) | |
238 | return NSS_STATUS_NOTFOUND; | |
239 | else | |
240 | return NSS_STATUS_SUCCESS; | |
241 | } | |
242 | ||
243 | enum nss_status | |
244 | _nss_nis_getntohost_r (struct ether_addr *addr, struct ether *eth, | |
245 | char *buffer, size_t buflen, int *errnop) | |
246 | { | |
247 | struct parser_data *data = (void *) buffer; | |
248 | enum nss_status retval; | |
249 | char *domain, *result, *p; | |
250 | int len, nlen, parse_res; | |
251 | char buf[33]; | |
252 | ||
253 | if (addr == NULL) | |
254 | { | |
255 | __set_errno (EINVAL); | |
256 | return NSS_STATUS_UNAVAIL; | |
257 | } | |
258 | ||
259 | if (yp_get_default_domain (&domain)) | |
260 | return NSS_STATUS_UNAVAIL; | |
261 | ||
262 | nlen = sprintf (buf, "%x:%x:%x:%x:%x:%x", | |
263 | (int) addr->ether_addr_octet[0], | |
264 | (int) addr->ether_addr_octet[1], | |
265 | (int) addr->ether_addr_octet[2], | |
266 | (int) addr->ether_addr_octet[3], | |
267 | (int) addr->ether_addr_octet[4], | |
268 | (int) addr->ether_addr_octet[5]); | |
269 | ||
270 | retval = yperr2nss (yp_match (domain, "ethers.byaddr", buf, | |
271 | nlen, &result, &len)); | |
272 | ||
273 | if (retval != NSS_STATUS_SUCCESS) | |
274 | { | |
275 | if (retval == NSS_STATUS_TRYAGAIN) | |
276 | *errnop = errno; | |
277 | return retval; | |
278 | } | |
279 | ||
280 | if ((size_t) (len + 1) > buflen) | |
281 | { | |
282 | free (result); | |
283 | *errnop = ERANGE; | |
284 | return NSS_STATUS_TRYAGAIN; | |
285 | } | |
286 | ||
287 | p = strncpy (buffer, result, len); | |
288 | buffer[len] = '\0'; | |
289 | while (isspace (*p)) | |
290 | ++p; | |
291 | free (result); | |
292 | ||
293 | parse_res = _nss_files_parse_etherent (p, eth, data, buflen, errnop); | |
294 | if (parse_res == -1) | |
295 | return NSS_STATUS_TRYAGAIN; | |
296 | ||
297 | if (!parse_res) | |
298 | return NSS_STATUS_NOTFOUND; | |
299 | else | |
300 | return NSS_STATUS_SUCCESS; | |
301 | } |