2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>
25 #include "../../libraries/liblber/lber-int.h"
27 static SLAP_CTRL_PARSE_FN parseAssert;
28 static SLAP_CTRL_PARSE_FN parsePreRead;
29 static SLAP_CTRL_PARSE_FN parsePostRead;
30 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
31 static SLAP_CTRL_PARSE_FN parseManageDSAit;
32 static SLAP_CTRL_PARSE_FN parseModifyIncrement;
33 static SLAP_CTRL_PARSE_FN parseNoOp;
34 static SLAP_CTRL_PARSE_FN parsePagedResults;
36 static SLAP_CTRL_PARSE_FN parseSortedResults;
38 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
39 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
40 static SLAP_CTRL_PARSE_FN parseDomainScope;
41 static SLAP_CTRL_PARSE_FN parseTreeDelete;
42 static SLAP_CTRL_PARSE_FN parseSearchOptions;
43 static SLAP_CTRL_PARSE_FN parseSubentries;
45 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
47 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
48 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
50 struct slap_control_ids slap_cids;
56 /* The controlID for this control */
59 /* Operations supported by control */
62 /* Extended operations supported by control */
63 char **sc_extendedops;
65 /* Control parsing callback */
66 SLAP_CTRL_PARSE_FN *sc_parse;
68 LDAP_SLIST_ENTRY(slap_control) sc_next;
71 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
72 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
75 * all known request control OIDs should be added to this list
78 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
79 * addresses an undefined control; this allows to safely test for
80 * well known controls even if they are not registered, e.g. if
81 * they get moved to modules. An example is sc_LDAPsync, which
82 * is implemented in the syncprov overlay and thus, if configured
83 * as dynamic module, may not be registered. One side effect is that
84 * slap_known_controls[0] == NULL, so it should always be used
86 * FIXME: should we define the "undefined control" oid?
88 char *slap_known_controls[SLAP_MAX_CIDS+1];
89 static int num_known_controls = 1;
91 static char *proxy_authz_extops[] = {
92 LDAP_EXOP_MODIFY_PASSWD,
97 static struct slap_control control_defs[] = {
98 { LDAP_CONTROL_ASSERT,
99 (int)offsetof(struct slap_control_ids, sc_assert),
100 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
101 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
102 { LDAP_CONTROL_PRE_READ,
103 (int)offsetof(struct slap_control_ids, sc_preRead),
104 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
105 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
106 { LDAP_CONTROL_POST_READ,
107 (int)offsetof(struct slap_control_ids, sc_postRead),
108 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
109 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
110 { LDAP_CONTROL_VALUESRETURNFILTER,
111 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
112 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
113 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
114 { LDAP_CONTROL_PAGEDRESULTS,
115 (int)offsetof(struct slap_control_ids, sc_pagedResults),
116 SLAP_CTRL_SEARCH, NULL,
117 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
119 { LDAP_CONTROL_SORTREQUEST,
120 (int)offsetof(struct slap_control_ids, sc_sortedResults),
121 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE, NULL,
122 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
124 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
125 { LDAP_CONTROL_X_DOMAIN_SCOPE,
126 (int)offsetof(struct slap_control_ids, sc_domainScope),
127 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
128 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
130 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
131 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
132 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
133 SLAP_CTRL_MODIFY, NULL,
134 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
136 #ifdef SLAP_CONTROL_X_TREE_DELETE
137 { LDAP_CONTROL_X_TREE_DELETE,
138 (int)offsetof(struct slap_control_ids, sc_treeDelete),
139 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
140 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
142 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
143 { LDAP_CONTORL_X_SEARCH_OPTIONS,
144 (int)offsetof(struct slap_control_ids, sc_searchOptions),
145 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
146 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
148 #ifdef LDAP_CONTROL_SUBENTRIES
149 { LDAP_CONTROL_SUBENTRIES,
150 (int)offsetof(struct slap_control_ids, sc_subentries),
151 SLAP_CTRL_SEARCH, NULL,
152 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
155 (int)offsetof(struct slap_control_ids, sc_noOp),
156 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
157 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
158 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
159 { LDAP_CONTROL_MODIFY_INCREMENT,
160 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
161 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
162 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
164 { LDAP_CONTROL_MANAGEDSAIT,
165 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
166 SLAP_CTRL_ACCESS, NULL,
167 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
168 { LDAP_CONTROL_PROXY_AUTHZ,
169 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
170 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS, proxy_authz_extops,
171 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
172 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
176 * Register a supported control.
178 * This can be called by an OpenLDAP plugin or, indirectly, by a
179 * SLAPI plugin calling slapi_register_supported_control().
182 register_supported_control(const char *controloid,
183 slap_mask_t controlmask,
185 SLAP_CTRL_PARSE_FN *controlparsefn,
188 struct slap_control *sc;
191 if ( num_known_controls >= SLAP_MAX_CIDS ) {
192 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
193 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
194 SLAP_MAX_CIDS, 0, 0 );
198 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
200 /* sanity check - should never happen */
201 for ( i = 0; slap_known_controls[ i ]; i++ ) {
202 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
203 Debug( LDAP_DEBUG_ANY,
204 "Control %s already registered.\n",
206 return LDAP_PARAM_ERROR;
210 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
211 if ( sc == NULL ) return LDAP_NO_MEMORY;
213 sc->sc_oid = ch_strdup( controloid );
214 sc->sc_mask = controlmask;
215 if ( controlexops != NULL ) {
216 sc->sc_extendedops = ldap_charray_dup( controlexops );
217 if ( sc->sc_extendedops == NULL ) {
219 return LDAP_NO_MEMORY;
222 sc->sc_extendedops = NULL;
224 sc->sc_parse = controlparsefn;
226 if ( controlcid ) *controlcid = num_known_controls;
227 sc->sc_cid = num_known_controls;
229 /* Update slap_known_controls, too. */
230 slap_known_controls[num_known_controls-1] = sc->sc_oid;
231 slap_known_controls[num_known_controls++] = NULL;
233 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
234 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
239 * One-time initialization of internal controls.
242 slap_controls_init( void )
245 struct slap_control *sc;
249 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
250 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
251 rc = register_supported_control( control_defs[i].sc_oid,
252 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
253 control_defs[i].sc_parse, cid );
254 if ( rc != LDAP_SUCCESS ) break;
261 * Free memory associated with list of supported controls.
264 controls_destroy( void )
266 struct slap_control *sc;
268 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
269 sc = LDAP_SLIST_FIRST(&controls_list);
270 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
272 ch_free( sc->sc_oid );
273 if ( sc->sc_extendedops != NULL ) {
274 ldap_charray_free( sc->sc_extendedops );
281 * Format the supportedControl attribute of the root DSE,
282 * detailing which controls are supported by the directory
286 controls_root_dse_info( Entry *e )
288 AttributeDescription *ad_supportedControl
289 = slap_schema.si_ad_supportedControl;
290 struct berval vals[2];
291 struct slap_control *sc;
293 vals[1].bv_val = NULL;
296 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
297 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
299 vals[0].bv_val = sc->sc_oid;
300 vals[0].bv_len = strlen( sc->sc_oid );
302 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
311 * Return a list of OIDs and operation masks for supported
312 * controls. Used by SLAPI.
315 get_supported_controls(char ***ctrloidsp,
316 slap_mask_t **ctrlmasks)
322 struct slap_control *sc;
326 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
336 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
337 if ( oids == NULL ) {
338 return LDAP_NO_MEMORY;
340 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
341 if ( masks == NULL ) {
343 return LDAP_NO_MEMORY;
348 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
349 oids[n] = ch_strdup( sc->sc_oid );
350 masks[n] = sc->sc_mask;
363 * Find a control given its OID.
365 static struct slap_control *
366 find_ctrl( const char *oid )
368 struct slap_control *sc;
370 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
371 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
380 slap_find_control_id(
384 struct slap_control *ctrl = find_ctrl( oid );
386 if ( cid ) *cid = ctrl->sc_cid;
389 return LDAP_CONTROL_NOT_FOUND;
393 slap_global_control( Operation *op, const char *oid, int *cid )
395 struct slap_control *ctrl = find_ctrl( oid );
397 if ( ctrl == NULL ) {
398 /* should not be reachable */
399 Debug( LDAP_DEBUG_ANY,
400 "slap_global_control: unrecognized control: %s\n",
402 return LDAP_CONTROL_NOT_FOUND;
405 if ( cid ) *cid = ctrl->sc_cid;
407 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
408 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
409 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
411 return LDAP_COMPARE_TRUE;
414 Debug( LDAP_DEBUG_TRACE,
415 "slap_global_control: unavailable control: %s\n",
418 return LDAP_COMPARE_FALSE;
421 void slap_free_ctrls(
423 LDAPControl **ctrls )
427 for (i=0; ctrls[i]; i++) {
428 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
430 op->o_tmpfree( ctrls, op->o_tmpmemctx );
442 BerElement *ber = op->o_ber;
443 struct slap_control *sc;
446 len = ber_pvt_ber_remaining(ber);
450 rs->sr_err = LDAP_SUCCESS;
454 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
455 if( tag == LBER_ERROR ) {
456 rs->sr_err = SLAPD_DISCONNECT;
457 rs->sr_text = "unexpected data in PDU";
463 Debug( LDAP_DEBUG_TRACE,
464 "=> get_ctrls\n", 0, 0, 0 );
466 if( op->o_protocol < LDAP_VERSION3 ) {
467 rs->sr_err = SLAPD_DISCONNECT;
468 rs->sr_text = "controls require LDAPv3";
472 /* one for first control, one for termination */
473 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
476 if( op->ctrls == NULL ) {
477 rs->sr_err = LDAP_NO_MEMORY;
478 rs->sr_text = "no memory";
483 op->o_ctrls[nctrls] = NULL;
485 /* step through each element */
486 for( tag = ber_first_element( ber, &len, &opaque );
488 tag = ber_next_element( ber, &len, opaque ) )
491 LDAPControl **tctrls;
493 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
494 memset(c, 0, sizeof(LDAPControl));
496 /* allocate pointer space for current controls (nctrls)
497 * + this control + extra NULL
499 tctrls = op->o_tmprealloc( op->o_ctrls,
500 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
503 if( tctrls == NULL ) {
505 ldap_controls_free(op->o_ctrls);
508 rs->sr_err = LDAP_NO_MEMORY;
509 rs->sr_text = "no memory";
513 op->o_ctrls = tctrls;
515 op->o_ctrls[nctrls++] = c;
516 op->o_ctrls[nctrls] = NULL;
518 tag = ber_scanf( ber, "{m" /*}*/, &bv );
519 c->ldctl_oid = bv.bv_val;
521 if( tag == LBER_ERROR ) {
522 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
525 slap_free_ctrls( op, op->o_ctrls );
527 rs->sr_err = SLAPD_DISCONNECT;
528 rs->sr_text = "decoding controls error";
531 } else if( c->ldctl_oid == NULL ) {
532 Debug( LDAP_DEBUG_TRACE,
533 "get_ctrls: conn %lu got emtpy OID.\n",
534 op->o_connid, 0, 0 );
536 slap_free_ctrls( op, op->o_ctrls );
538 rs->sr_err = LDAP_PROTOCOL_ERROR;
539 rs->sr_text = "OID field is empty";
543 tag = ber_peek_tag( ber, &len );
545 if( tag == LBER_BOOLEAN ) {
547 tag = ber_scanf( ber, "b", &crit );
549 if( tag == LBER_ERROR ) {
550 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
552 slap_free_ctrls( op, op->o_ctrls );
554 rs->sr_err = SLAPD_DISCONNECT;
555 rs->sr_text = "decoding controls error";
559 c->ldctl_iscritical = (crit != 0);
560 tag = ber_peek_tag( ber, &len );
563 if( tag == LBER_OCTETSTRING ) {
564 tag = ber_scanf( ber, "m", &c->ldctl_value );
566 if( tag == LBER_ERROR ) {
567 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
568 "%s (%scritical): get value failed.\n",
569 op->o_connid, c->ldctl_oid,
570 c->ldctl_iscritical ? "" : "non" );
571 slap_free_ctrls( op, op->o_ctrls );
573 rs->sr_err = SLAPD_DISCONNECT;
574 rs->sr_text = "decoding controls error";
579 Debug( LDAP_DEBUG_TRACE,
580 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
581 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
583 sc = find_ctrl( c->ldctl_oid );
585 /* recognized control */
587 switch( op->o_tag ) {
589 tagmask = SLAP_CTRL_ADD;
592 tagmask = SLAP_CTRL_BIND;
594 case LDAP_REQ_COMPARE:
595 tagmask = SLAP_CTRL_COMPARE;
597 case LDAP_REQ_DELETE:
598 tagmask = SLAP_CTRL_DELETE;
600 case LDAP_REQ_MODIFY:
601 tagmask = SLAP_CTRL_MODIFY;
603 case LDAP_REQ_RENAME:
604 tagmask = SLAP_CTRL_RENAME;
606 case LDAP_REQ_SEARCH:
607 tagmask = SLAP_CTRL_SEARCH;
609 case LDAP_REQ_UNBIND:
610 tagmask = SLAP_CTRL_UNBIND;
612 case LDAP_REQ_ABANDON:
613 tagmask = SLAP_CTRL_ABANDON;
615 case LDAP_REQ_EXTENDED:
617 assert( op->ore_reqoid.bv_val != NULL );
618 if( sc->sc_extendedops != NULL ) {
620 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
621 if( strcmp( op->ore_reqoid.bv_val,
622 sc->sc_extendedops[i] ) == 0 )
631 rs->sr_err = LDAP_OTHER;
632 rs->sr_text = "controls internal error";
636 if (( sc->sc_mask & tagmask ) == tagmask ) {
637 /* available extension */
640 if( !sc->sc_parse ) {
641 rs->sr_err = LDAP_OTHER;
642 rs->sr_text = "not yet implemented";
646 rc = sc->sc_parse( op, rs, c );
648 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
653 } else if( c->ldctl_iscritical ) {
654 /* unavailable CRITICAL control */
655 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
656 rs->sr_text = "critical extension is unavailable";
660 } else if( c->ldctl_iscritical ) {
661 /* unrecognized CRITICAL control */
662 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
663 rs->sr_text = "critical extension is not recognized";
670 Debug( LDAP_DEBUG_TRACE,
671 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
672 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
674 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
675 if( rs->sr_err == SLAPD_DISCONNECT ) {
676 rs->sr_err = LDAP_PROTOCOL_ERROR;
677 send_ldap_disconnect( op, rs );
678 rs->sr_err = SLAPD_DISCONNECT;
680 send_ldap_result( op, rs );
687 static int parseModifyIncrement (
693 if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
694 rs->sr_text = "modifyIncrement control specified multiple times";
695 return LDAP_PROTOCOL_ERROR;
699 if ( ctrl->ldctl_value.bv_len ) {
700 rs->sr_text = "modifyIncrement control value not empty";
701 return LDAP_PROTOCOL_ERROR;
705 op->o_modifyIncrement = ctrl->ldctl_iscritical
706 ? SLAP_CONTROL_CRITICAL
707 : SLAP_CONTROL_NONCRITICAL;
713 static int parseManageDSAit (
718 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
719 rs->sr_text = "manageDSAit control specified multiple times";
720 return LDAP_PROTOCOL_ERROR;
723 if ( ctrl->ldctl_value.bv_len ) {
724 rs->sr_text = "manageDSAit control value not empty";
725 return LDAP_PROTOCOL_ERROR;
728 op->o_managedsait = ctrl->ldctl_iscritical
729 ? SLAP_CONTROL_CRITICAL
730 : SLAP_CONTROL_NONCRITICAL;
735 static int parseProxyAuthz (
741 struct berval dn = BER_BVNULL;
743 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
744 rs->sr_text = "proxy authorization control specified multiple times";
745 return LDAP_PROTOCOL_ERROR;
748 op->o_proxy_authz = ctrl->ldctl_iscritical
749 ? SLAP_CONTROL_CRITICAL
750 : SLAP_CONTROL_NONCRITICAL;
752 Debug( LDAP_DEBUG_ARGS,
753 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
755 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
758 if ( ctrl->ldctl_value.bv_len == 0 ) {
759 Debug( LDAP_DEBUG_TRACE,
760 "parseProxyAuthz: conn=%lu anonymous\n",
761 op->o_connid, 0, 0 );
764 op->o_ndn.bv_val[ 0 ] = '\0';
765 op->o_ndn.bv_len = 0;
767 op->o_dn.bv_val[ 0 ] = '\0';
773 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
774 NULL, &dn, SLAP_GETDN_AUTHZID );
776 /* FIXME: empty DN in proxyAuthz control should be legal... */
777 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
779 ch_free( dn.bv_val );
781 rs->sr_text = "authzId mapping failed";
782 return LDAP_PROXY_AUTHZ_FAILURE;
785 Debug( LDAP_DEBUG_TRACE,
786 "parseProxyAuthz: conn=%lu \"%s\"\n",
788 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
790 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
793 ch_free( dn.bv_val );
794 rs->sr_text = "not authorized to assume identity";
795 return LDAP_PROXY_AUTHZ_FAILURE;
798 ch_free( op->o_ndn.bv_val );
799 ch_free( op->o_dn.bv_val );
802 * NOTE: since slap_sasl_getdn() returns a normalized dn,
803 * from now on op->o_dn is normalized
806 ber_dupbv( &op->o_dn, &dn );
809 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
810 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
815 static int parseNoOp (
820 if ( op->o_noop != SLAP_CONTROL_NONE ) {
821 rs->sr_text = "noop control specified multiple times";
822 return LDAP_PROTOCOL_ERROR;
825 if ( ctrl->ldctl_value.bv_len ) {
826 rs->sr_text = "noop control value not empty";
827 return LDAP_PROTOCOL_ERROR;
830 op->o_noop = ctrl->ldctl_iscritical
831 ? SLAP_CONTROL_CRITICAL
832 : SLAP_CONTROL_NONCRITICAL;
837 static int parsePagedResults (
842 int rc = LDAP_SUCCESS;
846 struct berval cookie = BER_BVNULL;
847 PagedResultsState *ps;
849 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
850 rs->sr_text = "paged results control specified multiple times";
851 return LDAP_PROTOCOL_ERROR;
854 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
855 rs->sr_text = "paged results control value is empty (or absent)";
856 return LDAP_PROTOCOL_ERROR;
859 /* Parse the control value
860 * realSearchControlValue ::= SEQUENCE {
861 * size INTEGER (0..maxInt),
862 * -- requested page size from client
863 * -- result set size estimate from server
864 * cookie OCTET STRING
867 ber = ber_init( &ctrl->ldctl_value );
869 rs->sr_text = "internal error";
873 tag = ber_scanf( ber, "{im}", &size, &cookie );
875 if ( tag == LBER_ERROR ) {
876 rs->sr_text = "paged results control could not be decoded";
877 rc = LDAP_PROTOCOL_ERROR;
882 rs->sr_text = "paged results control size invalid";
883 rc = LDAP_PROTOCOL_ERROR;
888 /* defer cookie decoding/checks to backend... */
889 if ( cookie.bv_len ) {
890 PagedResultsCookie reqcookie;
891 if( cookie.bv_len != sizeof( reqcookie ) ) {
893 rs->sr_text = "paged results cookie is invalid";
894 rc = LDAP_PROTOCOL_ERROR;
898 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
900 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
902 rs->sr_text = "paged results cookie is invalid";
903 rc = LDAP_PROTOCOL_ERROR;
906 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
907 rs->sr_text = "paged results cookie is invalid or old";
908 rc = LDAP_UNWILLING_TO_PERFORM;
913 /* Initial request. Initialize state. */
915 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
916 /* There's another pagedResults control on the
917 * same connection; reject new pagedResults controls
918 * (allowed by RFC2696) */
919 rs->sr_text = "paged results cookie unavailable; try later";
920 rc = LDAP_UNWILLING_TO_PERFORM;
924 op->o_pagedresults_state.ps_cookie = 0;
925 op->o_pagedresults_state.ps_count = 0;
929 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
930 *ps = op->o_conn->c_pagedresults_state;
932 op->o_pagedresults_state = ps;
934 /* NOTE: according to RFC 2696 3.:
936 If the page size is greater than or equal to the sizeLimit value, the
937 server should ignore the control as the request can be satisfied in a
940 * NOTE: this assumes that the op->ors_slimit be set
941 * before the controls are parsed.
944 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
945 op->o_pagedresults = SLAP_CONTROL_IGNORED;
947 } else if ( ctrl->ldctl_iscritical ) {
948 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
951 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
955 (void)ber_free( ber, 1 );
960 static int parseSortedResults (
965 int rc = LDAP_SUCCESS;
967 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
968 rs->sr_text = "sorted results control specified multiple times";
969 return LDAP_PROTOCOL_ERROR;
972 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
973 rs->sr_text = "sorted results control value is empty (or absent)";
974 return LDAP_PROTOCOL_ERROR;
977 /* blow off parsing the value */
979 op->o_sortedresults = ctrl->ldctl_iscritical
980 ? SLAP_CONTROL_CRITICAL
981 : SLAP_CONTROL_NONCRITICAL;
987 static int parseAssert (
993 struct berval fstr = BER_BVNULL;
994 const char *err_msg = "";
996 if ( op->o_assert != SLAP_CONTROL_NONE ) {
997 rs->sr_text = "assert control specified multiple times";
998 return LDAP_PROTOCOL_ERROR;
1001 if ( ctrl->ldctl_value.bv_len == 0 ) {
1002 rs->sr_text = "assert control value is empty (or absent)";
1003 return LDAP_PROTOCOL_ERROR;
1006 ber = ber_init( &(ctrl->ldctl_value) );
1008 rs->sr_text = "assert control: internal error";
1012 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
1014 if( rs->sr_err != LDAP_SUCCESS ) {
1015 if( rs->sr_err == SLAPD_DISCONNECT ) {
1016 rs->sr_err = LDAP_PROTOCOL_ERROR;
1017 send_ldap_disconnect( op, rs );
1018 rs->sr_err = SLAPD_DISCONNECT;
1020 send_ldap_result( op, rs );
1022 if( op->o_assertion != NULL ) {
1023 filter_free_x( op, op->o_assertion );
1029 filter2bv_x( op, op->o_assertion, &fstr );
1031 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1032 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1033 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1036 op->o_assert = ctrl->ldctl_iscritical
1037 ? SLAP_CONTROL_CRITICAL
1038 : SLAP_CONTROL_NONCRITICAL;
1040 rs->sr_err = LDAP_SUCCESS;
1041 return LDAP_SUCCESS;
1044 static int parsePreRead (
1049 ber_len_t siz, off, i;
1050 AttributeName *an = NULL;
1053 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1054 rs->sr_text = "preread control specified multiple times";
1055 return LDAP_PROTOCOL_ERROR;
1058 if ( ctrl->ldctl_value.bv_len == 0 ) {
1059 rs->sr_text = "preread control value is empty (or absent)";
1060 return LDAP_PROTOCOL_ERROR;
1063 ber = ber_init( &(ctrl->ldctl_value) );
1065 rs->sr_text = "preread control: internal error";
1069 siz = sizeof( AttributeName );
1070 off = offsetof( AttributeName, an_name );
1071 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1072 rs->sr_text = "preread control: decoding error";
1073 return LDAP_PROTOCOL_ERROR;
1076 for( i=0; i<siz; i++ ) {
1077 int rc = LDAP_SUCCESS;
1078 const char *dummy = NULL;
1080 an[i].an_desc = NULL;
1082 an[i].an_oc_exclude = 0;
1083 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1084 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1087 : "postread control: unknown attributeType";
1092 op->o_preread = ctrl->ldctl_iscritical
1093 ? SLAP_CONTROL_CRITICAL
1094 : SLAP_CONTROL_NONCRITICAL;
1096 op->o_preread_attrs = an;
1098 rs->sr_err = LDAP_SUCCESS;
1099 return LDAP_SUCCESS;
1102 static int parsePostRead (
1107 ber_len_t siz, off, i;
1108 AttributeName *an = NULL;
1111 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1112 rs->sr_text = "postread control specified multiple times";
1113 return LDAP_PROTOCOL_ERROR;
1116 if ( ctrl->ldctl_value.bv_len == 0 ) {
1117 rs->sr_text = "postread control value is empty (or absent)";
1118 return LDAP_PROTOCOL_ERROR;
1121 ber = ber_init( &(ctrl->ldctl_value) );
1123 rs->sr_text = "postread control: internal error";
1127 siz = sizeof( AttributeName );
1128 off = offsetof( AttributeName, an_name );
1129 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1130 rs->sr_text = "postread control: decoding error";
1131 return LDAP_PROTOCOL_ERROR;
1134 for( i=0; i<siz; i++ ) {
1135 int rc = LDAP_SUCCESS;
1136 const char *dummy = NULL;
1138 an[i].an_desc = NULL;
1140 an[i].an_oc_exclude = 0;
1141 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1142 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1145 : "postread control: unknown attributeType";
1150 op->o_postread = ctrl->ldctl_iscritical
1151 ? SLAP_CONTROL_CRITICAL
1152 : SLAP_CONTROL_NONCRITICAL;
1154 op->o_postread_attrs = an;
1156 rs->sr_err = LDAP_SUCCESS;
1157 return LDAP_SUCCESS;
1160 int parseValuesReturnFilter (
1166 struct berval fstr = BER_BVNULL;
1167 const char *err_msg = "";
1169 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1170 rs->sr_text = "valuesReturnFilter control specified multiple times";
1171 return LDAP_PROTOCOL_ERROR;
1174 if ( ctrl->ldctl_value.bv_len == 0 ) {
1175 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1176 return LDAP_PROTOCOL_ERROR;
1179 ber = ber_init( &(ctrl->ldctl_value) );
1181 rs->sr_text = "internal error";
1185 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1187 if( rs->sr_err != LDAP_SUCCESS ) {
1188 if( rs->sr_err == SLAPD_DISCONNECT ) {
1189 rs->sr_err = LDAP_PROTOCOL_ERROR;
1190 send_ldap_disconnect( op, rs );
1191 rs->sr_err = SLAPD_DISCONNECT;
1193 send_ldap_result( op, rs );
1195 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1199 vrFilter2bv( op, op->o_vrFilter, &fstr );
1202 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1203 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1204 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1207 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1208 ? SLAP_CONTROL_CRITICAL
1209 : SLAP_CONTROL_NONCRITICAL;
1211 rs->sr_err = LDAP_SUCCESS;
1212 return LDAP_SUCCESS;
1215 #ifdef LDAP_CONTROL_SUBENTRIES
1216 static int parseSubentries (
1221 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1222 rs->sr_text = "subentries control specified multiple times";
1223 return LDAP_PROTOCOL_ERROR;
1226 /* FIXME: should use BER library */
1227 if( ( ctrl->ldctl_value.bv_len != 3 )
1228 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1229 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1231 rs->sr_text = "subentries control value encoding is bogus";
1232 return LDAP_PROTOCOL_ERROR;
1235 op->o_subentries = ctrl->ldctl_iscritical
1236 ? SLAP_CONTROL_CRITICAL
1237 : SLAP_CONTROL_NONCRITICAL;
1239 if ( (void *)(ctrl->ldctl_value.bv_val[2] != 0x00)) {
1240 set_subentries_visibility( op );
1243 return LDAP_SUCCESS;
1247 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1248 static int parsePermissiveModify (
1253 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1254 rs->sr_text = "permissiveModify control specified multiple times";
1255 return LDAP_PROTOCOL_ERROR;
1258 if ( ctrl->ldctl_value.bv_len ) {
1259 rs->sr_text = "permissiveModify control value not empty";
1260 return LDAP_PROTOCOL_ERROR;
1263 op->o_permissive_modify = ctrl->ldctl_iscritical
1264 ? SLAP_CONTROL_CRITICAL
1265 : SLAP_CONTROL_NONCRITICAL;
1267 return LDAP_SUCCESS;
1271 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1272 static int parseDomainScope (
1277 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1278 rs->sr_text = "domainScope control specified multiple times";
1279 return LDAP_PROTOCOL_ERROR;
1282 if ( ctrl->ldctl_value.bv_len ) {
1283 rs->sr_text = "domainScope control value not empty";
1284 return LDAP_PROTOCOL_ERROR;
1287 op->o_domain_scope = ctrl->ldctl_iscritical
1288 ? SLAP_CONTROL_CRITICAL
1289 : SLAP_CONTROL_NONCRITICAL;
1291 return LDAP_SUCCESS;
1295 #ifdef LDAP_CONTROL_X_TREE_DELETE
1296 static int parseTreeDelete (
1301 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1302 rs->sr_text = "treeDelete control specified multiple times";
1303 return LDAP_PROTOCOL_ERROR;
1306 if ( ctrl->ldctl_value.bv_len ) {
1307 rs->sr_text = "treeDelete control value not empty";
1308 return LDAP_PROTOCOL_ERROR;
1311 op->o_tree_delete = ctrl->ldctl_iscritical
1312 ? SLAP_CONTROL_CRITICAL
1313 : SLAP_CONTROL_NONCRITICAL;
1315 return LDAP_SUCCESS;
1319 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1320 static int parseSearchOptions (
1326 ber_int_t search_flags;
1328 if ( ctrl->ldctl_value.bv_len == 0 ) {
1329 rs->sr_text = "searchOptions control value not empty";
1330 return LDAP_PROTOCOL_ERROR;
1333 ber = ber_init( &ctrl->ldctl_value );
1335 rs->sr_text = "internal error";
1339 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1340 rs->sr_text = "searchOptions control decoding error";
1341 return LDAP_PROTOCOL_ERROR;
1344 (void) ber_free( ber, 1 );
1346 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1347 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1348 rs->sr_text = "searchOptions control specified multiple times "
1349 "or with domainScope control";
1350 return LDAP_PROTOCOL_ERROR;
1353 op->o_domain_scope = ctrl->ldctl_iscritical
1354 ? SLAP_CONTROL_CRITICAL
1355 : SLAP_CONTROL_NONCRITICAL;
1358 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1359 /* Other search flags not recognised so far,
1361 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1363 rs->sr_text = "searchOptions contained unrecongized flag";
1364 return LDAP_UNWILLING_TO_PERFORM;
1367 return LDAP_SUCCESS;