"Fossies" - the Fresh Open Source Software Archive

Member "nss_ldap-265/ldap-network.c" (6 Nov 2009, 8677 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-network.c,v 2.35 2009/05/26 07:25:12 lukeh Exp $
   21  */
   22 
   23 /* parts based on nss_nis */
   24 
   25 static char rcsId[] =
   26   "$Id: ldap-network.c,v 2.35 2009/05/26 07:25:12 lukeh Exp $";
   27 
   28 #include "config.h"
   29 
   30 #ifdef HAVE_PORT_BEFORE_H
   31 #include <port_before.h>
   32 #endif
   33 
   34 #if defined(HAVE_THREAD_H) && !defined(_AIX)
   35 #include <thread.h>
   36 #elif defined(HAVE_PTHREAD_H)
   37 #include <pthread.h>
   38 #endif
   39 
   40 #include <stdio.h>
   41 #include <stdlib.h>
   42 #include <string.h>
   43 #include <netdb.h>
   44 #include <netinet/in.h>
   45 #include <arpa/inet.h>
   46 #include <arpa/nameser.h>
   47 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
   48 #include <arpa/nameser_compat.h>
   49 #endif
   50 #include <sys/socket.h>
   51 #include <errno.h>
   52 
   53 #ifdef HAVE_LBER_H
   54 #include <lber.h>
   55 #endif
   56 #ifdef HAVE_LDAP_H
   57 #include <ldap.h>
   58 #endif
   59 
   60 #include "ldap-nss.h"
   61 #include "ldap-network.h"
   62 #include "util.h"
   63 
   64 #if defined(HAVE_IRS_H) || defined(HAVE_USERSEC_H)
   65 #define MAXALIASES 35
   66 #define MAXADDRSIZE 4
   67 #endif /* HAVE_IRS_H || HAVE_USERSEC_H */
   68 
   69 #ifdef HAVE_PORT_AFTER_H
   70 #include <port_after.h>
   71 #endif
   72 
   73 #ifdef HAVE_NSS_H
   74 static ent_context_t *net_context = NULL;
   75 #endif
   76 
   77 static NSS_STATUS
   78 _nss_ldap_parse_net (LDAPMessage * e,
   79              ldap_state_t * pvt,
   80              void *result, char *buffer, size_t buflen)
   81 {
   82 
   83   char *tmp;
   84 #ifdef HAVE_IRS_H
   85   struct nwent *network = (struct nwent *) result;
   86   unsigned char *addr;
   87 #else
   88   struct netent *network = (struct netent *) result;
   89 #endif
   90   NSS_STATUS stat;
   91 
   92   /* IPv6 support ? XXX */
   93   network->n_addrtype = AF_INET;
   94 
   95   stat = _nss_ldap_assign_attrval (e, ATM (LM_NETWORKS, cn), &network->n_name,
   96                    &buffer, &buflen);
   97   if (stat != NSS_SUCCESS)
   98     return stat;
   99 
  100   stat =
  101     _nss_ldap_assign_attrval (e, AT (ipNetworkNumber), &tmp, &buffer,
  102                   &buflen);
  103   if (stat != NSS_SUCCESS)
  104     return stat;
  105 
  106 #ifdef HAVE_IRS_H
  107   if (buflen < MAXADDRSIZE)
  108     return NSS_TRYAGAIN;
  109   addr = buffer;
  110   buffer += MAXADDRSIZE;
  111   buffer -= MAXADDRSIZE;
  112   network->n_length = inet_net_pton (AF_INET, tmp, &addr, MAXADDRSIZE);
  113   network->n_addr = addr;
  114 #else
  115   network->n_net = inet_network (tmp);
  116 #endif
  117 
  118   stat =
  119     _nss_ldap_assign_attrvals (e, ATM (LM_NETWORKS, cn), network->n_name,
  120                    &network->n_aliases, &buffer, &buflen, NULL);
  121   if (stat != NSS_SUCCESS)
  122     return stat;
  123 
  124   return NSS_SUCCESS;
  125 }
  126 
  127 #ifdef HAVE_NSSWITCH_H
  128 static NSS_STATUS
  129 _nss_ldap_getnetbyname_r (nss_backend_t * be, void *args)
  130 {
  131   ldap_args_t a;
  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                 NSS_ARGS (args)->buf.result,
  140                 NSS_ARGS (args)->buf.buffer,
  141                 NSS_ARGS (args)->buf.buflen,
  142                 &NSS_ARGS (args)->erange,
  143                 _nss_ldap_filt_getnetbyname,
  144                 LM_NETWORKS, _nss_ldap_parse_net);
  145 
  146   if (status == NSS_SUCCESS)
  147     NSS_ARGS (args)->returnval = NSS_ARGS (args)->buf.result;
  148 
  149   MAP_H_ERRNO (status, NSS_ARGS (args)->h_errno);
  150 
  151   return status;
  152 }
  153 #elif defined(HAVE_NSS_H)
  154 NSS_STATUS
  155 _nss_ldap_getnetbyname_r (const char *name, struct netent * result,
  156               char *buffer, size_t buflen, int *errnop,
  157               int *herrnop)
  158 {
  159   NSS_STATUS status;
  160   ldap_args_t a;
  161 
  162   LA_INIT (a);
  163   LA_STRING (a) = name;
  164   LA_TYPE (a) = LA_TYPE_STRING;
  165 
  166   status = _nss_ldap_getbyname (&a,
  167                 result,
  168                 buffer,
  169                 buflen,
  170                 errnop,
  171                 _nss_ldap_filt_getnetbyname,
  172                 LM_NETWORKS, _nss_ldap_parse_net);
  173 
  174   MAP_H_ERRNO (status, *herrnop);
  175 
  176   return status;
  177 }
  178 #endif
  179 
  180 #if defined(HAVE_NSSWITCH_H) || defined(HAVE_NSS_H)
  181 #ifdef HAVE_NSSWITCH_H
  182 static NSS_STATUS
  183 _nss_ldap_getnetbyaddr_r (nss_backend_t * be, void *args)
  184 #else
  185 NSS_STATUS
  186 _nss_ldap_getnetbyaddr_r (unsigned long addr, int type,
  187               struct netent * result, char *buffer, size_t buflen,
  188               int *errnop, int *herrnop)
  189 #endif
  190 {
  191   struct in_addr in;
  192   char buf[256];
  193   int blen;
  194   ldap_args_t a;
  195   NSS_STATUS retval = NSS_NOTFOUND;
  196 
  197   LA_INIT (a);
  198   LA_TYPE (a) = LA_TYPE_STRING;
  199 
  200 #ifdef HAVE_NSSWITCH_H
  201   in = inet_makeaddr (NSS_ARGS (args)->key.netaddr.net, 0);
  202 #else
  203   in = inet_makeaddr (addr, 0);
  204 #endif
  205   strcpy (buf, inet_ntoa (in));
  206   blen = strlen (buf);
  207   LA_STRING (a) = buf;
  208 
  209   while (1)
  210     {
  211 #ifdef HAVE_NSSWITCH_H
  212       retval =
  213     _nss_ldap_getbyname (&a, NSS_ARGS (args)->buf.result,
  214                  NSS_ARGS (args)->buf.buffer,
  215                  NSS_ARGS (args)->buf.buflen,
  216                  &NSS_ARGS (args)->erange,
  217 #else
  218       retval = _nss_ldap_getbyname (&a, result, buffer, buflen, errnop,
  219 #endif
  220                     _nss_ldap_filt_getnetbyaddr,
  221                     LM_NETWORKS, _nss_ldap_parse_net);
  222 
  223       if (retval != NSS_SUCCESS)
  224     {
  225       if (retval == NSS_NOTFOUND)
  226         {
  227           if (blen > 1 && buf[blen - 2] == '.' && buf[blen - 1] == '\0')
  228         {
  229           buf[blen - 2] = '\0';
  230           blen -= 2;
  231           continue;
  232         }
  233           else
  234         {
  235 #ifdef HAVE_NSSWITCH_H
  236           NSS_ARGS (args)->returnval = NULL;
  237           MAP_H_ERRNO (retval, NSS_ARGS (args)->h_errno);
  238 #else
  239           MAP_H_ERRNO (retval, *herrnop);
  240 #endif
  241           return NSS_NOTFOUND;
  242         }
  243         }
  244       else
  245         {
  246 #ifdef HAVE_NSSWITCH_H
  247           NSS_ARGS (args)->returnval = NULL;
  248           MAP_H_ERRNO (retval, NSS_ARGS (args)->h_errno);
  249 #else
  250           MAP_H_ERRNO (retval, *herrnop);
  251 #endif
  252           return retval;
  253         }
  254     }
  255       else
  256     {
  257       /* retval == NSS_SUCCESS */
  258       break;
  259     }
  260     }
  261 
  262 #ifdef HAVE_NSSWITCH_H
  263   NSS_ARGS (args)->returnval = NSS_ARGS (args)->buf.result;
  264   MAP_H_ERRNO (retval, NSS_ARGS (args)->h_errno);
  265 #else
  266   MAP_H_ERRNO (NSS_SUCCESS, *herrnop);
  267 #endif
  268 
  269   return retval;
  270 }
  271 #endif
  272 
  273 #ifdef HAVE_NSSWITCH_H
  274 static NSS_STATUS
  275 _nss_ldap_setnetent_r (nss_backend_t * net_context, void *fakeargs)
  276 #elif defined(HAVE_NSS_H)
  277      NSS_STATUS _nss_ldap_setnetent (void)
  278 #endif
  279 #if defined(HAVE_NSS_H) || defined(HAVE_NSSWITCH_H)
  280 {
  281   LOOKUP_SETENT (net_context);
  282 }
  283 #endif
  284 
  285 #ifdef HAVE_NSSWITCH_H
  286 static NSS_STATUS
  287 _nss_ldap_endnetent_r (nss_backend_t * net_context, void *fakeargs)
  288 #elif defined(HAVE_NSS_H)
  289      NSS_STATUS _nss_ldap_endnetent (void)
  290 #endif
  291 #if defined(HAVE_NSS_H) || defined(HAVE_NSSWITCH_H)
  292 {
  293   LOOKUP_ENDENT (net_context);
  294 }
  295 #endif
  296 
  297 #ifdef HAVE_NSSWITCH_H
  298 static NSS_STATUS
  299 _nss_ldap_getnetent_r (nss_backend_t * net_context, void *args)
  300 {
  301   NSS_STATUS status =
  302     _nss_ldap_getent (&((nss_ldap_backend_t *) net_context)->state,
  303               NSS_ARGS (args)->buf.result,
  304               NSS_ARGS (args)->buf.buffer,
  305               NSS_ARGS (args)->buf.buflen,
  306               &NSS_ARGS (args)->erange,
  307               _nss_ldap_filt_getnetent,
  308               LM_NETWORKS,
  309               _nss_ldap_parse_net);
  310 
  311   if (status == NSS_SUCCESS)
  312     NSS_ARGS (args)->returnval = NSS_ARGS (args)->buf.result;
  313 
  314   return status;
  315 }
  316 #elif defined(HAVE_NSS_H)
  317 NSS_STATUS
  318 _nss_ldap_getnetent_r (struct netent * result, char *buffer, size_t buflen,
  319                int *errnop, int *herrnop)
  320 {
  321   NSS_STATUS status;
  322 
  323   status = _nss_ldap_getent (&net_context,
  324                  result,
  325                  buffer,
  326                  buflen,
  327                  errnop,
  328                  _nss_ldap_filt_getnetent,
  329                  LM_NETWORKS, _nss_ldap_parse_net);
  330 
  331   MAP_H_ERRNO (status, *herrnop);
  332 
  333   return status;
  334 }
  335 #endif
  336 
  337 #ifdef HAVE_NSSWITCH_H
  338 static NSS_STATUS
  339 _nss_ldap_networks_destr (nss_backend_t * net_context, void *args)
  340 {
  341   return _nss_ldap_default_destr (net_context, args);
  342 }
  343 
  344 static nss_backend_op_t net_ops[] = {
  345   _nss_ldap_networks_destr,
  346   _nss_ldap_endnetent_r,
  347   _nss_ldap_setnetent_r,
  348   _nss_ldap_getnetent_r,
  349   _nss_ldap_getnetbyname_r,
  350   _nss_ldap_getnetbyaddr_r
  351 };
  352 
  353 nss_backend_t *
  354 _nss_ldap_networks_constr (const char *db_name,
  355                const char *src_name, const char *cfg_args)
  356 {
  357   nss_ldap_backend_t *be;
  358 
  359   if (!(be = (nss_ldap_backend_t *) malloc (sizeof (*be))))
  360     return NULL;
  361 
  362   be->ops = net_ops;
  363   be->n_ops = sizeof (net_ops) / sizeof (nss_backend_op_t);
  364 
  365   if (_nss_ldap_default_constr (be) != NSS_SUCCESS)
  366     return NULL;
  367 
  368   return (nss_backend_t *) be;
  369 }
  370 
  371 #endif /* !HAVE_NSS_H */
  372 
  373 #ifdef HAVE_IRS_H
  374 #include "irs-network.c"
  375 #endif