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;
495 Debug( LDAP_DEBUG_TRACE,
496 "slap_global_control: unavailable control: %s\n",
499 return LDAP_COMPARE_FALSE;
502 void slap_free_ctrls(
504 LDAPControl **ctrls )
508 for (i=0; ctrls[i]; i++) {
509 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
511 op->o_tmpfree( ctrls, op->o_tmpmemctx );
517 LDAPControl *control,
520 struct slap_control *sc;
522 sc = find_ctrl( control->ldctl_oid );
524 /* recognized control */
526 switch( op->o_tag ) {
528 tagmask = SLAP_CTRL_ADD;
531 tagmask = SLAP_CTRL_BIND;
533 case LDAP_REQ_COMPARE:
534 tagmask = SLAP_CTRL_COMPARE;
536 case LDAP_REQ_DELETE:
537 tagmask = SLAP_CTRL_DELETE;
539 case LDAP_REQ_MODIFY:
540 tagmask = SLAP_CTRL_MODIFY;
542 case LDAP_REQ_RENAME:
543 tagmask = SLAP_CTRL_RENAME;
545 case LDAP_REQ_SEARCH:
546 tagmask = SLAP_CTRL_SEARCH;
548 case LDAP_REQ_UNBIND:
549 tagmask = SLAP_CTRL_UNBIND;
551 case LDAP_REQ_ABANDON:
552 tagmask = SLAP_CTRL_ABANDON;
554 case LDAP_REQ_EXTENDED:
556 assert( op->ore_reqoid.bv_val != NULL );
557 if( sc->sc_extendedopsbv != NULL ) {
559 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
560 if( bvmatch( &op->ore_reqoid,
561 &sc->sc_extendedopsbv[i] ) )
570 *text = "controls internal error";
574 if (( sc->sc_mask & tagmask ) == tagmask ) {
575 /* available extension */
578 if( !sc->sc_parse ) {
579 *text = "not yet implemented";
583 rc = sc->sc_parse( op, rs, control );
585 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
589 } else if( control->ldctl_iscritical ) {
590 /* unavailable CRITICAL control */
591 *text = "critical extension is unavailable";
592 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
594 } else if( control->ldctl_iscritical ) {
595 /* unrecognized CRITICAL control */
596 *text = "critical extension is not recognized";
597 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
612 BerElement *ber = op->o_ber;
615 len = ber_pvt_ber_remaining(ber);
619 rs->sr_err = LDAP_SUCCESS;
623 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
624 if( tag == LBER_ERROR ) {
625 rs->sr_err = SLAPD_DISCONNECT;
626 rs->sr_text = "unexpected data in PDU";
632 Debug( LDAP_DEBUG_TRACE,
633 "=> get_ctrls\n", 0, 0, 0 );
635 if( op->o_protocol < LDAP_VERSION3 ) {
636 rs->sr_err = SLAPD_DISCONNECT;
637 rs->sr_text = "controls require LDAPv3";
641 /* one for first control, one for termination */
642 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
645 if( op->ctrls == NULL ) {
646 rs->sr_err = LDAP_NO_MEMORY;
647 rs->sr_text = "no memory";
652 op->o_ctrls[nctrls] = NULL;
654 /* step through each element */
655 for( tag = ber_first_element( ber, &len, &opaque );
657 tag = ber_next_element( ber, &len, opaque ) )
660 LDAPControl **tctrls;
662 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
663 memset(c, 0, sizeof(LDAPControl));
665 /* allocate pointer space for current controls (nctrls)
666 * + this control + extra NULL
668 tctrls = op->o_tmprealloc( op->o_ctrls,
669 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
672 if( tctrls == NULL ) {
674 ldap_controls_free(op->o_ctrls);
677 rs->sr_err = LDAP_NO_MEMORY;
678 rs->sr_text = "no memory";
682 op->o_ctrls = tctrls;
684 op->o_ctrls[nctrls++] = c;
685 op->o_ctrls[nctrls] = NULL;
687 tag = ber_scanf( ber, "{m" /*}*/, &bv );
688 c->ldctl_oid = bv.bv_val;
690 if( tag == LBER_ERROR ) {
691 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
694 slap_free_ctrls( op, op->o_ctrls );
696 rs->sr_err = SLAPD_DISCONNECT;
697 rs->sr_text = "decoding controls error";
700 } else if( c->ldctl_oid == NULL ) {
701 Debug( LDAP_DEBUG_TRACE,
702 "get_ctrls: conn %lu got emtpy OID.\n",
703 op->o_connid, 0, 0 );
705 slap_free_ctrls( op, op->o_ctrls );
707 rs->sr_err = LDAP_PROTOCOL_ERROR;
708 rs->sr_text = "OID field is empty";
712 tag = ber_peek_tag( ber, &len );
714 if( tag == LBER_BOOLEAN ) {
716 tag = ber_scanf( ber, "b", &crit );
718 if( tag == LBER_ERROR ) {
719 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
721 slap_free_ctrls( op, op->o_ctrls );
723 rs->sr_err = SLAPD_DISCONNECT;
724 rs->sr_text = "decoding controls error";
728 c->ldctl_iscritical = (crit != 0);
729 tag = ber_peek_tag( ber, &len );
732 if( tag == LBER_OCTETSTRING ) {
733 tag = ber_scanf( ber, "m", &c->ldctl_value );
735 if( tag == LBER_ERROR ) {
736 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
737 "%s (%scritical): get value failed.\n",
738 op->o_connid, c->ldctl_oid,
739 c->ldctl_iscritical ? "" : "non" );
740 slap_free_ctrls( op, op->o_ctrls );
742 rs->sr_err = SLAPD_DISCONNECT;
743 rs->sr_text = "decoding controls error";
748 Debug( LDAP_DEBUG_TRACE,
749 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
750 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
752 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
753 if ( rs->sr_err != LDAP_SUCCESS ) {
759 Debug( LDAP_DEBUG_TRACE,
760 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
761 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
763 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
764 if( rs->sr_err == SLAPD_DISCONNECT ) {
765 rs->sr_err = LDAP_PROTOCOL_ERROR;
766 send_ldap_disconnect( op, rs );
767 rs->sr_err = SLAPD_DISCONNECT;
769 send_ldap_result( op, rs );
781 BI_chk_controls fnc )
785 switch ( op->o_ctrlflag[ ctrl ] ) {
786 case SLAP_CONTROL_NONCRITICAL:
787 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
788 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
789 slap_known_controls[ ctrl - 1 ] ) == 0 )
796 rs->sr_err = LDAP_OTHER;
804 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
807 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
808 ( i - j ) * sizeof( LDAPControl * ) );
811 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
815 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
817 Debug( LDAP_DEBUG_ANY, "%s: "
818 "non-critical control \"%s\" not supported; stripped.\n",
819 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
822 case SLAP_CONTROL_IGNORED:
823 case SLAP_CONTROL_NONE:
824 rs->sr_err = SLAP_CB_CONTINUE;
827 case SLAP_CONTROL_CRITICAL:
828 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
832 Debug( LDAP_DEBUG_ANY, "%s: "
833 "critical control \"%s\" not supported.\n",
834 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
838 /* handle all cases! */
846 static int parseDontUseCopy (
851 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
852 rs->sr_text = "dontUseCopy control specified multiple times";
853 return LDAP_PROTOCOL_ERROR;
856 if ( ctrl->ldctl_value.bv_len ) {
857 rs->sr_text = "dontUseCopy control value not empty";
858 return LDAP_PROTOCOL_ERROR;
861 if ( ctrl->ldctl_iscritical != SLAP_CONTROL_CRITICAL ) {
862 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
863 return LDAP_PROTOCOL_ERROR;
866 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
870 static int parseManageDIT (
875 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
876 rs->sr_text = "manageDIT control specified multiple times";
877 return LDAP_PROTOCOL_ERROR;
880 if ( ctrl->ldctl_value.bv_len ) {
881 rs->sr_text = "manageDIT control value not empty";
882 return LDAP_PROTOCOL_ERROR;
885 op->o_managedit = ctrl->ldctl_iscritical
886 ? SLAP_CONTROL_CRITICAL
887 : SLAP_CONTROL_NONCRITICAL;
893 static int parseManageDSAit (
898 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
899 rs->sr_text = "manageDSAit control specified multiple times";
900 return LDAP_PROTOCOL_ERROR;
903 if ( ctrl->ldctl_value.bv_len ) {
904 rs->sr_text = "manageDSAit control value not empty";
905 return LDAP_PROTOCOL_ERROR;
908 op->o_managedsait = ctrl->ldctl_iscritical
909 ? SLAP_CONTROL_CRITICAL
910 : SLAP_CONTROL_NONCRITICAL;
915 static int parseProxyAuthz (
921 struct berval dn = BER_BVNULL;
923 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
924 rs->sr_text = "proxy authorization control specified multiple times";
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 ( ctrl->ldctl_value.bv_len == 0 ) {
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 );
1000 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1001 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1003 return LDAP_SUCCESS;
1006 static int parseNoOp (
1011 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1012 rs->sr_text = "noop control specified multiple times";
1013 return LDAP_PROTOCOL_ERROR;
1016 if ( ctrl->ldctl_value.bv_len ) {
1017 rs->sr_text = "noop control value not empty";
1018 return LDAP_PROTOCOL_ERROR;
1021 op->o_noop = ctrl->ldctl_iscritical
1022 ? SLAP_CONTROL_CRITICAL
1023 : SLAP_CONTROL_NONCRITICAL;
1025 return LDAP_SUCCESS;
1028 static int parsePagedResults (
1033 int rc = LDAP_SUCCESS;
1037 struct berval cookie = BER_BVNULL;
1038 PagedResultsState *ps;
1040 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1041 rs->sr_text = "paged results control specified multiple times";
1042 return LDAP_PROTOCOL_ERROR;
1045 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1046 rs->sr_text = "paged results control value is empty (or absent)";
1047 return LDAP_PROTOCOL_ERROR;
1050 /* Parse the control value
1051 * realSearchControlValue ::= SEQUENCE {
1052 * size INTEGER (0..maxInt),
1053 * -- requested page size from client
1054 * -- result set size estimate from server
1055 * cookie OCTET STRING
1058 ber = ber_init( &ctrl->ldctl_value );
1059 if ( ber == NULL ) {
1060 rs->sr_text = "internal error";
1064 tag = ber_scanf( ber, "{im}", &size, &cookie );
1066 if ( tag == LBER_ERROR ) {
1067 rs->sr_text = "paged results control could not be decoded";
1068 rc = LDAP_PROTOCOL_ERROR;
1073 rs->sr_text = "paged results control size invalid";
1074 rc = LDAP_PROTOCOL_ERROR;
1078 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1079 *ps = op->o_conn->c_pagedresults_state;
1081 op->o_pagedresults_state = ps;
1083 /* NOTE: according to RFC 2696 3.:
1085 If the page size is greater than or equal to the sizeLimit value, the
1086 server should ignore the control as the request can be satisfied in a
1089 * NOTE: this assumes that the op->ors_slimit be set
1090 * before the controls are parsed.
1093 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1094 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1096 } else if ( ctrl->ldctl_iscritical ) {
1097 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1100 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1104 (void)ber_free( ber, 1 );
1109 static int parseSortedResults (
1114 int rc = LDAP_SUCCESS;
1116 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1117 rs->sr_text = "sorted results control specified multiple times";
1118 return LDAP_PROTOCOL_ERROR;
1121 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1122 rs->sr_text = "sorted results control value is empty (or absent)";
1123 return LDAP_PROTOCOL_ERROR;
1126 /* blow off parsing the value */
1128 op->o_sortedresults = ctrl->ldctl_iscritical
1129 ? SLAP_CONTROL_CRITICAL
1130 : SLAP_CONTROL_NONCRITICAL;
1136 static int parseAssert (
1142 struct berval fstr = BER_BVNULL;
1144 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1145 rs->sr_text = "assert control specified multiple times";
1146 return LDAP_PROTOCOL_ERROR;
1149 if ( ctrl->ldctl_value.bv_len == 0 ) {
1150 rs->sr_text = "assert control value is empty (or absent)";
1151 return LDAP_PROTOCOL_ERROR;
1154 ber = ber_init( &(ctrl->ldctl_value) );
1156 rs->sr_text = "assert control: internal error";
1160 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1162 if( rs->sr_err != LDAP_SUCCESS ) {
1163 if( rs->sr_err == SLAPD_DISCONNECT ) {
1164 rs->sr_err = LDAP_PROTOCOL_ERROR;
1165 send_ldap_disconnect( op, rs );
1166 rs->sr_err = SLAPD_DISCONNECT;
1168 send_ldap_result( op, rs );
1170 if( op->o_assertion != NULL ) {
1171 filter_free_x( op, op->o_assertion );
1177 filter2bv_x( op, op->o_assertion, &fstr );
1179 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1180 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1181 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1184 op->o_assert = ctrl->ldctl_iscritical
1185 ? SLAP_CONTROL_CRITICAL
1186 : SLAP_CONTROL_NONCRITICAL;
1188 rs->sr_err = LDAP_SUCCESS;
1189 return LDAP_SUCCESS;
1192 static int parsePreRead (
1197 ber_len_t siz, off, i;
1198 AttributeName *an = NULL;
1201 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1202 rs->sr_text = "preread control specified multiple times";
1203 return LDAP_PROTOCOL_ERROR;
1206 if ( ctrl->ldctl_value.bv_len == 0 ) {
1207 rs->sr_text = "preread control value is empty (or absent)";
1208 return LDAP_PROTOCOL_ERROR;
1211 if ( op->o_txnSpec ) { /* temporary limitation */
1212 rs->sr_text = "cannot perform pre-read in transaction";
1213 return LDAP_UNWILLING_TO_PERFORM;
1216 ber = ber_init( &(ctrl->ldctl_value) );
1218 rs->sr_text = "preread control: internal error";
1222 siz = sizeof( AttributeName );
1223 off = offsetof( AttributeName, an_name );
1224 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1225 rs->sr_text = "preread control: decoding error";
1226 return LDAP_PROTOCOL_ERROR;
1229 for( i=0; i<siz; i++ ) {
1230 int rc = LDAP_SUCCESS;
1231 const char *dummy = NULL;
1233 an[i].an_desc = NULL;
1235 an[i].an_oc_exclude = 0;
1236 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1237 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1240 : "postread control: unknown attributeType";
1245 op->o_preread = ctrl->ldctl_iscritical
1246 ? SLAP_CONTROL_CRITICAL
1247 : SLAP_CONTROL_NONCRITICAL;
1249 op->o_preread_attrs = an;
1251 rs->sr_err = LDAP_SUCCESS;
1252 return LDAP_SUCCESS;
1255 static int parsePostRead (
1260 ber_len_t siz, off, i;
1261 AttributeName *an = NULL;
1264 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1265 rs->sr_text = "postread control specified multiple times";
1266 return LDAP_PROTOCOL_ERROR;
1269 if ( ctrl->ldctl_value.bv_len == 0 ) {
1270 rs->sr_text = "postread control value is empty (or absent)";
1271 return LDAP_PROTOCOL_ERROR;
1274 if ( op->o_txnSpec ) { /* temporary limitation */
1275 rs->sr_text = "cannot perform post-read in transaction";
1276 return LDAP_UNWILLING_TO_PERFORM;
1279 ber = ber_init( &(ctrl->ldctl_value) );
1281 rs->sr_text = "postread control: internal error";
1285 siz = sizeof( AttributeName );
1286 off = offsetof( AttributeName, an_name );
1287 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1288 rs->sr_text = "postread control: decoding error";
1289 return LDAP_PROTOCOL_ERROR;
1292 for( i=0; i<siz; i++ ) {
1293 int rc = LDAP_SUCCESS;
1294 const char *dummy = NULL;
1296 an[i].an_desc = NULL;
1298 an[i].an_oc_exclude = 0;
1299 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1300 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1303 : "postread control: unknown attributeType";
1308 op->o_postread = ctrl->ldctl_iscritical
1309 ? SLAP_CONTROL_CRITICAL
1310 : SLAP_CONTROL_NONCRITICAL;
1312 op->o_postread_attrs = an;
1314 rs->sr_err = LDAP_SUCCESS;
1315 return LDAP_SUCCESS;
1318 static int parseValuesReturnFilter (
1324 struct berval fstr = BER_BVNULL;
1326 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1327 rs->sr_text = "valuesReturnFilter control specified multiple times";
1328 return LDAP_PROTOCOL_ERROR;
1331 if ( ctrl->ldctl_value.bv_len == 0 ) {
1332 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1333 return LDAP_PROTOCOL_ERROR;
1336 ber = ber_init( &(ctrl->ldctl_value) );
1338 rs->sr_text = "internal error";
1342 rs->sr_err = get_vrFilter( op, ber,
1343 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1345 if( rs->sr_err != LDAP_SUCCESS ) {
1346 if( rs->sr_err == SLAPD_DISCONNECT ) {
1347 rs->sr_err = LDAP_PROTOCOL_ERROR;
1348 send_ldap_disconnect( op, rs );
1349 rs->sr_err = SLAPD_DISCONNECT;
1351 send_ldap_result( op, rs );
1353 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1357 vrFilter2bv( op, op->o_vrFilter, &fstr );
1360 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1361 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1362 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1365 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1366 ? SLAP_CONTROL_CRITICAL
1367 : SLAP_CONTROL_NONCRITICAL;
1369 rs->sr_err = LDAP_SUCCESS;
1370 return LDAP_SUCCESS;
1373 static int parseSubentries (
1378 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1379 rs->sr_text = "subentries control specified multiple times";
1380 return LDAP_PROTOCOL_ERROR;
1383 /* FIXME: should use BER library */
1384 if( ( ctrl->ldctl_value.bv_len != 3 )
1385 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1386 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1388 rs->sr_text = "subentries control value encoding is bogus";
1389 return LDAP_PROTOCOL_ERROR;
1392 op->o_subentries = ctrl->ldctl_iscritical
1393 ? SLAP_CONTROL_CRITICAL
1394 : SLAP_CONTROL_NONCRITICAL;
1396 if (ctrl->ldctl_value.bv_val[2]) {
1397 set_subentries_visibility( op );
1400 return LDAP_SUCCESS;
1403 static int parsePermissiveModify (
1408 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1409 rs->sr_text = "permissiveModify control specified multiple times";
1410 return LDAP_PROTOCOL_ERROR;
1413 if ( ctrl->ldctl_value.bv_len ) {
1414 rs->sr_text = "permissiveModify control value not empty";
1415 return LDAP_PROTOCOL_ERROR;
1418 op->o_permissive_modify = ctrl->ldctl_iscritical
1419 ? SLAP_CONTROL_CRITICAL
1420 : SLAP_CONTROL_NONCRITICAL;
1422 return LDAP_SUCCESS;
1425 static int parseDomainScope (
1430 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1431 rs->sr_text = "domainScope control specified multiple times";
1432 return LDAP_PROTOCOL_ERROR;
1435 if ( ctrl->ldctl_value.bv_len ) {
1436 rs->sr_text = "domainScope control value not empty";
1437 return LDAP_PROTOCOL_ERROR;
1440 op->o_domain_scope = ctrl->ldctl_iscritical
1441 ? SLAP_CONTROL_CRITICAL
1442 : SLAP_CONTROL_NONCRITICAL;
1444 return LDAP_SUCCESS;
1447 #ifdef SLAP_CONTROL_X_TREE_DELETE
1448 static int parseTreeDelete (
1453 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1454 rs->sr_text = "treeDelete control specified multiple times";
1455 return LDAP_PROTOCOL_ERROR;
1458 if ( ctrl->ldctl_value.bv_len ) {
1459 rs->sr_text = "treeDelete control value not empty";
1460 return LDAP_PROTOCOL_ERROR;
1463 op->o_tree_delete = ctrl->ldctl_iscritical
1464 ? SLAP_CONTROL_CRITICAL
1465 : SLAP_CONTROL_NONCRITICAL;
1467 return LDAP_SUCCESS;
1471 static int parseSearchOptions (
1477 ber_int_t search_flags;
1480 if ( ctrl->ldctl_value.bv_len == 0 ) {
1481 rs->sr_text = "searchOptions control value is empty (or absent)";
1482 return LDAP_PROTOCOL_ERROR;
1485 ber = ber_init( &ctrl->ldctl_value );
1487 rs->sr_text = "internal error";
1491 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1492 rs->sr_text = "searchOptions control decoding error";
1493 return LDAP_PROTOCOL_ERROR;
1496 (void) ber_free( ber, 1 );
1498 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1499 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1500 rs->sr_text = "searchOptions control specified multiple times "
1501 "or with domainScope control";
1502 return LDAP_PROTOCOL_ERROR;
1505 op->o_domain_scope = ctrl->ldctl_iscritical
1506 ? SLAP_CONTROL_CRITICAL
1507 : SLAP_CONTROL_NONCRITICAL;
1510 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1511 /* Other search flags not recognised so far,
1513 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1515 rs->sr_text = "searchOptions contained unrecognized flag";
1516 return LDAP_UNWILLING_TO_PERFORM;
1519 return LDAP_SUCCESS;