2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/socket.h>
27 #include "../../libraries/liblber/lber-int.h"
29 static SLAP_CTRL_PARSE_FN parseAssert;
30 static SLAP_CTRL_PARSE_FN parseDomainScope;
31 static SLAP_CTRL_PARSE_FN parseDontUseCopy;
32 static SLAP_CTRL_PARSE_FN parseManageDSAit;
33 static SLAP_CTRL_PARSE_FN parseNoOp;
34 static SLAP_CTRL_PARSE_FN parsePagedResults;
35 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
36 static SLAP_CTRL_PARSE_FN parsePreRead, parsePostRead;
37 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
38 static SLAP_CTRL_PARSE_FN parseRelax;
39 static SLAP_CTRL_PARSE_FN parseSearchOptions;
40 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
41 static SLAP_CTRL_PARSE_FN parseSortedResults;
43 static SLAP_CTRL_PARSE_FN parseSubentries;
44 #ifdef SLAP_CONTROL_X_TREE_DELETE
45 static SLAP_CTRL_PARSE_FN parseTreeDelete;
47 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
48 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
49 static SLAP_CTRL_PARSE_FN parseSessionTracking;
51 #ifdef SLAP_CONTROL_X_WHATFAILED
52 static SLAP_CTRL_PARSE_FN parseWhatFailed;
55 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
57 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
58 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
60 struct slap_control_ids slap_cids;
66 /* The controlID for this control */
69 /* Operations supported by control */
72 /* Extended operations supported by control */
73 char **sc_extendedops; /* input */
74 BerVarray sc_extendedopsbv; /* run-time use */
76 /* Control parsing callback */
77 SLAP_CTRL_PARSE_FN *sc_parse;
79 LDAP_SLIST_ENTRY(slap_control) sc_next;
82 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
83 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
86 * all known request control OIDs should be added to this list
89 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
90 * addresses an undefined control; this allows to safely test for
91 * well known controls even if they are not registered, e.g. if
92 * they get moved to modules. An example is sc_LDAPsync, which
93 * is implemented in the syncprov overlay and thus, if configured
94 * as dynamic module, may not be registered. One side effect is that
95 * slap_known_controls[0] == NULL, so it should always be used
97 * FIXME: should we define the "undefined control" oid?
99 char *slap_known_controls[SLAP_MAX_CIDS+1];
100 static int num_known_controls = 1;
102 static char *proxy_authz_extops[] = {
103 LDAP_EXOP_MODIFY_PASSWD,
109 static char *manageDSAit_extops[] = {
114 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
115 static char *session_tracking_extops[] = {
116 LDAP_EXOP_MODIFY_PASSWD,
123 static struct slap_control control_defs[] = {
124 { LDAP_CONTROL_ASSERT,
125 (int)offsetof(struct slap_control_ids, sc_assert),
126 SLAP_CTRL_UPDATE|SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
128 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
129 { LDAP_CONTROL_PRE_READ,
130 (int)offsetof(struct slap_control_ids, sc_preRead),
131 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
133 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
134 { LDAP_CONTROL_POST_READ,
135 (int)offsetof(struct slap_control_ids, sc_postRead),
136 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
138 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 { LDAP_CONTROL_VALUESRETURNFILTER,
140 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
141 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
143 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
144 { LDAP_CONTROL_PAGEDRESULTS,
145 (int)offsetof(struct slap_control_ids, sc_pagedResults),
148 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
150 { LDAP_CONTROL_SORTREQUEST,
151 (int)offsetof(struct slap_control_ids, sc_sortedResults),
152 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
154 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
156 { LDAP_CONTROL_X_DOMAIN_SCOPE,
157 (int)offsetof(struct slap_control_ids, sc_domainScope),
158 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
160 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
161 { LDAP_CONTROL_DONTUSECOPY,
162 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
163 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE,
165 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
166 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
167 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
168 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
170 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
171 #ifdef SLAP_CONTROL_X_TREE_DELETE
172 { LDAP_CONTROL_X_TREE_DELETE,
173 (int)offsetof(struct slap_control_ids, sc_treeDelete),
174 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
176 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
178 { LDAP_CONTROL_X_SEARCH_OPTIONS,
179 (int)offsetof(struct slap_control_ids, sc_searchOptions),
180 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
182 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
183 { LDAP_CONTROL_SUBENTRIES,
184 (int)offsetof(struct slap_control_ids, sc_subentries),
187 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
189 (int)offsetof(struct slap_control_ids, sc_noOp),
190 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
192 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
193 { LDAP_CONTROL_RELAX,
194 (int)offsetof(struct slap_control_ids, sc_relax),
195 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
197 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
199 { LDAP_CONTROL_X_TXN_SPEC,
200 (int)offsetof(struct slap_control_ids, sc_txnSpec),
201 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
203 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
205 { LDAP_CONTROL_MANAGEDSAIT,
206 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
208 manageDSAit_extops, NULL,
209 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
210 { LDAP_CONTROL_PROXY_AUTHZ,
211 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
212 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
213 proxy_authz_extops, NULL,
214 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
215 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
216 { LDAP_CONTROL_X_SESSION_TRACKING,
217 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
218 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
219 session_tracking_extops, NULL,
220 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
222 #ifdef SLAP_CONTROL_X_WHATFAILED
223 { LDAP_CONTROL_X_WHATFAILED,
224 (int)offsetof(struct slap_control_ids, sc_whatFailed),
225 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
227 parseWhatFailed, LDAP_SLIST_ENTRY_INITIALIZER(next) },
230 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
233 static struct slap_control *
234 find_ctrl( const char *oid );
237 * Register a supported control.
239 * This can be called by an OpenLDAP plugin or, indirectly, by a
240 * SLAPI plugin calling slapi_register_supported_control().
242 * NOTE: if flags == 1 the control is replaced if already registered;
243 * otherwise registering an already registered control is not allowed.
246 register_supported_control2(const char *controloid,
247 slap_mask_t controlmask,
249 SLAP_CTRL_PARSE_FN *controlparsefn,
253 struct slap_control *sc = NULL;
255 BerVarray extendedopsbv = NULL;
257 if ( num_known_controls >= SLAP_MAX_CIDS ) {
258 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
259 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
260 SLAP_MAX_CIDS, 0, 0 );
264 if ( controloid == NULL ) {
265 return LDAP_PARAM_ERROR;
268 /* check if already registered */
269 for ( i = 0; slap_known_controls[ i ]; i++ ) {
270 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
272 Debug( LDAP_DEBUG_TRACE,
273 "Control %s already registered; replacing.\n",
275 /* (find and) replace existing handler */
276 sc = find_ctrl( controloid );
277 assert( sc != NULL );
281 Debug( LDAP_DEBUG_ANY,
282 "Control %s already registered.\n",
284 return LDAP_PARAM_ERROR;
288 /* turn compatible extended operations into bervals */
289 if ( controlexops != NULL ) {
292 for ( i = 0; controlexops[ i ]; i++ );
294 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
295 if ( extendedopsbv == NULL ) {
296 return LDAP_NO_MEMORY;
299 for ( i = 0; controlexops[ i ]; i++ ) {
300 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
305 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
307 ber_bvarray_free( extendedopsbv );
308 return LDAP_NO_MEMORY;
311 sc->sc_oid = ch_strdup( controloid );
312 sc->sc_cid = num_known_controls;
314 /* Update slap_known_controls, too. */
315 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
316 slap_known_controls[num_known_controls++] = NULL;
318 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
319 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
322 if ( sc->sc_extendedopsbv ) {
323 /* FIXME: in principle, we should rather merge
324 * existing extops with those supported by the
325 * new control handling implementation.
326 * In fact, whether a control is compatible with
327 * an extop should not be a matter of implementation.
328 * We likely also need a means for a newly
329 * registered extop to declare that it is
330 * comptible with an already registered control.
332 ber_bvarray_free( sc->sc_extendedopsbv );
333 sc->sc_extendedopsbv = NULL;
334 sc->sc_extendedops = NULL;
338 sc->sc_extendedopsbv = extendedopsbv;
339 sc->sc_mask = controlmask;
340 sc->sc_parse = controlparsefn;
342 *controlcid = sc->sc_cid;
348 #ifdef SLAP_CONFIG_DELETE
350 unregister_supported_control( const char *controloid )
352 struct slap_control *sc;
355 if ( controloid == NULL || (sc = find_ctrl( controloid )) == NULL ){
359 for ( i = 0; slap_known_controls[ i ]; i++ ) {
360 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
362 slap_known_controls[ i ] = slap_known_controls[ i+1 ];
363 } while ( slap_known_controls[ i++ ] );
364 num_known_controls--;
369 LDAP_SLIST_REMOVE(&controls_list, sc, slap_control, sc_next);
370 ch_free( sc->sc_oid );
371 if ( sc->sc_extendedopsbv != NULL ) {
372 ber_bvarray_free( sc->sc_extendedopsbv );
378 #endif /* SLAP_CONFIG_DELETE */
381 * One-time initialization of internal controls.
384 slap_controls_init( void )
390 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
391 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
392 rc = register_supported_control( control_defs[i].sc_oid,
393 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
394 control_defs[i].sc_parse, cid );
395 if ( rc != LDAP_SUCCESS ) break;
402 * Free memory associated with list of supported controls.
405 controls_destroy( void )
407 struct slap_control *sc;
409 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
410 sc = LDAP_SLIST_FIRST(&controls_list);
411 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
413 ch_free( sc->sc_oid );
414 if ( sc->sc_extendedopsbv != NULL ) {
415 ber_bvarray_free( sc->sc_extendedopsbv );
422 * Format the supportedControl attribute of the root DSE,
423 * detailing which controls are supported by the directory
427 controls_root_dse_info( Entry *e )
429 AttributeDescription *ad_supportedControl
430 = slap_schema.si_ad_supportedControl;
431 struct berval vals[2];
432 struct slap_control *sc;
434 vals[1].bv_val = NULL;
437 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
438 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
440 vals[0].bv_val = sc->sc_oid;
441 vals[0].bv_len = strlen( sc->sc_oid );
443 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
452 * Return a list of OIDs and operation masks for supported
453 * controls. Used by SLAPI.
456 get_supported_controls(char ***ctrloidsp,
457 slap_mask_t **ctrlmasks)
462 struct slap_control *sc;
466 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
476 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
477 if ( oids == NULL ) {
478 return LDAP_NO_MEMORY;
480 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
481 if ( masks == NULL ) {
483 return LDAP_NO_MEMORY;
488 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
489 oids[n] = ch_strdup( sc->sc_oid );
490 masks[n] = sc->sc_mask;
503 * Find a control given its OID.
505 static struct slap_control *
506 find_ctrl( const char *oid )
508 struct slap_control *sc;
510 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
511 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
520 slap_find_control_id(
524 struct slap_control *ctrl = find_ctrl( oid );
526 if ( cid ) *cid = ctrl->sc_cid;
529 return LDAP_CONTROL_NOT_FOUND;
533 slap_global_control( Operation *op, const char *oid, int *cid )
535 struct slap_control *ctrl = find_ctrl( oid );
537 if ( ctrl == NULL ) {
538 /* should not be reachable */
539 Debug( LDAP_DEBUG_ANY,
540 "slap_global_control: unrecognized control: %s\n",
542 return LDAP_CONTROL_NOT_FOUND;
545 if ( cid ) *cid = ctrl->sc_cid;
547 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
548 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
549 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
551 return LDAP_COMPARE_TRUE;
555 Debug( LDAP_DEBUG_TRACE,
556 "slap_global_control: unavailable control: %s\n",
560 return LDAP_COMPARE_FALSE;
563 void slap_free_ctrls(
565 LDAPControl **ctrls )
569 if( ctrls == op->o_ctrls ) {
570 if( op->o_assertion != NULL ) {
571 filter_free_x( op, op->o_assertion, 1 );
572 op->o_assertion = NULL;
574 if( op->o_vrFilter != NULL) {
575 vrFilter_free( op, op->o_vrFilter );
576 op->o_vrFilter = NULL;
578 if( op->o_preread_attrs != NULL ) {
579 op->o_tmpfree( op->o_preread_attrs, op->o_tmpmemctx );
580 op->o_preread_attrs = NULL;
582 if( op->o_postread_attrs != NULL ) {
583 op->o_tmpfree( op->o_postread_attrs, op->o_tmpmemctx );
584 op->o_postread_attrs = NULL;
586 if( op->o_pagedresults_state != NULL ) {
587 op->o_tmpfree( op->o_pagedresults_state, op->o_tmpmemctx );
588 op->o_pagedresults_state = NULL;
592 for (i=0; ctrls[i]; i++) {
593 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
595 op->o_tmpfree( ctrls, op->o_tmpmemctx );
601 LDAPControl **ctrls )
604 LDAPControl **ctrlsp;
606 if ( rs->sr_ctrls ) {
607 for ( ; rs->sr_ctrls[ i ]; i++ ) ;
610 for ( j=0; ctrls[j]; j++ ) ;
612 ctrlsp = op->o_tmpalloc(( i+j+1 )*sizeof(LDAPControl *), op->o_tmpmemctx );
614 if ( rs->sr_ctrls ) {
615 for ( ; rs->sr_ctrls[i]; i++ )
616 ctrlsp[i] = rs->sr_ctrls[i];
618 for ( j=0; ctrls[j]; j++)
619 ctrlsp[i++] = ctrls[j];
622 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED )
623 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
624 rs->sr_ctrls = ctrlsp;
625 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
632 LDAPControl *control,
635 struct slap_control *sc;
636 int rc = LDAP_SUCCESS;
638 sc = find_ctrl( control->ldctl_oid );
640 /* recognized control */
642 switch( op->o_tag ) {
644 tagmask = SLAP_CTRL_ADD;
647 tagmask = SLAP_CTRL_BIND;
649 case LDAP_REQ_COMPARE:
650 tagmask = SLAP_CTRL_COMPARE;
652 case LDAP_REQ_DELETE:
653 tagmask = SLAP_CTRL_DELETE;
655 case LDAP_REQ_MODIFY:
656 tagmask = SLAP_CTRL_MODIFY;
658 case LDAP_REQ_RENAME:
659 tagmask = SLAP_CTRL_RENAME;
661 case LDAP_REQ_SEARCH:
662 tagmask = SLAP_CTRL_SEARCH;
664 case LDAP_REQ_UNBIND:
665 tagmask = SLAP_CTRL_UNBIND;
667 case LDAP_REQ_ABANDON:
668 tagmask = SLAP_CTRL_ABANDON;
670 case LDAP_REQ_EXTENDED:
672 assert( op->ore_reqoid.bv_val != NULL );
673 if( sc->sc_extendedopsbv != NULL ) {
675 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
676 if( bvmatch( &op->ore_reqoid,
677 &sc->sc_extendedopsbv[i] ) )
686 *text = "controls internal error";
690 if (( sc->sc_mask & tagmask ) == tagmask ) {
691 /* available extension */
692 if ( sc->sc_parse ) {
693 rc = sc->sc_parse( op, rs, control );
694 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
696 } else if ( control->ldctl_iscritical ) {
697 *text = "not yet implemented";
702 } else if ( control->ldctl_iscritical ) {
703 /* unavailable CRITICAL control */
704 *text = "critical extension is unavailable";
705 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
708 } else if ( control->ldctl_iscritical ) {
709 /* unrecognized CRITICAL control */
710 *text = "critical extension is not recognized";
711 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
723 return get_ctrls2( op, rs, sendres, LDAP_TAG_CONTROLS );
737 BerElement *ber = op->o_ber;
739 #ifdef SLAP_CONTROL_X_WHATFAILED
740 /* NOTE: right now, slapd checks the validity of each control
741 * while parsing. As a consequence, it can only detect one
742 * cause of failure at a time. This results in returning
743 * exactly one OID with the whatFailed control, or no control
746 char *failed_oid = NULL;
749 len = ber_pvt_ber_remaining(ber);
753 rs->sr_err = LDAP_SUCCESS;
757 if(( tag = ber_peek_tag( ber, &len )) != ctag ) {
758 if( tag == LBER_ERROR ) {
759 rs->sr_err = SLAPD_DISCONNECT;
760 rs->sr_text = "unexpected data in PDU";
766 Debug( LDAP_DEBUG_TRACE,
767 "=> get_ctrls\n", 0, 0, 0 );
769 if( op->o_protocol < LDAP_VERSION3 ) {
770 rs->sr_err = SLAPD_DISCONNECT;
771 rs->sr_text = "controls require LDAPv3";
775 /* one for first control, one for termination */
776 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
779 if( op->ctrls == NULL ) {
780 rs->sr_err = LDAP_NO_MEMORY;
781 rs->sr_text = "no memory";
786 op->o_ctrls[nctrls] = NULL;
788 /* step through each element */
789 for( tag = ber_first_element( ber, &len, &opaque );
791 tag = ber_next_element( ber, &len, opaque ) )
794 LDAPControl **tctrls;
796 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
797 memset(c, 0, sizeof(LDAPControl));
799 /* allocate pointer space for current controls (nctrls)
800 * + this control + extra NULL
802 tctrls = op->o_tmprealloc( op->o_ctrls,
803 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
806 if( tctrls == NULL ) {
808 ldap_controls_free(op->o_ctrls);
811 rs->sr_err = LDAP_NO_MEMORY;
812 rs->sr_text = "no memory";
816 op->o_ctrls = tctrls;
818 op->o_ctrls[nctrls++] = c;
819 op->o_ctrls[nctrls] = NULL;
821 tag = ber_scanf( ber, "{m" /*}*/, &bv );
822 c->ldctl_oid = bv.bv_val;
824 if( tag == LBER_ERROR ) {
825 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
828 slap_free_ctrls( op, op->o_ctrls );
830 rs->sr_err = SLAPD_DISCONNECT;
831 rs->sr_text = "decoding controls error";
834 } else if( c->ldctl_oid == NULL ) {
835 Debug( LDAP_DEBUG_TRACE,
836 "get_ctrls: conn %lu got emtpy OID.\n",
837 op->o_connid, 0, 0 );
839 slap_free_ctrls( op, op->o_ctrls );
841 rs->sr_err = LDAP_PROTOCOL_ERROR;
842 rs->sr_text = "OID field is empty";
846 tag = ber_peek_tag( ber, &len );
848 if( tag == LBER_BOOLEAN ) {
850 tag = ber_scanf( ber, "b", &crit );
852 if( tag == LBER_ERROR ) {
853 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
855 slap_free_ctrls( op, op->o_ctrls );
857 rs->sr_err = SLAPD_DISCONNECT;
858 rs->sr_text = "decoding controls error";
862 c->ldctl_iscritical = (crit != 0);
863 tag = ber_peek_tag( ber, &len );
866 if( tag == LBER_OCTETSTRING ) {
867 tag = ber_scanf( ber, "m", &c->ldctl_value );
869 if( tag == LBER_ERROR ) {
870 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
871 "%s (%scritical): get value failed.\n",
872 op->o_connid, c->ldctl_oid,
873 c->ldctl_iscritical ? "" : "non" );
874 slap_free_ctrls( op, op->o_ctrls );
876 rs->sr_err = SLAPD_DISCONNECT;
877 rs->sr_text = "decoding controls error";
882 Debug( LDAP_DEBUG_TRACE,
883 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
884 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
886 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
887 if ( rs->sr_err != LDAP_SUCCESS ) {
888 #ifdef SLAP_CONTROL_X_WHATFAILED
889 failed_oid = c->ldctl_oid;
896 Debug( LDAP_DEBUG_TRACE,
897 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
898 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
900 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
901 if( rs->sr_err == SLAPD_DISCONNECT ) {
902 rs->sr_err = LDAP_PROTOCOL_ERROR;
903 send_ldap_disconnect( op, rs );
904 rs->sr_err = SLAPD_DISCONNECT;
906 #ifdef SLAP_CONTROL_X_WHATFAILED
907 /* might have not been parsed yet? */
908 if ( failed_oid != NULL ) {
909 if ( !get_whatFailed( op ) ) {
912 /* step through each remaining element */
913 for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
915 LDAPControl c = { 0 };
917 tag = ber_scanf( ber, "{m" /*}*/, &bv );
918 c.ldctl_oid = bv.bv_val;
920 if ( tag == LBER_ERROR ) {
921 slap_free_ctrls( op, op->o_ctrls );
925 } else if ( c.ldctl_oid == NULL ) {
926 slap_free_ctrls( op, op->o_ctrls );
931 tag = ber_peek_tag( ber, &len );
932 if ( tag == LBER_BOOLEAN ) {
934 tag = ber_scanf( ber, "b", &crit );
935 if( tag == LBER_ERROR ) {
936 slap_free_ctrls( op, op->o_ctrls );
941 tag = ber_peek_tag( ber, &len );
944 if ( tag == LBER_OCTETSTRING ) {
945 tag = ber_scanf( ber, "m", &c.ldctl_value );
947 if( tag == LBER_ERROR ) {
948 slap_free_ctrls( op, op->o_ctrls );
954 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
956 slap_parse_ctrl( op, rs, &c, &text );
962 if ( get_whatFailed( op ) ) {
964 oids[ 0 ] = failed_oid;
966 slap_ctrl_whatFailed_add( op, rs, oids );
971 send_ldap_result( op, rs );
983 BI_chk_controls fnc )
987 switch ( op->o_ctrlflag[ ctrl ] ) {
988 case SLAP_CONTROL_NONCRITICAL:
989 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
990 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
991 slap_known_controls[ ctrl - 1 ] ) == 0 )
998 rs->sr_err = LDAP_OTHER;
1003 (void)fnc( op, rs );
1006 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
1009 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
1010 ( i - j ) * sizeof( LDAPControl * ) );
1013 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
1017 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
1019 Debug( LDAP_DEBUG_ANY, "%s: "
1020 "non-critical control \"%s\" not supported; stripped.\n",
1021 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1024 case SLAP_CONTROL_IGNORED:
1025 case SLAP_CONTROL_NONE:
1026 rs->sr_err = SLAP_CB_CONTINUE;
1029 case SLAP_CONTROL_CRITICAL:
1030 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
1032 (void)fnc( op, rs );
1034 Debug( LDAP_DEBUG_ANY, "%s: "
1035 "critical control \"%s\" not supported.\n",
1036 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1040 /* handle all cases! */
1047 static int parseDontUseCopy (
1052 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
1053 rs->sr_text = "dontUseCopy control specified multiple times";
1054 return LDAP_PROTOCOL_ERROR;
1057 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1058 rs->sr_text = "dontUseCopy control value not absent";
1059 return LDAP_PROTOCOL_ERROR;
1062 if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
1063 && !ctrl->ldctl_iscritical )
1065 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
1066 return LDAP_PROTOCOL_ERROR;
1069 op->o_dontUseCopy = ctrl->ldctl_iscritical
1070 ? SLAP_CONTROL_CRITICAL
1071 : SLAP_CONTROL_NONCRITICAL;
1073 return LDAP_SUCCESS;
1076 static int parseRelax (
1081 if ( op->o_relax != SLAP_CONTROL_NONE ) {
1082 rs->sr_text = "relax control specified multiple times";
1083 return LDAP_PROTOCOL_ERROR;
1086 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1087 rs->sr_text = "relax control value not absent";
1088 return LDAP_PROTOCOL_ERROR;
1091 op->o_relax = ctrl->ldctl_iscritical
1092 ? SLAP_CONTROL_CRITICAL
1093 : SLAP_CONTROL_NONCRITICAL;
1095 return LDAP_SUCCESS;
1098 static int parseManageDSAit (
1103 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1104 rs->sr_text = "manageDSAit control specified multiple times";
1105 return LDAP_PROTOCOL_ERROR;
1108 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1109 rs->sr_text = "manageDSAit control value not absent";
1110 return LDAP_PROTOCOL_ERROR;
1113 op->o_managedsait = ctrl->ldctl_iscritical
1114 ? SLAP_CONTROL_CRITICAL
1115 : SLAP_CONTROL_NONCRITICAL;
1117 return LDAP_SUCCESS;
1120 static int parseProxyAuthz (
1126 struct berval dn = BER_BVNULL;
1128 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1129 rs->sr_text = "proxy authorization control specified multiple times";
1130 return LDAP_PROTOCOL_ERROR;
1133 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1134 rs->sr_text = "proxy authorization control value absent";
1135 return LDAP_PROTOCOL_ERROR;
1138 if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1139 && !ctrl->ldctl_iscritical )
1141 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1142 return LDAP_PROTOCOL_ERROR;
1145 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1146 && BER_BVISEMPTY( &op->o_ndn ) )
1148 rs->sr_text = "anonymous proxied authorization not allowed";
1149 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1152 op->o_proxy_authz = ctrl->ldctl_iscritical
1153 ? SLAP_CONTROL_CRITICAL
1154 : SLAP_CONTROL_NONCRITICAL;
1156 Debug( LDAP_DEBUG_ARGS,
1157 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
1159 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
1162 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1163 Debug( LDAP_DEBUG_TRACE,
1164 "parseProxyAuthz: conn=%lu anonymous\n",
1165 op->o_connid, 0, 0 );
1168 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1169 op->o_ndn.bv_val[ 0 ] = '\0';
1171 op->o_ndn.bv_len = 0;
1173 if ( !BER_BVISNULL( &op->o_dn ) ) {
1174 op->o_dn.bv_val[ 0 ] = '\0';
1176 op->o_dn.bv_len = 0;
1178 return LDAP_SUCCESS;
1181 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1182 NULL, &dn, SLAP_GETDN_AUTHZID );
1184 /* FIXME: empty DN in proxyAuthz control should be legal... */
1185 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1187 ch_free( dn.bv_val );
1189 rs->sr_text = "authzId mapping failed";
1190 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1193 Debug( LDAP_DEBUG_TRACE,
1194 "parseProxyAuthz: conn=%lu \"%s\"\n",
1196 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1198 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1201 ch_free( dn.bv_val );
1202 rs->sr_text = "not authorized to assume identity";
1203 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1206 ch_free( op->o_ndn.bv_val );
1209 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1210 * from now on op->o_dn is normalized
1213 ber_bvreplace( &op->o_dn, &dn );
1215 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1216 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1218 return LDAP_SUCCESS;
1221 static int parseNoOp (
1226 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1227 rs->sr_text = "noop control specified multiple times";
1228 return LDAP_PROTOCOL_ERROR;
1231 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1232 rs->sr_text = "noop control value not empty";
1233 return LDAP_PROTOCOL_ERROR;
1236 op->o_noop = ctrl->ldctl_iscritical
1237 ? SLAP_CONTROL_CRITICAL
1238 : SLAP_CONTROL_NONCRITICAL;
1240 return LDAP_SUCCESS;
1243 static int parsePagedResults (
1248 BerElementBuffer berbuf;
1249 BerElement *ber = (BerElement *)&berbuf;
1250 struct berval cookie;
1251 PagedResultsState *ps;
1252 int rc = LDAP_SUCCESS;
1256 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1257 rs->sr_text = "paged results control specified multiple times";
1258 return LDAP_PROTOCOL_ERROR;
1261 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1262 rs->sr_text = "paged results control value is absent";
1263 return LDAP_PROTOCOL_ERROR;
1266 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1267 rs->sr_text = "paged results control value is empty";
1268 return LDAP_PROTOCOL_ERROR;
1271 /* Parse the control value
1272 * realSearchControlValue ::= SEQUENCE {
1273 * size INTEGER (0..maxInt),
1274 * -- requested page size from client
1275 * -- result set size estimate from server
1276 * cookie OCTET STRING
1279 ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1281 tag = ber_scanf( ber, "{im}", &size, &cookie );
1283 if ( tag == LBER_ERROR ) {
1284 rs->sr_text = "paged results control could not be decoded";
1285 rc = LDAP_PROTOCOL_ERROR;
1290 rs->sr_text = "paged results control size invalid";
1291 rc = LDAP_PROTOCOL_ERROR;
1295 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1296 *ps = op->o_conn->c_pagedresults_state;
1298 ps->ps_cookieval = cookie;
1299 op->o_pagedresults_state = ps;
1300 if ( !cookie.bv_len ) {
1303 /* taint ps_cookie, to detect whether it's set */
1304 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1307 /* NOTE: according to RFC 2696 3.:
1309 If the page size is greater than or equal to the sizeLimit value, the
1310 server should ignore the control as the request can be satisfied in a
1313 * NOTE: this assumes that the op->ors_slimit be set
1314 * before the controls are parsed.
1317 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1318 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1320 } else if ( ctrl->ldctl_iscritical ) {
1321 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1324 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1331 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1332 static int parseSortedResults (
1337 int rc = LDAP_SUCCESS;
1339 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1340 rs->sr_text = "sorted results control specified multiple times";
1341 return LDAP_PROTOCOL_ERROR;
1344 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1345 rs->sr_text = "sorted results control value is absent";
1346 return LDAP_PROTOCOL_ERROR;
1349 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1350 rs->sr_text = "sorted results control value is empty";
1351 return LDAP_PROTOCOL_ERROR;
1354 /* blow off parsing the value */
1356 op->o_sortedresults = ctrl->ldctl_iscritical
1357 ? SLAP_CONTROL_CRITICAL
1358 : SLAP_CONTROL_NONCRITICAL;
1364 static int parseAssert (
1370 struct berval fstr = BER_BVNULL;
1372 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1373 rs->sr_text = "assert control specified multiple times";
1374 return LDAP_PROTOCOL_ERROR;
1377 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1378 rs->sr_text = "assert control value is absent";
1379 return LDAP_PROTOCOL_ERROR;
1382 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1383 rs->sr_text = "assert control value is empty";
1384 return LDAP_PROTOCOL_ERROR;
1387 ber = ber_init( &(ctrl->ldctl_value) );
1389 rs->sr_text = "assert control: internal error";
1393 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1395 (void) ber_free( ber, 1 );
1396 if( rs->sr_err != LDAP_SUCCESS ) {
1397 if( rs->sr_err == SLAPD_DISCONNECT ) {
1398 rs->sr_err = LDAP_PROTOCOL_ERROR;
1399 send_ldap_disconnect( op, rs );
1400 rs->sr_err = SLAPD_DISCONNECT;
1402 send_ldap_result( op, rs );
1404 if( op->o_assertion != NULL ) {
1405 filter_free_x( op, op->o_assertion, 1 );
1411 filter2bv_x( op, op->o_assertion, &fstr );
1413 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1414 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1415 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1418 op->o_assert = ctrl->ldctl_iscritical
1419 ? SLAP_CONTROL_CRITICAL
1420 : SLAP_CONTROL_NONCRITICAL;
1422 rs->sr_err = LDAP_SUCCESS;
1423 return LDAP_SUCCESS;
1426 #define READMSG(post, msg) \
1427 ( post ? "postread control: " msg : "preread control: " msg )
1436 ber_len_t siz, off, i;
1438 AttributeName *an = NULL;
1440 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1441 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1443 rs->sr_text = READMSG( post, "specified multiple times" );
1444 return LDAP_PROTOCOL_ERROR;
1447 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1448 rs->sr_text = READMSG( post, "value is absent" );
1449 return LDAP_PROTOCOL_ERROR;
1452 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1453 rs->sr_text = READMSG( post, "value is empty" );
1454 return LDAP_PROTOCOL_ERROR;
1458 if ( op->o_txnSpec ) { /* temporary limitation */
1459 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1460 return LDAP_UNWILLING_TO_PERFORM;
1464 ber = ber_init( &ctrl->ldctl_value );
1465 if ( ber == NULL ) {
1466 rs->sr_text = READMSG( post, "internal error" );
1470 rs->sr_err = LDAP_SUCCESS;
1471 siz = sizeof( AttributeName );
1472 off = offsetof( AttributeName, an_name );
1473 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1474 rs->sr_text = READMSG( post, "decoding error" );
1475 rs->sr_err = LDAP_PROTOCOL_ERROR;
1479 for ( i = 0; i < siz; i++ ) {
1480 const char *dummy = NULL;
1483 an[i].an_desc = NULL;
1486 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1487 if ( rc == LDAP_SUCCESS ) {
1488 an[i].an_name = an[i].an_desc->ad_cname;
1492 static struct berval special_attrs[] = {
1493 BER_BVC( LDAP_NO_ATTRS ),
1494 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1495 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1499 /* deal with special attribute types */
1500 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1501 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1502 an[i].an_name = special_attrs[ j ];
1507 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1509 rs->sr_text = dummy ? dummy
1510 : READMSG( post, "unknown attributeType" );
1517 op->o_postread_attrs = an;
1518 op->o_postread = ctrl->ldctl_iscritical
1519 ? SLAP_CONTROL_CRITICAL
1520 : SLAP_CONTROL_NONCRITICAL;
1522 op->o_preread_attrs = an;
1523 op->o_preread = ctrl->ldctl_iscritical
1524 ? SLAP_CONTROL_CRITICAL
1525 : SLAP_CONTROL_NONCRITICAL;
1529 (void) ber_free( ber, 1 );
1533 static int parsePreRead (
1538 return parseReadAttrs( op, rs, ctrl, 0 );
1541 static int parsePostRead (
1546 return parseReadAttrs( op, rs, ctrl, 1 );
1549 static int parseValuesReturnFilter (
1555 struct berval fstr = BER_BVNULL;
1557 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1558 rs->sr_text = "valuesReturnFilter control specified multiple times";
1559 return LDAP_PROTOCOL_ERROR;
1562 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1563 rs->sr_text = "valuesReturnFilter control value is absent";
1564 return LDAP_PROTOCOL_ERROR;
1567 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1568 rs->sr_text = "valuesReturnFilter control value is empty";
1569 return LDAP_PROTOCOL_ERROR;
1572 ber = ber_init( &(ctrl->ldctl_value) );
1574 rs->sr_text = "internal error";
1578 rs->sr_err = get_vrFilter( op, ber,
1579 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1581 (void) ber_free( ber, 1 );
1583 if( rs->sr_err != LDAP_SUCCESS ) {
1584 if( rs->sr_err == SLAPD_DISCONNECT ) {
1585 rs->sr_err = LDAP_PROTOCOL_ERROR;
1586 send_ldap_disconnect( op, rs );
1587 rs->sr_err = SLAPD_DISCONNECT;
1589 send_ldap_result( op, rs );
1591 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1595 vrFilter2bv( op, op->o_vrFilter, &fstr );
1598 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1599 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1600 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1603 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1604 ? SLAP_CONTROL_CRITICAL
1605 : SLAP_CONTROL_NONCRITICAL;
1607 rs->sr_err = LDAP_SUCCESS;
1608 return LDAP_SUCCESS;
1611 static int parseSubentries (
1616 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1617 rs->sr_text = "subentries control specified multiple times";
1618 return LDAP_PROTOCOL_ERROR;
1621 /* FIXME: should use BER library */
1622 if( ( ctrl->ldctl_value.bv_len != 3 )
1623 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1624 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1626 rs->sr_text = "subentries control value encoding is bogus";
1627 return LDAP_PROTOCOL_ERROR;
1630 op->o_subentries = ctrl->ldctl_iscritical
1631 ? SLAP_CONTROL_CRITICAL
1632 : SLAP_CONTROL_NONCRITICAL;
1634 if (ctrl->ldctl_value.bv_val[2]) {
1635 set_subentries_visibility( op );
1638 return LDAP_SUCCESS;
1641 static int parsePermissiveModify (
1646 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1647 rs->sr_text = "permissiveModify control specified multiple times";
1648 return LDAP_PROTOCOL_ERROR;
1651 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1652 rs->sr_text = "permissiveModify control value not absent";
1653 return LDAP_PROTOCOL_ERROR;
1656 op->o_permissive_modify = ctrl->ldctl_iscritical
1657 ? SLAP_CONTROL_CRITICAL
1658 : SLAP_CONTROL_NONCRITICAL;
1660 return LDAP_SUCCESS;
1663 static int parseDomainScope (
1668 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1669 rs->sr_text = "domainScope control specified multiple times";
1670 return LDAP_PROTOCOL_ERROR;
1673 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1674 rs->sr_text = "domainScope control value not empty";
1675 return LDAP_PROTOCOL_ERROR;
1678 op->o_domain_scope = ctrl->ldctl_iscritical
1679 ? SLAP_CONTROL_CRITICAL
1680 : SLAP_CONTROL_NONCRITICAL;
1682 return LDAP_SUCCESS;
1685 #ifdef SLAP_CONTROL_X_TREE_DELETE
1686 static int parseTreeDelete (
1691 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1692 rs->sr_text = "treeDelete control specified multiple times";
1693 return LDAP_PROTOCOL_ERROR;
1696 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1697 rs->sr_text = "treeDelete control value not absent";
1698 return LDAP_PROTOCOL_ERROR;
1701 op->o_tree_delete = ctrl->ldctl_iscritical
1702 ? SLAP_CONTROL_CRITICAL
1703 : SLAP_CONTROL_NONCRITICAL;
1705 return LDAP_SUCCESS;
1709 static int parseSearchOptions (
1715 ber_int_t search_flags;
1718 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1719 rs->sr_text = "searchOptions control value is absent";
1720 return LDAP_PROTOCOL_ERROR;
1723 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1724 rs->sr_text = "searchOptions control value is empty";
1725 return LDAP_PROTOCOL_ERROR;
1728 ber = ber_init( &ctrl->ldctl_value );
1730 rs->sr_text = "internal error";
1734 tag = ber_scanf( ber, "{i}", &search_flags );
1735 (void) ber_free( ber, 1 );
1737 if ( tag == LBER_ERROR ) {
1738 rs->sr_text = "searchOptions control decoding error";
1739 return LDAP_PROTOCOL_ERROR;
1742 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1743 /* Search flags not recognised so far,
1745 * LDAP_SEARCH_FLAG_PHANTOM_ROOT
1747 if ( ctrl->ldctl_iscritical ) {
1748 rs->sr_text = "searchOptions contained unrecognized flag";
1749 return LDAP_UNWILLING_TO_PERFORM;
1753 Debug( LDAP_DEBUG_TRACE,
1754 "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n",
1755 op->o_connid, (unsigned)search_flags, 0 );
1757 return LDAP_SUCCESS;
1760 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1761 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1762 rs->sr_text = "searchOptions control specified multiple times "
1763 "or with domainScope control";
1764 return LDAP_PROTOCOL_ERROR;
1767 op->o_domain_scope = ctrl->ldctl_iscritical
1768 ? SLAP_CONTROL_CRITICAL
1769 : SLAP_CONTROL_NONCRITICAL;
1772 return LDAP_SUCCESS;
1775 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1776 struct berval session_tracking_formats[] = {
1777 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1778 BER_BVC( "RADIUS-Acct-Session-Id" ),
1779 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1780 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1781 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1782 BER_BVC( "USERNAME" ),
1787 static int parseSessionTracking(
1797 struct berval sessionSourceIp = BER_BVNULL,
1798 sessionSourceName = BER_BVNULL,
1799 formatOID = BER_BVNULL,
1800 sessionTrackingIdentifier = BER_BVNULL;
1802 size_t st_len, st_pos;
1804 if ( ctrl->ldctl_iscritical ) {
1805 rs->sr_text = "sessionTracking criticality is TRUE";
1806 return LDAP_PROTOCOL_ERROR;
1809 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1810 rs->sr_text = "sessionTracking control value is absent";
1811 return LDAP_PROTOCOL_ERROR;
1814 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1815 rs->sr_text = "sessionTracking control value is empty";
1816 return LDAP_PROTOCOL_ERROR;
1819 /* TODO: add the capability to determine if a client is allowed
1820 * to use this control, based on identity, ip and so */
1822 ber = ber_init( &ctrl->ldctl_value );
1823 if ( ber == NULL ) {
1824 rs->sr_text = "internal error";
1828 tag = ber_skip_tag( ber, &len );
1829 if ( tag != LBER_SEQUENCE ) {
1834 /* sessionSourceIp */
1835 tag = ber_peek_tag( ber, &len );
1836 if ( tag == LBER_DEFAULT ) {
1842 tag = ber_skip_tag( ber, &len );
1844 } else if ( len > 128 ) {
1845 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1846 rs->sr_err = LDAP_PROTOCOL_ERROR;
1850 tag = ber_scanf( ber, "m", &sessionSourceIp );
1853 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1854 BER_BVZERO( &sessionSourceIp );
1857 /* sessionSourceName */
1858 tag = ber_peek_tag( ber, &len );
1859 if ( tag == LBER_DEFAULT ) {
1865 tag = ber_skip_tag( ber, &len );
1867 } else if ( len > 65536 ) {
1868 rs->sr_text = "sessionTracking.sessionSourceName too long";
1869 rs->sr_err = LDAP_PROTOCOL_ERROR;
1873 tag = ber_scanf( ber, "m", &sessionSourceName );
1876 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1877 BER_BVZERO( &sessionSourceName );
1881 tag = ber_peek_tag( ber, &len );
1882 if ( tag == LBER_DEFAULT ) {
1888 rs->sr_text = "sessionTracking.formatOID empty";
1889 rs->sr_err = LDAP_PROTOCOL_ERROR;
1892 } else if ( len > 1024 ) {
1893 rs->sr_text = "sessionTracking.formatOID too long";
1894 rs->sr_err = LDAP_PROTOCOL_ERROR;
1898 tag = ber_scanf( ber, "m", &formatOID );
1901 rc = numericoidValidate( NULL, &formatOID );
1902 if ( rc != LDAP_SUCCESS ) {
1903 rs->sr_text = "sessionTracking.formatOID invalid";
1907 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1909 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1910 formatOID = session_tracking_formats[ i + 1 ];
1915 /* sessionTrackingIdentifier */
1916 tag = ber_peek_tag( ber, &len );
1917 if ( tag == LBER_DEFAULT ) {
1923 tag = ber_skip_tag( ber, &len );
1926 /* note: should not be more than 65536... */
1927 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1928 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1929 /* we want the OID printed, at least */
1930 BER_BVSTR( &sessionTrackingIdentifier, "" );
1935 tag = ber_skip_tag( ber, &len );
1936 if ( tag != LBER_DEFAULT || len != 0 ) {
1943 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1944 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1946 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1947 if ( st_len ) st_len++;
1948 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1950 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1951 if ( st_len ) st_len++;
1952 st_len += formatOID.bv_len + STRLENOF( "=" )
1953 + sessionTrackingIdentifier.bv_len;
1956 if ( st_len == 0 ) {
1960 st_len += STRLENOF( " []" );
1961 st_pos = strlen( op->o_log_prefix );
1963 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1964 char *ptr = &op->o_log_prefix[ st_pos ];
1966 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1969 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1970 ptr = lutil_strcopy( ptr, "IP=" );
1971 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1975 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1976 if ( st_len ) *ptr++ = ' ';
1977 ptr = lutil_strcopy( ptr, "NAME=" );
1978 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1982 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1983 if ( st_len ) *ptr++ = ' ';
1984 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1986 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1994 (void)ber_free( ber, 1 );
1996 if ( tag == LBER_ERROR ) {
1997 rs->sr_text = "sessionTracking control decoding error";
1998 return LDAP_PROTOCOL_ERROR;
2006 slap_ctrl_session_tracking_add(
2010 struct berval *name,
2014 BerElementBuffer berbuf;
2015 BerElement *ber = (BerElement *)&berbuf;
2017 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
2019 assert( ctrl != NULL );
2021 ber_init2( ber, NULL, LBER_USE_DER );
2023 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
2025 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
2026 rs->sr_err = LDAP_OTHER;
2030 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
2031 ctrl->ldctl_iscritical = 0;
2033 rs->sr_err = LDAP_SUCCESS;
2040 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
2042 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
2043 struct berval ip = BER_BVNULL,
2047 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
2048 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
2052 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
2053 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
2055 ptr = ber_bvchr( &ip, ':' );
2057 ip.bv_len = ptr - ip.bv_val;
2061 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
2062 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
2064 name = op->o_conn->c_peer_domain;
2067 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2071 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2075 #ifdef SLAP_CONTROL_X_WHATFAILED
2076 static int parseWhatFailed(
2081 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2082 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2083 return LDAP_PROTOCOL_ERROR;
2086 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2087 rs->sr_text = "\"What Failed?\" control value not absent";
2088 return LDAP_PROTOCOL_ERROR;
2091 op->o_whatFailed = ctrl->ldctl_iscritical
2092 ? SLAP_CONTROL_CRITICAL
2093 : SLAP_CONTROL_NONCRITICAL;
2095 return LDAP_SUCCESS;
2099 slap_ctrl_whatFailed_add(
2104 BerElementBuffer berbuf;
2105 BerElement *ber = (BerElement *) &berbuf;
2106 LDAPControl **ctrls = NULL;
2107 struct berval ctrlval;
2108 int i, rc = LDAP_SUCCESS;
2110 ber_init2( ber, NULL, LBER_USE_DER );
2111 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2112 ber_printf( ber, "[" /*]*/ );
2113 for ( i = 0; oids[ i ] != NULL; i++ ) {
2114 ber_printf( ber, "s", oids[ i ] );
2116 ber_printf( ber, /*[*/ "]" );
2118 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2124 if ( rs->sr_ctrls != NULL ) {
2125 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2126 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2133 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2134 sizeof(LDAPControl *)*( i + 2 )
2135 + sizeof(LDAPControl)
2136 + ctrlval.bv_len + 1,
2138 if ( ctrls == NULL ) {
2142 ctrls[ i + 1 ] = NULL;
2143 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2144 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2145 ctrls[ i ]->ldctl_iscritical = 0;
2146 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2147 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2148 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2150 ber_free_buf( ber );
2152 rs->sr_ctrls = ctrls;