"Fossies" - the Fresh Open Source Software Archive

Member "nss_ldap-265/irs-network.c" (6 Nov 2009, 4671 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-network.c,v 2.25 2005/05/20 05:30:39 lukeh Exp $ */
   27 
   28 #ifdef HAVE_USERSEC_H
   29 void *nw_pvtinit (void);
   30 #endif
   31 IRS_EXPORT void nw_close (struct irs_nw *);
   32 IRS_EXPORT struct nwent *nw_byname (struct irs_nw *, const char *, int);
   33 IRS_EXPORT struct nwent *nw_byaddr (struct irs_nw *, void *, int, int);
   34 IRS_EXPORT struct nwent *nw_next (struct irs_nw *);
   35 IRS_EXPORT void nw_rewind (struct irs_nw *);
   36 IRS_EXPORT void nw_minimize (struct irs_nw *);
   37 
   38 struct pvt
   39 {
   40   struct nwent result;
   41   char buffer[NSS_BUFLEN_NETWORKS];
   42   ent_context_t *state;
   43 };
   44 
   45 IRS_EXPORT struct nwent *
   46 nw_byname (struct irs_nw *this, const char *name, int af)
   47 {
   48   NSS_STATUS s;
   49   struct pvt *pvt = (struct pvt *) this->private;
   50   ldap_args_t a;
   51 
   52   LA_INIT (a);
   53   LA_STRING (a) = name;
   54   LA_TYPE (a) = LA_TYPE_STRING;
   55 
   56   if (af != AF_INET)
   57     {
   58       h_errno = NETDB_INTERNAL;
   59       errno = EAFNOSUPPORT;
   60       return (NULL);
   61     }
   62 
   63   s = _nss_ldap_getbyname (&a,
   64                &pvt->result,
   65                pvt->buffer,
   66                sizeof (pvt->buffer),
   67                &errno,
   68                _nss_ldap_filt_getnetbyname,
   69                LM_NETWORKS, _nss_ldap_parse_net);
   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 nwent *
   80 nw_byaddr (struct irs_nw *this, void *net, int length, int af)
   81 {
   82   ldap_args_t a;
   83   NSS_STATUS s;
   84   struct pvt *pvt = (struct pvt *) this->private;
   85   char tmp[sizeof "255.255.255.255/32"], *t;
   86 
   87   if (af != AF_INET)
   88     {
   89       h_errno = NETDB_INTERNAL;
   90       errno = EAFNOSUPPORT;
   91       return (NULL);
   92     }
   93 
   94   /* Try it with /CIDR first. */
   95   if (inet_net_ntop (AF_INET, net, length, tmp, sizeof tmp) == NULL)
   96     {
   97       h_errno = NETDB_INTERNAL;
   98       return (NULL);
   99     }
  100 
  101   LA_INIT (a);
  102   LA_STRING (a) = tmp;
  103   LA_TYPE (a) = LA_TYPE_STRING;
  104 
  105   s = _nss_ldap_getbyname (&a,
  106                &pvt->result,
  107                pvt->buffer,
  108                sizeof (pvt->buffer),
  109                &errno,
  110                _nss_ldap_filt_getnetbyaddr,
  111                LM_NETWORKS, _nss_ldap_parse_net);
  112 
  113   if (s != NSS_SUCCESS)
  114     {
  115       if ((t = strchr (tmp, '/')) != NULL)
  116     {
  117       *t = '\0';
  118       s = _nss_ldap_getbyname (&a,
  119                    &pvt->result,
  120                    pvt->buffer,
  121                    sizeof (pvt->buffer),
  122                    &errno,
  123                    _nss_ldap_filt_getnetbyaddr,
  124                    LM_NETWORKS, _nss_ldap_parse_net);
  125       if (s != NSS_SUCCESS)
  126         {
  127           MAP_H_ERRNO (s, h_errno);
  128           return (NULL);
  129         }
  130     }
  131     }
  132 
  133   return &pvt->result;
  134 }
  135 
  136 IRS_EXPORT void
  137 nw_close (struct irs_nw *this)
  138 {
  139   LOOKUP_ENDENT (this);
  140 #ifdef HAVE_USERSEC_H
  141   free (this->private);
  142   free (this);
  143 #endif
  144 }
  145 
  146 IRS_EXPORT struct nwent *
  147 nw_next (struct irs_nw *this)
  148 {
  149   struct pvt *pvt = (struct pvt *) this->private;
  150   NSS_STATUS s;
  151 
  152   s = _nss_ldap_getent (&pvt->state,
  153             &pvt->result,
  154             pvt->buffer,
  155             sizeof (pvt->buffer),
  156             &errno,
  157             _nss_ldap_filt_getnetent,
  158             LM_NETWORKS, _nss_ldap_parse_net);
  159 
  160   if (s != NSS_SUCCESS)
  161     {
  162       MAP_H_ERRNO (s, h_errno);
  163       return NULL;
  164     }
  165   return &pvt->result;
  166 }
  167 
  168 IRS_EXPORT void
  169 nw_rewind (struct irs_nw *this)
  170 {
  171   LOOKUP_SETENT (this);
  172 }
  173 
  174 IRS_EXPORT void
  175 nw_minimize (struct irs_nw *this)
  176 {
  177 }
  178 
  179 #ifdef HAVE_USERSEC_H
  180 void *
  181 nw_pvtinit (void)
  182 #else
  183 struct irs_nw *
  184 irs_ldap_nw (struct irs_acc *this)
  185 #endif
  186 {
  187   struct irs_nw *nw;
  188   struct pvt *pvt;
  189 
  190   nw = calloc (1, sizeof (*nw));
  191   if (nw == NULL)
  192     return NULL;
  193 
  194   pvt = calloc (1, sizeof (*pvt));
  195   if (pvt == NULL)
  196     {
  197       free (nw);
  198       return NULL;
  199     }
  200 
  201   pvt->state = NULL;
  202   nw->private = pvt;
  203   nw->close = nw_close;
  204   nw->next = nw_next;
  205   nw->byname = nw_byname;
  206 /*      nw->byname2 = nw_byname2; */
  207   nw->byaddr = nw_byaddr;
  208   nw->rewind = nw_rewind;
  209   nw->minimize = nw_minimize;
  210   return nw;
  211 }
  212 
  213 #endif /*HAVE_IRS_H */