1
/* Copyright (C) 1997-2005 Luke Howard.
2
This file is part of the nss_ldap library.
3
Contributed by Luke Howard, <lukeh@padl.com>, 1997.
5
The nss_ldap 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 nss_ldap 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 nss_ldap 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.
20
$Id: ldap-ethers.c,v 2.36 2006/01/11 18:03:48 lukeh Exp $
25
"$Id: ldap-ethers.c,v 2.36 2006/01/11 18:03:48 lukeh Exp $";
29
#ifdef HAVE_PORT_BEFORE_H
30
#include <port_before.h>
33
#if defined(HAVE_THREAD_H) && !defined(_AIX)
35
#elif defined(HAVE_PTHREAD_H)
51
#include <sys/types.h>
52
#include <sys/socket.h>
53
#ifdef HAVE_NET_ROUTE_H
54
#include <net/route.h>
57
#include <netinet/in.h>
59
#ifdef HAVE_NETINET_IF_ETHER_H
60
#include <netinet/if_ether.h>
63
#ifdef HAVE_NETINET_ETHER_H
64
#include <netinet/ether.h>
68
#include "ldap-ethers.h"
71
#ifdef HAVE_PORT_AFTER_H
72
#include <port_after.h>
75
#ifndef NSS_BUFLEN_ETHERS
77
#define NSS_BUFLEN_ETHERS 1024
78
#endif /* NSS_BUFLEN_ETHERS */
80
#if defined(HAVE_NSSWITCH_H) || defined(HAVE_NSS_H)
82
#ifdef HAVE_NSSWITCH_H
83
#ifdef HAVE_ETHER_ATON
84
extern struct ether_addr *ether_aton (const char *s);
86
static struct ether_addr *ether_aton (const char *s);
87
#endif /* HAVE_ETHER_ATON */
88
#ifdef HAVE_ETHER_NTOA
89
extern char *ether_ntoa (const struct ether_addr *e);
91
static char *ether_ntoa (const struct ether_addr *e);
92
#endif /* HAVE_ETHER_NTOA */
93
#endif /* HAVE_NSSWITCH_H */
96
static ent_context_t *ether_context = NULL;
100
_nss_ldap_parse_ether (LDAPMessage * e,
102
void *result, char *buffer, size_t buflen)
104
struct ether *ether = (struct ether *) result;
107
struct ether_addr *addr;
109
stat = _nss_ldap_assign_attrval (e, ATM (LM_ETHERS, cn),
110
ðer->e_name, &buffer, &buflen);
111
if (stat != NSS_SUCCESS)
114
stat = _nss_ldap_assign_attrval (e, AT (macAddress), &saddr,
117
if (stat != NSS_SUCCESS || ((addr = ether_aton (saddr)) == NULL))
120
memcpy (ðer->e_addr, addr, sizeof (*addr));
125
#ifdef HAVE_NSSWITCH_H
127
_nss_ldap_gethostton_r (nss_backend_t * be, void *args)
131
char buffer[NSS_BUFLEN_ETHERS];
135
LA_STRING (a) = NSS_ARGS (args)->key.name;
136
LA_TYPE (a) = LA_TYPE_STRING;
138
status = _nss_ldap_getbyname (&a,
142
&NSS_ARGS (args)->erange,
143
_nss_ldap_filt_gethostton,
144
LM_ETHERS, _nss_ldap_parse_ether);
146
if (status == NSS_SUCCESS)
148
memcpy (NSS_ARGS (args)->buf.result, &result.e_addr,
149
sizeof (result.e_addr));
150
NSS_ARGS (args)->returnval = NSS_ARGS (args)->buf.result;
155
#elif defined(HAVE_NSS_H)
157
_nss_ldap_gethostton_r (const char *name, struct ether * result,
158
char *buffer, size_t buflen, int *errnop)
160
LOOKUP_NAME (name, result, buffer, buflen, errnop,
161
_nss_ldap_filt_gethostton, LM_ETHERS, _nss_ldap_parse_ether,
162
LDAP_NSS_BUFLEN_DEFAULT);
166
#ifdef HAVE_NSSWITCH_H
168
_nss_ldap_getntohost_r (nss_backend_t * be, void *args)
173
char buffer[NSS_BUFLEN_ETHERS];
176
addr = ether_ntoa ((struct ether_addr *) (NSS_ARGS (args)->key.ether));
179
LA_STRING (a) = addr;
180
LA_TYPE (a) = LA_TYPE_STRING;
182
status = _nss_ldap_getbyname (&a,
186
&NSS_ARGS (args)->erange,
187
_nss_ldap_filt_getntohost,
188
LM_ETHERS, _nss_ldap_parse_ether);
190
if (status == NSS_SUCCESS)
192
memcpy (NSS_ARGS (args)->buf.buffer, result.e_name,
193
strlen (result.e_name) + 1);
194
NSS_ARGS (args)->returnval = NSS_ARGS (args)->buf.result =
195
NSS_ARGS (args)->buf.buffer;
196
NSS_ARGS (args)->buf.buflen = strlen (result.e_name);
200
NSS_ARGS (args)->returnval = NULL;
205
#elif defined(HAVE_NSS_H)
207
_nss_ldap_getntohost_r (struct ether_addr * addr, struct ether * result,
208
char *buffer, size_t buflen, int *errnop)
210
/* The correct ether_ntoa call would have a struct ether instead of whatever
213
LOOKUP_NAME (ether_ntoa ((struct ether_addr *) (&result->e_addr)), result,
214
buffer, buflen, errnop, _nss_ldap_filt_getntohost, LM_ETHERS,
215
_nss_ldap_parse_ether, LDAP_NSS_BUFLEN_DEFAULT);
219
#ifdef HAVE_NSSWITCH_H
221
_nss_ldap_setetherent_r (nss_backend_t * ether_context, void *fakeargs)
222
#elif defined(HAVE_NSS_H)
223
NSS_STATUS _nss_ldap_setetherent (void)
225
#if defined(HAVE_NSSWITCH_H) || defined(HAVE_NSS_H)
227
LOOKUP_SETENT (ether_context);
231
#ifdef HAVE_NSSWITCH_H
233
_nss_ldap_endetherent_r (nss_backend_t * ether_context, void *fakeargs)
234
#elif defined(HAVE_NSS_H)
235
NSS_STATUS _nss_ldap_endetherent (void)
237
#if defined(HAVE_NSS_H) || defined(HAVE_NSSWITCH_H)
239
LOOKUP_ENDENT (ether_context);
243
#ifdef HAVE_NSSWITCH_H
245
_nss_ldap_getetherent_r (nss_backend_t * ether_context, void *args)
250
status = _nss_ldap_getent (&((nss_ldap_backend_t *) ether_context)->state,
252
NSS_ARGS (args)->buf.buffer,
253
NSS_ARGS (args)->buf.buflen,
254
&NSS_ARGS (args)->erange,
255
_nss_ldap_filt_getetherent,
256
LM_ETHERS, _nss_ldap_parse_ether);
258
if (status == NSS_SUCCESS)
260
memcpy (NSS_ARGS (args)->buf.result, &result.e_addr,
261
sizeof (result.e_addr));
262
NSS_ARGS (args)->returnval = NSS_ARGS (args)->buf.result;
266
NSS_ARGS (args)->returnval = NULL;
271
#elif defined(HAVE_NSS_H)
273
_nss_ldap_getetherent_r (struct ether * result, char *buffer, size_t buflen,
276
LOOKUP_GETENT (ether_context, result, buffer, buflen, errnop,
277
_nss_ldap_filt_getetherent, LM_ETHERS,
278
_nss_ldap_parse_ether, LDAP_NSS_BUFLEN_DEFAULT);
282
#ifdef HAVE_NSSWITCH_H
284
_nss_ldap_ethers_destr (nss_backend_t * ether_context, void *args)
286
return _nss_ldap_default_destr (ether_context, args);
289
static nss_backend_op_t ethers_ops[] = {
290
_nss_ldap_ethers_destr,
291
_nss_ldap_gethostton_r,
292
_nss_ldap_getntohost_r
296
_nss_ldap_ethers_constr (const char *db_name,
297
const char *src_name, const char *cfg_args)
299
nss_ldap_backend_t *be;
301
if (!(be = (nss_ldap_backend_t *) malloc (sizeof (*be))))
304
be->ops = ethers_ops;
305
be->n_ops = sizeof (ethers_ops) / sizeof (nss_backend_op_t);
307
if (_nss_ldap_default_constr (be) != NSS_SUCCESS)
310
return (nss_backend_t *) be;
314
#endif /* !HAVE_NSS_H */
316
#ifdef HAVE_NSSWITCH_H
318
#ifndef HAVE_ETHER_ATON
319
static struct ether_addr *ether_aton (const char *s)
321
static struct ether_addr ep;
325
i = sscanf(s, " %x:%x:%x:%x:%x:%x",
326
&t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
329
for (i = 0; i < 6; i++)
330
ep.ether_addr_octet[i] = t[i];
334
#endif /* !HAVE_ETHER_ATON */
336
#ifndef HAVE_ETHER_NTOA
337
#define EI(i) (unsigned int)(e->ether_addr_octet[(i)])
338
static char *ether_ntoa (const struct ether_addr *e)
343
sprintf(s, "%x:%x:%x:%x:%x:%x",
344
EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
348
#endif /* !HAVE_ETHER_NTOA */
350
#endif /* HAVE_NSSWITCH_H */
352
#endif /* !HAVE_IRS_H */