1 /* Copyright (C) 1996-2015 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 <http://www.gnu.org/licenses/>. */
20 /* The following is an ugly trick to avoid a prototype declaration for
22 #define _nss_nis_endnetent _nss_nis_endnetent_XXX
24 #undef _nss_nis_endnetent
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <bits/libc-lock.h>
32 #include <rpcsvc/yp.h>
33 #include <rpcsvc/ypclnt.h>
37 /* Get the declaration of the parser function. */
38 #define ENTNAME netent
40 #include <nss/nss_files/files-parse.c>
42 __libc_lock_define_initialized (static, lock
)
44 static bool_t new_start
= 1;
49 _nss_nis_setnetent (int stayopen
)
51 __libc_lock_lock (lock
);
61 __libc_lock_unlock (lock
);
63 return NSS_STATUS_SUCCESS
;
65 /* Make _nss_nis_endnetent an alias of _nss_nis_setnetent. We do this
66 even though the prototypes don't match. The argument of setnetent
67 is not used so this makes no difference. */
68 strong_alias (_nss_nis_setnetent
, _nss_nis_endnetent
)
70 static enum nss_status
71 internal_nis_getnetent_r (struct netent
*net
, char *buffer
, size_t buflen
,
72 int *errnop
, int *herrnop
)
74 struct parser_data
*data
= (void *) buffer
;
77 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
78 return NSS_STATUS_UNAVAIL
;
80 /* Get the next entry until we found a correct one. */
91 yperr
= yp_first (domain
, "networks.byname", &outkey
, &keylen
, &result
,
94 yperr
= yp_next (domain
, "networks.byname", oldkey
, oldkeylen
, &outkey
,
95 &keylen
, &result
, &len
);
97 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
99 enum nss_status retval
= yperr2nss (yperr
);
101 if (retval
== NSS_STATUS_TRYAGAIN
)
103 *herrnop
= NETDB_INTERNAL
;
109 if (__glibc_unlikely ((size_t) (len
+ 1) > buflen
))
113 *herrnop
= NETDB_INTERNAL
;
114 return NSS_STATUS_TRYAGAIN
;
117 char *p
= strncpy (buffer
, result
, len
);
123 parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
124 if (__glibc_unlikely (parse_res
== -1))
127 *herrnop
= NETDB_INTERNAL
;
129 return NSS_STATUS_TRYAGAIN
;
139 return NSS_STATUS_SUCCESS
;
143 _nss_nis_getnetent_r (struct netent
*net
, char *buffer
, size_t buflen
,
144 int *errnop
, int *herrnop
)
146 enum nss_status status
;
148 __libc_lock_lock (lock
);
150 status
= internal_nis_getnetent_r (net
, buffer
, buflen
, errnop
, herrnop
);
152 __libc_lock_unlock (lock
);
158 _nss_nis_getnetbyname_r (const char *name
, struct netent
*net
, char *buffer
,
159 size_t buflen
, int *errnop
, int *herrnop
)
164 *herrnop
= NETDB_INTERNAL
;
165 return NSS_STATUS_UNAVAIL
;
169 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
170 return NSS_STATUS_UNAVAIL
;
172 struct parser_data
*data
= (void *) buffer
;
173 if (buflen
< sizeof *data
+ 1)
175 *herrnop
= NETDB_INTERNAL
;
177 return NSS_STATUS_TRYAGAIN
;
180 /* Convert name to lowercase. */
181 size_t namlen
= strlen (name
);
182 /* Limit name length to the maximum size of an RPC packet. */
183 if (namlen
> UDPMSGSIZE
)
186 return NSS_STATUS_UNAVAIL
;
189 char name2
[namlen
+ 1];
192 for (i
= 0; i
< namlen
; ++i
)
193 name2
[i
] = _tolower (name
[i
]);
198 int yperr
= yp_match (domain
, "networks.byname", name2
, namlen
, &result
,
201 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
203 enum nss_status retval
= yperr2nss (yperr
);
205 if (retval
== NSS_STATUS_TRYAGAIN
)
208 *herrnop
= NETDB_INTERNAL
;
213 if (__glibc_unlikely ((size_t) (len
+ 1) > buflen
))
217 *herrnop
= NETDB_INTERNAL
;
218 return NSS_STATUS_TRYAGAIN
;
221 char *p
= strncpy (buffer
, result
, len
);
227 int parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
229 if (__glibc_unlikely (parse_res
< 1))
231 *herrnop
= NETDB_INTERNAL
;
233 return NSS_STATUS_TRYAGAIN
;
235 return NSS_STATUS_NOTFOUND
;
238 return NSS_STATUS_SUCCESS
;
242 _nss_nis_getnetbyaddr_r (uint32_t addr
, int type
, struct netent
*net
,
243 char *buffer
, size_t buflen
, int *errnop
,
247 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
248 return NSS_STATUS_UNAVAIL
;
250 struct in_addr in
= { .s_addr
= htonl (addr
) };
251 char *buf
= inet_ntoa (in
);
252 size_t blen
= strlen (buf
);
259 int yperr
= yp_match (domain
, "networks.byaddr", buf
, blen
, &result
,
262 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
264 enum nss_status retval
= yperr2nss (yperr
);
266 if (retval
== NSS_STATUS_NOTFOUND
)
268 if (buf
[blen
- 2] == '.' && buf
[blen
- 1] == '0')
270 /* Try again, but with trailing dot(s)
271 removed (one by one) */
272 buf
[blen
- 2] = '\0';
277 return NSS_STATUS_NOTFOUND
;
281 if (retval
== NSS_STATUS_TRYAGAIN
)
287 if (__glibc_unlikely ((size_t) (len
+ 1) > buflen
))
291 *herrnop
= NETDB_INTERNAL
;
292 return NSS_STATUS_TRYAGAIN
;
295 char *p
= strncpy (buffer
, result
, len
);
301 int parse_res
= _nss_files_parse_netent (p
, net
, (void *) buffer
,
304 if (__glibc_unlikely (parse_res
< 1))
306 *herrnop
= NETDB_INTERNAL
;
308 return NSS_STATUS_TRYAGAIN
;
310 return NSS_STATUS_NOTFOUND
;
313 return NSS_STATUS_SUCCESS
;