2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/socket.h>
27 #include "../../libraries/liblber/lber-int.h"
29 static SLAP_CTRL_PARSE_FN parseAssert;
30 static SLAP_CTRL_PARSE_FN parseDomainScope;
31 static SLAP_CTRL_PARSE_FN parseDontUseCopy;
32 static SLAP_CTRL_PARSE_FN parseManageDSAit;
33 static SLAP_CTRL_PARSE_FN parseNoOp;
34 static SLAP_CTRL_PARSE_FN parsePagedResults;
35 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
36 static SLAP_CTRL_PARSE_FN parsePreRead, parsePostRead;
37 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
38 static SLAP_CTRL_PARSE_FN parseRelax;
39 static SLAP_CTRL_PARSE_FN parseSearchOptions;
40 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
41 static SLAP_CTRL_PARSE_FN parseSortedResults;
43 static SLAP_CTRL_PARSE_FN parseSubentries;
44 #ifdef SLAP_CONTROL_X_TREE_DELETE
45 static SLAP_CTRL_PARSE_FN parseTreeDelete;
47 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
48 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
49 static SLAP_CTRL_PARSE_FN parseSessionTracking;
51 #ifdef SLAP_CONTROL_X_WHATFAILED
52 static SLAP_CTRL_PARSE_FN parseWhatFailed;
55 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
57 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
58 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
60 struct slap_control_ids slap_cids;
66 /* The controlID for this control */
69 /* Operations supported by control */
72 /* Extended operations supported by control */
73 char **sc_extendedops; /* input */
74 BerVarray sc_extendedopsbv; /* run-time use */
76 /* Control parsing callback */
77 SLAP_CTRL_PARSE_FN *sc_parse;
79 LDAP_SLIST_ENTRY(slap_control) sc_next;
82 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
83 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
86 * all known request control OIDs should be added to this list
89 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
90 * addresses an undefined control; this allows to safely test for
91 * well known controls even if they are not registered, e.g. if
92 * they get moved to modules. An example is sc_LDAPsync, which
93 * is implemented in the syncprov overlay and thus, if configured
94 * as dynamic module, may not be registered. One side effect is that
95 * slap_known_controls[0] == NULL, so it should always be used
97 * FIXME: should we define the "undefined control" oid?
99 char *slap_known_controls[SLAP_MAX_CIDS+1];
100 static int num_known_controls = 1;
102 static char *proxy_authz_extops[] = {
103 LDAP_EXOP_MODIFY_PASSWD,
109 static char *manageDSAit_extops[] = {
114 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
115 static char *session_tracking_extops[] = {
116 LDAP_EXOP_MODIFY_PASSWD,
123 static struct slap_control control_defs[] = {
124 { LDAP_CONTROL_ASSERT,
125 (int)offsetof(struct slap_control_ids, sc_assert),
126 SLAP_CTRL_UPDATE|SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
128 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
129 { LDAP_CONTROL_PRE_READ,
130 (int)offsetof(struct slap_control_ids, sc_preRead),
131 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
133 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
134 { LDAP_CONTROL_POST_READ,
135 (int)offsetof(struct slap_control_ids, sc_postRead),
136 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
138 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 { LDAP_CONTROL_VALUESRETURNFILTER,
140 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
141 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
143 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
144 { LDAP_CONTROL_PAGEDRESULTS,
145 (int)offsetof(struct slap_control_ids, sc_pagedResults),
148 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
150 { LDAP_CONTROL_SORTREQUEST,
151 (int)offsetof(struct slap_control_ids, sc_sortedResults),
152 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
154 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
156 { LDAP_CONTROL_X_DOMAIN_SCOPE,
157 (int)offsetof(struct slap_control_ids, sc_domainScope),
158 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
160 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
161 { LDAP_CONTROL_DONTUSECOPY,
162 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
163 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
165 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
166 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
167 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
168 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
170 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
171 #ifdef SLAP_CONTROL_X_TREE_DELETE
172 { LDAP_CONTROL_X_TREE_DELETE,
173 (int)offsetof(struct slap_control_ids, sc_treeDelete),
174 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
176 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
178 { LDAP_CONTROL_X_SEARCH_OPTIONS,
179 (int)offsetof(struct slap_control_ids, sc_searchOptions),
180 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
182 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
183 { LDAP_CONTROL_SUBENTRIES,
184 (int)offsetof(struct slap_control_ids, sc_subentries),
187 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
189 (int)offsetof(struct slap_control_ids, sc_noOp),
190 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
192 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
193 { LDAP_CONTROL_RELAX,
194 (int)offsetof(struct slap_control_ids, sc_relax),
195 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
197 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
199 { LDAP_CONTROL_X_TXN_SPEC,
200 (int)offsetof(struct slap_control_ids, sc_txnSpec),
201 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
203 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
205 { LDAP_CONTROL_MANAGEDSAIT,
206 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
208 manageDSAit_extops, NULL,
209 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
210 { LDAP_CONTROL_PROXY_AUTHZ,
211 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
212 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
213 proxy_authz_extops, NULL,
214 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
215 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
216 { LDAP_CONTROL_X_SESSION_TRACKING,
217 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
218 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
219 session_tracking_extops, NULL,
220 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
222 #ifdef SLAP_CONTROL_X_WHATFAILED
223 { LDAP_CONTROL_X_WHATFAILED,
224 (int)offsetof(struct slap_control_ids, sc_whatFailed),
225 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
227 parseWhatFailed, LDAP_SLIST_ENTRY_INITIALIZER(next) },
230 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
233 static struct slap_control *
234 find_ctrl( const char *oid );
237 * Register a supported control.
239 * This can be called by an OpenLDAP plugin or, indirectly, by a
240 * SLAPI plugin calling slapi_register_supported_control().
242 * NOTE: if flags == 1 the control is replaced if already registered;
243 * otherwise registering an already registered control is not allowed.
246 register_supported_control2(const char *controloid,
247 slap_mask_t controlmask,
249 SLAP_CTRL_PARSE_FN *controlparsefn,
253 struct slap_control *sc = NULL;
255 BerVarray extendedopsbv = NULL;
257 if ( num_known_controls >= SLAP_MAX_CIDS ) {
258 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
259 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
260 SLAP_MAX_CIDS, 0, 0 );
264 if ( controloid == NULL ) {
265 return LDAP_PARAM_ERROR;
268 /* check if already registered */
269 for ( i = 0; slap_known_controls[ i ]; i++ ) {
270 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
272 Debug( LDAP_DEBUG_TRACE,
273 "Control %s already registered; replacing.\n",
275 /* (find and) replace existing handler */
276 sc = find_ctrl( controloid );
277 assert( sc != NULL );
281 Debug( LDAP_DEBUG_ANY,
282 "Control %s already registered.\n",
284 return LDAP_PARAM_ERROR;
288 /* turn compatible extended operations into bervals */
289 if ( controlexops != NULL ) {
292 for ( i = 0; controlexops[ i ]; i++ );
294 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
295 if ( extendedopsbv == NULL ) {
296 return LDAP_NO_MEMORY;
299 for ( i = 0; controlexops[ i ]; i++ ) {
300 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
305 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
307 return LDAP_NO_MEMORY;
310 sc->sc_oid = ch_strdup( controloid );
311 sc->sc_cid = num_known_controls;
313 /* Update slap_known_controls, too. */
314 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
315 slap_known_controls[num_known_controls++] = NULL;
317 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
318 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
321 if ( sc->sc_extendedopsbv ) {
322 /* FIXME: in principle, we should rather merge
323 * existing extops with those supported by the
324 * new control handling implementation.
325 * In fact, whether a control is compatible with
326 * an extop should not be a matter of implementation.
327 * We likely also need a means for a newly
328 * registered extop to declare that it is
329 * comptible with an already registered control.
331 ber_bvarray_free( sc->sc_extendedopsbv );
332 sc->sc_extendedopsbv = NULL;
333 sc->sc_extendedops = NULL;
337 sc->sc_extendedopsbv = extendedopsbv;
338 sc->sc_mask = controlmask;
339 sc->sc_parse = controlparsefn;
341 *controlcid = sc->sc_cid;
348 * One-time initialization of internal controls.
351 slap_controls_init( void )
357 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
358 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
359 rc = register_supported_control( control_defs[i].sc_oid,
360 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
361 control_defs[i].sc_parse, cid );
362 if ( rc != LDAP_SUCCESS ) break;
369 * Free memory associated with list of supported controls.
372 controls_destroy( void )
374 struct slap_control *sc;
376 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
377 sc = LDAP_SLIST_FIRST(&controls_list);
378 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
380 ch_free( sc->sc_oid );
381 if ( sc->sc_extendedopsbv != NULL ) {
382 ber_bvarray_free( sc->sc_extendedopsbv );
389 * Format the supportedControl attribute of the root DSE,
390 * detailing which controls are supported by the directory
394 controls_root_dse_info( Entry *e )
396 AttributeDescription *ad_supportedControl
397 = slap_schema.si_ad_supportedControl;
398 struct berval vals[2];
399 struct slap_control *sc;
401 vals[1].bv_val = NULL;
404 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
405 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
407 vals[0].bv_val = sc->sc_oid;
408 vals[0].bv_len = strlen( sc->sc_oid );
410 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
419 * Return a list of OIDs and operation masks for supported
420 * controls. Used by SLAPI.
423 get_supported_controls(char ***ctrloidsp,
424 slap_mask_t **ctrlmasks)
429 struct slap_control *sc;
433 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
443 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
444 if ( oids == NULL ) {
445 return LDAP_NO_MEMORY;
447 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
448 if ( masks == NULL ) {
450 return LDAP_NO_MEMORY;
455 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
456 oids[n] = ch_strdup( sc->sc_oid );
457 masks[n] = sc->sc_mask;
470 * Find a control given its OID.
472 static struct slap_control *
473 find_ctrl( const char *oid )
475 struct slap_control *sc;
477 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
478 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
487 slap_find_control_id(
491 struct slap_control *ctrl = find_ctrl( oid );
493 if ( cid ) *cid = ctrl->sc_cid;
496 return LDAP_CONTROL_NOT_FOUND;
500 slap_global_control( Operation *op, const char *oid, int *cid )
502 struct slap_control *ctrl = find_ctrl( oid );
504 if ( ctrl == NULL ) {
505 /* should not be reachable */
506 Debug( LDAP_DEBUG_ANY,
507 "slap_global_control: unrecognized control: %s\n",
509 return LDAP_CONTROL_NOT_FOUND;
512 if ( cid ) *cid = ctrl->sc_cid;
514 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
515 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
516 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
518 return LDAP_COMPARE_TRUE;
522 Debug( LDAP_DEBUG_TRACE,
523 "slap_global_control: unavailable control: %s\n",
527 return LDAP_COMPARE_FALSE;
530 void slap_free_ctrls(
532 LDAPControl **ctrls )
536 for (i=0; ctrls[i]; i++) {
537 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
539 op->o_tmpfree( ctrls, op->o_tmpmemctx );
545 LDAPControl **ctrls )
548 LDAPControl **ctrlsp;
550 if ( rs->sr_ctrls ) {
551 for ( ; rs->sr_ctrls[ i ]; i++ ) ;
554 for ( j=0; ctrls[j]; j++ ) ;
556 ctrlsp = op->o_tmpalloc(( i+j+1 )*sizeof(LDAPControl *), op->o_tmpmemctx );
558 if ( rs->sr_ctrls ) {
559 for ( ; rs->sr_ctrls[i]; i++ )
560 ctrlsp[i] = rs->sr_ctrls[i];
562 for ( j=0; ctrls[j]; j++)
563 ctrlsp[i++] = ctrls[j];
566 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED )
567 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
568 rs->sr_ctrls = ctrlsp;
569 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
576 LDAPControl *control,
579 struct slap_control *sc;
580 int rc = LDAP_SUCCESS;
582 sc = find_ctrl( control->ldctl_oid );
584 /* recognized control */
586 switch( op->o_tag ) {
588 tagmask = SLAP_CTRL_ADD;
591 tagmask = SLAP_CTRL_BIND;
593 case LDAP_REQ_COMPARE:
594 tagmask = SLAP_CTRL_COMPARE;
596 case LDAP_REQ_DELETE:
597 tagmask = SLAP_CTRL_DELETE;
599 case LDAP_REQ_MODIFY:
600 tagmask = SLAP_CTRL_MODIFY;
602 case LDAP_REQ_RENAME:
603 tagmask = SLAP_CTRL_RENAME;
605 case LDAP_REQ_SEARCH:
606 tagmask = SLAP_CTRL_SEARCH;
608 case LDAP_REQ_UNBIND:
609 tagmask = SLAP_CTRL_UNBIND;
611 case LDAP_REQ_ABANDON:
612 tagmask = SLAP_CTRL_ABANDON;
614 case LDAP_REQ_EXTENDED:
616 assert( op->ore_reqoid.bv_val != NULL );
617 if( sc->sc_extendedopsbv != NULL ) {
619 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
620 if( bvmatch( &op->ore_reqoid,
621 &sc->sc_extendedopsbv[i] ) )
630 *text = "controls internal error";
634 if (( sc->sc_mask & tagmask ) == tagmask ) {
635 /* available extension */
636 if ( sc->sc_parse ) {
637 rc = sc->sc_parse( op, rs, control );
638 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
640 } else if ( control->ldctl_iscritical ) {
641 *text = "not yet implemented";
646 } else if ( control->ldctl_iscritical ) {
647 /* unavailable CRITICAL control */
648 *text = "critical extension is unavailable";
649 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
652 } else if ( control->ldctl_iscritical ) {
653 /* unrecognized CRITICAL control */
654 *text = "critical extension is not recognized";
655 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
670 BerElement *ber = op->o_ber;
672 #ifdef SLAP_CONTROL_X_WHATFAILED
673 /* NOTE: right now, slapd checks the validity of each control
674 * while parsing. As a consequence, it can only detect one
675 * cause of failure at a time. This results in returning
676 * exactly one OID with the whatFailed control, or no control
679 char *failed_oid = NULL;
682 len = ber_pvt_ber_remaining(ber);
686 rs->sr_err = LDAP_SUCCESS;
690 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
691 if( tag == LBER_ERROR ) {
692 rs->sr_err = SLAPD_DISCONNECT;
693 rs->sr_text = "unexpected data in PDU";
699 Debug( LDAP_DEBUG_TRACE,
700 "=> get_ctrls\n", 0, 0, 0 );
702 if( op->o_protocol < LDAP_VERSION3 ) {
703 rs->sr_err = SLAPD_DISCONNECT;
704 rs->sr_text = "controls require LDAPv3";
708 /* one for first control, one for termination */
709 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
712 if( op->ctrls == NULL ) {
713 rs->sr_err = LDAP_NO_MEMORY;
714 rs->sr_text = "no memory";
719 op->o_ctrls[nctrls] = NULL;
721 /* step through each element */
722 for( tag = ber_first_element( ber, &len, &opaque );
724 tag = ber_next_element( ber, &len, opaque ) )
727 LDAPControl **tctrls;
729 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
730 memset(c, 0, sizeof(LDAPControl));
732 /* allocate pointer space for current controls (nctrls)
733 * + this control + extra NULL
735 tctrls = op->o_tmprealloc( op->o_ctrls,
736 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
739 if( tctrls == NULL ) {
741 ldap_controls_free(op->o_ctrls);
744 rs->sr_err = LDAP_NO_MEMORY;
745 rs->sr_text = "no memory";
749 op->o_ctrls = tctrls;
751 op->o_ctrls[nctrls++] = c;
752 op->o_ctrls[nctrls] = NULL;
754 tag = ber_scanf( ber, "{m" /*}*/, &bv );
755 c->ldctl_oid = bv.bv_val;
757 if( tag == LBER_ERROR ) {
758 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
761 slap_free_ctrls( op, op->o_ctrls );
763 rs->sr_err = SLAPD_DISCONNECT;
764 rs->sr_text = "decoding controls error";
767 } else if( c->ldctl_oid == NULL ) {
768 Debug( LDAP_DEBUG_TRACE,
769 "get_ctrls: conn %lu got emtpy OID.\n",
770 op->o_connid, 0, 0 );
772 slap_free_ctrls( op, op->o_ctrls );
774 rs->sr_err = LDAP_PROTOCOL_ERROR;
775 rs->sr_text = "OID field is empty";
779 tag = ber_peek_tag( ber, &len );
781 if( tag == LBER_BOOLEAN ) {
783 tag = ber_scanf( ber, "b", &crit );
785 if( tag == LBER_ERROR ) {
786 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
788 slap_free_ctrls( op, op->o_ctrls );
790 rs->sr_err = SLAPD_DISCONNECT;
791 rs->sr_text = "decoding controls error";
795 c->ldctl_iscritical = (crit != 0);
796 tag = ber_peek_tag( ber, &len );
799 if( tag == LBER_OCTETSTRING ) {
800 tag = ber_scanf( ber, "m", &c->ldctl_value );
802 if( tag == LBER_ERROR ) {
803 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
804 "%s (%scritical): get value failed.\n",
805 op->o_connid, c->ldctl_oid,
806 c->ldctl_iscritical ? "" : "non" );
807 slap_free_ctrls( op, op->o_ctrls );
809 rs->sr_err = SLAPD_DISCONNECT;
810 rs->sr_text = "decoding controls error";
815 Debug( LDAP_DEBUG_TRACE,
816 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
817 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
819 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
820 if ( rs->sr_err != LDAP_SUCCESS ) {
821 #ifdef SLAP_CONTROL_X_WHATFAILED
822 failed_oid = c->ldctl_oid;
829 Debug( LDAP_DEBUG_TRACE,
830 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
831 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
833 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
834 if( rs->sr_err == SLAPD_DISCONNECT ) {
835 rs->sr_err = LDAP_PROTOCOL_ERROR;
836 send_ldap_disconnect( op, rs );
837 rs->sr_err = SLAPD_DISCONNECT;
839 #ifdef SLAP_CONTROL_X_WHATFAILED
840 /* might have not been parsed yet? */
841 if ( failed_oid != NULL ) {
842 if ( !get_whatFailed( op ) ) {
845 /* step through each remaining element */
846 for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
848 LDAPControl c = { 0 };
850 tag = ber_scanf( ber, "{m" /*}*/, &bv );
851 c.ldctl_oid = bv.bv_val;
853 if ( tag == LBER_ERROR ) {
854 slap_free_ctrls( op, op->o_ctrls );
858 } else if ( c.ldctl_oid == NULL ) {
859 slap_free_ctrls( op, op->o_ctrls );
864 tag = ber_peek_tag( ber, &len );
865 if ( tag == LBER_BOOLEAN ) {
867 tag = ber_scanf( ber, "b", &crit );
868 if( tag == LBER_ERROR ) {
869 slap_free_ctrls( op, op->o_ctrls );
874 tag = ber_peek_tag( ber, &len );
877 if ( tag == LBER_OCTETSTRING ) {
878 tag = ber_scanf( ber, "m", &c.ldctl_value );
880 if( tag == LBER_ERROR ) {
881 slap_free_ctrls( op, op->o_ctrls );
887 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
889 slap_parse_ctrl( op, rs, &c, &text );
895 if ( get_whatFailed( op ) ) {
897 oids[ 0 ] = failed_oid;
899 slap_ctrl_whatFailed_add( op, rs, oids );
904 send_ldap_result( op, rs );
916 BI_chk_controls fnc )
920 switch ( op->o_ctrlflag[ ctrl ] ) {
921 case SLAP_CONTROL_NONCRITICAL:
922 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
923 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
924 slap_known_controls[ ctrl - 1 ] ) == 0 )
931 rs->sr_err = LDAP_OTHER;
939 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
942 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
943 ( i - j ) * sizeof( LDAPControl * ) );
946 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
950 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
952 Debug( LDAP_DEBUG_ANY, "%s: "
953 "non-critical control \"%s\" not supported; stripped.\n",
954 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
957 case SLAP_CONTROL_IGNORED:
958 case SLAP_CONTROL_NONE:
959 rs->sr_err = SLAP_CB_CONTINUE;
962 case SLAP_CONTROL_CRITICAL:
963 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
967 Debug( LDAP_DEBUG_ANY, "%s: "
968 "critical control \"%s\" not supported.\n",
969 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
973 /* handle all cases! */
980 static int parseDontUseCopy (
985 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
986 rs->sr_text = "dontUseCopy control specified multiple times";
987 return LDAP_PROTOCOL_ERROR;
990 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
991 rs->sr_text = "dontUseCopy control value not absent";
992 return LDAP_PROTOCOL_ERROR;
995 if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
996 && !ctrl->ldctl_iscritical )
998 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
999 return LDAP_PROTOCOL_ERROR;
1002 op->o_dontUseCopy = ctrl->ldctl_iscritical
1003 ? SLAP_CONTROL_CRITICAL
1004 : SLAP_CONTROL_NONCRITICAL;
1006 return LDAP_SUCCESS;
1009 static int parseRelax (
1014 if ( op->o_relax != SLAP_CONTROL_NONE ) {
1015 rs->sr_text = "relax control specified multiple times";
1016 return LDAP_PROTOCOL_ERROR;
1019 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1020 rs->sr_text = "relax control value not absent";
1021 return LDAP_PROTOCOL_ERROR;
1024 op->o_relax = ctrl->ldctl_iscritical
1025 ? SLAP_CONTROL_CRITICAL
1026 : SLAP_CONTROL_NONCRITICAL;
1028 return LDAP_SUCCESS;
1031 static int parseManageDSAit (
1036 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1037 rs->sr_text = "manageDSAit control specified multiple times";
1038 return LDAP_PROTOCOL_ERROR;
1041 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1042 rs->sr_text = "manageDSAit control value not absent";
1043 return LDAP_PROTOCOL_ERROR;
1046 op->o_managedsait = ctrl->ldctl_iscritical
1047 ? SLAP_CONTROL_CRITICAL
1048 : SLAP_CONTROL_NONCRITICAL;
1050 return LDAP_SUCCESS;
1053 static int parseProxyAuthz (
1059 struct berval dn = BER_BVNULL;
1061 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1062 rs->sr_text = "proxy authorization control specified multiple times";
1063 return LDAP_PROTOCOL_ERROR;
1066 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1067 rs->sr_text = "proxy authorization control value absent";
1068 return LDAP_PROTOCOL_ERROR;
1071 if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1072 && !ctrl->ldctl_iscritical )
1074 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1075 return LDAP_PROTOCOL_ERROR;
1078 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1079 && BER_BVISEMPTY( &op->o_ndn ) )
1081 rs->sr_text = "anonymous proxied authorization not allowed";
1082 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1085 op->o_proxy_authz = ctrl->ldctl_iscritical
1086 ? SLAP_CONTROL_CRITICAL
1087 : SLAP_CONTROL_NONCRITICAL;
1089 Debug( LDAP_DEBUG_ARGS,
1090 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
1092 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
1095 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1096 Debug( LDAP_DEBUG_TRACE,
1097 "parseProxyAuthz: conn=%lu anonymous\n",
1098 op->o_connid, 0, 0 );
1101 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1102 op->o_ndn.bv_val[ 0 ] = '\0';
1104 op->o_ndn.bv_len = 0;
1106 if ( !BER_BVISNULL( &op->o_dn ) ) {
1107 op->o_dn.bv_val[ 0 ] = '\0';
1109 op->o_dn.bv_len = 0;
1111 return LDAP_SUCCESS;
1114 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1115 NULL, &dn, SLAP_GETDN_AUTHZID );
1117 /* FIXME: empty DN in proxyAuthz control should be legal... */
1118 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1120 ch_free( dn.bv_val );
1122 rs->sr_text = "authzId mapping failed";
1123 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1126 Debug( LDAP_DEBUG_TRACE,
1127 "parseProxyAuthz: conn=%lu \"%s\"\n",
1129 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1131 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1134 ch_free( dn.bv_val );
1135 rs->sr_text = "not authorized to assume identity";
1136 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1139 ch_free( op->o_ndn.bv_val );
1140 ch_free( op->o_dn.bv_val );
1143 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1144 * from now on op->o_dn is normalized
1147 ber_dupbv( &op->o_dn, &dn );
1149 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1150 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1152 return LDAP_SUCCESS;
1155 static int parseNoOp (
1160 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1161 rs->sr_text = "noop control specified multiple times";
1162 return LDAP_PROTOCOL_ERROR;
1165 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1166 rs->sr_text = "noop control value not empty";
1167 return LDAP_PROTOCOL_ERROR;
1170 op->o_noop = ctrl->ldctl_iscritical
1171 ? SLAP_CONTROL_CRITICAL
1172 : SLAP_CONTROL_NONCRITICAL;
1174 return LDAP_SUCCESS;
1177 static int parsePagedResults (
1182 BerElementBuffer berbuf;
1183 BerElement *ber = (BerElement *)&berbuf;
1184 struct berval cookie;
1185 PagedResultsState *ps;
1186 int rc = LDAP_SUCCESS;
1190 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1191 rs->sr_text = "paged results control specified multiple times";
1192 return LDAP_PROTOCOL_ERROR;
1195 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1196 rs->sr_text = "paged results control value is absent";
1197 return LDAP_PROTOCOL_ERROR;
1200 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1201 rs->sr_text = "paged results control value is empty";
1202 return LDAP_PROTOCOL_ERROR;
1205 /* Parse the control value
1206 * realSearchControlValue ::= SEQUENCE {
1207 * size INTEGER (0..maxInt),
1208 * -- requested page size from client
1209 * -- result set size estimate from server
1210 * cookie OCTET STRING
1213 ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1215 tag = ber_scanf( ber, "{im}", &size, &cookie );
1217 if ( tag == LBER_ERROR ) {
1218 rs->sr_text = "paged results control could not be decoded";
1219 rc = LDAP_PROTOCOL_ERROR;
1224 rs->sr_text = "paged results control size invalid";
1225 rc = LDAP_PROTOCOL_ERROR;
1229 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1230 *ps = op->o_conn->c_pagedresults_state;
1232 ps->ps_cookieval = cookie;
1233 op->o_pagedresults_state = ps;
1234 if ( !cookie.bv_len ) {
1239 /* NOTE: according to RFC 2696 3.:
1241 If the page size is greater than or equal to the sizeLimit value, the
1242 server should ignore the control as the request can be satisfied in a
1245 * NOTE: this assumes that the op->ors_slimit be set
1246 * before the controls are parsed.
1249 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1250 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1252 } else if ( ctrl->ldctl_iscritical ) {
1253 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1256 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1263 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1264 static int parseSortedResults (
1269 int rc = LDAP_SUCCESS;
1271 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1272 rs->sr_text = "sorted results control specified multiple times";
1273 return LDAP_PROTOCOL_ERROR;
1276 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1277 rs->sr_text = "sorted results control value is absent";
1278 return LDAP_PROTOCOL_ERROR;
1281 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1282 rs->sr_text = "sorted results control value is empty";
1283 return LDAP_PROTOCOL_ERROR;
1286 /* blow off parsing the value */
1288 op->o_sortedresults = ctrl->ldctl_iscritical
1289 ? SLAP_CONTROL_CRITICAL
1290 : SLAP_CONTROL_NONCRITICAL;
1296 static int parseAssert (
1302 struct berval fstr = BER_BVNULL;
1304 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1305 rs->sr_text = "assert control specified multiple times";
1306 return LDAP_PROTOCOL_ERROR;
1309 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1310 rs->sr_text = "assert control value is absent";
1311 return LDAP_PROTOCOL_ERROR;
1314 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1315 rs->sr_text = "assert control value is empty";
1316 return LDAP_PROTOCOL_ERROR;
1319 ber = ber_init( &(ctrl->ldctl_value) );
1321 rs->sr_text = "assert control: internal error";
1325 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1327 (void) ber_free( ber, 1 );
1328 if( rs->sr_err != LDAP_SUCCESS ) {
1329 if( rs->sr_err == SLAPD_DISCONNECT ) {
1330 rs->sr_err = LDAP_PROTOCOL_ERROR;
1331 send_ldap_disconnect( op, rs );
1332 rs->sr_err = SLAPD_DISCONNECT;
1334 send_ldap_result( op, rs );
1336 if( op->o_assertion != NULL ) {
1337 filter_free_x( op, op->o_assertion, 1 );
1343 filter2bv_x( op, op->o_assertion, &fstr );
1345 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1346 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1347 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1350 op->o_assert = ctrl->ldctl_iscritical
1351 ? SLAP_CONTROL_CRITICAL
1352 : SLAP_CONTROL_NONCRITICAL;
1354 rs->sr_err = LDAP_SUCCESS;
1355 return LDAP_SUCCESS;
1358 #define READMSG(post, msg) \
1359 ( post ? "postread control: " msg : "preread control: " msg )
1368 ber_len_t siz, off, i;
1370 AttributeName *an = NULL;
1372 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1373 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1375 rs->sr_text = READMSG( post, "specified multiple times" );
1376 return LDAP_PROTOCOL_ERROR;
1379 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1380 rs->sr_text = READMSG( post, "value is absent" );
1381 return LDAP_PROTOCOL_ERROR;
1384 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1385 rs->sr_text = READMSG( post, "value is empty" );
1386 return LDAP_PROTOCOL_ERROR;
1390 if ( op->o_txnSpec ) { /* temporary limitation */
1391 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1392 return LDAP_UNWILLING_TO_PERFORM;
1396 ber = ber_init( &ctrl->ldctl_value );
1397 if ( ber == NULL ) {
1398 rs->sr_text = READMSG( post, "internal error" );
1402 rs->sr_err = LDAP_SUCCESS;
1403 siz = sizeof( AttributeName );
1404 off = offsetof( AttributeName, an_name );
1405 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1406 rs->sr_text = READMSG( post, "decoding error" );
1407 rs->sr_err = LDAP_PROTOCOL_ERROR;
1411 for ( i = 0; i < siz; i++ ) {
1412 const char *dummy = NULL;
1415 an[i].an_desc = NULL;
1418 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1419 if ( rc == LDAP_SUCCESS ) {
1420 an[i].an_name = an[i].an_desc->ad_cname;
1424 static struct berval special_attrs[] = {
1425 BER_BVC( LDAP_NO_ATTRS ),
1426 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1427 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1431 /* deal with special attribute types */
1432 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1433 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1434 an[i].an_name = special_attrs[ j ];
1439 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1441 rs->sr_text = dummy ? dummy
1442 : READMSG( post, "unknown attributeType" );
1449 op->o_postread_attrs = an;
1450 op->o_postread = ctrl->ldctl_iscritical
1451 ? SLAP_CONTROL_CRITICAL
1452 : SLAP_CONTROL_NONCRITICAL;
1454 op->o_preread_attrs = an;
1455 op->o_preread = ctrl->ldctl_iscritical
1456 ? SLAP_CONTROL_CRITICAL
1457 : SLAP_CONTROL_NONCRITICAL;
1461 (void) ber_free( ber, 1 );
1465 static int parsePreRead (
1470 return parseReadAttrs( op, rs, ctrl, 0 );
1473 static int parsePostRead (
1478 return parseReadAttrs( op, rs, ctrl, 1 );
1481 static int parseValuesReturnFilter (
1487 struct berval fstr = BER_BVNULL;
1489 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1490 rs->sr_text = "valuesReturnFilter control specified multiple times";
1491 return LDAP_PROTOCOL_ERROR;
1494 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1495 rs->sr_text = "valuesReturnFilter control value is absent";
1496 return LDAP_PROTOCOL_ERROR;
1499 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1500 rs->sr_text = "valuesReturnFilter control value is empty";
1501 return LDAP_PROTOCOL_ERROR;
1504 ber = ber_init( &(ctrl->ldctl_value) );
1506 rs->sr_text = "internal error";
1510 rs->sr_err = get_vrFilter( op, ber,
1511 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1513 (void) ber_free( ber, 1 );
1515 if( rs->sr_err != LDAP_SUCCESS ) {
1516 if( rs->sr_err == SLAPD_DISCONNECT ) {
1517 rs->sr_err = LDAP_PROTOCOL_ERROR;
1518 send_ldap_disconnect( op, rs );
1519 rs->sr_err = SLAPD_DISCONNECT;
1521 send_ldap_result( op, rs );
1523 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1527 vrFilter2bv( op, op->o_vrFilter, &fstr );
1530 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1531 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1532 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1535 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1536 ? SLAP_CONTROL_CRITICAL
1537 : SLAP_CONTROL_NONCRITICAL;
1539 rs->sr_err = LDAP_SUCCESS;
1540 return LDAP_SUCCESS;
1543 static int parseSubentries (
1548 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1549 rs->sr_text = "subentries control specified multiple times";
1550 return LDAP_PROTOCOL_ERROR;
1553 /* FIXME: should use BER library */
1554 if( ( ctrl->ldctl_value.bv_len != 3 )
1555 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1556 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1558 rs->sr_text = "subentries control value encoding is bogus";
1559 return LDAP_PROTOCOL_ERROR;
1562 op->o_subentries = ctrl->ldctl_iscritical
1563 ? SLAP_CONTROL_CRITICAL
1564 : SLAP_CONTROL_NONCRITICAL;
1566 if (ctrl->ldctl_value.bv_val[2]) {
1567 set_subentries_visibility( op );
1570 return LDAP_SUCCESS;
1573 static int parsePermissiveModify (
1578 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1579 rs->sr_text = "permissiveModify control specified multiple times";
1580 return LDAP_PROTOCOL_ERROR;
1583 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1584 rs->sr_text = "permissiveModify control value not absent";
1585 return LDAP_PROTOCOL_ERROR;
1588 op->o_permissive_modify = ctrl->ldctl_iscritical
1589 ? SLAP_CONTROL_CRITICAL
1590 : SLAP_CONTROL_NONCRITICAL;
1592 return LDAP_SUCCESS;
1595 static int parseDomainScope (
1600 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1601 rs->sr_text = "domainScope control specified multiple times";
1602 return LDAP_PROTOCOL_ERROR;
1605 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1606 rs->sr_text = "domainScope control value not empty";
1607 return LDAP_PROTOCOL_ERROR;
1610 op->o_domain_scope = ctrl->ldctl_iscritical
1611 ? SLAP_CONTROL_CRITICAL
1612 : SLAP_CONTROL_NONCRITICAL;
1614 return LDAP_SUCCESS;
1617 #ifdef SLAP_CONTROL_X_TREE_DELETE
1618 static int parseTreeDelete (
1623 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1624 rs->sr_text = "treeDelete control specified multiple times";
1625 return LDAP_PROTOCOL_ERROR;
1628 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1629 rs->sr_text = "treeDelete control value not absent";
1630 return LDAP_PROTOCOL_ERROR;
1633 op->o_tree_delete = ctrl->ldctl_iscritical
1634 ? SLAP_CONTROL_CRITICAL
1635 : SLAP_CONTROL_NONCRITICAL;
1637 return LDAP_SUCCESS;
1641 static int parseSearchOptions (
1647 ber_int_t search_flags;
1650 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1651 rs->sr_text = "searchOptions control value is absent";
1652 return LDAP_PROTOCOL_ERROR;
1655 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1656 rs->sr_text = "searchOptions control value is empty";
1657 return LDAP_PROTOCOL_ERROR;
1660 ber = ber_init( &ctrl->ldctl_value );
1662 rs->sr_text = "internal error";
1666 tag = ber_scanf( ber, "{i}", &search_flags );
1667 (void) ber_free( ber, 1 );
1669 if ( tag == LBER_ERROR ) {
1670 rs->sr_text = "searchOptions control decoding error";
1671 return LDAP_PROTOCOL_ERROR;
1674 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1675 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1676 rs->sr_text = "searchOptions control specified multiple times "
1677 "or with domainScope control";
1678 return LDAP_PROTOCOL_ERROR;
1681 op->o_domain_scope = ctrl->ldctl_iscritical
1682 ? SLAP_CONTROL_CRITICAL
1683 : SLAP_CONTROL_NONCRITICAL;
1686 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1687 /* Other search flags not recognised so far,
1689 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1691 rs->sr_text = "searchOptions contained unrecognized flag";
1692 return LDAP_UNWILLING_TO_PERFORM;
1695 return LDAP_SUCCESS;
1698 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1699 struct berval session_tracking_formats[] = {
1700 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1701 BER_BVC( "RADIUS-Acct-Session-Id" ),
1702 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1703 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1704 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1705 BER_BVC( "USERNAME" ),
1710 static int parseSessionTracking(
1720 struct berval sessionSourceIp = BER_BVNULL,
1721 sessionSourceName = BER_BVNULL,
1722 formatOID = BER_BVNULL,
1723 sessionTrackingIdentifier = BER_BVNULL;
1725 size_t st_len, st_pos;
1727 if ( ctrl->ldctl_iscritical ) {
1728 rs->sr_text = "sessionTracking criticality is TRUE";
1729 return LDAP_PROTOCOL_ERROR;
1732 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1733 rs->sr_text = "sessionTracking control value is absent";
1734 return LDAP_PROTOCOL_ERROR;
1737 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1738 rs->sr_text = "sessionTracking control value is empty";
1739 return LDAP_PROTOCOL_ERROR;
1742 /* TODO: add the capability to determine if a client is allowed
1743 * to use this control, based on identity, ip and so */
1745 ber = ber_init( &ctrl->ldctl_value );
1746 if ( ber == NULL ) {
1747 rs->sr_text = "internal error";
1751 tag = ber_skip_tag( ber, &len );
1752 if ( tag != LBER_SEQUENCE ) {
1757 /* sessionSourceIp */
1758 tag = ber_peek_tag( ber, &len );
1759 if ( tag == LBER_DEFAULT ) {
1765 tag = ber_skip_tag( ber, &len );
1767 } else if ( len > 128 ) {
1768 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1769 rs->sr_err = LDAP_PROTOCOL_ERROR;
1773 tag = ber_scanf( ber, "m", &sessionSourceIp );
1776 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1777 BER_BVZERO( &sessionSourceIp );
1780 /* sessionSourceName */
1781 tag = ber_peek_tag( ber, &len );
1782 if ( tag == LBER_DEFAULT ) {
1788 tag = ber_skip_tag( ber, &len );
1790 } else if ( len > 65536 ) {
1791 rs->sr_text = "sessionTracking.sessionSourceName too long";
1792 rs->sr_err = LDAP_PROTOCOL_ERROR;
1796 tag = ber_scanf( ber, "m", &sessionSourceName );
1799 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1800 BER_BVZERO( &sessionSourceName );
1804 tag = ber_peek_tag( ber, &len );
1805 if ( tag == LBER_DEFAULT ) {
1811 rs->sr_text = "sessionTracking.formatOID empty";
1812 rs->sr_err = LDAP_PROTOCOL_ERROR;
1815 } else if ( len > 1024 ) {
1816 rs->sr_text = "sessionTracking.formatOID too long";
1817 rs->sr_err = LDAP_PROTOCOL_ERROR;
1821 tag = ber_scanf( ber, "m", &formatOID );
1824 rc = numericoidValidate( NULL, &formatOID );
1825 if ( rc != LDAP_SUCCESS ) {
1826 rs->sr_text = "sessionTracking.formatOID invalid";
1830 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1832 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1833 formatOID = session_tracking_formats[ i + 1 ];
1838 /* sessionTrackingIdentifier */
1839 tag = ber_peek_tag( ber, &len );
1840 if ( tag == LBER_DEFAULT ) {
1846 tag = ber_skip_tag( ber, &len );
1849 /* note: should not be more than 65536... */
1850 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1851 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1852 /* we want the OID printed, at least */
1853 BER_BVSTR( &sessionTrackingIdentifier, "" );
1858 tag = ber_skip_tag( ber, &len );
1859 if ( tag != LBER_DEFAULT || len != 0 ) {
1866 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1867 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1869 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1870 if ( st_len ) st_len++;
1871 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1873 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1874 if ( st_len ) st_len++;
1875 st_len += formatOID.bv_len + STRLENOF( "=" )
1876 + sessionTrackingIdentifier.bv_len;
1879 if ( st_len == 0 ) {
1883 st_len += STRLENOF( " []" );
1884 st_pos = strlen( op->o_log_prefix );
1886 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1887 char *ptr = &op->o_log_prefix[ st_pos ];
1889 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1892 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1893 ptr = lutil_strcopy( ptr, "IP=" );
1894 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1898 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1899 if ( st_len ) *ptr++ = ' ';
1900 ptr = lutil_strcopy( ptr, "NAME=" );
1901 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1905 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1906 if ( st_len ) *ptr++ = ' ';
1907 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1909 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1917 (void)ber_free( ber, 1 );
1919 if ( tag == LBER_ERROR ) {
1920 rs->sr_text = "sessionTracking control decoding error";
1921 return LDAP_PROTOCOL_ERROR;
1929 slap_ctrl_session_tracking_add(
1933 struct berval *name,
1937 BerElementBuffer berbuf;
1938 BerElement *ber = (BerElement *)&berbuf;
1940 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
1942 assert( ctrl != NULL );
1944 ber_init2( ber, NULL, LBER_USE_DER );
1946 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
1948 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
1949 rs->sr_err = LDAP_OTHER;
1953 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1954 ctrl->ldctl_iscritical = 0;
1956 rs->sr_err = LDAP_SUCCESS;
1963 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
1965 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
1966 struct berval ip = BER_BVNULL,
1970 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
1971 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1975 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
1976 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
1978 ptr = ber_bvchr( &ip, ':' );
1980 ip.bv_len = ptr - ip.bv_val;
1984 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
1985 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
1987 name = op->o_conn->c_peer_domain;
1990 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
1994 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
1998 #ifdef SLAP_CONTROL_X_WHATFAILED
1999 static int parseWhatFailed(
2004 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2005 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2006 return LDAP_PROTOCOL_ERROR;
2009 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2010 rs->sr_text = "\"What Failed?\" control value not absent";
2011 return LDAP_PROTOCOL_ERROR;
2014 op->o_whatFailed = ctrl->ldctl_iscritical
2015 ? SLAP_CONTROL_CRITICAL
2016 : SLAP_CONTROL_NONCRITICAL;
2018 return LDAP_SUCCESS;
2022 slap_ctrl_whatFailed_add(
2027 BerElementBuffer berbuf;
2028 BerElement *ber = (BerElement *) &berbuf;
2029 LDAPControl **ctrls = NULL;
2030 struct berval ctrlval;
2031 int i, rc = LDAP_SUCCESS;
2033 ber_init2( ber, NULL, LBER_USE_DER );
2034 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2035 ber_printf( ber, "[" /*]*/ );
2036 for ( i = 0; oids[ i ] != NULL; i++ ) {
2037 ber_printf( ber, "s", oids[ i ] );
2039 ber_printf( ber, /*[*/ "]" );
2041 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2047 if ( rs->sr_ctrls != NULL ) {
2048 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2049 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2056 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2057 sizeof(LDAPControl *)*( i + 2 )
2058 + sizeof(LDAPControl)
2059 + ctrlval.bv_len + 1,
2061 if ( ctrls == NULL ) {
2065 ctrls[ i + 1 ] = NULL;
2066 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2067 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2068 ctrls[ i ]->ldctl_iscritical = 0;
2069 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2070 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2071 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2073 ber_free_buf( ber );
2075 rs->sr_ctrls = ctrls;