2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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 parsePreRead;
29 static SLAP_CTRL_PARSE_FN parsePostRead;
30 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
32 static SLAP_CTRL_PARSE_FN parseDontUseCopy;
33 static SLAP_CTRL_PARSE_FN parseManageDIT;
35 static SLAP_CTRL_PARSE_FN parseManageDSAit;
36 static SLAP_CTRL_PARSE_FN parseNoOp;
37 static SLAP_CTRL_PARSE_FN parsePagedResults;
39 static SLAP_CTRL_PARSE_FN parseSortedResults;
41 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
42 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
43 static SLAP_CTRL_PARSE_FN parseDomainScope;
44 #ifdef SLAP_CONTROL_X_TREE_DELETE
45 static SLAP_CTRL_PARSE_FN parseTreeDelete;
47 static SLAP_CTRL_PARSE_FN parseSearchOptions;
48 static SLAP_CTRL_PARSE_FN parseSubentries;
50 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
52 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
53 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
55 struct slap_control_ids slap_cids;
61 /* The controlID for this control */
64 /* Operations supported by control */
67 /* Extended operations supported by control */
68 char **sc_extendedops; /* input */
69 BerVarray sc_extendedopsbv; /* run-time use */
71 /* Control parsing callback */
72 SLAP_CTRL_PARSE_FN *sc_parse;
74 LDAP_SLIST_ENTRY(slap_control) sc_next;
77 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
78 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
81 * all known request control OIDs should be added to this list
84 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
85 * addresses an undefined control; this allows to safely test for
86 * well known controls even if they are not registered, e.g. if
87 * they get moved to modules. An example is sc_LDAPsync, which
88 * is implemented in the syncprov overlay and thus, if configured
89 * as dynamic module, may not be registered. One side effect is that
90 * slap_known_controls[0] == NULL, so it should always be used
92 * FIXME: should we define the "undefined control" oid?
94 char *slap_known_controls[SLAP_MAX_CIDS+1];
95 static int num_known_controls = 1;
97 static char *proxy_authz_extops[] = {
98 LDAP_EXOP_MODIFY_PASSWD,
104 static char *manageDSAit_extops[] = {
109 static struct slap_control control_defs[] = {
110 { LDAP_CONTROL_ASSERT,
111 (int)offsetof(struct slap_control_ids, sc_assert),
112 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME|
113 SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
115 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
116 { LDAP_CONTROL_PRE_READ,
117 (int)offsetof(struct slap_control_ids, sc_preRead),
118 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
120 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
121 { LDAP_CONTROL_POST_READ,
122 (int)offsetof(struct slap_control_ids, sc_postRead),
123 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
125 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
126 { LDAP_CONTROL_VALUESRETURNFILTER,
127 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
128 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
130 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
131 { LDAP_CONTROL_PAGEDRESULTS,
132 (int)offsetof(struct slap_control_ids, sc_pagedResults),
135 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
137 { LDAP_CONTROL_SORTREQUEST,
138 (int)offsetof(struct slap_control_ids, sc_sortedResults),
139 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
141 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
143 { LDAP_CONTROL_X_DOMAIN_SCOPE,
144 (int)offsetof(struct slap_control_ids, sc_domainScope),
145 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
147 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
148 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
149 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
150 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
152 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
153 #ifdef SLAP_CONTROL_X_TREE_DELETE
154 { LDAP_CONTROL_X_TREE_DELETE,
155 (int)offsetof(struct slap_control_ids, sc_treeDelete),
156 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
158 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
160 { LDAP_CONTROL_X_SEARCH_OPTIONS,
161 (int)offsetof(struct slap_control_ids, sc_searchOptions),
162 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
164 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
165 { LDAP_CONTROL_SUBENTRIES,
166 (int)offsetof(struct slap_control_ids, sc_subentries),
169 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
171 (int)offsetof(struct slap_control_ids, sc_noOp),
172 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
174 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
176 { LDAP_CONTROL_DONTUSECOPY,
177 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
178 SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
180 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
181 { LDAP_CONTROL_MANAGEDIT,
182 (int)offsetof(struct slap_control_ids, sc_manageDIT),
183 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
185 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
188 { LDAP_CONTROL_X_TXN_SPEC,
189 (int)offsetof(struct slap_control_ids, sc_txnSpec),
190 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
192 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
194 { LDAP_CONTROL_MANAGEDSAIT,
195 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
197 manageDSAit_extops, NULL,
198 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
199 { LDAP_CONTROL_PROXY_AUTHZ,
200 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
201 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
202 proxy_authz_extops, NULL,
203 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
204 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
207 static struct slap_control *
208 find_ctrl( const char *oid );
211 * Register a supported control.
213 * This can be called by an OpenLDAP plugin or, indirectly, by a
214 * SLAPI plugin calling slapi_register_supported_control().
216 * NOTE: if flags == 1 the control is replaced if already registered;
217 * otherwise registering an already registered control is not allowed.
220 register_supported_control2(const char *controloid,
221 slap_mask_t controlmask,
223 SLAP_CTRL_PARSE_FN *controlparsefn,
227 struct slap_control *sc = NULL;
229 BerVarray extendedopsbv = NULL;
231 if ( num_known_controls >= SLAP_MAX_CIDS ) {
232 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
233 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
234 SLAP_MAX_CIDS, 0, 0 );
238 if ( controloid == NULL ) {
239 return LDAP_PARAM_ERROR;
242 /* check if already registered */
243 for ( i = 0; slap_known_controls[ i ]; i++ ) {
244 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
246 Debug( LDAP_DEBUG_TRACE,
247 "Control %s already registered; replacing.\n",
249 /* (find and) replace existing handler */
250 sc = find_ctrl( controloid );
251 assert( sc != NULL );
255 Debug( LDAP_DEBUG_ANY,
256 "Control %s already registered.\n",
258 return LDAP_PARAM_ERROR;
262 /* turn compatible extended operations into bervals */
263 if ( controlexops != NULL ) {
266 for ( i = 0; controlexops[ i ]; i++ );
268 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
269 if ( extendedopsbv == NULL ) {
270 return LDAP_NO_MEMORY;
273 for ( i = 0; controlexops[ i ]; i++ ) {
274 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
279 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
281 return LDAP_NO_MEMORY;
284 sc->sc_oid = ch_strdup( controloid );
285 sc->sc_cid = num_known_controls;
287 /* Update slap_known_controls, too. */
288 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
289 slap_known_controls[num_known_controls++] = NULL;
291 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
292 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
295 if ( sc->sc_extendedopsbv ) {
296 /* FIXME: in principle, we should rather merge
297 * existing extops with those supported by the
298 * new control handling implementation.
299 * In fact, whether a control is compatible with
300 * an extop should not be a matter of implementation.
301 * We likely also need a means for a newly
302 * registered extop to declare that it is
303 * comptible with an already registered control.
305 ber_bvarray_free( sc->sc_extendedopsbv );
306 sc->sc_extendedopsbv = NULL;
307 sc->sc_extendedops = NULL;
311 sc->sc_extendedopsbv = extendedopsbv;
312 sc->sc_mask = controlmask;
313 sc->sc_parse = controlparsefn;
315 *controlcid = sc->sc_cid;
322 * One-time initialization of internal controls.
325 slap_controls_init( void )
331 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
332 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
333 rc = register_supported_control( control_defs[i].sc_oid,
334 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
335 control_defs[i].sc_parse, cid );
336 if ( rc != LDAP_SUCCESS ) break;
343 * Free memory associated with list of supported controls.
346 controls_destroy( void )
348 struct slap_control *sc;
350 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
351 sc = LDAP_SLIST_FIRST(&controls_list);
352 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
354 ch_free( sc->sc_oid );
355 if ( sc->sc_extendedopsbv != NULL ) {
356 ber_bvarray_free( sc->sc_extendedopsbv );
363 * Format the supportedControl attribute of the root DSE,
364 * detailing which controls are supported by the directory
368 controls_root_dse_info( Entry *e )
370 AttributeDescription *ad_supportedControl
371 = slap_schema.si_ad_supportedControl;
372 struct berval vals[2];
373 struct slap_control *sc;
375 vals[1].bv_val = NULL;
378 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
379 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
381 vals[0].bv_val = sc->sc_oid;
382 vals[0].bv_len = strlen( sc->sc_oid );
384 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
393 * Return a list of OIDs and operation masks for supported
394 * controls. Used by SLAPI.
397 get_supported_controls(char ***ctrloidsp,
398 slap_mask_t **ctrlmasks)
403 struct slap_control *sc;
407 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
417 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
418 if ( oids == NULL ) {
419 return LDAP_NO_MEMORY;
421 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
422 if ( masks == NULL ) {
424 return LDAP_NO_MEMORY;
429 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
430 oids[n] = ch_strdup( sc->sc_oid );
431 masks[n] = sc->sc_mask;
444 * Find a control given its OID.
446 static struct slap_control *
447 find_ctrl( const char *oid )
449 struct slap_control *sc;
451 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
452 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
461 slap_find_control_id(
465 struct slap_control *ctrl = find_ctrl( oid );
467 if ( cid ) *cid = ctrl->sc_cid;
470 return LDAP_CONTROL_NOT_FOUND;
474 slap_global_control( Operation *op, const char *oid, int *cid )
476 struct slap_control *ctrl = find_ctrl( oid );
478 if ( ctrl == NULL ) {
479 /* should not be reachable */
480 Debug( LDAP_DEBUG_ANY,
481 "slap_global_control: unrecognized control: %s\n",
483 return LDAP_CONTROL_NOT_FOUND;
486 if ( cid ) *cid = ctrl->sc_cid;
488 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
489 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
490 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
492 return LDAP_COMPARE_TRUE;
496 Debug( LDAP_DEBUG_TRACE,
497 "slap_global_control: unavailable control: %s\n",
501 return LDAP_COMPARE_FALSE;
504 void slap_free_ctrls(
506 LDAPControl **ctrls )
510 for (i=0; ctrls[i]; i++) {
511 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
513 op->o_tmpfree( ctrls, op->o_tmpmemctx );
519 LDAPControl *control,
522 struct slap_control *sc;
524 sc = find_ctrl( control->ldctl_oid );
526 /* recognized control */
528 switch( op->o_tag ) {
530 tagmask = SLAP_CTRL_ADD;
533 tagmask = SLAP_CTRL_BIND;
535 case LDAP_REQ_COMPARE:
536 tagmask = SLAP_CTRL_COMPARE;
538 case LDAP_REQ_DELETE:
539 tagmask = SLAP_CTRL_DELETE;
541 case LDAP_REQ_MODIFY:
542 tagmask = SLAP_CTRL_MODIFY;
544 case LDAP_REQ_RENAME:
545 tagmask = SLAP_CTRL_RENAME;
547 case LDAP_REQ_SEARCH:
548 tagmask = SLAP_CTRL_SEARCH;
550 case LDAP_REQ_UNBIND:
551 tagmask = SLAP_CTRL_UNBIND;
553 case LDAP_REQ_ABANDON:
554 tagmask = SLAP_CTRL_ABANDON;
556 case LDAP_REQ_EXTENDED:
558 assert( op->ore_reqoid.bv_val != NULL );
559 if( sc->sc_extendedopsbv != NULL ) {
561 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
562 if( bvmatch( &op->ore_reqoid,
563 &sc->sc_extendedopsbv[i] ) )
572 *text = "controls internal error";
576 if (( sc->sc_mask & tagmask ) == tagmask ) {
577 /* available extension */
580 if( !sc->sc_parse ) {
581 *text = "not yet implemented";
585 rc = sc->sc_parse( op, rs, control );
587 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
591 } else if( control->ldctl_iscritical ) {
592 /* unavailable CRITICAL control */
593 *text = "critical extension is unavailable";
594 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
596 } else if( control->ldctl_iscritical ) {
597 /* unrecognized CRITICAL control */
598 *text = "critical extension is not recognized";
599 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
614 BerElement *ber = op->o_ber;
617 len = ber_pvt_ber_remaining(ber);
621 rs->sr_err = LDAP_SUCCESS;
625 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
626 if( tag == LBER_ERROR ) {
627 rs->sr_err = SLAPD_DISCONNECT;
628 rs->sr_text = "unexpected data in PDU";
634 Debug( LDAP_DEBUG_TRACE,
635 "=> get_ctrls\n", 0, 0, 0 );
637 if( op->o_protocol < LDAP_VERSION3 ) {
638 rs->sr_err = SLAPD_DISCONNECT;
639 rs->sr_text = "controls require LDAPv3";
643 /* one for first control, one for termination */
644 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
647 if( op->ctrls == NULL ) {
648 rs->sr_err = LDAP_NO_MEMORY;
649 rs->sr_text = "no memory";
654 op->o_ctrls[nctrls] = NULL;
656 /* step through each element */
657 for( tag = ber_first_element( ber, &len, &opaque );
659 tag = ber_next_element( ber, &len, opaque ) )
662 LDAPControl **tctrls;
664 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
665 memset(c, 0, sizeof(LDAPControl));
667 /* allocate pointer space for current controls (nctrls)
668 * + this control + extra NULL
670 tctrls = op->o_tmprealloc( op->o_ctrls,
671 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
674 if( tctrls == NULL ) {
676 ldap_controls_free(op->o_ctrls);
679 rs->sr_err = LDAP_NO_MEMORY;
680 rs->sr_text = "no memory";
684 op->o_ctrls = tctrls;
686 op->o_ctrls[nctrls++] = c;
687 op->o_ctrls[nctrls] = NULL;
689 tag = ber_scanf( ber, "{m" /*}*/, &bv );
690 c->ldctl_oid = bv.bv_val;
692 if( tag == LBER_ERROR ) {
693 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
696 slap_free_ctrls( op, op->o_ctrls );
698 rs->sr_err = SLAPD_DISCONNECT;
699 rs->sr_text = "decoding controls error";
702 } else if( c->ldctl_oid == NULL ) {
703 Debug( LDAP_DEBUG_TRACE,
704 "get_ctrls: conn %lu got emtpy OID.\n",
705 op->o_connid, 0, 0 );
707 slap_free_ctrls( op, op->o_ctrls );
709 rs->sr_err = LDAP_PROTOCOL_ERROR;
710 rs->sr_text = "OID field is empty";
714 tag = ber_peek_tag( ber, &len );
716 if( tag == LBER_BOOLEAN ) {
718 tag = ber_scanf( ber, "b", &crit );
720 if( tag == LBER_ERROR ) {
721 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
723 slap_free_ctrls( op, op->o_ctrls );
725 rs->sr_err = SLAPD_DISCONNECT;
726 rs->sr_text = "decoding controls error";
730 c->ldctl_iscritical = (crit != 0);
731 tag = ber_peek_tag( ber, &len );
734 if( tag == LBER_OCTETSTRING ) {
735 tag = ber_scanf( ber, "m", &c->ldctl_value );
737 if( tag == LBER_ERROR ) {
738 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
739 "%s (%scritical): get value failed.\n",
740 op->o_connid, c->ldctl_oid,
741 c->ldctl_iscritical ? "" : "non" );
742 slap_free_ctrls( op, op->o_ctrls );
744 rs->sr_err = SLAPD_DISCONNECT;
745 rs->sr_text = "decoding controls error";
750 Debug( LDAP_DEBUG_TRACE,
751 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
752 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
754 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
755 if ( rs->sr_err != LDAP_SUCCESS ) {
761 Debug( LDAP_DEBUG_TRACE,
762 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
763 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
765 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
766 if( rs->sr_err == SLAPD_DISCONNECT ) {
767 rs->sr_err = LDAP_PROTOCOL_ERROR;
768 send_ldap_disconnect( op, rs );
769 rs->sr_err = SLAPD_DISCONNECT;
771 send_ldap_result( op, rs );
783 BI_chk_controls fnc )
787 switch ( op->o_ctrlflag[ ctrl ] ) {
788 case SLAP_CONTROL_NONCRITICAL:
789 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
790 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
791 slap_known_controls[ ctrl - 1 ] ) == 0 )
798 rs->sr_err = LDAP_OTHER;
806 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
809 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
810 ( i - j ) * sizeof( LDAPControl * ) );
813 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
817 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
819 Debug( LDAP_DEBUG_ANY, "%s: "
820 "non-critical control \"%s\" not supported; stripped.\n",
821 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
824 case SLAP_CONTROL_IGNORED:
825 case SLAP_CONTROL_NONE:
826 rs->sr_err = SLAP_CB_CONTINUE;
829 case SLAP_CONTROL_CRITICAL:
830 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
834 Debug( LDAP_DEBUG_ANY, "%s: "
835 "critical control \"%s\" not supported.\n",
836 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
840 /* handle all cases! */
848 static int parseDontUseCopy (
853 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
854 rs->sr_text = "dontUseCopy control specified multiple times";
855 return LDAP_PROTOCOL_ERROR;
858 if ( ctrl->ldctl_value.bv_len ) {
859 rs->sr_text = "dontUseCopy control value not empty";
860 return LDAP_PROTOCOL_ERROR;
863 if ( ctrl->ldctl_iscritical != SLAP_CONTROL_CRITICAL ) {
864 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
865 return LDAP_PROTOCOL_ERROR;
868 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
872 static int parseManageDIT (
877 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
878 rs->sr_text = "manageDIT control specified multiple times";
879 return LDAP_PROTOCOL_ERROR;
882 if ( ctrl->ldctl_value.bv_len ) {
883 rs->sr_text = "manageDIT control value not empty";
884 return LDAP_PROTOCOL_ERROR;
887 op->o_managedit = ctrl->ldctl_iscritical
888 ? SLAP_CONTROL_CRITICAL
889 : SLAP_CONTROL_NONCRITICAL;
895 static int parseManageDSAit (
900 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
901 rs->sr_text = "manageDSAit control specified multiple times";
902 return LDAP_PROTOCOL_ERROR;
905 if ( ctrl->ldctl_value.bv_len ) {
906 rs->sr_text = "manageDSAit control value not empty";
907 return LDAP_PROTOCOL_ERROR;
910 op->o_managedsait = ctrl->ldctl_iscritical
911 ? SLAP_CONTROL_CRITICAL
912 : SLAP_CONTROL_NONCRITICAL;
917 static int parseProxyAuthz (
923 struct berval dn = BER_BVNULL;
925 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
926 rs->sr_text = "proxy authorization control specified multiple times";
927 return LDAP_PROTOCOL_ERROR;
930 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
931 && BER_BVISEMPTY( &op->o_ndn ) )
933 rs->sr_text = "anonymous proxied authorization not allowed";
934 return LDAP_PROXIED_AUTHORIZATION_DENIED;
937 op->o_proxy_authz = ctrl->ldctl_iscritical
938 ? SLAP_CONTROL_CRITICAL
939 : SLAP_CONTROL_NONCRITICAL;
941 Debug( LDAP_DEBUG_ARGS,
942 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
944 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
947 if ( ctrl->ldctl_value.bv_len == 0 ) {
948 Debug( LDAP_DEBUG_TRACE,
949 "parseProxyAuthz: conn=%lu anonymous\n",
950 op->o_connid, 0, 0 );
953 if ( !BER_BVISNULL( &op->o_ndn ) ) {
954 op->o_ndn.bv_val[ 0 ] = '\0';
956 op->o_ndn.bv_len = 0;
958 if ( !BER_BVISNULL( &op->o_dn ) ) {
959 op->o_dn.bv_val[ 0 ] = '\0';
966 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
967 NULL, &dn, SLAP_GETDN_AUTHZID );
969 /* FIXME: empty DN in proxyAuthz control should be legal... */
970 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
972 ch_free( dn.bv_val );
974 rs->sr_text = "authzId mapping failed";
975 return LDAP_PROXIED_AUTHORIZATION_DENIED;
978 Debug( LDAP_DEBUG_TRACE,
979 "parseProxyAuthz: conn=%lu \"%s\"\n",
981 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
983 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
986 ch_free( dn.bv_val );
987 rs->sr_text = "not authorized to assume identity";
988 return LDAP_PROXIED_AUTHORIZATION_DENIED;
991 ch_free( op->o_ndn.bv_val );
992 ch_free( op->o_dn.bv_val );
995 * NOTE: since slap_sasl_getdn() returns a normalized dn,
996 * from now on op->o_dn is normalized
999 ber_dupbv( &op->o_dn, &dn );
1002 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1003 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1005 return LDAP_SUCCESS;
1008 static int parseNoOp (
1013 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1014 rs->sr_text = "noop control specified multiple times";
1015 return LDAP_PROTOCOL_ERROR;
1018 if ( ctrl->ldctl_value.bv_len ) {
1019 rs->sr_text = "noop control value not empty";
1020 return LDAP_PROTOCOL_ERROR;
1023 op->o_noop = ctrl->ldctl_iscritical
1024 ? SLAP_CONTROL_CRITICAL
1025 : SLAP_CONTROL_NONCRITICAL;
1027 return LDAP_SUCCESS;
1030 static int parsePagedResults (
1035 int rc = LDAP_SUCCESS;
1039 struct berval cookie = BER_BVNULL;
1040 PagedResultsState *ps;
1042 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1043 rs->sr_text = "paged results control specified multiple times";
1044 return LDAP_PROTOCOL_ERROR;
1047 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1048 rs->sr_text = "paged results control value is empty (or absent)";
1049 return LDAP_PROTOCOL_ERROR;
1052 /* Parse the control value
1053 * realSearchControlValue ::= SEQUENCE {
1054 * size INTEGER (0..maxInt),
1055 * -- requested page size from client
1056 * -- result set size estimate from server
1057 * cookie OCTET STRING
1060 ber = ber_init( &ctrl->ldctl_value );
1061 if ( ber == NULL ) {
1062 rs->sr_text = "internal error";
1066 tag = ber_scanf( ber, "{im}", &size, &cookie );
1068 if ( tag == LBER_ERROR ) {
1069 rs->sr_text = "paged results control could not be decoded";
1070 rc = LDAP_PROTOCOL_ERROR;
1075 rs->sr_text = "paged results control size invalid";
1076 rc = LDAP_PROTOCOL_ERROR;
1080 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1081 *ps = op->o_conn->c_pagedresults_state;
1083 op->o_pagedresults_state = ps;
1085 /* NOTE: according to RFC 2696 3.:
1087 If the page size is greater than or equal to the sizeLimit value, the
1088 server should ignore the control as the request can be satisfied in a
1091 * NOTE: this assumes that the op->ors_slimit be set
1092 * before the controls are parsed.
1095 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1096 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1098 } else if ( ctrl->ldctl_iscritical ) {
1099 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1102 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1106 (void)ber_free( ber, 1 );
1111 static int parseSortedResults (
1116 int rc = LDAP_SUCCESS;
1118 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1119 rs->sr_text = "sorted results control specified multiple times";
1120 return LDAP_PROTOCOL_ERROR;
1123 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1124 rs->sr_text = "sorted results control value is empty (or absent)";
1125 return LDAP_PROTOCOL_ERROR;
1128 /* blow off parsing the value */
1130 op->o_sortedresults = ctrl->ldctl_iscritical
1131 ? SLAP_CONTROL_CRITICAL
1132 : SLAP_CONTROL_NONCRITICAL;
1138 static int parseAssert (
1144 struct berval fstr = BER_BVNULL;
1146 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1147 rs->sr_text = "assert control specified multiple times";
1148 return LDAP_PROTOCOL_ERROR;
1151 if ( ctrl->ldctl_value.bv_len == 0 ) {
1152 rs->sr_text = "assert control value is empty (or absent)";
1153 return LDAP_PROTOCOL_ERROR;
1156 ber = ber_init( &(ctrl->ldctl_value) );
1158 rs->sr_text = "assert control: internal error";
1162 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1164 (void) ber_free( ber, 1 );
1165 if( rs->sr_err != LDAP_SUCCESS ) {
1166 if( rs->sr_err == SLAPD_DISCONNECT ) {
1167 rs->sr_err = LDAP_PROTOCOL_ERROR;
1168 send_ldap_disconnect( op, rs );
1169 rs->sr_err = SLAPD_DISCONNECT;
1171 send_ldap_result( op, rs );
1173 if( op->o_assertion != NULL ) {
1174 filter_free_x( op, op->o_assertion );
1180 filter2bv_x( op, op->o_assertion, &fstr );
1182 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1183 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1184 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1187 op->o_assert = ctrl->ldctl_iscritical
1188 ? SLAP_CONTROL_CRITICAL
1189 : SLAP_CONTROL_NONCRITICAL;
1191 rs->sr_err = LDAP_SUCCESS;
1192 return LDAP_SUCCESS;
1195 static int parsePreRead (
1200 ber_len_t siz, off, i;
1201 AttributeName *an = NULL;
1204 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1205 rs->sr_text = "preread control specified multiple times";
1206 return LDAP_PROTOCOL_ERROR;
1209 if ( ctrl->ldctl_value.bv_len == 0 ) {
1210 rs->sr_text = "preread control value is empty (or absent)";
1211 return LDAP_PROTOCOL_ERROR;
1215 if ( op->o_txnSpec ) { /* temporary limitation */
1216 rs->sr_text = "cannot perform pre-read in transaction";
1217 return LDAP_UNWILLING_TO_PERFORM;
1221 ber = ber_init( &(ctrl->ldctl_value) );
1223 rs->sr_text = "preread control: internal error";
1227 rs->sr_err = LDAP_SUCCESS;
1229 siz = sizeof( AttributeName );
1230 off = offsetof( AttributeName, an_name );
1231 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1232 rs->sr_text = "preread control: decoding error";
1233 rs->sr_err = LDAP_PROTOCOL_ERROR;
1237 for( i=0; i<siz; i++ ) {
1238 const char *dummy = NULL;
1240 an[i].an_desc = NULL;
1242 an[i].an_oc_exclude = 0;
1243 rs->sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1244 if ( rs->sr_err != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1247 : "postread control: unknown attributeType";
1252 op->o_preread = ctrl->ldctl_iscritical
1253 ? SLAP_CONTROL_CRITICAL
1254 : SLAP_CONTROL_NONCRITICAL;
1256 op->o_preread_attrs = an;
1259 (void) ber_free( ber, 1 );
1263 static int parsePostRead (
1268 ber_len_t siz, off, i;
1269 AttributeName *an = NULL;
1272 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1273 rs->sr_text = "postread control specified multiple times";
1274 return LDAP_PROTOCOL_ERROR;
1277 if ( ctrl->ldctl_value.bv_len == 0 ) {
1278 rs->sr_text = "postread control value is empty (or absent)";
1279 return LDAP_PROTOCOL_ERROR;
1283 if ( op->o_txnSpec ) { /* temporary limitation */
1284 rs->sr_text = "cannot perform post-read in transaction";
1285 return LDAP_UNWILLING_TO_PERFORM;
1289 ber = ber_init( &(ctrl->ldctl_value) );
1291 rs->sr_text = "postread control: internal error";
1295 rs->sr_err = LDAP_SUCCESS;
1296 siz = sizeof( AttributeName );
1297 off = offsetof( AttributeName, an_name );
1298 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1299 rs->sr_text = "postread control: decoding error";
1300 rs->sr_err = LDAP_PROTOCOL_ERROR;
1304 for( i=0; i<siz; i++ ) {
1305 const char *dummy = NULL;
1307 an[i].an_desc = NULL;
1309 an[i].an_oc_exclude = 0;
1310 rs->sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1311 if ( rs->sr_err != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1314 : "postread control: unknown attributeType";
1319 op->o_postread = ctrl->ldctl_iscritical
1320 ? SLAP_CONTROL_CRITICAL
1321 : SLAP_CONTROL_NONCRITICAL;
1323 op->o_postread_attrs = an;
1326 (void) ber_free( ber, 1 );
1330 static int parseValuesReturnFilter (
1336 struct berval fstr = BER_BVNULL;
1338 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1339 rs->sr_text = "valuesReturnFilter control specified multiple times";
1340 return LDAP_PROTOCOL_ERROR;
1343 if ( ctrl->ldctl_value.bv_len == 0 ) {
1344 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1345 return LDAP_PROTOCOL_ERROR;
1348 ber = ber_init( &(ctrl->ldctl_value) );
1350 rs->sr_text = "internal error";
1354 rs->sr_err = get_vrFilter( op, ber,
1355 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1357 (void) ber_free( ber, 1 );
1359 if( rs->sr_err != LDAP_SUCCESS ) {
1360 if( rs->sr_err == SLAPD_DISCONNECT ) {
1361 rs->sr_err = LDAP_PROTOCOL_ERROR;
1362 send_ldap_disconnect( op, rs );
1363 rs->sr_err = SLAPD_DISCONNECT;
1365 send_ldap_result( op, rs );
1367 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1371 vrFilter2bv( op, op->o_vrFilter, &fstr );
1374 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1375 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1376 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1379 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1380 ? SLAP_CONTROL_CRITICAL
1381 : SLAP_CONTROL_NONCRITICAL;
1383 rs->sr_err = LDAP_SUCCESS;
1384 return LDAP_SUCCESS;
1387 static int parseSubentries (
1392 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1393 rs->sr_text = "subentries control specified multiple times";
1394 return LDAP_PROTOCOL_ERROR;
1397 /* FIXME: should use BER library */
1398 if( ( ctrl->ldctl_value.bv_len != 3 )
1399 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1400 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1402 rs->sr_text = "subentries control value encoding is bogus";
1403 return LDAP_PROTOCOL_ERROR;
1406 op->o_subentries = ctrl->ldctl_iscritical
1407 ? SLAP_CONTROL_CRITICAL
1408 : SLAP_CONTROL_NONCRITICAL;
1410 if (ctrl->ldctl_value.bv_val[2]) {
1411 set_subentries_visibility( op );
1414 return LDAP_SUCCESS;
1417 static int parsePermissiveModify (
1422 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1423 rs->sr_text = "permissiveModify control specified multiple times";
1424 return LDAP_PROTOCOL_ERROR;
1427 if ( ctrl->ldctl_value.bv_len ) {
1428 rs->sr_text = "permissiveModify control value not empty";
1429 return LDAP_PROTOCOL_ERROR;
1432 op->o_permissive_modify = ctrl->ldctl_iscritical
1433 ? SLAP_CONTROL_CRITICAL
1434 : SLAP_CONTROL_NONCRITICAL;
1436 return LDAP_SUCCESS;
1439 static int parseDomainScope (
1444 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1445 rs->sr_text = "domainScope control specified multiple times";
1446 return LDAP_PROTOCOL_ERROR;
1449 if ( ctrl->ldctl_value.bv_len ) {
1450 rs->sr_text = "domainScope control value not empty";
1451 return LDAP_PROTOCOL_ERROR;
1454 op->o_domain_scope = ctrl->ldctl_iscritical
1455 ? SLAP_CONTROL_CRITICAL
1456 : SLAP_CONTROL_NONCRITICAL;
1458 return LDAP_SUCCESS;
1461 #ifdef SLAP_CONTROL_X_TREE_DELETE
1462 static int parseTreeDelete (
1467 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1468 rs->sr_text = "treeDelete control specified multiple times";
1469 return LDAP_PROTOCOL_ERROR;
1472 if ( ctrl->ldctl_value.bv_len ) {
1473 rs->sr_text = "treeDelete control value not empty";
1474 return LDAP_PROTOCOL_ERROR;
1477 op->o_tree_delete = ctrl->ldctl_iscritical
1478 ? SLAP_CONTROL_CRITICAL
1479 : SLAP_CONTROL_NONCRITICAL;
1481 return LDAP_SUCCESS;
1485 static int parseSearchOptions (
1491 ber_int_t search_flags;
1494 if ( ctrl->ldctl_value.bv_len == 0 ) {
1495 rs->sr_text = "searchOptions control value is empty (or absent)";
1496 return LDAP_PROTOCOL_ERROR;
1499 ber = ber_init( &ctrl->ldctl_value );
1501 rs->sr_text = "internal error";
1505 tag = ber_scanf( ber, "{i}", &search_flags );
1506 (void) ber_free( ber, 1 );
1508 if ( tag == LBER_ERROR ) {
1509 rs->sr_text = "searchOptions control decoding error";
1510 return LDAP_PROTOCOL_ERROR;
1513 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1514 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1515 rs->sr_text = "searchOptions control specified multiple times "
1516 "or with domainScope control";
1517 return LDAP_PROTOCOL_ERROR;
1520 op->o_domain_scope = ctrl->ldctl_iscritical
1521 ? SLAP_CONTROL_CRITICAL
1522 : SLAP_CONTROL_NONCRITICAL;
1525 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1526 /* Other search flags not recognised so far,
1528 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1530 rs->sr_text = "searchOptions contained unrecognized flag";
1531 return LDAP_UNWILLING_TO_PERFORM;
1534 return LDAP_SUCCESS;