1 /* Copyright (C) 1996-2003, 2004 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
27 #include <netinet/ether.h>
28 #include <netinet/if_ether.h>
32 /* Protect global state against multiple changers */
33 __libc_lock_define_initialized (static, lock
)
35 /* Get the declaration of the parser function. */
36 #define ENTNAME etherent
37 #define STRUCTURE etherent
39 #include <nss/nss_files/files-parse.c>
43 struct response
*next
;
47 static struct response
*start
;
48 static struct response
*next
;
51 saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
52 int invallen
, char *indata
)
54 if (instatus
!= YP_TRUE
)
57 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
59 struct response
*newp
= malloc (sizeof (struct response
) + invallen
+ 1);
61 return 1; /* We have no error code for out of memory */
70 *((char *) mempcpy (newp
->val
, inval
, invallen
)) = '\0';
77 internal_nis_endetherent (void)
88 _nss_nis_endetherent (void)
90 __libc_lock_lock (lock
);
92 internal_nis_endetherent ();
95 __libc_lock_unlock (lock
);
97 return NSS_STATUS_SUCCESS
;
100 static enum nss_status
101 internal_nis_setetherent (void)
104 struct ypall_callback ypcb
;
105 enum nss_status status
;
107 yp_get_default_domain (&domainname
);
109 internal_nis_endetherent ();
111 ypcb
.foreach
= saveit
;
113 status
= yperr2nss (yp_all (domainname
, "ethers.byname", &ypcb
));
120 _nss_nis_setetherent (int stayopen
)
122 enum nss_status result
;
124 __libc_lock_lock (lock
);
126 result
= internal_nis_setetherent ();
128 __libc_lock_unlock (lock
);
133 static enum nss_status
134 internal_nis_getetherent_r (struct etherent
*eth
, char *buffer
, size_t buflen
,
137 struct parser_data
*data
= (void *) buffer
;
141 internal_nis_setetherent ();
143 /* Get the next entry until we found a correct one. */
149 return NSS_STATUS_NOTFOUND
;
151 p
= strncpy (buffer
, next
->val
, buflen
);
156 parse_res
= _nss_files_parse_etherent (p
, eth
, data
, buflen
, errnop
);
158 return NSS_STATUS_TRYAGAIN
;
163 return NSS_STATUS_SUCCESS
;
167 _nss_nis_getetherent_r (struct etherent
*result
, char *buffer
, size_t buflen
,
172 __libc_lock_lock (lock
);
174 status
= internal_nis_getetherent_r (result
, buffer
, buflen
, errnop
);
176 __libc_lock_unlock (lock
);
182 _nss_nis_gethostton_r (const char *name
, struct etherent
*eth
,
183 char *buffer
, size_t buflen
, int *errnop
)
185 struct parser_data
*data
= (void *) buffer
;
186 enum nss_status retval
;
187 char *domain
, *result
, *p
;
193 return NSS_STATUS_UNAVAIL
;
196 if (yp_get_default_domain (&domain
))
197 return NSS_STATUS_UNAVAIL
;
199 retval
= yperr2nss (yp_match (domain
, "ethers.byname", name
,
200 strlen (name
), &result
, &len
));
202 if (retval
!= NSS_STATUS_SUCCESS
)
204 if (retval
== NSS_STATUS_TRYAGAIN
)
209 if ((size_t) (len
+ 1) > buflen
)
213 return NSS_STATUS_TRYAGAIN
;
216 p
= strncpy (buffer
, result
, len
);
222 parse_res
= _nss_files_parse_etherent (p
, eth
, data
, buflen
, errnop
);
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
)
237 struct parser_data
*data
= (void *) buffer
;
238 enum nss_status retval
;
239 char *domain
, *result
, *p
;
240 int len
, nlen
, parse_res
;
246 return NSS_STATUS_UNAVAIL
;
249 if (yp_get_default_domain (&domain
))
250 return NSS_STATUS_UNAVAIL
;
252 nlen
= sprintf (buf
, "%x:%x:%x:%x:%x:%x",
253 (int) addr
->ether_addr_octet
[0],
254 (int) addr
->ether_addr_octet
[1],
255 (int) addr
->ether_addr_octet
[2],
256 (int) addr
->ether_addr_octet
[3],
257 (int) addr
->ether_addr_octet
[4],
258 (int) addr
->ether_addr_octet
[5]);
260 retval
= yperr2nss (yp_match (domain
, "ethers.byaddr", buf
,
261 nlen
, &result
, &len
));
263 if (retval
!= NSS_STATUS_SUCCESS
)
265 if (retval
== NSS_STATUS_TRYAGAIN
)
270 if ((size_t) (len
+ 1) > buflen
)
274 return NSS_STATUS_TRYAGAIN
;
277 p
= strncpy (buffer
, result
, len
);
283 parse_res
= _nss_files_parse_etherent (p
, eth
, data
, buflen
, errnop
);
287 return NSS_STATUS_TRYAGAIN
;
289 return NSS_STATUS_NOTFOUND
;
291 return NSS_STATUS_SUCCESS
;