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 parseModifyIncrement;
36 static SLAP_CTRL_PARSE_FN parseNoOp;
37 static SLAP_CTRL_PARSE_FN parsePagedResults;
39 static SLAP_CTRL_PARSE_FN parseSortedResults;
41 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
42 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
43 static SLAP_CTRL_PARSE_FN parseDomainScope;
44 static SLAP_CTRL_PARSE_FN parseTreeDelete;
45 static SLAP_CTRL_PARSE_FN parseSearchOptions;
46 static SLAP_CTRL_PARSE_FN parseSubentries;
48 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
50 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
51 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
53 struct slap_control_ids slap_cids;
59 /* The controlID for this control */
62 /* Operations supported by control */
65 /* Extended operations supported by control */
66 char **sc_extendedops;
68 /* Control parsing callback */
69 SLAP_CTRL_PARSE_FN *sc_parse;
71 LDAP_SLIST_ENTRY(slap_control) sc_next;
74 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
75 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
78 * all known request control OIDs should be added to this list
81 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
82 * addresses an undefined control; this allows to safely test for
83 * well known controls even if they are not registered, e.g. if
84 * they get moved to modules. An example is sc_LDAPsync, which
85 * is implemented in the syncprov overlay and thus, if configured
86 * as dynamic module, may not be registered. One side effect is that
87 * slap_known_controls[0] == NULL, so it should always be used
89 * FIXME: should we define the "undefined control" oid?
91 char *slap_known_controls[SLAP_MAX_CIDS+1];
92 static int num_known_controls = 1;
94 static char *proxy_authz_extops[] = {
95 LDAP_EXOP_MODIFY_PASSWD,
100 static struct slap_control control_defs[] = {
101 { LDAP_CONTROL_ASSERT,
102 (int)offsetof(struct slap_control_ids, sc_assert),
103 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
104 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
105 { LDAP_CONTROL_PRE_READ,
106 (int)offsetof(struct slap_control_ids, sc_preRead),
107 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
108 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
109 { LDAP_CONTROL_POST_READ,
110 (int)offsetof(struct slap_control_ids, sc_postRead),
111 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
112 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
113 { LDAP_CONTROL_VALUESRETURNFILTER,
114 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
115 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
116 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
117 { LDAP_CONTROL_PAGEDRESULTS,
118 (int)offsetof(struct slap_control_ids, sc_pagedResults),
119 SLAP_CTRL_SEARCH, NULL,
120 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
122 { LDAP_CONTROL_SORTREQUEST,
123 (int)offsetof(struct slap_control_ids, sc_sortedResults),
124 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE, NULL,
125 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
127 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
128 { LDAP_CONTROL_X_DOMAIN_SCOPE,
129 (int)offsetof(struct slap_control_ids, sc_domainScope),
130 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
131 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
133 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
134 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
135 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
136 SLAP_CTRL_MODIFY, NULL,
137 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 #ifdef SLAP_CONTROL_X_TREE_DELETE
140 { LDAP_CONTROL_X_TREE_DELETE,
141 (int)offsetof(struct slap_control_ids, sc_treeDelete),
142 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
143 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
145 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
146 { LDAP_CONTROL_X_SEARCH_OPTIONS,
147 (int)offsetof(struct slap_control_ids, sc_searchOptions),
148 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
149 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
151 #ifdef LDAP_CONTROL_SUBENTRIES
152 { LDAP_CONTROL_SUBENTRIES,
153 (int)offsetof(struct slap_control_ids, sc_subentries),
154 SLAP_CTRL_SEARCH, NULL,
155 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
158 (int)offsetof(struct slap_control_ids, sc_noOp),
159 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
160 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
161 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
162 { LDAP_CONTROL_MODIFY_INCREMENT,
163 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
164 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
165 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
168 { LDAP_CONTROL_MANAGEDIT,
169 (int)offsetof(struct slap_control_ids, sc_manageDIT),
170 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE, NULL,
171 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
173 { LDAP_CONTROL_MANAGEDSAIT,
174 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
175 SLAP_CTRL_ACCESS, NULL,
176 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
177 { LDAP_CONTROL_PROXY_AUTHZ,
178 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
179 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS, proxy_authz_extops,
180 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
181 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
185 * Register a supported control.
187 * This can be called by an OpenLDAP plugin or, indirectly, by a
188 * SLAPI plugin calling slapi_register_supported_control().
191 register_supported_control(const char *controloid,
192 slap_mask_t controlmask,
194 SLAP_CTRL_PARSE_FN *controlparsefn,
197 struct slap_control *sc;
200 if ( num_known_controls >= SLAP_MAX_CIDS ) {
201 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
202 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
203 SLAP_MAX_CIDS, 0, 0 );
207 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
209 /* sanity check - should never happen */
210 for ( i = 0; slap_known_controls[ i ]; i++ ) {
211 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
212 Debug( LDAP_DEBUG_ANY,
213 "Control %s already registered.\n",
215 return LDAP_PARAM_ERROR;
219 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
220 if ( sc == NULL ) return LDAP_NO_MEMORY;
222 sc->sc_oid = ch_strdup( controloid );
223 sc->sc_mask = controlmask;
224 if ( controlexops != NULL ) {
225 sc->sc_extendedops = ldap_charray_dup( controlexops );
226 if ( sc->sc_extendedops == NULL ) {
228 return LDAP_NO_MEMORY;
231 sc->sc_extendedops = NULL;
233 sc->sc_parse = controlparsefn;
235 if ( controlcid ) *controlcid = num_known_controls;
236 sc->sc_cid = num_known_controls;
238 /* Update slap_known_controls, too. */
239 slap_known_controls[num_known_controls-1] = sc->sc_oid;
240 slap_known_controls[num_known_controls++] = NULL;
242 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
243 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
248 * One-time initialization of internal controls.
251 slap_controls_init( void )
254 struct slap_control *sc;
258 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
259 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
260 rc = register_supported_control( control_defs[i].sc_oid,
261 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
262 control_defs[i].sc_parse, cid );
263 if ( rc != LDAP_SUCCESS ) break;
270 * Free memory associated with list of supported controls.
273 controls_destroy( void )
275 struct slap_control *sc;
277 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
278 sc = LDAP_SLIST_FIRST(&controls_list);
279 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
281 ch_free( sc->sc_oid );
282 if ( sc->sc_extendedops != NULL ) {
283 ldap_charray_free( sc->sc_extendedops );
290 * Format the supportedControl attribute of the root DSE,
291 * detailing which controls are supported by the directory
295 controls_root_dse_info( Entry *e )
297 AttributeDescription *ad_supportedControl
298 = slap_schema.si_ad_supportedControl;
299 struct berval vals[2];
300 struct slap_control *sc;
302 vals[1].bv_val = NULL;
305 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
306 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
308 vals[0].bv_val = sc->sc_oid;
309 vals[0].bv_len = strlen( sc->sc_oid );
311 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
320 * Return a list of OIDs and operation masks for supported
321 * controls. Used by SLAPI.
324 get_supported_controls(char ***ctrloidsp,
325 slap_mask_t **ctrlmasks)
331 struct slap_control *sc;
335 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
345 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
346 if ( oids == NULL ) {
347 return LDAP_NO_MEMORY;
349 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
350 if ( masks == NULL ) {
352 return LDAP_NO_MEMORY;
357 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
358 oids[n] = ch_strdup( sc->sc_oid );
359 masks[n] = sc->sc_mask;
372 * Find a control given its OID.
374 static struct slap_control *
375 find_ctrl( const char *oid )
377 struct slap_control *sc;
379 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
380 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
389 slap_find_control_id(
393 struct slap_control *ctrl = find_ctrl( oid );
395 if ( cid ) *cid = ctrl->sc_cid;
398 return LDAP_CONTROL_NOT_FOUND;
402 slap_global_control( Operation *op, const char *oid, int *cid )
404 struct slap_control *ctrl = find_ctrl( oid );
406 if ( ctrl == NULL ) {
407 /* should not be reachable */
408 Debug( LDAP_DEBUG_ANY,
409 "slap_global_control: unrecognized control: %s\n",
411 return LDAP_CONTROL_NOT_FOUND;
414 if ( cid ) *cid = ctrl->sc_cid;
416 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
417 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
418 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
420 return LDAP_COMPARE_TRUE;
423 Debug( LDAP_DEBUG_TRACE,
424 "slap_global_control: unavailable control: %s\n",
427 return LDAP_COMPARE_FALSE;
430 void slap_free_ctrls(
432 LDAPControl **ctrls )
436 for (i=0; ctrls[i]; i++) {
437 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
439 op->o_tmpfree( ctrls, op->o_tmpmemctx );
451 BerElement *ber = op->o_ber;
452 struct slap_control *sc;
455 len = ber_pvt_ber_remaining(ber);
459 rs->sr_err = LDAP_SUCCESS;
463 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
464 if( tag == LBER_ERROR ) {
465 rs->sr_err = SLAPD_DISCONNECT;
466 rs->sr_text = "unexpected data in PDU";
472 Debug( LDAP_DEBUG_TRACE,
473 "=> get_ctrls\n", 0, 0, 0 );
475 if( op->o_protocol < LDAP_VERSION3 ) {
476 rs->sr_err = SLAPD_DISCONNECT;
477 rs->sr_text = "controls require LDAPv3";
481 /* one for first control, one for termination */
482 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
485 if( op->ctrls == NULL ) {
486 rs->sr_err = LDAP_NO_MEMORY;
487 rs->sr_text = "no memory";
492 op->o_ctrls[nctrls] = NULL;
494 /* step through each element */
495 for( tag = ber_first_element( ber, &len, &opaque );
497 tag = ber_next_element( ber, &len, opaque ) )
500 LDAPControl **tctrls;
502 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
503 memset(c, 0, sizeof(LDAPControl));
505 /* allocate pointer space for current controls (nctrls)
506 * + this control + extra NULL
508 tctrls = op->o_tmprealloc( op->o_ctrls,
509 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
512 if( tctrls == NULL ) {
514 ldap_controls_free(op->o_ctrls);
517 rs->sr_err = LDAP_NO_MEMORY;
518 rs->sr_text = "no memory";
522 op->o_ctrls = tctrls;
524 op->o_ctrls[nctrls++] = c;
525 op->o_ctrls[nctrls] = NULL;
527 tag = ber_scanf( ber, "{m" /*}*/, &bv );
528 c->ldctl_oid = bv.bv_val;
530 if( tag == LBER_ERROR ) {
531 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
534 slap_free_ctrls( op, op->o_ctrls );
536 rs->sr_err = SLAPD_DISCONNECT;
537 rs->sr_text = "decoding controls error";
540 } else if( c->ldctl_oid == NULL ) {
541 Debug( LDAP_DEBUG_TRACE,
542 "get_ctrls: conn %lu got emtpy OID.\n",
543 op->o_connid, 0, 0 );
545 slap_free_ctrls( op, op->o_ctrls );
547 rs->sr_err = LDAP_PROTOCOL_ERROR;
548 rs->sr_text = "OID field is empty";
552 tag = ber_peek_tag( ber, &len );
554 if( tag == LBER_BOOLEAN ) {
556 tag = ber_scanf( ber, "b", &crit );
558 if( tag == LBER_ERROR ) {
559 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
561 slap_free_ctrls( op, op->o_ctrls );
563 rs->sr_err = SLAPD_DISCONNECT;
564 rs->sr_text = "decoding controls error";
568 c->ldctl_iscritical = (crit != 0);
569 tag = ber_peek_tag( ber, &len );
572 if( tag == LBER_OCTETSTRING ) {
573 tag = ber_scanf( ber, "m", &c->ldctl_value );
575 if( tag == LBER_ERROR ) {
576 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
577 "%s (%scritical): get value failed.\n",
578 op->o_connid, c->ldctl_oid,
579 c->ldctl_iscritical ? "" : "non" );
580 slap_free_ctrls( op, op->o_ctrls );
582 rs->sr_err = SLAPD_DISCONNECT;
583 rs->sr_text = "decoding controls error";
588 Debug( LDAP_DEBUG_TRACE,
589 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
590 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
592 sc = find_ctrl( c->ldctl_oid );
594 /* recognized control */
596 switch( op->o_tag ) {
598 tagmask = SLAP_CTRL_ADD;
601 tagmask = SLAP_CTRL_BIND;
603 case LDAP_REQ_COMPARE:
604 tagmask = SLAP_CTRL_COMPARE;
606 case LDAP_REQ_DELETE:
607 tagmask = SLAP_CTRL_DELETE;
609 case LDAP_REQ_MODIFY:
610 tagmask = SLAP_CTRL_MODIFY;
612 case LDAP_REQ_RENAME:
613 tagmask = SLAP_CTRL_RENAME;
615 case LDAP_REQ_SEARCH:
616 tagmask = SLAP_CTRL_SEARCH;
618 case LDAP_REQ_UNBIND:
619 tagmask = SLAP_CTRL_UNBIND;
621 case LDAP_REQ_ABANDON:
622 tagmask = SLAP_CTRL_ABANDON;
624 case LDAP_REQ_EXTENDED:
626 assert( op->ore_reqoid.bv_val != NULL );
627 if( sc->sc_extendedops != NULL ) {
629 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
630 if( strcmp( op->ore_reqoid.bv_val,
631 sc->sc_extendedops[i] ) == 0 )
640 rs->sr_err = LDAP_OTHER;
641 rs->sr_text = "controls internal error";
645 if (( sc->sc_mask & tagmask ) == tagmask ) {
646 /* available extension */
649 if( !sc->sc_parse ) {
650 rs->sr_err = LDAP_OTHER;
651 rs->sr_text = "not yet implemented";
655 rc = sc->sc_parse( op, rs, c );
657 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
662 } else if( c->ldctl_iscritical ) {
663 /* unavailable CRITICAL control */
664 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
665 rs->sr_text = "critical extension is unavailable";
669 } else if( c->ldctl_iscritical ) {
670 /* unrecognized CRITICAL control */
671 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
672 rs->sr_text = "critical extension is not recognized";
679 Debug( LDAP_DEBUG_TRACE,
680 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
681 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
683 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
684 if( rs->sr_err == SLAPD_DISCONNECT ) {
685 rs->sr_err = LDAP_PROTOCOL_ERROR;
686 send_ldap_disconnect( op, rs );
687 rs->sr_err = SLAPD_DISCONNECT;
689 send_ldap_result( op, rs );
696 static int parseModifyIncrement (
702 if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
703 rs->sr_text = "modifyIncrement control specified multiple times";
704 return LDAP_PROTOCOL_ERROR;
708 if ( ctrl->ldctl_value.bv_len ) {
709 rs->sr_text = "modifyIncrement control value not empty";
710 return LDAP_PROTOCOL_ERROR;
714 op->o_modifyIncrement = ctrl->ldctl_iscritical
715 ? SLAP_CONTROL_CRITICAL
716 : SLAP_CONTROL_NONCRITICAL;
723 static int parseManageDIT (
728 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
729 rs->sr_text = "manageDIT control specified multiple times";
730 return LDAP_PROTOCOL_ERROR;
733 if ( ctrl->ldctl_value.bv_len ) {
734 rs->sr_text = "manageDIT control value not empty";
735 return LDAP_PROTOCOL_ERROR;
738 op->o_managedit = ctrl->ldctl_iscritical
739 ? SLAP_CONTROL_CRITICAL
740 : SLAP_CONTROL_NONCRITICAL;
746 static int parseManageDSAit (
751 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
752 rs->sr_text = "manageDSAit control specified multiple times";
753 return LDAP_PROTOCOL_ERROR;
756 if ( ctrl->ldctl_value.bv_len ) {
757 rs->sr_text = "manageDSAit control value not empty";
758 return LDAP_PROTOCOL_ERROR;
761 op->o_managedsait = ctrl->ldctl_iscritical
762 ? SLAP_CONTROL_CRITICAL
763 : SLAP_CONTROL_NONCRITICAL;
768 static int parseProxyAuthz (
774 struct berval dn = BER_BVNULL;
776 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
777 rs->sr_text = "proxy authorization control specified multiple times";
778 return LDAP_PROTOCOL_ERROR;
781 op->o_proxy_authz = ctrl->ldctl_iscritical
782 ? SLAP_CONTROL_CRITICAL
783 : SLAP_CONTROL_NONCRITICAL;
785 Debug( LDAP_DEBUG_ARGS,
786 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
788 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
791 if ( ctrl->ldctl_value.bv_len == 0 ) {
792 Debug( LDAP_DEBUG_TRACE,
793 "parseProxyAuthz: conn=%lu anonymous\n",
794 op->o_connid, 0, 0 );
797 op->o_ndn.bv_val[ 0 ] = '\0';
798 op->o_ndn.bv_len = 0;
800 op->o_dn.bv_val[ 0 ] = '\0';
806 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
807 NULL, &dn, SLAP_GETDN_AUTHZID );
809 /* FIXME: empty DN in proxyAuthz control should be legal... */
810 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
812 ch_free( dn.bv_val );
814 rs->sr_text = "authzId mapping failed";
815 return LDAP_PROXY_AUTHZ_FAILURE;
818 Debug( LDAP_DEBUG_TRACE,
819 "parseProxyAuthz: conn=%lu \"%s\"\n",
821 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
823 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
826 ch_free( dn.bv_val );
827 rs->sr_text = "not authorized to assume identity";
828 return LDAP_PROXY_AUTHZ_FAILURE;
831 ch_free( op->o_ndn.bv_val );
832 ch_free( op->o_dn.bv_val );
835 * NOTE: since slap_sasl_getdn() returns a normalized dn,
836 * from now on op->o_dn is normalized
839 ber_dupbv( &op->o_dn, &dn );
842 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
843 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
848 static int parseNoOp (
853 if ( op->o_noop != SLAP_CONTROL_NONE ) {
854 rs->sr_text = "noop control specified multiple times";
855 return LDAP_PROTOCOL_ERROR;
858 if ( ctrl->ldctl_value.bv_len ) {
859 rs->sr_text = "noop control value not empty";
860 return LDAP_PROTOCOL_ERROR;
863 op->o_noop = ctrl->ldctl_iscritical
864 ? SLAP_CONTROL_CRITICAL
865 : SLAP_CONTROL_NONCRITICAL;
870 static int parsePagedResults (
875 int rc = LDAP_SUCCESS;
879 struct berval cookie = BER_BVNULL;
880 PagedResultsState *ps;
882 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
883 rs->sr_text = "paged results control specified multiple times";
884 return LDAP_PROTOCOL_ERROR;
887 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
888 rs->sr_text = "paged results control value is empty (or absent)";
889 return LDAP_PROTOCOL_ERROR;
892 /* Parse the control value
893 * realSearchControlValue ::= SEQUENCE {
894 * size INTEGER (0..maxInt),
895 * -- requested page size from client
896 * -- result set size estimate from server
897 * cookie OCTET STRING
900 ber = ber_init( &ctrl->ldctl_value );
902 rs->sr_text = "internal error";
906 tag = ber_scanf( ber, "{im}", &size, &cookie );
908 if ( tag == LBER_ERROR ) {
909 rs->sr_text = "paged results control could not be decoded";
910 rc = LDAP_PROTOCOL_ERROR;
915 rs->sr_text = "paged results control size invalid";
916 rc = LDAP_PROTOCOL_ERROR;
921 /* defer cookie decoding/checks to backend... */
922 if ( cookie.bv_len ) {
923 PagedResultsCookie reqcookie;
924 if( cookie.bv_len != sizeof( reqcookie ) ) {
926 rs->sr_text = "paged results cookie is invalid";
927 rc = LDAP_PROTOCOL_ERROR;
931 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
933 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
935 rs->sr_text = "paged results cookie is invalid";
936 rc = LDAP_PROTOCOL_ERROR;
939 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
940 rs->sr_text = "paged results cookie is invalid or old";
941 rc = LDAP_UNWILLING_TO_PERFORM;
946 /* Initial request. Initialize state. */
948 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
949 /* There's another pagedResults control on the
950 * same connection; reject new pagedResults controls
951 * (allowed by RFC2696) */
952 rs->sr_text = "paged results cookie unavailable; try later";
953 rc = LDAP_UNWILLING_TO_PERFORM;
957 op->o_pagedresults_state.ps_cookie = 0;
958 op->o_pagedresults_state.ps_count = 0;
962 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
963 *ps = op->o_conn->c_pagedresults_state;
965 op->o_pagedresults_state = ps;
967 /* NOTE: according to RFC 2696 3.:
969 If the page size is greater than or equal to the sizeLimit value, the
970 server should ignore the control as the request can be satisfied in a
973 * NOTE: this assumes that the op->ors_slimit be set
974 * before the controls are parsed.
977 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
978 op->o_pagedresults = SLAP_CONTROL_IGNORED;
980 } else if ( ctrl->ldctl_iscritical ) {
981 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
984 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
988 (void)ber_free( ber, 1 );
993 static int parseSortedResults (
998 int rc = LDAP_SUCCESS;
1000 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1001 rs->sr_text = "sorted results control specified multiple times";
1002 return LDAP_PROTOCOL_ERROR;
1005 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1006 rs->sr_text = "sorted results control value is empty (or absent)";
1007 return LDAP_PROTOCOL_ERROR;
1010 /* blow off parsing the value */
1012 op->o_sortedresults = ctrl->ldctl_iscritical
1013 ? SLAP_CONTROL_CRITICAL
1014 : SLAP_CONTROL_NONCRITICAL;
1020 static int parseAssert (
1026 struct berval fstr = BER_BVNULL;
1027 const char *err_msg = "";
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 int parseValuesReturnFilter (
1199 struct berval fstr = BER_BVNULL;
1200 const char *err_msg = "";
1202 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1203 rs->sr_text = "valuesReturnFilter control specified multiple times";
1204 return LDAP_PROTOCOL_ERROR;
1207 if ( ctrl->ldctl_value.bv_len == 0 ) {
1208 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1209 return LDAP_PROTOCOL_ERROR;
1212 ber = ber_init( &(ctrl->ldctl_value) );
1214 rs->sr_text = "internal error";
1218 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1220 if( rs->sr_err != LDAP_SUCCESS ) {
1221 if( rs->sr_err == SLAPD_DISCONNECT ) {
1222 rs->sr_err = LDAP_PROTOCOL_ERROR;
1223 send_ldap_disconnect( op, rs );
1224 rs->sr_err = SLAPD_DISCONNECT;
1226 send_ldap_result( op, rs );
1228 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1232 vrFilter2bv( op, op->o_vrFilter, &fstr );
1235 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1236 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1237 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1240 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1241 ? SLAP_CONTROL_CRITICAL
1242 : SLAP_CONTROL_NONCRITICAL;
1244 rs->sr_err = LDAP_SUCCESS;
1245 return LDAP_SUCCESS;
1248 #ifdef LDAP_CONTROL_SUBENTRIES
1249 static int parseSubentries (
1254 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1255 rs->sr_text = "subentries control specified multiple times";
1256 return LDAP_PROTOCOL_ERROR;
1259 /* FIXME: should use BER library */
1260 if( ( ctrl->ldctl_value.bv_len != 3 )
1261 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1262 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1264 rs->sr_text = "subentries control value encoding is bogus";
1265 return LDAP_PROTOCOL_ERROR;
1268 op->o_subentries = ctrl->ldctl_iscritical
1269 ? SLAP_CONTROL_CRITICAL
1270 : SLAP_CONTROL_NONCRITICAL;
1272 if (ctrl->ldctl_value.bv_val[2]) {
1273 set_subentries_visibility( op );
1276 return LDAP_SUCCESS;
1280 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1281 static int parsePermissiveModify (
1286 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1287 rs->sr_text = "permissiveModify control specified multiple times";
1288 return LDAP_PROTOCOL_ERROR;
1291 if ( ctrl->ldctl_value.bv_len ) {
1292 rs->sr_text = "permissiveModify control value not empty";
1293 return LDAP_PROTOCOL_ERROR;
1296 op->o_permissive_modify = ctrl->ldctl_iscritical
1297 ? SLAP_CONTROL_CRITICAL
1298 : SLAP_CONTROL_NONCRITICAL;
1300 return LDAP_SUCCESS;
1304 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1305 static int parseDomainScope (
1310 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1311 rs->sr_text = "domainScope control specified multiple times";
1312 return LDAP_PROTOCOL_ERROR;
1315 if ( ctrl->ldctl_value.bv_len ) {
1316 rs->sr_text = "domainScope control value not empty";
1317 return LDAP_PROTOCOL_ERROR;
1320 op->o_domain_scope = ctrl->ldctl_iscritical
1321 ? SLAP_CONTROL_CRITICAL
1322 : SLAP_CONTROL_NONCRITICAL;
1324 return LDAP_SUCCESS;
1328 #ifdef LDAP_CONTROL_X_TREE_DELETE
1329 static int parseTreeDelete (
1334 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1335 rs->sr_text = "treeDelete control specified multiple times";
1336 return LDAP_PROTOCOL_ERROR;
1339 if ( ctrl->ldctl_value.bv_len ) {
1340 rs->sr_text = "treeDelete control value not empty";
1341 return LDAP_PROTOCOL_ERROR;
1344 op->o_tree_delete = ctrl->ldctl_iscritical
1345 ? SLAP_CONTROL_CRITICAL
1346 : SLAP_CONTROL_NONCRITICAL;
1348 return LDAP_SUCCESS;
1352 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
1353 static int parseSearchOptions (
1359 ber_int_t search_flags;
1362 if ( ctrl->ldctl_value.bv_len == 0 ) {
1363 rs->sr_text = "searchOptions control value not empty";
1364 return LDAP_PROTOCOL_ERROR;
1367 ber = ber_init( &ctrl->ldctl_value );
1369 rs->sr_text = "internal error";
1373 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1374 rs->sr_text = "searchOptions control decoding error";
1375 return LDAP_PROTOCOL_ERROR;
1378 (void) ber_free( ber, 1 );
1380 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1381 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1382 rs->sr_text = "searchOptions control specified multiple times "
1383 "or with domainScope control";
1384 return LDAP_PROTOCOL_ERROR;
1387 op->o_domain_scope = ctrl->ldctl_iscritical
1388 ? SLAP_CONTROL_CRITICAL
1389 : SLAP_CONTROL_NONCRITICAL;
1392 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1393 /* Other search flags not recognised so far,
1395 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1397 rs->sr_text = "searchOptions contained unrecongized flag";
1398 return LDAP_UNWILLING_TO_PERFORM;
1401 return LDAP_SUCCESS;