"Fossies" - the Fresh Open Source Software Archive

Member "nss_ldap-265/irs-hosts.c" (6 Nov 2009, 4745 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 
   21 #ifdef HAVE_IRS_H
   22 
   23 #include <errno.h>
   24 #include "irs-nss.h"
   25 
   26 /* $Id: irs-hosts.c,v 2.26 2005/05/20 05:30:39 lukeh Exp $ */
   27 
   28 #ifdef HAVE_USERSEC_H
   29 void *ho_pvtinit (void);
   30 #endif
   31 IRS_EXPORT void ho_close (struct irs_ho *this);
   32 IRS_EXPORT struct hostent *ho_byname (struct irs_ho *this, const char *name);
   33 IRS_EXPORT struct hostent *ho_byname2 (struct irs_ho *this, const char *name,
   34                        int af);
   35 IRS_EXPORT struct hostent *ho_byaddr (struct irs_ho *this, const void *addr,
   36                       int len, int af);
   37 IRS_EXPORT struct hostent *ho_next (struct irs_ho *this);
   38 IRS_EXPORT void ho_rewind (struct irs_ho *this);
   39 IRS_EXPORT void ho_minimize (struct irs_ho *this);
   40 
   41 
   42 static const u_char mapped[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
   43 static const u_char tunnelled[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   44 
   45 struct pvt
   46 {
   47   struct hostent result;
   48   char buffer[NSS_BUFLEN_HOSTS];
   49   ent_context_t *state;
   50 };
   51 
   52 IRS_EXPORT struct hostent *
   53 ho_byname (struct irs_ho *this, const char *name)
   54 {
   55   NSS_STATUS s;
   56   struct pvt *pvt = (struct pvt *) this->private;
   57   ldap_args_t a;
   58 
   59   LA_INIT (a);
   60   LA_STRING (a) = name;
   61   LA_TYPE (a) = LA_TYPE_STRING;
   62 
   63   s = _nss_ldap_getbyname (&a,
   64                &pvt->result,
   65                pvt->buffer,
   66                sizeof (pvt->buffer),
   67                &errno,
   68                _nss_ldap_filt_gethostbyname,
   69                LM_HOSTS, _nss_ldap_parse_hostv4);
   70 
   71   if (s != NSS_SUCCESS)
   72     {
   73       MAP_H_ERRNO (s, h_errno);
   74       return NULL;
   75     }
   76   return &pvt->result;
   77 }
   78 
   79 IRS_EXPORT struct hostent *
   80 ho_byaddr (struct irs_ho *this, const void *addr, int len, int af)
   81 {
   82   struct pvt *pvt = (struct pvt *) this->private;
   83   char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
   84   const u_char *uaddr = addr;
   85   NSS_STATUS s;
   86   ldap_args_t a;
   87 
   88   if (af == AF_INET6 && len == IN6ADDRSZ
   89       && (!memcmp (uaddr, mapped, sizeof mapped) ||
   90       !memcmp (uaddr, tunnelled, sizeof tunnelled)))
   91     {
   92       /* Unmap. */
   93       addr = (u_char *) addr + sizeof mapped;
   94       uaddr += sizeof mapped;
   95       af = AF_INET;
   96       len = INADDRSZ;
   97     }
   98   if (inet_ntop (af, uaddr, tmp, sizeof tmp) == NULL)
   99     {
  100       h_errno = NETDB_INTERNAL;
  101       return (NULL);
  102     }
  103 
  104   LA_INIT (a);
  105   LA_STRING (a) = tmp;
  106   LA_TYPE (a) = LA_TYPE_STRING;
  107 
  108   s = _nss_ldap_getbyname (&a,
  109                &pvt->result,
  110                pvt->buffer,
  111                sizeof (pvt->buffer),
  112                &errno,
  113                _nss_ldap_filt_gethostbyaddr,
  114                LM_HOSTS, _nss_ldap_parse_hostv4);
  115 
  116   if (s != NSS_SUCCESS)
  117     {
  118       MAP_H_ERRNO (s, h_errno);
  119       return NULL;
  120     }
  121   return &pvt->result;
  122 }
  123 
  124 IRS_EXPORT void
  125 ho_close (struct irs_ho *this)
  126 {
  127   LOOKUP_ENDENT (this);
  128 #ifdef HAVE_USERSEC_H
  129   free (this->private);
  130   free (this);
  131 #endif
  132 }
  133 
  134 IRS_EXPORT struct hostent *
  135 ho_next (struct irs_ho *this)
  136 {
  137   struct pvt *pvt = (struct pvt *) this->private;
  138   NSS_STATUS s;
  139 
  140   s = _nss_ldap_getent (&pvt->state,
  141             &pvt->result,
  142             pvt->buffer,
  143             sizeof (pvt->buffer),
  144             &errno,
  145             _nss_ldap_filt_gethostent,
  146             LM_HOSTS, _nss_ldap_parse_hostv4);
  147 
  148   if (s != NSS_SUCCESS)
  149     {
  150       MAP_H_ERRNO (s, h_errno);
  151       return NULL;
  152     }
  153   return &pvt->result;
  154 }
  155 
  156 IRS_EXPORT void
  157 ho_rewind (struct irs_ho *this)
  158 {
  159   LOOKUP_SETENT (this);
  160 }
  161 
  162 IRS_EXPORT void
  163 ho_minimize (struct irs_ho *this)
  164 {
  165 }
  166 
  167 #ifdef HAVE_USERSEC_H
  168 void *
  169 ho_pvtinit (void)
  170 #else
  171 struct irs_ho *
  172 irs_ldap_ho (struct irs_acc *this)
  173 #endif
  174 {
  175   struct irs_ho *ho;
  176   struct pvt *pvt;
  177 
  178   ho = calloc (1, sizeof (*ho));
  179   if (ho == NULL)
  180     return NULL;
  181 
  182   pvt = calloc (1, sizeof (*pvt));
  183   if (pvt == NULL)
  184     {
  185       free (ho);
  186       return NULL;
  187     }
  188 
  189   pvt->state = NULL;
  190   ho->private = pvt;
  191   ho->close = ho_close;
  192   ho->next = ho_next;
  193   ho->byname = ho_byname;
  194 /*      ho->byname2 = ho_byname2; */
  195   ho->byaddr = ho_byaddr;
  196   ho->rewind = ho_rewind;
  197   ho->minimize = ho_minimize;
  198   return ho;
  199 }
  200 
  201 #endif /*HAVE_IRS_H */