1 /* Copyright (C) 1996-2000, 2001 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.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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 Lesser General Public License for more details.
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
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <bits/libc-lock.h>
29 #include <rpcsvc/yp.h>
30 #include <rpcsvc/ypclnt.h>
34 /* Get the declaration of the parser function. */
35 #define ENTNAME netent
37 #include <nss/nss_files/files-parse.c>
39 __libc_lock_define_initialized (static, lock)
41 static bool_t new_start = 1;
46 _nss_nis_setnetent (int stayopen)
48 __libc_lock_lock (lock);
58 __libc_lock_unlock (lock);
60 return NSS_STATUS_SUCCESS;
64 _nss_nis_endnetent (void)
66 __libc_lock_lock (lock);
76 __libc_lock_unlock (lock);
78 return NSS_STATUS_SUCCESS;
81 static enum nss_status
82 internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
83 int *errnop, int *herrnop)
85 struct parser_data *data = (void *) buffer;
86 char *domain, *result, *outkey;
87 int len, keylen, parse_res;
89 if (yp_get_default_domain (&domain))
90 return NSS_STATUS_UNAVAIL;
92 /* Get the next entry until we found a correct one. */
95 enum nss_status retval;
99 retval = yperr2nss (yp_first (domain, "networks.byname",
100 &outkey, &keylen, &result, &len));
102 retval = yperr2nss ( yp_next (domain, "networks.byname",
104 &outkey, &keylen, &result, &len));
106 if (retval != NSS_STATUS_SUCCESS)
108 if (retval == NSS_STATUS_NOTFOUND)
110 else if (retval == NSS_STATUS_TRYAGAIN)
112 *herrnop = NETDB_INTERNAL;
118 if ((size_t) (len + 1) > buflen)
122 *herrnop = NETDB_INTERNAL;
123 return NSS_STATUS_TRYAGAIN;
126 p = strncpy (buffer, result, len);
132 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
136 *herrnop = NETDB_INTERNAL;
138 return NSS_STATUS_TRYAGAIN;
148 return NSS_STATUS_SUCCESS;
152 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
153 int *errnop, int *herrnop)
155 enum nss_status status;
157 __libc_lock_lock (lock);
159 status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop);
161 __libc_lock_unlock (lock);
167 _nss_nis_getnetbyname_r (const char *name, struct netent *net, char *buffer,
168 size_t buflen, int *errnop, int *herrnop)
170 enum nss_status retval;
171 struct parser_data *data = (void *) buffer;
172 char *domain, *result, *p;
178 *herrnop = NETDB_INTERNAL;
179 return NSS_STATUS_UNAVAIL;
182 if (yp_get_default_domain (&domain))
183 return NSS_STATUS_UNAVAIL;
185 if (buflen < sizeof *data + 1)
187 *herrnop = NETDB_INTERNAL;
189 return NSS_STATUS_TRYAGAIN;
193 /* Convert name to lowercase. */
194 size_t namlen = strlen (name);
195 char name2[namlen + 1];
198 for (i = 0; i < namlen; ++i)
199 name2[i] = _tolower (name[i]);
202 retval = yperr2nss (yp_match (domain, "networks.byname", name2,
203 namlen, &result, &len));
207 if (retval != NSS_STATUS_SUCCESS)
209 if (retval == NSS_STATUS_NOTFOUND)
211 else if (retval == NSS_STATUS_TRYAGAIN)
214 *herrnop = NETDB_INTERNAL;
219 if ((size_t) (len + 1) > buflen)
223 *herrnop = NETDB_INTERNAL;
224 return NSS_STATUS_TRYAGAIN;
227 p = strncpy (buffer, result, len);
233 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
237 *herrnop = NETDB_INTERNAL;
239 return NSS_STATUS_TRYAGAIN;
243 return NSS_STATUS_NOTFOUND;
247 return NSS_STATUS_SUCCESS;
251 _nss_nis_getnetbyaddr_r (uint32_t addr, int type, struct netent *net,
252 char *buffer, size_t buflen, int *errnop,
255 struct parser_data *data = (void *) buffer;
264 if (yp_get_default_domain (&domain))
265 return NSS_STATUS_UNAVAIL;
267 in = inet_makeaddr (addr, 0);
268 strcpy (buf, inet_ntoa (in));
273 enum nss_status retval;
276 retval = yperr2nss (yp_match (domain, "networks.byaddr", buf,
277 strlen (buf), &result, &len));
279 if (retval != NSS_STATUS_SUCCESS)
281 if (retval == NSS_STATUS_NOTFOUND)
283 if (buf[blen - 2] == '.' && buf[blen - 1] == '0')
285 /* Try again, but with trailing dot(s)
286 removed (one by one) */
287 buf[blen - 2] = '\0';
294 return NSS_STATUS_NOTFOUND;
299 if (retval == NSS_STATUS_TRYAGAIN)
305 if ((size_t) (len + 1) > buflen)
309 *herrnop = NETDB_INTERNAL;
310 return NSS_STATUS_TRYAGAIN;
313 p = strncpy (buffer, result, len);
319 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
323 *herrnop = NETDB_INTERNAL;
325 return NSS_STATUS_TRYAGAIN;
329 return NSS_STATUS_NOTFOUND;
333 return NSS_STATUS_SUCCESS;