Back to index

lightning-sunbird  0.9+nobinonly
psearch.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  * psearch.c - Persistent search and "Entry Change Notification" support.
00039  */
00040 #include "ldap-int.h"
00041 
00042 
00043 int
00044 LDAP_CALL
00045 ldap_create_persistentsearch_control( LDAP *ld, int changetypes,
00046     int changesonly, int return_echg_ctls, char ctl_iscritical,
00047     LDAPControl **ctrlp )
00048 {
00049     BerElement       *ber;
00050     int              rc;
00051 
00052     if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00053        return( LDAP_PARAM_ERROR );
00054     }
00055 
00056     if ( ctrlp == NULL || ( changetypes & ~LDAP_CHANGETYPE_ANY ) != 0 ) {
00057        rc = LDAP_PARAM_ERROR;
00058        goto report_error_and_return;
00059     }
00060 
00061     /*
00062      * create a Persistent Search control.  The control value looks like this:
00063      *
00064      * PersistentSearch ::= SEQUENCE {
00065      *        changeTypes INTEGER,
00066      *        -- the changeTypes field is the logical OR of 
00067      *        -- one or more of these values: add (1), delete (2),
00068      *        -- modify (4), modDN (8).  It specifies which types of
00069      *        -- changes will cause an entry to be returned.
00070      *        changesOnly BOOLEAN, -- skip initial search?
00071      *        returnECs BOOLEAN,   -- return "Entry Change" controls?
00072      * }
00073      */
00074     if (( nsldapi_alloc_ber_with_options( ld, &ber )) != LDAP_SUCCESS ) {
00075        rc = LDAP_NO_MEMORY;
00076        goto report_error_and_return;
00077     }
00078 
00079     if ( ber_printf( ber, "{ibb}", changetypes, changesonly,
00080            return_echg_ctls ) == -1 ) {
00081        ber_free( ber, 1 );
00082        rc = LDAP_ENCODING_ERROR;
00083        goto report_error_and_return;
00084     }
00085 
00086     rc = nsldapi_build_control( LDAP_CONTROL_PERSISTENTSEARCH, ber, 1,
00087            ctl_iscritical, ctrlp );
00088 
00089 report_error_and_return:
00090     LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
00091     return( rc );
00092 }
00093 
00094 
00095 int
00096 LDAP_CALL
00097 ldap_parse_entrychange_control( LDAP *ld, LDAPControl **ctrls, int *chgtypep,
00098     char **prevdnp, int *chgnumpresentp, long *chgnump )
00099 {
00100     BerElement              *ber;
00101     int                     rc, i, changetype;
00102     unsigned long    len;
00103     long             along;
00104     char             *previousdn;
00105 
00106     if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00107        return( LDAP_PARAM_ERROR );
00108     }
00109 
00110     /*
00111      * find the entry change notification in the list of controls
00112      */
00113     for ( i = 0; ctrls != NULL && ctrls[i] != NULL; ++i ) {
00114        if ( strcmp( ctrls[i]->ldctl_oid, LDAP_CONTROL_ENTRYCHANGE ) == 0 ) {
00115            break;
00116        }
00117     }
00118 
00119     if ( ctrls == NULL || ctrls[i] == NULL ) {
00120        rc = LDAP_CONTROL_NOT_FOUND;
00121        goto report_error_and_return;
00122     }
00123 
00124     /*
00125      * allocate a BER element from the control value and parse it.  The control
00126      * value should look like this:
00127      *
00128      * EntryChangeNotification ::= SEQUENCE {
00129      *      changeType ENUMERATED {
00130      *        add             (1),  -- these values match the
00131      *        delete          (2),  -- values used for changeTypes
00132      *        modify          (4),  -- in the PersistentSearch control.
00133      *        modDN           (8),
00134      *      },
00135      *      previousDN   LDAPDN OPTIONAL,     -- modDN ops. only
00136      *      changeNumber INTEGER OPTIONAL,    -- if supported
00137      * }
00138      */
00139     if (( ber = ber_init( &(ctrls[i]->ldctl_value))) == NULL ) {
00140        rc = LDAP_NO_MEMORY;
00141        goto report_error_and_return;
00142     }         
00143 
00144     if ( ber_scanf( ber, "{e", &along ) == LBER_ERROR ) {
00145        ber_free( ber, 1 );
00146        rc = LDAP_DECODING_ERROR;
00147        goto report_error_and_return;
00148     }
00149     changetype = (int)along;       /* XXX lossy cast */
00150 
00151     if ( changetype == LDAP_CHANGETYPE_MODDN ) {
00152        if ( ber_scanf( ber, "a", &previousdn ) == LBER_ERROR ) {
00153            ber_free( ber, 1 );
00154            rc = LDAP_DECODING_ERROR;
00155            goto report_error_and_return;
00156        }
00157     } else {
00158        previousdn = NULL;
00159     }
00160 
00161     if ( chgtypep != NULL ) {
00162        *chgtypep = changetype;
00163     }
00164     if ( prevdnp != NULL ) {
00165        *prevdnp = previousdn;
00166     } else if ( previousdn != NULL ) {
00167        NSLDAPI_FREE( previousdn );
00168     }
00169 
00170     if ( chgnump != NULL ) {       /* check for optional changenumber */
00171        if ( ber_peek_tag( ber, &len ) == LBER_INTEGER
00172               && ber_get_int( ber, chgnump ) != LBER_ERROR ) {
00173            if ( chgnumpresentp != NULL ) {
00174               *chgnumpresentp = 1;
00175            }
00176        } else {
00177            if ( chgnumpresentp != NULL ) {
00178               *chgnumpresentp = 0;
00179            }
00180        }
00181     }
00182 
00183     ber_free( ber, 1 );
00184     rc = LDAP_SUCCESS;
00185 
00186 report_error_and_return:
00187     LDAP_SET_LDERRNO( ld, rc, NULL, NULL );
00188     return( rc );
00189 }