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|SLAP_CTRL_HIDE,
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;
723 BerElement *ber = op->o_ber;
725 #ifdef SLAP_CONTROL_X_WHATFAILED
726 /* NOTE: right now, slapd checks the validity of each control
727 * while parsing. As a consequence, it can only detect one
728 * cause of failure at a time. This results in returning
729 * exactly one OID with the whatFailed control, or no control
732 char *failed_oid = NULL;
735 len = ber_pvt_ber_remaining(ber);
739 rs->sr_err = LDAP_SUCCESS;
743 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
744 if( tag == LBER_ERROR ) {
745 rs->sr_err = SLAPD_DISCONNECT;
746 rs->sr_text = "unexpected data in PDU";
752 Debug( LDAP_DEBUG_TRACE,
753 "=> get_ctrls\n", 0, 0, 0 );
755 if( op->o_protocol < LDAP_VERSION3 ) {
756 rs->sr_err = SLAPD_DISCONNECT;
757 rs->sr_text = "controls require LDAPv3";
761 /* one for first control, one for termination */
762 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
765 if( op->ctrls == NULL ) {
766 rs->sr_err = LDAP_NO_MEMORY;
767 rs->sr_text = "no memory";
772 op->o_ctrls[nctrls] = NULL;
774 /* step through each element */
775 for( tag = ber_first_element( ber, &len, &opaque );
777 tag = ber_next_element( ber, &len, opaque ) )
780 LDAPControl **tctrls;
782 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
783 memset(c, 0, sizeof(LDAPControl));
785 /* allocate pointer space for current controls (nctrls)
786 * + this control + extra NULL
788 tctrls = op->o_tmprealloc( op->o_ctrls,
789 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
792 if( tctrls == NULL ) {
794 ldap_controls_free(op->o_ctrls);
797 rs->sr_err = LDAP_NO_MEMORY;
798 rs->sr_text = "no memory";
802 op->o_ctrls = tctrls;
804 op->o_ctrls[nctrls++] = c;
805 op->o_ctrls[nctrls] = NULL;
807 tag = ber_scanf( ber, "{m" /*}*/, &bv );
808 c->ldctl_oid = bv.bv_val;
810 if( tag == LBER_ERROR ) {
811 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
814 slap_free_ctrls( op, op->o_ctrls );
816 rs->sr_err = SLAPD_DISCONNECT;
817 rs->sr_text = "decoding controls error";
820 } else if( c->ldctl_oid == NULL ) {
821 Debug( LDAP_DEBUG_TRACE,
822 "get_ctrls: conn %lu got emtpy OID.\n",
823 op->o_connid, 0, 0 );
825 slap_free_ctrls( op, op->o_ctrls );
827 rs->sr_err = LDAP_PROTOCOL_ERROR;
828 rs->sr_text = "OID field is empty";
832 tag = ber_peek_tag( ber, &len );
834 if( tag == LBER_BOOLEAN ) {
836 tag = ber_scanf( ber, "b", &crit );
838 if( tag == LBER_ERROR ) {
839 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
841 slap_free_ctrls( op, op->o_ctrls );
843 rs->sr_err = SLAPD_DISCONNECT;
844 rs->sr_text = "decoding controls error";
848 c->ldctl_iscritical = (crit != 0);
849 tag = ber_peek_tag( ber, &len );
852 if( tag == LBER_OCTETSTRING ) {
853 tag = ber_scanf( ber, "m", &c->ldctl_value );
855 if( tag == LBER_ERROR ) {
856 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
857 "%s (%scritical): get value failed.\n",
858 op->o_connid, c->ldctl_oid,
859 c->ldctl_iscritical ? "" : "non" );
860 slap_free_ctrls( op, op->o_ctrls );
862 rs->sr_err = SLAPD_DISCONNECT;
863 rs->sr_text = "decoding controls error";
868 Debug( LDAP_DEBUG_TRACE,
869 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
870 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
872 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
873 if ( rs->sr_err != LDAP_SUCCESS ) {
874 #ifdef SLAP_CONTROL_X_WHATFAILED
875 failed_oid = c->ldctl_oid;
882 Debug( LDAP_DEBUG_TRACE,
883 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
884 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
886 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
887 if( rs->sr_err == SLAPD_DISCONNECT ) {
888 rs->sr_err = LDAP_PROTOCOL_ERROR;
889 send_ldap_disconnect( op, rs );
890 rs->sr_err = SLAPD_DISCONNECT;
892 #ifdef SLAP_CONTROL_X_WHATFAILED
893 /* might have not been parsed yet? */
894 if ( failed_oid != NULL ) {
895 if ( !get_whatFailed( op ) ) {
898 /* step through each remaining element */
899 for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
901 LDAPControl c = { 0 };
903 tag = ber_scanf( ber, "{m" /*}*/, &bv );
904 c.ldctl_oid = bv.bv_val;
906 if ( tag == LBER_ERROR ) {
907 slap_free_ctrls( op, op->o_ctrls );
911 } else if ( c.ldctl_oid == NULL ) {
912 slap_free_ctrls( op, op->o_ctrls );
917 tag = ber_peek_tag( ber, &len );
918 if ( tag == LBER_BOOLEAN ) {
920 tag = ber_scanf( ber, "b", &crit );
921 if( tag == LBER_ERROR ) {
922 slap_free_ctrls( op, op->o_ctrls );
927 tag = ber_peek_tag( ber, &len );
930 if ( tag == LBER_OCTETSTRING ) {
931 tag = ber_scanf( ber, "m", &c.ldctl_value );
933 if( tag == LBER_ERROR ) {
934 slap_free_ctrls( op, op->o_ctrls );
940 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
942 slap_parse_ctrl( op, rs, &c, &text );
948 if ( get_whatFailed( op ) ) {
950 oids[ 0 ] = failed_oid;
952 slap_ctrl_whatFailed_add( op, rs, oids );
957 send_ldap_result( op, rs );
969 BI_chk_controls fnc )
973 switch ( op->o_ctrlflag[ ctrl ] ) {
974 case SLAP_CONTROL_NONCRITICAL:
975 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
976 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
977 slap_known_controls[ ctrl - 1 ] ) == 0 )
984 rs->sr_err = LDAP_OTHER;
992 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
995 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
996 ( i - j ) * sizeof( LDAPControl * ) );
999 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
1003 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
1005 Debug( LDAP_DEBUG_ANY, "%s: "
1006 "non-critical control \"%s\" not supported; stripped.\n",
1007 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1010 case SLAP_CONTROL_IGNORED:
1011 case SLAP_CONTROL_NONE:
1012 rs->sr_err = SLAP_CB_CONTINUE;
1015 case SLAP_CONTROL_CRITICAL:
1016 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
1018 (void)fnc( op, rs );
1020 Debug( LDAP_DEBUG_ANY, "%s: "
1021 "critical control \"%s\" not supported.\n",
1022 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1026 /* handle all cases! */
1033 static int parseDontUseCopy (
1038 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
1039 rs->sr_text = "dontUseCopy control specified multiple times";
1040 return LDAP_PROTOCOL_ERROR;
1043 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1044 rs->sr_text = "dontUseCopy control value not absent";
1045 return LDAP_PROTOCOL_ERROR;
1048 if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
1049 && !ctrl->ldctl_iscritical )
1051 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
1052 return LDAP_PROTOCOL_ERROR;
1055 op->o_dontUseCopy = ctrl->ldctl_iscritical
1056 ? SLAP_CONTROL_CRITICAL
1057 : SLAP_CONTROL_NONCRITICAL;
1059 return LDAP_SUCCESS;
1062 static int parseRelax (
1067 if ( op->o_relax != SLAP_CONTROL_NONE ) {
1068 rs->sr_text = "relax control specified multiple times";
1069 return LDAP_PROTOCOL_ERROR;
1072 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1073 rs->sr_text = "relax control value not absent";
1074 return LDAP_PROTOCOL_ERROR;
1077 op->o_relax = ctrl->ldctl_iscritical
1078 ? SLAP_CONTROL_CRITICAL
1079 : SLAP_CONTROL_NONCRITICAL;
1081 return LDAP_SUCCESS;
1084 static int parseManageDSAit (
1089 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1090 rs->sr_text = "manageDSAit control specified multiple times";
1091 return LDAP_PROTOCOL_ERROR;
1094 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1095 rs->sr_text = "manageDSAit control value not absent";
1096 return LDAP_PROTOCOL_ERROR;
1099 op->o_managedsait = ctrl->ldctl_iscritical
1100 ? SLAP_CONTROL_CRITICAL
1101 : SLAP_CONTROL_NONCRITICAL;
1103 return LDAP_SUCCESS;
1106 static int parseProxyAuthz (
1112 struct berval dn = BER_BVNULL;
1114 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1115 rs->sr_text = "proxy authorization control specified multiple times";
1116 return LDAP_PROTOCOL_ERROR;
1119 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1120 rs->sr_text = "proxy authorization control value absent";
1121 return LDAP_PROTOCOL_ERROR;
1124 if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1125 && !ctrl->ldctl_iscritical )
1127 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1128 return LDAP_PROTOCOL_ERROR;
1131 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1132 && BER_BVISEMPTY( &op->o_ndn ) )
1134 rs->sr_text = "anonymous proxied authorization not allowed";
1135 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1138 op->o_proxy_authz = ctrl->ldctl_iscritical
1139 ? SLAP_CONTROL_CRITICAL
1140 : SLAP_CONTROL_NONCRITICAL;
1142 Debug( LDAP_DEBUG_ARGS,
1143 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
1145 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
1148 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1149 Debug( LDAP_DEBUG_TRACE,
1150 "parseProxyAuthz: conn=%lu anonymous\n",
1151 op->o_connid, 0, 0 );
1154 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1155 op->o_ndn.bv_val[ 0 ] = '\0';
1157 op->o_ndn.bv_len = 0;
1159 if ( !BER_BVISNULL( &op->o_dn ) ) {
1160 op->o_dn.bv_val[ 0 ] = '\0';
1162 op->o_dn.bv_len = 0;
1164 return LDAP_SUCCESS;
1167 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1168 NULL, &dn, SLAP_GETDN_AUTHZID );
1170 /* FIXME: empty DN in proxyAuthz control should be legal... */
1171 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1173 ch_free( dn.bv_val );
1175 rs->sr_text = "authzId mapping failed";
1176 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1179 Debug( LDAP_DEBUG_TRACE,
1180 "parseProxyAuthz: conn=%lu \"%s\"\n",
1182 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1184 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1187 ch_free( dn.bv_val );
1188 rs->sr_text = "not authorized to assume identity";
1189 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1192 ch_free( op->o_ndn.bv_val );
1195 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1196 * from now on op->o_dn is normalized
1199 ber_bvreplace( &op->o_dn, &dn );
1201 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1202 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1204 return LDAP_SUCCESS;
1207 static int parseNoOp (
1212 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1213 rs->sr_text = "noop control specified multiple times";
1214 return LDAP_PROTOCOL_ERROR;
1217 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1218 rs->sr_text = "noop control value not empty";
1219 return LDAP_PROTOCOL_ERROR;
1222 op->o_noop = ctrl->ldctl_iscritical
1223 ? SLAP_CONTROL_CRITICAL
1224 : SLAP_CONTROL_NONCRITICAL;
1226 return LDAP_SUCCESS;
1229 static int parsePagedResults (
1234 BerElementBuffer berbuf;
1235 BerElement *ber = (BerElement *)&berbuf;
1236 struct berval cookie;
1237 PagedResultsState *ps;
1238 int rc = LDAP_SUCCESS;
1242 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1243 rs->sr_text = "paged results control specified multiple times";
1244 return LDAP_PROTOCOL_ERROR;
1247 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1248 rs->sr_text = "paged results control value is absent";
1249 return LDAP_PROTOCOL_ERROR;
1252 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1253 rs->sr_text = "paged results control value is empty";
1254 return LDAP_PROTOCOL_ERROR;
1257 /* Parse the control value
1258 * realSearchControlValue ::= SEQUENCE {
1259 * size INTEGER (0..maxInt),
1260 * -- requested page size from client
1261 * -- result set size estimate from server
1262 * cookie OCTET STRING
1265 ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1267 tag = ber_scanf( ber, "{im}", &size, &cookie );
1269 if ( tag == LBER_ERROR ) {
1270 rs->sr_text = "paged results control could not be decoded";
1271 rc = LDAP_PROTOCOL_ERROR;
1276 rs->sr_text = "paged results control size invalid";
1277 rc = LDAP_PROTOCOL_ERROR;
1281 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1282 *ps = op->o_conn->c_pagedresults_state;
1284 ps->ps_cookieval = cookie;
1285 op->o_pagedresults_state = ps;
1286 if ( !cookie.bv_len ) {
1289 /* taint ps_cookie, to detect whether it's set */
1290 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1293 /* NOTE: according to RFC 2696 3.:
1295 If the page size is greater than or equal to the sizeLimit value, the
1296 server should ignore the control as the request can be satisfied in a
1299 * NOTE: this assumes that the op->ors_slimit be set
1300 * before the controls are parsed.
1303 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1304 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1306 } else if ( ctrl->ldctl_iscritical ) {
1307 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1310 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1317 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1318 static int parseSortedResults (
1323 int rc = LDAP_SUCCESS;
1325 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1326 rs->sr_text = "sorted results control specified multiple times";
1327 return LDAP_PROTOCOL_ERROR;
1330 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1331 rs->sr_text = "sorted results control value is absent";
1332 return LDAP_PROTOCOL_ERROR;
1335 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1336 rs->sr_text = "sorted results control value is empty";
1337 return LDAP_PROTOCOL_ERROR;
1340 /* blow off parsing the value */
1342 op->o_sortedresults = ctrl->ldctl_iscritical
1343 ? SLAP_CONTROL_CRITICAL
1344 : SLAP_CONTROL_NONCRITICAL;
1350 static int parseAssert (
1356 struct berval fstr = BER_BVNULL;
1358 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1359 rs->sr_text = "assert control specified multiple times";
1360 return LDAP_PROTOCOL_ERROR;
1363 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1364 rs->sr_text = "assert control value is absent";
1365 return LDAP_PROTOCOL_ERROR;
1368 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1369 rs->sr_text = "assert control value is empty";
1370 return LDAP_PROTOCOL_ERROR;
1373 ber = ber_init( &(ctrl->ldctl_value) );
1375 rs->sr_text = "assert control: internal error";
1379 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1381 (void) ber_free( ber, 1 );
1382 if( rs->sr_err != LDAP_SUCCESS ) {
1383 if( rs->sr_err == SLAPD_DISCONNECT ) {
1384 rs->sr_err = LDAP_PROTOCOL_ERROR;
1385 send_ldap_disconnect( op, rs );
1386 rs->sr_err = SLAPD_DISCONNECT;
1388 send_ldap_result( op, rs );
1390 if( op->o_assertion != NULL ) {
1391 filter_free_x( op, op->o_assertion, 1 );
1397 filter2bv_x( op, op->o_assertion, &fstr );
1399 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1400 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1401 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1404 op->o_assert = ctrl->ldctl_iscritical
1405 ? SLAP_CONTROL_CRITICAL
1406 : SLAP_CONTROL_NONCRITICAL;
1408 rs->sr_err = LDAP_SUCCESS;
1409 return LDAP_SUCCESS;
1412 #define READMSG(post, msg) \
1413 ( post ? "postread control: " msg : "preread control: " msg )
1422 ber_len_t siz, off, i;
1424 AttributeName *an = NULL;
1426 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1427 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1429 rs->sr_text = READMSG( post, "specified multiple times" );
1430 return LDAP_PROTOCOL_ERROR;
1433 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1434 rs->sr_text = READMSG( post, "value is absent" );
1435 return LDAP_PROTOCOL_ERROR;
1438 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1439 rs->sr_text = READMSG( post, "value is empty" );
1440 return LDAP_PROTOCOL_ERROR;
1444 if ( op->o_txnSpec ) { /* temporary limitation */
1445 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1446 return LDAP_UNWILLING_TO_PERFORM;
1450 ber = ber_init( &ctrl->ldctl_value );
1451 if ( ber == NULL ) {
1452 rs->sr_text = READMSG( post, "internal error" );
1456 rs->sr_err = LDAP_SUCCESS;
1457 siz = sizeof( AttributeName );
1458 off = offsetof( AttributeName, an_name );
1459 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1460 rs->sr_text = READMSG( post, "decoding error" );
1461 rs->sr_err = LDAP_PROTOCOL_ERROR;
1465 for ( i = 0; i < siz; i++ ) {
1466 const char *dummy = NULL;
1469 an[i].an_desc = NULL;
1472 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1473 if ( rc == LDAP_SUCCESS ) {
1474 an[i].an_name = an[i].an_desc->ad_cname;
1478 static struct berval special_attrs[] = {
1479 BER_BVC( LDAP_NO_ATTRS ),
1480 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1481 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1485 /* deal with special attribute types */
1486 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1487 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1488 an[i].an_name = special_attrs[ j ];
1493 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1495 rs->sr_text = dummy ? dummy
1496 : READMSG( post, "unknown attributeType" );
1503 op->o_postread_attrs = an;
1504 op->o_postread = ctrl->ldctl_iscritical
1505 ? SLAP_CONTROL_CRITICAL
1506 : SLAP_CONTROL_NONCRITICAL;
1508 op->o_preread_attrs = an;
1509 op->o_preread = ctrl->ldctl_iscritical
1510 ? SLAP_CONTROL_CRITICAL
1511 : SLAP_CONTROL_NONCRITICAL;
1515 (void) ber_free( ber, 1 );
1519 static int parsePreRead (
1524 return parseReadAttrs( op, rs, ctrl, 0 );
1527 static int parsePostRead (
1532 return parseReadAttrs( op, rs, ctrl, 1 );
1535 static int parseValuesReturnFilter (
1541 struct berval fstr = BER_BVNULL;
1543 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1544 rs->sr_text = "valuesReturnFilter control specified multiple times";
1545 return LDAP_PROTOCOL_ERROR;
1548 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1549 rs->sr_text = "valuesReturnFilter control value is absent";
1550 return LDAP_PROTOCOL_ERROR;
1553 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1554 rs->sr_text = "valuesReturnFilter control value is empty";
1555 return LDAP_PROTOCOL_ERROR;
1558 ber = ber_init( &(ctrl->ldctl_value) );
1560 rs->sr_text = "internal error";
1564 rs->sr_err = get_vrFilter( op, ber,
1565 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1567 (void) ber_free( ber, 1 );
1569 if( rs->sr_err != LDAP_SUCCESS ) {
1570 if( rs->sr_err == SLAPD_DISCONNECT ) {
1571 rs->sr_err = LDAP_PROTOCOL_ERROR;
1572 send_ldap_disconnect( op, rs );
1573 rs->sr_err = SLAPD_DISCONNECT;
1575 send_ldap_result( op, rs );
1577 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1581 vrFilter2bv( op, op->o_vrFilter, &fstr );
1584 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1585 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1586 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1589 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1590 ? SLAP_CONTROL_CRITICAL
1591 : SLAP_CONTROL_NONCRITICAL;
1593 rs->sr_err = LDAP_SUCCESS;
1594 return LDAP_SUCCESS;
1597 static int parseSubentries (
1602 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1603 rs->sr_text = "subentries control specified multiple times";
1604 return LDAP_PROTOCOL_ERROR;
1607 /* FIXME: should use BER library */
1608 if( ( ctrl->ldctl_value.bv_len != 3 )
1609 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1610 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1612 rs->sr_text = "subentries control value encoding is bogus";
1613 return LDAP_PROTOCOL_ERROR;
1616 op->o_subentries = ctrl->ldctl_iscritical
1617 ? SLAP_CONTROL_CRITICAL
1618 : SLAP_CONTROL_NONCRITICAL;
1620 if (ctrl->ldctl_value.bv_val[2]) {
1621 set_subentries_visibility( op );
1624 return LDAP_SUCCESS;
1627 static int parsePermissiveModify (
1632 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1633 rs->sr_text = "permissiveModify control specified multiple times";
1634 return LDAP_PROTOCOL_ERROR;
1637 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1638 rs->sr_text = "permissiveModify control value not absent";
1639 return LDAP_PROTOCOL_ERROR;
1642 op->o_permissive_modify = ctrl->ldctl_iscritical
1643 ? SLAP_CONTROL_CRITICAL
1644 : SLAP_CONTROL_NONCRITICAL;
1646 return LDAP_SUCCESS;
1649 static int parseDomainScope (
1654 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1655 rs->sr_text = "domainScope control specified multiple times";
1656 return LDAP_PROTOCOL_ERROR;
1659 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1660 rs->sr_text = "domainScope control value not empty";
1661 return LDAP_PROTOCOL_ERROR;
1664 op->o_domain_scope = ctrl->ldctl_iscritical
1665 ? SLAP_CONTROL_CRITICAL
1666 : SLAP_CONTROL_NONCRITICAL;
1668 return LDAP_SUCCESS;
1671 #ifdef SLAP_CONTROL_X_TREE_DELETE
1672 static int parseTreeDelete (
1677 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1678 rs->sr_text = "treeDelete control specified multiple times";
1679 return LDAP_PROTOCOL_ERROR;
1682 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1683 rs->sr_text = "treeDelete control value not absent";
1684 return LDAP_PROTOCOL_ERROR;
1687 op->o_tree_delete = ctrl->ldctl_iscritical
1688 ? SLAP_CONTROL_CRITICAL
1689 : SLAP_CONTROL_NONCRITICAL;
1691 return LDAP_SUCCESS;
1695 static int parseSearchOptions (
1701 ber_int_t search_flags;
1704 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1705 rs->sr_text = "searchOptions control value is absent";
1706 return LDAP_PROTOCOL_ERROR;
1709 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1710 rs->sr_text = "searchOptions control value is empty";
1711 return LDAP_PROTOCOL_ERROR;
1714 ber = ber_init( &ctrl->ldctl_value );
1716 rs->sr_text = "internal error";
1720 tag = ber_scanf( ber, "{i}", &search_flags );
1721 (void) ber_free( ber, 1 );
1723 if ( tag == LBER_ERROR ) {
1724 rs->sr_text = "searchOptions control decoding error";
1725 return LDAP_PROTOCOL_ERROR;
1728 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1729 /* Search flags not recognised so far,
1731 * LDAP_SEARCH_FLAG_PHANTOM_ROOT
1733 if ( ctrl->ldctl_iscritical ) {
1734 rs->sr_text = "searchOptions contained unrecognized flag";
1735 return LDAP_UNWILLING_TO_PERFORM;
1739 Debug( LDAP_DEBUG_TRACE,
1740 "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n",
1741 op->o_connid, (unsigned)search_flags, 0 );
1743 return LDAP_SUCCESS;
1746 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1747 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1748 rs->sr_text = "searchOptions control specified multiple times "
1749 "or with domainScope control";
1750 return LDAP_PROTOCOL_ERROR;
1753 op->o_domain_scope = ctrl->ldctl_iscritical
1754 ? SLAP_CONTROL_CRITICAL
1755 : SLAP_CONTROL_NONCRITICAL;
1758 return LDAP_SUCCESS;
1761 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1762 struct berval session_tracking_formats[] = {
1763 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1764 BER_BVC( "RADIUS-Acct-Session-Id" ),
1765 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1766 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1767 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1768 BER_BVC( "USERNAME" ),
1773 static int parseSessionTracking(
1783 struct berval sessionSourceIp = BER_BVNULL,
1784 sessionSourceName = BER_BVNULL,
1785 formatOID = BER_BVNULL,
1786 sessionTrackingIdentifier = BER_BVNULL;
1788 size_t st_len, st_pos;
1790 if ( ctrl->ldctl_iscritical ) {
1791 rs->sr_text = "sessionTracking criticality is TRUE";
1792 return LDAP_PROTOCOL_ERROR;
1795 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1796 rs->sr_text = "sessionTracking control value is absent";
1797 return LDAP_PROTOCOL_ERROR;
1800 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1801 rs->sr_text = "sessionTracking control value is empty";
1802 return LDAP_PROTOCOL_ERROR;
1805 /* TODO: add the capability to determine if a client is allowed
1806 * to use this control, based on identity, ip and so */
1808 ber = ber_init( &ctrl->ldctl_value );
1809 if ( ber == NULL ) {
1810 rs->sr_text = "internal error";
1814 tag = ber_skip_tag( ber, &len );
1815 if ( tag != LBER_SEQUENCE ) {
1820 /* sessionSourceIp */
1821 tag = ber_peek_tag( ber, &len );
1822 if ( tag == LBER_DEFAULT ) {
1828 tag = ber_skip_tag( ber, &len );
1830 } else if ( len > 128 ) {
1831 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1832 rs->sr_err = LDAP_PROTOCOL_ERROR;
1836 tag = ber_scanf( ber, "m", &sessionSourceIp );
1839 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1840 BER_BVZERO( &sessionSourceIp );
1843 /* sessionSourceName */
1844 tag = ber_peek_tag( ber, &len );
1845 if ( tag == LBER_DEFAULT ) {
1851 tag = ber_skip_tag( ber, &len );
1853 } else if ( len > 65536 ) {
1854 rs->sr_text = "sessionTracking.sessionSourceName too long";
1855 rs->sr_err = LDAP_PROTOCOL_ERROR;
1859 tag = ber_scanf( ber, "m", &sessionSourceName );
1862 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1863 BER_BVZERO( &sessionSourceName );
1867 tag = ber_peek_tag( ber, &len );
1868 if ( tag == LBER_DEFAULT ) {
1874 rs->sr_text = "sessionTracking.formatOID empty";
1875 rs->sr_err = LDAP_PROTOCOL_ERROR;
1878 } else if ( len > 1024 ) {
1879 rs->sr_text = "sessionTracking.formatOID too long";
1880 rs->sr_err = LDAP_PROTOCOL_ERROR;
1884 tag = ber_scanf( ber, "m", &formatOID );
1887 rc = numericoidValidate( NULL, &formatOID );
1888 if ( rc != LDAP_SUCCESS ) {
1889 rs->sr_text = "sessionTracking.formatOID invalid";
1893 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1895 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1896 formatOID = session_tracking_formats[ i + 1 ];
1901 /* sessionTrackingIdentifier */
1902 tag = ber_peek_tag( ber, &len );
1903 if ( tag == LBER_DEFAULT ) {
1909 tag = ber_skip_tag( ber, &len );
1912 /* note: should not be more than 65536... */
1913 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1914 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1915 /* we want the OID printed, at least */
1916 BER_BVSTR( &sessionTrackingIdentifier, "" );
1921 tag = ber_skip_tag( ber, &len );
1922 if ( tag != LBER_DEFAULT || len != 0 ) {
1929 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1930 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1932 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1933 if ( st_len ) st_len++;
1934 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1936 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1937 if ( st_len ) st_len++;
1938 st_len += formatOID.bv_len + STRLENOF( "=" )
1939 + sessionTrackingIdentifier.bv_len;
1942 if ( st_len == 0 ) {
1946 st_len += STRLENOF( " []" );
1947 st_pos = strlen( op->o_log_prefix );
1949 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1950 char *ptr = &op->o_log_prefix[ st_pos ];
1952 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1955 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1956 ptr = lutil_strcopy( ptr, "IP=" );
1957 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1961 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1962 if ( st_len ) *ptr++ = ' ';
1963 ptr = lutil_strcopy( ptr, "NAME=" );
1964 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1968 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1969 if ( st_len ) *ptr++ = ' ';
1970 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1972 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1980 (void)ber_free( ber, 1 );
1982 if ( tag == LBER_ERROR ) {
1983 rs->sr_text = "sessionTracking control decoding error";
1984 return LDAP_PROTOCOL_ERROR;
1992 slap_ctrl_session_tracking_add(
1996 struct berval *name,
2000 BerElementBuffer berbuf;
2001 BerElement *ber = (BerElement *)&berbuf;
2003 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
2005 assert( ctrl != NULL );
2007 ber_init2( ber, NULL, LBER_USE_DER );
2009 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
2011 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
2012 rs->sr_err = LDAP_OTHER;
2016 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
2017 ctrl->ldctl_iscritical = 0;
2019 rs->sr_err = LDAP_SUCCESS;
2026 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
2028 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
2029 struct berval ip = BER_BVNULL,
2033 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
2034 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
2038 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
2039 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
2041 ptr = ber_bvchr( &ip, ':' );
2043 ip.bv_len = ptr - ip.bv_val;
2047 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
2048 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
2050 name = op->o_conn->c_peer_domain;
2053 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2057 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2061 #ifdef SLAP_CONTROL_X_WHATFAILED
2062 static int parseWhatFailed(
2067 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2068 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2069 return LDAP_PROTOCOL_ERROR;
2072 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2073 rs->sr_text = "\"What Failed?\" control value not absent";
2074 return LDAP_PROTOCOL_ERROR;
2077 op->o_whatFailed = ctrl->ldctl_iscritical
2078 ? SLAP_CONTROL_CRITICAL
2079 : SLAP_CONTROL_NONCRITICAL;
2081 return LDAP_SUCCESS;
2085 slap_ctrl_whatFailed_add(
2090 BerElementBuffer berbuf;
2091 BerElement *ber = (BerElement *) &berbuf;
2092 LDAPControl **ctrls = NULL;
2093 struct berval ctrlval;
2094 int i, rc = LDAP_SUCCESS;
2096 ber_init2( ber, NULL, LBER_USE_DER );
2097 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2098 ber_printf( ber, "[" /*]*/ );
2099 for ( i = 0; oids[ i ] != NULL; i++ ) {
2100 ber_printf( ber, "s", oids[ i ] );
2102 ber_printf( ber, /*[*/ "]" );
2104 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2110 if ( rs->sr_ctrls != NULL ) {
2111 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2112 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2119 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2120 sizeof(LDAPControl *)*( i + 2 )
2121 + sizeof(LDAPControl)
2122 + ctrlval.bv_len + 1,
2124 if ( ctrls == NULL ) {
2128 ctrls[ i + 1 ] = NULL;
2129 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2130 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2131 ctrls[ i ]->ldctl_iscritical = 0;
2132 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2133 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2134 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2136 ber_free_buf( ber );
2138 rs->sr_ctrls = ctrls;