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@suse.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/>. */
23 #include <bits/libc-lock.h>
24 #include <rpcsvc/yp.h>
25 #include <rpcsvc/ypclnt.h>
26 #include <netinet/ether.h>
27 #include <netinet/if_ether.h>
31 /* Protect global state against multiple changers */
32 __libc_lock_define_initialized (static, lock
)
34 /* Get the declaration of the parser function. */
35 #define ENTNAME etherent
36 #define STRUCTURE etherent
38 #include <nss/nss_files/files-parse.c>
42 struct response
*next
;
46 static struct response
*start
;
47 static struct response
*next
;
50 saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
51 int invallen
, char *indata
)
53 if (instatus
!= YP_TRUE
)
56 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
58 struct response
*newp
= malloc (sizeof (struct response
) + invallen
+ 1);
60 return 1; /* We have no error code for out of memory */
69 *((char *) mempcpy (newp
->val
, inval
, invallen
)) = '\0';
76 internal_nis_endetherent (void)
87 _nss_nis_endetherent (void)
89 __libc_lock_lock (lock
);
91 internal_nis_endetherent ();
94 __libc_lock_unlock (lock
);
96 return NSS_STATUS_SUCCESS
;
99 static enum nss_status
100 internal_nis_setetherent (void)
103 struct ypall_callback ypcb
;
104 enum nss_status status
;
106 yp_get_default_domain (&domainname
);
108 internal_nis_endetherent ();
110 ypcb
.foreach
= saveit
;
112 status
= yperr2nss (yp_all (domainname
, "ethers.byname", &ypcb
));
119 _nss_nis_setetherent (int stayopen
)
121 enum nss_status result
;
123 __libc_lock_lock (lock
);
125 result
= internal_nis_setetherent ();
127 __libc_lock_unlock (lock
);
132 static enum nss_status
133 internal_nis_getetherent_r (struct etherent
*eth
, char *buffer
, size_t buflen
,
136 struct parser_data
*data
= (void *) buffer
;
140 internal_nis_setetherent ();
142 /* Get the next entry until we found a correct one. */
148 return NSS_STATUS_NOTFOUND
;
150 p
= strncpy (buffer
, next
->val
, buflen
);
155 parse_res
= _nss_files_parse_etherent (p
, eth
, data
, buflen
, errnop
);
157 return NSS_STATUS_TRYAGAIN
;
162 return NSS_STATUS_SUCCESS
;
166 _nss_nis_getetherent_r (struct etherent
*result
, char *buffer
, size_t buflen
,
171 __libc_lock_lock (lock
);
173 status
= internal_nis_getetherent_r (result
, buffer
, buflen
, errnop
);
175 __libc_lock_unlock (lock
);
181 _nss_nis_gethostton_r (const char *name
, struct etherent
*eth
,
182 char *buffer
, size_t buflen
, int *errnop
)
187 return NSS_STATUS_UNAVAIL
;
191 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
192 return NSS_STATUS_UNAVAIL
;
196 int yperr
= yp_match (domain
, "ethers.byname", name
, strlen (name
), &result
,
199 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
201 enum nss_status retval
= yperr2nss (yperr
);
203 if (retval
== NSS_STATUS_TRYAGAIN
)
208 if (__glibc_unlikely ((size_t) (len
+ 1) > buflen
))
212 return NSS_STATUS_TRYAGAIN
;
215 char *p
= strncpy (buffer
, result
, len
);
221 int parse_res
= _nss_files_parse_etherent (p
, eth
, (void *) buffer
, buflen
,
223 if (__glibc_unlikely (parse_res
< 1))
226 return NSS_STATUS_TRYAGAIN
;
228 return NSS_STATUS_NOTFOUND
;
230 return NSS_STATUS_SUCCESS
;
234 _nss_nis_getntohost_r (const struct ether_addr
*addr
, struct etherent
*eth
,
235 char *buffer
, size_t buflen
, int *errnop
)
240 return NSS_STATUS_UNAVAIL
;
244 if (__glibc_unlikely (yp_get_default_domain (&domain
)))
245 return NSS_STATUS_UNAVAIL
;
248 int nlen
= snprintf (buf
, sizeof (buf
), "%x:%x:%x:%x:%x:%x",
249 (int) addr
->ether_addr_octet
[0],
250 (int) addr
->ether_addr_octet
[1],
251 (int) addr
->ether_addr_octet
[2],
252 (int) addr
->ether_addr_octet
[3],
253 (int) addr
->ether_addr_octet
[4],
254 (int) addr
->ether_addr_octet
[5]);
258 int yperr
= yp_match (domain
, "ethers.byaddr", buf
, nlen
, &result
, &len
);
260 if (__glibc_unlikely (yperr
!= YPERR_SUCCESS
))
262 enum nss_status retval
= yperr2nss (yperr
);
264 if (retval
== NSS_STATUS_TRYAGAIN
)
269 if (__glibc_unlikely ((size_t) (len
+ 1) > buflen
))
273 return NSS_STATUS_TRYAGAIN
;
276 char *p
= strncpy (buffer
, result
, len
);
282 int parse_res
= _nss_files_parse_etherent (p
, eth
, (void *) buffer
, buflen
,
284 if (__glibc_unlikely (parse_res
< 1))
287 return NSS_STATUS_TRYAGAIN
;
289 return NSS_STATUS_NOTFOUND
;
291 return NSS_STATUS_SUCCESS
;