1 /* Copyright (C) 1996-2003, 2006 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
21 /* The following is an ugly trick to avoid a prototype declaration for
23 #define _nss_nis_endnetent _nss_nis_endnetent_XXX
25 #undef _nss_nis_endnetent
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <bits/libc-lock.h>
33 #include <rpcsvc/yp.h>
34 #include <rpcsvc/ypclnt.h>
38 /* Get the declaration of the parser function. */
39 #define ENTNAME netent
41 #include <nss/nss_files/files-parse.c>
43 __libc_lock_define_initialized (static, lock
)
45 static bool_t new_start
= 1;
50 _nss_nis_setnetent (int stayopen
)
52 __libc_lock_lock (lock
);
62 __libc_lock_unlock (lock
);
64 return NSS_STATUS_SUCCESS
;
66 /* Make _nss_nis_endnetent an alias of _nss_nis_setnetent. We do this
67 even though the prototypes don't match. The argument of setnetent
68 is not used so this makes no difference. */
69 strong_alias (_nss_nis_setnetent
, _nss_nis_endnetent
)
71 static enum nss_status
72 internal_nis_getnetent_r (struct netent
*net
, char *buffer
, size_t buflen
,
73 int *errnop
, int *herrnop
)
75 struct parser_data
*data
= (void *) buffer
;
78 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
79 return NSS_STATUS_UNAVAIL
;
81 /* Get the next entry until we found a correct one. */
92 yperr
= yp_first (domain
, "networks.byname", &outkey
, &keylen
, &result
,
95 yperr
= yp_next (domain
, "networks.byname", oldkey
, oldkeylen
, &outkey
,
96 &keylen
, &result
, &len
);
98 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
100 enum nss_status retval
= yperr2nss (yperr
);
102 if (retval
== NSS_STATUS_TRYAGAIN
)
104 *herrnop
= NETDB_INTERNAL
;
110 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
114 *herrnop
= NETDB_INTERNAL
;
115 return NSS_STATUS_TRYAGAIN
;
118 char *p
= strncpy (buffer
, result
, len
);
124 parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
125 if (__builtin_expect (parse_res
== -1, 0))
128 *herrnop
= NETDB_INTERNAL
;
130 return NSS_STATUS_TRYAGAIN
;
140 return NSS_STATUS_SUCCESS
;
144 _nss_nis_getnetent_r (struct netent
*net
, char *buffer
, size_t buflen
,
145 int *errnop
, int *herrnop
)
147 enum nss_status status
;
149 __libc_lock_lock (lock
);
151 status
= internal_nis_getnetent_r (net
, buffer
, buflen
, errnop
, herrnop
);
153 __libc_lock_unlock (lock
);
159 _nss_nis_getnetbyname_r (const char *name
, struct netent
*net
, char *buffer
,
160 size_t buflen
, int *errnop
, int *herrnop
)
165 *herrnop
= NETDB_INTERNAL
;
166 return NSS_STATUS_UNAVAIL
;
170 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
171 return NSS_STATUS_UNAVAIL
;
173 struct parser_data
*data
= (void *) buffer
;
174 if (buflen
< sizeof *data
+ 1)
176 *herrnop
= NETDB_INTERNAL
;
178 return NSS_STATUS_TRYAGAIN
;
181 /* Convert name to lowercase. */
182 size_t namlen
= strlen (name
);
183 char name2
[namlen
+ 1];
186 for (i
= 0; i
< namlen
; ++i
)
187 name2
[i
] = _tolower (name
[i
]);
192 int yperr
= yp_match (domain
, "networks.byname", name2
, namlen
, &result
,
195 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
197 enum nss_status retval
= yperr2nss (yperr
);
199 if (retval
== NSS_STATUS_TRYAGAIN
)
202 *herrnop
= NETDB_INTERNAL
;
207 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
211 *herrnop
= NETDB_INTERNAL
;
212 return NSS_STATUS_TRYAGAIN
;
215 char *p
= strncpy (buffer
, result
, len
);
221 int parse_res
= _nss_files_parse_netent (p
, net
, data
, buflen
, errnop
);
223 if (__builtin_expect (parse_res
< 1, 0))
225 *herrnop
= NETDB_INTERNAL
;
227 return NSS_STATUS_TRYAGAIN
;
229 return NSS_STATUS_NOTFOUND
;
232 return NSS_STATUS_SUCCESS
;
236 _nss_nis_getnetbyaddr_r (uint32_t addr
, int type
, struct netent
*net
,
237 char *buffer
, size_t buflen
, int *errnop
,
241 if (__builtin_expect (yp_get_default_domain (&domain
), 0))
242 return NSS_STATUS_UNAVAIL
;
244 struct in_addr in
= { .s_addr
= htonl (addr
) };
245 char *buf
= inet_ntoa (in
);
246 size_t blen
= strlen (buf
);
253 int yperr
= yp_match (domain
, "networks.byaddr", buf
, blen
, &result
,
256 if (__builtin_expect (yperr
!= YPERR_SUCCESS
, 0))
258 enum nss_status retval
= yperr2nss (yperr
);
260 if (retval
== NSS_STATUS_NOTFOUND
)
262 if (buf
[blen
- 2] == '.' && buf
[blen
- 1] == '0')
264 /* Try again, but with trailing dot(s)
265 removed (one by one) */
266 buf
[blen
- 2] = '\0';
271 return NSS_STATUS_NOTFOUND
;
275 if (retval
== NSS_STATUS_TRYAGAIN
)
281 if (__builtin_expect ((size_t) (len
+ 1) > buflen
, 0))
285 *herrnop
= NETDB_INTERNAL
;
286 return NSS_STATUS_TRYAGAIN
;
289 char *p
= strncpy (buffer
, result
, len
);
295 int parse_res
= _nss_files_parse_netent (p
, net
, (void *) buffer
,
298 if (__builtin_expect (parse_res
< 1, 0))
300 *herrnop
= NETDB_INTERNAL
;
302 return NSS_STATUS_TRYAGAIN
;
304 return NSS_STATUS_NOTFOUND
;
307 return NSS_STATUS_SUCCESS
;