]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-hosts.c
Update.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-hosts.c
1 /* Copyright (C) 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>, 1997.
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 <netdb.h>
22 #include <errno.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/nis.h>
29 #include <rpcsvc/nislib.h>
30
31 #include "nss-nisplus.h"
32
33 __libc_lock_define_initialized (static, lock)
34
35 static nis_result *result = NULL;
36 static nis_name tablename_val = NULL;
37 static u_long tablename_len = 0;
38
39 #define NISENTRYVAL(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
41
42 #define NISENTRYLEN(idx,col,res) \
43 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
44
45 /* Get implementation for some internal functions. */
46 #include "../../resolv/mapv4v6addr.h"
47
48 static int
49 _nss_nisplus_parse_hostent (nis_result *result, int af, struct hostent *host,
50 char *buffer, size_t buflen)
51 {
52 unsigned int i;
53 char *first_unused = buffer;
54 size_t room_left = buflen;
55 char *data, *p, *line;
56
57 if (result == NULL)
58 return 0;
59
60 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
61 __type_of (result->objects.objects_val) != ENTRY_OBJ ||
62 strcmp(result->objects.objects_val[0].EN_data.en_type,
63 "hosts_tbl") != 0 ||
64 result->objects.objects_val[0].EN_data.en_cols.en_cols_len < 4)
65 return 0;
66
67 if (room_left < NISENTRYLEN (0, 2, result) + 1)
68 {
69 no_more_room:
70 __set_errno (ERANGE);
71 return -1;
72 }
73
74 data = first_unused;
75 if (inet_pton (af, NISENTRYVAL (0, 2, result), data) < 1)
76 /* Illegal address: ignore line. */
77 return 0;
78
79 host->h_addrtype = af;
80 if (af == AF_INET6)
81 host->h_length = IN6ADDRSZ;
82 else
83 {
84 if (_res.options & RES_USE_INET6)
85 {
86 map_v4v6_address (data, data);
87 host->h_addrtype = AF_INET6;
88 host->h_length = IN6ADDRSZ;
89 }
90 else
91 {
92 host->h_addrtype = AF_INET;
93 host->h_length = INADDRSZ;
94 }
95 }
96 first_unused+=host->h_length;
97 room_left-=host->h_length;
98
99 if (NISENTRYLEN (0, 0, result) + 1 > room_left)
100 goto no_more_room;
101
102 p = stpncpy (first_unused, NISENTRYVAL (0, 0, result),
103 NISENTRYLEN (0, 0, result));
104 *p = '\0';
105 room_left -= (NISENTRYLEN (0, 0, result) + 1);
106 host->h_name = first_unused;
107 first_unused += NISENTRYLEN (0, 0, result) +1;
108 p = first_unused;
109
110 line = p;
111 for (i = 0; i < result->objects.objects_len; i++)
112 {
113 if (strcmp (NISENTRYVAL (i, 1, result), host->h_name) != 0)
114 {
115 if (NISENTRYLEN (i, 1, result) + 2 > room_left)
116 goto no_more_room;
117
118 *p++ = ' ';
119 p = stpncpy (p, NISENTRYVAL (i, 1, result),
120 NISENTRYLEN (i, 1, result));
121 *p = '\0';
122 room_left -= (NISENTRYLEN (i, 1, result) + 1);
123 }
124 }
125 ++p;
126 first_unused = p;
127 /* Adjust the pointer so it is aligned for
128 storing pointers. */
129 first_unused += __alignof__ (char *) - 1;
130 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
131 host->h_addr_list = (char **) first_unused;
132 if (room_left < 2 * sizeof (char *))
133 goto no_more_room;
134
135 room_left -= (2 * sizeof (char *));
136 host->h_addr_list[0] = data;
137 host->h_addr_list[1] = NULL;
138 host->h_aliases = &host->h_addr_list[2];
139 host->h_aliases[0] = NULL;
140
141 i = 0;
142 while (*line != '\0')
143 {
144 /* Skip leading blanks. */
145 while (isspace (*line))
146 line++;
147
148 if (*line == '\0')
149 break;
150
151 if (room_left < sizeof (char *))
152 goto no_more_room;
153
154 room_left -= sizeof (char *);
155 host->h_aliases[i] = line;
156
157 while (*line != '\0' && *line != ' ')
158 ++line;
159
160 if (*line == ' ')
161 {
162 *line = '\0';
163 ++line;
164 ++i;
165 }
166 else
167 host->h_aliases[i+1] = NULL;
168 }
169 return 1;
170 }
171
172 static enum nss_status
173 _nss_create_tablename (void)
174 {
175 if (tablename_val == NULL)
176 {
177 char buf [40 + strlen (nis_local_directory ())];
178 char *p;
179
180 p = stpcpy (buf, "hosts.org_dir.");
181 p = stpcpy (p, nis_local_directory ());
182 tablename_val = strdup (buf);
183 if (tablename_val == NULL)
184 return NSS_STATUS_TRYAGAIN;
185 tablename_len = strlen (tablename_val);
186 }
187 return NSS_STATUS_SUCCESS;
188 }
189
190 enum nss_status
191 _nss_nisplus_sethostent (void)
192 {
193 enum nss_status status = NSS_STATUS_SUCCESS;
194
195 __libc_lock_lock (lock);
196
197 if (result)
198 nis_freeresult (result);
199 result = NULL;
200
201 if (tablename_val == NULL)
202 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
203 status = NSS_STATUS_UNAVAIL;
204
205 __libc_lock_unlock (lock);
206
207 return status;
208 }
209
210 enum nss_status
211 _nss_nisplus_endhostent (void)
212 {
213 __libc_lock_lock (lock);
214
215 if (result)
216 nis_freeresult (result);
217 result = NULL;
218
219 __libc_lock_unlock (lock);
220
221 return NSS_STATUS_SUCCESS;
222 }
223
224 static enum nss_status
225 internal_nisplus_gethostent_r (struct hostent *host, char *buffer,
226 size_t buflen, int *herrnop)
227 {
228 int parse_res;
229
230 /* Get the next entry until we found a correct one. */
231 do
232 {
233 nis_result *saved_res;
234
235 if (result == NULL)
236 {
237 saved_res = NULL;
238 if (tablename_val == NULL)
239 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
240 return NSS_STATUS_UNAVAIL;
241
242 result = nis_first_entry(tablename_val);
243 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
244 {
245 enum nss_status retval = niserr2nss (result->status);
246 if (retval == NSS_STATUS_TRYAGAIN)
247 {
248 *herrnop = NETDB_INTERNAL;
249 __set_errno (EAGAIN);
250 }
251 return retval;
252 }
253
254 }
255 else
256 {
257 nis_result *res2;
258
259 saved_res = result;
260 res2 = nis_next_entry(tablename_val, &result->cookie);
261 result = res2;
262 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
263 {
264 enum nss_status retval= niserr2nss (result->status);
265
266 nis_freeresult (result);
267 result = saved_res;
268 if (retval == NSS_STATUS_TRYAGAIN)
269 {
270 *herrnop = NETDB_INTERNAL;
271 __set_errno (EAGAIN);
272 }
273 return retval;
274 }
275 }
276
277 parse_res = _nss_nisplus_parse_hostent (result, AF_INET6,
278 host, buffer, buflen);
279 if (parse_res < 1 && errno != ERANGE)
280 parse_res = _nss_nisplus_parse_hostent (result, AF_INET, host,
281 buffer, buflen);
282 if (parse_res < 1 && errno == ERANGE)
283 {
284 nis_freeresult (result);
285 result = saved_res;
286 *herrnop = NETDB_INTERNAL;
287 return NSS_STATUS_TRYAGAIN;
288 }
289 if (saved_res != NULL)
290 nis_freeresult (saved_res);
291
292 } while (!parse_res);
293
294 return NSS_STATUS_SUCCESS;
295 }
296
297 enum nss_status
298 _nss_nisplus_gethostent_r (struct hostent *result, char *buffer,
299 size_t buflen, int *herrnop)
300 {
301 int status;
302
303 __libc_lock_lock (lock);
304
305 status = internal_nisplus_gethostent_r (result, buffer, buflen, herrnop);
306
307 __libc_lock_unlock (lock);
308
309 return status;
310 }
311
312 enum nss_status
313 _nss_nisplus_gethostbyname2_r (const char *name, int af, struct hostent *host,
314 char *buffer, size_t buflen, int *herrnop)
315 {
316 int parse_res, retval;
317
318 if (tablename_val == NULL)
319 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
320 {
321 *herrnop = NETDB_INTERNAL;
322 return NSS_STATUS_UNAVAIL;
323 }
324
325 if (name == NULL)
326 {
327 __set_errno (EINVAL);
328 *herrnop = NETDB_INTERNAL;
329 return NSS_STATUS_NOTFOUND;
330 }
331 else
332 {
333 nis_result *result;
334 char buf[strlen (name) + 255 + tablename_len];
335
336 /* Search at first in the alias list, and use the correct name
337 for the next search */
338 sprintf(buf, "[name=%s],%s", name, tablename_val);
339 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
340
341 /* If we do not find it, try it as original name. But if the
342 database is correct, we should find it in the first case, too */
343 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
344 __type_of (result->objects.objects_val) != ENTRY_OBJ ||
345 strcmp(result->objects.objects_val->EN_data.en_type,
346 "hosts_tbl") != 0 ||
347 result->objects.objects_val->EN_data.en_cols.en_cols_len < 3)
348 sprintf(buf, "[cname=%s],%s", name, tablename_val);
349 else
350 sprintf(buf, "[cname=%s],%s", NISENTRYVAL(0, 0, result),
351 tablename_val);
352
353 nis_freeresult (result);
354 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
355
356 retval = niserr2nss (result->status);
357 if (retval != NSS_STATUS_SUCCESS)
358 {
359 if (retval == NSS_STATUS_TRYAGAIN)
360 {
361 __set_errno (EAGAIN);
362 *herrnop = NETDB_INTERNAL;
363 }
364 nis_freeresult (result);
365 return retval;
366 }
367
368 parse_res =
369 _nss_nisplus_parse_hostent (result, af, host, buffer, buflen);
370
371 nis_freeresult (result);
372
373 if (parse_res > 0)
374 return NSS_STATUS_SUCCESS;
375
376 *herrnop = NETDB_INTERNAL;
377 if (parse_res == -1)
378 return NSS_STATUS_TRYAGAIN;
379 else
380 return NSS_STATUS_NOTFOUND;
381 }
382 }
383
384 enum nss_status
385 _nss_nisplus_gethostbyname_r (const char *name, struct hostent *host,
386 char *buffer, size_t buflen, int *h_errnop)
387 {
388 if (_res.options & RES_USE_INET6)
389 {
390 enum nss_status status;
391
392 status = _nss_nisplus_gethostbyname2_r (name, AF_INET6, host, buffer,
393 buflen, h_errnop);
394 if (status == NSS_STATUS_SUCCESS)
395 return status;
396 }
397
398 return _nss_nisplus_gethostbyname2_r (name, AF_INET, host, buffer,
399 buflen, h_errnop);
400 }
401
402 enum nss_status
403 _nss_nisplus_gethostbyaddr_r (const char *addr, int addrlen, int type,
404 struct hostent *host, char *buffer,
405 size_t buflen, int *herrnop)
406 {
407 if (tablename_val == NULL)
408 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
409 return NSS_STATUS_UNAVAIL;
410
411 if (addr == NULL)
412 return NSS_STATUS_NOTFOUND;
413 else
414 {
415 nis_result *result;
416 char buf[255 + tablename_len];
417 int retval, parse_res;
418
419 snprintf(buf, sizeof (buf) -1, "[addr=%s],%s",
420 inet_ntoa (*(struct in_addr *)addr), tablename_val);
421 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
422
423 retval = niserr2nss (result->status);
424 if (retval != NSS_STATUS_SUCCESS)
425 {
426 if (retval == NSS_STATUS_TRYAGAIN)
427 {
428 __set_errno (EAGAIN);
429 *herrnop = NETDB_INTERNAL;
430 }
431 nis_freeresult (result);
432 return retval;
433 }
434
435 parse_res = _nss_nisplus_parse_hostent (result, type, host,
436 buffer, buflen);
437 nis_freeresult (result);
438
439 if (parse_res > 0)
440 return NSS_STATUS_SUCCESS;
441
442 *herrnop = NETDB_INTERNAL;
443 if (parse_res == -1)
444 return NSS_STATUS_TRYAGAIN;
445 else
446 return NSS_STATUS_NOTFOUND;
447 }
448 }