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.
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.
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.
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. */
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <libc-lock.h>
28 #include <rpcsvc/yp.h>
29 #include <rpcsvc/ypclnt.h>
33 /* Get the declaration of the parser function. */
34 #define ENTNAME netent
36 #include <nss/nss_files/files-parse.c>
38 __libc_lock_define_initialized (static, lock)
40 static bool_t new_start = 1;
41 static char *oldkey = NULL;
42 static int oldkeylen = 0;
45 _nss_nis_setnetent (void)
47 __libc_lock_lock (lock);
57 __libc_lock_unlock (lock);
59 return NSS_STATUS_SUCCESS;
63 _nss_nis_endnetent (void)
65 __libc_lock_lock (lock);
75 __libc_lock_unlock (lock);
77 return NSS_STATUS_SUCCESS;
80 static enum nss_status
81 internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
84 struct parser_data *data = (void *) buffer;
85 char *domain, *result, *outkey;
86 int len, keylen, parse_res;
88 if (yp_get_default_domain (&domain))
89 return NSS_STATUS_UNAVAIL;
91 /* Get the next entry until we found a correct one. */
94 enum nss_status retval;
98 retval = yperr2nss (yp_first (domain, "networks.byname",
99 &outkey, &keylen, &result, &len));
101 retval = yperr2nss ( yp_next (domain, "networks.byname",
103 &outkey, &keylen, &result, &len));
105 if (retval != NSS_STATUS_SUCCESS)
107 if (retval == NSS_STATUS_TRYAGAIN)
109 *herrnop = NETDB_INTERNAL;
110 __set_errno (EAGAIN);
115 if ((size_t) (len + 1) > buflen)
118 __set_errno (ERANGE);
119 *herrnop = NETDB_INTERNAL;
120 return NSS_STATUS_TRYAGAIN;
123 p = strncpy (buffer, result, len);
129 parse_res = _nss_files_parse_netent (p, net, data, buflen);
130 if (!parse_res && errno == ERANGE)
132 *herrnop = NETDB_INTERNAL;
133 return NSS_STATUS_TRYAGAIN;
143 return NSS_STATUS_SUCCESS;
147 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
150 enum nss_status status;
152 __libc_lock_lock (lock);
154 status = internal_nis_getnetent_r (net, buffer, buflen, herrnop);
156 __libc_lock_unlock (lock);
162 _nss_nis_getnetbyname_r (const char *name, struct netent *net,
163 char *buffer, size_t buflen, int *herrnop)
165 enum nss_status retval;
166 struct parser_data *data = (void *) buffer;
167 char *domain, *result, *p;
172 __set_errno (EINVAL);
173 *herrnop = NETDB_INTERNAL;
174 return NSS_STATUS_UNAVAIL;
177 if (yp_get_default_domain (&domain))
178 return NSS_STATUS_UNAVAIL;
180 retval = yperr2nss (yp_match (domain, "networks.byname", name,
181 strlen (name), &result, &len));
183 if (retval != NSS_STATUS_SUCCESS)
185 if (retval == NSS_STATUS_TRYAGAIN)
187 __set_errno (EAGAIN);
188 *herrnop = NETDB_INTERNAL;
193 if ((size_t) (len + 1) > buflen)
196 __set_errno (ERANGE);
197 *herrnop = NETDB_INTERNAL;
198 return NSS_STATUS_TRYAGAIN;
201 p = strncpy (buffer, result, len);
207 parse_res = _nss_files_parse_netent (p, net, data, buflen);
211 *herrnop = NETDB_INTERNAL;
213 return NSS_STATUS_TRYAGAIN;
215 return NSS_STATUS_NOTFOUND;
218 return NSS_STATUS_SUCCESS;
222 _nss_nis_getnetbyaddr_r (unsigned long addr, int type, struct netent *net,
223 char *buffer, size_t buflen, int *herrnop)
225 struct parser_data *data = (void *) buffer;
234 if (yp_get_default_domain (&domain))
235 return NSS_STATUS_UNAVAIL;
237 in = inet_makeaddr (addr, 0);
238 strcpy (buf, inet_ntoa (in));
243 enum nss_status retval;
246 retval = yperr2nss (yp_match (domain, "networks.byaddr", buf,
247 strlen (buf), &result, &len));
249 if (retval != NSS_STATUS_SUCCESS)
251 if (retval == NSS_STATUS_NOTFOUND)
253 if (buf[blen - 2] == '.' && buf[blen - 1] == '0')
255 /* Try again, but with trailing dot(s)
256 removed (one by one) */
257 buf[blen - 2] = '\0';
262 return NSS_STATUS_NOTFOUND;
266 if (retval == NSS_STATUS_TRYAGAIN)
267 __set_errno (EAGAIN);
272 if ((size_t) (len + 1) > buflen)
275 __set_errno (ERANGE);
276 *herrnop = NETDB_INTERNAL;
277 return NSS_STATUS_TRYAGAIN;
280 p = strncpy (buffer, result, len);
286 parse_res = _nss_files_parse_netent (p, net, data, buflen);
291 *herrnop = NETDB_INTERNAL;
293 return NSS_STATUS_TRYAGAIN;
295 return NSS_STATUS_NOTFOUND;
298 return NSS_STATUS_SUCCESS;