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 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
54 static struct slap_control {
57 char **sc_extendedops;
58 SLAP_CTRL_PARSE_FN *sc_parse;
60 } supportedControls[] = {
61 { LDAP_CONTROL_MANAGEDSAIT,
62 SLAP_CTRL_ACCESS, NULL,
64 #ifdef LDAP_CONTROL_SUBENTRIES
65 { LDAP_CONTROL_SUBENTRIES,
66 SLAP_CTRL_SEARCH, NULL,
69 #ifdef LDAP_CONTROL_NOOP
71 SLAP_CTRL_UPDATE, NULL,
74 #ifdef LDAP_CONTROL_PAGEDRESULTS_REQUEST
75 { LDAP_CONTROL_PAGEDRESULTS_REQUEST,
76 SLAP_CTRL_SEARCH, NULL,
79 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
80 { LDAP_CONTROL_VALUESRETURNFILTER,
81 SLAP_CTRL_SEARCH, NULL,
82 parseValuesReturnFilter },
88 get_supported_ctrl(int index)
90 return supportedControls[index].sc_oid;
93 static struct slap_control *
94 find_ctrl( const char *oid )
97 for( i=0; supportedControls[i].sc_oid; i++ ) {
98 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
99 return &supportedControls[i];
114 BerElement *ber = op->o_ber;
115 struct slap_control *sc;
116 int rc = LDAP_SUCCESS;
117 const char *errmsg = NULL;
119 len = ber_pvt_ber_remaining(ber);
127 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
128 if( tag == LBER_ERROR ) {
129 rc = SLAPD_DISCONNECT;
130 errmsg = "unexpected data in PDU";
137 LDAP_LOG( OPERATION, ENTRY, "get_ctrls: conn %lu\n", conn->c_connid, 0, 0 );
139 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
141 if( op->o_protocol < LDAP_VERSION3 ) {
142 rc = SLAPD_DISCONNECT;
143 errmsg = "controls require LDAPv3";
147 /* one for first control, one for termination */
148 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
151 if( op->ctrls == NULL ) {
153 errmsg = "no memory";
158 op->o_ctrls[nctrls] = NULL;
160 /* step through each element */
161 for( tag = ber_first_element( ber, &len, &opaque );
163 tag = ber_next_element( ber, &len, opaque ) )
166 LDAPControl **tctrls;
168 c = ch_calloc( 1, sizeof(LDAPControl) );
172 ldap_controls_free(op->o_ctrls);
176 errmsg = "no memory";
181 /* allocate pointer space for current controls (nctrls)
182 * + this control + extra NULL
184 tctrls = ch_realloc( op->o_ctrls,
185 (nctrls+2) * sizeof(LDAPControl *));
188 if( tctrls == NULL ) {
190 ldap_controls_free(op->o_ctrls);
194 errmsg = "no memory";
198 op->o_ctrls = tctrls;
200 op->o_ctrls[nctrls++] = c;
201 op->o_ctrls[nctrls] = NULL;
203 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
205 if( tag == LBER_ERROR ) {
207 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
208 conn->c_connid, 0, 0 );
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, INFO,
229 "get_ctrls: conn %lu get crit failed.\n",
230 conn->c_connid, 0, 0 );
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, INFO, "get_ctrls: conn %lu: "
252 "%s (%scritical): get value failed.\n",
253 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
254 c->ldctl_iscritical ? "" : "non" );
256 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
257 "%s (%scritical): get value failed.\n",
259 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
260 c->ldctl_iscritical ? "" : "non" );
262 ldap_controls_free( op->o_ctrls );
264 rc = SLAPD_DISCONNECT;
265 errmsg = "decoding controls error";
271 LDAP_LOG( OPERATION, INFO,
272 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
273 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
274 c->ldctl_iscritical ? "" : "non" );
276 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
277 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
278 c->ldctl_iscritical ? "" : "non",
282 sc = find_ctrl( c->ldctl_oid );
284 /* recognized control */
286 switch( op->o_tag ) {
288 tagmask = SLAP_CTRL_ADD;
291 tagmask = SLAP_CTRL_BIND;
293 case LDAP_REQ_COMPARE:
294 tagmask = SLAP_CTRL_COMPARE;
296 case LDAP_REQ_DELETE:
297 tagmask = SLAP_CTRL_DELETE;
299 case LDAP_REQ_MODIFY:
300 tagmask = SLAP_CTRL_MODIFY;
302 case LDAP_REQ_RENAME:
303 tagmask = SLAP_CTRL_RENAME;
305 case LDAP_REQ_SEARCH:
306 tagmask = SLAP_CTRL_SEARCH;
308 case LDAP_REQ_UNBIND:
309 tagmask = SLAP_CTRL_UNBIND;
311 case LDAP_REQ_EXTENDED:
312 /* FIXME: check list of extended operations */
317 errmsg = "controls internal error";
321 if (( sc->sc_mask & tagmask ) == tagmask ) {
322 /* available extension */
324 if( !sc->sc_parse ) {
326 errmsg = "not yet implemented";
330 rc = sc->sc_parse( conn, op, c, &errmsg );
332 if( rc != LDAP_SUCCESS ) goto return_results;
334 if( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
335 /* kludge to disable backend_control() check */
336 c->ldctl_iscritical = 0;
339 } else if( c->ldctl_iscritical ) {
340 /* unavailable CRITICAL control */
341 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
342 errmsg = "critical extension is unavailable";
346 } else if( c->ldctl_iscritical ) {
347 /* unrecognized CRITICAL control */
348 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
349 errmsg = "critical extension is not recognized";
356 LDAP_LOG( OPERATION, RESULTS,
357 "get_ctrls: n=%d rc=%d err=%s\n", nctrls, rc, errmsg ? errmsg : "" );
359 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: n=%d rc=%d err=%s\n",
360 nctrls, rc, errmsg ? errmsg : "");
363 if( sendres && rc != LDAP_SUCCESS ) {
364 if( rc == SLAPD_DISCONNECT ) {
365 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
367 send_ldap_result( conn, op, rc,
368 NULL, errmsg, NULL, NULL );
375 static int parseManageDSAit (
381 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
382 *text = "manageDSAit control specified multiple times";
383 return LDAP_PROTOCOL_ERROR;
386 if ( ctrl->ldctl_value.bv_len ) {
387 *text = "manageDSAit control value not empty";
388 return LDAP_PROTOCOL_ERROR;
391 op->o_managedsait = ctrl->ldctl_iscritical
392 ? SLAP_CRITICAL_CONTROL
393 : SLAP_NONCRITICAL_CONTROL;
398 #ifdef LDAP_CONTROL_SUBENTRIES
399 static int parseSubentries (
405 if ( op->o_subentries != SLAP_NO_CONTROL ) {
406 *text = "subentries control specified multiple times";
407 return LDAP_PROTOCOL_ERROR;
410 /* FIXME: should use BER library */
411 if( ( ctrl->ldctl_value.bv_len != 3 )
412 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
413 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
415 *text = "subentries control value encoding is bogus";
416 return LDAP_PROTOCOL_ERROR;
419 op->o_subentries = ctrl->ldctl_iscritical
420 ? SLAP_CRITICAL_CONTROL
421 : SLAP_NONCRITICAL_CONTROL;
423 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
429 #ifdef LDAP_CONTROL_NOOP
430 static int parseNoOp (
436 if ( op->o_noop != SLAP_NO_CONTROL ) {
437 *text = "noop control specified multiple times";
438 return LDAP_PROTOCOL_ERROR;
441 if ( ctrl->ldctl_value.bv_len ) {
442 *text = "noop control value not empty";
443 return LDAP_PROTOCOL_ERROR;
446 op->o_noop = ctrl->ldctl_iscritical
447 ? SLAP_CRITICAL_CONTROL
448 : SLAP_NONCRITICAL_CONTROL;
454 #ifdef LDAP_CONTROL_PAGEDRESULTS_REQUEST
455 static int parsePagedResults (
464 struct berval cookie = { 0, NULL };
466 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
467 *text = "paged results control specified multiple times";
468 return LDAP_PROTOCOL_ERROR;
471 if ( ctrl->ldctl_value.bv_len == 0 ) {
472 *text = "paged results control value is empty";
473 return LDAP_PROTOCOL_ERROR;
476 /* Parse the control value
477 * realSearchControlValue ::= SEQUENCE {
478 * size INTEGER (0..maxInt),
479 * -- requested page size from client
480 * -- result set size estimate from server
481 * cookie OCTET STRING
483 ber = ber_init( &ctrl->ldctl_value );
485 *text = "internal error";
489 tag = ber_scanf( ber, "{im}", &size, &cookie );
490 (void) ber_free( ber, 1 );
492 if( tag == LBER_ERROR ) {
493 *text = "paged results control could not be decoded";
494 return LDAP_PROTOCOL_ERROR;
498 *text = "paged results control size invalid";
499 return LDAP_PROTOCOL_ERROR;
502 if( cookie.bv_len ) {
503 PagedResultsCookie reqcookie;
504 if( cookie.bv_len != sizeof( reqcookie ) ) {
506 *text = "paged results cookie is invalid";
507 return LDAP_PROTOCOL_ERROR;
510 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
512 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
514 *text = "paged results cookie is invalid";
515 return LDAP_PROTOCOL_ERROR;
517 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
518 *text = "paged results cookie is invalid or old";
519 return LDAP_UNWILLING_TO_PERFORM;
523 op->o_pagedresults_state.ps_cookie = op->o_opid;
524 op->o_pagedresults_size = size;
526 op->o_pagedresults = ctrl->ldctl_iscritical
527 ? SLAP_CRITICAL_CONTROL
528 : SLAP_NONCRITICAL_CONTROL;
534 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
535 int parseValuesReturnFilter (
543 struct berval fstr = { 0, NULL };
544 const char *err_msg = "";
546 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
547 *text = "valuesreturnfilter control specified multiple times";
548 return LDAP_PROTOCOL_ERROR;
551 ber = ber_init( &(ctrl->ldctl_value) );
553 *text = "internal error";
557 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
559 if( rc != LDAP_SUCCESS ) {
561 if( rc == SLAPD_DISCONNECT ) {
562 send_ldap_disconnect( conn, op,
563 LDAP_PROTOCOL_ERROR, *text );
565 send_ldap_result( conn, op, rc,
566 NULL, *text, NULL, NULL );
568 if( fstr.bv_val != NULL) free( fstr.bv_val );
569 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
572 vrFilter2bv( op->vrFilter, &fstr );
576 LDAP_LOG( OPERATION, ARGS,
577 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
578 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
580 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
581 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
584 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
585 ? SLAP_CRITICAL_CONTROL
586 : SLAP_NONCRITICAL_CONTROL;