1 /* Copyright (C) 1996-2020 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, see
17 <https://www.gnu.org/licenses/>. */
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;
45 _nss_nis_setnetent (int stayopen)
47 return _nss_nis_endnetent ();
51 _nss_nis_endnetent (void)
53 __libc_lock_lock (lock);
63 __libc_lock_unlock (lock);
65 return NSS_STATUS_SUCCESS;
67 libnss_nis_hidden_def (_nss_nis_endnetent)
69 static enum nss_status
70 internal_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
71 int *errnop, int *herrnop)
73 struct parser_data *data = (void *) buffer;
76 if (__glibc_unlikely (yp_get_default_domain (&domain)))
77 return NSS_STATUS_UNAVAIL;
79 /* Get the next entry until we found a correct one. */
90 yperr = yp_first (domain, "networks.byname", &outkey, &keylen, &result,
93 yperr = yp_next (domain, "networks.byname", oldkey, oldkeylen, &outkey,
94 &keylen, &result, &len);
96 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
98 enum nss_status retval = yperr2nss (yperr);
100 if (retval == NSS_STATUS_TRYAGAIN)
102 *herrnop = NETDB_INTERNAL;
108 if (__glibc_unlikely ((size_t) (len + 1) > buflen))
112 *herrnop = NETDB_INTERNAL;
113 return NSS_STATUS_TRYAGAIN;
116 char *p = strncpy (buffer, result, len);
122 parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
123 if (__glibc_unlikely (parse_res == -1))
126 *herrnop = NETDB_INTERNAL;
128 return NSS_STATUS_TRYAGAIN;
138 return NSS_STATUS_SUCCESS;
142 _nss_nis_getnetent_r (struct netent *net, char *buffer, size_t buflen,
143 int *errnop, int *herrnop)
145 enum nss_status status;
147 __libc_lock_lock (lock);
149 status = internal_nis_getnetent_r (net, buffer, buflen, errnop, herrnop);
151 __libc_lock_unlock (lock);
157 _nss_nis_getnetbyname_r (const char *name, struct netent *net, char *buffer,
158 size_t buflen, int *errnop, int *herrnop)
163 *herrnop = NETDB_INTERNAL;
164 return NSS_STATUS_UNAVAIL;
168 if (__glibc_unlikely (yp_get_default_domain (&domain)))
169 return NSS_STATUS_UNAVAIL;
171 struct parser_data *data = (void *) buffer;
172 if (buflen < sizeof *data + 1)
174 *herrnop = NETDB_INTERNAL;
176 return NSS_STATUS_TRYAGAIN;
179 /* Convert name to lowercase. */
180 size_t namlen = strlen (name);
181 /* Limit name length to the maximum size of an RPC packet. */
182 if (namlen > UDPMSGSIZE)
185 return NSS_STATUS_UNAVAIL;
188 char name2[namlen + 1];
191 for (i = 0; i < namlen; ++i)
192 name2[i] = _tolower (name[i]);
197 int yperr = yp_match (domain, "networks.byname", name2, namlen, &result,
200 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
202 enum nss_status retval = yperr2nss (yperr);
204 if (retval == NSS_STATUS_TRYAGAIN)
207 *herrnop = NETDB_INTERNAL;
212 if (__glibc_unlikely ((size_t) (len + 1) > buflen))
216 *herrnop = NETDB_INTERNAL;
217 return NSS_STATUS_TRYAGAIN;
220 char *p = strncpy (buffer, result, len);
226 int parse_res = _nss_files_parse_netent (p, net, data, buflen, errnop);
228 if (__glibc_unlikely (parse_res < 1))
230 *herrnop = NETDB_INTERNAL;
232 return NSS_STATUS_TRYAGAIN;
234 return NSS_STATUS_NOTFOUND;
237 return NSS_STATUS_SUCCESS;
241 _nss_nis_getnetbyaddr_r (uint32_t addr, int type, struct netent *net,
242 char *buffer, size_t buflen, int *errnop,
246 if (__glibc_unlikely (yp_get_default_domain (&domain)))
247 return NSS_STATUS_UNAVAIL;
249 struct in_addr in = { .s_addr = htonl (addr) };
250 char *buf = inet_ntoa (in);
251 size_t blen = strlen (buf);
258 int yperr = yp_match (domain, "networks.byaddr", buf, blen, &result,
261 if (__glibc_unlikely (yperr != YPERR_SUCCESS))
263 enum nss_status retval = yperr2nss (yperr);
265 if (retval == NSS_STATUS_NOTFOUND)
267 if (buf[blen - 2] == '.' && buf[blen - 1] == '0')
269 /* Try again, but with trailing dot(s)
270 removed (one by one) */
271 buf[blen - 2] = '\0';
276 return NSS_STATUS_NOTFOUND;
280 if (retval == NSS_STATUS_TRYAGAIN)
286 if (__glibc_unlikely ((size_t) (len + 1) > buflen))
290 *herrnop = NETDB_INTERNAL;
291 return NSS_STATUS_TRYAGAIN;
294 char *p = strncpy (buffer, result, len);
300 int parse_res = _nss_files_parse_netent (p, net, (void *) buffer,
303 if (__glibc_unlikely (parse_res < 1))
305 *herrnop = NETDB_INTERNAL;
307 return NSS_STATUS_TRYAGAIN;
309 return NSS_STATUS_NOTFOUND;
312 return NSS_STATUS_SUCCESS;