2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2011 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 );
1142 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1143 * from now on op->o_dn is normalized
1146 ber_bvreplace( &op->o_dn, &dn );
1148 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1149 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1151 return LDAP_SUCCESS;
1154 static int parseNoOp (
1159 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1160 rs->sr_text = "noop control specified multiple times";
1161 return LDAP_PROTOCOL_ERROR;
1164 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1165 rs->sr_text = "noop control value not empty";
1166 return LDAP_PROTOCOL_ERROR;
1169 op->o_noop = ctrl->ldctl_iscritical
1170 ? SLAP_CONTROL_CRITICAL
1171 : SLAP_CONTROL_NONCRITICAL;
1173 return LDAP_SUCCESS;
1176 static int parsePagedResults (
1181 BerElementBuffer berbuf;
1182 BerElement *ber = (BerElement *)&berbuf;
1183 struct berval cookie;
1184 PagedResultsState *ps;
1185 int rc = LDAP_SUCCESS;
1189 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1190 rs->sr_text = "paged results control specified multiple times";
1191 return LDAP_PROTOCOL_ERROR;
1194 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1195 rs->sr_text = "paged results control value is absent";
1196 return LDAP_PROTOCOL_ERROR;
1199 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1200 rs->sr_text = "paged results control value is empty";
1201 return LDAP_PROTOCOL_ERROR;
1204 /* Parse the control value
1205 * realSearchControlValue ::= SEQUENCE {
1206 * size INTEGER (0..maxInt),
1207 * -- requested page size from client
1208 * -- result set size estimate from server
1209 * cookie OCTET STRING
1212 ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1214 tag = ber_scanf( ber, "{im}", &size, &cookie );
1216 if ( tag == LBER_ERROR ) {
1217 rs->sr_text = "paged results control could not be decoded";
1218 rc = LDAP_PROTOCOL_ERROR;
1223 rs->sr_text = "paged results control size invalid";
1224 rc = LDAP_PROTOCOL_ERROR;
1228 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1229 *ps = op->o_conn->c_pagedresults_state;
1231 ps->ps_cookieval = cookie;
1232 op->o_pagedresults_state = ps;
1233 if ( !cookie.bv_len ) {
1236 /* taint ps_cookie, to detect whether it's set */
1237 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1240 /* NOTE: according to RFC 2696 3.:
1242 If the page size is greater than or equal to the sizeLimit value, the
1243 server should ignore the control as the request can be satisfied in a
1246 * NOTE: this assumes that the op->ors_slimit be set
1247 * before the controls are parsed.
1250 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1251 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1253 } else if ( ctrl->ldctl_iscritical ) {
1254 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1257 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1264 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1265 static int parseSortedResults (
1270 int rc = LDAP_SUCCESS;
1272 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1273 rs->sr_text = "sorted results control specified multiple times";
1274 return LDAP_PROTOCOL_ERROR;
1277 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1278 rs->sr_text = "sorted results control value is absent";
1279 return LDAP_PROTOCOL_ERROR;
1282 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1283 rs->sr_text = "sorted results control value is empty";
1284 return LDAP_PROTOCOL_ERROR;
1287 /* blow off parsing the value */
1289 op->o_sortedresults = ctrl->ldctl_iscritical
1290 ? SLAP_CONTROL_CRITICAL
1291 : SLAP_CONTROL_NONCRITICAL;
1297 static int parseAssert (
1303 struct berval fstr = BER_BVNULL;
1305 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1306 rs->sr_text = "assert control specified multiple times";
1307 return LDAP_PROTOCOL_ERROR;
1310 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1311 rs->sr_text = "assert control value is absent";
1312 return LDAP_PROTOCOL_ERROR;
1315 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1316 rs->sr_text = "assert control value is empty";
1317 return LDAP_PROTOCOL_ERROR;
1320 ber = ber_init( &(ctrl->ldctl_value) );
1322 rs->sr_text = "assert control: internal error";
1326 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1328 (void) ber_free( ber, 1 );
1329 if( rs->sr_err != LDAP_SUCCESS ) {
1330 if( rs->sr_err == SLAPD_DISCONNECT ) {
1331 rs->sr_err = LDAP_PROTOCOL_ERROR;
1332 send_ldap_disconnect( op, rs );
1333 rs->sr_err = SLAPD_DISCONNECT;
1335 send_ldap_result( op, rs );
1337 if( op->o_assertion != NULL ) {
1338 filter_free_x( op, op->o_assertion, 1 );
1344 filter2bv_x( op, op->o_assertion, &fstr );
1346 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1347 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1348 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1351 op->o_assert = ctrl->ldctl_iscritical
1352 ? SLAP_CONTROL_CRITICAL
1353 : SLAP_CONTROL_NONCRITICAL;
1355 rs->sr_err = LDAP_SUCCESS;
1356 return LDAP_SUCCESS;
1359 #define READMSG(post, msg) \
1360 ( post ? "postread control: " msg : "preread control: " msg )
1369 ber_len_t siz, off, i;
1371 AttributeName *an = NULL;
1373 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1374 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1376 rs->sr_text = READMSG( post, "specified multiple times" );
1377 return LDAP_PROTOCOL_ERROR;
1380 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1381 rs->sr_text = READMSG( post, "value is absent" );
1382 return LDAP_PROTOCOL_ERROR;
1385 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1386 rs->sr_text = READMSG( post, "value is empty" );
1387 return LDAP_PROTOCOL_ERROR;
1391 if ( op->o_txnSpec ) { /* temporary limitation */
1392 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1393 return LDAP_UNWILLING_TO_PERFORM;
1397 ber = ber_init( &ctrl->ldctl_value );
1398 if ( ber == NULL ) {
1399 rs->sr_text = READMSG( post, "internal error" );
1403 rs->sr_err = LDAP_SUCCESS;
1404 siz = sizeof( AttributeName );
1405 off = offsetof( AttributeName, an_name );
1406 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1407 rs->sr_text = READMSG( post, "decoding error" );
1408 rs->sr_err = LDAP_PROTOCOL_ERROR;
1412 for ( i = 0; i < siz; i++ ) {
1413 const char *dummy = NULL;
1416 an[i].an_desc = NULL;
1419 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1420 if ( rc == LDAP_SUCCESS ) {
1421 an[i].an_name = an[i].an_desc->ad_cname;
1425 static struct berval special_attrs[] = {
1426 BER_BVC( LDAP_NO_ATTRS ),
1427 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1428 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1432 /* deal with special attribute types */
1433 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1434 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1435 an[i].an_name = special_attrs[ j ];
1440 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1442 rs->sr_text = dummy ? dummy
1443 : READMSG( post, "unknown attributeType" );
1450 op->o_postread_attrs = an;
1451 op->o_postread = ctrl->ldctl_iscritical
1452 ? SLAP_CONTROL_CRITICAL
1453 : SLAP_CONTROL_NONCRITICAL;
1455 op->o_preread_attrs = an;
1456 op->o_preread = ctrl->ldctl_iscritical
1457 ? SLAP_CONTROL_CRITICAL
1458 : SLAP_CONTROL_NONCRITICAL;
1462 (void) ber_free( ber, 1 );
1466 static int parsePreRead (
1471 return parseReadAttrs( op, rs, ctrl, 0 );
1474 static int parsePostRead (
1479 return parseReadAttrs( op, rs, ctrl, 1 );
1482 static int parseValuesReturnFilter (
1488 struct berval fstr = BER_BVNULL;
1490 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1491 rs->sr_text = "valuesReturnFilter control specified multiple times";
1492 return LDAP_PROTOCOL_ERROR;
1495 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1496 rs->sr_text = "valuesReturnFilter control value is absent";
1497 return LDAP_PROTOCOL_ERROR;
1500 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1501 rs->sr_text = "valuesReturnFilter control value is empty";
1502 return LDAP_PROTOCOL_ERROR;
1505 ber = ber_init( &(ctrl->ldctl_value) );
1507 rs->sr_text = "internal error";
1511 rs->sr_err = get_vrFilter( op, ber,
1512 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1514 (void) ber_free( ber, 1 );
1516 if( rs->sr_err != LDAP_SUCCESS ) {
1517 if( rs->sr_err == SLAPD_DISCONNECT ) {
1518 rs->sr_err = LDAP_PROTOCOL_ERROR;
1519 send_ldap_disconnect( op, rs );
1520 rs->sr_err = SLAPD_DISCONNECT;
1522 send_ldap_result( op, rs );
1524 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1528 vrFilter2bv( op, op->o_vrFilter, &fstr );
1531 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1532 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1533 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1536 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1537 ? SLAP_CONTROL_CRITICAL
1538 : SLAP_CONTROL_NONCRITICAL;
1540 rs->sr_err = LDAP_SUCCESS;
1541 return LDAP_SUCCESS;
1544 static int parseSubentries (
1549 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1550 rs->sr_text = "subentries control specified multiple times";
1551 return LDAP_PROTOCOL_ERROR;
1554 /* FIXME: should use BER library */
1555 if( ( ctrl->ldctl_value.bv_len != 3 )
1556 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1557 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1559 rs->sr_text = "subentries control value encoding is bogus";
1560 return LDAP_PROTOCOL_ERROR;
1563 op->o_subentries = ctrl->ldctl_iscritical
1564 ? SLAP_CONTROL_CRITICAL
1565 : SLAP_CONTROL_NONCRITICAL;
1567 if (ctrl->ldctl_value.bv_val[2]) {
1568 set_subentries_visibility( op );
1571 return LDAP_SUCCESS;
1574 static int parsePermissiveModify (
1579 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1580 rs->sr_text = "permissiveModify control specified multiple times";
1581 return LDAP_PROTOCOL_ERROR;
1584 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1585 rs->sr_text = "permissiveModify control value not absent";
1586 return LDAP_PROTOCOL_ERROR;
1589 op->o_permissive_modify = ctrl->ldctl_iscritical
1590 ? SLAP_CONTROL_CRITICAL
1591 : SLAP_CONTROL_NONCRITICAL;
1593 return LDAP_SUCCESS;
1596 static int parseDomainScope (
1601 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1602 rs->sr_text = "domainScope control specified multiple times";
1603 return LDAP_PROTOCOL_ERROR;
1606 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1607 rs->sr_text = "domainScope control value not empty";
1608 return LDAP_PROTOCOL_ERROR;
1611 op->o_domain_scope = ctrl->ldctl_iscritical
1612 ? SLAP_CONTROL_CRITICAL
1613 : SLAP_CONTROL_NONCRITICAL;
1615 return LDAP_SUCCESS;
1618 #ifdef SLAP_CONTROL_X_TREE_DELETE
1619 static int parseTreeDelete (
1624 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1625 rs->sr_text = "treeDelete control specified multiple times";
1626 return LDAP_PROTOCOL_ERROR;
1629 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1630 rs->sr_text = "treeDelete control value not absent";
1631 return LDAP_PROTOCOL_ERROR;
1634 op->o_tree_delete = ctrl->ldctl_iscritical
1635 ? SLAP_CONTROL_CRITICAL
1636 : SLAP_CONTROL_NONCRITICAL;
1638 return LDAP_SUCCESS;
1642 static int parseSearchOptions (
1648 ber_int_t search_flags;
1651 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1652 rs->sr_text = "searchOptions control value is absent";
1653 return LDAP_PROTOCOL_ERROR;
1656 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1657 rs->sr_text = "searchOptions control value is empty";
1658 return LDAP_PROTOCOL_ERROR;
1661 ber = ber_init( &ctrl->ldctl_value );
1663 rs->sr_text = "internal error";
1667 tag = ber_scanf( ber, "{i}", &search_flags );
1668 (void) ber_free( ber, 1 );
1670 if ( tag == LBER_ERROR ) {
1671 rs->sr_text = "searchOptions control decoding error";
1672 return LDAP_PROTOCOL_ERROR;
1675 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1676 /* Search flags not recognised so far,
1678 * LDAP_SEARCH_FLAG_PHANTOM_ROOT
1680 if ( ctrl->ldctl_iscritical ) {
1681 rs->sr_text = "searchOptions contained unrecognized flag";
1682 return LDAP_UNWILLING_TO_PERFORM;
1686 Debug( LDAP_DEBUG_TRACE,
1687 "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n",
1688 op->o_connid, (unsigned)search_flags, 0 );
1690 return LDAP_SUCCESS;
1693 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1694 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1695 rs->sr_text = "searchOptions control specified multiple times "
1696 "or with domainScope control";
1697 return LDAP_PROTOCOL_ERROR;
1700 op->o_domain_scope = ctrl->ldctl_iscritical
1701 ? SLAP_CONTROL_CRITICAL
1702 : SLAP_CONTROL_NONCRITICAL;
1705 return LDAP_SUCCESS;
1708 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1709 struct berval session_tracking_formats[] = {
1710 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1711 BER_BVC( "RADIUS-Acct-Session-Id" ),
1712 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1713 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1714 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1715 BER_BVC( "USERNAME" ),
1720 static int parseSessionTracking(
1730 struct berval sessionSourceIp = BER_BVNULL,
1731 sessionSourceName = BER_BVNULL,
1732 formatOID = BER_BVNULL,
1733 sessionTrackingIdentifier = BER_BVNULL;
1735 size_t st_len, st_pos;
1737 if ( ctrl->ldctl_iscritical ) {
1738 rs->sr_text = "sessionTracking criticality is TRUE";
1739 return LDAP_PROTOCOL_ERROR;
1742 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1743 rs->sr_text = "sessionTracking control value is absent";
1744 return LDAP_PROTOCOL_ERROR;
1747 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1748 rs->sr_text = "sessionTracking control value is empty";
1749 return LDAP_PROTOCOL_ERROR;
1752 /* TODO: add the capability to determine if a client is allowed
1753 * to use this control, based on identity, ip and so */
1755 ber = ber_init( &ctrl->ldctl_value );
1756 if ( ber == NULL ) {
1757 rs->sr_text = "internal error";
1761 tag = ber_skip_tag( ber, &len );
1762 if ( tag != LBER_SEQUENCE ) {
1767 /* sessionSourceIp */
1768 tag = ber_peek_tag( ber, &len );
1769 if ( tag == LBER_DEFAULT ) {
1775 tag = ber_skip_tag( ber, &len );
1777 } else if ( len > 128 ) {
1778 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1779 rs->sr_err = LDAP_PROTOCOL_ERROR;
1783 tag = ber_scanf( ber, "m", &sessionSourceIp );
1786 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1787 BER_BVZERO( &sessionSourceIp );
1790 /* sessionSourceName */
1791 tag = ber_peek_tag( ber, &len );
1792 if ( tag == LBER_DEFAULT ) {
1798 tag = ber_skip_tag( ber, &len );
1800 } else if ( len > 65536 ) {
1801 rs->sr_text = "sessionTracking.sessionSourceName too long";
1802 rs->sr_err = LDAP_PROTOCOL_ERROR;
1806 tag = ber_scanf( ber, "m", &sessionSourceName );
1809 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1810 BER_BVZERO( &sessionSourceName );
1814 tag = ber_peek_tag( ber, &len );
1815 if ( tag == LBER_DEFAULT ) {
1821 rs->sr_text = "sessionTracking.formatOID empty";
1822 rs->sr_err = LDAP_PROTOCOL_ERROR;
1825 } else if ( len > 1024 ) {
1826 rs->sr_text = "sessionTracking.formatOID too long";
1827 rs->sr_err = LDAP_PROTOCOL_ERROR;
1831 tag = ber_scanf( ber, "m", &formatOID );
1834 rc = numericoidValidate( NULL, &formatOID );
1835 if ( rc != LDAP_SUCCESS ) {
1836 rs->sr_text = "sessionTracking.formatOID invalid";
1840 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1842 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1843 formatOID = session_tracking_formats[ i + 1 ];
1848 /* sessionTrackingIdentifier */
1849 tag = ber_peek_tag( ber, &len );
1850 if ( tag == LBER_DEFAULT ) {
1856 tag = ber_skip_tag( ber, &len );
1859 /* note: should not be more than 65536... */
1860 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1861 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1862 /* we want the OID printed, at least */
1863 BER_BVSTR( &sessionTrackingIdentifier, "" );
1868 tag = ber_skip_tag( ber, &len );
1869 if ( tag != LBER_DEFAULT || len != 0 ) {
1876 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1877 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1879 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1880 if ( st_len ) st_len++;
1881 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1883 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1884 if ( st_len ) st_len++;
1885 st_len += formatOID.bv_len + STRLENOF( "=" )
1886 + sessionTrackingIdentifier.bv_len;
1889 if ( st_len == 0 ) {
1893 st_len += STRLENOF( " []" );
1894 st_pos = strlen( op->o_log_prefix );
1896 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1897 char *ptr = &op->o_log_prefix[ st_pos ];
1899 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1902 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1903 ptr = lutil_strcopy( ptr, "IP=" );
1904 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1908 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1909 if ( st_len ) *ptr++ = ' ';
1910 ptr = lutil_strcopy( ptr, "NAME=" );
1911 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1915 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1916 if ( st_len ) *ptr++ = ' ';
1917 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1919 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1927 (void)ber_free( ber, 1 );
1929 if ( tag == LBER_ERROR ) {
1930 rs->sr_text = "sessionTracking control decoding error";
1931 return LDAP_PROTOCOL_ERROR;
1939 slap_ctrl_session_tracking_add(
1943 struct berval *name,
1947 BerElementBuffer berbuf;
1948 BerElement *ber = (BerElement *)&berbuf;
1950 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
1952 assert( ctrl != NULL );
1954 ber_init2( ber, NULL, LBER_USE_DER );
1956 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
1958 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
1959 rs->sr_err = LDAP_OTHER;
1963 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1964 ctrl->ldctl_iscritical = 0;
1966 rs->sr_err = LDAP_SUCCESS;
1973 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
1975 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
1976 struct berval ip = BER_BVNULL,
1980 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
1981 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1985 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
1986 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
1988 ptr = ber_bvchr( &ip, ':' );
1990 ip.bv_len = ptr - ip.bv_val;
1994 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
1995 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
1997 name = op->o_conn->c_peer_domain;
2000 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2004 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2008 #ifdef SLAP_CONTROL_X_WHATFAILED
2009 static int parseWhatFailed(
2014 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2015 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2016 return LDAP_PROTOCOL_ERROR;
2019 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2020 rs->sr_text = "\"What Failed?\" control value not absent";
2021 return LDAP_PROTOCOL_ERROR;
2024 op->o_whatFailed = ctrl->ldctl_iscritical
2025 ? SLAP_CONTROL_CRITICAL
2026 : SLAP_CONTROL_NONCRITICAL;
2028 return LDAP_SUCCESS;
2032 slap_ctrl_whatFailed_add(
2037 BerElementBuffer berbuf;
2038 BerElement *ber = (BerElement *) &berbuf;
2039 LDAPControl **ctrls = NULL;
2040 struct berval ctrlval;
2041 int i, rc = LDAP_SUCCESS;
2043 ber_init2( ber, NULL, LBER_USE_DER );
2044 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2045 ber_printf( ber, "[" /*]*/ );
2046 for ( i = 0; oids[ i ] != NULL; i++ ) {
2047 ber_printf( ber, "s", oids[ i ] );
2049 ber_printf( ber, /*[*/ "]" );
2051 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2057 if ( rs->sr_ctrls != NULL ) {
2058 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2059 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2066 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2067 sizeof(LDAPControl *)*( i + 2 )
2068 + sizeof(LDAPControl)
2069 + ctrlval.bv_len + 1,
2071 if ( ctrls == NULL ) {
2075 ctrls[ i + 1 ] = NULL;
2076 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2077 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2078 ctrls[ i ]->ldctl_iscritical = 0;
2079 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2080 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2081 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2083 ber_free_buf( ber );
2085 rs->sr_ctrls = ctrls;