~ubuntu-branches/ubuntu/trusty/libnss-ldap/trusty-proposed

« back to all changes in this revision

Viewing changes to ldap-ethers.c

  • Committer: Bazaar Package Importer
  • Author(s): Richard A Nelson (Rick)
  • Date: 2007-05-14 19:40:00 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070514194000-40u9ndh540lgliqe
Tags: 255-1
Survived i386 and amd64, let it loose

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
4
 
 
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.
9
 
 
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.
14
 
 
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.
19
 
 
20
 
   $Id: ldap-ethers.c,v 2.36 2006/01/11 18:03:48 lukeh Exp $
21
 
 */
22
 
 
23
 
 
24
 
static char rcsId[] =
25
 
  "$Id: ldap-ethers.c,v 2.36 2006/01/11 18:03:48 lukeh Exp $";
26
 
 
27
 
#include "config.h"
28
 
 
29
 
#ifdef HAVE_PORT_BEFORE_H
30
 
#include <port_before.h>
31
 
#endif
32
 
 
33
 
#if defined(HAVE_THREAD_H) && !defined(_AIX)
34
 
#include <thread.h>
35
 
#elif defined(HAVE_PTHREAD_H)
36
 
#include <pthread.h>
37
 
#endif
38
 
 
39
 
#include <stdio.h>
40
 
#include <stdlib.h>
41
 
#include <string.h>
42
 
#include <netdb.h>
43
 
 
44
 
#ifdef HAVE_LBER_H
45
 
#include <lber.h>
46
 
#endif
47
 
#ifdef HAVE_LDAP_H
48
 
#include <ldap.h>
49
 
#endif
50
 
 
51
 
#include <sys/types.h>
52
 
#include <sys/socket.h>
53
 
#ifdef HAVE_NET_ROUTE_H
54
 
#include <net/route.h>
55
 
#endif
56
 
#include <net/if.h>
57
 
#include <netinet/in.h>
58
 
 
59
 
#ifdef HAVE_NETINET_IF_ETHER_H
60
 
#include <netinet/if_ether.h>
61
 
#endif
62
 
 
63
 
#ifdef HAVE_NETINET_ETHER_H
64
 
#include <netinet/ether.h>
65
 
#endif
66
 
 
67
 
#include "ldap-nss.h"
68
 
#include "ldap-ethers.h"
69
 
#include "util.h"
70
 
 
71
 
#ifdef HAVE_PORT_AFTER_H
72
 
#include <port_after.h>
73
 
#endif
74
 
 
75
 
#ifndef NSS_BUFLEN_ETHERS
76
 
/* for HP-UX */
77
 
#define NSS_BUFLEN_ETHERS 1024
78
 
#endif /* NSS_BUFLEN_ETHERS */
79
 
 
80
 
#if defined(HAVE_NSSWITCH_H) || defined(HAVE_NSS_H)
81
 
 
82
 
#ifdef HAVE_NSSWITCH_H
83
 
#ifdef HAVE_ETHER_ATON
84
 
extern struct ether_addr *ether_aton (const char *s);
85
 
#else
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);
90
 
#else
91
 
static char *ether_ntoa (const struct ether_addr *e);
92
 
#endif /* HAVE_ETHER_NTOA */
93
 
#endif /* HAVE_NSSWITCH_H */
94
 
 
95
 
#ifdef HAVE_NSS_H
96
 
static ent_context_t *ether_context = NULL;
97
 
#endif
98
 
 
99
 
static NSS_STATUS
100
 
_nss_ldap_parse_ether (LDAPMessage * e,
101
 
                       ldap_state_t * pvt,
102
 
                       void *result, char *buffer, size_t buflen)
103
 
{
104
 
  struct ether *ether = (struct ether *) result;
105
 
  char *saddr;
106
 
  NSS_STATUS stat;
107
 
  struct ether_addr *addr;
108
 
 
109
 
  stat = _nss_ldap_assign_attrval (e, ATM (LM_ETHERS, cn),
110
 
                                   &ether->e_name, &buffer, &buflen);
111
 
  if (stat != NSS_SUCCESS)
112
 
    return stat;
113
 
 
114
 
  stat = _nss_ldap_assign_attrval (e, AT (macAddress), &saddr,
115
 
                                   &buffer, &buflen);
116
 
 
117
 
  if (stat != NSS_SUCCESS || ((addr = ether_aton (saddr)) == NULL))
118
 
    return NSS_NOTFOUND;
119
 
 
120
 
  memcpy (&ether->e_addr, addr, sizeof (*addr));
121
 
 
122
 
  return NSS_SUCCESS;
123
 
}
124
 
 
125
 
#ifdef HAVE_NSSWITCH_H
126
 
static NSS_STATUS
127
 
_nss_ldap_gethostton_r (nss_backend_t * be, void *args)
128
 
{
129
 
  struct ether result;
130
 
  ldap_args_t a;
131
 
  char buffer[NSS_BUFLEN_ETHERS];
132
 
  NSS_STATUS status;
133
 
 
134
 
  LA_INIT (a);
135
 
  LA_STRING (a) = NSS_ARGS (args)->key.name;
136
 
  LA_TYPE (a) = LA_TYPE_STRING;
137
 
 
138
 
  status = _nss_ldap_getbyname (&a,
139
 
                                &result,
140
 
                                buffer,
141
 
                                sizeof (buffer),
142
 
                                &NSS_ARGS (args)->erange,
143
 
                                _nss_ldap_filt_gethostton,
144
 
                                LM_ETHERS, _nss_ldap_parse_ether);
145
 
 
146
 
  if (status == NSS_SUCCESS)
147
 
    {
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;
151
 
    }
152
 
 
153
 
  return status;
154
 
}
155
 
#elif defined(HAVE_NSS_H)
156
 
NSS_STATUS
157
 
_nss_ldap_gethostton_r (const char *name, struct ether * result,
158
 
                        char *buffer, size_t buflen, int *errnop)
159
 
{
160
 
  LOOKUP_NAME (name, result, buffer, buflen, errnop,
161
 
               _nss_ldap_filt_gethostton, LM_ETHERS, _nss_ldap_parse_ether,
162
 
               LDAP_NSS_BUFLEN_DEFAULT);
163
 
}
164
 
#endif
165
 
 
166
 
#ifdef HAVE_NSSWITCH_H
167
 
static NSS_STATUS
168
 
_nss_ldap_getntohost_r (nss_backend_t * be, void *args)
169
 
{
170
 
  struct ether result;
171
 
  char *addr;
172
 
  ldap_args_t a;
173
 
  char buffer[NSS_BUFLEN_ETHERS];
174
 
  NSS_STATUS status;
175
 
 
176
 
  addr = ether_ntoa ((struct ether_addr *) (NSS_ARGS (args)->key.ether));
177
 
 
178
 
  LA_INIT (a);
179
 
  LA_STRING (a) = addr;
180
 
  LA_TYPE (a) = LA_TYPE_STRING;
181
 
 
182
 
  status = _nss_ldap_getbyname (&a,
183
 
                                &result,
184
 
                                buffer,
185
 
                                sizeof (buffer),
186
 
                                &NSS_ARGS (args)->erange,
187
 
                                _nss_ldap_filt_getntohost,
188
 
                                LM_ETHERS, _nss_ldap_parse_ether);
189
 
 
190
 
  if (status == NSS_SUCCESS)
191
 
    {
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);
197
 
    }
198
 
  else
199
 
    {
200
 
      NSS_ARGS (args)->returnval = NULL;
201
 
    }
202
 
 
203
 
  return status;
204
 
}
205
 
#elif defined(HAVE_NSS_H)
206
 
NSS_STATUS
207
 
_nss_ldap_getntohost_r (struct ether_addr * addr, struct ether * result,
208
 
                        char *buffer, size_t buflen, int *errnop)
209
 
{
210
 
/* The correct ether_ntoa call would have a struct ether instead of whatever
211
 
   result->e_addr is */
212
 
 
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);
216
 
}
217
 
#endif
218
 
 
219
 
#ifdef HAVE_NSSWITCH_H
220
 
static NSS_STATUS
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)
224
 
#endif
225
 
#if defined(HAVE_NSSWITCH_H) || defined(HAVE_NSS_H)
226
 
{
227
 
  LOOKUP_SETENT (ether_context);
228
 
}
229
 
#endif
230
 
 
231
 
#ifdef HAVE_NSSWITCH_H
232
 
static NSS_STATUS
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)
236
 
#endif
237
 
#if defined(HAVE_NSS_H) || defined(HAVE_NSSWITCH_H)
238
 
{
239
 
  LOOKUP_ENDENT (ether_context);
240
 
}
241
 
#endif
242
 
 
243
 
#ifdef HAVE_NSSWITCH_H
244
 
static NSS_STATUS
245
 
_nss_ldap_getetherent_r (nss_backend_t * ether_context, void *args)
246
 
{
247
 
  struct ether result;
248
 
  NSS_STATUS status;
249
 
 
250
 
  status = _nss_ldap_getent (&((nss_ldap_backend_t *) ether_context)->state,
251
 
                             &result,
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);
257
 
 
258
 
  if (status == NSS_SUCCESS)
259
 
    {
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;
263
 
    }
264
 
  else
265
 
    {
266
 
      NSS_ARGS (args)->returnval = NULL;
267
 
    }
268
 
 
269
 
  return status;
270
 
}
271
 
#elif defined(HAVE_NSS_H)
272
 
NSS_STATUS
273
 
_nss_ldap_getetherent_r (struct ether * result, char *buffer, size_t buflen,
274
 
                         int *errnop)
275
 
{
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);
279
 
}
280
 
#endif
281
 
 
282
 
#ifdef HAVE_NSSWITCH_H
283
 
static NSS_STATUS
284
 
_nss_ldap_ethers_destr (nss_backend_t * ether_context, void *args)
285
 
{
286
 
  return _nss_ldap_default_destr (ether_context, args);
287
 
}
288
 
 
289
 
static nss_backend_op_t ethers_ops[] = {
290
 
  _nss_ldap_ethers_destr,
291
 
  _nss_ldap_gethostton_r,
292
 
  _nss_ldap_getntohost_r
293
 
};
294
 
 
295
 
nss_backend_t *
296
 
_nss_ldap_ethers_constr (const char *db_name,
297
 
                         const char *src_name, const char *cfg_args)
298
 
{
299
 
  nss_ldap_backend_t *be;
300
 
 
301
 
  if (!(be = (nss_ldap_backend_t *) malloc (sizeof (*be))))
302
 
    return NULL;
303
 
 
304
 
  be->ops = ethers_ops;
305
 
  be->n_ops = sizeof (ethers_ops) / sizeof (nss_backend_op_t);
306
 
 
307
 
  if (_nss_ldap_default_constr (be) != NSS_SUCCESS)
308
 
    return NULL;
309
 
 
310
 
  return (nss_backend_t *) be;
311
 
 
312
 
}
313
 
 
314
 
#endif /* !HAVE_NSS_H */
315
 
 
316
 
#ifdef HAVE_NSSWITCH_H
317
 
 
318
 
#ifndef HAVE_ETHER_ATON
319
 
static struct ether_addr *ether_aton (const char *s)
320
 
{
321
 
        static struct ether_addr ep;
322
 
        register int i;
323
 
        unsigned int t[6];
324
 
        
325
 
        i = sscanf(s, " %x:%x:%x:%x:%x:%x",
326
 
                &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
327
 
        if (i != 6)
328
 
                return NULL;
329
 
        for (i = 0; i < 6; i++)
330
 
                ep.ether_addr_octet[i] = t[i];
331
 
 
332
 
        return &ep;
333
 
}
334
 
#endif /* !HAVE_ETHER_ATON */
335
 
 
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)
339
 
{
340
 
        static char s[18];
341
 
 
342
 
        s[0] = 0;
343
 
        sprintf(s, "%x:%x:%x:%x:%x:%x",
344
 
                EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
345
 
 
346
 
        return s;
347
 
}
348
 
#endif /* !HAVE_ETHER_NTOA */
349
 
 
350
 
#endif /* HAVE_NSSWITCH_H */
351
 
 
352
 
#endif /* !HAVE_IRS_H */