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", LDAP_LEVEL_ENTRY,
136 "get_ctrls: conn %lu\n", conn->c_connid ));
138 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
140 if( op->o_protocol < LDAP_VERSION3 ) {
141 rc = SLAPD_DISCONNECT;
142 errmsg = "controls require LDAPv3";
146 /* one for first control, one for termination */
147 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
150 if( op->ctrls == NULL ) {
152 errmsg = "no memory";
157 op->o_ctrls[nctrls] = NULL;
159 /* step through each element */
160 for( tag = ber_first_element( ber, &len, &opaque );
162 tag = ber_next_element( ber, &len, opaque ) )
165 LDAPControl **tctrls;
167 c = ch_calloc( 1, sizeof(LDAPControl) );
171 ldap_controls_free(op->o_ctrls);
175 errmsg = "no memory";
180 /* allocate pointer space for current controls (nctrls)
181 * + this control + extra NULL
183 tctrls = ch_realloc( op->o_ctrls,
184 (nctrls+2) * sizeof(LDAPControl *));
187 if( tctrls == NULL ) {
189 ldap_controls_free(op->o_ctrls);
193 errmsg = "no memory";
197 op->o_ctrls = tctrls;
199 op->o_ctrls[nctrls++] = c;
200 op->o_ctrls[nctrls] = NULL;
202 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
204 if( tag == LBER_ERROR ) {
206 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
207 "get_ctrls: conn %lu get OID failed.\n",
210 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
213 ldap_controls_free( op->o_ctrls );
215 rc = SLAPD_DISCONNECT;
216 errmsg = "decoding controls error";
220 tag = ber_peek_tag( ber, &len );
222 if( tag == LBER_BOOLEAN ) {
224 tag = ber_scanf( ber, "b", &crit );
226 if( tag == LBER_ERROR ) {
228 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
229 "get_ctrls: conn %lu get crit failed.\n",
232 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
235 ldap_controls_free( op->o_ctrls );
237 rc = SLAPD_DISCONNECT;
238 errmsg = "decoding controls error";
242 c->ldctl_iscritical = (crit != 0);
243 tag = ber_peek_tag( ber, &len );
246 if( tag == LBER_OCTETSTRING ) {
247 tag = ber_scanf( ber, "o", &c->ldctl_value );
249 if( tag == LBER_ERROR ) {
251 LDAP_LOG(( "operation", LDAP_LEVEL_INFO, "get_ctrls: conn %lu: "
252 "%s (%scritical): get value failed.\n",
254 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", LDAP_LEVEL_INFO,
273 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
275 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
276 c->ldctl_iscritical ? "" : "non" ));
278 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
279 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
280 c->ldctl_iscritical ? "" : "non",
284 sc = find_ctrl( c->ldctl_oid );
286 /* recognized control */
288 switch( op->o_tag ) {
290 tagmask = SLAP_CTRL_ADD;
293 tagmask = SLAP_CTRL_BIND;
295 case LDAP_REQ_COMPARE:
296 tagmask = SLAP_CTRL_COMPARE;
298 case LDAP_REQ_DELETE:
299 tagmask = SLAP_CTRL_DELETE;
301 case LDAP_REQ_MODIFY:
302 tagmask = SLAP_CTRL_MODIFY;
304 case LDAP_REQ_RENAME:
305 tagmask = SLAP_CTRL_RENAME;
307 case LDAP_REQ_SEARCH:
308 tagmask = SLAP_CTRL_SEARCH;
310 case LDAP_REQ_UNBIND:
311 tagmask = SLAP_CTRL_UNBIND;
313 case LDAP_REQ_EXTENDED:
314 /* FIXME: check list of extended operations */
319 errmsg = "controls internal error";
323 if (( sc->sc_mask & tagmask ) == tagmask ) {
324 /* available extension */
326 if( !sc->sc_parse ) {
328 errmsg = "not yet implemented";
332 rc = sc->sc_parse( conn, op, c, &errmsg );
334 if( rc != LDAP_SUCCESS ) goto return_results;
336 if( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
337 /* kludge to disable backend_control() check */
338 c->ldctl_iscritical = 0;
341 } else if( c->ldctl_iscritical ) {
342 /* unavailable CRITICAL control */
343 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
344 errmsg = "critical extension is unavailable";
348 } else if( c->ldctl_iscritical ) {
349 /* unrecognized CRITICAL control */
350 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
351 errmsg = "critical extension is not recognized";
358 LDAP_LOG(( "operation", LDAP_LEVEL_RESULTS,
359 "get_ctrls: conn=%lu n=%d rc=%d err=%s\n",
360 conn->c_connid, nctrls, rc, errmsg ? errmsg : "" ));
362 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: n=%d rc=%d err=%s\n",
363 nctrls, rc, errmsg ? errmsg : "");
366 if( sendres && rc != LDAP_SUCCESS ) {
367 if( rc == SLAPD_DISCONNECT ) {
368 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
370 send_ldap_result( conn, op, rc,
371 NULL, errmsg, NULL, NULL );
378 static int parseManageDSAit (
384 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
385 *text = "manageDSAit control specified multiple times";
386 return LDAP_PROTOCOL_ERROR;
389 if ( ctrl->ldctl_value.bv_len ) {
390 *text = "manageDSAit control value not empty";
391 return LDAP_PROTOCOL_ERROR;
394 op->o_managedsait = ctrl->ldctl_iscritical
395 ? SLAP_CRITICAL_CONTROL
396 : SLAP_NONCRITICAL_CONTROL;
401 #ifdef LDAP_CONTROL_SUBENTRIES
402 static int parseSubentries (
408 if ( op->o_subentries != SLAP_NO_CONTROL ) {
409 *text = "subentries control specified multiple times";
410 return LDAP_PROTOCOL_ERROR;
413 /* FIXME: should use BER library */
414 if( ( ctrl->ldctl_value.bv_len != 3 )
415 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
416 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
418 *text = "subentries control value encoding is bogus";
419 return LDAP_PROTOCOL_ERROR;
422 op->o_subentries = ctrl->ldctl_iscritical
423 ? SLAP_CRITICAL_CONTROL
424 : SLAP_NONCRITICAL_CONTROL;
426 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
432 #ifdef LDAP_CONTROL_NOOP
433 static int parseNoOp (
439 if ( op->o_noop != SLAP_NO_CONTROL ) {
440 *text = "noop control specified multiple times";
441 return LDAP_PROTOCOL_ERROR;
444 if ( ctrl->ldctl_value.bv_len ) {
445 *text = "noop control value not empty";
446 return LDAP_PROTOCOL_ERROR;
449 op->o_noop = ctrl->ldctl_iscritical
450 ? SLAP_CRITICAL_CONTROL
451 : SLAP_NONCRITICAL_CONTROL;
457 #ifdef LDAP_CONTROL_PAGEDRESULTS_REQUEST
458 static int parsePagedResults (
467 struct berval cookie = { 0, NULL };
469 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
470 *text = "paged results control specified multiple times";
471 return LDAP_PROTOCOL_ERROR;
474 if ( ctrl->ldctl_value.bv_len == 0 ) {
475 *text = "paged results control value is empty";
476 return LDAP_PROTOCOL_ERROR;
479 /* Parse the control value
480 * realSearchControlValue ::= SEQUENCE {
481 * size INTEGER (0..maxInt),
482 * -- requested page size from client
483 * -- result set size estimate from server
484 * cookie OCTET STRING
486 ber = ber_init( &ctrl->ldctl_value );
488 *text = "internal error";
492 tag = ber_scanf( ber, "{im}", &size, &cookie );
493 (void) ber_free( ber, 1 );
495 if( tag == LBER_ERROR ) {
496 *text = "paged results control could not be decoded";
497 return LDAP_PROTOCOL_ERROR;
501 *text = "paged results control size invalid";
502 return LDAP_PROTOCOL_ERROR;
505 if( cookie.bv_len ) {
506 PagedResultsCookie reqcookie;
507 if( cookie.bv_len != sizeof( reqcookie ) ) {
509 *text = "paged results cookie is invalid";
510 return LDAP_PROTOCOL_ERROR;
513 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
515 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
517 *text = "paged results cookie is invalid";
518 return LDAP_PROTOCOL_ERROR;
520 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
521 *text = "paged results cookie is invalid or old";
522 return LDAP_UNWILLING_TO_PERFORM;
526 op->o_pagedresults_state.ps_cookie = op->o_opid;
527 op->o_pagedresults_size = size;
529 op->o_pagedresults = ctrl->ldctl_iscritical
530 ? SLAP_CRITICAL_CONTROL
531 : SLAP_NONCRITICAL_CONTROL;
537 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
538 int parseValuesReturnFilter (
546 struct berval fstr = { 0, NULL };
547 const char *err_msg = "";
549 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
550 *text = "valuesreturnfilter control specified multiple times";
551 return LDAP_PROTOCOL_ERROR;
554 ber = ber_init( &(ctrl->ldctl_value) );
556 *text = "internal error";
560 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
562 if( rc != LDAP_SUCCESS ) {
564 if( rc == SLAPD_DISCONNECT ) {
565 send_ldap_disconnect( conn, op,
566 LDAP_PROTOCOL_ERROR, *text );
568 send_ldap_result( conn, op, rc,
569 NULL, *text, NULL, NULL );
571 if( fstr.bv_val != NULL) free( fstr.bv_val );
572 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
575 vrFilter2bv( op->vrFilter, &fstr );
579 LDAP_LOG(( "operation", LDAP_LEVEL_ARGS,
580 "parseValuesReturnFilter: conn %d vrFilter: %s\n", conn->c_connid,
581 fstr.bv_len ? fstr.bv_val : "empty" ));
583 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
584 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
587 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
588 ? SLAP_CRITICAL_CONTROL
589 : SLAP_NONCRITICAL_CONTROL;