2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 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>
27 #include "../../libraries/liblber/lber-int.h"
29 static SLAP_CTRL_PARSE_FN parseAssert;
30 static SLAP_CTRL_PARSE_FN parseDomainScope;
31 static SLAP_CTRL_PARSE_FN parseDontUseCopy;
32 static SLAP_CTRL_PARSE_FN parseManageDSAit;
33 static SLAP_CTRL_PARSE_FN parseNoOp;
34 static SLAP_CTRL_PARSE_FN parsePagedResults;
35 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
36 static SLAP_CTRL_PARSE_FN parsePreRead, parsePostRead;
37 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
38 static SLAP_CTRL_PARSE_FN parseRelax;
39 static SLAP_CTRL_PARSE_FN parseSearchOptions;
40 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
41 static SLAP_CTRL_PARSE_FN parseSortedResults;
43 static SLAP_CTRL_PARSE_FN parseSubentries;
44 #ifdef SLAP_CONTROL_X_TREE_DELETE
45 static SLAP_CTRL_PARSE_FN parseTreeDelete;
47 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
48 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
49 static SLAP_CTRL_PARSE_FN parseSessionTracking;
51 #ifdef SLAP_CONTROL_X_WHATFAILED
52 static SLAP_CTRL_PARSE_FN parseWhatFailed;
54 #ifdef SLAP_CONTROL_X_LAZY_COMMIT
55 static SLAP_CTRL_PARSE_FN parseLazyCommit;
58 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
60 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
61 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
63 struct slap_control_ids slap_cids;
69 /* The controlID for this control */
72 /* Operations supported by control */
75 /* Extended operations supported by control */
76 char **sc_extendedops; /* input */
77 BerVarray sc_extendedopsbv; /* run-time use */
79 /* Control parsing callback */
80 SLAP_CTRL_PARSE_FN *sc_parse;
82 LDAP_SLIST_ENTRY(slap_control) sc_next;
85 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
86 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
89 * all known request control OIDs should be added to this list
92 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
93 * addresses an undefined control; this allows to safely test for
94 * well known controls even if they are not registered, e.g. if
95 * they get moved to modules. An example is sc_LDAPsync, which
96 * is implemented in the syncprov overlay and thus, if configured
97 * as dynamic module, may not be registered. One side effect is that
98 * slap_known_controls[0] == NULL, so it should always be used
100 * FIXME: should we define the "undefined control" oid?
102 char *slap_known_controls[SLAP_MAX_CIDS+1];
103 static int num_known_controls = 1;
105 static char *proxy_authz_extops[] = {
106 LDAP_EXOP_MODIFY_PASSWD,
112 static char *manageDSAit_extops[] = {
117 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
118 static char *session_tracking_extops[] = {
119 LDAP_EXOP_MODIFY_PASSWD,
126 static struct slap_control control_defs[] = {
127 { LDAP_CONTROL_ASSERT,
128 (int)offsetof(struct slap_control_ids, sc_assert),
129 SLAP_CTRL_UPDATE|SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
131 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
132 { LDAP_CONTROL_PRE_READ,
133 (int)offsetof(struct slap_control_ids, sc_preRead),
134 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
136 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
137 { LDAP_CONTROL_POST_READ,
138 (int)offsetof(struct slap_control_ids, sc_postRead),
139 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
141 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
142 { LDAP_CONTROL_VALUESRETURNFILTER,
143 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
144 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
146 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
147 { LDAP_CONTROL_PAGEDRESULTS,
148 (int)offsetof(struct slap_control_ids, sc_pagedResults),
151 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
152 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
153 { LDAP_CONTROL_SORTREQUEST,
154 (int)offsetof(struct slap_control_ids, sc_sortedResults),
155 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
157 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
159 { LDAP_CONTROL_X_DOMAIN_SCOPE,
160 (int)offsetof(struct slap_control_ids, sc_domainScope),
161 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
163 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
164 { LDAP_CONTROL_DONTUSECOPY,
165 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
166 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE,
168 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
169 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
170 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
171 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
173 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
174 #ifdef SLAP_CONTROL_X_TREE_DELETE
175 { LDAP_CONTROL_X_TREE_DELETE,
176 (int)offsetof(struct slap_control_ids, sc_treeDelete),
177 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
179 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
181 { LDAP_CONTROL_X_SEARCH_OPTIONS,
182 (int)offsetof(struct slap_control_ids, sc_searchOptions),
183 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
185 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
186 { LDAP_CONTROL_SUBENTRIES,
187 (int)offsetof(struct slap_control_ids, sc_subentries),
190 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
192 (int)offsetof(struct slap_control_ids, sc_noOp),
193 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
195 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
196 { LDAP_CONTROL_RELAX,
197 (int)offsetof(struct slap_control_ids, sc_relax),
198 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
200 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
202 { LDAP_CONTROL_X_TXN_SPEC,
203 (int)offsetof(struct slap_control_ids, sc_txnSpec),
204 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
206 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
208 { LDAP_CONTROL_MANAGEDSAIT,
209 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
211 manageDSAit_extops, NULL,
212 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
213 { LDAP_CONTROL_PROXY_AUTHZ,
214 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
215 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
216 proxy_authz_extops, NULL,
217 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
218 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
219 { LDAP_CONTROL_X_SESSION_TRACKING,
220 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
221 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
222 session_tracking_extops, NULL,
223 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
225 #ifdef SLAP_CONTROL_X_WHATFAILED
226 { LDAP_CONTROL_X_WHATFAILED,
227 (int)offsetof(struct slap_control_ids, sc_whatFailed),
228 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
230 parseWhatFailed, LDAP_SLIST_ENTRY_INITIALIZER(next) },
232 #ifdef SLAP_CONTROL_X_LAZY_COMMIT
233 { LDAP_CONTROL_X_LAZY_COMMIT,
234 (int)offsetof(struct slap_control_ids, sc_lazyCommit),
235 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
237 parseLazyCommit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
240 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
243 static struct slap_control *
244 find_ctrl( const char *oid );
247 * Register a supported control.
249 * This can be called by an OpenLDAP plugin or, indirectly, by a
250 * SLAPI plugin calling slapi_register_supported_control().
252 * NOTE: if flags == 1 the control is replaced if already registered;
253 * otherwise registering an already registered control is not allowed.
256 register_supported_control2(const char *controloid,
257 slap_mask_t controlmask,
259 SLAP_CTRL_PARSE_FN *controlparsefn,
263 struct slap_control *sc = NULL;
265 BerVarray extendedopsbv = NULL;
267 if ( num_known_controls >= SLAP_MAX_CIDS ) {
268 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
269 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
270 SLAP_MAX_CIDS, 0, 0 );
274 if ( controloid == NULL ) {
275 return LDAP_PARAM_ERROR;
278 /* check if already registered */
279 for ( i = 0; slap_known_controls[ i ]; i++ ) {
280 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
282 Debug( LDAP_DEBUG_TRACE,
283 "Control %s already registered; replacing.\n",
285 /* (find and) replace existing handler */
286 sc = find_ctrl( controloid );
287 assert( sc != NULL );
291 Debug( LDAP_DEBUG_ANY,
292 "Control %s already registered.\n",
294 return LDAP_PARAM_ERROR;
298 /* turn compatible extended operations into bervals */
299 if ( controlexops != NULL ) {
302 for ( i = 0; controlexops[ i ]; i++ );
304 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
305 if ( extendedopsbv == NULL ) {
306 return LDAP_NO_MEMORY;
309 for ( i = 0; controlexops[ i ]; i++ ) {
310 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
315 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
317 ber_bvarray_free( extendedopsbv );
318 return LDAP_NO_MEMORY;
321 sc->sc_oid = ch_strdup( controloid );
322 sc->sc_cid = num_known_controls;
324 /* Update slap_known_controls, too. */
325 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
326 slap_known_controls[num_known_controls++] = NULL;
328 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
329 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
332 if ( sc->sc_extendedopsbv ) {
333 /* FIXME: in principle, we should rather merge
334 * existing extops with those supported by the
335 * new control handling implementation.
336 * In fact, whether a control is compatible with
337 * an extop should not be a matter of implementation.
338 * We likely also need a means for a newly
339 * registered extop to declare that it is
340 * comptible with an already registered control.
342 ber_bvarray_free( sc->sc_extendedopsbv );
343 sc->sc_extendedopsbv = NULL;
344 sc->sc_extendedops = NULL;
348 sc->sc_extendedopsbv = extendedopsbv;
349 sc->sc_mask = controlmask;
350 sc->sc_parse = controlparsefn;
352 *controlcid = sc->sc_cid;
358 #ifdef SLAP_CONFIG_DELETE
360 unregister_supported_control( const char *controloid )
362 struct slap_control *sc;
365 if ( controloid == NULL || (sc = find_ctrl( controloid )) == NULL ){
369 for ( i = 0; slap_known_controls[ i ]; i++ ) {
370 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
372 slap_known_controls[ i ] = slap_known_controls[ i+1 ];
373 } while ( slap_known_controls[ i++ ] );
374 num_known_controls--;
379 LDAP_SLIST_REMOVE(&controls_list, sc, slap_control, sc_next);
380 ch_free( sc->sc_oid );
381 if ( sc->sc_extendedopsbv != NULL ) {
382 ber_bvarray_free( sc->sc_extendedopsbv );
388 #endif /* SLAP_CONFIG_DELETE */
391 * One-time initialization of internal controls.
394 slap_controls_init( void )
400 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
401 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
402 rc = register_supported_control( control_defs[i].sc_oid,
403 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
404 control_defs[i].sc_parse, cid );
405 if ( rc != LDAP_SUCCESS ) break;
412 * Free memory associated with list of supported controls.
415 controls_destroy( void )
417 struct slap_control *sc;
419 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
420 sc = LDAP_SLIST_FIRST(&controls_list);
421 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
423 ch_free( sc->sc_oid );
424 if ( sc->sc_extendedopsbv != NULL ) {
425 ber_bvarray_free( sc->sc_extendedopsbv );
432 * Format the supportedControl attribute of the root DSE,
433 * detailing which controls are supported by the directory
437 controls_root_dse_info( Entry *e )
439 AttributeDescription *ad_supportedControl
440 = slap_schema.si_ad_supportedControl;
441 struct berval vals[2];
442 struct slap_control *sc;
444 vals[1].bv_val = NULL;
447 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
448 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
450 vals[0].bv_val = sc->sc_oid;
451 vals[0].bv_len = strlen( sc->sc_oid );
453 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
462 * Return a list of OIDs and operation masks for supported
463 * controls. Used by SLAPI.
466 get_supported_controls(char ***ctrloidsp,
467 slap_mask_t **ctrlmasks)
472 struct slap_control *sc;
476 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
486 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
487 if ( oids == NULL ) {
488 return LDAP_NO_MEMORY;
490 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
491 if ( masks == NULL ) {
493 return LDAP_NO_MEMORY;
498 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
499 oids[n] = ch_strdup( sc->sc_oid );
500 masks[n] = sc->sc_mask;
513 * Find a control given its OID.
515 static struct slap_control *
516 find_ctrl( const char *oid )
518 struct slap_control *sc;
520 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
521 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
530 slap_find_control_id(
534 struct slap_control *ctrl = find_ctrl( oid );
536 if ( cid ) *cid = ctrl->sc_cid;
539 return LDAP_CONTROL_NOT_FOUND;
543 slap_global_control( Operation *op, const char *oid, int *cid )
545 struct slap_control *ctrl = find_ctrl( oid );
547 if ( ctrl == NULL ) {
548 /* should not be reachable */
549 Debug( LDAP_DEBUG_ANY,
550 "slap_global_control: unrecognized control: %s\n",
552 return LDAP_CONTROL_NOT_FOUND;
555 if ( cid ) *cid = ctrl->sc_cid;
557 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
558 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
559 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
561 return LDAP_COMPARE_TRUE;
565 Debug( LDAP_DEBUG_TRACE,
566 "slap_global_control: unavailable control: %s\n",
570 return LDAP_COMPARE_FALSE;
573 void slap_free_ctrls(
575 LDAPControl **ctrls )
579 if( ctrls == op->o_ctrls ) {
580 if( op->o_assertion != NULL ) {
581 filter_free_x( op, op->o_assertion, 1 );
582 op->o_assertion = NULL;
584 if( op->o_vrFilter != NULL) {
585 vrFilter_free( op, op->o_vrFilter );
586 op->o_vrFilter = NULL;
588 if( op->o_preread_attrs != NULL ) {
589 op->o_tmpfree( op->o_preread_attrs, op->o_tmpmemctx );
590 op->o_preread_attrs = NULL;
592 if( op->o_postread_attrs != NULL ) {
593 op->o_tmpfree( op->o_postread_attrs, op->o_tmpmemctx );
594 op->o_postread_attrs = NULL;
596 if( op->o_pagedresults_state != NULL ) {
597 op->o_tmpfree( op->o_pagedresults_state, op->o_tmpmemctx );
598 op->o_pagedresults_state = NULL;
602 for (i=0; ctrls[i]; i++) {
603 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
605 op->o_tmpfree( ctrls, op->o_tmpmemctx );
611 LDAPControl **ctrls )
614 LDAPControl **ctrlsp;
616 if ( rs->sr_ctrls ) {
617 for ( ; rs->sr_ctrls[ i ]; i++ ) ;
620 for ( j=0; ctrls[j]; j++ ) ;
622 ctrlsp = op->o_tmpalloc(( i+j+1 )*sizeof(LDAPControl *), op->o_tmpmemctx );
624 if ( rs->sr_ctrls ) {
625 for ( ; rs->sr_ctrls[i]; i++ )
626 ctrlsp[i] = rs->sr_ctrls[i];
628 for ( j=0; ctrls[j]; j++)
629 ctrlsp[i++] = ctrls[j];
632 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED )
633 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
634 rs->sr_ctrls = ctrlsp;
635 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
642 LDAPControl *control,
645 struct slap_control *sc;
646 int rc = LDAP_SUCCESS;
648 sc = find_ctrl( control->ldctl_oid );
650 /* recognized control */
652 switch( op->o_tag ) {
654 tagmask = SLAP_CTRL_ADD;
657 tagmask = SLAP_CTRL_BIND;
659 case LDAP_REQ_COMPARE:
660 tagmask = SLAP_CTRL_COMPARE;
662 case LDAP_REQ_DELETE:
663 tagmask = SLAP_CTRL_DELETE;
665 case LDAP_REQ_MODIFY:
666 tagmask = SLAP_CTRL_MODIFY;
668 case LDAP_REQ_RENAME:
669 tagmask = SLAP_CTRL_RENAME;
671 case LDAP_REQ_SEARCH:
672 tagmask = SLAP_CTRL_SEARCH;
674 case LDAP_REQ_UNBIND:
675 tagmask = SLAP_CTRL_UNBIND;
677 case LDAP_REQ_ABANDON:
678 tagmask = SLAP_CTRL_ABANDON;
680 case LDAP_REQ_EXTENDED:
682 assert( op->ore_reqoid.bv_val != NULL );
683 if( sc->sc_extendedopsbv != NULL ) {
685 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
686 if( bvmatch( &op->ore_reqoid,
687 &sc->sc_extendedopsbv[i] ) )
696 *text = "controls internal error";
700 if (( sc->sc_mask & tagmask ) == tagmask ) {
701 /* available extension */
702 if ( sc->sc_parse ) {
703 rc = sc->sc_parse( op, rs, control );
704 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
706 } else if ( control->ldctl_iscritical ) {
707 *text = "not yet implemented";
712 } else if ( control->ldctl_iscritical ) {
713 /* unavailable CRITICAL control */
714 *text = "critical extension is unavailable";
715 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
718 } else if ( control->ldctl_iscritical ) {
719 /* unrecognized CRITICAL control */
720 *text = "critical extension is not recognized";
721 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
733 return get_ctrls2( op, rs, sendres, LDAP_TAG_CONTROLS );
747 BerElement *ber = op->o_ber;
749 #ifdef SLAP_CONTROL_X_WHATFAILED
750 /* NOTE: right now, slapd checks the validity of each control
751 * while parsing. As a consequence, it can only detect one
752 * cause of failure at a time. This results in returning
753 * exactly one OID with the whatFailed control, or no control
756 char *failed_oid = NULL;
759 len = ber_pvt_ber_remaining(ber);
763 rs->sr_err = LDAP_SUCCESS;
767 if(( tag = ber_peek_tag( ber, &len )) != ctag ) {
768 if( tag == LBER_ERROR ) {
769 rs->sr_err = SLAPD_DISCONNECT;
770 rs->sr_text = "unexpected data in PDU";
776 Debug( LDAP_DEBUG_TRACE,
777 "=> get_ctrls\n", 0, 0, 0 );
779 if( op->o_protocol < LDAP_VERSION3 ) {
780 rs->sr_err = SLAPD_DISCONNECT;
781 rs->sr_text = "controls require LDAPv3";
785 /* one for first control, one for termination */
786 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
789 if( op->ctrls == NULL ) {
790 rs->sr_err = LDAP_NO_MEMORY;
791 rs->sr_text = "no memory";
796 op->o_ctrls[nctrls] = NULL;
798 /* step through each element */
799 for( tag = ber_first_element( ber, &len, &opaque );
801 tag = ber_next_element( ber, &len, opaque ) )
804 LDAPControl **tctrls;
806 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
807 memset(c, 0, sizeof(LDAPControl));
809 /* allocate pointer space for current controls (nctrls)
810 * + this control + extra NULL
812 tctrls = op->o_tmprealloc( op->o_ctrls,
813 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
816 if( tctrls == NULL ) {
818 ldap_controls_free(op->o_ctrls);
821 rs->sr_err = LDAP_NO_MEMORY;
822 rs->sr_text = "no memory";
826 op->o_ctrls = tctrls;
828 op->o_ctrls[nctrls++] = c;
829 op->o_ctrls[nctrls] = NULL;
831 tag = ber_scanf( ber, "{m" /*}*/, &bv );
832 c->ldctl_oid = bv.bv_val;
834 if( tag == LBER_ERROR ) {
835 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
838 slap_free_ctrls( op, op->o_ctrls );
840 rs->sr_err = SLAPD_DISCONNECT;
841 rs->sr_text = "decoding controls error";
844 } else if( c->ldctl_oid == NULL ) {
845 Debug( LDAP_DEBUG_TRACE,
846 "get_ctrls: conn %lu got emtpy OID.\n",
847 op->o_connid, 0, 0 );
849 slap_free_ctrls( op, op->o_ctrls );
851 rs->sr_err = LDAP_PROTOCOL_ERROR;
852 rs->sr_text = "OID field is empty";
856 tag = ber_peek_tag( ber, &len );
858 if( tag == LBER_BOOLEAN ) {
860 tag = ber_scanf( ber, "b", &crit );
862 if( tag == LBER_ERROR ) {
863 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
865 slap_free_ctrls( op, op->o_ctrls );
867 rs->sr_err = SLAPD_DISCONNECT;
868 rs->sr_text = "decoding controls error";
872 c->ldctl_iscritical = (crit != 0);
873 tag = ber_peek_tag( ber, &len );
876 if( tag == LBER_OCTETSTRING ) {
877 tag = ber_scanf( ber, "m", &c->ldctl_value );
879 if( tag == LBER_ERROR ) {
880 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
881 "%s (%scritical): get value failed.\n",
882 op->o_connid, c->ldctl_oid,
883 c->ldctl_iscritical ? "" : "non" );
884 slap_free_ctrls( op, op->o_ctrls );
886 rs->sr_err = SLAPD_DISCONNECT;
887 rs->sr_text = "decoding controls error";
892 Debug( LDAP_DEBUG_TRACE,
893 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
894 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
896 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
897 if ( rs->sr_err != LDAP_SUCCESS ) {
898 #ifdef SLAP_CONTROL_X_WHATFAILED
899 failed_oid = c->ldctl_oid;
906 Debug( LDAP_DEBUG_TRACE,
907 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
908 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
910 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
911 if( rs->sr_err == SLAPD_DISCONNECT ) {
912 rs->sr_err = LDAP_PROTOCOL_ERROR;
913 send_ldap_disconnect( op, rs );
914 rs->sr_err = SLAPD_DISCONNECT;
916 #ifdef SLAP_CONTROL_X_WHATFAILED
917 /* might have not been parsed yet? */
918 if ( failed_oid != NULL ) {
919 if ( !get_whatFailed( op ) ) {
922 /* step through each remaining element */
923 for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
925 LDAPControl c = { 0 };
927 tag = ber_scanf( ber, "{m" /*}*/, &bv );
928 c.ldctl_oid = bv.bv_val;
930 if ( tag == LBER_ERROR ) {
931 slap_free_ctrls( op, op->o_ctrls );
935 } else if ( c.ldctl_oid == NULL ) {
936 slap_free_ctrls( op, op->o_ctrls );
941 tag = ber_peek_tag( ber, &len );
942 if ( tag == LBER_BOOLEAN ) {
944 tag = ber_scanf( ber, "b", &crit );
945 if( tag == LBER_ERROR ) {
946 slap_free_ctrls( op, op->o_ctrls );
951 tag = ber_peek_tag( ber, &len );
954 if ( tag == LBER_OCTETSTRING ) {
955 tag = ber_scanf( ber, "m", &c.ldctl_value );
957 if( tag == LBER_ERROR ) {
958 slap_free_ctrls( op, op->o_ctrls );
964 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
966 slap_parse_ctrl( op, rs, &c, &text );
972 if ( get_whatFailed( op ) ) {
974 oids[ 0 ] = failed_oid;
976 slap_ctrl_whatFailed_add( op, rs, oids );
981 send_ldap_result( op, rs );
993 BI_chk_controls fnc )
997 switch ( op->o_ctrlflag[ ctrl ] ) {
998 case SLAP_CONTROL_NONCRITICAL:
999 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
1000 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
1001 slap_known_controls[ ctrl - 1 ] ) == 0 )
1008 rs->sr_err = LDAP_OTHER;
1013 (void)fnc( op, rs );
1016 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
1019 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
1020 ( i - j ) * sizeof( LDAPControl * ) );
1023 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
1027 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
1029 Debug( LDAP_DEBUG_ANY, "%s: "
1030 "non-critical control \"%s\" not supported; stripped.\n",
1031 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1034 case SLAP_CONTROL_IGNORED:
1035 case SLAP_CONTROL_NONE:
1036 rs->sr_err = SLAP_CB_CONTINUE;
1039 case SLAP_CONTROL_CRITICAL:
1040 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
1042 (void)fnc( op, rs );
1044 Debug( LDAP_DEBUG_ANY, "%s: "
1045 "critical control \"%s\" not supported.\n",
1046 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1050 /* handle all cases! */
1057 static int parseDontUseCopy (
1062 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
1063 rs->sr_text = "dontUseCopy control specified multiple times";
1064 return LDAP_PROTOCOL_ERROR;
1067 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1068 rs->sr_text = "dontUseCopy control value not absent";
1069 return LDAP_PROTOCOL_ERROR;
1072 if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
1073 && !ctrl->ldctl_iscritical )
1075 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
1076 return LDAP_PROTOCOL_ERROR;
1079 op->o_dontUseCopy = ctrl->ldctl_iscritical
1080 ? SLAP_CONTROL_CRITICAL
1081 : SLAP_CONTROL_NONCRITICAL;
1083 return LDAP_SUCCESS;
1086 static int parseRelax (
1091 if ( op->o_relax != SLAP_CONTROL_NONE ) {
1092 rs->sr_text = "relax control specified multiple times";
1093 return LDAP_PROTOCOL_ERROR;
1096 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1097 rs->sr_text = "relax control value not absent";
1098 return LDAP_PROTOCOL_ERROR;
1101 op->o_relax = ctrl->ldctl_iscritical
1102 ? SLAP_CONTROL_CRITICAL
1103 : SLAP_CONTROL_NONCRITICAL;
1105 return LDAP_SUCCESS;
1108 static int parseManageDSAit (
1113 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1114 rs->sr_text = "manageDSAit control specified multiple times";
1115 return LDAP_PROTOCOL_ERROR;
1118 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1119 rs->sr_text = "manageDSAit control value not absent";
1120 return LDAP_PROTOCOL_ERROR;
1123 op->o_managedsait = ctrl->ldctl_iscritical
1124 ? SLAP_CONTROL_CRITICAL
1125 : SLAP_CONTROL_NONCRITICAL;
1127 return LDAP_SUCCESS;
1130 static int parseProxyAuthz (
1136 struct berval dn = BER_BVNULL;
1138 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1139 rs->sr_text = "proxy authorization control specified multiple times";
1140 return LDAP_PROTOCOL_ERROR;
1143 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1144 rs->sr_text = "proxy authorization control value absent";
1145 return LDAP_PROTOCOL_ERROR;
1148 if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1149 && !ctrl->ldctl_iscritical )
1151 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1152 return LDAP_PROTOCOL_ERROR;
1155 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1156 && BER_BVISEMPTY( &op->o_ndn ) )
1158 rs->sr_text = "anonymous proxied authorization not allowed";
1159 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1162 op->o_proxy_authz = ctrl->ldctl_iscritical
1163 ? SLAP_CONTROL_CRITICAL
1164 : SLAP_CONTROL_NONCRITICAL;
1166 Debug( LDAP_DEBUG_ARGS,
1167 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
1169 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
1172 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1173 Debug( LDAP_DEBUG_TRACE,
1174 "parseProxyAuthz: conn=%lu anonymous\n",
1175 op->o_connid, 0, 0 );
1178 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1179 op->o_ndn.bv_val[ 0 ] = '\0';
1181 op->o_ndn.bv_len = 0;
1183 if ( !BER_BVISNULL( &op->o_dn ) ) {
1184 op->o_dn.bv_val[ 0 ] = '\0';
1186 op->o_dn.bv_len = 0;
1188 return LDAP_SUCCESS;
1191 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1192 NULL, &dn, SLAP_GETDN_AUTHZID );
1194 /* FIXME: empty DN in proxyAuthz control should be legal... */
1195 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1197 ch_free( dn.bv_val );
1199 rs->sr_text = "authzId mapping failed";
1200 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1203 Debug( LDAP_DEBUG_TRACE,
1204 "parseProxyAuthz: conn=%lu \"%s\"\n",
1206 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1208 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1211 ch_free( dn.bv_val );
1212 rs->sr_text = "not authorized to assume identity";
1213 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1216 ch_free( op->o_ndn.bv_val );
1219 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1220 * from now on op->o_dn is normalized
1223 ber_bvreplace( &op->o_dn, &dn );
1225 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1226 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1228 return LDAP_SUCCESS;
1231 static int parseNoOp (
1236 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1237 rs->sr_text = "noop control specified multiple times";
1238 return LDAP_PROTOCOL_ERROR;
1241 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1242 rs->sr_text = "noop control value not empty";
1243 return LDAP_PROTOCOL_ERROR;
1246 op->o_noop = ctrl->ldctl_iscritical
1247 ? SLAP_CONTROL_CRITICAL
1248 : SLAP_CONTROL_NONCRITICAL;
1250 return LDAP_SUCCESS;
1253 static int parsePagedResults (
1258 BerElementBuffer berbuf;
1259 BerElement *ber = (BerElement *)&berbuf;
1260 struct berval cookie;
1261 PagedResultsState *ps;
1262 int rc = LDAP_SUCCESS;
1266 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1267 rs->sr_text = "paged results control specified multiple times";
1268 return LDAP_PROTOCOL_ERROR;
1271 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1272 rs->sr_text = "paged results control value is absent";
1273 return LDAP_PROTOCOL_ERROR;
1276 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1277 rs->sr_text = "paged results control value is empty";
1278 return LDAP_PROTOCOL_ERROR;
1281 /* Parse the control value
1282 * realSearchControlValue ::= SEQUENCE {
1283 * size INTEGER (0..maxInt),
1284 * -- requested page size from client
1285 * -- result set size estimate from server
1286 * cookie OCTET STRING
1289 ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1291 tag = ber_scanf( ber, "{im}", &size, &cookie );
1293 if ( tag == LBER_ERROR ) {
1294 rs->sr_text = "paged results control could not be decoded";
1295 rc = LDAP_PROTOCOL_ERROR;
1300 rs->sr_text = "paged results control size invalid";
1301 rc = LDAP_PROTOCOL_ERROR;
1305 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1306 *ps = op->o_conn->c_pagedresults_state;
1308 ps->ps_cookieval = cookie;
1309 op->o_pagedresults_state = ps;
1310 if ( !cookie.bv_len ) {
1313 /* taint ps_cookie, to detect whether it's set */
1314 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1317 /* NOTE: according to RFC 2696 3.:
1319 If the page size is greater than or equal to the sizeLimit value, the
1320 server should ignore the control as the request can be satisfied in a
1323 * NOTE: this assumes that the op->ors_slimit be set
1324 * before the controls are parsed.
1327 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1328 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1330 } else if ( ctrl->ldctl_iscritical ) {
1331 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1334 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1341 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1342 static int parseSortedResults (
1347 int rc = LDAP_SUCCESS;
1349 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1350 rs->sr_text = "sorted results control specified multiple times";
1351 return LDAP_PROTOCOL_ERROR;
1354 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1355 rs->sr_text = "sorted results control value is absent";
1356 return LDAP_PROTOCOL_ERROR;
1359 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1360 rs->sr_text = "sorted results control value is empty";
1361 return LDAP_PROTOCOL_ERROR;
1364 /* blow off parsing the value */
1366 op->o_sortedresults = ctrl->ldctl_iscritical
1367 ? SLAP_CONTROL_CRITICAL
1368 : SLAP_CONTROL_NONCRITICAL;
1374 static int parseAssert (
1380 struct berval fstr = BER_BVNULL;
1382 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1383 rs->sr_text = "assert control specified multiple times";
1384 return LDAP_PROTOCOL_ERROR;
1387 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1388 rs->sr_text = "assert control value is absent";
1389 return LDAP_PROTOCOL_ERROR;
1392 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1393 rs->sr_text = "assert control value is empty";
1394 return LDAP_PROTOCOL_ERROR;
1397 ber = ber_init( &(ctrl->ldctl_value) );
1399 rs->sr_text = "assert control: internal error";
1403 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1405 (void) ber_free( ber, 1 );
1406 if( rs->sr_err != LDAP_SUCCESS ) {
1407 if( rs->sr_err == SLAPD_DISCONNECT ) {
1408 rs->sr_err = LDAP_PROTOCOL_ERROR;
1409 send_ldap_disconnect( op, rs );
1410 rs->sr_err = SLAPD_DISCONNECT;
1412 send_ldap_result( op, rs );
1414 if( op->o_assertion != NULL ) {
1415 filter_free_x( op, op->o_assertion, 1 );
1421 filter2bv_x( op, op->o_assertion, &fstr );
1423 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1424 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1425 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1428 op->o_assert = ctrl->ldctl_iscritical
1429 ? SLAP_CONTROL_CRITICAL
1430 : SLAP_CONTROL_NONCRITICAL;
1432 rs->sr_err = LDAP_SUCCESS;
1433 return LDAP_SUCCESS;
1436 #define READMSG(post, msg) \
1437 ( post ? "postread control: " msg : "preread control: " msg )
1446 ber_len_t siz, off, i;
1448 AttributeName *an = NULL;
1450 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1451 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1453 rs->sr_text = READMSG( post, "specified multiple times" );
1454 return LDAP_PROTOCOL_ERROR;
1457 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1458 rs->sr_text = READMSG( post, "value is absent" );
1459 return LDAP_PROTOCOL_ERROR;
1462 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1463 rs->sr_text = READMSG( post, "value is empty" );
1464 return LDAP_PROTOCOL_ERROR;
1468 if ( op->o_txnSpec ) { /* temporary limitation */
1469 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1470 return LDAP_UNWILLING_TO_PERFORM;
1474 ber = ber_init( &ctrl->ldctl_value );
1475 if ( ber == NULL ) {
1476 rs->sr_text = READMSG( post, "internal error" );
1480 rs->sr_err = LDAP_SUCCESS;
1481 siz = sizeof( AttributeName );
1482 off = offsetof( AttributeName, an_name );
1483 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1484 rs->sr_text = READMSG( post, "decoding error" );
1485 rs->sr_err = LDAP_PROTOCOL_ERROR;
1489 for ( i = 0; i < siz; i++ ) {
1490 const char *dummy = NULL;
1493 an[i].an_desc = NULL;
1496 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1497 if ( rc == LDAP_SUCCESS ) {
1498 an[i].an_name = an[i].an_desc->ad_cname;
1502 static struct berval special_attrs[] = {
1503 BER_BVC( LDAP_NO_ATTRS ),
1504 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1505 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1509 /* deal with special attribute types */
1510 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1511 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1512 an[i].an_name = special_attrs[ j ];
1517 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1519 rs->sr_text = dummy ? dummy
1520 : READMSG( post, "unknown attributeType" );
1527 op->o_postread_attrs = an;
1528 op->o_postread = ctrl->ldctl_iscritical
1529 ? SLAP_CONTROL_CRITICAL
1530 : SLAP_CONTROL_NONCRITICAL;
1532 op->o_preread_attrs = an;
1533 op->o_preread = ctrl->ldctl_iscritical
1534 ? SLAP_CONTROL_CRITICAL
1535 : SLAP_CONTROL_NONCRITICAL;
1539 (void) ber_free( ber, 1 );
1543 static int parsePreRead (
1548 return parseReadAttrs( op, rs, ctrl, 0 );
1551 static int parsePostRead (
1556 return parseReadAttrs( op, rs, ctrl, 1 );
1559 static int parseValuesReturnFilter (
1565 struct berval fstr = BER_BVNULL;
1567 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1568 rs->sr_text = "valuesReturnFilter control specified multiple times";
1569 return LDAP_PROTOCOL_ERROR;
1572 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1573 rs->sr_text = "valuesReturnFilter control value is absent";
1574 return LDAP_PROTOCOL_ERROR;
1577 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1578 rs->sr_text = "valuesReturnFilter control value is empty";
1579 return LDAP_PROTOCOL_ERROR;
1582 ber = ber_init( &(ctrl->ldctl_value) );
1584 rs->sr_text = "internal error";
1588 rs->sr_err = get_vrFilter( op, ber,
1589 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1591 (void) ber_free( ber, 1 );
1593 if( rs->sr_err != LDAP_SUCCESS ) {
1594 if( rs->sr_err == SLAPD_DISCONNECT ) {
1595 rs->sr_err = LDAP_PROTOCOL_ERROR;
1596 send_ldap_disconnect( op, rs );
1597 rs->sr_err = SLAPD_DISCONNECT;
1599 send_ldap_result( op, rs );
1601 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1605 vrFilter2bv( op, op->o_vrFilter, &fstr );
1608 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1609 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1610 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1613 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1614 ? SLAP_CONTROL_CRITICAL
1615 : SLAP_CONTROL_NONCRITICAL;
1617 rs->sr_err = LDAP_SUCCESS;
1618 return LDAP_SUCCESS;
1621 static int parseSubentries (
1626 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1627 rs->sr_text = "subentries control specified multiple times";
1628 return LDAP_PROTOCOL_ERROR;
1631 /* FIXME: should use BER library */
1632 if( ( ctrl->ldctl_value.bv_len != 3 )
1633 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1634 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1636 rs->sr_text = "subentries control value encoding is bogus";
1637 return LDAP_PROTOCOL_ERROR;
1640 op->o_subentries = ctrl->ldctl_iscritical
1641 ? SLAP_CONTROL_CRITICAL
1642 : SLAP_CONTROL_NONCRITICAL;
1644 if (ctrl->ldctl_value.bv_val[2]) {
1645 set_subentries_visibility( op );
1648 return LDAP_SUCCESS;
1651 static int parsePermissiveModify (
1656 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1657 rs->sr_text = "permissiveModify control specified multiple times";
1658 return LDAP_PROTOCOL_ERROR;
1661 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1662 rs->sr_text = "permissiveModify control value not absent";
1663 return LDAP_PROTOCOL_ERROR;
1666 op->o_permissive_modify = ctrl->ldctl_iscritical
1667 ? SLAP_CONTROL_CRITICAL
1668 : SLAP_CONTROL_NONCRITICAL;
1670 return LDAP_SUCCESS;
1673 static int parseDomainScope (
1678 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1679 rs->sr_text = "domainScope control specified multiple times";
1680 return LDAP_PROTOCOL_ERROR;
1683 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1684 rs->sr_text = "domainScope control value not empty";
1685 return LDAP_PROTOCOL_ERROR;
1688 op->o_domain_scope = ctrl->ldctl_iscritical
1689 ? SLAP_CONTROL_CRITICAL
1690 : SLAP_CONTROL_NONCRITICAL;
1692 return LDAP_SUCCESS;
1695 #ifdef SLAP_CONTROL_X_TREE_DELETE
1696 static int parseTreeDelete (
1701 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1702 rs->sr_text = "treeDelete control specified multiple times";
1703 return LDAP_PROTOCOL_ERROR;
1706 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1707 rs->sr_text = "treeDelete control value not absent";
1708 return LDAP_PROTOCOL_ERROR;
1711 op->o_tree_delete = ctrl->ldctl_iscritical
1712 ? SLAP_CONTROL_CRITICAL
1713 : SLAP_CONTROL_NONCRITICAL;
1715 return LDAP_SUCCESS;
1719 static int parseSearchOptions (
1725 ber_int_t search_flags;
1728 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1729 rs->sr_text = "searchOptions control value is absent";
1730 return LDAP_PROTOCOL_ERROR;
1733 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1734 rs->sr_text = "searchOptions control value is empty";
1735 return LDAP_PROTOCOL_ERROR;
1738 ber = ber_init( &ctrl->ldctl_value );
1740 rs->sr_text = "internal error";
1744 tag = ber_scanf( ber, "{i}", &search_flags );
1745 (void) ber_free( ber, 1 );
1747 if ( tag == LBER_ERROR ) {
1748 rs->sr_text = "searchOptions control decoding error";
1749 return LDAP_PROTOCOL_ERROR;
1752 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1753 /* Search flags not recognised so far,
1755 * LDAP_SEARCH_FLAG_PHANTOM_ROOT
1757 if ( ctrl->ldctl_iscritical ) {
1758 rs->sr_text = "searchOptions contained unrecognized flag";
1759 return LDAP_UNWILLING_TO_PERFORM;
1763 Debug( LDAP_DEBUG_TRACE,
1764 "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n",
1765 op->o_connid, (unsigned)search_flags, 0 );
1767 return LDAP_SUCCESS;
1770 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1771 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1772 rs->sr_text = "searchOptions control specified multiple times "
1773 "or with domainScope control";
1774 return LDAP_PROTOCOL_ERROR;
1777 op->o_domain_scope = ctrl->ldctl_iscritical
1778 ? SLAP_CONTROL_CRITICAL
1779 : SLAP_CONTROL_NONCRITICAL;
1782 return LDAP_SUCCESS;
1785 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1786 struct berval session_tracking_formats[] = {
1787 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1788 BER_BVC( "RADIUS-Acct-Session-Id" ),
1789 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1790 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1791 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1792 BER_BVC( "USERNAME" ),
1797 static int parseSessionTracking(
1807 struct berval sessionSourceIp = BER_BVNULL,
1808 sessionSourceName = BER_BVNULL,
1809 formatOID = BER_BVNULL,
1810 sessionTrackingIdentifier = BER_BVNULL;
1812 size_t st_len, st_pos;
1814 if ( ctrl->ldctl_iscritical ) {
1815 rs->sr_text = "sessionTracking criticality is TRUE";
1816 return LDAP_PROTOCOL_ERROR;
1819 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1820 rs->sr_text = "sessionTracking control value is absent";
1821 return LDAP_PROTOCOL_ERROR;
1824 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1825 rs->sr_text = "sessionTracking control value is empty";
1826 return LDAP_PROTOCOL_ERROR;
1829 /* TODO: add the capability to determine if a client is allowed
1830 * to use this control, based on identity, ip and so */
1832 ber = ber_init( &ctrl->ldctl_value );
1833 if ( ber == NULL ) {
1834 rs->sr_text = "internal error";
1838 tag = ber_skip_tag( ber, &len );
1839 if ( tag != LBER_SEQUENCE ) {
1844 /* sessionSourceIp */
1845 tag = ber_peek_tag( ber, &len );
1846 if ( tag == LBER_DEFAULT ) {
1852 tag = ber_skip_tag( ber, &len );
1854 } else if ( len > 128 ) {
1855 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1856 rs->sr_err = LDAP_PROTOCOL_ERROR;
1860 tag = ber_scanf( ber, "m", &sessionSourceIp );
1863 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1864 BER_BVZERO( &sessionSourceIp );
1867 /* sessionSourceName */
1868 tag = ber_peek_tag( ber, &len );
1869 if ( tag == LBER_DEFAULT ) {
1875 tag = ber_skip_tag( ber, &len );
1877 } else if ( len > 65536 ) {
1878 rs->sr_text = "sessionTracking.sessionSourceName too long";
1879 rs->sr_err = LDAP_PROTOCOL_ERROR;
1883 tag = ber_scanf( ber, "m", &sessionSourceName );
1886 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1887 BER_BVZERO( &sessionSourceName );
1891 tag = ber_peek_tag( ber, &len );
1892 if ( tag == LBER_DEFAULT ) {
1898 rs->sr_text = "sessionTracking.formatOID empty";
1899 rs->sr_err = LDAP_PROTOCOL_ERROR;
1902 } else if ( len > 1024 ) {
1903 rs->sr_text = "sessionTracking.formatOID too long";
1904 rs->sr_err = LDAP_PROTOCOL_ERROR;
1908 tag = ber_scanf( ber, "m", &formatOID );
1911 rc = numericoidValidate( NULL, &formatOID );
1912 if ( rc != LDAP_SUCCESS ) {
1913 rs->sr_text = "sessionTracking.formatOID invalid";
1917 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1919 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1920 formatOID = session_tracking_formats[ i + 1 ];
1925 /* sessionTrackingIdentifier */
1926 tag = ber_peek_tag( ber, &len );
1927 if ( tag == LBER_DEFAULT ) {
1933 tag = ber_skip_tag( ber, &len );
1936 /* note: should not be more than 65536... */
1937 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1938 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1939 /* we want the OID printed, at least */
1940 BER_BVSTR( &sessionTrackingIdentifier, "" );
1945 tag = ber_skip_tag( ber, &len );
1946 if ( tag != LBER_DEFAULT || len != 0 ) {
1953 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1954 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1956 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1957 if ( st_len ) st_len++;
1958 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1960 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1961 if ( st_len ) st_len++;
1962 st_len += formatOID.bv_len + STRLENOF( "=" )
1963 + sessionTrackingIdentifier.bv_len;
1966 if ( st_len == 0 ) {
1970 st_len += STRLENOF( " []" );
1971 st_pos = strlen( op->o_log_prefix );
1973 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1974 char *ptr = &op->o_log_prefix[ st_pos ];
1976 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1979 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1980 ptr = lutil_strcopy( ptr, "IP=" );
1981 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1985 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1986 if ( st_len ) *ptr++ = ' ';
1987 ptr = lutil_strcopy( ptr, "NAME=" );
1988 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1992 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1993 if ( st_len ) *ptr++ = ' ';
1994 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1996 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
2004 (void)ber_free( ber, 1 );
2006 if ( tag == LBER_ERROR ) {
2007 rs->sr_text = "sessionTracking control decoding error";
2008 return LDAP_PROTOCOL_ERROR;
2016 slap_ctrl_session_tracking_add(
2020 struct berval *name,
2024 BerElementBuffer berbuf;
2025 BerElement *ber = (BerElement *)&berbuf;
2027 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
2029 assert( ctrl != NULL );
2031 ber_init2( ber, NULL, LBER_USE_DER );
2033 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
2035 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
2036 rs->sr_err = LDAP_OTHER;
2040 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
2041 ctrl->ldctl_iscritical = 0;
2043 rs->sr_err = LDAP_SUCCESS;
2050 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
2052 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
2053 struct berval ip = BER_BVNULL,
2057 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
2058 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
2062 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
2063 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
2065 ptr = ber_bvchr( &ip, ':' );
2067 ip.bv_len = ptr - ip.bv_val;
2071 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
2072 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
2074 name = op->o_conn->c_peer_domain;
2077 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2081 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2085 #ifdef SLAP_CONTROL_X_WHATFAILED
2086 static int parseWhatFailed(
2091 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2092 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2093 return LDAP_PROTOCOL_ERROR;
2096 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2097 rs->sr_text = "\"What Failed?\" control value not absent";
2098 return LDAP_PROTOCOL_ERROR;
2101 op->o_whatFailed = ctrl->ldctl_iscritical
2102 ? SLAP_CONTROL_CRITICAL
2103 : SLAP_CONTROL_NONCRITICAL;
2105 return LDAP_SUCCESS;
2109 slap_ctrl_whatFailed_add(
2114 BerElementBuffer berbuf;
2115 BerElement *ber = (BerElement *) &berbuf;
2116 LDAPControl **ctrls = NULL;
2117 struct berval ctrlval;
2118 int i, rc = LDAP_SUCCESS;
2120 ber_init2( ber, NULL, LBER_USE_DER );
2121 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2122 ber_printf( ber, "[" /*]*/ );
2123 for ( i = 0; oids[ i ] != NULL; i++ ) {
2124 ber_printf( ber, "s", oids[ i ] );
2126 ber_printf( ber, /*[*/ "]" );
2128 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2134 if ( rs->sr_ctrls != NULL ) {
2135 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2136 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2143 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2144 sizeof(LDAPControl *)*( i + 2 )
2145 + sizeof(LDAPControl)
2146 + ctrlval.bv_len + 1,
2148 if ( ctrls == NULL ) {
2152 ctrls[ i + 1 ] = NULL;
2153 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2154 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2155 ctrls[ i ]->ldctl_iscritical = 0;
2156 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2157 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2158 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2160 ber_free_buf( ber );
2162 rs->sr_ctrls = ctrls;
2169 #ifdef SLAP_CONTROL_X_LAZY_COMMIT
2170 static int parseLazyCommit(
2175 if ( op->o_lazyCommit != SLAP_CONTROL_NONE ) {
2176 rs->sr_text = "\"Lazy Commit?\" control specified multiple times";
2177 return LDAP_PROTOCOL_ERROR;
2180 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2181 rs->sr_text = "\"Lazy Commit?\" control value not absent";
2182 return LDAP_PROTOCOL_ERROR;
2185 op->o_lazyCommit = ctrl->ldctl_iscritical
2186 ? SLAP_CONTROL_CRITICAL
2187 : SLAP_CONTROL_NONCRITICAL;
2189 return LDAP_SUCCESS;