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;
32 static SLAP_CTRL_PARSE_FN parseManageDIT;
34 static SLAP_CTRL_PARSE_FN parseManageDSAit;
35 static SLAP_CTRL_PARSE_FN parseNoOp;
36 static SLAP_CTRL_PARSE_FN parsePagedResults;
38 static SLAP_CTRL_PARSE_FN parseSortedResults;
40 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
41 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
42 static SLAP_CTRL_PARSE_FN parseDomainScope;
43 #ifdef SLAP_CONTROL_X_TREE_DELETE
44 static SLAP_CTRL_PARSE_FN parseTreeDelete;
46 static SLAP_CTRL_PARSE_FN parseSearchOptions;
47 static SLAP_CTRL_PARSE_FN parseSubentries;
49 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
51 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
52 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
54 struct slap_control_ids slap_cids;
60 /* The controlID for this control */
63 /* Operations supported by control */
66 /* Extended operations supported by control */
67 char **sc_extendedops;
69 /* Control parsing callback */
70 SLAP_CTRL_PARSE_FN *sc_parse;
72 LDAP_SLIST_ENTRY(slap_control) sc_next;
75 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
76 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
79 * all known request control OIDs should be added to this list
82 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
83 * addresses an undefined control; this allows to safely test for
84 * well known controls even if they are not registered, e.g. if
85 * they get moved to modules. An example is sc_LDAPsync, which
86 * is implemented in the syncprov overlay and thus, if configured
87 * as dynamic module, may not be registered. One side effect is that
88 * slap_known_controls[0] == NULL, so it should always be used
90 * FIXME: should we define the "undefined control" oid?
92 char *slap_known_controls[SLAP_MAX_CIDS+1];
93 static int num_known_controls = 1;
95 static char *proxy_authz_extops[] = {
96 LDAP_EXOP_MODIFY_PASSWD,
101 static struct slap_control control_defs[] = {
102 { LDAP_CONTROL_ASSERT,
103 (int)offsetof(struct slap_control_ids, sc_assert),
104 SLAP_CTRL_ACCESS, NULL,
105 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
106 { LDAP_CONTROL_PRE_READ,
107 (int)offsetof(struct slap_control_ids, sc_preRead),
108 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
109 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
110 { LDAP_CONTROL_POST_READ,
111 (int)offsetof(struct slap_control_ids, sc_postRead),
112 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
113 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
114 { LDAP_CONTROL_VALUESRETURNFILTER,
115 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
116 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
117 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
118 { LDAP_CONTROL_PAGEDRESULTS,
119 (int)offsetof(struct slap_control_ids, sc_pagedResults),
120 SLAP_CTRL_SEARCH, NULL,
121 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
123 { LDAP_CONTROL_SORTREQUEST,
124 (int)offsetof(struct slap_control_ids, sc_sortedResults),
125 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE, NULL,
126 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
128 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
129 { LDAP_CONTROL_X_DOMAIN_SCOPE,
130 (int)offsetof(struct slap_control_ids, sc_domainScope),
131 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
132 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
134 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
135 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
136 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
137 SLAP_CTRL_MODIFY, NULL,
138 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
140 #ifdef SLAP_CONTROL_X_TREE_DELETE
141 { LDAP_CONTROL_X_TREE_DELETE,
142 (int)offsetof(struct slap_control_ids, sc_treeDelete),
143 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
144 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
146 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
147 { LDAP_CONTROL_X_SEARCH_OPTIONS,
148 (int)offsetof(struct slap_control_ids, sc_searchOptions),
149 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
150 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
152 #ifdef LDAP_CONTROL_SUBENTRIES
153 { LDAP_CONTROL_SUBENTRIES,
154 (int)offsetof(struct slap_control_ids, sc_subentries),
155 SLAP_CTRL_SEARCH, NULL,
156 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
159 (int)offsetof(struct slap_control_ids, sc_noOp),
160 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
161 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
163 { LDAP_CONTROL_MANAGEDIT,
164 (int)offsetof(struct slap_control_ids, sc_manageDIT),
165 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE, NULL,
166 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
168 { LDAP_CONTROL_MANAGEDSAIT,
169 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
170 SLAP_CTRL_ACCESS, NULL,
171 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
172 { LDAP_CONTROL_PROXY_AUTHZ,
173 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
174 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS, proxy_authz_extops,
175 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
176 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
180 * Register a supported control.
182 * This can be called by an OpenLDAP plugin or, indirectly, by a
183 * SLAPI plugin calling slapi_register_supported_control().
186 register_supported_control(const char *controloid,
187 slap_mask_t controlmask,
189 SLAP_CTRL_PARSE_FN *controlparsefn,
192 struct slap_control *sc;
195 if ( num_known_controls >= SLAP_MAX_CIDS ) {
196 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
197 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
198 SLAP_MAX_CIDS, 0, 0 );
202 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
204 /* sanity check - should never happen */
205 for ( i = 0; slap_known_controls[ i ]; i++ ) {
206 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
207 Debug( LDAP_DEBUG_ANY,
208 "Control %s already registered.\n",
210 return LDAP_PARAM_ERROR;
214 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
215 if ( sc == NULL ) return LDAP_NO_MEMORY;
217 sc->sc_oid = ch_strdup( controloid );
218 sc->sc_mask = controlmask;
219 if ( controlexops != NULL ) {
220 sc->sc_extendedops = ldap_charray_dup( controlexops );
221 if ( sc->sc_extendedops == NULL ) {
223 return LDAP_NO_MEMORY;
226 sc->sc_extendedops = NULL;
228 sc->sc_parse = controlparsefn;
230 if ( controlcid ) *controlcid = num_known_controls;
231 sc->sc_cid = num_known_controls;
233 /* Update slap_known_controls, too. */
234 slap_known_controls[num_known_controls-1] = sc->sc_oid;
235 slap_known_controls[num_known_controls++] = NULL;
237 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
238 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
243 * One-time initialization of internal controls.
246 slap_controls_init( void )
252 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
253 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
254 rc = register_supported_control( control_defs[i].sc_oid,
255 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
256 control_defs[i].sc_parse, cid );
257 if ( rc != LDAP_SUCCESS ) break;
264 * Free memory associated with list of supported controls.
267 controls_destroy( void )
269 struct slap_control *sc;
271 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
272 sc = LDAP_SLIST_FIRST(&controls_list);
273 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
275 ch_free( sc->sc_oid );
276 if ( sc->sc_extendedops != NULL ) {
277 ldap_charray_free( sc->sc_extendedops );
284 * Format the supportedControl attribute of the root DSE,
285 * detailing which controls are supported by the directory
289 controls_root_dse_info( Entry *e )
291 AttributeDescription *ad_supportedControl
292 = slap_schema.si_ad_supportedControl;
293 struct berval vals[2];
294 struct slap_control *sc;
296 vals[1].bv_val = NULL;
299 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
300 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
302 vals[0].bv_val = sc->sc_oid;
303 vals[0].bv_len = strlen( sc->sc_oid );
305 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
314 * Return a list of OIDs and operation masks for supported
315 * controls. Used by SLAPI.
318 get_supported_controls(char ***ctrloidsp,
319 slap_mask_t **ctrlmasks)
324 struct slap_control *sc;
328 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
338 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
339 if ( oids == NULL ) {
340 return LDAP_NO_MEMORY;
342 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
343 if ( masks == NULL ) {
345 return LDAP_NO_MEMORY;
350 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
351 oids[n] = ch_strdup( sc->sc_oid );
352 masks[n] = sc->sc_mask;
365 * Find a control given its OID.
367 static struct slap_control *
368 find_ctrl( const char *oid )
370 struct slap_control *sc;
372 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
373 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
382 slap_find_control_id(
386 struct slap_control *ctrl = find_ctrl( oid );
388 if ( cid ) *cid = ctrl->sc_cid;
391 return LDAP_CONTROL_NOT_FOUND;
395 slap_global_control( Operation *op, const char *oid, int *cid )
397 struct slap_control *ctrl = find_ctrl( oid );
399 if ( ctrl == NULL ) {
400 /* should not be reachable */
401 Debug( LDAP_DEBUG_ANY,
402 "slap_global_control: unrecognized control: %s\n",
404 return LDAP_CONTROL_NOT_FOUND;
407 if ( cid ) *cid = ctrl->sc_cid;
409 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
410 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
411 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
413 return LDAP_COMPARE_TRUE;
416 Debug( LDAP_DEBUG_TRACE,
417 "slap_global_control: unavailable control: %s\n",
420 return LDAP_COMPARE_FALSE;
423 void slap_free_ctrls(
425 LDAPControl **ctrls )
429 for (i=0; ctrls[i]; i++) {
430 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
432 op->o_tmpfree( ctrls, op->o_tmpmemctx );
438 LDAPControl *control,
441 struct slap_control *sc;
443 sc = find_ctrl( control->ldctl_oid );
445 /* recognized control */
447 switch( op->o_tag ) {
449 tagmask = SLAP_CTRL_ADD;
452 tagmask = SLAP_CTRL_BIND;
454 case LDAP_REQ_COMPARE:
455 tagmask = SLAP_CTRL_COMPARE;
457 case LDAP_REQ_DELETE:
458 tagmask = SLAP_CTRL_DELETE;
460 case LDAP_REQ_MODIFY:
461 tagmask = SLAP_CTRL_MODIFY;
463 case LDAP_REQ_RENAME:
464 tagmask = SLAP_CTRL_RENAME;
466 case LDAP_REQ_SEARCH:
467 tagmask = SLAP_CTRL_SEARCH;
469 case LDAP_REQ_UNBIND:
470 tagmask = SLAP_CTRL_UNBIND;
472 case LDAP_REQ_ABANDON:
473 tagmask = SLAP_CTRL_ABANDON;
475 case LDAP_REQ_EXTENDED:
477 assert( op->ore_reqoid.bv_val != NULL );
478 if( sc->sc_extendedops != NULL ) {
480 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
481 if( strcmp( op->ore_reqoid.bv_val,
482 sc->sc_extendedops[i] ) == 0 )
491 *text = "controls internal error";
495 if (( sc->sc_mask & tagmask ) == tagmask ) {
496 /* available extension */
499 if( !sc->sc_parse ) {
500 *text = "not yet implemented";
504 rc = sc->sc_parse( op, rs, control );
506 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
510 } else if( control->ldctl_iscritical ) {
511 /* unavailable CRITICAL control */
512 *text = "critical extension is unavailable";
513 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
515 } else if( control->ldctl_iscritical ) {
516 /* unrecognized CRITICAL control */
517 *text = "critical extension is not recognized";
518 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
533 BerElement *ber = op->o_ber;
536 len = ber_pvt_ber_remaining(ber);
540 rs->sr_err = LDAP_SUCCESS;
544 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
545 if( tag == LBER_ERROR ) {
546 rs->sr_err = SLAPD_DISCONNECT;
547 rs->sr_text = "unexpected data in PDU";
553 Debug( LDAP_DEBUG_TRACE,
554 "=> get_ctrls\n", 0, 0, 0 );
556 if( op->o_protocol < LDAP_VERSION3 ) {
557 rs->sr_err = SLAPD_DISCONNECT;
558 rs->sr_text = "controls require LDAPv3";
562 /* one for first control, one for termination */
563 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
566 if( op->ctrls == NULL ) {
567 rs->sr_err = LDAP_NO_MEMORY;
568 rs->sr_text = "no memory";
573 op->o_ctrls[nctrls] = NULL;
575 /* step through each element */
576 for( tag = ber_first_element( ber, &len, &opaque );
578 tag = ber_next_element( ber, &len, opaque ) )
581 LDAPControl **tctrls;
583 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
584 memset(c, 0, sizeof(LDAPControl));
586 /* allocate pointer space for current controls (nctrls)
587 * + this control + extra NULL
589 tctrls = op->o_tmprealloc( op->o_ctrls,
590 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
593 if( tctrls == NULL ) {
595 ldap_controls_free(op->o_ctrls);
598 rs->sr_err = LDAP_NO_MEMORY;
599 rs->sr_text = "no memory";
603 op->o_ctrls = tctrls;
605 op->o_ctrls[nctrls++] = c;
606 op->o_ctrls[nctrls] = NULL;
608 tag = ber_scanf( ber, "{m" /*}*/, &bv );
609 c->ldctl_oid = bv.bv_val;
611 if( tag == LBER_ERROR ) {
612 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
615 slap_free_ctrls( op, op->o_ctrls );
617 rs->sr_err = SLAPD_DISCONNECT;
618 rs->sr_text = "decoding controls error";
621 } else if( c->ldctl_oid == NULL ) {
622 Debug( LDAP_DEBUG_TRACE,
623 "get_ctrls: conn %lu got emtpy OID.\n",
624 op->o_connid, 0, 0 );
626 slap_free_ctrls( op, op->o_ctrls );
628 rs->sr_err = LDAP_PROTOCOL_ERROR;
629 rs->sr_text = "OID field is empty";
633 tag = ber_peek_tag( ber, &len );
635 if( tag == LBER_BOOLEAN ) {
637 tag = ber_scanf( ber, "b", &crit );
639 if( tag == LBER_ERROR ) {
640 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
642 slap_free_ctrls( op, op->o_ctrls );
644 rs->sr_err = SLAPD_DISCONNECT;
645 rs->sr_text = "decoding controls error";
649 c->ldctl_iscritical = (crit != 0);
650 tag = ber_peek_tag( ber, &len );
653 if( tag == LBER_OCTETSTRING ) {
654 tag = ber_scanf( ber, "m", &c->ldctl_value );
656 if( tag == LBER_ERROR ) {
657 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
658 "%s (%scritical): get value failed.\n",
659 op->o_connid, c->ldctl_oid,
660 c->ldctl_iscritical ? "" : "non" );
661 slap_free_ctrls( op, op->o_ctrls );
663 rs->sr_err = SLAPD_DISCONNECT;
664 rs->sr_text = "decoding controls error";
669 Debug( LDAP_DEBUG_TRACE,
670 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
671 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
673 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
674 if ( rs->sr_err != LDAP_SUCCESS ) {
680 Debug( LDAP_DEBUG_TRACE,
681 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
682 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
684 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
685 if( rs->sr_err == SLAPD_DISCONNECT ) {
686 rs->sr_err = LDAP_PROTOCOL_ERROR;
687 send_ldap_disconnect( op, rs );
688 rs->sr_err = SLAPD_DISCONNECT;
690 send_ldap_result( op, rs );
702 BI_chk_controls fnc )
706 switch ( op->o_ctrlflag[ ctrl ] ) {
707 case SLAP_CONTROL_NONCRITICAL:
708 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
709 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid, slap_known_controls[ ctrl - 1 ] ) == 0 )
716 rs->sr_err = LDAP_OTHER;
724 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
727 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
728 ( i - j ) * sizeof( LDAPControl * ) );
731 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
735 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
737 Debug( LDAP_DEBUG_ANY, "%s: "
738 "non-critical control \"%s\" not supported; stripped.\n",
739 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
742 case SLAP_CONTROL_IGNORED:
743 case SLAP_CONTROL_NONE:
744 rs->sr_err = SLAP_CB_CONTINUE;
747 case SLAP_CONTROL_CRITICAL:
748 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
752 Debug( LDAP_DEBUG_ANY, "%s: "
753 "critical control \"%s\" not supported.\n",
754 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
758 /* handle all cases! */
766 static int parseManageDIT (
771 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
772 rs->sr_text = "manageDIT control specified multiple times";
773 return LDAP_PROTOCOL_ERROR;
776 if ( ctrl->ldctl_value.bv_len ) {
777 rs->sr_text = "manageDIT control value not empty";
778 return LDAP_PROTOCOL_ERROR;
781 op->o_managedit = ctrl->ldctl_iscritical
782 ? SLAP_CONTROL_CRITICAL
783 : SLAP_CONTROL_NONCRITICAL;
789 static int parseManageDSAit (
794 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
795 rs->sr_text = "manageDSAit control specified multiple times";
796 return LDAP_PROTOCOL_ERROR;
799 if ( ctrl->ldctl_value.bv_len ) {
800 rs->sr_text = "manageDSAit control value not empty";
801 return LDAP_PROTOCOL_ERROR;
804 op->o_managedsait = ctrl->ldctl_iscritical
805 ? SLAP_CONTROL_CRITICAL
806 : SLAP_CONTROL_NONCRITICAL;
811 static int parseProxyAuthz (
817 struct berval dn = BER_BVNULL;
819 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
820 rs->sr_text = "proxy authorization control specified multiple times";
821 return LDAP_PROTOCOL_ERROR;
824 op->o_proxy_authz = ctrl->ldctl_iscritical
825 ? SLAP_CONTROL_CRITICAL
826 : SLAP_CONTROL_NONCRITICAL;
828 Debug( LDAP_DEBUG_ARGS,
829 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
831 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
834 if ( ctrl->ldctl_value.bv_len == 0 ) {
835 Debug( LDAP_DEBUG_TRACE,
836 "parseProxyAuthz: conn=%lu anonymous\n",
837 op->o_connid, 0, 0 );
840 op->o_ndn.bv_val[ 0 ] = '\0';
841 op->o_ndn.bv_len = 0;
843 op->o_dn.bv_val[ 0 ] = '\0';
849 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
850 NULL, &dn, SLAP_GETDN_AUTHZID );
852 /* FIXME: empty DN in proxyAuthz control should be legal... */
853 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
855 ch_free( dn.bv_val );
857 rs->sr_text = "authzId mapping failed";
858 return LDAP_PROXY_AUTHZ_FAILURE;
861 Debug( LDAP_DEBUG_TRACE,
862 "parseProxyAuthz: conn=%lu \"%s\"\n",
864 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
866 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
869 ch_free( dn.bv_val );
870 rs->sr_text = "not authorized to assume identity";
871 return LDAP_PROXY_AUTHZ_FAILURE;
874 ch_free( op->o_ndn.bv_val );
875 ch_free( op->o_dn.bv_val );
878 * NOTE: since slap_sasl_getdn() returns a normalized dn,
879 * from now on op->o_dn is normalized
882 ber_dupbv( &op->o_dn, &dn );
885 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
886 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
891 static int parseNoOp (
896 if ( op->o_noop != SLAP_CONTROL_NONE ) {
897 rs->sr_text = "noop control specified multiple times";
898 return LDAP_PROTOCOL_ERROR;
901 if ( ctrl->ldctl_value.bv_len ) {
902 rs->sr_text = "noop control value not empty";
903 return LDAP_PROTOCOL_ERROR;
906 op->o_noop = ctrl->ldctl_iscritical
907 ? SLAP_CONTROL_CRITICAL
908 : SLAP_CONTROL_NONCRITICAL;
913 static int parsePagedResults (
918 int rc = LDAP_SUCCESS;
922 struct berval cookie = BER_BVNULL;
923 PagedResultsState *ps;
925 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
926 rs->sr_text = "paged results control specified multiple times";
927 return LDAP_PROTOCOL_ERROR;
930 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
931 rs->sr_text = "paged results control value is empty (or absent)";
932 return LDAP_PROTOCOL_ERROR;
935 /* Parse the control value
936 * realSearchControlValue ::= SEQUENCE {
937 * size INTEGER (0..maxInt),
938 * -- requested page size from client
939 * -- result set size estimate from server
940 * cookie OCTET STRING
943 ber = ber_init( &ctrl->ldctl_value );
945 rs->sr_text = "internal error";
949 tag = ber_scanf( ber, "{im}", &size, &cookie );
951 if ( tag == LBER_ERROR ) {
952 rs->sr_text = "paged results control could not be decoded";
953 rc = LDAP_PROTOCOL_ERROR;
958 rs->sr_text = "paged results control size invalid";
959 rc = LDAP_PROTOCOL_ERROR;
963 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
964 *ps = op->o_conn->c_pagedresults_state;
966 op->o_pagedresults_state = ps;
968 /* NOTE: according to RFC 2696 3.:
970 If the page size is greater than or equal to the sizeLimit value, the
971 server should ignore the control as the request can be satisfied in a
974 * NOTE: this assumes that the op->ors_slimit be set
975 * before the controls are parsed.
978 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
979 op->o_pagedresults = SLAP_CONTROL_IGNORED;
981 } else if ( ctrl->ldctl_iscritical ) {
982 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
985 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
989 (void)ber_free( ber, 1 );
994 static int parseSortedResults (
999 int rc = LDAP_SUCCESS;
1001 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1002 rs->sr_text = "sorted results control specified multiple times";
1003 return LDAP_PROTOCOL_ERROR;
1006 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1007 rs->sr_text = "sorted results control value is empty (or absent)";
1008 return LDAP_PROTOCOL_ERROR;
1011 /* blow off parsing the value */
1013 op->o_sortedresults = ctrl->ldctl_iscritical
1014 ? SLAP_CONTROL_CRITICAL
1015 : SLAP_CONTROL_NONCRITICAL;
1021 static int parseAssert (
1027 struct berval fstr = BER_BVNULL;
1029 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1030 rs->sr_text = "assert control specified multiple times";
1031 return LDAP_PROTOCOL_ERROR;
1034 if ( ctrl->ldctl_value.bv_len == 0 ) {
1035 rs->sr_text = "assert control value is empty (or absent)";
1036 return LDAP_PROTOCOL_ERROR;
1039 ber = ber_init( &(ctrl->ldctl_value) );
1041 rs->sr_text = "assert control: internal error";
1045 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
1047 if( rs->sr_err != LDAP_SUCCESS ) {
1048 if( rs->sr_err == SLAPD_DISCONNECT ) {
1049 rs->sr_err = LDAP_PROTOCOL_ERROR;
1050 send_ldap_disconnect( op, rs );
1051 rs->sr_err = SLAPD_DISCONNECT;
1053 send_ldap_result( op, rs );
1055 if( op->o_assertion != NULL ) {
1056 filter_free_x( op, op->o_assertion );
1062 filter2bv_x( op, op->o_assertion, &fstr );
1064 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1065 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1066 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1069 op->o_assert = ctrl->ldctl_iscritical
1070 ? SLAP_CONTROL_CRITICAL
1071 : SLAP_CONTROL_NONCRITICAL;
1073 rs->sr_err = LDAP_SUCCESS;
1074 return LDAP_SUCCESS;
1077 static int parsePreRead (
1082 ber_len_t siz, off, i;
1083 AttributeName *an = NULL;
1086 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1087 rs->sr_text = "preread control specified multiple times";
1088 return LDAP_PROTOCOL_ERROR;
1091 if ( ctrl->ldctl_value.bv_len == 0 ) {
1092 rs->sr_text = "preread control value is empty (or absent)";
1093 return LDAP_PROTOCOL_ERROR;
1096 ber = ber_init( &(ctrl->ldctl_value) );
1098 rs->sr_text = "preread control: internal error";
1102 siz = sizeof( AttributeName );
1103 off = offsetof( AttributeName, an_name );
1104 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1105 rs->sr_text = "preread control: decoding error";
1106 return LDAP_PROTOCOL_ERROR;
1109 for( i=0; i<siz; i++ ) {
1110 int rc = LDAP_SUCCESS;
1111 const char *dummy = NULL;
1113 an[i].an_desc = NULL;
1115 an[i].an_oc_exclude = 0;
1116 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1117 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1120 : "postread control: unknown attributeType";
1125 op->o_preread = ctrl->ldctl_iscritical
1126 ? SLAP_CONTROL_CRITICAL
1127 : SLAP_CONTROL_NONCRITICAL;
1129 op->o_preread_attrs = an;
1131 rs->sr_err = LDAP_SUCCESS;
1132 return LDAP_SUCCESS;
1135 static int parsePostRead (
1140 ber_len_t siz, off, i;
1141 AttributeName *an = NULL;
1144 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1145 rs->sr_text = "postread control specified multiple times";
1146 return LDAP_PROTOCOL_ERROR;
1149 if ( ctrl->ldctl_value.bv_len == 0 ) {
1150 rs->sr_text = "postread control value is empty (or absent)";
1151 return LDAP_PROTOCOL_ERROR;
1154 ber = ber_init( &(ctrl->ldctl_value) );
1156 rs->sr_text = "postread control: internal error";
1160 siz = sizeof( AttributeName );
1161 off = offsetof( AttributeName, an_name );
1162 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1163 rs->sr_text = "postread control: decoding error";
1164 return LDAP_PROTOCOL_ERROR;
1167 for( i=0; i<siz; i++ ) {
1168 int rc = LDAP_SUCCESS;
1169 const char *dummy = NULL;
1171 an[i].an_desc = NULL;
1173 an[i].an_oc_exclude = 0;
1174 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1175 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1178 : "postread control: unknown attributeType";
1183 op->o_postread = ctrl->ldctl_iscritical
1184 ? SLAP_CONTROL_CRITICAL
1185 : SLAP_CONTROL_NONCRITICAL;
1187 op->o_postread_attrs = an;
1189 rs->sr_err = LDAP_SUCCESS;
1190 return LDAP_SUCCESS;
1193 static int parseValuesReturnFilter (
1199 struct berval fstr = BER_BVNULL;
1201 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1202 rs->sr_text = "valuesReturnFilter control specified multiple times";
1203 return LDAP_PROTOCOL_ERROR;
1206 if ( ctrl->ldctl_value.bv_len == 0 ) {
1207 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1208 return LDAP_PROTOCOL_ERROR;
1211 ber = ber_init( &(ctrl->ldctl_value) );
1213 rs->sr_text = "internal error";
1217 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1219 if( rs->sr_err != LDAP_SUCCESS ) {
1220 if( rs->sr_err == SLAPD_DISCONNECT ) {
1221 rs->sr_err = LDAP_PROTOCOL_ERROR;
1222 send_ldap_disconnect( op, rs );
1223 rs->sr_err = SLAPD_DISCONNECT;
1225 send_ldap_result( op, rs );
1227 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1231 vrFilter2bv( op, op->o_vrFilter, &fstr );
1234 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1235 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1236 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1239 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1240 ? SLAP_CONTROL_CRITICAL
1241 : SLAP_CONTROL_NONCRITICAL;
1243 rs->sr_err = LDAP_SUCCESS;
1244 return LDAP_SUCCESS;
1247 #ifdef LDAP_CONTROL_SUBENTRIES
1248 static int parseSubentries (
1253 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1254 rs->sr_text = "subentries control specified multiple times";
1255 return LDAP_PROTOCOL_ERROR;
1258 /* FIXME: should use BER library */
1259 if( ( ctrl->ldctl_value.bv_len != 3 )
1260 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1261 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1263 rs->sr_text = "subentries control value encoding is bogus";
1264 return LDAP_PROTOCOL_ERROR;
1267 op->o_subentries = ctrl->ldctl_iscritical
1268 ? SLAP_CONTROL_CRITICAL
1269 : SLAP_CONTROL_NONCRITICAL;
1271 if (ctrl->ldctl_value.bv_val[2]) {
1272 set_subentries_visibility( op );
1275 return LDAP_SUCCESS;
1279 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1280 static int parsePermissiveModify (
1285 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1286 rs->sr_text = "permissiveModify control specified multiple times";
1287 return LDAP_PROTOCOL_ERROR;
1290 if ( ctrl->ldctl_value.bv_len ) {
1291 rs->sr_text = "permissiveModify control value not empty";
1292 return LDAP_PROTOCOL_ERROR;
1295 op->o_permissive_modify = ctrl->ldctl_iscritical
1296 ? SLAP_CONTROL_CRITICAL
1297 : SLAP_CONTROL_NONCRITICAL;
1299 return LDAP_SUCCESS;
1303 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1304 static int parseDomainScope (
1309 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1310 rs->sr_text = "domainScope control specified multiple times";
1311 return LDAP_PROTOCOL_ERROR;
1314 if ( ctrl->ldctl_value.bv_len ) {
1315 rs->sr_text = "domainScope control value not empty";
1316 return LDAP_PROTOCOL_ERROR;
1319 op->o_domain_scope = ctrl->ldctl_iscritical
1320 ? SLAP_CONTROL_CRITICAL
1321 : SLAP_CONTROL_NONCRITICAL;
1323 return LDAP_SUCCESS;
1327 #ifdef SLAP_CONTROL_X_TREE_DELETE
1328 static int parseTreeDelete (
1333 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1334 rs->sr_text = "treeDelete control specified multiple times";
1335 return LDAP_PROTOCOL_ERROR;
1338 if ( ctrl->ldctl_value.bv_len ) {
1339 rs->sr_text = "treeDelete control value not empty";
1340 return LDAP_PROTOCOL_ERROR;
1343 op->o_tree_delete = ctrl->ldctl_iscritical
1344 ? SLAP_CONTROL_CRITICAL
1345 : SLAP_CONTROL_NONCRITICAL;
1347 return LDAP_SUCCESS;
1351 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
1352 static int parseSearchOptions (
1358 ber_int_t search_flags;
1361 if ( ctrl->ldctl_value.bv_len == 0 ) {
1362 rs->sr_text = "searchOptions control value is empty (or absent)";
1363 return LDAP_PROTOCOL_ERROR;
1366 ber = ber_init( &ctrl->ldctl_value );
1368 rs->sr_text = "internal error";
1372 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1373 rs->sr_text = "searchOptions control decoding error";
1374 return LDAP_PROTOCOL_ERROR;
1377 (void) ber_free( ber, 1 );
1379 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1380 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1381 rs->sr_text = "searchOptions control specified multiple times "
1382 "or with domainScope control";
1383 return LDAP_PROTOCOL_ERROR;
1386 op->o_domain_scope = ctrl->ldctl_iscritical
1387 ? SLAP_CONTROL_CRITICAL
1388 : SLAP_CONTROL_NONCRITICAL;
1391 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1392 /* Other search flags not recognised so far,
1394 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1396 rs->sr_text = "searchOptions contained unrecognized flag";
1397 return LDAP_UNWILLING_TO_PERFORM;
1400 return LDAP_SUCCESS;