1*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gate /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public License 6*0Sstevel@tonic-gate * Version 1.0 (the "NPL"); you may not use this file except in 7*0Sstevel@tonic-gate * compliance with the NPL. You may obtain a copy of the NPL at 8*0Sstevel@tonic-gate * http://www.mozilla.org/NPL/ 9*0Sstevel@tonic-gate * 10*0Sstevel@tonic-gate * Software distributed under the NPL is distributed on an "AS IS" basis, 11*0Sstevel@tonic-gate * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL 12*0Sstevel@tonic-gate * for the specific language governing rights and limitations under the 13*0Sstevel@tonic-gate * NPL. 14*0Sstevel@tonic-gate * 15*0Sstevel@tonic-gate * The Initial Developer of this code under the NPL is Netscape 16*0Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are 17*0Sstevel@tonic-gate * Copyright (C) 1998 Netscape Communications Corporation. All Rights 18*0Sstevel@tonic-gate * Reserved. 19*0Sstevel@tonic-gate */ 20*0Sstevel@tonic-gate /* 21*0Sstevel@tonic-gate * psearch.c - Persistent search and "Entry Change Notification" support. 22*0Sstevel@tonic-gate */ 23*0Sstevel@tonic-gate #include "ldap-int.h" 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate int 27*0Sstevel@tonic-gate LDAP_CALL 28*0Sstevel@tonic-gate ldap_create_persistentsearch_control( LDAP *ld, int changetypes, 29*0Sstevel@tonic-gate int changesonly, int return_echg_ctls, char ctl_iscritical, 30*0Sstevel@tonic-gate LDAPControl **ctrlp ) 31*0Sstevel@tonic-gate { 32*0Sstevel@tonic-gate BerElement *ber; 33*0Sstevel@tonic-gate int rc; 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) { 36*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 37*0Sstevel@tonic-gate } 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate if ( ctrlp == NULL || ( changetypes & ~LDAP_CHANGETYPE_ANY ) != 0 ) { 40*0Sstevel@tonic-gate rc = LDAP_PARAM_ERROR; 41*0Sstevel@tonic-gate goto report_error_and_return; 42*0Sstevel@tonic-gate } 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate /* 45*0Sstevel@tonic-gate * create a Persistent Search control. The control value looks like this: 46*0Sstevel@tonic-gate * 47*0Sstevel@tonic-gate * PersistentSearch ::= SEQUENCE { 48*0Sstevel@tonic-gate * changeTypes INTEGER, 49*0Sstevel@tonic-gate * -- the changeTypes field is the logical OR of 50*0Sstevel@tonic-gate * -- one or more of these values: add (1), delete (2), 51*0Sstevel@tonic-gate * -- modify (4), modDN (8). It specifies which types of 52*0Sstevel@tonic-gate * -- changes will cause an entry to be returned. 53*0Sstevel@tonic-gate * changesOnly BOOLEAN, -- skip initial search? 54*0Sstevel@tonic-gate * returnECs BOOLEAN, -- return "Entry Change" controls? 55*0Sstevel@tonic-gate * } 56*0Sstevel@tonic-gate */ 57*0Sstevel@tonic-gate if (( nsldapi_alloc_ber_with_options( ld, &ber )) != LDAP_SUCCESS ) { 58*0Sstevel@tonic-gate rc = LDAP_NO_MEMORY; 59*0Sstevel@tonic-gate goto report_error_and_return; 60*0Sstevel@tonic-gate } 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate if ( ber_printf( ber, "{ibb}", changetypes, changesonly, 63*0Sstevel@tonic-gate return_echg_ctls ) == -1 ) { 64*0Sstevel@tonic-gate ber_free( ber, 1 ); 65*0Sstevel@tonic-gate rc = LDAP_ENCODING_ERROR; 66*0Sstevel@tonic-gate goto report_error_and_return; 67*0Sstevel@tonic-gate } 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate rc = nsldapi_build_control( LDAP_CONTROL_PERSISTENTSEARCH, ber, 1, 70*0Sstevel@tonic-gate ctl_iscritical, ctrlp ); 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate report_error_and_return: 73*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, rc, NULL, NULL ); 74*0Sstevel@tonic-gate return( rc ); 75*0Sstevel@tonic-gate } 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate int 79*0Sstevel@tonic-gate LDAP_CALL 80*0Sstevel@tonic-gate ldap_parse_entrychange_control( LDAP *ld, LDAPControl **ctrls, int *chgtypep, 81*0Sstevel@tonic-gate char **prevdnp, int *chgnumpresentp, ber_int_t *chgnump ) 82*0Sstevel@tonic-gate { 83*0Sstevel@tonic-gate BerElement *ber; 84*0Sstevel@tonic-gate int rc, i, changetype; 85*0Sstevel@tonic-gate ber_len_t len; 86*0Sstevel@tonic-gate ber_int_t along; 87*0Sstevel@tonic-gate char *previousdn; 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) { 90*0Sstevel@tonic-gate return( LDAP_PARAM_ERROR ); 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate /* 94*0Sstevel@tonic-gate * find the entry change notification in the list of controls 95*0Sstevel@tonic-gate */ 96*0Sstevel@tonic-gate for ( i = 0; ctrls != NULL && ctrls[i] != NULL; ++i ) { 97*0Sstevel@tonic-gate if ( strcmp( ctrls[i]->ldctl_oid, LDAP_CONTROL_ENTRYCHANGE ) == 0 ) { 98*0Sstevel@tonic-gate break; 99*0Sstevel@tonic-gate } 100*0Sstevel@tonic-gate } 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate if ( ctrls == NULL || ctrls[i] == NULL ) { 103*0Sstevel@tonic-gate rc = LDAP_CONTROL_NOT_FOUND; 104*0Sstevel@tonic-gate goto report_error_and_return; 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate /* 108*0Sstevel@tonic-gate * allocate a BER element from the control value and parse it. The control 109*0Sstevel@tonic-gate * value should look like this: 110*0Sstevel@tonic-gate * 111*0Sstevel@tonic-gate * EntryChangeNotification ::= SEQUENCE { 112*0Sstevel@tonic-gate * changeType ENUMERATED { 113*0Sstevel@tonic-gate * add (1), -- these values match the 114*0Sstevel@tonic-gate * delete (2), -- values used for changeTypes 115*0Sstevel@tonic-gate * modify (4), -- in the PersistentSearch control. 116*0Sstevel@tonic-gate * modDN (8), 117*0Sstevel@tonic-gate * }, 118*0Sstevel@tonic-gate * previousDN LDAPDN OPTIONAL, -- modDN ops. only 119*0Sstevel@tonic-gate * changeNumber INTEGER OPTIONAL, -- if supported 120*0Sstevel@tonic-gate * } 121*0Sstevel@tonic-gate */ 122*0Sstevel@tonic-gate if (( ber = ber_init( &(ctrls[i]->ldctl_value))) == NULL ) { 123*0Sstevel@tonic-gate rc = LDAP_NO_MEMORY; 124*0Sstevel@tonic-gate goto report_error_and_return; 125*0Sstevel@tonic-gate } 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate if ( ber_scanf( ber, "{e", &along ) == LBER_ERROR ) { 128*0Sstevel@tonic-gate ber_free( ber, 1 ); 129*0Sstevel@tonic-gate rc = LDAP_DECODING_ERROR; 130*0Sstevel@tonic-gate goto report_error_and_return; 131*0Sstevel@tonic-gate } 132*0Sstevel@tonic-gate changetype = (int)along; /* XXX lossy cast */ 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate if ( changetype == LDAP_CHANGETYPE_MODDN ) { 135*0Sstevel@tonic-gate if ( ber_scanf( ber, "a", &previousdn ) == LBER_ERROR ) { 136*0Sstevel@tonic-gate ber_free( ber, 1 ); 137*0Sstevel@tonic-gate rc = LDAP_DECODING_ERROR; 138*0Sstevel@tonic-gate goto report_error_and_return; 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate } else { 141*0Sstevel@tonic-gate previousdn = NULL; 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate if ( chgtypep != NULL ) { 145*0Sstevel@tonic-gate *chgtypep = changetype; 146*0Sstevel@tonic-gate } 147*0Sstevel@tonic-gate if ( prevdnp != NULL ) { 148*0Sstevel@tonic-gate *prevdnp = previousdn; 149*0Sstevel@tonic-gate } else if ( previousdn != NULL ) { 150*0Sstevel@tonic-gate NSLDAPI_FREE( previousdn ); 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate if ( chgnump != NULL ) { /* check for optional changenumber */ 154*0Sstevel@tonic-gate if ( ber_peek_tag( ber, &len ) == LBER_INTEGER 155*0Sstevel@tonic-gate && ber_get_int( ber, chgnump ) != LBER_ERROR ) { 156*0Sstevel@tonic-gate if ( chgnumpresentp != NULL ) { 157*0Sstevel@tonic-gate *chgnumpresentp = 1; 158*0Sstevel@tonic-gate } 159*0Sstevel@tonic-gate } else { 160*0Sstevel@tonic-gate if ( chgnumpresentp != NULL ) { 161*0Sstevel@tonic-gate *chgnumpresentp = 0; 162*0Sstevel@tonic-gate } 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate } 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate ber_free( ber, 1 ); 167*0Sstevel@tonic-gate rc = LDAP_SUCCESS; 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate report_error_and_return: 170*0Sstevel@tonic-gate LDAP_SET_LDERRNO( ld, rc, NULL, NULL ); 171*0Sstevel@tonic-gate return( rc ); 172*0Sstevel@tonic-gate } 173