2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/socket.h>
25 #include "../../libraries/liblber/lber-int.h"
27 static SLAP_CTRL_PARSE_FN parseAssert;
28 static SLAP_CTRL_PARSE_FN parseDomainScope;
29 static SLAP_CTRL_PARSE_FN parseDontUseCopy;
30 static SLAP_CTRL_PARSE_FN parseManageDSAit;
31 static SLAP_CTRL_PARSE_FN parseNoOp;
32 static SLAP_CTRL_PARSE_FN parsePagedResults;
33 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
34 static SLAP_CTRL_PARSE_FN parsePreRead, parsePostRead;
35 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
36 static SLAP_CTRL_PARSE_FN parseRelax;
37 static SLAP_CTRL_PARSE_FN parseSearchOptions;
38 #ifdef SLAP_SORTEDRESULTS
39 static SLAP_CTRL_PARSE_FN parseSortedResults;
41 static SLAP_CTRL_PARSE_FN parseSubentries;
42 #ifdef SLAP_CONTROL_X_TREE_DELETE
43 static SLAP_CTRL_PARSE_FN parseTreeDelete;
45 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
47 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
49 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
50 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
52 struct slap_control_ids slap_cids;
58 /* The controlID for this control */
61 /* Operations supported by control */
64 /* Extended operations supported by control */
65 char **sc_extendedops; /* input */
66 BerVarray sc_extendedopsbv; /* run-time use */
68 /* Control parsing callback */
69 SLAP_CTRL_PARSE_FN *sc_parse;
71 LDAP_SLIST_ENTRY(slap_control) sc_next;
74 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
75 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
78 * all known request control OIDs should be added to this list
81 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
82 * addresses an undefined control; this allows to safely test for
83 * well known controls even if they are not registered, e.g. if
84 * they get moved to modules. An example is sc_LDAPsync, which
85 * is implemented in the syncprov overlay and thus, if configured
86 * as dynamic module, may not be registered. One side effect is that
87 * slap_known_controls[0] == NULL, so it should always be used
89 * FIXME: should we define the "undefined control" oid?
91 char *slap_known_controls[SLAP_MAX_CIDS+1];
92 static int num_known_controls = 1;
94 static char *proxy_authz_extops[] = {
95 LDAP_EXOP_MODIFY_PASSWD,
101 static char *manageDSAit_extops[] = {
106 static struct slap_control control_defs[] = {
107 { LDAP_CONTROL_ASSERT,
108 (int)offsetof(struct slap_control_ids, sc_assert),
109 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME|
110 SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
112 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
113 { LDAP_CONTROL_PRE_READ,
114 (int)offsetof(struct slap_control_ids, sc_preRead),
115 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
117 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
118 { LDAP_CONTROL_POST_READ,
119 (int)offsetof(struct slap_control_ids, sc_postRead),
120 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
122 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
123 { LDAP_CONTROL_VALUESRETURNFILTER,
124 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
125 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
127 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
128 { LDAP_CONTROL_PAGEDRESULTS,
129 (int)offsetof(struct slap_control_ids, sc_pagedResults),
132 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
133 #ifdef SLAP_SORTEDRESULTS
134 { LDAP_CONTROL_SORTREQUEST,
135 (int)offsetof(struct slap_control_ids, sc_sortedResults),
136 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
138 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
140 { LDAP_CONTROL_X_DOMAIN_SCOPE,
141 (int)offsetof(struct slap_control_ids, sc_domainScope),
142 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
144 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
145 { LDAP_CONTROL_DONTUSECOPY,
146 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
147 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
149 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
150 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
151 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
152 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
154 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
155 #ifdef SLAP_CONTROL_X_TREE_DELETE
156 { LDAP_CONTROL_X_TREE_DELETE,
157 (int)offsetof(struct slap_control_ids, sc_treeDelete),
158 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
160 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
162 { LDAP_CONTROL_X_SEARCH_OPTIONS,
163 (int)offsetof(struct slap_control_ids, sc_searchOptions),
164 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
166 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
167 { LDAP_CONTROL_SUBENTRIES,
168 (int)offsetof(struct slap_control_ids, sc_subentries),
171 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
173 (int)offsetof(struct slap_control_ids, sc_noOp),
174 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
176 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
177 { LDAP_CONTROL_RELAX,
178 (int)offsetof(struct slap_control_ids, sc_relax),
179 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
181 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
183 { LDAP_CONTROL_X_TXN_SPEC,
184 (int)offsetof(struct slap_control_ids, sc_txnSpec),
185 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
187 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
189 { LDAP_CONTROL_MANAGEDSAIT,
190 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
192 manageDSAit_extops, NULL,
193 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
194 { LDAP_CONTROL_PROXY_AUTHZ,
195 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
196 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
197 proxy_authz_extops, NULL,
198 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
199 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
202 static struct slap_control *
203 find_ctrl( const char *oid );
206 * Register a supported control.
208 * This can be called by an OpenLDAP plugin or, indirectly, by a
209 * SLAPI plugin calling slapi_register_supported_control().
211 * NOTE: if flags == 1 the control is replaced if already registered;
212 * otherwise registering an already registered control is not allowed.
215 register_supported_control2(const char *controloid,
216 slap_mask_t controlmask,
218 SLAP_CTRL_PARSE_FN *controlparsefn,
222 struct slap_control *sc = NULL;
224 BerVarray extendedopsbv = NULL;
226 if ( num_known_controls >= SLAP_MAX_CIDS ) {
227 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
228 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
229 SLAP_MAX_CIDS, 0, 0 );
233 if ( controloid == NULL ) {
234 return LDAP_PARAM_ERROR;
237 /* check if already registered */
238 for ( i = 0; slap_known_controls[ i ]; i++ ) {
239 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
241 Debug( LDAP_DEBUG_TRACE,
242 "Control %s already registered; replacing.\n",
244 /* (find and) replace existing handler */
245 sc = find_ctrl( controloid );
246 assert( sc != NULL );
250 Debug( LDAP_DEBUG_ANY,
251 "Control %s already registered.\n",
253 return LDAP_PARAM_ERROR;
257 /* turn compatible extended operations into bervals */
258 if ( controlexops != NULL ) {
261 for ( i = 0; controlexops[ i ]; i++ );
263 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
264 if ( extendedopsbv == NULL ) {
265 return LDAP_NO_MEMORY;
268 for ( i = 0; controlexops[ i ]; i++ ) {
269 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
274 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
276 return LDAP_NO_MEMORY;
279 sc->sc_oid = ch_strdup( controloid );
280 sc->sc_cid = num_known_controls;
282 /* Update slap_known_controls, too. */
283 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
284 slap_known_controls[num_known_controls++] = NULL;
286 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
287 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
290 if ( sc->sc_extendedopsbv ) {
291 /* FIXME: in principle, we should rather merge
292 * existing extops with those supported by the
293 * new control handling implementation.
294 * In fact, whether a control is compatible with
295 * an extop should not be a matter of implementation.
296 * We likely also need a means for a newly
297 * registered extop to declare that it is
298 * comptible with an already registered control.
300 ber_bvarray_free( sc->sc_extendedopsbv );
301 sc->sc_extendedopsbv = NULL;
302 sc->sc_extendedops = NULL;
306 sc->sc_extendedopsbv = extendedopsbv;
307 sc->sc_mask = controlmask;
308 sc->sc_parse = controlparsefn;
310 *controlcid = sc->sc_cid;
317 * One-time initialization of internal controls.
320 slap_controls_init( void )
326 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
327 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
328 rc = register_supported_control( control_defs[i].sc_oid,
329 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
330 control_defs[i].sc_parse, cid );
331 if ( rc != LDAP_SUCCESS ) break;
338 * Free memory associated with list of supported controls.
341 controls_destroy( void )
343 struct slap_control *sc;
345 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
346 sc = LDAP_SLIST_FIRST(&controls_list);
347 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
349 ch_free( sc->sc_oid );
350 if ( sc->sc_extendedopsbv != NULL ) {
351 ber_bvarray_free( sc->sc_extendedopsbv );
358 * Format the supportedControl attribute of the root DSE,
359 * detailing which controls are supported by the directory
363 controls_root_dse_info( Entry *e )
365 AttributeDescription *ad_supportedControl
366 = slap_schema.si_ad_supportedControl;
367 struct berval vals[2];
368 struct slap_control *sc;
370 vals[1].bv_val = NULL;
373 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
374 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
376 vals[0].bv_val = sc->sc_oid;
377 vals[0].bv_len = strlen( sc->sc_oid );
379 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
388 * Return a list of OIDs and operation masks for supported
389 * controls. Used by SLAPI.
392 get_supported_controls(char ***ctrloidsp,
393 slap_mask_t **ctrlmasks)
398 struct slap_control *sc;
402 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
412 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
413 if ( oids == NULL ) {
414 return LDAP_NO_MEMORY;
416 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
417 if ( masks == NULL ) {
419 return LDAP_NO_MEMORY;
424 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
425 oids[n] = ch_strdup( sc->sc_oid );
426 masks[n] = sc->sc_mask;
439 * Find a control given its OID.
441 static struct slap_control *
442 find_ctrl( const char *oid )
444 struct slap_control *sc;
446 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
447 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
456 slap_find_control_id(
460 struct slap_control *ctrl = find_ctrl( oid );
462 if ( cid ) *cid = ctrl->sc_cid;
465 return LDAP_CONTROL_NOT_FOUND;
469 slap_global_control( Operation *op, const char *oid, int *cid )
471 struct slap_control *ctrl = find_ctrl( oid );
473 if ( ctrl == NULL ) {
474 /* should not be reachable */
475 Debug( LDAP_DEBUG_ANY,
476 "slap_global_control: unrecognized control: %s\n",
478 return LDAP_CONTROL_NOT_FOUND;
481 if ( cid ) *cid = ctrl->sc_cid;
483 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
484 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
485 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
487 return LDAP_COMPARE_TRUE;
491 Debug( LDAP_DEBUG_TRACE,
492 "slap_global_control: unavailable control: %s\n",
496 return LDAP_COMPARE_FALSE;
499 void slap_free_ctrls(
501 LDAPControl **ctrls )
505 for (i=0; ctrls[i]; i++) {
506 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
508 op->o_tmpfree( ctrls, op->o_tmpmemctx );
514 LDAPControl *control,
517 struct slap_control *sc;
519 sc = find_ctrl( control->ldctl_oid );
521 /* recognized control */
523 switch( op->o_tag ) {
525 tagmask = SLAP_CTRL_ADD;
528 tagmask = SLAP_CTRL_BIND;
530 case LDAP_REQ_COMPARE:
531 tagmask = SLAP_CTRL_COMPARE;
533 case LDAP_REQ_DELETE:
534 tagmask = SLAP_CTRL_DELETE;
536 case LDAP_REQ_MODIFY:
537 tagmask = SLAP_CTRL_MODIFY;
539 case LDAP_REQ_RENAME:
540 tagmask = SLAP_CTRL_RENAME;
542 case LDAP_REQ_SEARCH:
543 tagmask = SLAP_CTRL_SEARCH;
545 case LDAP_REQ_UNBIND:
546 tagmask = SLAP_CTRL_UNBIND;
548 case LDAP_REQ_ABANDON:
549 tagmask = SLAP_CTRL_ABANDON;
551 case LDAP_REQ_EXTENDED:
553 assert( op->ore_reqoid.bv_val != NULL );
554 if( sc->sc_extendedopsbv != NULL ) {
556 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
557 if( bvmatch( &op->ore_reqoid,
558 &sc->sc_extendedopsbv[i] ) )
567 *text = "controls internal error";
571 if (( sc->sc_mask & tagmask ) == tagmask ) {
572 /* available extension */
575 if( !sc->sc_parse ) {
576 *text = "not yet implemented";
580 rc = sc->sc_parse( op, rs, control );
582 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
586 } else if( control->ldctl_iscritical ) {
587 /* unavailable CRITICAL control */
588 *text = "critical extension is unavailable";
589 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
591 } else if( control->ldctl_iscritical ) {
592 /* unrecognized CRITICAL control */
593 *text = "critical extension is not recognized";
594 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
609 BerElement *ber = op->o_ber;
612 len = ber_pvt_ber_remaining(ber);
616 rs->sr_err = LDAP_SUCCESS;
620 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
621 if( tag == LBER_ERROR ) {
622 rs->sr_err = SLAPD_DISCONNECT;
623 rs->sr_text = "unexpected data in PDU";
629 Debug( LDAP_DEBUG_TRACE,
630 "=> get_ctrls\n", 0, 0, 0 );
632 if( op->o_protocol < LDAP_VERSION3 ) {
633 rs->sr_err = SLAPD_DISCONNECT;
634 rs->sr_text = "controls require LDAPv3";
638 /* one for first control, one for termination */
639 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
642 if( op->ctrls == NULL ) {
643 rs->sr_err = LDAP_NO_MEMORY;
644 rs->sr_text = "no memory";
649 op->o_ctrls[nctrls] = NULL;
651 /* step through each element */
652 for( tag = ber_first_element( ber, &len, &opaque );
654 tag = ber_next_element( ber, &len, opaque ) )
657 LDAPControl **tctrls;
659 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
660 memset(c, 0, sizeof(LDAPControl));
662 /* allocate pointer space for current controls (nctrls)
663 * + this control + extra NULL
665 tctrls = op->o_tmprealloc( op->o_ctrls,
666 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
669 if( tctrls == NULL ) {
671 ldap_controls_free(op->o_ctrls);
674 rs->sr_err = LDAP_NO_MEMORY;
675 rs->sr_text = "no memory";
679 op->o_ctrls = tctrls;
681 op->o_ctrls[nctrls++] = c;
682 op->o_ctrls[nctrls] = NULL;
684 tag = ber_scanf( ber, "{m" /*}*/, &bv );
685 c->ldctl_oid = bv.bv_val;
687 if( tag == LBER_ERROR ) {
688 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
691 slap_free_ctrls( op, op->o_ctrls );
693 rs->sr_err = SLAPD_DISCONNECT;
694 rs->sr_text = "decoding controls error";
697 } else if( c->ldctl_oid == NULL ) {
698 Debug( LDAP_DEBUG_TRACE,
699 "get_ctrls: conn %lu got emtpy OID.\n",
700 op->o_connid, 0, 0 );
702 slap_free_ctrls( op, op->o_ctrls );
704 rs->sr_err = LDAP_PROTOCOL_ERROR;
705 rs->sr_text = "OID field is empty";
709 tag = ber_peek_tag( ber, &len );
711 if( tag == LBER_BOOLEAN ) {
713 tag = ber_scanf( ber, "b", &crit );
715 if( tag == LBER_ERROR ) {
716 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
718 slap_free_ctrls( op, op->o_ctrls );
720 rs->sr_err = SLAPD_DISCONNECT;
721 rs->sr_text = "decoding controls error";
725 c->ldctl_iscritical = (crit != 0);
726 tag = ber_peek_tag( ber, &len );
729 if( tag == LBER_OCTETSTRING ) {
730 tag = ber_scanf( ber, "m", &c->ldctl_value );
732 if( tag == LBER_ERROR ) {
733 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
734 "%s (%scritical): get value failed.\n",
735 op->o_connid, c->ldctl_oid,
736 c->ldctl_iscritical ? "" : "non" );
737 slap_free_ctrls( op, op->o_ctrls );
739 rs->sr_err = SLAPD_DISCONNECT;
740 rs->sr_text = "decoding controls error";
745 Debug( LDAP_DEBUG_TRACE,
746 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
747 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
749 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
750 if ( rs->sr_err != LDAP_SUCCESS ) {
756 Debug( LDAP_DEBUG_TRACE,
757 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
758 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
760 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
761 if( rs->sr_err == SLAPD_DISCONNECT ) {
762 rs->sr_err = LDAP_PROTOCOL_ERROR;
763 send_ldap_disconnect( op, rs );
764 rs->sr_err = SLAPD_DISCONNECT;
766 send_ldap_result( op, rs );
778 BI_chk_controls fnc )
782 switch ( op->o_ctrlflag[ ctrl ] ) {
783 case SLAP_CONTROL_NONCRITICAL:
784 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
785 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
786 slap_known_controls[ ctrl - 1 ] ) == 0 )
793 rs->sr_err = LDAP_OTHER;
801 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
804 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
805 ( i - j ) * sizeof( LDAPControl * ) );
808 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
812 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
814 Debug( LDAP_DEBUG_ANY, "%s: "
815 "non-critical control \"%s\" not supported; stripped.\n",
816 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
819 case SLAP_CONTROL_IGNORED:
820 case SLAP_CONTROL_NONE:
821 rs->sr_err = SLAP_CB_CONTINUE;
824 case SLAP_CONTROL_CRITICAL:
825 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
829 Debug( LDAP_DEBUG_ANY, "%s: "
830 "critical control \"%s\" not supported.\n",
831 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
835 /* handle all cases! */
842 static int parseDontUseCopy (
847 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
848 rs->sr_text = "dontUseCopy control specified multiple times";
849 return LDAP_PROTOCOL_ERROR;
852 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
853 rs->sr_text = "dontUseCopy control value not absent";
854 return LDAP_PROTOCOL_ERROR;
857 if ( !ctrl->ldctl_iscritical ) {
858 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
859 return LDAP_PROTOCOL_ERROR;
862 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
866 static int parseRelax (
871 if ( op->o_relax != SLAP_CONTROL_NONE ) {
872 rs->sr_text = "relax control specified multiple times";
873 return LDAP_PROTOCOL_ERROR;
876 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
877 rs->sr_text = "relax control value not absent";
878 return LDAP_PROTOCOL_ERROR;
881 op->o_relax = ctrl->ldctl_iscritical
882 ? SLAP_CONTROL_CRITICAL
883 : SLAP_CONTROL_NONCRITICAL;
888 static int parseManageDSAit (
893 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
894 rs->sr_text = "manageDSAit control specified multiple times";
895 return LDAP_PROTOCOL_ERROR;
898 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
899 rs->sr_text = "manageDSAit control value not absent";
900 return LDAP_PROTOCOL_ERROR;
903 op->o_managedsait = ctrl->ldctl_iscritical
904 ? SLAP_CONTROL_CRITICAL
905 : SLAP_CONTROL_NONCRITICAL;
910 static int parseProxyAuthz (
916 struct berval dn = BER_BVNULL;
918 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
919 rs->sr_text = "proxy authorization control specified multiple times";
920 return LDAP_PROTOCOL_ERROR;
923 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
924 rs->sr_text = "proxy authorization control value absent";
925 return LDAP_PROTOCOL_ERROR;
928 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
929 && BER_BVISEMPTY( &op->o_ndn ) )
931 rs->sr_text = "anonymous proxied authorization not allowed";
932 return LDAP_PROXIED_AUTHORIZATION_DENIED;
935 op->o_proxy_authz = ctrl->ldctl_iscritical
936 ? SLAP_CONTROL_CRITICAL
937 : SLAP_CONTROL_NONCRITICAL;
939 Debug( LDAP_DEBUG_ARGS,
940 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
942 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
945 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
946 Debug( LDAP_DEBUG_TRACE,
947 "parseProxyAuthz: conn=%lu anonymous\n",
948 op->o_connid, 0, 0 );
951 if ( !BER_BVISNULL( &op->o_ndn ) ) {
952 op->o_ndn.bv_val[ 0 ] = '\0';
954 op->o_ndn.bv_len = 0;
956 if ( !BER_BVISNULL( &op->o_dn ) ) {
957 op->o_dn.bv_val[ 0 ] = '\0';
964 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
965 NULL, &dn, SLAP_GETDN_AUTHZID );
967 /* FIXME: empty DN in proxyAuthz control should be legal... */
968 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
970 ch_free( dn.bv_val );
972 rs->sr_text = "authzId mapping failed";
973 return LDAP_PROXIED_AUTHORIZATION_DENIED;
976 Debug( LDAP_DEBUG_TRACE,
977 "parseProxyAuthz: conn=%lu \"%s\"\n",
979 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
981 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
984 ch_free( dn.bv_val );
985 rs->sr_text = "not authorized to assume identity";
986 return LDAP_PROXIED_AUTHORIZATION_DENIED;
989 ch_free( op->o_ndn.bv_val );
990 ch_free( op->o_dn.bv_val );
993 * NOTE: since slap_sasl_getdn() returns a normalized dn,
994 * from now on op->o_dn is normalized
997 ber_dupbv( &op->o_dn, &dn );
999 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1000 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1002 return LDAP_SUCCESS;
1005 static int parseNoOp (
1010 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1011 rs->sr_text = "noop control specified multiple times";
1012 return LDAP_PROTOCOL_ERROR;
1015 if ( ctrl->ldctl_value.bv_len ) {
1016 rs->sr_text = "noop control value not empty";
1017 return LDAP_PROTOCOL_ERROR;
1020 op->o_noop = ctrl->ldctl_iscritical
1021 ? SLAP_CONTROL_CRITICAL
1022 : SLAP_CONTROL_NONCRITICAL;
1024 return LDAP_SUCCESS;
1027 static int parsePagedResults (
1032 int rc = LDAP_SUCCESS;
1036 struct berval cookie = BER_BVNULL;
1037 PagedResultsState *ps;
1039 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1040 rs->sr_text = "paged results control specified multiple times";
1041 return LDAP_PROTOCOL_ERROR;
1044 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1045 rs->sr_text = "paged results control value is absent";
1046 return LDAP_PROTOCOL_ERROR;
1049 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1050 rs->sr_text = "paged results control value is empty";
1051 return LDAP_PROTOCOL_ERROR;
1054 /* Parse the control value
1055 * realSearchControlValue ::= SEQUENCE {
1056 * size INTEGER (0..maxInt),
1057 * -- requested page size from client
1058 * -- result set size estimate from server
1059 * cookie OCTET STRING
1062 ber = ber_init( &ctrl->ldctl_value );
1063 if ( ber == NULL ) {
1064 rs->sr_text = "internal error";
1068 tag = ber_scanf( ber, "{im}", &size, &cookie );
1070 if ( tag == LBER_ERROR ) {
1071 rs->sr_text = "paged results control could not be decoded";
1072 rc = LDAP_PROTOCOL_ERROR;
1077 rs->sr_text = "paged results control size invalid";
1078 rc = LDAP_PROTOCOL_ERROR;
1082 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1083 *ps = op->o_conn->c_pagedresults_state;
1085 op->o_pagedresults_state = ps;
1087 /* NOTE: according to RFC 2696 3.:
1089 If the page size is greater than or equal to the sizeLimit value, the
1090 server should ignore the control as the request can be satisfied in a
1093 * NOTE: this assumes that the op->ors_slimit be set
1094 * before the controls are parsed.
1097 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1098 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1100 } else if ( ctrl->ldctl_iscritical ) {
1101 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1104 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1108 (void)ber_free( ber, 1 );
1112 #ifdef SLAP_SORTEDRESULTS
1113 static int parseSortedResults (
1118 int rc = LDAP_SUCCESS;
1120 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1121 rs->sr_text = "sorted results control specified multiple times";
1122 return LDAP_PROTOCOL_ERROR;
1125 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1126 rs->sr_text = "sorted results control value is absent";
1127 return LDAP_PROTOCOL_ERROR;
1130 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1131 rs->sr_text = "sorted results control value is empty";
1132 return LDAP_PROTOCOL_ERROR;
1135 /* blow off parsing the value */
1137 op->o_sortedresults = ctrl->ldctl_iscritical
1138 ? SLAP_CONTROL_CRITICAL
1139 : SLAP_CONTROL_NONCRITICAL;
1145 static int parseAssert (
1151 struct berval fstr = BER_BVNULL;
1153 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1154 rs->sr_text = "assert control specified multiple times";
1155 return LDAP_PROTOCOL_ERROR;
1158 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1159 rs->sr_text = "assert control value is absent";
1160 return LDAP_PROTOCOL_ERROR;
1163 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1164 rs->sr_text = "assert control value is empty";
1165 return LDAP_PROTOCOL_ERROR;
1168 ber = ber_init( &(ctrl->ldctl_value) );
1170 rs->sr_text = "assert control: internal error";
1174 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1176 (void) ber_free( ber, 1 );
1177 if( rs->sr_err != LDAP_SUCCESS ) {
1178 if( rs->sr_err == SLAPD_DISCONNECT ) {
1179 rs->sr_err = LDAP_PROTOCOL_ERROR;
1180 send_ldap_disconnect( op, rs );
1181 rs->sr_err = SLAPD_DISCONNECT;
1183 send_ldap_result( op, rs );
1185 if( op->o_assertion != NULL ) {
1186 filter_free_x( op, op->o_assertion );
1192 filter2bv_x( op, op->o_assertion, &fstr );
1194 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1195 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1196 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1199 op->o_assert = ctrl->ldctl_iscritical
1200 ? SLAP_CONTROL_CRITICAL
1201 : SLAP_CONTROL_NONCRITICAL;
1203 rs->sr_err = LDAP_SUCCESS;
1204 return LDAP_SUCCESS;
1207 static int parsePreRead (
1212 ber_len_t siz, off, i;
1213 AttributeName *an = NULL;
1216 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1217 rs->sr_text = "preread control specified multiple times";
1218 return LDAP_PROTOCOL_ERROR;
1221 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1222 rs->sr_text = "preread control value is absent";
1223 return LDAP_PROTOCOL_ERROR;
1226 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1227 rs->sr_text = "preread control value is empty";
1228 return LDAP_PROTOCOL_ERROR;
1232 if ( op->o_txnSpec ) { /* temporary limitation */
1233 rs->sr_text = "cannot perform pre-read in transaction";
1234 return LDAP_UNWILLING_TO_PERFORM;
1238 ber = ber_init( &(ctrl->ldctl_value) );
1240 rs->sr_text = "preread control: internal error";
1244 rs->sr_err = LDAP_SUCCESS;
1246 siz = sizeof( AttributeName );
1247 off = offsetof( AttributeName, an_name );
1248 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1249 rs->sr_text = "preread control: decoding error";
1250 rs->sr_err = LDAP_PROTOCOL_ERROR;
1254 for( i=0; i<siz; i++ ) {
1255 const char *dummy = NULL;
1257 an[i].an_desc = NULL;
1259 an[i].an_oc_exclude = 0;
1260 rs->sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1261 if ( rs->sr_err != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1264 : "postread control: unknown attributeType";
1269 op->o_preread = ctrl->ldctl_iscritical
1270 ? SLAP_CONTROL_CRITICAL
1271 : SLAP_CONTROL_NONCRITICAL;
1273 op->o_preread_attrs = an;
1276 (void) ber_free( ber, 1 );
1280 static int parsePostRead (
1285 ber_len_t siz, off, i;
1286 AttributeName *an = NULL;
1289 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1290 rs->sr_text = "postread control specified multiple times";
1291 return LDAP_PROTOCOL_ERROR;
1294 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1295 rs->sr_text = "postread control value is absent";
1296 return LDAP_PROTOCOL_ERROR;
1299 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1300 rs->sr_text = "postread control value is empty";
1301 return LDAP_PROTOCOL_ERROR;
1305 if ( op->o_txnSpec ) { /* temporary limitation */
1306 rs->sr_text = "cannot perform post-read in transaction";
1307 return LDAP_UNWILLING_TO_PERFORM;
1311 ber = ber_init( &(ctrl->ldctl_value) );
1313 rs->sr_text = "postread control: internal error";
1317 rs->sr_err = LDAP_SUCCESS;
1318 siz = sizeof( AttributeName );
1319 off = offsetof( AttributeName, an_name );
1320 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1321 rs->sr_text = "postread control: decoding error";
1322 rs->sr_err = LDAP_PROTOCOL_ERROR;
1326 for ( i = 0; i < siz; i++ ) {
1327 const char *dummy = NULL;
1330 an[i].an_desc = NULL;
1332 an[i].an_oc_exclude = 0;
1333 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1334 if ( rc != LDAP_SUCCESS ) {
1336 static struct berval special_attrs[] = {
1337 BER_BVC( LDAP_NO_ATTRS ),
1338 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1339 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1343 /* deal with special attribute types */
1344 for ( i = 0; !BER_BVISNULL( &special_attrs[ i ] ); i++ ) {
1345 if ( bvmatch( &an[i].an_name, &special_attrs[ i ] ) ) {
1350 if ( BER_BVISNULL( &special_attrs[ i ] ) && ctrl->ldctl_iscritical ) {
1354 : "postread control: unknown attributeType";
1360 op->o_postread = ctrl->ldctl_iscritical
1361 ? SLAP_CONTROL_CRITICAL
1362 : SLAP_CONTROL_NONCRITICAL;
1364 op->o_postread_attrs = an;
1367 (void) ber_free( ber, 1 );
1371 static int parseValuesReturnFilter (
1377 struct berval fstr = BER_BVNULL;
1379 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1380 rs->sr_text = "valuesReturnFilter control specified multiple times";
1381 return LDAP_PROTOCOL_ERROR;
1384 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1385 rs->sr_text = "valuesReturnFilter control value is absent";
1386 return LDAP_PROTOCOL_ERROR;
1389 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1390 rs->sr_text = "valuesReturnFilter control value is empty";
1391 return LDAP_PROTOCOL_ERROR;
1394 ber = ber_init( &(ctrl->ldctl_value) );
1396 rs->sr_text = "internal error";
1400 rs->sr_err = get_vrFilter( op, ber,
1401 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1403 (void) ber_free( ber, 1 );
1405 if( rs->sr_err != LDAP_SUCCESS ) {
1406 if( rs->sr_err == SLAPD_DISCONNECT ) {
1407 rs->sr_err = LDAP_PROTOCOL_ERROR;
1408 send_ldap_disconnect( op, rs );
1409 rs->sr_err = SLAPD_DISCONNECT;
1411 send_ldap_result( op, rs );
1413 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1417 vrFilter2bv( op, op->o_vrFilter, &fstr );
1420 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1421 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1422 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1425 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1426 ? SLAP_CONTROL_CRITICAL
1427 : SLAP_CONTROL_NONCRITICAL;
1429 rs->sr_err = LDAP_SUCCESS;
1430 return LDAP_SUCCESS;
1433 static int parseSubentries (
1438 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1439 rs->sr_text = "subentries control specified multiple times";
1440 return LDAP_PROTOCOL_ERROR;
1443 /* FIXME: should use BER library */
1444 if( ( ctrl->ldctl_value.bv_len != 3 )
1445 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1446 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1448 rs->sr_text = "subentries control value encoding is bogus";
1449 return LDAP_PROTOCOL_ERROR;
1452 op->o_subentries = ctrl->ldctl_iscritical
1453 ? SLAP_CONTROL_CRITICAL
1454 : SLAP_CONTROL_NONCRITICAL;
1456 if (ctrl->ldctl_value.bv_val[2]) {
1457 set_subentries_visibility( op );
1460 return LDAP_SUCCESS;
1463 static int parsePermissiveModify (
1468 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1469 rs->sr_text = "permissiveModify control specified multiple times";
1470 return LDAP_PROTOCOL_ERROR;
1473 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1474 rs->sr_text = "permissiveModify control value not absent";
1475 return LDAP_PROTOCOL_ERROR;
1478 op->o_permissive_modify = ctrl->ldctl_iscritical
1479 ? SLAP_CONTROL_CRITICAL
1480 : SLAP_CONTROL_NONCRITICAL;
1482 return LDAP_SUCCESS;
1485 static int parseDomainScope (
1490 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1491 rs->sr_text = "domainScope control specified multiple times";
1492 return LDAP_PROTOCOL_ERROR;
1495 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1496 rs->sr_text = "domainScope control value not empty";
1497 return LDAP_PROTOCOL_ERROR;
1500 op->o_domain_scope = ctrl->ldctl_iscritical
1501 ? SLAP_CONTROL_CRITICAL
1502 : SLAP_CONTROL_NONCRITICAL;
1504 return LDAP_SUCCESS;
1507 #ifdef SLAP_CONTROL_X_TREE_DELETE
1508 static int parseTreeDelete (
1513 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1514 rs->sr_text = "treeDelete control specified multiple times";
1515 return LDAP_PROTOCOL_ERROR;
1518 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1519 rs->sr_text = "treeDelete control value not absent";
1520 return LDAP_PROTOCOL_ERROR;
1523 op->o_tree_delete = ctrl->ldctl_iscritical
1524 ? SLAP_CONTROL_CRITICAL
1525 : SLAP_CONTROL_NONCRITICAL;
1527 return LDAP_SUCCESS;
1531 static int parseSearchOptions (
1537 ber_int_t search_flags;
1540 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1541 rs->sr_text = "searchOptions control value is absent";
1542 return LDAP_PROTOCOL_ERROR;
1545 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1546 rs->sr_text = "searchOptions control value is empty";
1547 return LDAP_PROTOCOL_ERROR;
1550 ber = ber_init( &ctrl->ldctl_value );
1552 rs->sr_text = "internal error";
1556 tag = ber_scanf( ber, "{i}", &search_flags );
1557 (void) ber_free( ber, 1 );
1559 if ( tag == LBER_ERROR ) {
1560 rs->sr_text = "searchOptions control decoding error";
1561 return LDAP_PROTOCOL_ERROR;
1564 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1565 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1566 rs->sr_text = "searchOptions control specified multiple times "
1567 "or with domainScope control";
1568 return LDAP_PROTOCOL_ERROR;
1571 op->o_domain_scope = ctrl->ldctl_iscritical
1572 ? SLAP_CONTROL_CRITICAL
1573 : SLAP_CONTROL_NONCRITICAL;
1576 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1577 /* Other search flags not recognised so far,
1579 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1581 rs->sr_text = "searchOptions contained unrecognized flag";
1582 return LDAP_UNWILLING_TO_PERFORM;
1585 return LDAP_SUCCESS;