3 * Copyright 1999-2002 The OpenLDAP Foundation.
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
15 #include <ac/string.h>
16 #include <ac/socket.h>
20 #include "../../libraries/liblber/lber-int.h"
22 #define SLAP_CTRL_FRONTEND 0x80000000U
24 #define SLAP_CTRL_OPFLAGS 0x0000FFFFU
25 #define SLAP_CTRL_ABANDON 0x00000001U
26 #define SLAP_CTRL_ADD 0x00002002U
27 #define SLAP_CTRL_BIND 0x00000004U
28 #define SLAP_CTRL_COMPARE 0x00001008U
29 #define SLAP_CTRL_DELETE 0x00002010U
30 #define SLAP_CTRL_MODIFY 0x00002020U
31 #define SLAP_CTRL_RENAME 0x00002040U
32 #define SLAP_CTRL_SEARCH 0x00001080U
33 #define SLAP_CTRL_UNBIND 0x00000100U
35 #define SLAP_CTRL_INTROGATE (SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH)
36 #define SLAP_CTRL_UPDATE \
37 (SLAP_CTRL_ADD|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME)
38 #define SLAP_CTRL_ACCESS (SLAP_CTRL_INTROGATE|SLAP_CTRL_UPDATE)
40 typedef int (SLAP_CTRL_PARSE_FN) LDAP_P((
46 static SLAP_CTRL_PARSE_FN parseManageDSAit;
47 static SLAP_CTRL_PARSE_FN parseSubentries;
48 static SLAP_CTRL_PARSE_FN parseNoOp;
50 static struct slap_control {
53 char **sc_extendedops;
54 SLAP_CTRL_PARSE_FN *sc_parse;
56 } supportedControls[] = {
57 { LDAP_CONTROL_MANAGEDSAIT,
58 SLAP_CTRL_ACCESS, NULL,
60 { LDAP_CONTROL_SUBENTRIES,
61 SLAP_CTRL_SEARCH, NULL,
63 #ifdef LDAP_CONTROL_NOOP
65 SLAP_CTRL_UPDATE, NULL,
72 get_supported_ctrl(int index)
74 return supportedControls[index].sc_oid;
77 static struct slap_control *
78 find_ctrl( const char *oid )
81 for( i=0; supportedControls[i].sc_oid; i++ ) {
82 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
83 return &supportedControls[i];
98 BerElement *ber = op->o_ber;
99 LDAPControl ***ctrls = &op->o_ctrls;
100 struct slap_control *c;
101 int rc = LDAP_SUCCESS;
102 const char *errmsg = NULL;
104 len = ber_pvt_ber_remaining(ber);
112 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
113 if( tag == LBER_ERROR ) {
114 rc = SLAPD_DISCONNECT;
115 errmsg = "unexpected data in PDU";
122 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
123 "get_ctrls: conn %d\n", conn->c_connid ));
125 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
127 if( op->o_protocol < LDAP_VERSION3 ) {
128 rc = SLAPD_DISCONNECT;
129 errmsg = "controls require LDAPv3";
133 /* set through each element */
134 *ctrls = ch_malloc( 1 * sizeof(LDAPControl *) );
137 if( *ctrls == NULL ) {
139 errmsg = "no memory";
144 *ctrls[nctrls] = NULL;
146 for( tag = ber_first_element( ber, &len, &opaque );
148 tag = ber_next_element( ber, &len, opaque ) )
151 LDAPControl **tctrls;
153 tctrl = ch_calloc( 1, sizeof(LDAPControl) );
154 tctrl->ldctl_oid = NULL;
155 tctrl->ldctl_value.bv_val = NULL;
157 /* allocate pointer space for current controls (nctrls)
158 * + this control + extra NULL
160 tctrls = (tctrl == NULL) ? NULL :
161 ch_realloc(*ctrls, (nctrls+2) * sizeof(LDAPControl *));
164 if( tctrls == NULL ) {
165 /* one of the above allocation failed */
167 if( tctrl != NULL ) {
171 ldap_controls_free(*ctrls);
175 errmsg = "no memory";
180 tctrls[nctrls++] = tctrl;
181 tctrls[nctrls] = NULL;
183 tag = ber_scanf( ber, "{a" /*}*/, &tctrl->ldctl_oid );
185 if( tag == LBER_ERROR ) {
187 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
188 "get_ctrls: conn %d get OID failed.\n",
191 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
195 ldap_controls_free( tctrls );
196 rc = SLAPD_DISCONNECT;
197 errmsg = "decoding controls error";
201 tag = ber_peek_tag( ber, &len );
203 if( tag == LBER_BOOLEAN ) {
205 tag = ber_scanf( ber, "b", &crit );
207 if( tag == LBER_ERROR ) {
209 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
210 "get_ctrls: conn %d get crit failed.\n",
213 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
217 ldap_controls_free( tctrls );
218 rc = SLAPD_DISCONNECT;
219 errmsg = "decoding controls error";
223 tctrl->ldctl_iscritical = (crit != 0);
224 tag = ber_peek_tag( ber, &len );
228 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
229 "get_ctrls: conn %d oid=\"%s\" (%scritical)\n",
230 conn->c_connid, tctrl->ldctl_oid,
231 tctrl->ldctl_iscritical ? "" : "non" ));
233 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
235 tctrl->ldctl_iscritical ? "" : "non",
238 if( tag == LBER_OCTETSTRING ) {
239 tag = ber_scanf( ber, "o", &tctrl->ldctl_value );
241 if( tag == LBER_ERROR ) {
243 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
244 "get_ctrls: conn %d get value failed.\n", conn->c_connid ));
246 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get value failed.\n",
250 ldap_controls_free( tctrls );
251 rc = SLAPD_DISCONNECT;
252 errmsg = "decoding controls error";
257 c = find_ctrl( tctrl->ldctl_oid );
259 /* recognized control */
261 switch( op->o_tag ) {
263 tagmask = SLAP_CTRL_ADD;
266 tagmask = SLAP_CTRL_BIND;
268 case LDAP_REQ_COMPARE:
269 tagmask = SLAP_CTRL_COMPARE;
271 case LDAP_REQ_DELETE:
272 tagmask = SLAP_CTRL_DELETE;
274 case LDAP_REQ_MODIFY:
275 tagmask = SLAP_CTRL_MODIFY;
277 case LDAP_REQ_RENAME:
278 tagmask = SLAP_CTRL_RENAME;
280 case LDAP_REQ_SEARCH:
281 tagmask = SLAP_CTRL_SEARCH;
283 case LDAP_REQ_UNBIND:
284 tagmask = SLAP_CTRL_UNBIND;
286 case LDAP_REQ_EXTENDED:
287 /* FIXME: check list of extended operations */
292 errmsg = "controls internal error";
296 if (( c->sc_mask & tagmask ) == tagmask ) {
297 /* available extension */
301 errmsg = "not yet implemented";
305 rc = c->sc_parse( conn, op, tctrl, &errmsg );
307 if( rc != LDAP_SUCCESS ) goto return_results;
309 if( c->sc_mask & SLAP_CTRL_FRONTEND ) {
310 /* kludge to disable backend_control() check */
311 tctrl->ldctl_iscritical = 0;
314 } else if( tctrl->ldctl_iscritical ) {
315 /* unavailable CRITICAL control */
316 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
317 errmsg = "critical extension is unavailable";
321 } else if( tctrl->ldctl_iscritical ) {
322 /* unrecognized CRITICAL control */
323 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
324 errmsg = "critical extension is not recognized";
333 LDAP_LOG(( "operation", LDAP_LEVEL_RESULTS,
334 "get_ctrls: conn %d %d %d %s\n",
335 conn->c_connid, nctrls, rc, errmsg ? errmsg : "" ));
337 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: %d %d %s\n",
338 nctrls, rc, errmsg ? errmsg : "");
341 if( sendres && rc != LDAP_SUCCESS ) {
342 if( rc == SLAPD_DISCONNECT ) {
343 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
345 send_ldap_result( conn, op, rc,
346 NULL, errmsg, NULL, NULL );
353 static int parseManageDSAit (
359 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
360 *text = "manageDSAit control specified multiple times";
361 return LDAP_PROTOCOL_ERROR;
364 if ( ctrl->ldctl_value.bv_len ) {
365 *text = "manageDSAit control value not empty";
366 return LDAP_PROTOCOL_ERROR;
369 op->o_managedsait = ctrl->ldctl_iscritical
370 ? SLAP_CRITICAL_CONTROL
371 : SLAP_NONCRITICAL_CONTROL;
376 static int parseSubentries (
382 if ( op->o_subentries != SLAP_NO_CONTROL ) {
383 *text = "subentries control specified multiple times";
384 return LDAP_PROTOCOL_ERROR;
387 /* FIXME: should use BER library */
388 if( ( ctrl->ldctl_value.bv_len != 3 )
389 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
390 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
392 *text = "subentries control value encoding is bogus";
393 return LDAP_PROTOCOL_ERROR;
396 op->o_subentries = ctrl->ldctl_iscritical
397 ? SLAP_CRITICAL_CONTROL
398 : SLAP_NONCRITICAL_CONTROL;
400 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
405 static int parseNoOp (
411 if ( op->o_noop != SLAP_NO_CONTROL ) {
412 *text = "noop control specified multiple times";
413 return LDAP_PROTOCOL_ERROR;
416 if ( ctrl->ldctl_value.bv_len ) {
417 *text = "noop control value not empty";
418 return LDAP_PROTOCOL_ERROR;
421 op->o_noop = ctrl->ldctl_iscritical
422 ? SLAP_CRITICAL_CONTROL
423 : SLAP_NONCRITICAL_CONTROL;