2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/socket.h>
27 #include "../../libraries/liblber/lber-int.h"
29 static SLAP_CTRL_PARSE_FN parseAssert;
30 static SLAP_CTRL_PARSE_FN parseDomainScope;
31 static SLAP_CTRL_PARSE_FN parseDontUseCopy;
32 static SLAP_CTRL_PARSE_FN parseManageDSAit;
33 static SLAP_CTRL_PARSE_FN parseNoOp;
34 static SLAP_CTRL_PARSE_FN parsePagedResults;
35 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
36 static SLAP_CTRL_PARSE_FN parsePreRead, parsePostRead;
37 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
38 static SLAP_CTRL_PARSE_FN parseRelax;
39 static SLAP_CTRL_PARSE_FN parseSearchOptions;
40 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
41 static SLAP_CTRL_PARSE_FN parseSortedResults;
43 static SLAP_CTRL_PARSE_FN parseSubentries;
44 #ifdef SLAP_CONTROL_X_TREE_DELETE
45 static SLAP_CTRL_PARSE_FN parseTreeDelete;
47 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
48 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
49 static SLAP_CTRL_PARSE_FN parseSessionTracking;
51 #ifdef SLAP_CONTROL_X_WHATFAILED
52 static SLAP_CTRL_PARSE_FN parseWhatFailed;
55 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
57 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
58 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
60 struct slap_control_ids slap_cids;
66 /* The controlID for this control */
69 /* Operations supported by control */
72 /* Extended operations supported by control */
73 char **sc_extendedops; /* input */
74 BerVarray sc_extendedopsbv; /* run-time use */
76 /* Control parsing callback */
77 SLAP_CTRL_PARSE_FN *sc_parse;
79 LDAP_SLIST_ENTRY(slap_control) sc_next;
82 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
83 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
86 * all known request control OIDs should be added to this list
89 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
90 * addresses an undefined control; this allows to safely test for
91 * well known controls even if they are not registered, e.g. if
92 * they get moved to modules. An example is sc_LDAPsync, which
93 * is implemented in the syncprov overlay and thus, if configured
94 * as dynamic module, may not be registered. One side effect is that
95 * slap_known_controls[0] == NULL, so it should always be used
97 * FIXME: should we define the "undefined control" oid?
99 char *slap_known_controls[SLAP_MAX_CIDS+1];
100 static int num_known_controls = 1;
102 static char *proxy_authz_extops[] = {
103 LDAP_EXOP_MODIFY_PASSWD,
109 static char *manageDSAit_extops[] = {
114 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
115 static char *session_tracking_extops[] = {
116 LDAP_EXOP_MODIFY_PASSWD,
123 static struct slap_control control_defs[] = {
124 { LDAP_CONTROL_ASSERT,
125 (int)offsetof(struct slap_control_ids, sc_assert),
126 SLAP_CTRL_UPDATE|SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
128 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
129 { LDAP_CONTROL_PRE_READ,
130 (int)offsetof(struct slap_control_ids, sc_preRead),
131 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
133 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
134 { LDAP_CONTROL_POST_READ,
135 (int)offsetof(struct slap_control_ids, sc_postRead),
136 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
138 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 { LDAP_CONTROL_VALUESRETURNFILTER,
140 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
141 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
143 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
144 { LDAP_CONTROL_PAGEDRESULTS,
145 (int)offsetof(struct slap_control_ids, sc_pagedResults),
148 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
150 { LDAP_CONTROL_SORTREQUEST,
151 (int)offsetof(struct slap_control_ids, sc_sortedResults),
152 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
154 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
156 { LDAP_CONTROL_X_DOMAIN_SCOPE,
157 (int)offsetof(struct slap_control_ids, sc_domainScope),
158 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
160 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
161 { LDAP_CONTROL_DONTUSECOPY,
162 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
163 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
165 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
166 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
167 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
168 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
170 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
171 #ifdef SLAP_CONTROL_X_TREE_DELETE
172 { LDAP_CONTROL_X_TREE_DELETE,
173 (int)offsetof(struct slap_control_ids, sc_treeDelete),
174 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
176 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
178 { LDAP_CONTROL_X_SEARCH_OPTIONS,
179 (int)offsetof(struct slap_control_ids, sc_searchOptions),
180 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
182 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
183 { LDAP_CONTROL_SUBENTRIES,
184 (int)offsetof(struct slap_control_ids, sc_subentries),
187 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
189 (int)offsetof(struct slap_control_ids, sc_noOp),
190 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
192 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
193 { LDAP_CONTROL_RELAX,
194 (int)offsetof(struct slap_control_ids, sc_relax),
195 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
197 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
199 { LDAP_CONTROL_X_TXN_SPEC,
200 (int)offsetof(struct slap_control_ids, sc_txnSpec),
201 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
203 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
205 { LDAP_CONTROL_MANAGEDSAIT,
206 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
208 manageDSAit_extops, NULL,
209 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
210 { LDAP_CONTROL_PROXY_AUTHZ,
211 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
212 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
213 proxy_authz_extops, NULL,
214 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
215 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
216 { LDAP_CONTROL_X_SESSION_TRACKING,
217 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
218 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
219 session_tracking_extops, NULL,
220 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
222 #ifdef SLAP_CONTROL_X_WHATFAILED
223 { LDAP_CONTROL_X_WHATFAILED,
224 (int)offsetof(struct slap_control_ids, sc_whatFailed),
225 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
227 parseWhatFailed, LDAP_SLIST_ENTRY_INITIALIZER(next) },
230 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
233 static struct slap_control *
234 find_ctrl( const char *oid );
237 * Register a supported control.
239 * This can be called by an OpenLDAP plugin or, indirectly, by a
240 * SLAPI plugin calling slapi_register_supported_control().
242 * NOTE: if flags == 1 the control is replaced if already registered;
243 * otherwise registering an already registered control is not allowed.
246 register_supported_control2(const char *controloid,
247 slap_mask_t controlmask,
249 SLAP_CTRL_PARSE_FN *controlparsefn,
253 struct slap_control *sc = NULL;
255 BerVarray extendedopsbv = NULL;
257 if ( num_known_controls >= SLAP_MAX_CIDS ) {
258 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
259 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
260 SLAP_MAX_CIDS, 0, 0 );
264 if ( controloid == NULL ) {
265 return LDAP_PARAM_ERROR;
268 /* check if already registered */
269 for ( i = 0; slap_known_controls[ i ]; i++ ) {
270 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
272 Debug( LDAP_DEBUG_TRACE,
273 "Control %s already registered; replacing.\n",
275 /* (find and) replace existing handler */
276 sc = find_ctrl( controloid );
277 assert( sc != NULL );
281 Debug( LDAP_DEBUG_ANY,
282 "Control %s already registered.\n",
284 return LDAP_PARAM_ERROR;
288 /* turn compatible extended operations into bervals */
289 if ( controlexops != NULL ) {
292 for ( i = 0; controlexops[ i ]; i++ );
294 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
295 if ( extendedopsbv == NULL ) {
296 return LDAP_NO_MEMORY;
299 for ( i = 0; controlexops[ i ]; i++ ) {
300 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
305 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
307 return LDAP_NO_MEMORY;
310 sc->sc_oid = ch_strdup( controloid );
311 sc->sc_cid = num_known_controls;
313 /* Update slap_known_controls, too. */
314 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
315 slap_known_controls[num_known_controls++] = NULL;
317 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
318 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
321 if ( sc->sc_extendedopsbv ) {
322 /* FIXME: in principle, we should rather merge
323 * existing extops with those supported by the
324 * new control handling implementation.
325 * In fact, whether a control is compatible with
326 * an extop should not be a matter of implementation.
327 * We likely also need a means for a newly
328 * registered extop to declare that it is
329 * comptible with an already registered control.
331 ber_bvarray_free( sc->sc_extendedopsbv );
332 sc->sc_extendedopsbv = NULL;
333 sc->sc_extendedops = NULL;
337 sc->sc_extendedopsbv = extendedopsbv;
338 sc->sc_mask = controlmask;
339 sc->sc_parse = controlparsefn;
341 *controlcid = sc->sc_cid;
347 #ifdef SLAP_CONFIG_DELETE
349 unregister_supported_control( const char *controloid )
351 struct slap_control *sc;
354 if ( controloid == NULL || (sc = find_ctrl( controloid )) == NULL ){
358 for ( i = 0; slap_known_controls[ i ]; i++ ) {
359 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
361 slap_known_controls[ i ] = slap_known_controls[ i+1 ];
362 } while ( slap_known_controls[ i++ ] );
363 num_known_controls--;
368 LDAP_SLIST_REMOVE(&controls_list, sc, slap_control, sc_next);
369 ch_free( sc->sc_oid );
370 if ( sc->sc_extendedopsbv != NULL ) {
371 ber_bvarray_free( sc->sc_extendedopsbv );
377 #endif /* SLAP_CONFIG_DELETE */
380 * One-time initialization of internal controls.
383 slap_controls_init( void )
389 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
390 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
391 rc = register_supported_control( control_defs[i].sc_oid,
392 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
393 control_defs[i].sc_parse, cid );
394 if ( rc != LDAP_SUCCESS ) break;
401 * Free memory associated with list of supported controls.
404 controls_destroy( void )
406 struct slap_control *sc;
408 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
409 sc = LDAP_SLIST_FIRST(&controls_list);
410 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
412 ch_free( sc->sc_oid );
413 if ( sc->sc_extendedopsbv != NULL ) {
414 ber_bvarray_free( sc->sc_extendedopsbv );
421 * Format the supportedControl attribute of the root DSE,
422 * detailing which controls are supported by the directory
426 controls_root_dse_info( Entry *e )
428 AttributeDescription *ad_supportedControl
429 = slap_schema.si_ad_supportedControl;
430 struct berval vals[2];
431 struct slap_control *sc;
433 vals[1].bv_val = NULL;
436 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
437 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
439 vals[0].bv_val = sc->sc_oid;
440 vals[0].bv_len = strlen( sc->sc_oid );
442 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
451 * Return a list of OIDs and operation masks for supported
452 * controls. Used by SLAPI.
455 get_supported_controls(char ***ctrloidsp,
456 slap_mask_t **ctrlmasks)
461 struct slap_control *sc;
465 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
475 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
476 if ( oids == NULL ) {
477 return LDAP_NO_MEMORY;
479 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
480 if ( masks == NULL ) {
482 return LDAP_NO_MEMORY;
487 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
488 oids[n] = ch_strdup( sc->sc_oid );
489 masks[n] = sc->sc_mask;
502 * Find a control given its OID.
504 static struct slap_control *
505 find_ctrl( const char *oid )
507 struct slap_control *sc;
509 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
510 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
519 slap_find_control_id(
523 struct slap_control *ctrl = find_ctrl( oid );
525 if ( cid ) *cid = ctrl->sc_cid;
528 return LDAP_CONTROL_NOT_FOUND;
532 slap_global_control( Operation *op, const char *oid, int *cid )
534 struct slap_control *ctrl = find_ctrl( oid );
536 if ( ctrl == NULL ) {
537 /* should not be reachable */
538 Debug( LDAP_DEBUG_ANY,
539 "slap_global_control: unrecognized control: %s\n",
541 return LDAP_CONTROL_NOT_FOUND;
544 if ( cid ) *cid = ctrl->sc_cid;
546 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
547 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
548 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
550 return LDAP_COMPARE_TRUE;
554 Debug( LDAP_DEBUG_TRACE,
555 "slap_global_control: unavailable control: %s\n",
559 return LDAP_COMPARE_FALSE;
562 void slap_free_ctrls(
564 LDAPControl **ctrls )
568 for (i=0; ctrls[i]; i++) {
569 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
571 op->o_tmpfree( ctrls, op->o_tmpmemctx );
577 LDAPControl **ctrls )
580 LDAPControl **ctrlsp;
582 if ( rs->sr_ctrls ) {
583 for ( ; rs->sr_ctrls[ i ]; i++ ) ;
586 for ( j=0; ctrls[j]; j++ ) ;
588 ctrlsp = op->o_tmpalloc(( i+j+1 )*sizeof(LDAPControl *), op->o_tmpmemctx );
590 if ( rs->sr_ctrls ) {
591 for ( ; rs->sr_ctrls[i]; i++ )
592 ctrlsp[i] = rs->sr_ctrls[i];
594 for ( j=0; ctrls[j]; j++)
595 ctrlsp[i++] = ctrls[j];
598 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED )
599 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
600 rs->sr_ctrls = ctrlsp;
601 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
608 LDAPControl *control,
611 struct slap_control *sc;
612 int rc = LDAP_SUCCESS;
614 sc = find_ctrl( control->ldctl_oid );
616 /* recognized control */
618 switch( op->o_tag ) {
620 tagmask = SLAP_CTRL_ADD;
623 tagmask = SLAP_CTRL_BIND;
625 case LDAP_REQ_COMPARE:
626 tagmask = SLAP_CTRL_COMPARE;
628 case LDAP_REQ_DELETE:
629 tagmask = SLAP_CTRL_DELETE;
631 case LDAP_REQ_MODIFY:
632 tagmask = SLAP_CTRL_MODIFY;
634 case LDAP_REQ_RENAME:
635 tagmask = SLAP_CTRL_RENAME;
637 case LDAP_REQ_SEARCH:
638 tagmask = SLAP_CTRL_SEARCH;
640 case LDAP_REQ_UNBIND:
641 tagmask = SLAP_CTRL_UNBIND;
643 case LDAP_REQ_ABANDON:
644 tagmask = SLAP_CTRL_ABANDON;
646 case LDAP_REQ_EXTENDED:
648 assert( op->ore_reqoid.bv_val != NULL );
649 if( sc->sc_extendedopsbv != NULL ) {
651 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
652 if( bvmatch( &op->ore_reqoid,
653 &sc->sc_extendedopsbv[i] ) )
662 *text = "controls internal error";
666 if (( sc->sc_mask & tagmask ) == tagmask ) {
667 /* available extension */
668 if ( sc->sc_parse ) {
669 rc = sc->sc_parse( op, rs, control );
670 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
672 } else if ( control->ldctl_iscritical ) {
673 *text = "not yet implemented";
678 } else if ( control->ldctl_iscritical ) {
679 /* unavailable CRITICAL control */
680 *text = "critical extension is unavailable";
681 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
684 } else if ( control->ldctl_iscritical ) {
685 /* unrecognized CRITICAL control */
686 *text = "critical extension is not recognized";
687 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
702 BerElement *ber = op->o_ber;
704 #ifdef SLAP_CONTROL_X_WHATFAILED
705 /* NOTE: right now, slapd checks the validity of each control
706 * while parsing. As a consequence, it can only detect one
707 * cause of failure at a time. This results in returning
708 * exactly one OID with the whatFailed control, or no control
711 char *failed_oid = NULL;
714 len = ber_pvt_ber_remaining(ber);
718 rs->sr_err = LDAP_SUCCESS;
722 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
723 if( tag == LBER_ERROR ) {
724 rs->sr_err = SLAPD_DISCONNECT;
725 rs->sr_text = "unexpected data in PDU";
731 Debug( LDAP_DEBUG_TRACE,
732 "=> get_ctrls\n", 0, 0, 0 );
734 if( op->o_protocol < LDAP_VERSION3 ) {
735 rs->sr_err = SLAPD_DISCONNECT;
736 rs->sr_text = "controls require LDAPv3";
740 /* one for first control, one for termination */
741 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
744 if( op->ctrls == NULL ) {
745 rs->sr_err = LDAP_NO_MEMORY;
746 rs->sr_text = "no memory";
751 op->o_ctrls[nctrls] = NULL;
753 /* step through each element */
754 for( tag = ber_first_element( ber, &len, &opaque );
756 tag = ber_next_element( ber, &len, opaque ) )
759 LDAPControl **tctrls;
761 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
762 memset(c, 0, sizeof(LDAPControl));
764 /* allocate pointer space for current controls (nctrls)
765 * + this control + extra NULL
767 tctrls = op->o_tmprealloc( op->o_ctrls,
768 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
771 if( tctrls == NULL ) {
773 ldap_controls_free(op->o_ctrls);
776 rs->sr_err = LDAP_NO_MEMORY;
777 rs->sr_text = "no memory";
781 op->o_ctrls = tctrls;
783 op->o_ctrls[nctrls++] = c;
784 op->o_ctrls[nctrls] = NULL;
786 tag = ber_scanf( ber, "{m" /*}*/, &bv );
787 c->ldctl_oid = bv.bv_val;
789 if( tag == LBER_ERROR ) {
790 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
793 slap_free_ctrls( op, op->o_ctrls );
795 rs->sr_err = SLAPD_DISCONNECT;
796 rs->sr_text = "decoding controls error";
799 } else if( c->ldctl_oid == NULL ) {
800 Debug( LDAP_DEBUG_TRACE,
801 "get_ctrls: conn %lu got emtpy OID.\n",
802 op->o_connid, 0, 0 );
804 slap_free_ctrls( op, op->o_ctrls );
806 rs->sr_err = LDAP_PROTOCOL_ERROR;
807 rs->sr_text = "OID field is empty";
811 tag = ber_peek_tag( ber, &len );
813 if( tag == LBER_BOOLEAN ) {
815 tag = ber_scanf( ber, "b", &crit );
817 if( tag == LBER_ERROR ) {
818 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
820 slap_free_ctrls( op, op->o_ctrls );
822 rs->sr_err = SLAPD_DISCONNECT;
823 rs->sr_text = "decoding controls error";
827 c->ldctl_iscritical = (crit != 0);
828 tag = ber_peek_tag( ber, &len );
831 if( tag == LBER_OCTETSTRING ) {
832 tag = ber_scanf( ber, "m", &c->ldctl_value );
834 if( tag == LBER_ERROR ) {
835 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
836 "%s (%scritical): get value failed.\n",
837 op->o_connid, c->ldctl_oid,
838 c->ldctl_iscritical ? "" : "non" );
839 slap_free_ctrls( op, op->o_ctrls );
841 rs->sr_err = SLAPD_DISCONNECT;
842 rs->sr_text = "decoding controls error";
847 Debug( LDAP_DEBUG_TRACE,
848 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
849 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
851 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
852 if ( rs->sr_err != LDAP_SUCCESS ) {
853 #ifdef SLAP_CONTROL_X_WHATFAILED
854 failed_oid = c->ldctl_oid;
861 Debug( LDAP_DEBUG_TRACE,
862 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
863 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
865 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
866 if( rs->sr_err == SLAPD_DISCONNECT ) {
867 rs->sr_err = LDAP_PROTOCOL_ERROR;
868 send_ldap_disconnect( op, rs );
869 rs->sr_err = SLAPD_DISCONNECT;
871 #ifdef SLAP_CONTROL_X_WHATFAILED
872 /* might have not been parsed yet? */
873 if ( failed_oid != NULL ) {
874 if ( !get_whatFailed( op ) ) {
877 /* step through each remaining element */
878 for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
880 LDAPControl c = { 0 };
882 tag = ber_scanf( ber, "{m" /*}*/, &bv );
883 c.ldctl_oid = bv.bv_val;
885 if ( tag == LBER_ERROR ) {
886 slap_free_ctrls( op, op->o_ctrls );
890 } else if ( c.ldctl_oid == NULL ) {
891 slap_free_ctrls( op, op->o_ctrls );
896 tag = ber_peek_tag( ber, &len );
897 if ( tag == LBER_BOOLEAN ) {
899 tag = ber_scanf( ber, "b", &crit );
900 if( tag == LBER_ERROR ) {
901 slap_free_ctrls( op, op->o_ctrls );
906 tag = ber_peek_tag( ber, &len );
909 if ( tag == LBER_OCTETSTRING ) {
910 tag = ber_scanf( ber, "m", &c.ldctl_value );
912 if( tag == LBER_ERROR ) {
913 slap_free_ctrls( op, op->o_ctrls );
919 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
921 slap_parse_ctrl( op, rs, &c, &text );
927 if ( get_whatFailed( op ) ) {
929 oids[ 0 ] = failed_oid;
931 slap_ctrl_whatFailed_add( op, rs, oids );
936 send_ldap_result( op, rs );
948 BI_chk_controls fnc )
952 switch ( op->o_ctrlflag[ ctrl ] ) {
953 case SLAP_CONTROL_NONCRITICAL:
954 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
955 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
956 slap_known_controls[ ctrl - 1 ] ) == 0 )
963 rs->sr_err = LDAP_OTHER;
971 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
974 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
975 ( i - j ) * sizeof( LDAPControl * ) );
978 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
982 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
984 Debug( LDAP_DEBUG_ANY, "%s: "
985 "non-critical control \"%s\" not supported; stripped.\n",
986 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
989 case SLAP_CONTROL_IGNORED:
990 case SLAP_CONTROL_NONE:
991 rs->sr_err = SLAP_CB_CONTINUE;
994 case SLAP_CONTROL_CRITICAL:
995 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
999 Debug( LDAP_DEBUG_ANY, "%s: "
1000 "critical control \"%s\" not supported.\n",
1001 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1005 /* handle all cases! */
1012 static int parseDontUseCopy (
1017 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
1018 rs->sr_text = "dontUseCopy control specified multiple times";
1019 return LDAP_PROTOCOL_ERROR;
1022 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1023 rs->sr_text = "dontUseCopy control value not absent";
1024 return LDAP_PROTOCOL_ERROR;
1027 if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
1028 && !ctrl->ldctl_iscritical )
1030 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
1031 return LDAP_PROTOCOL_ERROR;
1034 op->o_dontUseCopy = ctrl->ldctl_iscritical
1035 ? SLAP_CONTROL_CRITICAL
1036 : SLAP_CONTROL_NONCRITICAL;
1038 return LDAP_SUCCESS;
1041 static int parseRelax (
1046 if ( op->o_relax != SLAP_CONTROL_NONE ) {
1047 rs->sr_text = "relax control specified multiple times";
1048 return LDAP_PROTOCOL_ERROR;
1051 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1052 rs->sr_text = "relax control value not absent";
1053 return LDAP_PROTOCOL_ERROR;
1056 op->o_relax = ctrl->ldctl_iscritical
1057 ? SLAP_CONTROL_CRITICAL
1058 : SLAP_CONTROL_NONCRITICAL;
1060 return LDAP_SUCCESS;
1063 static int parseManageDSAit (
1068 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1069 rs->sr_text = "manageDSAit control specified multiple times";
1070 return LDAP_PROTOCOL_ERROR;
1073 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1074 rs->sr_text = "manageDSAit control value not absent";
1075 return LDAP_PROTOCOL_ERROR;
1078 op->o_managedsait = ctrl->ldctl_iscritical
1079 ? SLAP_CONTROL_CRITICAL
1080 : SLAP_CONTROL_NONCRITICAL;
1082 return LDAP_SUCCESS;
1085 static int parseProxyAuthz (
1091 struct berval dn = BER_BVNULL;
1093 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1094 rs->sr_text = "proxy authorization control specified multiple times";
1095 return LDAP_PROTOCOL_ERROR;
1098 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1099 rs->sr_text = "proxy authorization control value absent";
1100 return LDAP_PROTOCOL_ERROR;
1103 if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1104 && !ctrl->ldctl_iscritical )
1106 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1107 return LDAP_PROTOCOL_ERROR;
1110 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1111 && BER_BVISEMPTY( &op->o_ndn ) )
1113 rs->sr_text = "anonymous proxied authorization not allowed";
1114 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1117 op->o_proxy_authz = ctrl->ldctl_iscritical
1118 ? SLAP_CONTROL_CRITICAL
1119 : SLAP_CONTROL_NONCRITICAL;
1121 Debug( LDAP_DEBUG_ARGS,
1122 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
1124 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
1127 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1128 Debug( LDAP_DEBUG_TRACE,
1129 "parseProxyAuthz: conn=%lu anonymous\n",
1130 op->o_connid, 0, 0 );
1133 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1134 op->o_ndn.bv_val[ 0 ] = '\0';
1136 op->o_ndn.bv_len = 0;
1138 if ( !BER_BVISNULL( &op->o_dn ) ) {
1139 op->o_dn.bv_val[ 0 ] = '\0';
1141 op->o_dn.bv_len = 0;
1143 return LDAP_SUCCESS;
1146 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1147 NULL, &dn, SLAP_GETDN_AUTHZID );
1149 /* FIXME: empty DN in proxyAuthz control should be legal... */
1150 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1152 ch_free( dn.bv_val );
1154 rs->sr_text = "authzId mapping failed";
1155 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1158 Debug( LDAP_DEBUG_TRACE,
1159 "parseProxyAuthz: conn=%lu \"%s\"\n",
1161 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1163 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1166 ch_free( dn.bv_val );
1167 rs->sr_text = "not authorized to assume identity";
1168 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1171 ch_free( op->o_ndn.bv_val );
1174 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1175 * from now on op->o_dn is normalized
1178 ber_bvreplace( &op->o_dn, &dn );
1180 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1181 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1183 return LDAP_SUCCESS;
1186 static int parseNoOp (
1191 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1192 rs->sr_text = "noop control specified multiple times";
1193 return LDAP_PROTOCOL_ERROR;
1196 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1197 rs->sr_text = "noop control value not empty";
1198 return LDAP_PROTOCOL_ERROR;
1201 op->o_noop = ctrl->ldctl_iscritical
1202 ? SLAP_CONTROL_CRITICAL
1203 : SLAP_CONTROL_NONCRITICAL;
1205 return LDAP_SUCCESS;
1208 static int parsePagedResults (
1213 BerElementBuffer berbuf;
1214 BerElement *ber = (BerElement *)&berbuf;
1215 struct berval cookie;
1216 PagedResultsState *ps;
1217 int rc = LDAP_SUCCESS;
1221 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1222 rs->sr_text = "paged results control specified multiple times";
1223 return LDAP_PROTOCOL_ERROR;
1226 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1227 rs->sr_text = "paged results control value is absent";
1228 return LDAP_PROTOCOL_ERROR;
1231 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1232 rs->sr_text = "paged results control value is empty";
1233 return LDAP_PROTOCOL_ERROR;
1236 /* Parse the control value
1237 * realSearchControlValue ::= SEQUENCE {
1238 * size INTEGER (0..maxInt),
1239 * -- requested page size from client
1240 * -- result set size estimate from server
1241 * cookie OCTET STRING
1244 ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1246 tag = ber_scanf( ber, "{im}", &size, &cookie );
1248 if ( tag == LBER_ERROR ) {
1249 rs->sr_text = "paged results control could not be decoded";
1250 rc = LDAP_PROTOCOL_ERROR;
1255 rs->sr_text = "paged results control size invalid";
1256 rc = LDAP_PROTOCOL_ERROR;
1260 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1261 *ps = op->o_conn->c_pagedresults_state;
1263 ps->ps_cookieval = cookie;
1264 op->o_pagedresults_state = ps;
1265 if ( !cookie.bv_len ) {
1268 /* taint ps_cookie, to detect whether it's set */
1269 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1272 /* NOTE: according to RFC 2696 3.:
1274 If the page size is greater than or equal to the sizeLimit value, the
1275 server should ignore the control as the request can be satisfied in a
1278 * NOTE: this assumes that the op->ors_slimit be set
1279 * before the controls are parsed.
1282 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1283 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1285 } else if ( ctrl->ldctl_iscritical ) {
1286 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1289 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1296 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1297 static int parseSortedResults (
1302 int rc = LDAP_SUCCESS;
1304 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1305 rs->sr_text = "sorted results control specified multiple times";
1306 return LDAP_PROTOCOL_ERROR;
1309 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1310 rs->sr_text = "sorted results control value is absent";
1311 return LDAP_PROTOCOL_ERROR;
1314 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1315 rs->sr_text = "sorted results control value is empty";
1316 return LDAP_PROTOCOL_ERROR;
1319 /* blow off parsing the value */
1321 op->o_sortedresults = ctrl->ldctl_iscritical
1322 ? SLAP_CONTROL_CRITICAL
1323 : SLAP_CONTROL_NONCRITICAL;
1329 static int parseAssert (
1335 struct berval fstr = BER_BVNULL;
1337 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1338 rs->sr_text = "assert control specified multiple times";
1339 return LDAP_PROTOCOL_ERROR;
1342 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1343 rs->sr_text = "assert control value is absent";
1344 return LDAP_PROTOCOL_ERROR;
1347 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1348 rs->sr_text = "assert control value is empty";
1349 return LDAP_PROTOCOL_ERROR;
1352 ber = ber_init( &(ctrl->ldctl_value) );
1354 rs->sr_text = "assert control: internal error";
1358 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1360 (void) ber_free( ber, 1 );
1361 if( rs->sr_err != LDAP_SUCCESS ) {
1362 if( rs->sr_err == SLAPD_DISCONNECT ) {
1363 rs->sr_err = LDAP_PROTOCOL_ERROR;
1364 send_ldap_disconnect( op, rs );
1365 rs->sr_err = SLAPD_DISCONNECT;
1367 send_ldap_result( op, rs );
1369 if( op->o_assertion != NULL ) {
1370 filter_free_x( op, op->o_assertion, 1 );
1376 filter2bv_x( op, op->o_assertion, &fstr );
1378 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1379 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1380 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1383 op->o_assert = ctrl->ldctl_iscritical
1384 ? SLAP_CONTROL_CRITICAL
1385 : SLAP_CONTROL_NONCRITICAL;
1387 rs->sr_err = LDAP_SUCCESS;
1388 return LDAP_SUCCESS;
1391 #define READMSG(post, msg) \
1392 ( post ? "postread control: " msg : "preread control: " msg )
1401 ber_len_t siz, off, i;
1403 AttributeName *an = NULL;
1405 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1406 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1408 rs->sr_text = READMSG( post, "specified multiple times" );
1409 return LDAP_PROTOCOL_ERROR;
1412 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1413 rs->sr_text = READMSG( post, "value is absent" );
1414 return LDAP_PROTOCOL_ERROR;
1417 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1418 rs->sr_text = READMSG( post, "value is empty" );
1419 return LDAP_PROTOCOL_ERROR;
1423 if ( op->o_txnSpec ) { /* temporary limitation */
1424 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1425 return LDAP_UNWILLING_TO_PERFORM;
1429 ber = ber_init( &ctrl->ldctl_value );
1430 if ( ber == NULL ) {
1431 rs->sr_text = READMSG( post, "internal error" );
1435 rs->sr_err = LDAP_SUCCESS;
1436 siz = sizeof( AttributeName );
1437 off = offsetof( AttributeName, an_name );
1438 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1439 rs->sr_text = READMSG( post, "decoding error" );
1440 rs->sr_err = LDAP_PROTOCOL_ERROR;
1444 for ( i = 0; i < siz; i++ ) {
1445 const char *dummy = NULL;
1448 an[i].an_desc = NULL;
1451 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1452 if ( rc == LDAP_SUCCESS ) {
1453 an[i].an_name = an[i].an_desc->ad_cname;
1457 static struct berval special_attrs[] = {
1458 BER_BVC( LDAP_NO_ATTRS ),
1459 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1460 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1464 /* deal with special attribute types */
1465 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1466 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1467 an[i].an_name = special_attrs[ j ];
1472 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1474 rs->sr_text = dummy ? dummy
1475 : READMSG( post, "unknown attributeType" );
1482 op->o_postread_attrs = an;
1483 op->o_postread = ctrl->ldctl_iscritical
1484 ? SLAP_CONTROL_CRITICAL
1485 : SLAP_CONTROL_NONCRITICAL;
1487 op->o_preread_attrs = an;
1488 op->o_preread = ctrl->ldctl_iscritical
1489 ? SLAP_CONTROL_CRITICAL
1490 : SLAP_CONTROL_NONCRITICAL;
1494 (void) ber_free( ber, 1 );
1498 static int parsePreRead (
1503 return parseReadAttrs( op, rs, ctrl, 0 );
1506 static int parsePostRead (
1511 return parseReadAttrs( op, rs, ctrl, 1 );
1514 static int parseValuesReturnFilter (
1520 struct berval fstr = BER_BVNULL;
1522 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1523 rs->sr_text = "valuesReturnFilter control specified multiple times";
1524 return LDAP_PROTOCOL_ERROR;
1527 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1528 rs->sr_text = "valuesReturnFilter control value is absent";
1529 return LDAP_PROTOCOL_ERROR;
1532 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1533 rs->sr_text = "valuesReturnFilter control value is empty";
1534 return LDAP_PROTOCOL_ERROR;
1537 ber = ber_init( &(ctrl->ldctl_value) );
1539 rs->sr_text = "internal error";
1543 rs->sr_err = get_vrFilter( op, ber,
1544 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1546 (void) ber_free( ber, 1 );
1548 if( rs->sr_err != LDAP_SUCCESS ) {
1549 if( rs->sr_err == SLAPD_DISCONNECT ) {
1550 rs->sr_err = LDAP_PROTOCOL_ERROR;
1551 send_ldap_disconnect( op, rs );
1552 rs->sr_err = SLAPD_DISCONNECT;
1554 send_ldap_result( op, rs );
1556 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1560 vrFilter2bv( op, op->o_vrFilter, &fstr );
1563 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1564 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1565 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1568 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1569 ? SLAP_CONTROL_CRITICAL
1570 : SLAP_CONTROL_NONCRITICAL;
1572 rs->sr_err = LDAP_SUCCESS;
1573 return LDAP_SUCCESS;
1576 static int parseSubentries (
1581 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1582 rs->sr_text = "subentries control specified multiple times";
1583 return LDAP_PROTOCOL_ERROR;
1586 /* FIXME: should use BER library */
1587 if( ( ctrl->ldctl_value.bv_len != 3 )
1588 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1589 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1591 rs->sr_text = "subentries control value encoding is bogus";
1592 return LDAP_PROTOCOL_ERROR;
1595 op->o_subentries = ctrl->ldctl_iscritical
1596 ? SLAP_CONTROL_CRITICAL
1597 : SLAP_CONTROL_NONCRITICAL;
1599 if (ctrl->ldctl_value.bv_val[2]) {
1600 set_subentries_visibility( op );
1603 return LDAP_SUCCESS;
1606 static int parsePermissiveModify (
1611 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1612 rs->sr_text = "permissiveModify control specified multiple times";
1613 return LDAP_PROTOCOL_ERROR;
1616 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1617 rs->sr_text = "permissiveModify control value not absent";
1618 return LDAP_PROTOCOL_ERROR;
1621 op->o_permissive_modify = ctrl->ldctl_iscritical
1622 ? SLAP_CONTROL_CRITICAL
1623 : SLAP_CONTROL_NONCRITICAL;
1625 return LDAP_SUCCESS;
1628 static int parseDomainScope (
1633 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1634 rs->sr_text = "domainScope control specified multiple times";
1635 return LDAP_PROTOCOL_ERROR;
1638 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1639 rs->sr_text = "domainScope control value not empty";
1640 return LDAP_PROTOCOL_ERROR;
1643 op->o_domain_scope = ctrl->ldctl_iscritical
1644 ? SLAP_CONTROL_CRITICAL
1645 : SLAP_CONTROL_NONCRITICAL;
1647 return LDAP_SUCCESS;
1650 #ifdef SLAP_CONTROL_X_TREE_DELETE
1651 static int parseTreeDelete (
1656 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1657 rs->sr_text = "treeDelete control specified multiple times";
1658 return LDAP_PROTOCOL_ERROR;
1661 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1662 rs->sr_text = "treeDelete control value not absent";
1663 return LDAP_PROTOCOL_ERROR;
1666 op->o_tree_delete = ctrl->ldctl_iscritical
1667 ? SLAP_CONTROL_CRITICAL
1668 : SLAP_CONTROL_NONCRITICAL;
1670 return LDAP_SUCCESS;
1674 static int parseSearchOptions (
1680 ber_int_t search_flags;
1683 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1684 rs->sr_text = "searchOptions control value is absent";
1685 return LDAP_PROTOCOL_ERROR;
1688 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1689 rs->sr_text = "searchOptions control value is empty";
1690 return LDAP_PROTOCOL_ERROR;
1693 ber = ber_init( &ctrl->ldctl_value );
1695 rs->sr_text = "internal error";
1699 tag = ber_scanf( ber, "{i}", &search_flags );
1700 (void) ber_free( ber, 1 );
1702 if ( tag == LBER_ERROR ) {
1703 rs->sr_text = "searchOptions control decoding error";
1704 return LDAP_PROTOCOL_ERROR;
1707 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1708 /* Search flags not recognised so far,
1710 * LDAP_SEARCH_FLAG_PHANTOM_ROOT
1712 if ( ctrl->ldctl_iscritical ) {
1713 rs->sr_text = "searchOptions contained unrecognized flag";
1714 return LDAP_UNWILLING_TO_PERFORM;
1718 Debug( LDAP_DEBUG_TRACE,
1719 "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n",
1720 op->o_connid, (unsigned)search_flags, 0 );
1722 return LDAP_SUCCESS;
1725 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1726 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1727 rs->sr_text = "searchOptions control specified multiple times "
1728 "or with domainScope control";
1729 return LDAP_PROTOCOL_ERROR;
1732 op->o_domain_scope = ctrl->ldctl_iscritical
1733 ? SLAP_CONTROL_CRITICAL
1734 : SLAP_CONTROL_NONCRITICAL;
1737 return LDAP_SUCCESS;
1740 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1741 struct berval session_tracking_formats[] = {
1742 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1743 BER_BVC( "RADIUS-Acct-Session-Id" ),
1744 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1745 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1746 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1747 BER_BVC( "USERNAME" ),
1752 static int parseSessionTracking(
1762 struct berval sessionSourceIp = BER_BVNULL,
1763 sessionSourceName = BER_BVNULL,
1764 formatOID = BER_BVNULL,
1765 sessionTrackingIdentifier = BER_BVNULL;
1767 size_t st_len, st_pos;
1769 if ( ctrl->ldctl_iscritical ) {
1770 rs->sr_text = "sessionTracking criticality is TRUE";
1771 return LDAP_PROTOCOL_ERROR;
1774 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1775 rs->sr_text = "sessionTracking control value is absent";
1776 return LDAP_PROTOCOL_ERROR;
1779 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1780 rs->sr_text = "sessionTracking control value is empty";
1781 return LDAP_PROTOCOL_ERROR;
1784 /* TODO: add the capability to determine if a client is allowed
1785 * to use this control, based on identity, ip and so */
1787 ber = ber_init( &ctrl->ldctl_value );
1788 if ( ber == NULL ) {
1789 rs->sr_text = "internal error";
1793 tag = ber_skip_tag( ber, &len );
1794 if ( tag != LBER_SEQUENCE ) {
1799 /* sessionSourceIp */
1800 tag = ber_peek_tag( ber, &len );
1801 if ( tag == LBER_DEFAULT ) {
1807 tag = ber_skip_tag( ber, &len );
1809 } else if ( len > 128 ) {
1810 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1811 rs->sr_err = LDAP_PROTOCOL_ERROR;
1815 tag = ber_scanf( ber, "m", &sessionSourceIp );
1818 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1819 BER_BVZERO( &sessionSourceIp );
1822 /* sessionSourceName */
1823 tag = ber_peek_tag( ber, &len );
1824 if ( tag == LBER_DEFAULT ) {
1830 tag = ber_skip_tag( ber, &len );
1832 } else if ( len > 65536 ) {
1833 rs->sr_text = "sessionTracking.sessionSourceName too long";
1834 rs->sr_err = LDAP_PROTOCOL_ERROR;
1838 tag = ber_scanf( ber, "m", &sessionSourceName );
1841 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1842 BER_BVZERO( &sessionSourceName );
1846 tag = ber_peek_tag( ber, &len );
1847 if ( tag == LBER_DEFAULT ) {
1853 rs->sr_text = "sessionTracking.formatOID empty";
1854 rs->sr_err = LDAP_PROTOCOL_ERROR;
1857 } else if ( len > 1024 ) {
1858 rs->sr_text = "sessionTracking.formatOID too long";
1859 rs->sr_err = LDAP_PROTOCOL_ERROR;
1863 tag = ber_scanf( ber, "m", &formatOID );
1866 rc = numericoidValidate( NULL, &formatOID );
1867 if ( rc != LDAP_SUCCESS ) {
1868 rs->sr_text = "sessionTracking.formatOID invalid";
1872 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1874 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1875 formatOID = session_tracking_formats[ i + 1 ];
1880 /* sessionTrackingIdentifier */
1881 tag = ber_peek_tag( ber, &len );
1882 if ( tag == LBER_DEFAULT ) {
1888 tag = ber_skip_tag( ber, &len );
1891 /* note: should not be more than 65536... */
1892 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1893 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1894 /* we want the OID printed, at least */
1895 BER_BVSTR( &sessionTrackingIdentifier, "" );
1900 tag = ber_skip_tag( ber, &len );
1901 if ( tag != LBER_DEFAULT || len != 0 ) {
1908 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1909 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1911 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1912 if ( st_len ) st_len++;
1913 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1915 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1916 if ( st_len ) st_len++;
1917 st_len += formatOID.bv_len + STRLENOF( "=" )
1918 + sessionTrackingIdentifier.bv_len;
1921 if ( st_len == 0 ) {
1925 st_len += STRLENOF( " []" );
1926 st_pos = strlen( op->o_log_prefix );
1928 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1929 char *ptr = &op->o_log_prefix[ st_pos ];
1931 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1934 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1935 ptr = lutil_strcopy( ptr, "IP=" );
1936 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1940 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1941 if ( st_len ) *ptr++ = ' ';
1942 ptr = lutil_strcopy( ptr, "NAME=" );
1943 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1947 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1948 if ( st_len ) *ptr++ = ' ';
1949 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1951 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1959 (void)ber_free( ber, 1 );
1961 if ( tag == LBER_ERROR ) {
1962 rs->sr_text = "sessionTracking control decoding error";
1963 return LDAP_PROTOCOL_ERROR;
1971 slap_ctrl_session_tracking_add(
1975 struct berval *name,
1979 BerElementBuffer berbuf;
1980 BerElement *ber = (BerElement *)&berbuf;
1982 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
1984 assert( ctrl != NULL );
1986 ber_init2( ber, NULL, LBER_USE_DER );
1988 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
1990 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
1991 rs->sr_err = LDAP_OTHER;
1995 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1996 ctrl->ldctl_iscritical = 0;
1998 rs->sr_err = LDAP_SUCCESS;
2005 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
2007 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
2008 struct berval ip = BER_BVNULL,
2012 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
2013 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
2017 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
2018 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
2020 ptr = ber_bvchr( &ip, ':' );
2022 ip.bv_len = ptr - ip.bv_val;
2026 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
2027 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
2029 name = op->o_conn->c_peer_domain;
2032 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2036 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2040 #ifdef SLAP_CONTROL_X_WHATFAILED
2041 static int parseWhatFailed(
2046 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2047 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2048 return LDAP_PROTOCOL_ERROR;
2051 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2052 rs->sr_text = "\"What Failed?\" control value not absent";
2053 return LDAP_PROTOCOL_ERROR;
2056 op->o_whatFailed = ctrl->ldctl_iscritical
2057 ? SLAP_CONTROL_CRITICAL
2058 : SLAP_CONTROL_NONCRITICAL;
2060 return LDAP_SUCCESS;
2064 slap_ctrl_whatFailed_add(
2069 BerElementBuffer berbuf;
2070 BerElement *ber = (BerElement *) &berbuf;
2071 LDAPControl **ctrls = NULL;
2072 struct berval ctrlval;
2073 int i, rc = LDAP_SUCCESS;
2075 ber_init2( ber, NULL, LBER_USE_DER );
2076 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2077 ber_printf( ber, "[" /*]*/ );
2078 for ( i = 0; oids[ i ] != NULL; i++ ) {
2079 ber_printf( ber, "s", oids[ i ] );
2081 ber_printf( ber, /*[*/ "]" );
2083 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2089 if ( rs->sr_ctrls != NULL ) {
2090 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2091 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2098 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2099 sizeof(LDAPControl *)*( i + 2 )
2100 + sizeof(LDAPControl)
2101 + ctrlval.bv_len + 1,
2103 if ( ctrls == NULL ) {
2107 ctrls[ i + 1 ] = NULL;
2108 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2109 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2110 ctrls[ i ]->ldctl_iscritical = 0;
2111 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2112 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2113 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2115 ber_free_buf( ber );
2117 rs->sr_ctrls = ctrls;