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;
49 static SLAP_CTRL_PARSE_FN parsePagedResults;
50 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
52 static struct slap_control {
55 char **sc_extendedops;
56 SLAP_CTRL_PARSE_FN *sc_parse;
58 } supportedControls[] = {
59 { LDAP_CONTROL_MANAGEDSAIT,
60 SLAP_CTRL_ACCESS, NULL,
62 #ifdef LDAP_CONTROL_SUBENTRIES
63 { LDAP_CONTROL_SUBENTRIES,
64 SLAP_CTRL_SEARCH, NULL,
67 #ifdef LDAP_CONTROL_NOOP
69 SLAP_CTRL_UPDATE, NULL,
72 #ifdef LDAP_CONTROL_PAGEDRESULTS_REQUEST
73 { LDAP_CONTROL_PAGEDRESULTS_REQUEST,
74 SLAP_CTRL_SEARCH, NULL,
77 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
78 { LDAP_CONTROL_VALUESRETURNFILTER,
79 SLAP_CTRL_SEARCH, NULL,
80 parseValuesReturnFilter },
86 get_supported_ctrl(int index)
88 return supportedControls[index].sc_oid;
91 static struct slap_control *
92 find_ctrl( const char *oid )
95 for( i=0; supportedControls[i].sc_oid; i++ ) {
96 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
97 return &supportedControls[i];
112 BerElement *ber = op->o_ber;
113 struct slap_control *sc;
114 int rc = LDAP_SUCCESS;
115 const char *errmsg = NULL;
117 len = ber_pvt_ber_remaining(ber);
125 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
126 if( tag == LBER_ERROR ) {
127 rc = SLAPD_DISCONNECT;
128 errmsg = "unexpected data in PDU";
135 LDAP_LOG( OPERATION, ENTRY, "get_ctrls: conn %lu\n", conn->c_connid, 0, 0 );
137 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
139 if( op->o_protocol < LDAP_VERSION3 ) {
140 rc = SLAPD_DISCONNECT;
141 errmsg = "controls require LDAPv3";
145 /* one for first control, one for termination */
146 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
149 if( op->ctrls == NULL ) {
151 errmsg = "no memory";
156 op->o_ctrls[nctrls] = NULL;
158 /* step through each element */
159 for( tag = ber_first_element( ber, &len, &opaque );
161 tag = ber_next_element( ber, &len, opaque ) )
164 LDAPControl **tctrls;
166 c = ch_calloc( 1, sizeof(LDAPControl) );
170 ldap_controls_free(op->o_ctrls);
174 errmsg = "no memory";
179 /* allocate pointer space for current controls (nctrls)
180 * + this control + extra NULL
182 tctrls = ch_realloc( op->o_ctrls,
183 (nctrls+2) * sizeof(LDAPControl *));
186 if( tctrls == NULL ) {
188 ldap_controls_free(op->o_ctrls);
192 errmsg = "no memory";
196 op->o_ctrls = tctrls;
198 op->o_ctrls[nctrls++] = c;
199 op->o_ctrls[nctrls] = NULL;
201 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
203 if( tag == LBER_ERROR ) {
205 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
206 conn->c_connid, 0, 0 );
208 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
211 ldap_controls_free( op->o_ctrls );
213 rc = SLAPD_DISCONNECT;
214 errmsg = "decoding controls error";
218 tag = ber_peek_tag( ber, &len );
220 if( tag == LBER_BOOLEAN ) {
222 tag = ber_scanf( ber, "b", &crit );
224 if( tag == LBER_ERROR ) {
226 LDAP_LOG( OPERATION, INFO,
227 "get_ctrls: conn %lu get crit failed.\n",
228 conn->c_connid, 0, 0 );
230 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
233 ldap_controls_free( op->o_ctrls );
235 rc = SLAPD_DISCONNECT;
236 errmsg = "decoding controls error";
240 c->ldctl_iscritical = (crit != 0);
241 tag = ber_peek_tag( ber, &len );
244 if( tag == LBER_OCTETSTRING ) {
245 tag = ber_scanf( ber, "o", &c->ldctl_value );
247 if( tag == LBER_ERROR ) {
249 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
250 "%s (%scritical): get value failed.\n",
251 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
252 c->ldctl_iscritical ? "" : "non" );
254 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
255 "%s (%scritical): get value failed.\n",
257 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
258 c->ldctl_iscritical ? "" : "non" );
260 ldap_controls_free( op->o_ctrls );
262 rc = SLAPD_DISCONNECT;
263 errmsg = "decoding controls error";
269 LDAP_LOG( OPERATION, INFO,
270 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
271 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
272 c->ldctl_iscritical ? "" : "non" );
274 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
275 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
276 c->ldctl_iscritical ? "" : "non",
280 sc = find_ctrl( c->ldctl_oid );
282 /* recognized control */
284 switch( op->o_tag ) {
286 tagmask = SLAP_CTRL_ADD;
289 tagmask = SLAP_CTRL_BIND;
291 case LDAP_REQ_COMPARE:
292 tagmask = SLAP_CTRL_COMPARE;
294 case LDAP_REQ_DELETE:
295 tagmask = SLAP_CTRL_DELETE;
297 case LDAP_REQ_MODIFY:
298 tagmask = SLAP_CTRL_MODIFY;
300 case LDAP_REQ_RENAME:
301 tagmask = SLAP_CTRL_RENAME;
303 case LDAP_REQ_SEARCH:
304 tagmask = SLAP_CTRL_SEARCH;
306 case LDAP_REQ_UNBIND:
307 tagmask = SLAP_CTRL_UNBIND;
309 case LDAP_REQ_EXTENDED:
310 /* FIXME: check list of extended operations */
315 errmsg = "controls internal error";
319 if (( sc->sc_mask & tagmask ) == tagmask ) {
320 /* available extension */
322 if( !sc->sc_parse ) {
324 errmsg = "not yet implemented";
328 rc = sc->sc_parse( conn, op, c, &errmsg );
330 if( rc != LDAP_SUCCESS ) goto return_results;
332 if( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
333 /* kludge to disable backend_control() check */
334 c->ldctl_iscritical = 0;
337 } else if( c->ldctl_iscritical ) {
338 /* unavailable CRITICAL control */
339 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
340 errmsg = "critical extension is unavailable";
344 } else if( c->ldctl_iscritical ) {
345 /* unrecognized CRITICAL control */
346 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
347 errmsg = "critical extension is not recognized";
354 LDAP_LOG( OPERATION, RESULTS,
355 "get_ctrls: n=%d rc=%d err=%s\n", nctrls, rc, errmsg ? errmsg : "" );
357 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: n=%d rc=%d err=%s\n",
358 nctrls, rc, errmsg ? errmsg : "");
361 if( sendres && rc != LDAP_SUCCESS ) {
362 if( rc == SLAPD_DISCONNECT ) {
363 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
365 send_ldap_result( conn, op, rc,
366 NULL, errmsg, NULL, NULL );
373 static int parseManageDSAit (
379 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
380 *text = "manageDSAit control specified multiple times";
381 return LDAP_PROTOCOL_ERROR;
384 if ( ctrl->ldctl_value.bv_len ) {
385 *text = "manageDSAit control value not empty";
386 return LDAP_PROTOCOL_ERROR;
389 op->o_managedsait = ctrl->ldctl_iscritical
390 ? SLAP_CRITICAL_CONTROL
391 : SLAP_NONCRITICAL_CONTROL;
396 #ifdef LDAP_CONTROL_SUBENTRIES
397 static int parseSubentries (
403 if ( op->o_subentries != SLAP_NO_CONTROL ) {
404 *text = "subentries control specified multiple times";
405 return LDAP_PROTOCOL_ERROR;
408 /* FIXME: should use BER library */
409 if( ( ctrl->ldctl_value.bv_len != 3 )
410 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
411 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
413 *text = "subentries control value encoding is bogus";
414 return LDAP_PROTOCOL_ERROR;
417 op->o_subentries = ctrl->ldctl_iscritical
418 ? SLAP_CRITICAL_CONTROL
419 : SLAP_NONCRITICAL_CONTROL;
421 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
427 #ifdef LDAP_CONTROL_NOOP
428 static int parseNoOp (
434 if ( op->o_noop != SLAP_NO_CONTROL ) {
435 *text = "noop control specified multiple times";
436 return LDAP_PROTOCOL_ERROR;
439 if ( ctrl->ldctl_value.bv_len ) {
440 *text = "noop control value not empty";
441 return LDAP_PROTOCOL_ERROR;
444 op->o_noop = ctrl->ldctl_iscritical
445 ? SLAP_CRITICAL_CONTROL
446 : SLAP_NONCRITICAL_CONTROL;
452 #ifdef LDAP_CONTROL_PAGEDRESULTS_REQUEST
453 static int parsePagedResults (
462 struct berval cookie = { 0, NULL };
464 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
465 *text = "paged results control specified multiple times";
466 return LDAP_PROTOCOL_ERROR;
469 if ( ctrl->ldctl_value.bv_len == 0 ) {
470 *text = "paged results control value is empty";
471 return LDAP_PROTOCOL_ERROR;
474 /* Parse the control value
475 * realSearchControlValue ::= SEQUENCE {
476 * size INTEGER (0..maxInt),
477 * -- requested page size from client
478 * -- result set size estimate from server
479 * cookie OCTET STRING
481 ber = ber_init( &ctrl->ldctl_value );
483 *text = "internal error";
487 tag = ber_scanf( ber, "{im}", &size, &cookie );
488 (void) ber_free( ber, 1 );
490 if( tag == LBER_ERROR ) {
491 *text = "paged results control could not be decoded";
492 return LDAP_PROTOCOL_ERROR;
496 *text = "paged results control size invalid";
497 return LDAP_PROTOCOL_ERROR;
500 if( cookie.bv_len ) {
501 PagedResultsCookie reqcookie;
502 if( cookie.bv_len != sizeof( reqcookie ) ) {
504 *text = "paged results cookie is invalid";
505 return LDAP_PROTOCOL_ERROR;
508 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
510 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
512 *text = "paged results cookie is invalid";
513 return LDAP_PROTOCOL_ERROR;
515 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
516 *text = "paged results cookie is invalid or old";
517 return LDAP_UNWILLING_TO_PERFORM;
521 op->o_pagedresults_state.ps_cookie = op->o_opid;
522 op->o_pagedresults_size = size;
524 op->o_pagedresults = ctrl->ldctl_iscritical
525 ? SLAP_CRITICAL_CONTROL
526 : SLAP_NONCRITICAL_CONTROL;
532 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
533 int parseValuesReturnFilter (
541 struct berval fstr = { 0, NULL };
542 const char *err_msg = "";
544 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
545 *text = "valuesreturnfilter control specified multiple times";
546 return LDAP_PROTOCOL_ERROR;
549 ber = ber_init( &(ctrl->ldctl_value) );
551 *text = "internal error";
555 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
557 if( rc != LDAP_SUCCESS ) {
559 if( rc == SLAPD_DISCONNECT ) {
560 send_ldap_disconnect( conn, op,
561 LDAP_PROTOCOL_ERROR, *text );
563 send_ldap_result( conn, op, rc,
564 NULL, *text, NULL, NULL );
566 if( fstr.bv_val != NULL) free( fstr.bv_val );
567 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
570 vrFilter2bv( op->vrFilter, &fstr );
574 LDAP_LOG( OPERATION, ARGS,
575 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
576 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
578 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
579 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
582 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
583 ? SLAP_CRITICAL_CONTROL
584 : SLAP_NONCRITICAL_CONTROL;