1 /* Copyright (C) 1996, 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
27 #include <netinet/if_ether.h>
31 /* Protect global state against multiple changers */
32 __libc_lock_define_initialized (static, lock
)
37 struct ether_addr e_addr
;
40 /* Get the declaration of the parser function. */
41 #define ENTNAME etherent
42 #define STRUCTURE ether
44 #include <nss/nss_files/files-parse.c>
49 struct response
*next
;
52 static struct response
*start
= NULL
;
53 static struct response
*next
= NULL
;
56 saveit (int instatus
, char *inkey
, int inkeylen
, char *inval
,
57 int invallen
, char *indata
)
59 if (instatus
!= YP_TRUE
)
62 if (inkey
&& inkeylen
> 0 && inval
&& invallen
> 0)
66 start
= malloc (sizeof (struct response
));
71 next
->next
= malloc (sizeof (struct response
));
75 next
->val
= malloc (invallen
+ 1);
76 strncpy (next
->val
, inval
, invallen
);
77 next
->val
[invallen
] = '\0';
83 static enum nss_status
84 internal_nis_setetherent (void)
87 struct ypall_callback ypcb
;
88 enum nss_status status
;
90 yp_get_default_domain (&domainname
);
94 if (start
->val
!= NULL
)
102 ypcb
.foreach
= saveit
;
104 status
= yperr2nss (yp_all (domainname
, "ethers.byname", &ypcb
));
111 _nss_nis_setetherent (void)
113 enum nss_status result
;
115 __libc_lock_lock (lock
);
117 result
= internal_nis_setetherent ();
119 __libc_lock_unlock (lock
);
125 _nss_nis_endetherent (void)
127 __libc_lock_lock (lock
);
129 while (start
!= NULL
)
131 if (start
->val
!= NULL
)
140 __libc_lock_unlock (lock
);
142 return NSS_STATUS_SUCCESS
;
145 static enum nss_status
146 internal_nis_getetherent_r (struct ether
*eth
, char *buffer
, size_t buflen
,
149 struct parser_data
*data
= (void *) buffer
;
153 internal_nis_setetherent ();
155 /* Get the next entry until we found a correct one. */
161 return NSS_STATUS_NOTFOUND
;
162 p
= strncpy (buffer
, next
->val
, buflen
);
167 parse_res
= _nss_files_parse_etherent (p
, eth
, data
, buflen
, errnop
);
169 return NSS_STATUS_TRYAGAIN
;
174 return NSS_STATUS_SUCCESS
;
178 _nss_nis_getetherent_r (struct ether
*result
, char *buffer
, size_t buflen
,
183 __libc_lock_lock (lock
);
185 status
= internal_nis_getetherent_r (result
, buffer
, buflen
, errnop
);
187 __libc_lock_unlock (lock
);
193 _nss_nis_gethostton_r (const char *name
, struct ether
*eth
,
194 char *buffer
, size_t buflen
, int *errnop
)
196 struct parser_data
*data
= (void *) buffer
;
197 enum nss_status retval
;
198 char *domain
, *result
, *p
;
204 return NSS_STATUS_UNAVAIL
;
207 if (yp_get_default_domain (&domain
))
208 return NSS_STATUS_UNAVAIL
;
210 retval
= yperr2nss (yp_match (domain
, "ethers.byname", name
,
211 strlen (name
), &result
, &len
));
213 if (retval
!= NSS_STATUS_SUCCESS
)
215 if (retval
== NSS_STATUS_TRYAGAIN
)
220 if ((size_t) (len
+ 1) > buflen
)
224 return NSS_STATUS_TRYAGAIN
;
227 p
= strncpy (buffer
, result
, len
);
233 parse_res
= _nss_files_parse_etherent (p
, eth
, data
, buflen
, errnop
);
237 return NSS_STATUS_TRYAGAIN
;
239 return NSS_STATUS_NOTFOUND
;
241 return NSS_STATUS_SUCCESS
;
245 _nss_nis_getntohost_r (struct ether_addr
*addr
, struct ether
*eth
,
246 char *buffer
, size_t buflen
, int *errnop
)
248 struct parser_data
*data
= (void *) buffer
;
249 enum nss_status retval
;
250 char *domain
, *result
, *p
;
251 int len
, nlen
, parse_res
;
257 return NSS_STATUS_UNAVAIL
;
260 if (yp_get_default_domain (&domain
))
261 return NSS_STATUS_UNAVAIL
;
263 nlen
= sprintf (buf
, "%x:%x:%x:%x:%x:%x",
264 (int) addr
->ether_addr_octet
[0],
265 (int) addr
->ether_addr_octet
[1],
266 (int) addr
->ether_addr_octet
[2],
267 (int) addr
->ether_addr_octet
[3],
268 (int) addr
->ether_addr_octet
[4],
269 (int) addr
->ether_addr_octet
[5]);
271 retval
= yperr2nss (yp_match (domain
, "ethers.byaddr", buf
,
272 nlen
, &result
, &len
));
274 if (retval
!= NSS_STATUS_SUCCESS
)
276 if (retval
== NSS_STATUS_TRYAGAIN
)
281 if ((size_t) (len
+ 1) > buflen
)
285 return NSS_STATUS_TRYAGAIN
;
288 p
= strncpy (buffer
, result
, len
);
294 parse_res
= _nss_files_parse_etherent (p
, eth
, data
, buflen
, errnop
);
298 return NSS_STATUS_TRYAGAIN
;
300 return NSS_STATUS_NOTFOUND
;
302 return NSS_STATUS_SUCCESS
;