From b715720d366164532f1c9d65cfffb7134da416be Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Thu, 31 Jan 2008 22:04:10 +0000 Subject: move pagectrl code into compat directory git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-ldapd@592 ef36b2f9-881f-0410-afb5-c4e39611909c --- compat/Makefile.am | 3 +- compat/pagectrl.c | 217 +++++++++++++++++++++++++++++++++++++++++++++++++++++ compat/pagectrl.h | 45 +++++++++++ 3 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 compat/pagectrl.c create mode 100644 compat/pagectrl.h (limited to 'compat') diff --git a/compat/Makefile.am b/compat/Makefile.am index 54e9be1..d71d087 100644 --- a/compat/Makefile.am +++ b/compat/Makefile.am @@ -23,7 +23,8 @@ AM_CPPFLAGS=-I$(top_srcdir) AM_CFLAGS = -fPIC EXTRA_DIST = getopt_long.c getopt_long.h \ - daemon.c daemon.h + daemon.c daemon.h \ + pagectrl.c pagectrl.h libcompat_a_SOURCES = getpeercred.c getpeercred.h libcompat_a_LIBADD = @LIBOBJS@ diff --git a/compat/pagectrl.c b/compat/pagectrl.c new file mode 100644 index 0000000..9dff696 --- /dev/null +++ b/compat/pagectrl.c @@ -0,0 +1,217 @@ +/* + pagectrl.c - provide a replacement ldap_create_page_control() function. + This file was part of the nss_ldap library which has been + forked into the nss-ldapd library. + + Copyright (C) 2002 Max Caines + This software is not subject to any license of the University + of Wolverhampton. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#include "pagectrl.h" + +#ifndef LDAP_CONTROL_PAGE_OID +#define LDAP_CONTROL_PAGE_OID "1.2.840.113556.1.4.319" +#endif + +#ifndef HAVE_LDAP_CREATE_PAGE_CONTROL +/*--- + ldap_create_page_control + + Create and encode the Paged Results control. + + ld (IN) An LDAP session handle, as obtained from a call to + ldap_init(). + + pagesize (IN) The number of entries to return in each page + + cookiep (IN) Pointer to a berVal structure that the server uses to + determine the current location in the + result set (opaque). Set to NULL the + first time. + + iscritical (IN) Is this control critical to the search? + + ctrlp (OUT) A result parameter that will be assigned the address + of an LDAPControl structure that contains the + PagedResult control created by this function. + The memory occupied by the LDAPControl structure + SHOULD be freed when it is no longer in use by + calling ldap_control_free(). + + + Ber encoding + + PageResult ::= SEQUENCE { + pageSize INTEGER + cookie OCTET STRING } + + + Note: The first time the Page control is created, the cookie + should be set to a zero-length string. The cookie obtained + from calling ldap_parse_page_control() should be used as + the cookie in the next ldap_create_page_control call. + + ---*/ + +int +ldap_create_page_control (LDAP * ld, + unsigned long pagesize, + struct berval *cookiep, + int iscritical, LDAPControl ** ctrlp) +{ + ber_tag_t tag; + BerElement *ber; + BerElement *ldap_alloc_ber_with_options (LDAP * ld); + int rc; + + if ((ld == NULL) || (ctrlp == NULL)) + { + return (LDAP_PARAM_ERROR); + } + + if ((ber = ldap_alloc_ber_with_options (ld)) == NULL) + { + return (LDAP_NO_MEMORY); + } + + tag = ber_printf (ber, "{i", pagesize); + if (tag == LBER_ERROR) + goto exit; + + if (cookiep == NULL) + tag = ber_printf (ber, "o", "", 0); + else + tag = ber_printf (ber, "O", cookiep); + if (tag == LBER_ERROR) + goto exit; + + tag = ber_printf (ber, /*{ */ "N}"); + if (tag == LBER_ERROR) + goto exit; + + rc = ldap_create_control (LDAP_CONTROL_PAGE_OID, ber, iscritical, ctrlp); + + ber_free (ber, 1); + return (rc); + +exit: + ber_free (ber, 1); + return (LDAP_ENCODING_ERROR); +} +#endif /* not HAVE_LDAP_CREATE_PAGE_CONTROL */ + +#ifndef HAVE_LDAP_PARSE_PAGE_CONTROL +/*--- + ldap_parse_page_control + + Decode the Virtual List View control return information. + + ld (IN) An LDAP session handle. + + ctrls (IN) The address of a NULL-terminated array of + LDAPControl structures, typically obtained + by a call to ldap_parse_result(). + + list_countp (OUT) This result parameter is filled in with the number + of entries returned in this page + + cookiep (OUT) This result parameter is filled in with the address + of a struct berval that contains the server- + generated cookie. + The returned cookie SHOULD be used in the next call + to create a Page sort control. The struct berval + returned SHOULD be disposed of by calling ber_bvfree() + when it is no longer needed. + +---*/ +int +ldap_parse_page_control (LDAP * ld, + LDAPControl ** ctrls, + unsigned long *list_countp, struct berval **cookiep) +{ + BerElement *ber; + LDAPControl *pControl; + int i; + unsigned long count; + ber_tag_t tag; + + if (cookiep) + { + *cookiep = NULL; /* Make sure we return a NULL if error occurs. */ + } + + if (ld == NULL) + { + return (LDAP_PARAM_ERROR); + } + + if (ctrls == NULL) + { + return (LDAP_CONTROL_NOT_FOUND); + } + + /* Search the list of control responses for a Page control. */ + for (i = 0; ctrls[i]; i++) + { + pControl = ctrls[i]; + if (!strcmp (LDAP_CONTROL_PAGE_OID, pControl->ldctl_oid)) + goto foundPageControl; + } + + /* No page control was found. */ + return (LDAP_CONTROL_NOT_FOUND); + +foundPageControl: + /* Create a BerElement from the berval returned in the control. */ + ber = ber_init (&pControl->ldctl_value); + + if (ber == NULL) + { + return (LDAP_NO_MEMORY); + } + + /* Extract the data returned in the control. */ + tag = ber_scanf (ber, "{iO" /*} */ , &count, cookiep); + + if (tag == LBER_ERROR) + { + ber_free (ber, 1); + return (LDAP_DECODING_ERROR); + } + + ber_free (ber, 1); + + /* Return data to the caller for items that were requested. */ + if (list_countp) + { + *list_countp = count; + } + + return (LDAP_SUCCESS); +} +#endif /* not HAVE_LDAP_PARSE_PAGE_CONTROL */ diff --git a/compat/pagectrl.h b/compat/pagectrl.h new file mode 100644 index 0000000..cd64b77 --- /dev/null +++ b/compat/pagectrl.h @@ -0,0 +1,45 @@ +/* + pagectrl.h - provide a replacement ldap_create_page_control() function. + This file was part of the nss_ldap library which has been + forked into the nss-ldapd library. + + Copyright (C) 1997-2005 Luke Howard + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#ifndef _LDAP_NSS_LDAP_PAGECTRL_H +#define _LDAP_NSS_LDAP_PAGECTRL_H + +#ifndef HAVE_LDAP_CREATE_PAGE_CONTROL +int +ldap_create_page_control( LDAP *ld, + unsigned long pagesize, + struct berval *cookiep, + int iscritical, + LDAPControl **ctrlp ); +#endif /* not HAVE_LDAP_CREATE_PAGE_CONTROL */ + +#ifndef HAVE_LDAP_PARSE_PAGE_CONTROL +int +ldap_parse_page_control( + LDAP *ld, + LDAPControl **ctrls, + unsigned long *list_countp, + struct berval **cookiep ); +#endif /* not HAVE_LDAP_PARSE_PAGE_CONTROL */ + +#endif /* _LDAP_NSS_LDAP_UTIL_H */ -- cgit v1.2.3