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