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