]>
Commit | Line | Data |
---|---|---|
1fb05e3d | 1 | /* Copyright (C) 1996, 1997 Free Software Foundation, Inc. |
6259ec0d UD |
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> | |
5107cf1d | 24 | #include <bits/libc-lock.h> |
6259ec0d UD |
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 | { | |
7e3be507 | 36 | const char *e_name; |
6259ec0d UD |
37 | struct ether_addr e_addr; |
38 | }; | |
39 | ||
7e3be507 UD |
40 | /* Get the declaration of the parser function. */ |
41 | #define ENTNAME etherent | |
42 | #define STRUCTURE ether | |
43 | #define EXTERN_PARSER | |
cf29ffbe | 44 | #include <nss/nss_files/files-parse.c> |
7e3be507 | 45 | |
f166d865 UD |
46 | struct response |
47 | { | |
48 | char *val; | |
49 | struct response *next; | |
50 | }; | |
6259ec0d | 51 | |
f166d865 UD |
52 | static struct response *start = NULL; |
53 | static struct response *next = NULL; | |
54 | ||
55 | static int | |
0d8733c4 | 56 | saveit (int instatus, char *inkey, int inkeylen, char *inval, |
f166d865 | 57 | int invallen, char *indata) |
6259ec0d | 58 | { |
f166d865 UD |
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 | } | |
0d8733c4 | 79 | |
f166d865 UD |
80 | return 0; |
81 | } | |
6259ec0d | 82 | |
900bec85 | 83 | static enum nss_status |
f166d865 UD |
84 | internal_nis_setetherent (void) |
85 | { | |
86 | char *domainname; | |
87 | struct ypall_callback ypcb; | |
0d8733c4 UD |
88 | enum nss_status status; |
89 | ||
90 | yp_get_default_domain (&domainname); | |
91 | ||
f166d865 | 92 | while (start != NULL) |
6259ec0d | 93 | { |
f166d865 UD |
94 | if (start->val != NULL) |
95 | free (start->val); | |
96 | next = start; | |
97 | start = start->next; | |
98 | free (next); | |
6259ec0d | 99 | } |
f166d865 | 100 | start = NULL; |
6259ec0d | 101 | |
f166d865 UD |
102 | ypcb.foreach = saveit; |
103 | ypcb.data = NULL; | |
0d8733c4 | 104 | status = yperr2nss (yp_all (domainname, "ethers.byname", &ypcb)); |
f166d865 | 105 | next = start; |
6259ec0d | 106 | |
0d8733c4 | 107 | return status; |
6259ec0d UD |
108 | } |
109 | ||
f166d865 UD |
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 | } | |
0d8733c4 | 123 | |
6259ec0d UD |
124 | enum nss_status |
125 | _nss_nis_endetherent (void) | |
126 | { | |
127 | __libc_lock_lock (lock); | |
128 | ||
f166d865 | 129 | while (start != NULL) |
6259ec0d | 130 | { |
f166d865 UD |
131 | if (start->val != NULL) |
132 | free (start->val); | |
133 | next = start; | |
134 | start = start->next; | |
135 | free (next); | |
6259ec0d | 136 | } |
f166d865 UD |
137 | start = NULL; |
138 | next = NULL; | |
0d8733c4 | 139 | |
6259ec0d | 140 | __libc_lock_unlock (lock); |
0d8733c4 | 141 | |
6259ec0d UD |
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 | { | |
7e3be507 | 148 | struct parser_data *data = (void *) buffer; |
f166d865 | 149 | int parse_res; |
6259ec0d | 150 | |
f166d865 UD |
151 | if (start == NULL) |
152 | internal_nis_setetherent (); | |
6259ec0d UD |
153 | |
154 | /* Get the next entry until we found a correct one. */ | |
155 | do | |
156 | { | |
6259ec0d | 157 | char *p; |
0d8733c4 | 158 | |
f166d865 UD |
159 | if (next == NULL) |
160 | return NSS_STATUS_NOTFOUND; | |
161 | p = strcpy (buffer, next->val); | |
0d8733c4 | 162 | |
6259ec0d UD |
163 | while (isspace (*p)) |
164 | ++p; | |
0d8733c4 | 165 | |
60c96635 UD |
166 | if ((parse_res = _nss_files_parse_etherent (p, eth, data, buflen)) == -1) |
167 | return NSS_STATUS_TRYAGAIN; | |
168 | next = next->next; | |
6259ec0d UD |
169 | } |
170 | while (!parse_res); | |
60c96635 | 171 | |
6259ec0d UD |
172 | return NSS_STATUS_SUCCESS; |
173 | } | |
174 | ||
175 | enum nss_status | |
176 | _nss_nis_getetherent_r (struct ether *result, char *buffer, size_t buflen) | |
177 | { | |
178 | int status; | |
179 | ||
180 | __libc_lock_lock (lock); | |
181 | ||
182 | status = internal_nis_getetherent_r (result, buffer, buflen); | |
183 | ||
184 | __libc_lock_unlock (lock); | |
185 | ||
186 | return status; | |
187 | } | |
188 | ||
189 | enum nss_status | |
1fb05e3d UD |
190 | _nss_nis_gethostton_r (const char *name, struct ether *eth, |
191 | char *buffer, size_t buflen) | |
6259ec0d | 192 | { |
7e3be507 | 193 | struct parser_data *data = (void *) buffer; |
6259ec0d UD |
194 | enum nss_status retval; |
195 | char *domain, *result, *p; | |
196 | int len, parse_res; | |
197 | ||
198 | if (name == NULL) | |
199 | { | |
200 | __set_errno (EINVAL); | |
201 | return NSS_STATUS_UNAVAIL; | |
202 | } | |
203 | ||
204 | if (yp_get_default_domain (&domain)) | |
205 | return NSS_STATUS_UNAVAIL; | |
206 | ||
207 | retval = yperr2nss (yp_match (domain, "ethers.byname", name, | |
208 | strlen (name), &result, &len)); | |
209 | ||
210 | if (retval != NSS_STATUS_SUCCESS) | |
211 | { | |
212 | if (retval == NSS_STATUS_TRYAGAIN) | |
213 | __set_errno (EAGAIN); | |
214 | return retval; | |
215 | } | |
216 | ||
f8b87ef0 | 217 | if ((size_t) (len + 1) > buflen) |
6259ec0d UD |
218 | { |
219 | free (result); | |
220 | __set_errno (ERANGE); | |
221 | return NSS_STATUS_TRYAGAIN; | |
222 | } | |
223 | ||
224 | p = strncpy (buffer, result, len); | |
225 | buffer[len] = '\0'; | |
226 | while (isspace (*p)) | |
227 | ++p; | |
228 | free (result); | |
229 | ||
60c96635 UD |
230 | if ((parse_res = _nss_files_parse_etherent (p, eth, data, buflen)) == -1) |
231 | return NSS_STATUS_TRYAGAIN; | |
6259ec0d UD |
232 | |
233 | if (!parse_res) | |
60c96635 | 234 | return NSS_STATUS_NOTFOUND; |
6259ec0d UD |
235 | else |
236 | return NSS_STATUS_SUCCESS; | |
237 | } | |
238 | ||
239 | enum nss_status | |
1fb05e3d UD |
240 | _nss_nis_getntohost_r (struct ether_addr *addr, struct ether *eth, |
241 | char *buffer, size_t buflen) | |
6259ec0d | 242 | { |
7e3be507 | 243 | struct parser_data *data = (void *) buffer; |
6259ec0d UD |
244 | enum nss_status retval; |
245 | char *domain, *result, *p; | |
246 | int len, nlen, parse_res; | |
247 | char buf[33]; | |
248 | ||
249 | if (addr == NULL) | |
250 | { | |
251 | __set_errno (EINVAL); | |
252 | return NSS_STATUS_UNAVAIL; | |
253 | } | |
254 | ||
255 | if (yp_get_default_domain (&domain)) | |
256 | return NSS_STATUS_UNAVAIL; | |
257 | ||
258 | nlen = sprintf (buf, "%x:%x:%x:%x:%x:%x", | |
259 | (int) addr->ether_addr_octet[0], | |
260 | (int) addr->ether_addr_octet[1], | |
261 | (int) addr->ether_addr_octet[2], | |
262 | (int) addr->ether_addr_octet[3], | |
263 | (int) addr->ether_addr_octet[4], | |
264 | (int) addr->ether_addr_octet[5]); | |
265 | ||
266 | retval = yperr2nss (yp_match (domain, "ethers.byaddr", buf, | |
267 | nlen, &result, &len)); | |
268 | ||
269 | if (retval != NSS_STATUS_SUCCESS) | |
270 | { | |
271 | if (retval == NSS_STATUS_TRYAGAIN) | |
272 | __set_errno (EAGAIN); | |
273 | return retval; | |
274 | } | |
275 | ||
f8b87ef0 | 276 | if ((size_t) (len + 1) > buflen) |
6259ec0d UD |
277 | { |
278 | free (result); | |
279 | __set_errno (ERANGE); | |
280 | return NSS_STATUS_TRYAGAIN; | |
281 | } | |
282 | ||
283 | p = strncpy (buffer, result, len); | |
284 | buffer[len] = '\0'; | |
285 | while (isspace (*p)) | |
286 | ++p; | |
287 | free (result); | |
288 | ||
60c96635 UD |
289 | if ((parse_res = _nss_files_parse_etherent (p, eth, data, buflen)) == -1) |
290 | return NSS_STATUS_TRYAGAIN; | |
6259ec0d UD |
291 | |
292 | if (!parse_res) | |
60c96635 | 293 | return NSS_STATUS_NOTFOUND; |
6259ec0d UD |
294 | else |
295 | return NSS_STATUS_SUCCESS; | |
296 | } |