2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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;
52 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
54 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
55 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
57 struct slap_control_ids slap_cids;
63 /* The controlID for this control */
66 /* Operations supported by control */
69 /* Extended operations supported by control */
70 char **sc_extendedops; /* input */
71 BerVarray sc_extendedopsbv; /* run-time use */
73 /* Control parsing callback */
74 SLAP_CTRL_PARSE_FN *sc_parse;
76 LDAP_SLIST_ENTRY(slap_control) sc_next;
79 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
80 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
83 * all known request control OIDs should be added to this list
86 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
87 * addresses an undefined control; this allows to safely test for
88 * well known controls even if they are not registered, e.g. if
89 * they get moved to modules. An example is sc_LDAPsync, which
90 * is implemented in the syncprov overlay and thus, if configured
91 * as dynamic module, may not be registered. One side effect is that
92 * slap_known_controls[0] == NULL, so it should always be used
94 * FIXME: should we define the "undefined control" oid?
96 char *slap_known_controls[SLAP_MAX_CIDS+1];
97 static int num_known_controls = 1;
99 static char *proxy_authz_extops[] = {
100 LDAP_EXOP_MODIFY_PASSWD,
106 static char *manageDSAit_extops[] = {
111 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
112 static char *session_tracking_extops[] = {
113 LDAP_EXOP_MODIFY_PASSWD,
120 static struct slap_control control_defs[] = {
121 { LDAP_CONTROL_ASSERT,
122 (int)offsetof(struct slap_control_ids, sc_assert),
123 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME|
124 SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
126 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
127 { LDAP_CONTROL_PRE_READ,
128 (int)offsetof(struct slap_control_ids, sc_preRead),
129 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
131 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
132 { LDAP_CONTROL_POST_READ,
133 (int)offsetof(struct slap_control_ids, sc_postRead),
134 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
136 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
137 { LDAP_CONTROL_VALUESRETURNFILTER,
138 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
139 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
141 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
142 { LDAP_CONTROL_PAGEDRESULTS,
143 (int)offsetof(struct slap_control_ids, sc_pagedResults),
146 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
147 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
148 { LDAP_CONTROL_SORTREQUEST,
149 (int)offsetof(struct slap_control_ids, sc_sortedResults),
150 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
152 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
154 { LDAP_CONTROL_X_DOMAIN_SCOPE,
155 (int)offsetof(struct slap_control_ids, sc_domainScope),
156 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
158 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
159 { LDAP_CONTROL_DONTUSECOPY,
160 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
161 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
163 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
164 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
165 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
166 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
168 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
169 #ifdef SLAP_CONTROL_X_TREE_DELETE
170 { LDAP_CONTROL_X_TREE_DELETE,
171 (int)offsetof(struct slap_control_ids, sc_treeDelete),
172 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
174 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
176 { LDAP_CONTROL_X_SEARCH_OPTIONS,
177 (int)offsetof(struct slap_control_ids, sc_searchOptions),
178 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
180 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
181 { LDAP_CONTROL_SUBENTRIES,
182 (int)offsetof(struct slap_control_ids, sc_subentries),
185 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
187 (int)offsetof(struct slap_control_ids, sc_noOp),
188 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
190 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
191 { LDAP_CONTROL_RELAX,
192 (int)offsetof(struct slap_control_ids, sc_relax),
193 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
195 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
197 { LDAP_CONTROL_X_TXN_SPEC,
198 (int)offsetof(struct slap_control_ids, sc_txnSpec),
199 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
201 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
203 { LDAP_CONTROL_MANAGEDSAIT,
204 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
206 manageDSAit_extops, NULL,
207 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
208 { LDAP_CONTROL_PROXY_AUTHZ,
209 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
210 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
211 proxy_authz_extops, NULL,
212 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
213 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
214 { LDAP_CONTROL_X_SESSION_TRACKING,
215 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
216 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
217 session_tracking_extops, NULL,
218 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
220 { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
223 static struct slap_control *
224 find_ctrl( const char *oid );
227 * Register a supported control.
229 * This can be called by an OpenLDAP plugin or, indirectly, by a
230 * SLAPI plugin calling slapi_register_supported_control().
232 * NOTE: if flags == 1 the control is replaced if already registered;
233 * otherwise registering an already registered control is not allowed.
236 register_supported_control2(const char *controloid,
237 slap_mask_t controlmask,
239 SLAP_CTRL_PARSE_FN *controlparsefn,
243 struct slap_control *sc = NULL;
245 BerVarray extendedopsbv = NULL;
247 if ( num_known_controls >= SLAP_MAX_CIDS ) {
248 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
249 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
250 SLAP_MAX_CIDS, 0, 0 );
254 if ( controloid == NULL ) {
255 return LDAP_PARAM_ERROR;
258 /* check if already registered */
259 for ( i = 0; slap_known_controls[ i ]; i++ ) {
260 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
262 Debug( LDAP_DEBUG_TRACE,
263 "Control %s already registered; replacing.\n",
265 /* (find and) replace existing handler */
266 sc = find_ctrl( controloid );
267 assert( sc != NULL );
271 Debug( LDAP_DEBUG_ANY,
272 "Control %s already registered.\n",
274 return LDAP_PARAM_ERROR;
278 /* turn compatible extended operations into bervals */
279 if ( controlexops != NULL ) {
282 for ( i = 0; controlexops[ i ]; i++ );
284 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
285 if ( extendedopsbv == NULL ) {
286 return LDAP_NO_MEMORY;
289 for ( i = 0; controlexops[ i ]; i++ ) {
290 ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
295 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
297 return LDAP_NO_MEMORY;
300 sc->sc_oid = ch_strdup( controloid );
301 sc->sc_cid = num_known_controls;
303 /* Update slap_known_controls, too. */
304 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
305 slap_known_controls[num_known_controls++] = NULL;
307 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
308 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
311 if ( sc->sc_extendedopsbv ) {
312 /* FIXME: in principle, we should rather merge
313 * existing extops with those supported by the
314 * new control handling implementation.
315 * In fact, whether a control is compatible with
316 * an extop should not be a matter of implementation.
317 * We likely also need a means for a newly
318 * registered extop to declare that it is
319 * comptible with an already registered control.
321 ber_bvarray_free( sc->sc_extendedopsbv );
322 sc->sc_extendedopsbv = NULL;
323 sc->sc_extendedops = NULL;
327 sc->sc_extendedopsbv = extendedopsbv;
328 sc->sc_mask = controlmask;
329 sc->sc_parse = controlparsefn;
331 *controlcid = sc->sc_cid;
338 * One-time initialization of internal controls.
341 slap_controls_init( void )
347 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
348 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
349 rc = register_supported_control( control_defs[i].sc_oid,
350 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
351 control_defs[i].sc_parse, cid );
352 if ( rc != LDAP_SUCCESS ) break;
359 * Free memory associated with list of supported controls.
362 controls_destroy( void )
364 struct slap_control *sc;
366 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
367 sc = LDAP_SLIST_FIRST(&controls_list);
368 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
370 ch_free( sc->sc_oid );
371 if ( sc->sc_extendedopsbv != NULL ) {
372 ber_bvarray_free( sc->sc_extendedopsbv );
379 * Format the supportedControl attribute of the root DSE,
380 * detailing which controls are supported by the directory
384 controls_root_dse_info( Entry *e )
386 AttributeDescription *ad_supportedControl
387 = slap_schema.si_ad_supportedControl;
388 struct berval vals[2];
389 struct slap_control *sc;
391 vals[1].bv_val = NULL;
394 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
395 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
397 vals[0].bv_val = sc->sc_oid;
398 vals[0].bv_len = strlen( sc->sc_oid );
400 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
409 * Return a list of OIDs and operation masks for supported
410 * controls. Used by SLAPI.
413 get_supported_controls(char ***ctrloidsp,
414 slap_mask_t **ctrlmasks)
419 struct slap_control *sc;
423 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
433 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
434 if ( oids == NULL ) {
435 return LDAP_NO_MEMORY;
437 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
438 if ( masks == NULL ) {
440 return LDAP_NO_MEMORY;
445 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
446 oids[n] = ch_strdup( sc->sc_oid );
447 masks[n] = sc->sc_mask;
460 * Find a control given its OID.
462 static struct slap_control *
463 find_ctrl( const char *oid )
465 struct slap_control *sc;
467 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
468 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
477 slap_find_control_id(
481 struct slap_control *ctrl = find_ctrl( oid );
483 if ( cid ) *cid = ctrl->sc_cid;
486 return LDAP_CONTROL_NOT_FOUND;
490 slap_global_control( Operation *op, const char *oid, int *cid )
492 struct slap_control *ctrl = find_ctrl( oid );
494 if ( ctrl == NULL ) {
495 /* should not be reachable */
496 Debug( LDAP_DEBUG_ANY,
497 "slap_global_control: unrecognized control: %s\n",
499 return LDAP_CONTROL_NOT_FOUND;
502 if ( cid ) *cid = ctrl->sc_cid;
504 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
505 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
506 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
508 return LDAP_COMPARE_TRUE;
512 Debug( LDAP_DEBUG_TRACE,
513 "slap_global_control: unavailable control: %s\n",
517 return LDAP_COMPARE_FALSE;
520 void slap_free_ctrls(
522 LDAPControl **ctrls )
526 for (i=0; ctrls[i]; i++) {
527 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
529 op->o_tmpfree( ctrls, op->o_tmpmemctx );
535 LDAPControl *control,
538 struct slap_control *sc;
540 sc = find_ctrl( control->ldctl_oid );
542 /* recognized control */
544 switch( op->o_tag ) {
546 tagmask = SLAP_CTRL_ADD;
549 tagmask = SLAP_CTRL_BIND;
551 case LDAP_REQ_COMPARE:
552 tagmask = SLAP_CTRL_COMPARE;
554 case LDAP_REQ_DELETE:
555 tagmask = SLAP_CTRL_DELETE;
557 case LDAP_REQ_MODIFY:
558 tagmask = SLAP_CTRL_MODIFY;
560 case LDAP_REQ_RENAME:
561 tagmask = SLAP_CTRL_RENAME;
563 case LDAP_REQ_SEARCH:
564 tagmask = SLAP_CTRL_SEARCH;
566 case LDAP_REQ_UNBIND:
567 tagmask = SLAP_CTRL_UNBIND;
569 case LDAP_REQ_ABANDON:
570 tagmask = SLAP_CTRL_ABANDON;
572 case LDAP_REQ_EXTENDED:
574 assert( op->ore_reqoid.bv_val != NULL );
575 if( sc->sc_extendedopsbv != NULL ) {
577 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
578 if( bvmatch( &op->ore_reqoid,
579 &sc->sc_extendedopsbv[i] ) )
588 *text = "controls internal error";
592 if (( sc->sc_mask & tagmask ) == tagmask ) {
593 /* available extension */
596 if( !sc->sc_parse ) {
597 *text = "not yet implemented";
601 rc = sc->sc_parse( op, rs, control );
603 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
607 } else if( control->ldctl_iscritical ) {
608 /* unavailable CRITICAL control */
609 *text = "critical extension is unavailable";
610 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
612 } else if( control->ldctl_iscritical ) {
613 /* unrecognized CRITICAL control */
614 *text = "critical extension is not recognized";
615 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
630 BerElement *ber = op->o_ber;
633 len = ber_pvt_ber_remaining(ber);
637 rs->sr_err = LDAP_SUCCESS;
641 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
642 if( tag == LBER_ERROR ) {
643 rs->sr_err = SLAPD_DISCONNECT;
644 rs->sr_text = "unexpected data in PDU";
650 Debug( LDAP_DEBUG_TRACE,
651 "=> get_ctrls\n", 0, 0, 0 );
653 if( op->o_protocol < LDAP_VERSION3 ) {
654 rs->sr_err = SLAPD_DISCONNECT;
655 rs->sr_text = "controls require LDAPv3";
659 /* one for first control, one for termination */
660 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
663 if( op->ctrls == NULL ) {
664 rs->sr_err = LDAP_NO_MEMORY;
665 rs->sr_text = "no memory";
670 op->o_ctrls[nctrls] = NULL;
672 /* step through each element */
673 for( tag = ber_first_element( ber, &len, &opaque );
675 tag = ber_next_element( ber, &len, opaque ) )
678 LDAPControl **tctrls;
680 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
681 memset(c, 0, sizeof(LDAPControl));
683 /* allocate pointer space for current controls (nctrls)
684 * + this control + extra NULL
686 tctrls = op->o_tmprealloc( op->o_ctrls,
687 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
690 if( tctrls == NULL ) {
692 ldap_controls_free(op->o_ctrls);
695 rs->sr_err = LDAP_NO_MEMORY;
696 rs->sr_text = "no memory";
700 op->o_ctrls = tctrls;
702 op->o_ctrls[nctrls++] = c;
703 op->o_ctrls[nctrls] = NULL;
705 tag = ber_scanf( ber, "{m" /*}*/, &bv );
706 c->ldctl_oid = bv.bv_val;
708 if( tag == LBER_ERROR ) {
709 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
712 slap_free_ctrls( op, op->o_ctrls );
714 rs->sr_err = SLAPD_DISCONNECT;
715 rs->sr_text = "decoding controls error";
718 } else if( c->ldctl_oid == NULL ) {
719 Debug( LDAP_DEBUG_TRACE,
720 "get_ctrls: conn %lu got emtpy OID.\n",
721 op->o_connid, 0, 0 );
723 slap_free_ctrls( op, op->o_ctrls );
725 rs->sr_err = LDAP_PROTOCOL_ERROR;
726 rs->sr_text = "OID field is empty";
730 tag = ber_peek_tag( ber, &len );
732 if( tag == LBER_BOOLEAN ) {
734 tag = ber_scanf( ber, "b", &crit );
736 if( tag == LBER_ERROR ) {
737 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
739 slap_free_ctrls( op, op->o_ctrls );
741 rs->sr_err = SLAPD_DISCONNECT;
742 rs->sr_text = "decoding controls error";
746 c->ldctl_iscritical = (crit != 0);
747 tag = ber_peek_tag( ber, &len );
750 if( tag == LBER_OCTETSTRING ) {
751 tag = ber_scanf( ber, "m", &c->ldctl_value );
753 if( tag == LBER_ERROR ) {
754 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
755 "%s (%scritical): get value failed.\n",
756 op->o_connid, c->ldctl_oid,
757 c->ldctl_iscritical ? "" : "non" );
758 slap_free_ctrls( op, op->o_ctrls );
760 rs->sr_err = SLAPD_DISCONNECT;
761 rs->sr_text = "decoding controls error";
766 Debug( LDAP_DEBUG_TRACE,
767 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
768 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
770 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
771 if ( rs->sr_err != LDAP_SUCCESS ) {
777 Debug( LDAP_DEBUG_TRACE,
778 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
779 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
781 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
782 if( rs->sr_err == SLAPD_DISCONNECT ) {
783 rs->sr_err = LDAP_PROTOCOL_ERROR;
784 send_ldap_disconnect( op, rs );
785 rs->sr_err = SLAPD_DISCONNECT;
787 send_ldap_result( op, rs );
799 BI_chk_controls fnc )
803 switch ( op->o_ctrlflag[ ctrl ] ) {
804 case SLAP_CONTROL_NONCRITICAL:
805 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
806 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
807 slap_known_controls[ ctrl - 1 ] ) == 0 )
814 rs->sr_err = LDAP_OTHER;
822 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
825 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
826 ( i - j ) * sizeof( LDAPControl * ) );
829 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
833 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
835 Debug( LDAP_DEBUG_ANY, "%s: "
836 "non-critical control \"%s\" not supported; stripped.\n",
837 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
840 case SLAP_CONTROL_IGNORED:
841 case SLAP_CONTROL_NONE:
842 rs->sr_err = SLAP_CB_CONTINUE;
845 case SLAP_CONTROL_CRITICAL:
846 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
850 Debug( LDAP_DEBUG_ANY, "%s: "
851 "critical control \"%s\" not supported.\n",
852 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
856 /* handle all cases! */
863 static int parseDontUseCopy (
868 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
869 rs->sr_text = "dontUseCopy control specified multiple times";
870 return LDAP_PROTOCOL_ERROR;
873 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
874 rs->sr_text = "dontUseCopy control value not absent";
875 return LDAP_PROTOCOL_ERROR;
878 if ( !ctrl->ldctl_iscritical ) {
879 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
880 return LDAP_PROTOCOL_ERROR;
883 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
887 static int parseRelax (
892 if ( op->o_relax != SLAP_CONTROL_NONE ) {
893 rs->sr_text = "relax control specified multiple times";
894 return LDAP_PROTOCOL_ERROR;
897 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
898 rs->sr_text = "relax control value not absent";
899 return LDAP_PROTOCOL_ERROR;
902 op->o_relax = ctrl->ldctl_iscritical
903 ? SLAP_CONTROL_CRITICAL
904 : SLAP_CONTROL_NONCRITICAL;
909 static int parseManageDSAit (
914 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
915 rs->sr_text = "manageDSAit control specified multiple times";
916 return LDAP_PROTOCOL_ERROR;
919 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
920 rs->sr_text = "manageDSAit control value not absent";
921 return LDAP_PROTOCOL_ERROR;
924 op->o_managedsait = ctrl->ldctl_iscritical
925 ? SLAP_CONTROL_CRITICAL
926 : SLAP_CONTROL_NONCRITICAL;
931 static int parseProxyAuthz (
937 struct berval dn = BER_BVNULL;
939 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
940 rs->sr_text = "proxy authorization control specified multiple times";
941 return LDAP_PROTOCOL_ERROR;
944 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
945 rs->sr_text = "proxy authorization control value absent";
946 return LDAP_PROTOCOL_ERROR;
949 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
950 && BER_BVISEMPTY( &op->o_ndn ) )
952 rs->sr_text = "anonymous proxied authorization not allowed";
953 return LDAP_PROXIED_AUTHORIZATION_DENIED;
956 op->o_proxy_authz = ctrl->ldctl_iscritical
957 ? SLAP_CONTROL_CRITICAL
958 : SLAP_CONTROL_NONCRITICAL;
960 Debug( LDAP_DEBUG_ARGS,
961 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
963 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
966 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
967 Debug( LDAP_DEBUG_TRACE,
968 "parseProxyAuthz: conn=%lu anonymous\n",
969 op->o_connid, 0, 0 );
972 if ( !BER_BVISNULL( &op->o_ndn ) ) {
973 op->o_ndn.bv_val[ 0 ] = '\0';
975 op->o_ndn.bv_len = 0;
977 if ( !BER_BVISNULL( &op->o_dn ) ) {
978 op->o_dn.bv_val[ 0 ] = '\0';
985 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
986 NULL, &dn, SLAP_GETDN_AUTHZID );
988 /* FIXME: empty DN in proxyAuthz control should be legal... */
989 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
991 ch_free( dn.bv_val );
993 rs->sr_text = "authzId mapping failed";
994 return LDAP_PROXIED_AUTHORIZATION_DENIED;
997 Debug( LDAP_DEBUG_TRACE,
998 "parseProxyAuthz: conn=%lu \"%s\"\n",
1000 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1002 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1005 ch_free( dn.bv_val );
1006 rs->sr_text = "not authorized to assume identity";
1007 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1010 ch_free( op->o_ndn.bv_val );
1011 ch_free( op->o_dn.bv_val );
1014 * NOTE: since slap_sasl_getdn() returns a normalized dn,
1015 * from now on op->o_dn is normalized
1018 ber_dupbv( &op->o_dn, &dn );
1020 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1021 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1023 return LDAP_SUCCESS;
1026 static int parseNoOp (
1031 if ( op->o_noop != SLAP_CONTROL_NONE ) {
1032 rs->sr_text = "noop control specified multiple times";
1033 return LDAP_PROTOCOL_ERROR;
1036 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1037 rs->sr_text = "noop control value not empty";
1038 return LDAP_PROTOCOL_ERROR;
1041 op->o_noop = ctrl->ldctl_iscritical
1042 ? SLAP_CONTROL_CRITICAL
1043 : SLAP_CONTROL_NONCRITICAL;
1045 return LDAP_SUCCESS;
1048 static int parsePagedResults (
1053 int rc = LDAP_SUCCESS;
1057 struct berval cookie = BER_BVNULL;
1058 PagedResultsState *ps;
1060 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1061 rs->sr_text = "paged results control specified multiple times";
1062 return LDAP_PROTOCOL_ERROR;
1065 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1066 rs->sr_text = "paged results control value is absent";
1067 return LDAP_PROTOCOL_ERROR;
1070 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1071 rs->sr_text = "paged results control value is empty";
1072 return LDAP_PROTOCOL_ERROR;
1075 /* Parse the control value
1076 * realSearchControlValue ::= SEQUENCE {
1077 * size INTEGER (0..maxInt),
1078 * -- requested page size from client
1079 * -- result set size estimate from server
1080 * cookie OCTET STRING
1083 ber = ber_init( &ctrl->ldctl_value );
1084 if ( ber == NULL ) {
1085 rs->sr_text = "internal error";
1089 tag = ber_scanf( ber, "{im}", &size, &cookie );
1091 if ( tag == LBER_ERROR ) {
1092 rs->sr_text = "paged results control could not be decoded";
1093 rc = LDAP_PROTOCOL_ERROR;
1098 rs->sr_text = "paged results control size invalid";
1099 rc = LDAP_PROTOCOL_ERROR;
1103 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1104 *ps = op->o_conn->c_pagedresults_state;
1106 op->o_pagedresults_state = ps;
1108 /* NOTE: according to RFC 2696 3.:
1110 If the page size is greater than or equal to the sizeLimit value, the
1111 server should ignore the control as the request can be satisfied in a
1114 * NOTE: this assumes that the op->ors_slimit be set
1115 * before the controls are parsed.
1118 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1119 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1121 } else if ( ctrl->ldctl_iscritical ) {
1122 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1125 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1129 (void)ber_free( ber, 1 );
1133 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1134 static int parseSortedResults (
1139 int rc = LDAP_SUCCESS;
1141 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1142 rs->sr_text = "sorted results control specified multiple times";
1143 return LDAP_PROTOCOL_ERROR;
1146 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1147 rs->sr_text = "sorted results control value is absent";
1148 return LDAP_PROTOCOL_ERROR;
1151 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1152 rs->sr_text = "sorted results control value is empty";
1153 return LDAP_PROTOCOL_ERROR;
1156 /* blow off parsing the value */
1158 op->o_sortedresults = ctrl->ldctl_iscritical
1159 ? SLAP_CONTROL_CRITICAL
1160 : SLAP_CONTROL_NONCRITICAL;
1166 static int parseAssert (
1172 struct berval fstr = BER_BVNULL;
1174 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1175 rs->sr_text = "assert control specified multiple times";
1176 return LDAP_PROTOCOL_ERROR;
1179 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1180 rs->sr_text = "assert control value is absent";
1181 return LDAP_PROTOCOL_ERROR;
1184 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1185 rs->sr_text = "assert control value is empty";
1186 return LDAP_PROTOCOL_ERROR;
1189 ber = ber_init( &(ctrl->ldctl_value) );
1191 rs->sr_text = "assert control: internal error";
1195 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1197 (void) ber_free( ber, 1 );
1198 if( rs->sr_err != LDAP_SUCCESS ) {
1199 if( rs->sr_err == SLAPD_DISCONNECT ) {
1200 rs->sr_err = LDAP_PROTOCOL_ERROR;
1201 send_ldap_disconnect( op, rs );
1202 rs->sr_err = SLAPD_DISCONNECT;
1204 send_ldap_result( op, rs );
1206 if( op->o_assertion != NULL ) {
1207 filter_free_x( op, op->o_assertion );
1213 filter2bv_x( op, op->o_assertion, &fstr );
1215 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1216 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1217 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1220 op->o_assert = ctrl->ldctl_iscritical
1221 ? SLAP_CONTROL_CRITICAL
1222 : SLAP_CONTROL_NONCRITICAL;
1224 rs->sr_err = LDAP_SUCCESS;
1225 return LDAP_SUCCESS;
1228 static int parsePreRead (
1233 ber_len_t siz, off, i;
1234 AttributeName *an = NULL;
1237 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1238 rs->sr_text = "preread control specified multiple times";
1239 return LDAP_PROTOCOL_ERROR;
1242 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1243 rs->sr_text = "preread control value is absent";
1244 return LDAP_PROTOCOL_ERROR;
1247 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1248 rs->sr_text = "preread control value is empty";
1249 return LDAP_PROTOCOL_ERROR;
1253 if ( op->o_txnSpec ) { /* temporary limitation */
1254 rs->sr_text = "cannot perform pre-read in transaction";
1255 return LDAP_UNWILLING_TO_PERFORM;
1259 ber = ber_init( &(ctrl->ldctl_value) );
1261 rs->sr_text = "preread control: internal error";
1265 rs->sr_err = LDAP_SUCCESS;
1267 siz = sizeof( AttributeName );
1268 off = offsetof( AttributeName, an_name );
1269 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1270 rs->sr_text = "preread control: decoding error";
1271 rs->sr_err = LDAP_PROTOCOL_ERROR;
1275 for( i=0; i<siz; i++ ) {
1276 const char *dummy = NULL;
1278 an[i].an_desc = NULL;
1280 an[i].an_oc_exclude = 0;
1281 rs->sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1282 if ( rs->sr_err != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1285 : "postread control: unknown attributeType";
1290 op->o_preread = ctrl->ldctl_iscritical
1291 ? SLAP_CONTROL_CRITICAL
1292 : SLAP_CONTROL_NONCRITICAL;
1294 op->o_preread_attrs = an;
1297 (void) ber_free( ber, 1 );
1301 static int parsePostRead (
1306 ber_len_t siz, off, i;
1307 AttributeName *an = NULL;
1310 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1311 rs->sr_text = "postread control specified multiple times";
1312 return LDAP_PROTOCOL_ERROR;
1315 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1316 rs->sr_text = "postread control value is absent";
1317 return LDAP_PROTOCOL_ERROR;
1320 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1321 rs->sr_text = "postread control value is empty";
1322 return LDAP_PROTOCOL_ERROR;
1326 if ( op->o_txnSpec ) { /* temporary limitation */
1327 rs->sr_text = "cannot perform post-read in transaction";
1328 return LDAP_UNWILLING_TO_PERFORM;
1332 ber = ber_init( &(ctrl->ldctl_value) );
1334 rs->sr_text = "postread control: internal error";
1338 rs->sr_err = LDAP_SUCCESS;
1339 siz = sizeof( AttributeName );
1340 off = offsetof( AttributeName, an_name );
1341 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1342 rs->sr_text = "postread control: decoding error";
1343 rs->sr_err = LDAP_PROTOCOL_ERROR;
1347 for ( i = 0; i < siz; i++ ) {
1348 const char *dummy = NULL;
1351 an[i].an_desc = NULL;
1353 an[i].an_oc_exclude = 0;
1354 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1355 if ( rc != LDAP_SUCCESS ) {
1357 static struct berval special_attrs[] = {
1358 BER_BVC( LDAP_NO_ATTRS ),
1359 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1360 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1364 /* deal with special attribute types */
1365 for ( i = 0; !BER_BVISNULL( &special_attrs[ i ] ); i++ ) {
1366 if ( bvmatch( &an[i].an_name, &special_attrs[ i ] ) ) {
1371 if ( BER_BVISNULL( &special_attrs[ i ] ) && ctrl->ldctl_iscritical ) {
1375 : "postread control: unknown attributeType";
1381 op->o_postread = ctrl->ldctl_iscritical
1382 ? SLAP_CONTROL_CRITICAL
1383 : SLAP_CONTROL_NONCRITICAL;
1385 op->o_postread_attrs = an;
1388 (void) ber_free( ber, 1 );
1392 static int parseValuesReturnFilter (
1398 struct berval fstr = BER_BVNULL;
1400 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1401 rs->sr_text = "valuesReturnFilter control specified multiple times";
1402 return LDAP_PROTOCOL_ERROR;
1405 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1406 rs->sr_text = "valuesReturnFilter control value is absent";
1407 return LDAP_PROTOCOL_ERROR;
1410 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1411 rs->sr_text = "valuesReturnFilter control value is empty";
1412 return LDAP_PROTOCOL_ERROR;
1415 ber = ber_init( &(ctrl->ldctl_value) );
1417 rs->sr_text = "internal error";
1421 rs->sr_err = get_vrFilter( op, ber,
1422 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1424 (void) ber_free( ber, 1 );
1426 if( rs->sr_err != LDAP_SUCCESS ) {
1427 if( rs->sr_err == SLAPD_DISCONNECT ) {
1428 rs->sr_err = LDAP_PROTOCOL_ERROR;
1429 send_ldap_disconnect( op, rs );
1430 rs->sr_err = SLAPD_DISCONNECT;
1432 send_ldap_result( op, rs );
1434 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1438 vrFilter2bv( op, op->o_vrFilter, &fstr );
1441 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1442 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1443 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1446 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1447 ? SLAP_CONTROL_CRITICAL
1448 : SLAP_CONTROL_NONCRITICAL;
1450 rs->sr_err = LDAP_SUCCESS;
1451 return LDAP_SUCCESS;
1454 static int parseSubentries (
1459 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1460 rs->sr_text = "subentries control specified multiple times";
1461 return LDAP_PROTOCOL_ERROR;
1464 /* FIXME: should use BER library */
1465 if( ( ctrl->ldctl_value.bv_len != 3 )
1466 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1467 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1469 rs->sr_text = "subentries control value encoding is bogus";
1470 return LDAP_PROTOCOL_ERROR;
1473 op->o_subentries = ctrl->ldctl_iscritical
1474 ? SLAP_CONTROL_CRITICAL
1475 : SLAP_CONTROL_NONCRITICAL;
1477 if (ctrl->ldctl_value.bv_val[2]) {
1478 set_subentries_visibility( op );
1481 return LDAP_SUCCESS;
1484 static int parsePermissiveModify (
1489 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1490 rs->sr_text = "permissiveModify control specified multiple times";
1491 return LDAP_PROTOCOL_ERROR;
1494 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1495 rs->sr_text = "permissiveModify control value not absent";
1496 return LDAP_PROTOCOL_ERROR;
1499 op->o_permissive_modify = ctrl->ldctl_iscritical
1500 ? SLAP_CONTROL_CRITICAL
1501 : SLAP_CONTROL_NONCRITICAL;
1503 return LDAP_SUCCESS;
1506 static int parseDomainScope (
1511 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1512 rs->sr_text = "domainScope control specified multiple times";
1513 return LDAP_PROTOCOL_ERROR;
1516 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1517 rs->sr_text = "domainScope control value not empty";
1518 return LDAP_PROTOCOL_ERROR;
1521 op->o_domain_scope = ctrl->ldctl_iscritical
1522 ? SLAP_CONTROL_CRITICAL
1523 : SLAP_CONTROL_NONCRITICAL;
1525 return LDAP_SUCCESS;
1528 #ifdef SLAP_CONTROL_X_TREE_DELETE
1529 static int parseTreeDelete (
1534 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1535 rs->sr_text = "treeDelete control specified multiple times";
1536 return LDAP_PROTOCOL_ERROR;
1539 if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1540 rs->sr_text = "treeDelete control value not absent";
1541 return LDAP_PROTOCOL_ERROR;
1544 op->o_tree_delete = ctrl->ldctl_iscritical
1545 ? SLAP_CONTROL_CRITICAL
1546 : SLAP_CONTROL_NONCRITICAL;
1548 return LDAP_SUCCESS;
1552 static int parseSearchOptions (
1558 ber_int_t search_flags;
1561 if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1562 rs->sr_text = "searchOptions control value is absent";
1563 return LDAP_PROTOCOL_ERROR;
1566 if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1567 rs->sr_text = "searchOptions control value is empty";
1568 return LDAP_PROTOCOL_ERROR;
1571 ber = ber_init( &ctrl->ldctl_value );
1573 rs->sr_text = "internal error";
1577 tag = ber_scanf( ber, "{i}", &search_flags );
1578 (void) ber_free( ber, 1 );
1580 if ( tag == LBER_ERROR ) {
1581 rs->sr_text = "searchOptions control decoding error";
1582 return LDAP_PROTOCOL_ERROR;
1585 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1586 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1587 rs->sr_text = "searchOptions control specified multiple times "
1588 "or with domainScope control";
1589 return LDAP_PROTOCOL_ERROR;
1592 op->o_domain_scope = ctrl->ldctl_iscritical
1593 ? SLAP_CONTROL_CRITICAL
1594 : SLAP_CONTROL_NONCRITICAL;
1597 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1598 /* Other search flags not recognised so far,
1600 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1602 rs->sr_text = "searchOptions contained unrecognized flag";
1603 return LDAP_UNWILLING_TO_PERFORM;
1606 return LDAP_SUCCESS;
1609 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1610 struct berval session_tracking_formats[] = {
1611 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1612 BER_BVC( "RADIUS-Acct-Session-Id" ),
1613 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1614 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1615 BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1616 BER_BVC( "USERNAME" ),
1621 static int parseSessionTracking(
1631 struct berval sessionSourceIp = BER_BVNULL,
1632 sessionSourceName = BER_BVNULL,
1633 formatOID = BER_BVNULL,
1634 sessionTrackingIdentifier = BER_BVNULL;
1636 size_t st_len, st_pos;
1638 if ( ctrl->ldctl_iscritical ) {
1639 rs->sr_text = "sessionTracking criticality is TRUE";
1640 return LDAP_PROTOCOL_ERROR;
1643 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1644 rs->sr_text = "sessionTracking control value is absent";
1645 return LDAP_PROTOCOL_ERROR;
1648 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1649 rs->sr_text = "sessionTracking control value is empty";
1650 return LDAP_PROTOCOL_ERROR;
1653 /* TODO: add the capability to determine if a client is allowed
1654 * to use this control, based on identity, ip and so */
1656 ber = ber_init( &ctrl->ldctl_value );
1657 if ( ber == NULL ) {
1658 rs->sr_text = "internal error";
1662 tag = ber_skip_tag( ber, &len );
1663 if ( tag != LBER_SEQUENCE ) {
1668 /* sessionSourceIp */
1669 tag = ber_peek_tag( ber, &len );
1670 if ( tag == LBER_DEFAULT ) {
1676 tag = ber_skip_tag( ber, &len );
1678 } else if ( len > 128 ) {
1679 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1680 rs->sr_err = LDAP_PROTOCOL_ERROR;
1684 tag = ber_scanf( ber, "m", &sessionSourceIp );
1687 if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1688 BER_BVZERO( &sessionSourceIp );
1691 /* sessionSourceName */
1692 tag = ber_peek_tag( ber, &len );
1693 if ( tag == LBER_DEFAULT ) {
1699 tag = ber_skip_tag( ber, &len );
1701 } else if ( len > 65536 ) {
1702 rs->sr_text = "sessionTracking.sessionSourceName too long";
1703 rs->sr_err = LDAP_PROTOCOL_ERROR;
1707 tag = ber_scanf( ber, "m", &sessionSourceName );
1710 if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1711 BER_BVZERO( &sessionSourceName );
1715 tag = ber_peek_tag( ber, &len );
1716 if ( tag == LBER_DEFAULT ) {
1722 rs->sr_text = "sessionTracking.formatOID empty";
1723 rs->sr_err = LDAP_PROTOCOL_ERROR;
1726 } else if ( len > 1024 ) {
1727 rs->sr_text = "sessionTracking.formatOID too long";
1728 rs->sr_err = LDAP_PROTOCOL_ERROR;
1732 tag = ber_scanf( ber, "m", &formatOID );
1735 rc = numericoidValidate( NULL, &formatOID );
1736 if ( rc != LDAP_SUCCESS ) {
1737 rs->sr_text = "sessionTracking.formatOID invalid";
1741 for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1743 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1744 formatOID = session_tracking_formats[ i + 1 ];
1749 /* sessionTrackingIdentifier */
1750 tag = ber_peek_tag( ber, &len );
1751 if ( tag == LBER_DEFAULT ) {
1757 tag = ber_skip_tag( ber, &len );
1760 /* note: should not be more than 65536... */
1761 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1762 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1763 /* we want the OID printed, at least */
1764 BER_BVSTR( &sessionTrackingIdentifier, "" );
1769 tag = ber_skip_tag( ber, &len );
1770 if ( tag != LBER_DEFAULT || len != 0 ) {
1777 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1778 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1780 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1781 if ( st_len ) st_len++;
1782 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1784 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1785 if ( st_len ) st_len++;
1786 st_len += formatOID.bv_len + STRLENOF( "=" )
1787 + sessionTrackingIdentifier.bv_len;
1790 if ( st_len == 0 ) {
1794 st_len += STRLENOF( " []" );
1795 st_pos = strlen( op->o_log_prefix );
1797 if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1798 char *ptr = &op->o_log_prefix[ st_pos ];
1800 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1803 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1804 ptr = lutil_strcopy( ptr, "IP=" );
1805 ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1809 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1810 if ( st_len ) *ptr++ = ' ';
1811 ptr = lutil_strcopy( ptr, "NAME=" );
1812 ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1816 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1817 if ( st_len ) *ptr++ = ' ';
1818 ptr = lutil_strcopy( ptr, formatOID.bv_val );
1820 ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1828 (void)ber_free( ber, 1 );
1830 if ( tag == LBER_ERROR ) {
1831 rs->sr_text = "sessionTracking control decoding error";
1832 return LDAP_PROTOCOL_ERROR;
1840 slap_ctrl_session_tracking_add(
1844 struct berval *name,
1848 BerElementBuffer berbuf;
1849 BerElement *ber = (BerElement *)&berbuf;
1851 static struct berval oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
1853 assert( ctrl != NULL );
1855 ber_init2( ber, NULL, LBER_USE_DER );
1857 ber_printf( ber, "{OOOO}", ip, name, &oid, id );
1859 if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
1860 rs->sr_err = LDAP_OTHER;
1864 ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
1865 ctrl->ldctl_iscritical = 0;
1867 rs->sr_err = LDAP_SUCCESS;
1874 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
1876 static struct berval bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
1877 struct berval ip = BER_BVNULL,
1881 if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
1882 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1886 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
1887 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
1889 ptr = ber_bvchr( &ip, ':' );
1891 ip.bv_len = ptr - ip.bv_val;
1895 if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
1896 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
1898 name = op->o_conn->c_peer_domain;
1901 if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
1905 return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );