2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2016 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 );
1416 op->o_assertion = NULL;
1422 filter2bv_x( op, op->o_assertion, &fstr );
1424 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1425 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1426 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1429 op->o_assert = ctrl->ldctl_iscritical
1430 ? SLAP_CONTROL_CRITICAL
1431 : SLAP_CONTROL_NONCRITICAL;
1433 rs->sr_err = LDAP_SUCCESS;
1434 return LDAP_SUCCESS;
1437 #define READMSG(post, msg) \
1438 ( post ? "postread control: " msg : "preread control: " msg )
1447 ber_len_t siz, off, i;
1449 AttributeName *an = NULL;
1451 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1452 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1454 rs->sr_text = READMSG( post, "specified multiple times" );
1455 return LDAP_PROTOCOL_ERROR;
1458 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1459 rs->sr_text = READMSG( post, "value is absent" );
1460 return LDAP_PROTOCOL_ERROR;
1463 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1464 rs->sr_text = READMSG( post, "value is empty" );
1465 return LDAP_PROTOCOL_ERROR;
1469 if ( op->o_txnSpec ) { /* temporary limitation */
1470 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1471 return LDAP_UNWILLING_TO_PERFORM;
1475 ber = ber_init( &ctrl->ldctl_value );
1476 if ( ber == NULL ) {
1477 rs->sr_text = READMSG( post, "internal error" );
1481 rs->sr_err = LDAP_SUCCESS;
1482 siz = sizeof( AttributeName );
1483 off = offsetof( AttributeName, an_name );
1484 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1485 rs->sr_text = READMSG( post, "decoding error" );
1486 rs->sr_err = LDAP_PROTOCOL_ERROR;
1490 for ( i = 0; i < siz; i++ ) {
1491 const char *dummy = NULL;
1494 an[i].an_desc = NULL;
1497 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1498 if ( rc == LDAP_SUCCESS ) {
1499 an[i].an_name = an[i].an_desc->ad_cname;
1503 static struct berval special_attrs[] = {
1504 BER_BVC( LDAP_NO_ATTRS ),
1505 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1506 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1510 /* deal with special attribute types */
1511 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1512 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1513 an[i].an_name = special_attrs[ j ];
1518 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1520 rs->sr_text = dummy ? dummy
1521 : READMSG( post, "unknown attributeType" );
1528 op->o_postread_attrs = an;
1529 op->o_postread = ctrl->ldctl_iscritical
1530 ? SLAP_CONTROL_CRITICAL
1531 : SLAP_CONTROL_NONCRITICAL;
1533 op->o_preread_attrs = an;
1534 op->o_preread = ctrl->ldctl_iscritical
1535 ? SLAP_CONTROL_CRITICAL
1536 : SLAP_CONTROL_NONCRITICAL;
1540 (void) ber_free( ber, 1 );
1544 static int parsePreRead (
1549 return parseReadAttrs( op, rs, ctrl, 0 );
1552 static int parsePostRead (
1557 return parseReadAttrs( op, rs, ctrl, 1 );
1560 static int parseValuesReturnFilter (
1566 struct berval fstr = BER_BVNULL;
1568 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1569 rs->sr_text = "valuesReturnFilter control specified multiple times";
1570 return LDAP_PROTOCOL_ERROR;
1573 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1574 rs->sr_text = "valuesReturnFilter control value is absent";
1575 return LDAP_PROTOCOL_ERROR;
1578 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1579 rs->sr_text = "valuesReturnFilter control value is empty";
1580 return LDAP_PROTOCOL_ERROR;
1583 ber = ber_init( &(ctrl->ldctl_value) );
1585 rs->sr_text = "internal error";
1589 rs->sr_err = get_vrFilter( op, ber,
1590 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1592 (void) ber_free( ber, 1 );
1594 if( rs->sr_err != LDAP_SUCCESS ) {
1595 if( rs->sr_err == SLAPD_DISCONNECT ) {
1596 rs->sr_err = LDAP_PROTOCOL_ERROR;
1597 send_ldap_disconnect( op, rs );
1598 rs->sr_err = SLAPD_DISCONNECT;
1600 send_ldap_result( op, rs );
1602 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1606 vrFilter2bv( op, op->o_vrFilter, &fstr );
1609 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1610 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1611 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1614 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1615 ? SLAP_CONTROL_CRITICAL
1616 : SLAP_CONTROL_NONCRITICAL;
1618 rs->sr_err = LDAP_SUCCESS;
1619 return LDAP_SUCCESS;
1622 static int parseSubentries (
1627 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1628 rs->sr_text = "subentries control specified multiple times";
1629 return LDAP_PROTOCOL_ERROR;
1632 /* FIXME: should use BER library */
1633 if( ( ctrl->ldctl_value.bv_len != 3 )
1634 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1635 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1637 rs->sr_text = "subentries control value encoding is bogus";
1638 return LDAP_PROTOCOL_ERROR;
1641 op->o_subentries = ctrl->ldctl_iscritical
1642 ? SLAP_CONTROL_CRITICAL
1643 : SLAP_CONTROL_NONCRITICAL;
1645 if (ctrl->ldctl_value.bv_val[2]) {
1646 set_subentries_visibility( op );
1649 return LDAP_SUCCESS;
1652 static int parsePermissiveModify (
1657 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1658 rs->sr_text = "permissiveModify control specified multiple times";
1659 return LDAP_PROTOCOL_ERROR;
1662 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1663 rs->sr_text = "permissiveModify control value not absent";
1664 return LDAP_PROTOCOL_ERROR;
1667 op->o_permissive_modify = ctrl->ldctl_iscritical
1668 ? SLAP_CONTROL_CRITICAL
1669 : SLAP_CONTROL_NONCRITICAL;
1671 return LDAP_SUCCESS;
1674 static int parseDomainScope (
1679 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1680 rs->sr_text = "domainScope control specified multiple times";
1681 return LDAP_PROTOCOL_ERROR;
1684 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1685 rs->sr_text = "domainScope control value not empty";
1686 return LDAP_PROTOCOL_ERROR;
1689 op->o_domain_scope = ctrl->ldctl_iscritical
1690 ? SLAP_CONTROL_CRITICAL
1691 : SLAP_CONTROL_NONCRITICAL;
1693 return LDAP_SUCCESS;
1696 #ifdef SLAP_CONTROL_X_TREE_DELETE
1697 static int parseTreeDelete (
1702 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1703 rs->sr_text = "treeDelete control specified multiple times";
1704 return LDAP_PROTOCOL_ERROR;
1707 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1708 rs->sr_text = "treeDelete control value not absent";
1709 return LDAP_PROTOCOL_ERROR;
1712 op->o_tree_delete = ctrl->ldctl_iscritical
1713 ? SLAP_CONTROL_CRITICAL
1714 : SLAP_CONTROL_NONCRITICAL;
1716 return LDAP_SUCCESS;
1720 static int parseSearchOptions (
1726 ber_int_t search_flags;
1729 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1730 rs->sr_text = "searchOptions control value is absent";
1731 return LDAP_PROTOCOL_ERROR;
1734 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1735 rs->sr_text = "searchOptions control value is empty";
1736 return LDAP_PROTOCOL_ERROR;
1739 ber = ber_init( &ctrl->ldctl_value );
1741 rs->sr_text = "internal error";
1745 tag = ber_scanf( ber, "{i}", &search_flags );
1746 (void) ber_free( ber, 1 );
1748 if ( tag == LBER_ERROR ) {
1749 rs->sr_text = "searchOptions control decoding error";
1750 return LDAP_PROTOCOL_ERROR;
1753 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1754 /* Search flags not recognised so far,
1756 * LDAP_SEARCH_FLAG_PHANTOM_ROOT
1758 if ( ctrl->ldctl_iscritical ) {
1759 rs->sr_text = "searchOptions contained unrecognized flag";
1760 return LDAP_UNWILLING_TO_PERFORM;
1764 Debug( LDAP_DEBUG_TRACE,
1765 "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n",
1766 op->o_connid, (unsigned)search_flags, 0 );
1768 return LDAP_SUCCESS;
1771 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1772 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1773 rs->sr_text = "searchOptions control specified multiple times "
1774 "or with domainScope control";
1775 return LDAP_PROTOCOL_ERROR;
1778 op->o_domain_scope = ctrl->ldctl_iscritical
1779 ? SLAP_CONTROL_CRITICAL
1780 : SLAP_CONTROL_NONCRITICAL;
1783 return LDAP_SUCCESS;
1786 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1787 struct berval session_tracking_formats[] = {
1788 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1789 BER_BVC( "RADIUS-Acct-Session-Id" ),
1790 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1791 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1792 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1793 BER_BVC( "USERNAME" ),
1798 static int parseSessionTracking(
1808 struct berval sessionSourceIp = BER_BVNULL,
1809 sessionSourceName = BER_BVNULL,
1810 formatOID = BER_BVNULL,
1811 sessionTrackingIdentifier = BER_BVNULL;
1813 size_t st_len, st_pos;
1815 if ( ctrl->ldctl_iscritical ) {
1816 rs->sr_text = "sessionTracking criticality is TRUE";
1817 return LDAP_PROTOCOL_ERROR;
1820 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1821 rs->sr_text = "sessionTracking control value is absent";
1822 return LDAP_PROTOCOL_ERROR;
1825 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1826 rs->sr_text = "sessionTracking control value is empty";
1827 return LDAP_PROTOCOL_ERROR;
1830 /* TODO: add the capability to determine if a client is allowed
1831 * to use this control, based on identity, ip and so */
1833 ber = ber_init( &ctrl->ldctl_value );
1834 if ( ber == NULL ) {
1835 rs->sr_text = "internal error";
1839 tag = ber_skip_tag( ber, &len );
1840 if ( tag != LBER_SEQUENCE ) {
1845 /* sessionSourceIp */
1846 tag = ber_peek_tag( ber, &len );
1847 if ( tag == LBER_DEFAULT ) {
1853 tag = ber_skip_tag( ber, &len );
1855 } else if ( len > 128 ) {
1856 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1857 rs->sr_err = LDAP_PROTOCOL_ERROR;
1861 tag = ber_scanf( ber, "m", &sessionSourceIp );
1864 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1865 BER_BVZERO( &sessionSourceIp );
1868 /* sessionSourceName */
1869 tag = ber_peek_tag( ber, &len );
1870 if ( tag == LBER_DEFAULT ) {
1876 tag = ber_skip_tag( ber, &len );
1878 } else if ( len > 65536 ) {
1879 rs->sr_text = "sessionTracking.sessionSourceName too long";
1880 rs->sr_err = LDAP_PROTOCOL_ERROR;
1884 tag = ber_scanf( ber, "m", &sessionSourceName );
1887 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1888 BER_BVZERO( &sessionSourceName );
1892 tag = ber_peek_tag( ber, &len );
1893 if ( tag == LBER_DEFAULT ) {
1899 rs->sr_text = "sessionTracking.formatOID empty";
1900 rs->sr_err = LDAP_PROTOCOL_ERROR;
1903 } else if ( len > 1024 ) {
1904 rs->sr_text = "sessionTracking.formatOID too long";
1905 rs->sr_err = LDAP_PROTOCOL_ERROR;
1909 tag = ber_scanf( ber, "m", &formatOID );
1912 rc = numericoidValidate( NULL, &formatOID );
1913 if ( rc != LDAP_SUCCESS ) {
1914 rs->sr_text = "sessionTracking.formatOID invalid";
1918 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1920 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1921 formatOID = session_tracking_formats[ i + 1 ];
1926 /* sessionTrackingIdentifier */
1927 tag = ber_peek_tag( ber, &len );
1928 if ( tag == LBER_DEFAULT ) {
1934 tag = ber_skip_tag( ber, &len );
1937 /* note: should not be more than 65536... */
1938 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1939 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1940 /* we want the OID printed, at least */
1941 BER_BVSTR( &sessionTrackingIdentifier, "" );
1946 tag = ber_skip_tag( ber, &len );
1947 if ( tag != LBER_DEFAULT || len != 0 ) {
1954 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1955 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1957 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1958 if ( st_len ) st_len++;
1959 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1961 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1962 if ( st_len ) st_len++;
1963 st_len += formatOID.bv_len + STRLENOF( "=" )
1964 + sessionTrackingIdentifier.bv_len;
1967 if ( st_len == 0 ) {
1971 st_len += STRLENOF( " []" );
1972 st_pos = strlen( op->o_log_prefix );
1974 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1975 char *ptr = &op->o_log_prefix[ st_pos ];
1977 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1980 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1981 ptr = lutil_strcopy( ptr, "IP=" );
1982 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1986 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1987 if ( st_len ) *ptr++ = ' ';
1988 ptr = lutil_strcopy( ptr, "NAME=" );
1989 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1993 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1994 if ( st_len ) *ptr++ = ' ';
1995 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1997 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
2005 (void)ber_free( ber, 1 );
2007 if ( tag == LBER_ERROR ) {
2008 rs->sr_text = "sessionTracking control decoding error";
2009 return LDAP_PROTOCOL_ERROR;
2017 slap_ctrl_session_tracking_add(
2021 struct berval *name,
2025 BerElementBuffer berbuf;
2026 BerElement *ber = (BerElement *)&berbuf;
2028 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
2030 assert( ctrl != NULL );
2032 ber_init2( ber, NULL, LBER_USE_DER );
2034 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
2036 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
2037 rs->sr_err = LDAP_OTHER;
2041 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
2042 ctrl->ldctl_iscritical = 0;
2044 rs->sr_err = LDAP_SUCCESS;
2051 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
2053 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
2054 struct berval ip = BER_BVNULL,
2058 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
2059 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
2063 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
2064 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
2066 ptr = ber_bvchr( &ip, ':' );
2068 ip.bv_len = ptr - ip.bv_val;
2072 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
2073 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
2075 name = op->o_conn->c_peer_domain;
2078 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2082 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2086 #ifdef SLAP_CONTROL_X_WHATFAILED
2087 static int parseWhatFailed(
2092 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2093 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2094 return LDAP_PROTOCOL_ERROR;
2097 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2098 rs->sr_text = "\"What Failed?\" control value not absent";
2099 return LDAP_PROTOCOL_ERROR;
2102 op->o_whatFailed = ctrl->ldctl_iscritical
2103 ? SLAP_CONTROL_CRITICAL
2104 : SLAP_CONTROL_NONCRITICAL;
2106 return LDAP_SUCCESS;
2110 slap_ctrl_whatFailed_add(
2115 BerElementBuffer berbuf;
2116 BerElement *ber = (BerElement *) &berbuf;
2117 LDAPControl **ctrls = NULL;
2118 struct berval ctrlval;
2119 int i, rc = LDAP_SUCCESS;
2121 ber_init2( ber, NULL, LBER_USE_DER );
2122 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2123 ber_printf( ber, "[" /*]*/ );
2124 for ( i = 0; oids[ i ] != NULL; i++ ) {
2125 ber_printf( ber, "s", oids[ i ] );
2127 ber_printf( ber, /*[*/ "]" );
2129 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2135 if ( rs->sr_ctrls != NULL ) {
2136 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2137 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2144 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2145 sizeof(LDAPControl *)*( i + 2 )
2146 + sizeof(LDAPControl)
2147 + ctrlval.bv_len + 1,
2149 if ( ctrls == NULL ) {
2153 ctrls[ i + 1 ] = NULL;
2154 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2155 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2156 ctrls[ i ]->ldctl_iscritical = 0;
2157 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2158 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2159 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2161 ber_free_buf( ber );
2163 rs->sr_ctrls = ctrls;
2170 #ifdef SLAP_CONTROL_X_LAZY_COMMIT
2171 static int parseLazyCommit(
2176 if ( op->o_lazyCommit != SLAP_CONTROL_NONE ) {
2177 rs->sr_text = "\"Lazy Commit?\" control specified multiple times";
2178 return LDAP_PROTOCOL_ERROR;
2181 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2182 rs->sr_text = "\"Lazy Commit?\" control value not absent";
2183 return LDAP_PROTOCOL_ERROR;
2186 op->o_lazyCommit = ctrl->ldctl_iscritical
2187 ? SLAP_CONTROL_CRITICAL
2188 : SLAP_CONTROL_NONCRITICAL;
2190 return LDAP_SUCCESS;