"Fossies" - the Fresh Open Source Software Archive

Member "nss_ldap-265/pagectrl.c" (6 Nov 2009, 6210 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) 2002 Max Caines, All Rights Reserved.
    2    This file is part of the nss_ldap library.
    3    Contributed by Max Caines, <Max.Caines@wlv.ac.uk>, April 2002.
    4    This software is not subject to any license of the University
    5    of Wolverhampton.
    6 
    7    The nss_ldap library is free software; you can redistribute it and/or
    8    modify it under the terms of the GNU Library General Public License as
    9    published by the Free Software Foundation; either version 2 of the
   10    License, or (at your option) any later version.
   11 
   12    The nss_ldap library is distributed in the hope that it will be useful,
   13    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15    Library General Public License for more details.
   16 
   17    You should have received a copy of the GNU Library General Public
   18    License along with the nss_ldap library; see the file COPYING.LIB.  If not,
   19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   20    Boston, MA 02111-1307, USA.
   21  */
   22 
   23 static char rcsId[] = "$Id: pagectrl.c,v 2.4 2006/01/13 10:24:59 lukeh Exp $";
   24 
   25 #include "config.h"
   26 
   27 #include <stdio.h>
   28 #include <stdlib.h>
   29 #include <string.h>
   30 #include <time.h>
   31 
   32 #include <lber.h>
   33 #include <ldap.h>
   34 
   35 #include "pagectrl.h"
   36 
   37 #ifndef LDAP_CONTROL_PAGE_OID
   38 #define LDAP_CONTROL_PAGE_OID           "1.2.840.113556.1.4.319"
   39 #endif
   40 
   41 #ifndef HAVE_LDAP_CREATE_PAGE_CONTROL
   42 /*---
   43    ldap_create_page_control
   44    
   45    Create and encode the Paged Results control.
   46 
   47    ld        (IN)  An LDAP session handle, as obtained from a call to
   48                    ldap_init().
   49    
   50    pagesize  (IN)  The number of entries to return in each page
   51 
   52    cookiep   (IN)  Pointer to a berVal structure that the server uses to
   53                                    determine the current location in the
   54                                    result set (opaque). Set to NULL the
   55                                    first time.
   56                                  
   57    iscritical (IN) Is this control critical to the search?
   58    
   59    ctrlp     (OUT) A result parameter that will be assigned the address
   60                    of an LDAPControl structure that contains the 
   61                    PagedResult control created by this function.
   62                    The memory occupied by the LDAPControl structure
   63                    SHOULD be freed when it is no longer in use by
   64                    calling ldap_control_free().
   65                       
   66    
   67    Ber encoding
   68    
   69    PageResult ::= SEQUENCE {
   70         pageSize     INTEGER
   71         cookie       OCTET STRING }
   72       
   73    
   74    Note:  The first time the Page control is created, the cookie
   75           should be set to a zero-length string. The cookie obtained
   76           from calling ldap_parse_page_control() should be used as
   77           the cookie in the next ldap_create_page_control call.
   78 
   79  ---*/
   80 
   81 #ifndef HAVE_LDAP_CREATE_CONTROL
   82 #error LDAP client library does not support ldap_create_control()
   83 #else
   84 int
   85 ldap_create_page_control (LDAP * ld,
   86               unsigned long pagesize,
   87               struct berval *cookiep,
   88               int iscritical, LDAPControl ** ctrlp)
   89 {
   90   ber_tag_t tag;
   91   BerElement *ber;
   92   BerElement *ldap_alloc_ber_with_options (LDAP * ld);
   93   int rc;
   94 
   95   if ((ld == NULL) || (ctrlp == NULL))
   96     {
   97       return (LDAP_PARAM_ERROR);
   98     }
   99 
  100   if ((ber = ldap_alloc_ber_with_options (ld)) == NULL)
  101     {
  102       return (LDAP_NO_MEMORY);
  103     }
  104 
  105   tag = ber_printf (ber, "{i", pagesize);
  106   if (tag == LBER_ERROR)
  107     goto exit;
  108 
  109   if (cookiep == NULL)
  110     tag = ber_printf (ber, "o", "", 0);
  111   else
  112     tag = ber_printf (ber, "O", cookiep);
  113   if (tag == LBER_ERROR)
  114     goto exit;
  115 
  116   tag = ber_printf (ber, /*{ */ "N}");
  117   if (tag == LBER_ERROR)
  118     goto exit;
  119 
  120   rc = ldap_create_control (LDAP_CONTROL_PAGE_OID, ber, iscritical, ctrlp);
  121 
  122   ber_free (ber, 1);
  123   return (rc);
  124 
  125 exit:
  126   ber_free (ber, 1);
  127   return (LDAP_ENCODING_ERROR);
  128 }
  129 #endif /* HAVE_LDAP_CREATE_CONTROL */
  130 #endif /* HAVE_LDAP_CREATE_PAGE_CONTROL */
  131 
  132 #ifndef HAVE_LDAP_PARSE_PAGE_CONTROL
  133 /*---
  134    ldap_parse_page_control
  135    
  136    Decode the Virtual List View control return information.
  137 
  138    ld           (IN)   An LDAP session handle.
  139    
  140    ctrls        (IN)   The address of a NULL-terminated array of 
  141                        LDAPControl structures, typically obtained 
  142                        by a call to ldap_parse_result().
  143    
  144    list_countp  (OUT)  This result parameter is filled in with the number
  145                        of entries returned in this page
  146    
  147    cookiep      (OUT)  This result parameter is filled in with the address
  148                        of a struct berval that contains the server-
  149                        generated cookie.
  150                        The returned cookie SHOULD be used in the next call
  151                        to create a Page sort control.  The struct berval
  152                        returned SHOULD be disposed of by calling ber_bvfree()
  153                        when it is no longer needed.
  154    
  155 ---*/
  156 
  157 #ifndef HAVE_LDAP_CREATE_CONTROL
  158 #error LDAP client library does not support ldap_create_control()
  159 #else
  160 int
  161 ldap_parse_page_control (LDAP * ld,
  162              LDAPControl ** ctrls,
  163              unsigned long *list_countp, struct berval **cookiep)
  164 {
  165   BerElement *ber;
  166   LDAPControl *pControl;
  167   int i;
  168   unsigned long count;
  169   ber_tag_t tag;
  170 
  171   if (cookiep)
  172     {
  173       *cookiep = NULL;      /* Make sure we return a NULL if error occurs. */
  174     }
  175 
  176   if (ld == NULL)
  177     {
  178       return (LDAP_PARAM_ERROR);
  179     }
  180 
  181   if (ctrls == NULL)
  182     {
  183       return (LDAP_CONTROL_NOT_FOUND);
  184     }
  185 
  186   /* Search the list of control responses for a Page control. */
  187   for (i = 0; ctrls[i]; i++)
  188     {
  189       pControl = ctrls[i];
  190       if (!strcmp (LDAP_CONTROL_PAGE_OID, pControl->ldctl_oid))
  191     goto foundPageControl;
  192     }
  193 
  194   /* No page control was found. */
  195   return (LDAP_CONTROL_NOT_FOUND);
  196 
  197 foundPageControl:
  198   /* Create a BerElement from the berval returned in the control. */
  199   ber = ber_init (&pControl->ldctl_value);
  200 
  201   if (ber == NULL)
  202     {
  203       return (LDAP_NO_MEMORY);
  204     }
  205 
  206   /* Extract the data returned in the control. */
  207   tag = ber_scanf (ber, "{iO" /*} */ , &count, cookiep);
  208 
  209   if (tag == LBER_ERROR)
  210     {
  211       ber_free (ber, 1);
  212       return (LDAP_DECODING_ERROR);
  213     }
  214 
  215   ber_free (ber, 1);
  216 
  217   /* Return data to the caller for items that were requested. */
  218   if (list_countp)
  219     {
  220       *list_countp = count;
  221     }
  222 
  223   return (LDAP_SUCCESS);
  224 }
  225 #endif /* HAVE_LDAP_CREATE_CONTROL */
  226 #endif /* HAVE_LDAP_PARSE_PAGE_CONTROL */