2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 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 for (i=0; ctrls[i]; i++) {
569 op->o_tmpfree( ctrls[i], op->o_tmpmemctx );
571 op->o_tmpfree( ctrls, op->o_tmpmemctx );
574 void slap_free_ctrl_vals(
576 LDAPControl **ctrls )
580 for (i=0; ctrls[i]; i++) {
581 if ( ctrls[i]->ldctl_value.bv_val )
582 op->o_tmpfree( ctrls[i]->ldctl_value.bv_val, op->o_tmpmemctx );
584 slap_free_ctrls( op, ctrls );
590 LDAPControl **ctrls )
593 LDAPControl **ctrlsp;
595 if ( rs->sr_ctrls ) {
596 for ( ; rs->sr_ctrls[ i ]; i++ ) ;
599 for ( j=0; ctrls[j]; j++ ) ;
601 ctrlsp = op->o_tmpalloc(( i+j+1 )*sizeof(LDAPControl *), op->o_tmpmemctx );
603 if ( rs->sr_ctrls ) {
604 for ( ; rs->sr_ctrls[i]; i++ )
605 ctrlsp[i] = rs->sr_ctrls[i];
607 for ( j=0; ctrls[j]; j++)
608 ctrlsp[i++] = ctrls[j];
611 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED )
612 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
613 rs->sr_ctrls = ctrlsp;
614 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
621 LDAPControl *control,
624 struct slap_control *sc;
625 int rc = LDAP_SUCCESS;
627 sc = find_ctrl( control->ldctl_oid );
629 /* recognized control */
631 switch( op->o_tag ) {
633 tagmask = SLAP_CTRL_ADD;
636 tagmask = SLAP_CTRL_BIND;
638 case LDAP_REQ_COMPARE:
639 tagmask = SLAP_CTRL_COMPARE;
641 case LDAP_REQ_DELETE:
642 tagmask = SLAP_CTRL_DELETE;
644 case LDAP_REQ_MODIFY:
645 tagmask = SLAP_CTRL_MODIFY;
647 case LDAP_REQ_RENAME:
648 tagmask = SLAP_CTRL_RENAME;
650 case LDAP_REQ_SEARCH:
651 tagmask = SLAP_CTRL_SEARCH;
653 case LDAP_REQ_UNBIND:
654 tagmask = SLAP_CTRL_UNBIND;
656 case LDAP_REQ_ABANDON:
657 tagmask = SLAP_CTRL_ABANDON;
659 case LDAP_REQ_EXTENDED:
661 assert( op->ore_reqoid.bv_val != NULL );
662 if( sc->sc_extendedopsbv != NULL ) {
664 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
665 if( bvmatch( &op->ore_reqoid,
666 &sc->sc_extendedopsbv[i] ) )
675 *text = "controls internal error";
679 if (( sc->sc_mask & tagmask ) == tagmask ) {
680 /* available extension */
681 if ( sc->sc_parse ) {
682 rc = sc->sc_parse( op, rs, control );
683 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
685 } else if ( control->ldctl_iscritical ) {
686 *text = "not yet implemented";
691 } else if ( control->ldctl_iscritical ) {
692 /* unavailable CRITICAL control */
693 *text = "critical extension is unavailable";
694 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
697 } else if ( control->ldctl_iscritical ) {
698 /* unrecognized CRITICAL control */
699 *text = "critical extension is not recognized";
700 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
712 return get_ctrls2( op, rs, sendres, LDAP_TAG_CONTROLS );
726 BerElement *ber = op->o_ber;
728 #ifdef SLAP_CONTROL_X_WHATFAILED
729 /* NOTE: right now, slapd checks the validity of each control
730 * while parsing. As a consequence, it can only detect one
731 * cause of failure at a time. This results in returning
732 * exactly one OID with the whatFailed control, or no control
735 char *failed_oid = NULL;
738 len = ber_pvt_ber_remaining(ber);
742 rs->sr_err = LDAP_SUCCESS;
746 if(( tag = ber_peek_tag( ber, &len )) != ctag ) {
747 if( tag == LBER_ERROR ) {
748 rs->sr_err = SLAPD_DISCONNECT;
749 rs->sr_text = "unexpected data in PDU";
755 Debug( LDAP_DEBUG_TRACE,
756 "=> get_ctrls\n", 0, 0, 0 );
758 if( op->o_protocol < LDAP_VERSION3 ) {
759 rs->sr_err = SLAPD_DISCONNECT;
760 rs->sr_text = "controls require LDAPv3";
764 /* one for first control, one for termination */
765 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
768 if( op->ctrls == NULL ) {
769 rs->sr_err = LDAP_NO_MEMORY;
770 rs->sr_text = "no memory";
775 op->o_ctrls[nctrls] = NULL;
777 /* step through each element */
778 for( tag = ber_first_element( ber, &len, &opaque );
780 tag = ber_next_element( ber, &len, opaque ) )
783 LDAPControl **tctrls;
785 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
786 memset(c, 0, sizeof(LDAPControl));
788 /* allocate pointer space for current controls (nctrls)
789 * + this control + extra NULL
791 tctrls = op->o_tmprealloc( op->o_ctrls,
792 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
795 if( tctrls == NULL ) {
797 ldap_controls_free(op->o_ctrls);
800 rs->sr_err = LDAP_NO_MEMORY;
801 rs->sr_text = "no memory";
805 op->o_ctrls = tctrls;
807 op->o_ctrls[nctrls++] = c;
808 op->o_ctrls[nctrls] = NULL;
810 tag = ber_scanf( ber, "{m" /*}*/, &bv );
811 c->ldctl_oid = bv.bv_val;
813 if( tag == LBER_ERROR ) {
814 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
817 slap_free_ctrls( op, op->o_ctrls );
819 rs->sr_err = SLAPD_DISCONNECT;
820 rs->sr_text = "decoding controls error";
823 } else if( c->ldctl_oid == NULL ) {
824 Debug( LDAP_DEBUG_TRACE,
825 "get_ctrls: conn %lu got emtpy OID.\n",
826 op->o_connid, 0, 0 );
828 slap_free_ctrls( op, op->o_ctrls );
830 rs->sr_err = LDAP_PROTOCOL_ERROR;
831 rs->sr_text = "OID field is empty";
835 tag = ber_peek_tag( ber, &len );
837 if( tag == LBER_BOOLEAN ) {
839 tag = ber_scanf( ber, "b", &crit );
841 if( tag == LBER_ERROR ) {
842 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
844 slap_free_ctrls( op, op->o_ctrls );
846 rs->sr_err = SLAPD_DISCONNECT;
847 rs->sr_text = "decoding controls error";
851 c->ldctl_iscritical = (crit != 0);
852 tag = ber_peek_tag( ber, &len );
855 if( tag == LBER_OCTETSTRING ) {
856 tag = ber_scanf( ber, "m", &c->ldctl_value );
858 if( tag == LBER_ERROR ) {
859 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
860 "%s (%scritical): get value failed.\n",
861 op->o_connid, c->ldctl_oid,
862 c->ldctl_iscritical ? "" : "non" );
863 slap_free_ctrls( op, op->o_ctrls );
865 rs->sr_err = SLAPD_DISCONNECT;
866 rs->sr_text = "decoding controls error";
871 Debug( LDAP_DEBUG_TRACE,
872 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
873 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
875 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
876 if ( rs->sr_err != LDAP_SUCCESS ) {
877 #ifdef SLAP_CONTROL_X_WHATFAILED
878 failed_oid = c->ldctl_oid;
885 Debug( LDAP_DEBUG_TRACE,
886 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
887 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
889 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
890 if( rs->sr_err == SLAPD_DISCONNECT ) {
891 rs->sr_err = LDAP_PROTOCOL_ERROR;
892 send_ldap_disconnect( op, rs );
893 rs->sr_err = SLAPD_DISCONNECT;
895 #ifdef SLAP_CONTROL_X_WHATFAILED
896 /* might have not been parsed yet? */
897 if ( failed_oid != NULL ) {
898 if ( !get_whatFailed( op ) ) {
901 /* step through each remaining element */
902 for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
904 LDAPControl c = { 0 };
906 tag = ber_scanf( ber, "{m" /*}*/, &bv );
907 c.ldctl_oid = bv.bv_val;
909 if ( tag == LBER_ERROR ) {
910 slap_free_ctrls( op, op->o_ctrls );
914 } else if ( c.ldctl_oid == NULL ) {
915 slap_free_ctrls( op, op->o_ctrls );
920 tag = ber_peek_tag( ber, &len );
921 if ( tag == LBER_BOOLEAN ) {
923 tag = ber_scanf( ber, "b", &crit );
924 if( tag == LBER_ERROR ) {
925 slap_free_ctrls( op, op->o_ctrls );
930 tag = ber_peek_tag( ber, &len );
933 if ( tag == LBER_OCTETSTRING ) {
934 tag = ber_scanf( ber, "m", &c.ldctl_value );
936 if( tag == LBER_ERROR ) {
937 slap_free_ctrls( op, op->o_ctrls );
943 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
945 slap_parse_ctrl( op, rs, &c, &text );
951 if ( get_whatFailed( op ) ) {
953 oids[ 0 ] = failed_oid;
955 slap_ctrl_whatFailed_add( op, rs, oids );
960 send_ldap_result( op, rs );
972 BI_chk_controls fnc )
976 switch ( op->o_ctrlflag[ ctrl ] ) {
977 case SLAP_CONTROL_NONCRITICAL:
978 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
979 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
980 slap_known_controls[ ctrl - 1 ] ) == 0 )
987 rs->sr_err = LDAP_OTHER;
995 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
998 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
999 ( i - j ) * sizeof( LDAPControl * ) );
1002 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
1006 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
1008 Debug( LDAP_DEBUG_ANY, "%s: "
1009 "non-critical control \"%s\" not supported; stripped.\n",
1010 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1013 case SLAP_CONTROL_IGNORED:
1014 case SLAP_CONTROL_NONE:
1015 rs->sr_err = SLAP_CB_CONTINUE;
1018 case SLAP_CONTROL_CRITICAL:
1019 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
1021 (void)fnc( op, rs );
1023 Debug( LDAP_DEBUG_ANY, "%s: "
1024 "critical control \"%s\" not supported.\n",
1025 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1029 /* handle all cases! */
1036 static int parseDontUseCopy (
1041 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
1042 rs->sr_text = "dontUseCopy control specified multiple times";
1043 return LDAP_PROTOCOL_ERROR;
1046 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1047 rs->sr_text = "dontUseCopy control value not absent";
1048 return LDAP_PROTOCOL_ERROR;
1051 if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
1052 && !ctrl->ldctl_iscritical )
1054 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
1055 return LDAP_PROTOCOL_ERROR;
1058 op->o_dontUseCopy = ctrl->ldctl_iscritical
1059 ? SLAP_CONTROL_CRITICAL
1060 : SLAP_CONTROL_NONCRITICAL;
1062 return LDAP_SUCCESS;
1065 static int parseRelax (
1070 if ( op->o_relax != SLAP_CONTROL_NONE ) {
1071 rs->sr_text = "relax control specified multiple times";
1072 return LDAP_PROTOCOL_ERROR;
1075 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1076 rs->sr_text = "relax control value not absent";
1077 return LDAP_PROTOCOL_ERROR;
1080 op->o_relax = ctrl->ldctl_iscritical
1081 ? SLAP_CONTROL_CRITICAL
1082 : SLAP_CONTROL_NONCRITICAL;
1084 return LDAP_SUCCESS;
1087 static int parseManageDSAit (
1092 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1093 rs->sr_text = "manageDSAit control specified multiple times";
1094 return LDAP_PROTOCOL_ERROR;
1097 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1098 rs->sr_text = "manageDSAit control value not absent";
1099 return LDAP_PROTOCOL_ERROR;
1102 op->o_managedsait = ctrl->ldctl_iscritical
1103 ? SLAP_CONTROL_CRITICAL
1104 : SLAP_CONTROL_NONCRITICAL;
1106 return LDAP_SUCCESS;
1109 static int parseProxyAuthz (
1115 struct berval dn = BER_BVNULL;
1117 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1118 rs->sr_text = "proxy authorization control specified multiple times";
1119 return LDAP_PROTOCOL_ERROR;
1122 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1123 rs->sr_text = "proxy authorization control value absent";
1124 return LDAP_PROTOCOL_ERROR;
1127 if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1128 && !ctrl->ldctl_iscritical )
1130 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1131 return LDAP_PROTOCOL_ERROR;
1134 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1135 && BER_BVISEMPTY( &op->o_ndn ) )
1137 rs->sr_text = "anonymous proxied authorization not allowed";
1138 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1141 op->o_proxy_authz = ctrl->ldctl_iscritical
1142 ? SLAP_CONTROL_CRITICAL
1143 : SLAP_CONTROL_NONCRITICAL;
1145 Debug( LDAP_DEBUG_ARGS,
1146 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
1148 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
1151 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1152 Debug( LDAP_DEBUG_TRACE,
1153 "parseProxyAuthz: conn=%lu anonymous\n",
1154 op->o_connid, 0, 0 );
1157 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1158 op->o_ndn.bv_val[ 0 ] = '\0';
1160 op->o_ndn.bv_len = 0;
1162 if ( !BER_BVISNULL( &op->o_dn ) ) {
1163 op->o_dn.bv_val[ 0 ] = '\0';
1165 op->o_dn.bv_len = 0;
1167 return LDAP_SUCCESS;
1170 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1171 NULL, &dn, SLAP_GETDN_AUTHZID );
1173 /* FIXME: empty DN in proxyAuthz control should be legal... */
1174 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1176 ch_free( dn.bv_val );
1178 rs->sr_text = "authzId mapping failed";
1179 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1182 Debug( LDAP_DEBUG_TRACE,
1183 "parseProxyAuthz: conn=%lu \"%s\"\n",
1185 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1187 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1190 ch_free( dn.bv_val );
1191 rs->sr_text = "not authorized to assume identity";
1192 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1195 ch_free( op->o_ndn.bv_val );
1198 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1199 * from now on op->o_dn is normalized
1202 ber_bvreplace( &op->o_dn, &dn );
1204 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1205 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1207 return LDAP_SUCCESS;
1210 static int parseNoOp (
1215 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1216 rs->sr_text = "noop control specified multiple times";
1217 return LDAP_PROTOCOL_ERROR;
1220 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1221 rs->sr_text = "noop control value not empty";
1222 return LDAP_PROTOCOL_ERROR;
1225 op->o_noop = ctrl->ldctl_iscritical
1226 ? SLAP_CONTROL_CRITICAL
1227 : SLAP_CONTROL_NONCRITICAL;
1229 return LDAP_SUCCESS;
1232 static int parsePagedResults (
1237 BerElementBuffer berbuf;
1238 BerElement *ber = (BerElement *)&berbuf;
1239 struct berval cookie;
1240 PagedResultsState *ps;
1241 int rc = LDAP_SUCCESS;
1245 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1246 rs->sr_text = "paged results control specified multiple times";
1247 return LDAP_PROTOCOL_ERROR;
1250 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1251 rs->sr_text = "paged results control value is absent";
1252 return LDAP_PROTOCOL_ERROR;
1255 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1256 rs->sr_text = "paged results control value is empty";
1257 return LDAP_PROTOCOL_ERROR;
1260 /* Parse the control value
1261 * realSearchControlValue ::= SEQUENCE {
1262 * size INTEGER (0..maxInt),
1263 * -- requested page size from client
1264 * -- result set size estimate from server
1265 * cookie OCTET STRING
1268 ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1270 tag = ber_scanf( ber, "{im}", &size, &cookie );
1272 if ( tag == LBER_ERROR ) {
1273 rs->sr_text = "paged results control could not be decoded";
1274 rc = LDAP_PROTOCOL_ERROR;
1279 rs->sr_text = "paged results control size invalid";
1280 rc = LDAP_PROTOCOL_ERROR;
1284 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1285 *ps = op->o_conn->c_pagedresults_state;
1287 ps->ps_cookieval = cookie;
1288 op->o_pagedresults_state = ps;
1289 if ( !cookie.bv_len ) {
1292 /* taint ps_cookie, to detect whether it's set */
1293 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1296 /* NOTE: according to RFC 2696 3.:
1298 If the page size is greater than or equal to the sizeLimit value, the
1299 server should ignore the control as the request can be satisfied in a
1302 * NOTE: this assumes that the op->ors_slimit be set
1303 * before the controls are parsed.
1306 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1307 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1309 } else if ( ctrl->ldctl_iscritical ) {
1310 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1313 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1320 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1321 static int parseSortedResults (
1326 int rc = LDAP_SUCCESS;
1328 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1329 rs->sr_text = "sorted results control specified multiple times";
1330 return LDAP_PROTOCOL_ERROR;
1333 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1334 rs->sr_text = "sorted results control value is absent";
1335 return LDAP_PROTOCOL_ERROR;
1338 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1339 rs->sr_text = "sorted results control value is empty";
1340 return LDAP_PROTOCOL_ERROR;
1343 /* blow off parsing the value */
1345 op->o_sortedresults = ctrl->ldctl_iscritical
1346 ? SLAP_CONTROL_CRITICAL
1347 : SLAP_CONTROL_NONCRITICAL;
1353 static int parseAssert (
1359 struct berval fstr = BER_BVNULL;
1361 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1362 rs->sr_text = "assert control specified multiple times";
1363 return LDAP_PROTOCOL_ERROR;
1366 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1367 rs->sr_text = "assert control value is absent";
1368 return LDAP_PROTOCOL_ERROR;
1371 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1372 rs->sr_text = "assert control value is empty";
1373 return LDAP_PROTOCOL_ERROR;
1376 ber = ber_init( &(ctrl->ldctl_value) );
1378 rs->sr_text = "assert control: internal error";
1382 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1384 (void) ber_free( ber, 1 );
1385 if( rs->sr_err != LDAP_SUCCESS ) {
1386 if( rs->sr_err == SLAPD_DISCONNECT ) {
1387 rs->sr_err = LDAP_PROTOCOL_ERROR;
1388 send_ldap_disconnect( op, rs );
1389 rs->sr_err = SLAPD_DISCONNECT;
1391 send_ldap_result( op, rs );
1393 if( op->o_assertion != NULL ) {
1394 filter_free_x( op, op->o_assertion, 1 );
1400 filter2bv_x( op, op->o_assertion, &fstr );
1402 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1403 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1404 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1407 op->o_assert = ctrl->ldctl_iscritical
1408 ? SLAP_CONTROL_CRITICAL
1409 : SLAP_CONTROL_NONCRITICAL;
1411 rs->sr_err = LDAP_SUCCESS;
1412 return LDAP_SUCCESS;
1415 #define READMSG(post, msg) \
1416 ( post ? "postread control: " msg : "preread control: " msg )
1425 ber_len_t siz, off, i;
1427 AttributeName *an = NULL;
1429 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1430 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1432 rs->sr_text = READMSG( post, "specified multiple times" );
1433 return LDAP_PROTOCOL_ERROR;
1436 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1437 rs->sr_text = READMSG( post, "value is absent" );
1438 return LDAP_PROTOCOL_ERROR;
1441 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1442 rs->sr_text = READMSG( post, "value is empty" );
1443 return LDAP_PROTOCOL_ERROR;
1447 if ( op->o_txnSpec ) { /* temporary limitation */
1448 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1449 return LDAP_UNWILLING_TO_PERFORM;
1453 ber = ber_init( &ctrl->ldctl_value );
1454 if ( ber == NULL ) {
1455 rs->sr_text = READMSG( post, "internal error" );
1459 rs->sr_err = LDAP_SUCCESS;
1460 siz = sizeof( AttributeName );
1461 off = offsetof( AttributeName, an_name );
1462 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1463 rs->sr_text = READMSG( post, "decoding error" );
1464 rs->sr_err = LDAP_PROTOCOL_ERROR;
1468 for ( i = 0; i < siz; i++ ) {
1469 const char *dummy = NULL;
1472 an[i].an_desc = NULL;
1475 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1476 if ( rc == LDAP_SUCCESS ) {
1477 an[i].an_name = an[i].an_desc->ad_cname;
1481 static struct berval special_attrs[] = {
1482 BER_BVC( LDAP_NO_ATTRS ),
1483 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1484 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1488 /* deal with special attribute types */
1489 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1490 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1491 an[i].an_name = special_attrs[ j ];
1496 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1498 rs->sr_text = dummy ? dummy
1499 : READMSG( post, "unknown attributeType" );
1506 op->o_postread_attrs = an;
1507 op->o_postread = ctrl->ldctl_iscritical
1508 ? SLAP_CONTROL_CRITICAL
1509 : SLAP_CONTROL_NONCRITICAL;
1511 op->o_preread_attrs = an;
1512 op->o_preread = ctrl->ldctl_iscritical
1513 ? SLAP_CONTROL_CRITICAL
1514 : SLAP_CONTROL_NONCRITICAL;
1518 (void) ber_free( ber, 1 );
1522 static int parsePreRead (
1527 return parseReadAttrs( op, rs, ctrl, 0 );
1530 static int parsePostRead (
1535 return parseReadAttrs( op, rs, ctrl, 1 );
1538 static int parseValuesReturnFilter (
1544 struct berval fstr = BER_BVNULL;
1546 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1547 rs->sr_text = "valuesReturnFilter control specified multiple times";
1548 return LDAP_PROTOCOL_ERROR;
1551 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1552 rs->sr_text = "valuesReturnFilter control value is absent";
1553 return LDAP_PROTOCOL_ERROR;
1556 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1557 rs->sr_text = "valuesReturnFilter control value is empty";
1558 return LDAP_PROTOCOL_ERROR;
1561 ber = ber_init( &(ctrl->ldctl_value) );
1563 rs->sr_text = "internal error";
1567 rs->sr_err = get_vrFilter( op, ber,
1568 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1570 (void) ber_free( ber, 1 );
1572 if( rs->sr_err != LDAP_SUCCESS ) {
1573 if( rs->sr_err == SLAPD_DISCONNECT ) {
1574 rs->sr_err = LDAP_PROTOCOL_ERROR;
1575 send_ldap_disconnect( op, rs );
1576 rs->sr_err = SLAPD_DISCONNECT;
1578 send_ldap_result( op, rs );
1580 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1584 vrFilter2bv( op, op->o_vrFilter, &fstr );
1587 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1588 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1589 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1592 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1593 ? SLAP_CONTROL_CRITICAL
1594 : SLAP_CONTROL_NONCRITICAL;
1596 rs->sr_err = LDAP_SUCCESS;
1597 return LDAP_SUCCESS;
1600 static int parseSubentries (
1605 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1606 rs->sr_text = "subentries control specified multiple times";
1607 return LDAP_PROTOCOL_ERROR;
1610 /* FIXME: should use BER library */
1611 if( ( ctrl->ldctl_value.bv_len != 3 )
1612 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1613 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1615 rs->sr_text = "subentries control value encoding is bogus";
1616 return LDAP_PROTOCOL_ERROR;
1619 op->o_subentries = ctrl->ldctl_iscritical
1620 ? SLAP_CONTROL_CRITICAL
1621 : SLAP_CONTROL_NONCRITICAL;
1623 if (ctrl->ldctl_value.bv_val[2]) {
1624 set_subentries_visibility( op );
1627 return LDAP_SUCCESS;
1630 static int parsePermissiveModify (
1635 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1636 rs->sr_text = "permissiveModify control specified multiple times";
1637 return LDAP_PROTOCOL_ERROR;
1640 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1641 rs->sr_text = "permissiveModify control value not absent";
1642 return LDAP_PROTOCOL_ERROR;
1645 op->o_permissive_modify = ctrl->ldctl_iscritical
1646 ? SLAP_CONTROL_CRITICAL
1647 : SLAP_CONTROL_NONCRITICAL;
1649 return LDAP_SUCCESS;
1652 static int parseDomainScope (
1657 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1658 rs->sr_text = "domainScope control specified multiple times";
1659 return LDAP_PROTOCOL_ERROR;
1662 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1663 rs->sr_text = "domainScope control value not empty";
1664 return LDAP_PROTOCOL_ERROR;
1667 op->o_domain_scope = ctrl->ldctl_iscritical
1668 ? SLAP_CONTROL_CRITICAL
1669 : SLAP_CONTROL_NONCRITICAL;
1671 return LDAP_SUCCESS;
1674 #ifdef SLAP_CONTROL_X_TREE_DELETE
1675 static int parseTreeDelete (
1680 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1681 rs->sr_text = "treeDelete control specified multiple times";
1682 return LDAP_PROTOCOL_ERROR;
1685 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1686 rs->sr_text = "treeDelete control value not absent";
1687 return LDAP_PROTOCOL_ERROR;
1690 op->o_tree_delete = ctrl->ldctl_iscritical
1691 ? SLAP_CONTROL_CRITICAL
1692 : SLAP_CONTROL_NONCRITICAL;
1694 return LDAP_SUCCESS;
1698 static int parseSearchOptions (
1704 ber_int_t search_flags;
1707 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1708 rs->sr_text = "searchOptions control value is absent";
1709 return LDAP_PROTOCOL_ERROR;
1712 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1713 rs->sr_text = "searchOptions control value is empty";
1714 return LDAP_PROTOCOL_ERROR;
1717 ber = ber_init( &ctrl->ldctl_value );
1719 rs->sr_text = "internal error";
1723 tag = ber_scanf( ber, "{i}", &search_flags );
1724 (void) ber_free( ber, 1 );
1726 if ( tag == LBER_ERROR ) {
1727 rs->sr_text = "searchOptions control decoding error";
1728 return LDAP_PROTOCOL_ERROR;
1731 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1732 /* Search flags not recognised so far,
1734 * LDAP_SEARCH_FLAG_PHANTOM_ROOT
1736 if ( ctrl->ldctl_iscritical ) {
1737 rs->sr_text = "searchOptions contained unrecognized flag";
1738 return LDAP_UNWILLING_TO_PERFORM;
1742 Debug( LDAP_DEBUG_TRACE,
1743 "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n",
1744 op->o_connid, (unsigned)search_flags, 0 );
1746 return LDAP_SUCCESS;
1749 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1750 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1751 rs->sr_text = "searchOptions control specified multiple times "
1752 "or with domainScope control";
1753 return LDAP_PROTOCOL_ERROR;
1756 op->o_domain_scope = ctrl->ldctl_iscritical
1757 ? SLAP_CONTROL_CRITICAL
1758 : SLAP_CONTROL_NONCRITICAL;
1761 return LDAP_SUCCESS;
1764 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1765 struct berval session_tracking_formats[] = {
1766 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1767 BER_BVC( "RADIUS-Acct-Session-Id" ),
1768 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1769 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1770 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1771 BER_BVC( "USERNAME" ),
1776 static int parseSessionTracking(
1786 struct berval sessionSourceIp = BER_BVNULL,
1787 sessionSourceName = BER_BVNULL,
1788 formatOID = BER_BVNULL,
1789 sessionTrackingIdentifier = BER_BVNULL;
1791 size_t st_len, st_pos;
1793 if ( ctrl->ldctl_iscritical ) {
1794 rs->sr_text = "sessionTracking criticality is TRUE";
1795 return LDAP_PROTOCOL_ERROR;
1798 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1799 rs->sr_text = "sessionTracking control value is absent";
1800 return LDAP_PROTOCOL_ERROR;
1803 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1804 rs->sr_text = "sessionTracking control value is empty";
1805 return LDAP_PROTOCOL_ERROR;
1808 /* TODO: add the capability to determine if a client is allowed
1809 * to use this control, based on identity, ip and so */
1811 ber = ber_init( &ctrl->ldctl_value );
1812 if ( ber == NULL ) {
1813 rs->sr_text = "internal error";
1817 tag = ber_skip_tag( ber, &len );
1818 if ( tag != LBER_SEQUENCE ) {
1823 /* sessionSourceIp */
1824 tag = ber_peek_tag( ber, &len );
1825 if ( tag == LBER_DEFAULT ) {
1831 tag = ber_skip_tag( ber, &len );
1833 } else if ( len > 128 ) {
1834 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1835 rs->sr_err = LDAP_PROTOCOL_ERROR;
1839 tag = ber_scanf( ber, "m", &sessionSourceIp );
1842 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1843 BER_BVZERO( &sessionSourceIp );
1846 /* sessionSourceName */
1847 tag = ber_peek_tag( ber, &len );
1848 if ( tag == LBER_DEFAULT ) {
1854 tag = ber_skip_tag( ber, &len );
1856 } else if ( len > 65536 ) {
1857 rs->sr_text = "sessionTracking.sessionSourceName too long";
1858 rs->sr_err = LDAP_PROTOCOL_ERROR;
1862 tag = ber_scanf( ber, "m", &sessionSourceName );
1865 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1866 BER_BVZERO( &sessionSourceName );
1870 tag = ber_peek_tag( ber, &len );
1871 if ( tag == LBER_DEFAULT ) {
1877 rs->sr_text = "sessionTracking.formatOID empty";
1878 rs->sr_err = LDAP_PROTOCOL_ERROR;
1881 } else if ( len > 1024 ) {
1882 rs->sr_text = "sessionTracking.formatOID too long";
1883 rs->sr_err = LDAP_PROTOCOL_ERROR;
1887 tag = ber_scanf( ber, "m", &formatOID );
1890 rc = numericoidValidate( NULL, &formatOID );
1891 if ( rc != LDAP_SUCCESS ) {
1892 rs->sr_text = "sessionTracking.formatOID invalid";
1896 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1898 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1899 formatOID = session_tracking_formats[ i + 1 ];
1904 /* sessionTrackingIdentifier */
1905 tag = ber_peek_tag( ber, &len );
1906 if ( tag == LBER_DEFAULT ) {
1912 tag = ber_skip_tag( ber, &len );
1915 /* note: should not be more than 65536... */
1916 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1917 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1918 /* we want the OID printed, at least */
1919 BER_BVSTR( &sessionTrackingIdentifier, "" );
1924 tag = ber_skip_tag( ber, &len );
1925 if ( tag != LBER_DEFAULT || len != 0 ) {
1932 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1933 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1935 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1936 if ( st_len ) st_len++;
1937 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1939 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1940 if ( st_len ) st_len++;
1941 st_len += formatOID.bv_len + STRLENOF( "=" )
1942 + sessionTrackingIdentifier.bv_len;
1945 if ( st_len == 0 ) {
1949 st_len += STRLENOF( " []" );
1950 st_pos = strlen( op->o_log_prefix );
1952 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1953 char *ptr = &op->o_log_prefix[ st_pos ];
1955 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1958 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1959 ptr = lutil_strcopy( ptr, "IP=" );
1960 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1964 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1965 if ( st_len ) *ptr++ = ' ';
1966 ptr = lutil_strcopy( ptr, "NAME=" );
1967 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1971 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1972 if ( st_len ) *ptr++ = ' ';
1973 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1975 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1983 (void)ber_free( ber, 1 );
1985 if ( tag == LBER_ERROR ) {
1986 rs->sr_text = "sessionTracking control decoding error";
1987 return LDAP_PROTOCOL_ERROR;
1995 slap_ctrl_session_tracking_add(
1999 struct berval *name,
2003 BerElementBuffer berbuf;
2004 BerElement *ber = (BerElement *)&berbuf;
2006 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
2008 assert( ctrl != NULL );
2010 ber_init2( ber, NULL, LBER_USE_DER );
2012 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
2014 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
2015 rs->sr_err = LDAP_OTHER;
2019 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
2020 ctrl->ldctl_iscritical = 0;
2022 rs->sr_err = LDAP_SUCCESS;
2029 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
2031 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
2032 struct berval ip = BER_BVNULL,
2036 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
2037 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
2041 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
2042 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
2044 ptr = ber_bvchr( &ip, ':' );
2046 ip.bv_len = ptr - ip.bv_val;
2050 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
2051 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
2053 name = op->o_conn->c_peer_domain;
2056 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2060 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2064 #ifdef SLAP_CONTROL_X_WHATFAILED
2065 static int parseWhatFailed(
2070 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2071 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2072 return LDAP_PROTOCOL_ERROR;
2075 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2076 rs->sr_text = "\"What Failed?\" control value not absent";
2077 return LDAP_PROTOCOL_ERROR;
2080 op->o_whatFailed = ctrl->ldctl_iscritical
2081 ? SLAP_CONTROL_CRITICAL
2082 : SLAP_CONTROL_NONCRITICAL;
2084 return LDAP_SUCCESS;
2088 slap_ctrl_whatFailed_add(
2093 BerElementBuffer berbuf;
2094 BerElement *ber = (BerElement *) &berbuf;
2095 LDAPControl **ctrls = NULL;
2096 struct berval ctrlval;
2097 int i, rc = LDAP_SUCCESS;
2099 ber_init2( ber, NULL, LBER_USE_DER );
2100 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2101 ber_printf( ber, "[" /*]*/ );
2102 for ( i = 0; oids[ i ] != NULL; i++ ) {
2103 ber_printf( ber, "s", oids[ i ] );
2105 ber_printf( ber, /*[*/ "]" );
2107 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2113 if ( rs->sr_ctrls != NULL ) {
2114 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2115 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2122 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2123 sizeof(LDAPControl *)*( i + 2 )
2124 + sizeof(LDAPControl)
2125 + ctrlval.bv_len + 1,
2127 if ( ctrls == NULL ) {
2131 ctrls[ i + 1 ] = NULL;
2132 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2133 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2134 ctrls[ i ]->ldctl_iscritical = 0;
2135 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2136 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2137 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2139 ber_free_buf( ber );
2141 rs->sr_ctrls = ctrls;