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
23 #define SLAP_CTRL_FRONTEND_SEARCH 0x01000000U /* for NOOP */
25 #define SLAP_CTRL_OPFLAGS 0x0000FFFFU
26 #define SLAP_CTRL_ABANDON 0x00000001U
27 #define SLAP_CTRL_ADD 0x00002002U
28 #define SLAP_CTRL_BIND 0x00000004U
29 #define SLAP_CTRL_COMPARE 0x00001008U
30 #define SLAP_CTRL_DELETE 0x00002010U
31 #define SLAP_CTRL_MODIFY 0x00002020U
32 #define SLAP_CTRL_RENAME 0x00002040U
33 #define SLAP_CTRL_SEARCH 0x00001080U
34 #define SLAP_CTRL_UNBIND 0x00000100U
36 #define SLAP_CTRL_INTROGATE (SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH)
37 #define SLAP_CTRL_UPDATE \
38 (SLAP_CTRL_ADD|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME)
39 #define SLAP_CTRL_ACCESS (SLAP_CTRL_INTROGATE|SLAP_CTRL_UPDATE)
41 typedef int (SLAP_CTRL_PARSE_FN) LDAP_P((
47 static SLAP_CTRL_PARSE_FN parseManageDSAit;
48 static SLAP_CTRL_PARSE_FN parseSubentries;
49 static SLAP_CTRL_PARSE_FN parseNoOp;
50 static SLAP_CTRL_PARSE_FN parsePagedResults;
51 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
53 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
55 static struct slap_control {
58 char **sc_extendedops;
59 SLAP_CTRL_PARSE_FN *sc_parse;
61 } supportedControls[] = {
62 { LDAP_CONTROL_MANAGEDSAIT,
63 SLAP_CTRL_ACCESS, NULL,
65 #ifdef LDAP_CONTROL_SUBENTRIES
66 { LDAP_CONTROL_SUBENTRIES,
67 SLAP_CTRL_SEARCH, NULL,
70 #ifdef LDAP_CONTROL_NOOP
72 SLAP_CTRL_ACCESS, NULL,
75 #ifdef LDAP_CONTROL_PAGEDRESULTS_REQUEST
76 { LDAP_CONTROL_PAGEDRESULTS_REQUEST,
77 SLAP_CTRL_SEARCH, NULL,
80 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
81 { LDAP_CONTROL_VALUESRETURNFILTER,
82 SLAP_CTRL_SEARCH, NULL,
83 parseValuesReturnFilter },
89 get_supported_ctrl(int index)
91 return supportedControls[index].sc_oid;
94 static struct slap_control *
95 find_ctrl( const char *oid )
98 for( i=0; supportedControls[i].sc_oid; i++ ) {
99 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
100 return &supportedControls[i];
115 BerElement *ber = op->o_ber;
116 struct slap_control *sc;
117 int rc = LDAP_SUCCESS;
118 const char *errmsg = NULL;
120 len = ber_pvt_ber_remaining(ber);
128 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
129 if( tag == LBER_ERROR ) {
130 rc = SLAPD_DISCONNECT;
131 errmsg = "unexpected data in PDU";
138 LDAP_LOG( OPERATION, ENTRY, "get_ctrls: conn %lu\n", conn->c_connid, 0, 0 );
140 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
142 if( op->o_protocol < LDAP_VERSION3 ) {
143 rc = SLAPD_DISCONNECT;
144 errmsg = "controls require LDAPv3";
148 /* one for first control, one for termination */
149 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
152 if( op->ctrls == NULL ) {
154 errmsg = "no memory";
159 op->o_ctrls[nctrls] = NULL;
161 /* step through each element */
162 for( tag = ber_first_element( ber, &len, &opaque );
164 tag = ber_next_element( ber, &len, opaque ) )
167 LDAPControl **tctrls;
169 c = ch_calloc( 1, sizeof(LDAPControl) );
173 ldap_controls_free(op->o_ctrls);
177 errmsg = "no memory";
182 /* allocate pointer space for current controls (nctrls)
183 * + this control + extra NULL
185 tctrls = ch_realloc( op->o_ctrls,
186 (nctrls+2) * sizeof(LDAPControl *));
189 if( tctrls == NULL ) {
191 ldap_controls_free(op->o_ctrls);
195 errmsg = "no memory";
199 op->o_ctrls = tctrls;
201 op->o_ctrls[nctrls++] = c;
202 op->o_ctrls[nctrls] = NULL;
204 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
206 if( tag == LBER_ERROR ) {
208 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
209 conn->c_connid, 0, 0 );
211 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
214 ldap_controls_free( op->o_ctrls );
216 rc = SLAPD_DISCONNECT;
217 errmsg = "decoding controls error";
221 tag = ber_peek_tag( ber, &len );
223 if( tag == LBER_BOOLEAN ) {
225 tag = ber_scanf( ber, "b", &crit );
227 if( tag == LBER_ERROR ) {
229 LDAP_LOG( OPERATION, INFO,
230 "get_ctrls: conn %lu get crit failed.\n",
231 conn->c_connid, 0, 0 );
233 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
236 ldap_controls_free( op->o_ctrls );
238 rc = SLAPD_DISCONNECT;
239 errmsg = "decoding controls error";
243 c->ldctl_iscritical = (crit != 0);
244 tag = ber_peek_tag( ber, &len );
247 if( tag == LBER_OCTETSTRING ) {
248 tag = ber_scanf( ber, "o", &c->ldctl_value );
250 if( tag == LBER_ERROR ) {
252 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
253 "%s (%scritical): get value failed.\n",
254 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
255 c->ldctl_iscritical ? "" : "non" );
257 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
258 "%s (%scritical): get value failed.\n",
260 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
261 c->ldctl_iscritical ? "" : "non" );
263 ldap_controls_free( op->o_ctrls );
265 rc = SLAPD_DISCONNECT;
266 errmsg = "decoding controls error";
272 LDAP_LOG( OPERATION, INFO,
273 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
274 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
275 c->ldctl_iscritical ? "" : "non" );
277 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
278 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
279 c->ldctl_iscritical ? "" : "non",
283 sc = find_ctrl( c->ldctl_oid );
285 /* recognized control */
287 switch( op->o_tag ) {
289 tagmask = SLAP_CTRL_ADD;
292 tagmask = SLAP_CTRL_BIND;
294 case LDAP_REQ_COMPARE:
295 tagmask = SLAP_CTRL_COMPARE;
297 case LDAP_REQ_DELETE:
298 tagmask = SLAP_CTRL_DELETE;
300 case LDAP_REQ_MODIFY:
301 tagmask = SLAP_CTRL_MODIFY;
303 case LDAP_REQ_RENAME:
304 tagmask = SLAP_CTRL_RENAME;
306 case LDAP_REQ_SEARCH:
307 tagmask = SLAP_CTRL_SEARCH;
309 case LDAP_REQ_UNBIND:
310 tagmask = SLAP_CTRL_UNBIND;
312 case LDAP_REQ_EXTENDED:
313 /* FIXME: check list of extended operations */
318 errmsg = "controls internal error";
322 if (( sc->sc_mask & tagmask ) == tagmask ) {
323 /* available extension */
325 if( !sc->sc_parse ) {
327 errmsg = "not yet implemented";
331 rc = sc->sc_parse( conn, op, c, &errmsg );
333 if( rc != LDAP_SUCCESS ) goto return_results;
335 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
336 /* kludge to disable backend_control() check */
337 c->ldctl_iscritical = 0;
339 } else if ( tagmask == SLAP_CTRL_SEARCH &&
340 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
342 /* kludge to disable backend_control() check */
343 c->ldctl_iscritical = 0;
346 } else if( c->ldctl_iscritical ) {
347 /* unavailable CRITICAL control */
348 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
349 errmsg = "critical extension is unavailable";
353 } else if( c->ldctl_iscritical ) {
354 /* unrecognized CRITICAL control */
355 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
356 errmsg = "critical extension is not recognized";
363 LDAP_LOG( OPERATION, RESULTS,
364 "get_ctrls: n=%d rc=%d err=%s\n", nctrls, rc, errmsg ? errmsg : "" );
366 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: n=%d rc=%d err=%s\n",
367 nctrls, rc, errmsg ? errmsg : "");
370 if( sendres && rc != LDAP_SUCCESS ) {
371 if( rc == SLAPD_DISCONNECT ) {
372 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
374 send_ldap_result( conn, op, rc,
375 NULL, errmsg, NULL, NULL );
382 static int parseManageDSAit (
388 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
389 *text = "manageDSAit control specified multiple times";
390 return LDAP_PROTOCOL_ERROR;
393 if ( ctrl->ldctl_value.bv_len ) {
394 *text = "manageDSAit control value not empty";
395 return LDAP_PROTOCOL_ERROR;
398 op->o_managedsait = ctrl->ldctl_iscritical
399 ? SLAP_CRITICAL_CONTROL
400 : SLAP_NONCRITICAL_CONTROL;
405 #ifdef LDAP_CONTROL_SUBENTRIES
406 static int parseSubentries (
412 if ( op->o_subentries != SLAP_NO_CONTROL ) {
413 *text = "subentries control specified multiple times";
414 return LDAP_PROTOCOL_ERROR;
417 /* FIXME: should use BER library */
418 if( ( ctrl->ldctl_value.bv_len != 3 )
419 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
420 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
422 *text = "subentries control value encoding is bogus";
423 return LDAP_PROTOCOL_ERROR;
426 op->o_subentries = ctrl->ldctl_iscritical
427 ? SLAP_CRITICAL_CONTROL
428 : SLAP_NONCRITICAL_CONTROL;
430 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
436 #ifdef LDAP_CONTROL_NOOP
437 static int parseNoOp (
443 if ( op->o_noop != SLAP_NO_CONTROL ) {
444 *text = "noop control specified multiple times";
445 return LDAP_PROTOCOL_ERROR;
448 if ( ctrl->ldctl_value.bv_len ) {
449 *text = "noop control value not empty";
450 return LDAP_PROTOCOL_ERROR;
453 op->o_noop = ctrl->ldctl_iscritical
454 ? SLAP_CRITICAL_CONTROL
455 : SLAP_NONCRITICAL_CONTROL;
461 #ifdef LDAP_CONTROL_PAGEDRESULTS_REQUEST
462 static int parsePagedResults (
471 struct berval cookie = { 0, NULL };
473 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
474 *text = "paged results control specified multiple times";
475 return LDAP_PROTOCOL_ERROR;
478 if ( ctrl->ldctl_value.bv_len == 0 ) {
479 *text = "paged results control value is empty";
480 return LDAP_PROTOCOL_ERROR;
483 /* Parse the control value
484 * realSearchControlValue ::= SEQUENCE {
485 * size INTEGER (0..maxInt),
486 * -- requested page size from client
487 * -- result set size estimate from server
488 * cookie OCTET STRING
490 ber = ber_init( &ctrl->ldctl_value );
492 *text = "internal error";
496 tag = ber_scanf( ber, "{im}", &size, &cookie );
497 (void) ber_free( ber, 1 );
499 if( tag == LBER_ERROR ) {
500 *text = "paged results control could not be decoded";
501 return LDAP_PROTOCOL_ERROR;
505 *text = "paged results control size invalid";
506 return LDAP_PROTOCOL_ERROR;
509 if( cookie.bv_len ) {
510 PagedResultsCookie reqcookie;
511 if( cookie.bv_len != sizeof( reqcookie ) ) {
513 *text = "paged results cookie is invalid";
514 return LDAP_PROTOCOL_ERROR;
517 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
519 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
521 *text = "paged results cookie is invalid";
522 return LDAP_PROTOCOL_ERROR;
524 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
525 *text = "paged results cookie is invalid or old";
526 return LDAP_UNWILLING_TO_PERFORM;
530 op->o_pagedresults_state.ps_cookie = op->o_opid;
531 op->o_pagedresults_size = size;
533 op->o_pagedresults = ctrl->ldctl_iscritical
534 ? SLAP_CRITICAL_CONTROL
535 : SLAP_NONCRITICAL_CONTROL;
541 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
542 int parseValuesReturnFilter (
550 struct berval fstr = { 0, NULL };
551 const char *err_msg = "";
553 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
554 *text = "valuesreturnfilter control specified multiple times";
555 return LDAP_PROTOCOL_ERROR;
558 ber = ber_init( &(ctrl->ldctl_value) );
560 *text = "internal error";
564 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
566 if( rc != LDAP_SUCCESS ) {
568 if( rc == SLAPD_DISCONNECT ) {
569 send_ldap_disconnect( conn, op,
570 LDAP_PROTOCOL_ERROR, *text );
572 send_ldap_result( conn, op, rc,
573 NULL, *text, NULL, NULL );
575 if( fstr.bv_val != NULL) free( fstr.bv_val );
576 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
579 vrFilter2bv( op->vrFilter, &fstr );
583 LDAP_LOG( OPERATION, ARGS,
584 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
585 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
587 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
588 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
591 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
592 ? SLAP_CRITICAL_CONTROL
593 : SLAP_NONCRITICAL_CONTROL;