"Fossies" - the Fresh Open Source Software Archive

Member "nss_ldap-265/ldap-ethers.c" (6 Nov 2009, 8917 Bytes) of package /linux/privat/old/nss_ldap-265.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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.37 2009/02/03 22:59:34 lukeh Exp $
   21  */
   22 
   23 
   24 static char rcsId[] =
   25   "$Id: ldap-ethers.c,v 2.37 2009/02/03 22:59:34 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   ldap_args_t a;
  211   char fullmac[18];
  212 
  213   if (buflen < LDAP_NSS_BUFLEN_DEFAULT)
  214     {
  215       *errnop = ERANGE;
  216       return NSS_TRYAGAIN;
  217     }
  218 
  219   snprintf(fullmac, sizeof(fullmac), "%02x:%02x:%02x:%02x:%02x:%02x",
  220        addr->ether_addr_octet[0], addr->ether_addr_octet[1],
  221        addr->ether_addr_octet[2], addr->ether_addr_octet[3],
  222        addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
  223 
  224   LA_INIT(a);
  225   LA_STRING(a) = ether_ntoa(addr);
  226   LA_TYPE(a) = LA_TYPE_STRING_AND_STRING;
  227   LA_STRING2(a) = fullmac;
  228 
  229   return _nss_ldap_getbyname(&a, result, buffer, buflen, errnop,
  230                  _nss_ldap_filt_getntohost, LM_ETHERS,
  231                  _nss_ldap_parse_ether);
  232 }
  233 #endif
  234 
  235 #ifdef HAVE_NSSWITCH_H
  236 static NSS_STATUS
  237 _nss_ldap_setetherent_r (nss_backend_t * ether_context, void *fakeargs)
  238 #elif defined(HAVE_NSS_H)
  239      NSS_STATUS _nss_ldap_setetherent (void)
  240 #endif
  241 #if defined(HAVE_NSSWITCH_H) || defined(HAVE_NSS_H)
  242 {
  243   LOOKUP_SETENT (ether_context);
  244 }
  245 #endif
  246 
  247 #ifdef HAVE_NSSWITCH_H
  248 static NSS_STATUS
  249 _nss_ldap_endetherent_r (nss_backend_t * ether_context, void *fakeargs)
  250 #elif defined(HAVE_NSS_H)
  251      NSS_STATUS _nss_ldap_endetherent (void)
  252 #endif
  253 #if defined(HAVE_NSS_H) || defined(HAVE_NSSWITCH_H)
  254 {
  255   LOOKUP_ENDENT (ether_context);
  256 }
  257 #endif
  258 
  259 #ifdef HAVE_NSSWITCH_H
  260 static NSS_STATUS
  261 _nss_ldap_getetherent_r (nss_backend_t * ether_context, void *args)
  262 {
  263   struct ether result;
  264   NSS_STATUS status;
  265 
  266   status = _nss_ldap_getent (&((nss_ldap_backend_t *) ether_context)->state,
  267                  &result,
  268                  NSS_ARGS (args)->buf.buffer,
  269                  NSS_ARGS (args)->buf.buflen,
  270                  &NSS_ARGS (args)->erange,
  271                  _nss_ldap_filt_getetherent,
  272                  LM_ETHERS, _nss_ldap_parse_ether);
  273 
  274   if (status == NSS_SUCCESS)
  275     {
  276       memcpy (NSS_ARGS (args)->buf.result, &result.e_addr,
  277           sizeof (result.e_addr));
  278       NSS_ARGS (args)->returnval = NSS_ARGS (args)->buf.result;
  279     }
  280   else
  281     {
  282       NSS_ARGS (args)->returnval = NULL;
  283     }
  284 
  285   return status;
  286 }
  287 #elif defined(HAVE_NSS_H)
  288 NSS_STATUS
  289 _nss_ldap_getetherent_r (struct ether * result, char *buffer, size_t buflen,
  290              int *errnop)
  291 {
  292   LOOKUP_GETENT (ether_context, result, buffer, buflen, errnop,
  293          _nss_ldap_filt_getetherent, LM_ETHERS,
  294          _nss_ldap_parse_ether, LDAP_NSS_BUFLEN_DEFAULT);
  295 }
  296 #endif
  297 
  298 #ifdef HAVE_NSSWITCH_H
  299 static NSS_STATUS
  300 _nss_ldap_ethers_destr (nss_backend_t * ether_context, void *args)
  301 {
  302   return _nss_ldap_default_destr (ether_context, args);
  303 }
  304 
  305 static nss_backend_op_t ethers_ops[] = {
  306   _nss_ldap_ethers_destr,
  307   _nss_ldap_gethostton_r,
  308   _nss_ldap_getntohost_r
  309 };
  310 
  311 nss_backend_t *
  312 _nss_ldap_ethers_constr (const char *db_name,
  313              const char *src_name, const char *cfg_args)
  314 {
  315   nss_ldap_backend_t *be;
  316 
  317   if (!(be = (nss_ldap_backend_t *) malloc (sizeof (*be))))
  318     return NULL;
  319 
  320   be->ops = ethers_ops;
  321   be->n_ops = sizeof (ethers_ops) / sizeof (nss_backend_op_t);
  322 
  323   if (_nss_ldap_default_constr (be) != NSS_SUCCESS)
  324     return NULL;
  325 
  326   return (nss_backend_t *) be;
  327 
  328 }
  329 
  330 #endif /* !HAVE_NSS_H */
  331 
  332 #ifdef HAVE_NSSWITCH_H
  333 
  334 #ifndef HAVE_ETHER_ATON
  335 static struct ether_addr *ether_aton (const char *s)
  336 {
  337     static struct ether_addr ep;
  338     register int i;
  339     unsigned int t[6];
  340         
  341     i = sscanf(s, " %x:%x:%x:%x:%x:%x",
  342         &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
  343     if (i != 6)
  344         return NULL;
  345     for (i = 0; i < 6; i++)
  346         ep.ether_addr_octet[i] = t[i];
  347 
  348     return &ep;
  349 }
  350 #endif /* !HAVE_ETHER_ATON */
  351 
  352 #ifndef HAVE_ETHER_NTOA
  353 #define EI(i)   (unsigned int)(e->ether_addr_octet[(i)])
  354 static char *ether_ntoa (const struct ether_addr *e)
  355 {
  356     static char s[18];
  357 
  358     s[0] = 0;
  359     sprintf(s, "%x:%x:%x:%x:%x:%x",
  360         EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
  361 
  362     return s;
  363 }
  364 #endif /* !HAVE_ETHER_NTOA */
  365 
  366 #endif /* HAVE_NSSWITCH_H */
  367 
  368 #endif /* !HAVE_IRS_H */