2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 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;
55 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
57 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
58 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
60 struct slap_control_ids slap_cids;
66 /* The controlID for this control */
69 /* Operations supported by control */
72 /* Extended operations supported by control */
73 char **sc_extendedops; /* input */
74 BerVarray sc_extendedopsbv; /* run-time use */
76 /* Control parsing callback */
77 SLAP_CTRL_PARSE_FN *sc_parse;
79 LDAP_SLIST_ENTRY(slap_control) sc_next;
82 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
83 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
86 * all known request control OIDs should be added to this list
89 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
90 * addresses an undefined control; this allows to safely test for
91 * well known controls even if they are not registered, e.g. if
92 * they get moved to modules. An example is sc_LDAPsync, which
93 * is implemented in the syncprov overlay and thus, if configured
94 * as dynamic module, may not be registered. One side effect is that
95 * slap_known_controls[0] == NULL, so it should always be used
97 * FIXME: should we define the "undefined control" oid?
99 char *slap_known_controls[SLAP_MAX_CIDS+1];
100 static int num_known_controls = 1;
102 static char *proxy_authz_extops[] = {
103 LDAP_EXOP_MODIFY_PASSWD,
109 static char *manageDSAit_extops[] = {
114 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
115 static char *session_tracking_extops[] = {
116 LDAP_EXOP_MODIFY_PASSWD,
123 static struct slap_control control_defs[] = {
124 { LDAP_CONTROL_ASSERT,
125 (int)offsetof(struct slap_control_ids, sc_assert),
126 SLAP_CTRL_UPDATE|SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
128 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
129 { LDAP_CONTROL_PRE_READ,
130 (int)offsetof(struct slap_control_ids, sc_preRead),
131 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
133 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
134 { LDAP_CONTROL_POST_READ,
135 (int)offsetof(struct slap_control_ids, sc_postRead),
136 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
138 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 { LDAP_CONTROL_VALUESRETURNFILTER,
140 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
141 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
143 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
144 { LDAP_CONTROL_PAGEDRESULTS,
145 (int)offsetof(struct slap_control_ids, sc_pagedResults),
148 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
150 { LDAP_CONTROL_SORTREQUEST,
151 (int)offsetof(struct slap_control_ids, sc_sortedResults),
152 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
154 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
156 { LDAP_CONTROL_X_DOMAIN_SCOPE,
157 (int)offsetof(struct slap_control_ids, sc_domainScope),
158 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
160 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
161 { LDAP_CONTROL_DONTUSECOPY,
162 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
163 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE,
165 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
166 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
167 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
168 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
170 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
171 #ifdef SLAP_CONTROL_X_TREE_DELETE
172 { LDAP_CONTROL_X_TREE_DELETE,
173 (int)offsetof(struct slap_control_ids, sc_treeDelete),
174 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
176 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
178 { LDAP_CONTROL_X_SEARCH_OPTIONS,
179 (int)offsetof(struct slap_control_ids, sc_searchOptions),
180 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
182 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
183 { LDAP_CONTROL_SUBENTRIES,
184 (int)offsetof(struct slap_control_ids, sc_subentries),
187 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
189 (int)offsetof(struct slap_control_ids, sc_noOp),
190 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
192 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
193 { LDAP_CONTROL_RELAX,
194 (int)offsetof(struct slap_control_ids, sc_relax),
195 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
197 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
199 { LDAP_CONTROL_X_TXN_SPEC,
200 (int)offsetof(struct slap_control_ids, sc_txnSpec),
201 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
203 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
205 { LDAP_CONTROL_MANAGEDSAIT,
206 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
208 manageDSAit_extops, NULL,
209 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
210 { LDAP_CONTROL_PROXY_AUTHZ,
211 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
212 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
213 proxy_authz_extops, NULL,
214 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
215 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
216 { LDAP_CONTROL_X_SESSION_TRACKING,
217 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
218 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
219 session_tracking_extops, NULL,
220 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
222 #ifdef SLAP_CONTROL_X_WHATFAILED
223 { LDAP_CONTROL_X_WHATFAILED,
224 (int)offsetof(struct slap_control_ids, sc_whatFailed),
225 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
227 parseWhatFailed, LDAP_SLIST_ENTRY_INITIALIZER(next) },
230 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
233 static struct slap_control *
234 find_ctrl( const char *oid );
237 * Register a supported control.
239 * This can be called by an OpenLDAP plugin or, indirectly, by a
240 * SLAPI plugin calling slapi_register_supported_control().
242 * NOTE: if flags == 1 the control is replaced if already registered;
243 * otherwise registering an already registered control is not allowed.
246 register_supported_control2(const char *controloid,
247 slap_mask_t controlmask,
249 SLAP_CTRL_PARSE_FN *controlparsefn,
253 struct slap_control *sc = NULL;
255 BerVarray extendedopsbv = NULL;
257 if ( num_known_controls >= SLAP_MAX_CIDS ) {
258 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
259 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
260 SLAP_MAX_CIDS, 0, 0 );
264 if ( controloid == NULL ) {
265 return LDAP_PARAM_ERROR;
268 /* check if already registered */
269 for ( i = 0; slap_known_controls[ i ]; i++ ) {
270 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
272 Debug( LDAP_DEBUG_TRACE,
273 "Control %s already registered; replacing.\n",
275 /* (find and) replace existing handler */
276 sc = find_ctrl( controloid );
277 assert( sc != NULL );
281 Debug( LDAP_DEBUG_ANY,
282 "Control %s already registered.\n",
284 return LDAP_PARAM_ERROR;
288 /* turn compatible extended operations into bervals */
289 if ( controlexops != NULL ) {
292 for ( i = 0; controlexops[ i ]; i++ );
294 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
295 if ( extendedopsbv == NULL ) {
296 return LDAP_NO_MEMORY;
299 for ( i = 0; controlexops[ i ]; i++ ) {
300 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
305 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
307 return LDAP_NO_MEMORY;
310 sc->sc_oid = ch_strdup( controloid );
311 sc->sc_cid = num_known_controls;
313 /* Update slap_known_controls, too. */
314 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
315 slap_known_controls[num_known_controls++] = NULL;
317 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
318 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
321 if ( sc->sc_extendedopsbv ) {
322 /* FIXME: in principle, we should rather merge
323 * existing extops with those supported by the
324 * new control handling implementation.
325 * In fact, whether a control is compatible with
326 * an extop should not be a matter of implementation.
327 * We likely also need a means for a newly
328 * registered extop to declare that it is
329 * comptible with an already registered control.
331 ber_bvarray_free( sc->sc_extendedopsbv );
332 sc->sc_extendedopsbv = NULL;
333 sc->sc_extendedops = NULL;
337 sc->sc_extendedopsbv = extendedopsbv;
338 sc->sc_mask = controlmask;
339 sc->sc_parse = controlparsefn;
341 *controlcid = sc->sc_cid;
347 #ifdef SLAP_CONFIG_DELETE
349 unregister_supported_control( const char *controloid )
351 struct slap_control *sc;
354 if ( controloid == NULL || (sc = find_ctrl( controloid )) == NULL ){
358 for ( i = 0; slap_known_controls[ i ]; i++ ) {
359 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
361 slap_known_controls[ i ] = slap_known_controls[ i+1 ];
362 } while ( slap_known_controls[ i++ ] );
363 num_known_controls--;
368 LDAP_SLIST_REMOVE(&controls_list, sc, slap_control, sc_next);
369 ch_free( sc->sc_oid );
370 if ( sc->sc_extendedopsbv != NULL ) {
371 ber_bvarray_free( sc->sc_extendedopsbv );
377 #endif /* SLAP_CONFIG_DELETE */
380 * One-time initialization of internal controls.
383 slap_controls_init( void )
389 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
390 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
391 rc = register_supported_control( control_defs[i].sc_oid,
392 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
393 control_defs[i].sc_parse, cid );
394 if ( rc != LDAP_SUCCESS ) break;
401 * Free memory associated with list of supported controls.
404 controls_destroy( void )
406 struct slap_control *sc;
408 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
409 sc = LDAP_SLIST_FIRST(&controls_list);
410 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
412 ch_free( sc->sc_oid );
413 if ( sc->sc_extendedopsbv != NULL ) {
414 ber_bvarray_free( sc->sc_extendedopsbv );
421 * Format the supportedControl attribute of the root DSE,
422 * detailing which controls are supported by the directory
426 controls_root_dse_info( Entry *e )
428 AttributeDescription *ad_supportedControl
429 = slap_schema.si_ad_supportedControl;
430 struct berval vals[2];
431 struct slap_control *sc;
433 vals[1].bv_val = NULL;
436 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
437 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
439 vals[0].bv_val = sc->sc_oid;
440 vals[0].bv_len = strlen( sc->sc_oid );
442 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
451 * Return a list of OIDs and operation masks for supported
452 * controls. Used by SLAPI.
455 get_supported_controls(char ***ctrloidsp,
456 slap_mask_t **ctrlmasks)
461 struct slap_control *sc;
465 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
475 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
476 if ( oids == NULL ) {
477 return LDAP_NO_MEMORY;
479 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
480 if ( masks == NULL ) {
482 return LDAP_NO_MEMORY;
487 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
488 oids[n] = ch_strdup( sc->sc_oid );
489 masks[n] = sc->sc_mask;
502 * Find a control given its OID.
504 static struct slap_control *
505 find_ctrl( const char *oid )
507 struct slap_control *sc;
509 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
510 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
519 slap_find_control_id(
523 struct slap_control *ctrl = find_ctrl( oid );
525 if ( cid ) *cid = ctrl->sc_cid;
528 return LDAP_CONTROL_NOT_FOUND;
532 slap_global_control( Operation *op, const char *oid, int *cid )
534 struct slap_control *ctrl = find_ctrl( oid );
536 if ( ctrl == NULL ) {
537 /* should not be reachable */
538 Debug( LDAP_DEBUG_ANY,
539 "slap_global_control: unrecognized control: %s\n",
541 return LDAP_CONTROL_NOT_FOUND;
544 if ( cid ) *cid = ctrl->sc_cid;
546 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
547 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
548 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
550 return LDAP_COMPARE_TRUE;
554 Debug( LDAP_DEBUG_TRACE,
555 "slap_global_control: unavailable control: %s\n",
559 return LDAP_COMPARE_FALSE;
562 void slap_free_ctrls(
564 LDAPControl **ctrls )
568 if( op->o_assertion != NULL ) {
569 filter_free_x( op, op->o_assertion, 1 );
570 op->o_assertion = NULL;
572 if( op->o_vrFilter != NULL) {
573 vrFilter_free( op, op->o_vrFilter );
574 op->o_vrFilter = NULL;
576 if( op->o_preread_attrs != NULL ) {
577 op->o_tmpfree( op->o_preread_attrs, op->o_tmpmemctx );
578 op->o_preread_attrs = NULL;
580 if( op->o_postread_attrs != NULL ) {
581 op->o_tmpfree( op->o_postread_attrs, op->o_tmpmemctx );
582 op->o_postread_attrs = NULL;
584 if( op->o_pagedresults_state != NULL ) {
585 op->o_tmpfree( op->o_pagedresults_state, op->o_tmpmemctx );
586 op->o_pagedresults_state = NULL;
589 for (i=0; ctrls[i]; i++) {
590 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
592 op->o_tmpfree( ctrls, op->o_tmpmemctx );
598 LDAPControl **ctrls )
601 LDAPControl **ctrlsp;
603 if ( rs->sr_ctrls ) {
604 for ( ; rs->sr_ctrls[ i ]; i++ ) ;
607 for ( j=0; ctrls[j]; j++ ) ;
609 ctrlsp = op->o_tmpalloc(( i+j+1 )*sizeof(LDAPControl *), op->o_tmpmemctx );
611 if ( rs->sr_ctrls ) {
612 for ( ; rs->sr_ctrls[i]; i++ )
613 ctrlsp[i] = rs->sr_ctrls[i];
615 for ( j=0; ctrls[j]; j++)
616 ctrlsp[i++] = ctrls[j];
619 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED )
620 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
621 rs->sr_ctrls = ctrlsp;
622 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
629 LDAPControl *control,
632 struct slap_control *sc;
633 int rc = LDAP_SUCCESS;
635 sc = find_ctrl( control->ldctl_oid );
637 /* recognized control */
639 switch( op->o_tag ) {
641 tagmask = SLAP_CTRL_ADD;
644 tagmask = SLAP_CTRL_BIND;
646 case LDAP_REQ_COMPARE:
647 tagmask = SLAP_CTRL_COMPARE;
649 case LDAP_REQ_DELETE:
650 tagmask = SLAP_CTRL_DELETE;
652 case LDAP_REQ_MODIFY:
653 tagmask = SLAP_CTRL_MODIFY;
655 case LDAP_REQ_RENAME:
656 tagmask = SLAP_CTRL_RENAME;
658 case LDAP_REQ_SEARCH:
659 tagmask = SLAP_CTRL_SEARCH;
661 case LDAP_REQ_UNBIND:
662 tagmask = SLAP_CTRL_UNBIND;
664 case LDAP_REQ_ABANDON:
665 tagmask = SLAP_CTRL_ABANDON;
667 case LDAP_REQ_EXTENDED:
669 assert( op->ore_reqoid.bv_val != NULL );
670 if( sc->sc_extendedopsbv != NULL ) {
672 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
673 if( bvmatch( &op->ore_reqoid,
674 &sc->sc_extendedopsbv[i] ) )
683 *text = "controls internal error";
687 if (( sc->sc_mask & tagmask ) == tagmask ) {
688 /* available extension */
689 if ( sc->sc_parse ) {
690 rc = sc->sc_parse( op, rs, control );
691 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
693 } else if ( control->ldctl_iscritical ) {
694 *text = "not yet implemented";
699 } else if ( control->ldctl_iscritical ) {
700 /* unavailable CRITICAL control */
701 *text = "critical extension is unavailable";
702 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
705 } else if ( control->ldctl_iscritical ) {
706 /* unrecognized CRITICAL control */
707 *text = "critical extension is not recognized";
708 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
720 return get_ctrls2( op, rs, sendres, LDAP_TAG_CONTROLS );
734 BerElement *ber = op->o_ber;
736 #ifdef SLAP_CONTROL_X_WHATFAILED
737 /* NOTE: right now, slapd checks the validity of each control
738 * while parsing. As a consequence, it can only detect one
739 * cause of failure at a time. This results in returning
740 * exactly one OID with the whatFailed control, or no control
743 char *failed_oid = NULL;
746 len = ber_pvt_ber_remaining(ber);
750 rs->sr_err = LDAP_SUCCESS;
754 if(( tag = ber_peek_tag( ber, &len )) != ctag ) {
755 if( tag == LBER_ERROR ) {
756 rs->sr_err = SLAPD_DISCONNECT;
757 rs->sr_text = "unexpected data in PDU";
763 Debug( LDAP_DEBUG_TRACE,
764 "=> get_ctrls\n", 0, 0, 0 );
766 if( op->o_protocol < LDAP_VERSION3 ) {
767 rs->sr_err = SLAPD_DISCONNECT;
768 rs->sr_text = "controls require LDAPv3";
772 /* one for first control, one for termination */
773 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
776 if( op->ctrls == NULL ) {
777 rs->sr_err = LDAP_NO_MEMORY;
778 rs->sr_text = "no memory";
783 op->o_ctrls[nctrls] = NULL;
785 /* step through each element */
786 for( tag = ber_first_element( ber, &len, &opaque );
788 tag = ber_next_element( ber, &len, opaque ) )
791 LDAPControl **tctrls;
793 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
794 memset(c, 0, sizeof(LDAPControl));
796 /* allocate pointer space for current controls (nctrls)
797 * + this control + extra NULL
799 tctrls = op->o_tmprealloc( op->o_ctrls,
800 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
803 if( tctrls == NULL ) {
805 ldap_controls_free(op->o_ctrls);
808 rs->sr_err = LDAP_NO_MEMORY;
809 rs->sr_text = "no memory";
813 op->o_ctrls = tctrls;
815 op->o_ctrls[nctrls++] = c;
816 op->o_ctrls[nctrls] = NULL;
818 tag = ber_scanf( ber, "{m" /*}*/, &bv );
819 c->ldctl_oid = bv.bv_val;
821 if( tag == LBER_ERROR ) {
822 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
825 slap_free_ctrls( op, op->o_ctrls );
827 rs->sr_err = SLAPD_DISCONNECT;
828 rs->sr_text = "decoding controls error";
831 } else if( c->ldctl_oid == NULL ) {
832 Debug( LDAP_DEBUG_TRACE,
833 "get_ctrls: conn %lu got emtpy OID.\n",
834 op->o_connid, 0, 0 );
836 slap_free_ctrls( op, op->o_ctrls );
838 rs->sr_err = LDAP_PROTOCOL_ERROR;
839 rs->sr_text = "OID field is empty";
843 tag = ber_peek_tag( ber, &len );
845 if( tag == LBER_BOOLEAN ) {
847 tag = ber_scanf( ber, "b", &crit );
849 if( tag == LBER_ERROR ) {
850 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
852 slap_free_ctrls( op, op->o_ctrls );
854 rs->sr_err = SLAPD_DISCONNECT;
855 rs->sr_text = "decoding controls error";
859 c->ldctl_iscritical = (crit != 0);
860 tag = ber_peek_tag( ber, &len );
863 if( tag == LBER_OCTETSTRING ) {
864 tag = ber_scanf( ber, "m", &c->ldctl_value );
866 if( tag == LBER_ERROR ) {
867 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
868 "%s (%scritical): get value failed.\n",
869 op->o_connid, c->ldctl_oid,
870 c->ldctl_iscritical ? "" : "non" );
871 slap_free_ctrls( op, op->o_ctrls );
873 rs->sr_err = SLAPD_DISCONNECT;
874 rs->sr_text = "decoding controls error";
879 Debug( LDAP_DEBUG_TRACE,
880 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
881 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
883 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
884 if ( rs->sr_err != LDAP_SUCCESS ) {
885 #ifdef SLAP_CONTROL_X_WHATFAILED
886 failed_oid = c->ldctl_oid;
893 Debug( LDAP_DEBUG_TRACE,
894 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
895 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
897 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
898 if( rs->sr_err == SLAPD_DISCONNECT ) {
899 rs->sr_err = LDAP_PROTOCOL_ERROR;
900 send_ldap_disconnect( op, rs );
901 rs->sr_err = SLAPD_DISCONNECT;
903 #ifdef SLAP_CONTROL_X_WHATFAILED
904 /* might have not been parsed yet? */
905 if ( failed_oid != NULL ) {
906 if ( !get_whatFailed( op ) ) {
909 /* step through each remaining element */
910 for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
912 LDAPControl c = { 0 };
914 tag = ber_scanf( ber, "{m" /*}*/, &bv );
915 c.ldctl_oid = bv.bv_val;
917 if ( tag == LBER_ERROR ) {
918 slap_free_ctrls( op, op->o_ctrls );
922 } else if ( c.ldctl_oid == NULL ) {
923 slap_free_ctrls( op, op->o_ctrls );
928 tag = ber_peek_tag( ber, &len );
929 if ( tag == LBER_BOOLEAN ) {
931 tag = ber_scanf( ber, "b", &crit );
932 if( tag == LBER_ERROR ) {
933 slap_free_ctrls( op, op->o_ctrls );
938 tag = ber_peek_tag( ber, &len );
941 if ( tag == LBER_OCTETSTRING ) {
942 tag = ber_scanf( ber, "m", &c.ldctl_value );
944 if( tag == LBER_ERROR ) {
945 slap_free_ctrls( op, op->o_ctrls );
951 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
953 slap_parse_ctrl( op, rs, &c, &text );
959 if ( get_whatFailed( op ) ) {
961 oids[ 0 ] = failed_oid;
963 slap_ctrl_whatFailed_add( op, rs, oids );
968 send_ldap_result( op, rs );
980 BI_chk_controls fnc )
984 switch ( op->o_ctrlflag[ ctrl ] ) {
985 case SLAP_CONTROL_NONCRITICAL:
986 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
987 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
988 slap_known_controls[ ctrl - 1 ] ) == 0 )
995 rs->sr_err = LDAP_OTHER;
1000 (void)fnc( op, rs );
1003 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
1006 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
1007 ( i - j ) * sizeof( LDAPControl * ) );
1010 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
1014 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
1016 Debug( LDAP_DEBUG_ANY, "%s: "
1017 "non-critical control \"%s\" not supported; stripped.\n",
1018 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1021 case SLAP_CONTROL_IGNORED:
1022 case SLAP_CONTROL_NONE:
1023 rs->sr_err = SLAP_CB_CONTINUE;
1026 case SLAP_CONTROL_CRITICAL:
1027 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
1029 (void)fnc( op, rs );
1031 Debug( LDAP_DEBUG_ANY, "%s: "
1032 "critical control \"%s\" not supported.\n",
1033 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1037 /* handle all cases! */
1044 static int parseDontUseCopy (
1049 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
1050 rs->sr_text = "dontUseCopy control specified multiple times";
1051 return LDAP_PROTOCOL_ERROR;
1054 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1055 rs->sr_text = "dontUseCopy control value not absent";
1056 return LDAP_PROTOCOL_ERROR;
1059 if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
1060 && !ctrl->ldctl_iscritical )
1062 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
1063 return LDAP_PROTOCOL_ERROR;
1066 op->o_dontUseCopy = ctrl->ldctl_iscritical
1067 ? SLAP_CONTROL_CRITICAL
1068 : SLAP_CONTROL_NONCRITICAL;
1070 return LDAP_SUCCESS;
1073 static int parseRelax (
1078 if ( op->o_relax != SLAP_CONTROL_NONE ) {
1079 rs->sr_text = "relax control specified multiple times";
1080 return LDAP_PROTOCOL_ERROR;
1083 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1084 rs->sr_text = "relax control value not absent";
1085 return LDAP_PROTOCOL_ERROR;
1088 op->o_relax = ctrl->ldctl_iscritical
1089 ? SLAP_CONTROL_CRITICAL
1090 : SLAP_CONTROL_NONCRITICAL;
1092 return LDAP_SUCCESS;
1095 static int parseManageDSAit (
1100 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1101 rs->sr_text = "manageDSAit control specified multiple times";
1102 return LDAP_PROTOCOL_ERROR;
1105 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1106 rs->sr_text = "manageDSAit control value not absent";
1107 return LDAP_PROTOCOL_ERROR;
1110 op->o_managedsait = ctrl->ldctl_iscritical
1111 ? SLAP_CONTROL_CRITICAL
1112 : SLAP_CONTROL_NONCRITICAL;
1114 return LDAP_SUCCESS;
1117 static int parseProxyAuthz (
1123 struct berval dn = BER_BVNULL;
1125 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1126 rs->sr_text = "proxy authorization control specified multiple times";
1127 return LDAP_PROTOCOL_ERROR;
1130 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1131 rs->sr_text = "proxy authorization control value absent";
1132 return LDAP_PROTOCOL_ERROR;
1135 if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1136 && !ctrl->ldctl_iscritical )
1138 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1139 return LDAP_PROTOCOL_ERROR;
1142 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1143 && BER_BVISEMPTY( &op->o_ndn ) )
1145 rs->sr_text = "anonymous proxied authorization not allowed";
1146 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1149 op->o_proxy_authz = ctrl->ldctl_iscritical
1150 ? SLAP_CONTROL_CRITICAL
1151 : SLAP_CONTROL_NONCRITICAL;
1153 Debug( LDAP_DEBUG_ARGS,
1154 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
1156 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
1159 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1160 Debug( LDAP_DEBUG_TRACE,
1161 "parseProxyAuthz: conn=%lu anonymous\n",
1162 op->o_connid, 0, 0 );
1165 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1166 op->o_ndn.bv_val[ 0 ] = '\0';
1168 op->o_ndn.bv_len = 0;
1170 if ( !BER_BVISNULL( &op->o_dn ) ) {
1171 op->o_dn.bv_val[ 0 ] = '\0';
1173 op->o_dn.bv_len = 0;
1175 return LDAP_SUCCESS;
1178 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1179 NULL, &dn, SLAP_GETDN_AUTHZID );
1181 /* FIXME: empty DN in proxyAuthz control should be legal... */
1182 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1184 ch_free( dn.bv_val );
1186 rs->sr_text = "authzId mapping failed";
1187 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1190 Debug( LDAP_DEBUG_TRACE,
1191 "parseProxyAuthz: conn=%lu \"%s\"\n",
1193 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1195 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1198 ch_free( dn.bv_val );
1199 rs->sr_text = "not authorized to assume identity";
1200 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1203 ch_free( op->o_ndn.bv_val );
1206 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1207 * from now on op->o_dn is normalized
1210 ber_bvreplace( &op->o_dn, &dn );
1212 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1213 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1215 return LDAP_SUCCESS;
1218 static int parseNoOp (
1223 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1224 rs->sr_text = "noop control specified multiple times";
1225 return LDAP_PROTOCOL_ERROR;
1228 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1229 rs->sr_text = "noop control value not empty";
1230 return LDAP_PROTOCOL_ERROR;
1233 op->o_noop = ctrl->ldctl_iscritical
1234 ? SLAP_CONTROL_CRITICAL
1235 : SLAP_CONTROL_NONCRITICAL;
1237 return LDAP_SUCCESS;
1240 static int parsePagedResults (
1245 BerElementBuffer berbuf;
1246 BerElement *ber = (BerElement *)&berbuf;
1247 struct berval cookie;
1248 PagedResultsState *ps;
1249 int rc = LDAP_SUCCESS;
1253 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1254 rs->sr_text = "paged results control specified multiple times";
1255 return LDAP_PROTOCOL_ERROR;
1258 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1259 rs->sr_text = "paged results control value is absent";
1260 return LDAP_PROTOCOL_ERROR;
1263 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1264 rs->sr_text = "paged results control value is empty";
1265 return LDAP_PROTOCOL_ERROR;
1268 /* Parse the control value
1269 * realSearchControlValue ::= SEQUENCE {
1270 * size INTEGER (0..maxInt),
1271 * -- requested page size from client
1272 * -- result set size estimate from server
1273 * cookie OCTET STRING
1276 ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1278 tag = ber_scanf( ber, "{im}", &size, &cookie );
1280 if ( tag == LBER_ERROR ) {
1281 rs->sr_text = "paged results control could not be decoded";
1282 rc = LDAP_PROTOCOL_ERROR;
1287 rs->sr_text = "paged results control size invalid";
1288 rc = LDAP_PROTOCOL_ERROR;
1292 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1293 *ps = op->o_conn->c_pagedresults_state;
1295 ps->ps_cookieval = cookie;
1296 op->o_pagedresults_state = ps;
1297 if ( !cookie.bv_len ) {
1300 /* taint ps_cookie, to detect whether it's set */
1301 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1304 /* NOTE: according to RFC 2696 3.:
1306 If the page size is greater than or equal to the sizeLimit value, the
1307 server should ignore the control as the request can be satisfied in a
1310 * NOTE: this assumes that the op->ors_slimit be set
1311 * before the controls are parsed.
1314 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1315 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1317 } else if ( ctrl->ldctl_iscritical ) {
1318 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1321 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1328 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1329 static int parseSortedResults (
1334 int rc = LDAP_SUCCESS;
1336 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1337 rs->sr_text = "sorted results control specified multiple times";
1338 return LDAP_PROTOCOL_ERROR;
1341 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1342 rs->sr_text = "sorted results control value is absent";
1343 return LDAP_PROTOCOL_ERROR;
1346 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1347 rs->sr_text = "sorted results control value is empty";
1348 return LDAP_PROTOCOL_ERROR;
1351 /* blow off parsing the value */
1353 op->o_sortedresults = ctrl->ldctl_iscritical
1354 ? SLAP_CONTROL_CRITICAL
1355 : SLAP_CONTROL_NONCRITICAL;
1361 static int parseAssert (
1367 struct berval fstr = BER_BVNULL;
1369 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1370 rs->sr_text = "assert control specified multiple times";
1371 return LDAP_PROTOCOL_ERROR;
1374 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1375 rs->sr_text = "assert control value is absent";
1376 return LDAP_PROTOCOL_ERROR;
1379 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1380 rs->sr_text = "assert control value is empty";
1381 return LDAP_PROTOCOL_ERROR;
1384 ber = ber_init( &(ctrl->ldctl_value) );
1386 rs->sr_text = "assert control: internal error";
1390 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1392 (void) ber_free( ber, 1 );
1393 if( rs->sr_err != LDAP_SUCCESS ) {
1394 if( rs->sr_err == SLAPD_DISCONNECT ) {
1395 rs->sr_err = LDAP_PROTOCOL_ERROR;
1396 send_ldap_disconnect( op, rs );
1397 rs->sr_err = SLAPD_DISCONNECT;
1399 send_ldap_result( op, rs );
1401 if( op->o_assertion != NULL ) {
1402 filter_free_x( op, op->o_assertion, 1 );
1408 filter2bv_x( op, op->o_assertion, &fstr );
1410 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1411 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1412 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1415 op->o_assert = ctrl->ldctl_iscritical
1416 ? SLAP_CONTROL_CRITICAL
1417 : SLAP_CONTROL_NONCRITICAL;
1419 rs->sr_err = LDAP_SUCCESS;
1420 return LDAP_SUCCESS;
1423 #define READMSG(post, msg) \
1424 ( post ? "postread control: " msg : "preread control: " msg )
1433 ber_len_t siz, off, i;
1435 AttributeName *an = NULL;
1437 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1438 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1440 rs->sr_text = READMSG( post, "specified multiple times" );
1441 return LDAP_PROTOCOL_ERROR;
1444 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1445 rs->sr_text = READMSG( post, "value is absent" );
1446 return LDAP_PROTOCOL_ERROR;
1449 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1450 rs->sr_text = READMSG( post, "value is empty" );
1451 return LDAP_PROTOCOL_ERROR;
1455 if ( op->o_txnSpec ) { /* temporary limitation */
1456 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1457 return LDAP_UNWILLING_TO_PERFORM;
1461 ber = ber_init( &ctrl->ldctl_value );
1462 if ( ber == NULL ) {
1463 rs->sr_text = READMSG( post, "internal error" );
1467 rs->sr_err = LDAP_SUCCESS;
1468 siz = sizeof( AttributeName );
1469 off = offsetof( AttributeName, an_name );
1470 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1471 rs->sr_text = READMSG( post, "decoding error" );
1472 rs->sr_err = LDAP_PROTOCOL_ERROR;
1476 for ( i = 0; i < siz; i++ ) {
1477 const char *dummy = NULL;
1480 an[i].an_desc = NULL;
1483 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1484 if ( rc == LDAP_SUCCESS ) {
1485 an[i].an_name = an[i].an_desc->ad_cname;
1489 static struct berval special_attrs[] = {
1490 BER_BVC( LDAP_NO_ATTRS ),
1491 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1492 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1496 /* deal with special attribute types */
1497 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1498 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1499 an[i].an_name = special_attrs[ j ];
1504 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1506 rs->sr_text = dummy ? dummy
1507 : READMSG( post, "unknown attributeType" );
1514 op->o_postread_attrs = an;
1515 op->o_postread = ctrl->ldctl_iscritical
1516 ? SLAP_CONTROL_CRITICAL
1517 : SLAP_CONTROL_NONCRITICAL;
1519 op->o_preread_attrs = an;
1520 op->o_preread = ctrl->ldctl_iscritical
1521 ? SLAP_CONTROL_CRITICAL
1522 : SLAP_CONTROL_NONCRITICAL;
1526 (void) ber_free( ber, 1 );
1530 static int parsePreRead (
1535 return parseReadAttrs( op, rs, ctrl, 0 );
1538 static int parsePostRead (
1543 return parseReadAttrs( op, rs, ctrl, 1 );
1546 static int parseValuesReturnFilter (
1552 struct berval fstr = BER_BVNULL;
1554 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1555 rs->sr_text = "valuesReturnFilter control specified multiple times";
1556 return LDAP_PROTOCOL_ERROR;
1559 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1560 rs->sr_text = "valuesReturnFilter control value is absent";
1561 return LDAP_PROTOCOL_ERROR;
1564 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1565 rs->sr_text = "valuesReturnFilter control value is empty";
1566 return LDAP_PROTOCOL_ERROR;
1569 ber = ber_init( &(ctrl->ldctl_value) );
1571 rs->sr_text = "internal error";
1575 rs->sr_err = get_vrFilter( op, ber,
1576 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1578 (void) ber_free( ber, 1 );
1580 if( rs->sr_err != LDAP_SUCCESS ) {
1581 if( rs->sr_err == SLAPD_DISCONNECT ) {
1582 rs->sr_err = LDAP_PROTOCOL_ERROR;
1583 send_ldap_disconnect( op, rs );
1584 rs->sr_err = SLAPD_DISCONNECT;
1586 send_ldap_result( op, rs );
1588 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1592 vrFilter2bv( op, op->o_vrFilter, &fstr );
1595 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1596 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1597 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1600 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1601 ? SLAP_CONTROL_CRITICAL
1602 : SLAP_CONTROL_NONCRITICAL;
1604 rs->sr_err = LDAP_SUCCESS;
1605 return LDAP_SUCCESS;
1608 static int parseSubentries (
1613 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1614 rs->sr_text = "subentries control specified multiple times";
1615 return LDAP_PROTOCOL_ERROR;
1618 /* FIXME: should use BER library */
1619 if( ( ctrl->ldctl_value.bv_len != 3 )
1620 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1621 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1623 rs->sr_text = "subentries control value encoding is bogus";
1624 return LDAP_PROTOCOL_ERROR;
1627 op->o_subentries = ctrl->ldctl_iscritical
1628 ? SLAP_CONTROL_CRITICAL
1629 : SLAP_CONTROL_NONCRITICAL;
1631 if (ctrl->ldctl_value.bv_val[2]) {
1632 set_subentries_visibility( op );
1635 return LDAP_SUCCESS;
1638 static int parsePermissiveModify (
1643 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1644 rs->sr_text = "permissiveModify control specified multiple times";
1645 return LDAP_PROTOCOL_ERROR;
1648 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1649 rs->sr_text = "permissiveModify control value not absent";
1650 return LDAP_PROTOCOL_ERROR;
1653 op->o_permissive_modify = ctrl->ldctl_iscritical
1654 ? SLAP_CONTROL_CRITICAL
1655 : SLAP_CONTROL_NONCRITICAL;
1657 return LDAP_SUCCESS;
1660 static int parseDomainScope (
1665 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1666 rs->sr_text = "domainScope control specified multiple times";
1667 return LDAP_PROTOCOL_ERROR;
1670 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1671 rs->sr_text = "domainScope control value not empty";
1672 return LDAP_PROTOCOL_ERROR;
1675 op->o_domain_scope = ctrl->ldctl_iscritical
1676 ? SLAP_CONTROL_CRITICAL
1677 : SLAP_CONTROL_NONCRITICAL;
1679 return LDAP_SUCCESS;
1682 #ifdef SLAP_CONTROL_X_TREE_DELETE
1683 static int parseTreeDelete (
1688 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1689 rs->sr_text = "treeDelete control specified multiple times";
1690 return LDAP_PROTOCOL_ERROR;
1693 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1694 rs->sr_text = "treeDelete control value not absent";
1695 return LDAP_PROTOCOL_ERROR;
1698 op->o_tree_delete = ctrl->ldctl_iscritical
1699 ? SLAP_CONTROL_CRITICAL
1700 : SLAP_CONTROL_NONCRITICAL;
1702 return LDAP_SUCCESS;
1706 static int parseSearchOptions (
1712 ber_int_t search_flags;
1715 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1716 rs->sr_text = "searchOptions control value is absent";
1717 return LDAP_PROTOCOL_ERROR;
1720 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1721 rs->sr_text = "searchOptions control value is empty";
1722 return LDAP_PROTOCOL_ERROR;
1725 ber = ber_init( &ctrl->ldctl_value );
1727 rs->sr_text = "internal error";
1731 tag = ber_scanf( ber, "{i}", &search_flags );
1732 (void) ber_free( ber, 1 );
1734 if ( tag == LBER_ERROR ) {
1735 rs->sr_text = "searchOptions control decoding error";
1736 return LDAP_PROTOCOL_ERROR;
1739 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1740 /* Search flags not recognised so far,
1742 * LDAP_SEARCH_FLAG_PHANTOM_ROOT
1744 if ( ctrl->ldctl_iscritical ) {
1745 rs->sr_text = "searchOptions contained unrecognized flag";
1746 return LDAP_UNWILLING_TO_PERFORM;
1750 Debug( LDAP_DEBUG_TRACE,
1751 "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n",
1752 op->o_connid, (unsigned)search_flags, 0 );
1754 return LDAP_SUCCESS;
1757 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1758 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1759 rs->sr_text = "searchOptions control specified multiple times "
1760 "or with domainScope control";
1761 return LDAP_PROTOCOL_ERROR;
1764 op->o_domain_scope = ctrl->ldctl_iscritical
1765 ? SLAP_CONTROL_CRITICAL
1766 : SLAP_CONTROL_NONCRITICAL;
1769 return LDAP_SUCCESS;
1772 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1773 struct berval session_tracking_formats[] = {
1774 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1775 BER_BVC( "RADIUS-Acct-Session-Id" ),
1776 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1777 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1778 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1779 BER_BVC( "USERNAME" ),
1784 static int parseSessionTracking(
1794 struct berval sessionSourceIp = BER_BVNULL,
1795 sessionSourceName = BER_BVNULL,
1796 formatOID = BER_BVNULL,
1797 sessionTrackingIdentifier = BER_BVNULL;
1799 size_t st_len, st_pos;
1801 if ( ctrl->ldctl_iscritical ) {
1802 rs->sr_text = "sessionTracking criticality is TRUE";
1803 return LDAP_PROTOCOL_ERROR;
1806 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1807 rs->sr_text = "sessionTracking control value is absent";
1808 return LDAP_PROTOCOL_ERROR;
1811 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1812 rs->sr_text = "sessionTracking control value is empty";
1813 return LDAP_PROTOCOL_ERROR;
1816 /* TODO: add the capability to determine if a client is allowed
1817 * to use this control, based on identity, ip and so */
1819 ber = ber_init( &ctrl->ldctl_value );
1820 if ( ber == NULL ) {
1821 rs->sr_text = "internal error";
1825 tag = ber_skip_tag( ber, &len );
1826 if ( tag != LBER_SEQUENCE ) {
1831 /* sessionSourceIp */
1832 tag = ber_peek_tag( ber, &len );
1833 if ( tag == LBER_DEFAULT ) {
1839 tag = ber_skip_tag( ber, &len );
1841 } else if ( len > 128 ) {
1842 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1843 rs->sr_err = LDAP_PROTOCOL_ERROR;
1847 tag = ber_scanf( ber, "m", &sessionSourceIp );
1850 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1851 BER_BVZERO( &sessionSourceIp );
1854 /* sessionSourceName */
1855 tag = ber_peek_tag( ber, &len );
1856 if ( tag == LBER_DEFAULT ) {
1862 tag = ber_skip_tag( ber, &len );
1864 } else if ( len > 65536 ) {
1865 rs->sr_text = "sessionTracking.sessionSourceName too long";
1866 rs->sr_err = LDAP_PROTOCOL_ERROR;
1870 tag = ber_scanf( ber, "m", &sessionSourceName );
1873 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1874 BER_BVZERO( &sessionSourceName );
1878 tag = ber_peek_tag( ber, &len );
1879 if ( tag == LBER_DEFAULT ) {
1885 rs->sr_text = "sessionTracking.formatOID empty";
1886 rs->sr_err = LDAP_PROTOCOL_ERROR;
1889 } else if ( len > 1024 ) {
1890 rs->sr_text = "sessionTracking.formatOID too long";
1891 rs->sr_err = LDAP_PROTOCOL_ERROR;
1895 tag = ber_scanf( ber, "m", &formatOID );
1898 rc = numericoidValidate( NULL, &formatOID );
1899 if ( rc != LDAP_SUCCESS ) {
1900 rs->sr_text = "sessionTracking.formatOID invalid";
1904 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1906 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1907 formatOID = session_tracking_formats[ i + 1 ];
1912 /* sessionTrackingIdentifier */
1913 tag = ber_peek_tag( ber, &len );
1914 if ( tag == LBER_DEFAULT ) {
1920 tag = ber_skip_tag( ber, &len );
1923 /* note: should not be more than 65536... */
1924 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1925 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1926 /* we want the OID printed, at least */
1927 BER_BVSTR( &sessionTrackingIdentifier, "" );
1932 tag = ber_skip_tag( ber, &len );
1933 if ( tag != LBER_DEFAULT || len != 0 ) {
1940 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1941 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1943 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1944 if ( st_len ) st_len++;
1945 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1947 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1948 if ( st_len ) st_len++;
1949 st_len += formatOID.bv_len + STRLENOF( "=" )
1950 + sessionTrackingIdentifier.bv_len;
1953 if ( st_len == 0 ) {
1957 st_len += STRLENOF( " []" );
1958 st_pos = strlen( op->o_log_prefix );
1960 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1961 char *ptr = &op->o_log_prefix[ st_pos ];
1963 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1966 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1967 ptr = lutil_strcopy( ptr, "IP=" );
1968 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1972 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1973 if ( st_len ) *ptr++ = ' ';
1974 ptr = lutil_strcopy( ptr, "NAME=" );
1975 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1979 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1980 if ( st_len ) *ptr++ = ' ';
1981 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1983 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1991 (void)ber_free( ber, 1 );
1993 if ( tag == LBER_ERROR ) {
1994 rs->sr_text = "sessionTracking control decoding error";
1995 return LDAP_PROTOCOL_ERROR;
2003 slap_ctrl_session_tracking_add(
2007 struct berval *name,
2011 BerElementBuffer berbuf;
2012 BerElement *ber = (BerElement *)&berbuf;
2014 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
2016 assert( ctrl != NULL );
2018 ber_init2( ber, NULL, LBER_USE_DER );
2020 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
2022 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
2023 rs->sr_err = LDAP_OTHER;
2027 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
2028 ctrl->ldctl_iscritical = 0;
2030 rs->sr_err = LDAP_SUCCESS;
2037 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
2039 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
2040 struct berval ip = BER_BVNULL,
2044 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
2045 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
2049 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
2050 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
2052 ptr = ber_bvchr( &ip, ':' );
2054 ip.bv_len = ptr - ip.bv_val;
2058 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
2059 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
2061 name = op->o_conn->c_peer_domain;
2064 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2068 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2072 #ifdef SLAP_CONTROL_X_WHATFAILED
2073 static int parseWhatFailed(
2078 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2079 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2080 return LDAP_PROTOCOL_ERROR;
2083 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2084 rs->sr_text = "\"What Failed?\" control value not absent";
2085 return LDAP_PROTOCOL_ERROR;
2088 op->o_whatFailed = ctrl->ldctl_iscritical
2089 ? SLAP_CONTROL_CRITICAL
2090 : SLAP_CONTROL_NONCRITICAL;
2092 return LDAP_SUCCESS;
2096 slap_ctrl_whatFailed_add(
2101 BerElementBuffer berbuf;
2102 BerElement *ber = (BerElement *) &berbuf;
2103 LDAPControl **ctrls = NULL;
2104 struct berval ctrlval;
2105 int i, rc = LDAP_SUCCESS;
2107 ber_init2( ber, NULL, LBER_USE_DER );
2108 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2109 ber_printf( ber, "[" /*]*/ );
2110 for ( i = 0; oids[ i ] != NULL; i++ ) {
2111 ber_printf( ber, "s", oids[ i ] );
2113 ber_printf( ber, /*[*/ "]" );
2115 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2121 if ( rs->sr_ctrls != NULL ) {
2122 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2123 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2130 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2131 sizeof(LDAPControl *)*( i + 2 )
2132 + sizeof(LDAPControl)
2133 + ctrlval.bv_len + 1,
2135 if ( ctrls == NULL ) {
2139 ctrls[ i + 1 ] = NULL;
2140 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2141 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2142 ctrls[ i ]->ldctl_iscritical = 0;
2143 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2144 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2145 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2147 ber_free_buf( ber );
2149 rs->sr_ctrls = ctrls;