2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME|
127 SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
129 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
130 { LDAP_CONTROL_PRE_READ,
131 (int)offsetof(struct slap_control_ids, sc_preRead),
132 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
134 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
135 { LDAP_CONTROL_POST_READ,
136 (int)offsetof(struct slap_control_ids, sc_postRead),
137 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
139 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
140 { LDAP_CONTROL_VALUESRETURNFILTER,
141 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
142 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
144 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
145 { LDAP_CONTROL_PAGEDRESULTS,
146 (int)offsetof(struct slap_control_ids, sc_pagedResults),
149 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
150 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
151 { LDAP_CONTROL_SORTREQUEST,
152 (int)offsetof(struct slap_control_ids, sc_sortedResults),
153 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
155 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
157 { LDAP_CONTROL_X_DOMAIN_SCOPE,
158 (int)offsetof(struct slap_control_ids, sc_domainScope),
159 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
161 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
162 { LDAP_CONTROL_DONTUSECOPY,
163 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
164 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
166 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
167 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
168 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
169 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
171 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
172 #ifdef SLAP_CONTROL_X_TREE_DELETE
173 { LDAP_CONTROL_X_TREE_DELETE,
174 (int)offsetof(struct slap_control_ids, sc_treeDelete),
175 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
177 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
179 { LDAP_CONTROL_X_SEARCH_OPTIONS,
180 (int)offsetof(struct slap_control_ids, sc_searchOptions),
181 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
183 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
184 { LDAP_CONTROL_SUBENTRIES,
185 (int)offsetof(struct slap_control_ids, sc_subentries),
188 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
190 (int)offsetof(struct slap_control_ids, sc_noOp),
191 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
193 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
194 { LDAP_CONTROL_RELAX,
195 (int)offsetof(struct slap_control_ids, sc_relax),
196 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
198 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
200 { LDAP_CONTROL_X_TXN_SPEC,
201 (int)offsetof(struct slap_control_ids, sc_txnSpec),
202 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
204 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
206 { LDAP_CONTROL_MANAGEDSAIT,
207 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
209 manageDSAit_extops, NULL,
210 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
211 { LDAP_CONTROL_PROXY_AUTHZ,
212 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
213 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
214 proxy_authz_extops, NULL,
215 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
216 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
217 { LDAP_CONTROL_X_SESSION_TRACKING,
218 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
219 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
220 session_tracking_extops, NULL,
221 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
223 #ifdef SLAP_CONTROL_X_WHATFAILED
224 { LDAP_CONTROL_X_WHATFAILED,
225 (int)offsetof(struct slap_control_ids, sc_whatFailed),
226 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
228 parseWhatFailed, LDAP_SLIST_ENTRY_INITIALIZER(next) },
231 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
234 static struct slap_control *
235 find_ctrl( const char *oid );
238 * Register a supported control.
240 * This can be called by an OpenLDAP plugin or, indirectly, by a
241 * SLAPI plugin calling slapi_register_supported_control().
243 * NOTE: if flags == 1 the control is replaced if already registered;
244 * otherwise registering an already registered control is not allowed.
247 register_supported_control2(const char *controloid,
248 slap_mask_t controlmask,
250 SLAP_CTRL_PARSE_FN *controlparsefn,
254 struct slap_control *sc = NULL;
256 BerVarray extendedopsbv = NULL;
258 if ( num_known_controls >= SLAP_MAX_CIDS ) {
259 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
260 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
261 SLAP_MAX_CIDS, 0, 0 );
265 if ( controloid == NULL ) {
266 return LDAP_PARAM_ERROR;
269 /* check if already registered */
270 for ( i = 0; slap_known_controls[ i ]; i++ ) {
271 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
273 Debug( LDAP_DEBUG_TRACE,
274 "Control %s already registered; replacing.\n",
276 /* (find and) replace existing handler */
277 sc = find_ctrl( controloid );
278 assert( sc != NULL );
282 Debug( LDAP_DEBUG_ANY,
283 "Control %s already registered.\n",
285 return LDAP_PARAM_ERROR;
289 /* turn compatible extended operations into bervals */
290 if ( controlexops != NULL ) {
293 for ( i = 0; controlexops[ i ]; i++ );
295 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
296 if ( extendedopsbv == NULL ) {
297 return LDAP_NO_MEMORY;
300 for ( i = 0; controlexops[ i ]; i++ ) {
301 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
306 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
308 return LDAP_NO_MEMORY;
311 sc->sc_oid = ch_strdup( controloid );
312 sc->sc_cid = num_known_controls;
314 /* Update slap_known_controls, too. */
315 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
316 slap_known_controls[num_known_controls++] = NULL;
318 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
319 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
322 if ( sc->sc_extendedopsbv ) {
323 /* FIXME: in principle, we should rather merge
324 * existing extops with those supported by the
325 * new control handling implementation.
326 * In fact, whether a control is compatible with
327 * an extop should not be a matter of implementation.
328 * We likely also need a means for a newly
329 * registered extop to declare that it is
330 * comptible with an already registered control.
332 ber_bvarray_free( sc->sc_extendedopsbv );
333 sc->sc_extendedopsbv = NULL;
334 sc->sc_extendedops = NULL;
338 sc->sc_extendedopsbv = extendedopsbv;
339 sc->sc_mask = controlmask;
340 sc->sc_parse = controlparsefn;
342 *controlcid = sc->sc_cid;
349 * One-time initialization of internal controls.
352 slap_controls_init( void )
358 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
359 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
360 rc = register_supported_control( control_defs[i].sc_oid,
361 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
362 control_defs[i].sc_parse, cid );
363 if ( rc != LDAP_SUCCESS ) break;
370 * Free memory associated with list of supported controls.
373 controls_destroy( void )
375 struct slap_control *sc;
377 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
378 sc = LDAP_SLIST_FIRST(&controls_list);
379 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
381 ch_free( sc->sc_oid );
382 if ( sc->sc_extendedopsbv != NULL ) {
383 ber_bvarray_free( sc->sc_extendedopsbv );
390 * Format the supportedControl attribute of the root DSE,
391 * detailing which controls are supported by the directory
395 controls_root_dse_info( Entry *e )
397 AttributeDescription *ad_supportedControl
398 = slap_schema.si_ad_supportedControl;
399 struct berval vals[2];
400 struct slap_control *sc;
402 vals[1].bv_val = NULL;
405 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
406 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
408 vals[0].bv_val = sc->sc_oid;
409 vals[0].bv_len = strlen( sc->sc_oid );
411 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
420 * Return a list of OIDs and operation masks for supported
421 * controls. Used by SLAPI.
424 get_supported_controls(char ***ctrloidsp,
425 slap_mask_t **ctrlmasks)
430 struct slap_control *sc;
434 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
444 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
445 if ( oids == NULL ) {
446 return LDAP_NO_MEMORY;
448 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
449 if ( masks == NULL ) {
451 return LDAP_NO_MEMORY;
456 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
457 oids[n] = ch_strdup( sc->sc_oid );
458 masks[n] = sc->sc_mask;
471 * Find a control given its OID.
473 static struct slap_control *
474 find_ctrl( const char *oid )
476 struct slap_control *sc;
478 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
479 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
488 slap_find_control_id(
492 struct slap_control *ctrl = find_ctrl( oid );
494 if ( cid ) *cid = ctrl->sc_cid;
497 return LDAP_CONTROL_NOT_FOUND;
501 slap_global_control( Operation *op, const char *oid, int *cid )
503 struct slap_control *ctrl = find_ctrl( oid );
505 if ( ctrl == NULL ) {
506 /* should not be reachable */
507 Debug( LDAP_DEBUG_ANY,
508 "slap_global_control: unrecognized control: %s\n",
510 return LDAP_CONTROL_NOT_FOUND;
513 if ( cid ) *cid = ctrl->sc_cid;
515 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
516 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
517 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
519 return LDAP_COMPARE_TRUE;
523 Debug( LDAP_DEBUG_TRACE,
524 "slap_global_control: unavailable control: %s\n",
528 return LDAP_COMPARE_FALSE;
531 void slap_free_ctrls(
533 LDAPControl **ctrls )
537 for (i=0; ctrls[i]; i++) {
538 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
540 op->o_tmpfree( ctrls, op->o_tmpmemctx );
546 LDAPControl *control,
549 struct slap_control *sc;
550 int rc = LDAP_SUCCESS;
552 sc = find_ctrl( control->ldctl_oid );
554 /* recognized control */
556 switch( op->o_tag ) {
558 tagmask = SLAP_CTRL_ADD;
561 tagmask = SLAP_CTRL_BIND;
563 case LDAP_REQ_COMPARE:
564 tagmask = SLAP_CTRL_COMPARE;
566 case LDAP_REQ_DELETE:
567 tagmask = SLAP_CTRL_DELETE;
569 case LDAP_REQ_MODIFY:
570 tagmask = SLAP_CTRL_MODIFY;
572 case LDAP_REQ_RENAME:
573 tagmask = SLAP_CTRL_RENAME;
575 case LDAP_REQ_SEARCH:
576 tagmask = SLAP_CTRL_SEARCH;
578 case LDAP_REQ_UNBIND:
579 tagmask = SLAP_CTRL_UNBIND;
581 case LDAP_REQ_ABANDON:
582 tagmask = SLAP_CTRL_ABANDON;
584 case LDAP_REQ_EXTENDED:
586 assert( op->ore_reqoid.bv_val != NULL );
587 if( sc->sc_extendedopsbv != NULL ) {
589 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
590 if( bvmatch( &op->ore_reqoid,
591 &sc->sc_extendedopsbv[i] ) )
600 *text = "controls internal error";
604 if (( sc->sc_mask & tagmask ) == tagmask ) {
605 /* available extension */
606 if ( sc->sc_parse ) {
607 rc = sc->sc_parse( op, rs, control );
608 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
610 } else if ( control->ldctl_iscritical ) {
611 *text = "not yet implemented";
616 } else if ( control->ldctl_iscritical ) {
617 /* unavailable CRITICAL control */
618 *text = "critical extension is unavailable";
619 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
622 } else if ( control->ldctl_iscritical ) {
623 /* unrecognized CRITICAL control */
624 *text = "critical extension is not recognized";
625 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
640 BerElement *ber = op->o_ber;
642 #ifdef SLAP_CONTROL_X_WHATFAILED
643 /* NOTE: right now, slapd checks the validity of each control
644 * while parsing. As a consequence, it can only detect one
645 * cause of failure at a time. This results in returning
646 * exactly one OID with the whatFailed control, or no control
649 char *failed_oid = NULL;
652 len = ber_pvt_ber_remaining(ber);
656 rs->sr_err = LDAP_SUCCESS;
660 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
661 if( tag == LBER_ERROR ) {
662 rs->sr_err = SLAPD_DISCONNECT;
663 rs->sr_text = "unexpected data in PDU";
669 Debug( LDAP_DEBUG_TRACE,
670 "=> get_ctrls\n", 0, 0, 0 );
672 if( op->o_protocol < LDAP_VERSION3 ) {
673 rs->sr_err = SLAPD_DISCONNECT;
674 rs->sr_text = "controls require LDAPv3";
678 /* one for first control, one for termination */
679 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
682 if( op->ctrls == NULL ) {
683 rs->sr_err = LDAP_NO_MEMORY;
684 rs->sr_text = "no memory";
689 op->o_ctrls[nctrls] = NULL;
691 /* step through each element */
692 for( tag = ber_first_element( ber, &len, &opaque );
694 tag = ber_next_element( ber, &len, opaque ) )
697 LDAPControl **tctrls;
699 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
700 memset(c, 0, sizeof(LDAPControl));
702 /* allocate pointer space for current controls (nctrls)
703 * + this control + extra NULL
705 tctrls = op->o_tmprealloc( op->o_ctrls,
706 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
709 if( tctrls == NULL ) {
711 ldap_controls_free(op->o_ctrls);
714 rs->sr_err = LDAP_NO_MEMORY;
715 rs->sr_text = "no memory";
719 op->o_ctrls = tctrls;
721 op->o_ctrls[nctrls++] = c;
722 op->o_ctrls[nctrls] = NULL;
724 tag = ber_scanf( ber, "{m" /*}*/, &bv );
725 c->ldctl_oid = bv.bv_val;
727 if( tag == LBER_ERROR ) {
728 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
731 slap_free_ctrls( op, op->o_ctrls );
733 rs->sr_err = SLAPD_DISCONNECT;
734 rs->sr_text = "decoding controls error";
737 } else if( c->ldctl_oid == NULL ) {
738 Debug( LDAP_DEBUG_TRACE,
739 "get_ctrls: conn %lu got emtpy OID.\n",
740 op->o_connid, 0, 0 );
742 slap_free_ctrls( op, op->o_ctrls );
744 rs->sr_err = LDAP_PROTOCOL_ERROR;
745 rs->sr_text = "OID field is empty";
749 tag = ber_peek_tag( ber, &len );
751 if( tag == LBER_BOOLEAN ) {
753 tag = ber_scanf( ber, "b", &crit );
755 if( tag == LBER_ERROR ) {
756 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
758 slap_free_ctrls( op, op->o_ctrls );
760 rs->sr_err = SLAPD_DISCONNECT;
761 rs->sr_text = "decoding controls error";
765 c->ldctl_iscritical = (crit != 0);
766 tag = ber_peek_tag( ber, &len );
769 if( tag == LBER_OCTETSTRING ) {
770 tag = ber_scanf( ber, "m", &c->ldctl_value );
772 if( tag == LBER_ERROR ) {
773 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
774 "%s (%scritical): get value failed.\n",
775 op->o_connid, c->ldctl_oid,
776 c->ldctl_iscritical ? "" : "non" );
777 slap_free_ctrls( op, op->o_ctrls );
779 rs->sr_err = SLAPD_DISCONNECT;
780 rs->sr_text = "decoding controls error";
785 Debug( LDAP_DEBUG_TRACE,
786 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
787 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
789 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
790 if ( rs->sr_err != LDAP_SUCCESS ) {
791 #ifdef SLAP_CONTROL_X_WHATFAILED
792 failed_oid = c->ldctl_oid;
799 Debug( LDAP_DEBUG_TRACE,
800 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
801 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
803 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
804 if( rs->sr_err == SLAPD_DISCONNECT ) {
805 rs->sr_err = LDAP_PROTOCOL_ERROR;
806 send_ldap_disconnect( op, rs );
807 rs->sr_err = SLAPD_DISCONNECT;
809 #ifdef SLAP_CONTROL_X_WHATFAILED
810 /* might have not been parsed yet? */
811 if ( failed_oid != NULL ) {
812 if ( !get_whatFailed( op ) ) {
815 /* step through each remaining element */
816 for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
818 LDAPControl c = { 0 };
820 tag = ber_scanf( ber, "{m" /*}*/, &bv );
821 c.ldctl_oid = bv.bv_val;
823 if ( tag == LBER_ERROR ) {
824 slap_free_ctrls( op, op->o_ctrls );
828 } else if ( c.ldctl_oid == NULL ) {
829 slap_free_ctrls( op, op->o_ctrls );
834 tag = ber_peek_tag( ber, &len );
835 if ( tag == LBER_BOOLEAN ) {
837 tag = ber_scanf( ber, "b", &crit );
838 if( tag == LBER_ERROR ) {
839 slap_free_ctrls( op, op->o_ctrls );
844 tag = ber_peek_tag( ber, &len );
847 if ( tag == LBER_OCTETSTRING ) {
848 tag = ber_scanf( ber, "m", &c.ldctl_value );
850 if( tag == LBER_ERROR ) {
851 slap_free_ctrls( op, op->o_ctrls );
857 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
859 slap_parse_ctrl( op, rs, &c, &text );
865 if ( get_whatFailed( op ) ) {
866 char *oids[ 2 ] = { failed_oid, NULL };
867 slap_ctrl_whatFailed_add( op, rs, oids );
872 send_ldap_result( op, rs );
884 BI_chk_controls fnc )
888 switch ( op->o_ctrlflag[ ctrl ] ) {
889 case SLAP_CONTROL_NONCRITICAL:
890 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
891 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
892 slap_known_controls[ ctrl - 1 ] ) == 0 )
899 rs->sr_err = LDAP_OTHER;
907 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
910 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
911 ( i - j ) * sizeof( LDAPControl * ) );
914 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
918 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
920 Debug( LDAP_DEBUG_ANY, "%s: "
921 "non-critical control \"%s\" not supported; stripped.\n",
922 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
925 case SLAP_CONTROL_IGNORED:
926 case SLAP_CONTROL_NONE:
927 rs->sr_err = SLAP_CB_CONTINUE;
930 case SLAP_CONTROL_CRITICAL:
931 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
935 Debug( LDAP_DEBUG_ANY, "%s: "
936 "critical control \"%s\" not supported.\n",
937 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
941 /* handle all cases! */
948 static int parseDontUseCopy (
953 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
954 rs->sr_text = "dontUseCopy control specified multiple times";
955 return LDAP_PROTOCOL_ERROR;
958 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
959 rs->sr_text = "dontUseCopy control value not absent";
960 return LDAP_PROTOCOL_ERROR;
963 if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
964 && !ctrl->ldctl_iscritical )
966 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
967 return LDAP_PROTOCOL_ERROR;
970 op->o_dontUseCopy = ctrl->ldctl_iscritical
971 ? SLAP_CONTROL_CRITICAL
972 : SLAP_CONTROL_NONCRITICAL;
977 static int parseRelax (
982 if ( op->o_relax != SLAP_CONTROL_NONE ) {
983 rs->sr_text = "relax control specified multiple times";
984 return LDAP_PROTOCOL_ERROR;
987 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
988 rs->sr_text = "relax control value not absent";
989 return LDAP_PROTOCOL_ERROR;
992 op->o_relax = ctrl->ldctl_iscritical
993 ? SLAP_CONTROL_CRITICAL
994 : SLAP_CONTROL_NONCRITICAL;
999 static int parseManageDSAit (
1004 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1005 rs->sr_text = "manageDSAit control specified multiple times";
1006 return LDAP_PROTOCOL_ERROR;
1009 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1010 rs->sr_text = "manageDSAit control value not absent";
1011 return LDAP_PROTOCOL_ERROR;
1014 op->o_managedsait = ctrl->ldctl_iscritical
1015 ? SLAP_CONTROL_CRITICAL
1016 : SLAP_CONTROL_NONCRITICAL;
1018 return LDAP_SUCCESS;
1021 static int parseProxyAuthz (
1027 struct berval dn = BER_BVNULL;
1029 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1030 rs->sr_text = "proxy authorization control specified multiple times";
1031 return LDAP_PROTOCOL_ERROR;
1034 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1035 rs->sr_text = "proxy authorization control value absent";
1036 return LDAP_PROTOCOL_ERROR;
1039 if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1040 && !ctrl->ldctl_iscritical )
1042 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1043 return LDAP_PROTOCOL_ERROR;
1046 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1047 && BER_BVISEMPTY( &op->o_ndn ) )
1049 rs->sr_text = "anonymous proxied authorization not allowed";
1050 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1053 op->o_proxy_authz = ctrl->ldctl_iscritical
1054 ? SLAP_CONTROL_CRITICAL
1055 : SLAP_CONTROL_NONCRITICAL;
1057 Debug( LDAP_DEBUG_ARGS,
1058 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
1060 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
1063 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1064 Debug( LDAP_DEBUG_TRACE,
1065 "parseProxyAuthz: conn=%lu anonymous\n",
1066 op->o_connid, 0, 0 );
1069 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1070 op->o_ndn.bv_val[ 0 ] = '\0';
1072 op->o_ndn.bv_len = 0;
1074 if ( !BER_BVISNULL( &op->o_dn ) ) {
1075 op->o_dn.bv_val[ 0 ] = '\0';
1077 op->o_dn.bv_len = 0;
1079 return LDAP_SUCCESS;
1082 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1083 NULL, &dn, SLAP_GETDN_AUTHZID );
1085 /* FIXME: empty DN in proxyAuthz control should be legal... */
1086 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1088 ch_free( dn.bv_val );
1090 rs->sr_text = "authzId mapping failed";
1091 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1094 Debug( LDAP_DEBUG_TRACE,
1095 "parseProxyAuthz: conn=%lu \"%s\"\n",
1097 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1099 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1102 ch_free( dn.bv_val );
1103 rs->sr_text = "not authorized to assume identity";
1104 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1107 ch_free( op->o_ndn.bv_val );
1108 ch_free( op->o_dn.bv_val );
1111 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1112 * from now on op->o_dn is normalized
1115 ber_dupbv( &op->o_dn, &dn );
1117 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1118 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1120 return LDAP_SUCCESS;
1123 static int parseNoOp (
1128 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1129 rs->sr_text = "noop control specified multiple times";
1130 return LDAP_PROTOCOL_ERROR;
1133 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1134 rs->sr_text = "noop control value not empty";
1135 return LDAP_PROTOCOL_ERROR;
1138 op->o_noop = ctrl->ldctl_iscritical
1139 ? SLAP_CONTROL_CRITICAL
1140 : SLAP_CONTROL_NONCRITICAL;
1142 return LDAP_SUCCESS;
1145 static int parsePagedResults (
1150 BerElementBuffer berbuf;
1151 BerElement *ber = (BerElement *)&berbuf;
1152 struct berval cookie;
1153 PagedResultsState *ps;
1154 int rc = LDAP_SUCCESS;
1158 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1159 rs->sr_text = "paged results control specified multiple times";
1160 return LDAP_PROTOCOL_ERROR;
1163 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1164 rs->sr_text = "paged results control value is absent";
1165 return LDAP_PROTOCOL_ERROR;
1168 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1169 rs->sr_text = "paged results control value is empty";
1170 return LDAP_PROTOCOL_ERROR;
1173 /* Parse the control value
1174 * realSearchControlValue ::= SEQUENCE {
1175 * size INTEGER (0..maxInt),
1176 * -- requested page size from client
1177 * -- result set size estimate from server
1178 * cookie OCTET STRING
1181 ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1183 tag = ber_scanf( ber, "{im}", &size, &cookie );
1185 if ( tag == LBER_ERROR ) {
1186 rs->sr_text = "paged results control could not be decoded";
1187 rc = LDAP_PROTOCOL_ERROR;
1192 rs->sr_text = "paged results control size invalid";
1193 rc = LDAP_PROTOCOL_ERROR;
1197 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1198 *ps = op->o_conn->c_pagedresults_state;
1200 ps->ps_cookieval = cookie;
1201 op->o_pagedresults_state = ps;
1202 if ( !cookie.bv_len ) {
1207 /* NOTE: according to RFC 2696 3.:
1209 If the page size is greater than or equal to the sizeLimit value, the
1210 server should ignore the control as the request can be satisfied in a
1213 * NOTE: this assumes that the op->ors_slimit be set
1214 * before the controls are parsed.
1217 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1218 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1220 } else if ( ctrl->ldctl_iscritical ) {
1221 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1224 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1231 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1232 static int parseSortedResults (
1237 int rc = LDAP_SUCCESS;
1239 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1240 rs->sr_text = "sorted results control specified multiple times";
1241 return LDAP_PROTOCOL_ERROR;
1244 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1245 rs->sr_text = "sorted results control value is absent";
1246 return LDAP_PROTOCOL_ERROR;
1249 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1250 rs->sr_text = "sorted results control value is empty";
1251 return LDAP_PROTOCOL_ERROR;
1254 /* blow off parsing the value */
1256 op->o_sortedresults = ctrl->ldctl_iscritical
1257 ? SLAP_CONTROL_CRITICAL
1258 : SLAP_CONTROL_NONCRITICAL;
1264 static int parseAssert (
1270 struct berval fstr = BER_BVNULL;
1272 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1273 rs->sr_text = "assert control specified multiple times";
1274 return LDAP_PROTOCOL_ERROR;
1277 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1278 rs->sr_text = "assert control value is absent";
1279 return LDAP_PROTOCOL_ERROR;
1282 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1283 rs->sr_text = "assert control value is empty";
1284 return LDAP_PROTOCOL_ERROR;
1287 ber = ber_init( &(ctrl->ldctl_value) );
1289 rs->sr_text = "assert control: internal error";
1293 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1295 (void) ber_free( ber, 1 );
1296 if( rs->sr_err != LDAP_SUCCESS ) {
1297 if( rs->sr_err == SLAPD_DISCONNECT ) {
1298 rs->sr_err = LDAP_PROTOCOL_ERROR;
1299 send_ldap_disconnect( op, rs );
1300 rs->sr_err = SLAPD_DISCONNECT;
1302 send_ldap_result( op, rs );
1304 if( op->o_assertion != NULL ) {
1305 filter_free_x( op, op->o_assertion, 1 );
1311 filter2bv_x( op, op->o_assertion, &fstr );
1313 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1314 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1315 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1318 op->o_assert = ctrl->ldctl_iscritical
1319 ? SLAP_CONTROL_CRITICAL
1320 : SLAP_CONTROL_NONCRITICAL;
1322 rs->sr_err = LDAP_SUCCESS;
1323 return LDAP_SUCCESS;
1326 #define READMSG(post, msg) \
1327 ( post ? "postread control: " msg : "preread control: " msg )
1336 ber_len_t siz, off, i;
1338 AttributeName *an = NULL;
1340 if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1341 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1343 rs->sr_text = READMSG( post, "specified multiple times" );
1344 return LDAP_PROTOCOL_ERROR;
1347 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1348 rs->sr_text = READMSG( post, "value is absent" );
1349 return LDAP_PROTOCOL_ERROR;
1352 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1353 rs->sr_text = READMSG( post, "value is empty" );
1354 return LDAP_PROTOCOL_ERROR;
1358 if ( op->o_txnSpec ) { /* temporary limitation */
1359 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1360 return LDAP_UNWILLING_TO_PERFORM;
1364 ber = ber_init( &ctrl->ldctl_value );
1365 if ( ber == NULL ) {
1366 rs->sr_text = READMSG( post, "internal error" );
1370 rs->sr_err = LDAP_SUCCESS;
1371 siz = sizeof( AttributeName );
1372 off = offsetof( AttributeName, an_name );
1373 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1374 rs->sr_text = READMSG( post, "decoding error" );
1375 rs->sr_err = LDAP_PROTOCOL_ERROR;
1379 for ( i = 0; i < siz; i++ ) {
1380 const char *dummy = NULL;
1383 an[i].an_desc = NULL;
1385 an[i].an_oc_exclude = 0;
1386 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1387 if ( rc == LDAP_SUCCESS ) {
1388 an[i].an_name = an[i].an_desc->ad_cname;
1392 static struct berval special_attrs[] = {
1393 BER_BVC( LDAP_NO_ATTRS ),
1394 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1395 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1399 /* deal with special attribute types */
1400 for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1401 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1402 an[i].an_name = special_attrs[ j ];
1407 if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1409 rs->sr_text = dummy ? dummy
1410 : READMSG( post, "unknown attributeType" );
1417 op->o_postread_attrs = an;
1418 op->o_postread = ctrl->ldctl_iscritical
1419 ? SLAP_CONTROL_CRITICAL
1420 : SLAP_CONTROL_NONCRITICAL;
1422 op->o_preread_attrs = an;
1423 op->o_preread = ctrl->ldctl_iscritical
1424 ? SLAP_CONTROL_CRITICAL
1425 : SLAP_CONTROL_NONCRITICAL;
1429 (void) ber_free( ber, 1 );
1433 static int parsePreRead (
1438 return parseReadAttrs( op, rs, ctrl, 0 );
1441 static int parsePostRead (
1446 return parseReadAttrs( op, rs, ctrl, 1 );
1449 static int parseValuesReturnFilter (
1455 struct berval fstr = BER_BVNULL;
1457 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1458 rs->sr_text = "valuesReturnFilter control specified multiple times";
1459 return LDAP_PROTOCOL_ERROR;
1462 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1463 rs->sr_text = "valuesReturnFilter control value is absent";
1464 return LDAP_PROTOCOL_ERROR;
1467 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1468 rs->sr_text = "valuesReturnFilter control value is empty";
1469 return LDAP_PROTOCOL_ERROR;
1472 ber = ber_init( &(ctrl->ldctl_value) );
1474 rs->sr_text = "internal error";
1478 rs->sr_err = get_vrFilter( op, ber,
1479 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1481 (void) ber_free( ber, 1 );
1483 if( rs->sr_err != LDAP_SUCCESS ) {
1484 if( rs->sr_err == SLAPD_DISCONNECT ) {
1485 rs->sr_err = LDAP_PROTOCOL_ERROR;
1486 send_ldap_disconnect( op, rs );
1487 rs->sr_err = SLAPD_DISCONNECT;
1489 send_ldap_result( op, rs );
1491 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1495 vrFilter2bv( op, op->o_vrFilter, &fstr );
1498 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1499 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1500 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1503 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1504 ? SLAP_CONTROL_CRITICAL
1505 : SLAP_CONTROL_NONCRITICAL;
1507 rs->sr_err = LDAP_SUCCESS;
1508 return LDAP_SUCCESS;
1511 static int parseSubentries (
1516 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1517 rs->sr_text = "subentries control specified multiple times";
1518 return LDAP_PROTOCOL_ERROR;
1521 /* FIXME: should use BER library */
1522 if( ( ctrl->ldctl_value.bv_len != 3 )
1523 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1524 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1526 rs->sr_text = "subentries control value encoding is bogus";
1527 return LDAP_PROTOCOL_ERROR;
1530 op->o_subentries = ctrl->ldctl_iscritical
1531 ? SLAP_CONTROL_CRITICAL
1532 : SLAP_CONTROL_NONCRITICAL;
1534 if (ctrl->ldctl_value.bv_val[2]) {
1535 set_subentries_visibility( op );
1538 return LDAP_SUCCESS;
1541 static int parsePermissiveModify (
1546 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1547 rs->sr_text = "permissiveModify control specified multiple times";
1548 return LDAP_PROTOCOL_ERROR;
1551 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1552 rs->sr_text = "permissiveModify control value not absent";
1553 return LDAP_PROTOCOL_ERROR;
1556 op->o_permissive_modify = ctrl->ldctl_iscritical
1557 ? SLAP_CONTROL_CRITICAL
1558 : SLAP_CONTROL_NONCRITICAL;
1560 return LDAP_SUCCESS;
1563 static int parseDomainScope (
1568 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1569 rs->sr_text = "domainScope control specified multiple times";
1570 return LDAP_PROTOCOL_ERROR;
1573 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1574 rs->sr_text = "domainScope control value not empty";
1575 return LDAP_PROTOCOL_ERROR;
1578 op->o_domain_scope = ctrl->ldctl_iscritical
1579 ? SLAP_CONTROL_CRITICAL
1580 : SLAP_CONTROL_NONCRITICAL;
1582 return LDAP_SUCCESS;
1585 #ifdef SLAP_CONTROL_X_TREE_DELETE
1586 static int parseTreeDelete (
1591 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1592 rs->sr_text = "treeDelete control specified multiple times";
1593 return LDAP_PROTOCOL_ERROR;
1596 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1597 rs->sr_text = "treeDelete control value not absent";
1598 return LDAP_PROTOCOL_ERROR;
1601 op->o_tree_delete = ctrl->ldctl_iscritical
1602 ? SLAP_CONTROL_CRITICAL
1603 : SLAP_CONTROL_NONCRITICAL;
1605 return LDAP_SUCCESS;
1609 static int parseSearchOptions (
1615 ber_int_t search_flags;
1618 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1619 rs->sr_text = "searchOptions control value is absent";
1620 return LDAP_PROTOCOL_ERROR;
1623 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1624 rs->sr_text = "searchOptions control value is empty";
1625 return LDAP_PROTOCOL_ERROR;
1628 ber = ber_init( &ctrl->ldctl_value );
1630 rs->sr_text = "internal error";
1634 tag = ber_scanf( ber, "{i}", &search_flags );
1635 (void) ber_free( ber, 1 );
1637 if ( tag == LBER_ERROR ) {
1638 rs->sr_text = "searchOptions control decoding error";
1639 return LDAP_PROTOCOL_ERROR;
1642 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1643 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1644 rs->sr_text = "searchOptions control specified multiple times "
1645 "or with domainScope control";
1646 return LDAP_PROTOCOL_ERROR;
1649 op->o_domain_scope = ctrl->ldctl_iscritical
1650 ? SLAP_CONTROL_CRITICAL
1651 : SLAP_CONTROL_NONCRITICAL;
1654 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1655 /* Other search flags not recognised so far,
1657 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1659 rs->sr_text = "searchOptions contained unrecognized flag";
1660 return LDAP_UNWILLING_TO_PERFORM;
1663 return LDAP_SUCCESS;
1666 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1667 struct berval session_tracking_formats[] = {
1668 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1669 BER_BVC( "RADIUS-Acct-Session-Id" ),
1670 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1671 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1672 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1673 BER_BVC( "USERNAME" ),
1678 static int parseSessionTracking(
1688 struct berval sessionSourceIp = BER_BVNULL,
1689 sessionSourceName = BER_BVNULL,
1690 formatOID = BER_BVNULL,
1691 sessionTrackingIdentifier = BER_BVNULL;
1693 size_t st_len, st_pos;
1695 if ( ctrl->ldctl_iscritical ) {
1696 rs->sr_text = "sessionTracking criticality is TRUE";
1697 return LDAP_PROTOCOL_ERROR;
1700 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1701 rs->sr_text = "sessionTracking control value is absent";
1702 return LDAP_PROTOCOL_ERROR;
1705 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1706 rs->sr_text = "sessionTracking control value is empty";
1707 return LDAP_PROTOCOL_ERROR;
1710 /* TODO: add the capability to determine if a client is allowed
1711 * to use this control, based on identity, ip and so */
1713 ber = ber_init( &ctrl->ldctl_value );
1714 if ( ber == NULL ) {
1715 rs->sr_text = "internal error";
1719 tag = ber_skip_tag( ber, &len );
1720 if ( tag != LBER_SEQUENCE ) {
1725 /* sessionSourceIp */
1726 tag = ber_peek_tag( ber, &len );
1727 if ( tag == LBER_DEFAULT ) {
1733 tag = ber_skip_tag( ber, &len );
1735 } else if ( len > 128 ) {
1736 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1737 rs->sr_err = LDAP_PROTOCOL_ERROR;
1741 tag = ber_scanf( ber, "m", &sessionSourceIp );
1744 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1745 BER_BVZERO( &sessionSourceIp );
1748 /* sessionSourceName */
1749 tag = ber_peek_tag( ber, &len );
1750 if ( tag == LBER_DEFAULT ) {
1756 tag = ber_skip_tag( ber, &len );
1758 } else if ( len > 65536 ) {
1759 rs->sr_text = "sessionTracking.sessionSourceName too long";
1760 rs->sr_err = LDAP_PROTOCOL_ERROR;
1764 tag = ber_scanf( ber, "m", &sessionSourceName );
1767 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1768 BER_BVZERO( &sessionSourceName );
1772 tag = ber_peek_tag( ber, &len );
1773 if ( tag == LBER_DEFAULT ) {
1779 rs->sr_text = "sessionTracking.formatOID empty";
1780 rs->sr_err = LDAP_PROTOCOL_ERROR;
1783 } else if ( len > 1024 ) {
1784 rs->sr_text = "sessionTracking.formatOID too long";
1785 rs->sr_err = LDAP_PROTOCOL_ERROR;
1789 tag = ber_scanf( ber, "m", &formatOID );
1792 rc = numericoidValidate( NULL, &formatOID );
1793 if ( rc != LDAP_SUCCESS ) {
1794 rs->sr_text = "sessionTracking.formatOID invalid";
1798 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1800 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1801 formatOID = session_tracking_formats[ i + 1 ];
1806 /* sessionTrackingIdentifier */
1807 tag = ber_peek_tag( ber, &len );
1808 if ( tag == LBER_DEFAULT ) {
1814 tag = ber_skip_tag( ber, &len );
1817 /* note: should not be more than 65536... */
1818 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1819 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1820 /* we want the OID printed, at least */
1821 BER_BVSTR( &sessionTrackingIdentifier, "" );
1826 tag = ber_skip_tag( ber, &len );
1827 if ( tag != LBER_DEFAULT || len != 0 ) {
1834 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1835 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1837 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1838 if ( st_len ) st_len++;
1839 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1841 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1842 if ( st_len ) st_len++;
1843 st_len += formatOID.bv_len + STRLENOF( "=" )
1844 + sessionTrackingIdentifier.bv_len;
1847 if ( st_len == 0 ) {
1851 st_len += STRLENOF( " []" );
1852 st_pos = strlen( op->o_log_prefix );
1854 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1855 char *ptr = &op->o_log_prefix[ st_pos ];
1857 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1860 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1861 ptr = lutil_strcopy( ptr, "IP=" );
1862 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1866 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1867 if ( st_len ) *ptr++ = ' ';
1868 ptr = lutil_strcopy( ptr, "NAME=" );
1869 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1873 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1874 if ( st_len ) *ptr++ = ' ';
1875 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1877 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1885 (void)ber_free( ber, 1 );
1887 if ( tag == LBER_ERROR ) {
1888 rs->sr_text = "sessionTracking control decoding error";
1889 return LDAP_PROTOCOL_ERROR;
1897 slap_ctrl_session_tracking_add(
1901 struct berval *name,
1905 BerElementBuffer berbuf;
1906 BerElement *ber = (BerElement *)&berbuf;
1908 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
1910 assert( ctrl != NULL );
1912 ber_init2( ber, NULL, LBER_USE_DER );
1914 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
1916 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
1917 rs->sr_err = LDAP_OTHER;
1921 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1922 ctrl->ldctl_iscritical = 0;
1924 rs->sr_err = LDAP_SUCCESS;
1931 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
1933 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
1934 struct berval ip = BER_BVNULL,
1938 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
1939 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1943 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
1944 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
1946 ptr = ber_bvchr( &ip, ':' );
1948 ip.bv_len = ptr - ip.bv_val;
1952 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
1953 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
1955 name = op->o_conn->c_peer_domain;
1958 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
1962 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
1966 #ifdef SLAP_CONTROL_X_WHATFAILED
1967 static int parseWhatFailed(
1972 if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
1973 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
1974 return LDAP_PROTOCOL_ERROR;
1977 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1978 rs->sr_text = "\"What Failed?\" control value not absent";
1979 return LDAP_PROTOCOL_ERROR;
1982 op->o_whatFailed = ctrl->ldctl_iscritical
1983 ? SLAP_CONTROL_CRITICAL
1984 : SLAP_CONTROL_NONCRITICAL;
1986 return LDAP_SUCCESS;
1990 slap_ctrl_whatFailed_add(
1995 BerElementBuffer berbuf;
1996 BerElement *ber = (BerElement *) &berbuf;
1997 LDAPControl **ctrls = NULL;
1998 struct berval ctrlval;
1999 int i, rc = LDAP_SUCCESS;
2001 ber_init2( ber, NULL, LBER_USE_DER );
2002 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2003 ber_printf( ber, "[" /*]*/ );
2004 for ( i = 0; oids[ i ] != NULL; i++ ) {
2005 ber_printf( ber, "s", oids[ i ] );
2007 ber_printf( ber, /*[*/ "]" );
2009 if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2015 if ( rs->sr_ctrls != NULL ) {
2016 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2017 if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2024 ctrls = op->o_tmprealloc( rs->sr_ctrls,
2025 sizeof(LDAPControl *)*( i + 2 )
2026 + sizeof(LDAPControl)
2027 + ctrlval.bv_len + 1,
2029 if ( ctrls == NULL ) {
2033 ctrls[ i + 1 ] = NULL;
2034 ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2035 ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2036 ctrls[ i ]->ldctl_iscritical = 0;
2037 ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2038 AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2039 ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2041 ber_free_buf( ber );
2043 rs->sr_ctrls = ctrls;