2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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 parseDontUseCopy;
33 static SLAP_CTRL_PARSE_FN parseManageDIT;
35 static SLAP_CTRL_PARSE_FN parseManageDSAit;
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 #ifdef SLAP_CONTROL_X_TREE_DELETE
45 static SLAP_CTRL_PARSE_FN parseTreeDelete;
47 static SLAP_CTRL_PARSE_FN parseSearchOptions;
48 static SLAP_CTRL_PARSE_FN parseSubentries;
50 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
52 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
53 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
55 struct slap_control_ids slap_cids;
61 /* The controlID for this control */
64 /* Operations supported by control */
67 /* Extended operations supported by control */
68 char **sc_extendedops; /* input */
69 BerVarray sc_extendedopsbv; /* run-time use */
71 /* Control parsing callback */
72 SLAP_CTRL_PARSE_FN *sc_parse;
74 LDAP_SLIST_ENTRY(slap_control) sc_next;
77 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
78 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
81 * all known request control OIDs should be added to this list
84 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
85 * addresses an undefined control; this allows to safely test for
86 * well known controls even if they are not registered, e.g. if
87 * they get moved to modules. An example is sc_LDAPsync, which
88 * is implemented in the syncprov overlay and thus, if configured
89 * as dynamic module, may not be registered. One side effect is that
90 * slap_known_controls[0] == NULL, so it should always be used
92 * FIXME: should we define the "undefined control" oid?
94 char *slap_known_controls[SLAP_MAX_CIDS+1];
95 static int num_known_controls = 1;
97 static char *proxy_authz_extops[] = {
98 LDAP_EXOP_MODIFY_PASSWD,
104 static char *manageDSAit_extops[] = {
109 static struct slap_control control_defs[] = {
110 { LDAP_CONTROL_ASSERT,
111 (int)offsetof(struct slap_control_ids, sc_assert),
112 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME|
113 SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
115 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
116 { LDAP_CONTROL_PRE_READ,
117 (int)offsetof(struct slap_control_ids, sc_preRead),
118 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
120 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
121 { LDAP_CONTROL_POST_READ,
122 (int)offsetof(struct slap_control_ids, sc_postRead),
123 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
125 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
126 { LDAP_CONTROL_VALUESRETURNFILTER,
127 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
128 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
130 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
131 { LDAP_CONTROL_PAGEDRESULTS,
132 (int)offsetof(struct slap_control_ids, sc_pagedResults),
135 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
137 { LDAP_CONTROL_SORTREQUEST,
138 (int)offsetof(struct slap_control_ids, sc_sortedResults),
139 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
141 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
143 { LDAP_CONTROL_X_DOMAIN_SCOPE,
144 (int)offsetof(struct slap_control_ids, sc_domainScope),
145 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
147 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
148 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
149 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
150 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
152 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
153 #ifdef SLAP_CONTROL_X_TREE_DELETE
154 { LDAP_CONTROL_X_TREE_DELETE,
155 (int)offsetof(struct slap_control_ids, sc_treeDelete),
156 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
158 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
160 { LDAP_CONTROL_X_SEARCH_OPTIONS,
161 (int)offsetof(struct slap_control_ids, sc_searchOptions),
162 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
164 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
165 { LDAP_CONTROL_SUBENTRIES,
166 (int)offsetof(struct slap_control_ids, sc_subentries),
169 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
171 (int)offsetof(struct slap_control_ids, sc_noOp),
172 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
174 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
176 { LDAP_CONTROL_DONTUSECOPY,
177 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
178 SLAP_CTRL_INTROGATE|SLAP_CTRL_HIDE,
180 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
181 { LDAP_CONTROL_MANAGEDIT,
182 (int)offsetof(struct slap_control_ids, sc_manageDIT),
183 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
185 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
187 { LDAP_CONTROL_MANAGEDSAIT,
188 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
190 manageDSAit_extops, NULL,
191 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
192 { LDAP_CONTROL_PROXY_AUTHZ,
193 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
194 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
195 proxy_authz_extops, NULL,
196 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
197 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
201 * Register a supported control.
203 * This can be called by an OpenLDAP plugin or, indirectly, by a
204 * SLAPI plugin calling slapi_register_supported_control().
207 register_supported_control(const char *controloid,
208 slap_mask_t controlmask,
210 SLAP_CTRL_PARSE_FN *controlparsefn,
213 struct slap_control *sc;
216 if ( num_known_controls >= SLAP_MAX_CIDS ) {
217 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
218 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
219 SLAP_MAX_CIDS, 0, 0 );
223 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
225 /* sanity check - should never happen */
226 for ( i = 0; slap_known_controls[ i ]; i++ ) {
227 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
228 Debug( LDAP_DEBUG_ANY,
229 "Control %s already registered.\n",
231 return LDAP_PARAM_ERROR;
235 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
236 if ( sc == NULL ) return LDAP_NO_MEMORY;
238 sc->sc_oid = ch_strdup( controloid );
239 sc->sc_mask = controlmask;
240 if ( controlexops != NULL ) {
243 for ( i = 0; controlexops[ i ]; i++ );
245 sc->sc_extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
246 if ( sc->sc_extendedopsbv == NULL ) {
248 return LDAP_NO_MEMORY;
251 for ( i = 0; controlexops[ i ]; i++ ) {
252 ber_str2bv( controlexops[ i ], 0, 1, &sc->sc_extendedopsbv[ i ] );
256 sc->sc_extendedopsbv = NULL;
258 sc->sc_extendedops = NULL;
259 sc->sc_parse = controlparsefn;
261 if ( controlcid ) *controlcid = num_known_controls;
262 sc->sc_cid = num_known_controls;
264 /* Update slap_known_controls, too. */
265 slap_known_controls[num_known_controls-1] = sc->sc_oid;
266 slap_known_controls[num_known_controls++] = NULL;
268 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
269 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
274 * One-time initialization of internal controls.
277 slap_controls_init( void )
283 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
284 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
285 rc = register_supported_control( control_defs[i].sc_oid,
286 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
287 control_defs[i].sc_parse, cid );
288 if ( rc != LDAP_SUCCESS ) break;
295 * Free memory associated with list of supported controls.
298 controls_destroy( void )
300 struct slap_control *sc;
302 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
303 sc = LDAP_SLIST_FIRST(&controls_list);
304 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
306 ch_free( sc->sc_oid );
307 if ( sc->sc_extendedopsbv != NULL ) {
308 ber_bvarray_free( sc->sc_extendedopsbv );
315 * Format the supportedControl attribute of the root DSE,
316 * detailing which controls are supported by the directory
320 controls_root_dse_info( Entry *e )
322 AttributeDescription *ad_supportedControl
323 = slap_schema.si_ad_supportedControl;
324 struct berval vals[2];
325 struct slap_control *sc;
327 vals[1].bv_val = NULL;
330 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
331 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
333 vals[0].bv_val = sc->sc_oid;
334 vals[0].bv_len = strlen( sc->sc_oid );
336 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
345 * Return a list of OIDs and operation masks for supported
346 * controls. Used by SLAPI.
349 get_supported_controls(char ***ctrloidsp,
350 slap_mask_t **ctrlmasks)
355 struct slap_control *sc;
359 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
369 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
370 if ( oids == NULL ) {
371 return LDAP_NO_MEMORY;
373 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
374 if ( masks == NULL ) {
376 return LDAP_NO_MEMORY;
381 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
382 oids[n] = ch_strdup( sc->sc_oid );
383 masks[n] = sc->sc_mask;
396 * Find a control given its OID.
398 static struct slap_control *
399 find_ctrl( const char *oid )
401 struct slap_control *sc;
403 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
404 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
413 slap_find_control_id(
417 struct slap_control *ctrl = find_ctrl( oid );
419 if ( cid ) *cid = ctrl->sc_cid;
422 return LDAP_CONTROL_NOT_FOUND;
426 slap_global_control( Operation *op, const char *oid, int *cid )
428 struct slap_control *ctrl = find_ctrl( oid );
430 if ( ctrl == NULL ) {
431 /* should not be reachable */
432 Debug( LDAP_DEBUG_ANY,
433 "slap_global_control: unrecognized control: %s\n",
435 return LDAP_CONTROL_NOT_FOUND;
438 if ( cid ) *cid = ctrl->sc_cid;
440 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
441 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
442 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
444 return LDAP_COMPARE_TRUE;
447 Debug( LDAP_DEBUG_TRACE,
448 "slap_global_control: unavailable control: %s\n",
451 return LDAP_COMPARE_FALSE;
454 void slap_free_ctrls(
456 LDAPControl **ctrls )
460 for (i=0; ctrls[i]; i++) {
461 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
463 op->o_tmpfree( ctrls, op->o_tmpmemctx );
469 LDAPControl *control,
472 struct slap_control *sc;
474 sc = find_ctrl( control->ldctl_oid );
476 /* recognized control */
478 switch( op->o_tag ) {
480 tagmask = SLAP_CTRL_ADD;
483 tagmask = SLAP_CTRL_BIND;
485 case LDAP_REQ_COMPARE:
486 tagmask = SLAP_CTRL_COMPARE;
488 case LDAP_REQ_DELETE:
489 tagmask = SLAP_CTRL_DELETE;
491 case LDAP_REQ_MODIFY:
492 tagmask = SLAP_CTRL_MODIFY;
494 case LDAP_REQ_RENAME:
495 tagmask = SLAP_CTRL_RENAME;
497 case LDAP_REQ_SEARCH:
498 tagmask = SLAP_CTRL_SEARCH;
500 case LDAP_REQ_UNBIND:
501 tagmask = SLAP_CTRL_UNBIND;
503 case LDAP_REQ_ABANDON:
504 tagmask = SLAP_CTRL_ABANDON;
506 case LDAP_REQ_EXTENDED:
508 assert( op->ore_reqoid.bv_val != NULL );
509 if( sc->sc_extendedopsbv != NULL ) {
511 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
512 if( bvmatch( &op->ore_reqoid,
513 &sc->sc_extendedopsbv[i] ) )
522 *text = "controls internal error";
526 if (( sc->sc_mask & tagmask ) == tagmask ) {
527 /* available extension */
530 if( !sc->sc_parse ) {
531 *text = "not yet implemented";
535 rc = sc->sc_parse( op, rs, control );
537 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
541 } else if( control->ldctl_iscritical ) {
542 /* unavailable CRITICAL control */
543 *text = "critical extension is unavailable";
544 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
546 } else if( control->ldctl_iscritical ) {
547 /* unrecognized CRITICAL control */
548 *text = "critical extension is not recognized";
549 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
564 BerElement *ber = op->o_ber;
567 len = ber_pvt_ber_remaining(ber);
571 rs->sr_err = LDAP_SUCCESS;
575 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
576 if( tag == LBER_ERROR ) {
577 rs->sr_err = SLAPD_DISCONNECT;
578 rs->sr_text = "unexpected data in PDU";
584 Debug( LDAP_DEBUG_TRACE,
585 "=> get_ctrls\n", 0, 0, 0 );
587 if( op->o_protocol < LDAP_VERSION3 ) {
588 rs->sr_err = SLAPD_DISCONNECT;
589 rs->sr_text = "controls require LDAPv3";
593 /* one for first control, one for termination */
594 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
597 if( op->ctrls == NULL ) {
598 rs->sr_err = LDAP_NO_MEMORY;
599 rs->sr_text = "no memory";
604 op->o_ctrls[nctrls] = NULL;
606 /* step through each element */
607 for( tag = ber_first_element( ber, &len, &opaque );
609 tag = ber_next_element( ber, &len, opaque ) )
612 LDAPControl **tctrls;
614 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
615 memset(c, 0, sizeof(LDAPControl));
617 /* allocate pointer space for current controls (nctrls)
618 * + this control + extra NULL
620 tctrls = op->o_tmprealloc( op->o_ctrls,
621 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
624 if( tctrls == NULL ) {
626 ldap_controls_free(op->o_ctrls);
629 rs->sr_err = LDAP_NO_MEMORY;
630 rs->sr_text = "no memory";
634 op->o_ctrls = tctrls;
636 op->o_ctrls[nctrls++] = c;
637 op->o_ctrls[nctrls] = NULL;
639 tag = ber_scanf( ber, "{m" /*}*/, &bv );
640 c->ldctl_oid = bv.bv_val;
642 if( tag == LBER_ERROR ) {
643 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
646 slap_free_ctrls( op, op->o_ctrls );
648 rs->sr_err = SLAPD_DISCONNECT;
649 rs->sr_text = "decoding controls error";
652 } else if( c->ldctl_oid == NULL ) {
653 Debug( LDAP_DEBUG_TRACE,
654 "get_ctrls: conn %lu got emtpy OID.\n",
655 op->o_connid, 0, 0 );
657 slap_free_ctrls( op, op->o_ctrls );
659 rs->sr_err = LDAP_PROTOCOL_ERROR;
660 rs->sr_text = "OID field is empty";
664 tag = ber_peek_tag( ber, &len );
666 if( tag == LBER_BOOLEAN ) {
668 tag = ber_scanf( ber, "b", &crit );
670 if( tag == LBER_ERROR ) {
671 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
673 slap_free_ctrls( op, op->o_ctrls );
675 rs->sr_err = SLAPD_DISCONNECT;
676 rs->sr_text = "decoding controls error";
680 c->ldctl_iscritical = (crit != 0);
681 tag = ber_peek_tag( ber, &len );
684 if( tag == LBER_OCTETSTRING ) {
685 tag = ber_scanf( ber, "m", &c->ldctl_value );
687 if( tag == LBER_ERROR ) {
688 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
689 "%s (%scritical): get value failed.\n",
690 op->o_connid, c->ldctl_oid,
691 c->ldctl_iscritical ? "" : "non" );
692 slap_free_ctrls( op, op->o_ctrls );
694 rs->sr_err = SLAPD_DISCONNECT;
695 rs->sr_text = "decoding controls error";
700 Debug( LDAP_DEBUG_TRACE,
701 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
702 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
704 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
705 if ( rs->sr_err != LDAP_SUCCESS ) {
711 Debug( LDAP_DEBUG_TRACE,
712 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
713 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
715 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
716 if( rs->sr_err == SLAPD_DISCONNECT ) {
717 rs->sr_err = LDAP_PROTOCOL_ERROR;
718 send_ldap_disconnect( op, rs );
719 rs->sr_err = SLAPD_DISCONNECT;
721 send_ldap_result( op, rs );
733 BI_chk_controls fnc )
737 switch ( op->o_ctrlflag[ ctrl ] ) {
738 case SLAP_CONTROL_NONCRITICAL:
739 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
740 if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
741 slap_known_controls[ ctrl - 1 ] ) == 0 )
748 rs->sr_err = LDAP_OTHER;
756 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
759 AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
760 ( i - j ) * sizeof( LDAPControl * ) );
763 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
767 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
769 Debug( LDAP_DEBUG_ANY, "%s: "
770 "non-critical control \"%s\" not supported; stripped.\n",
771 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
774 case SLAP_CONTROL_IGNORED:
775 case SLAP_CONTROL_NONE:
776 rs->sr_err = SLAP_CB_CONTINUE;
779 case SLAP_CONTROL_CRITICAL:
780 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
784 Debug( LDAP_DEBUG_ANY, "%s: "
785 "critical control \"%s\" not supported.\n",
786 op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
790 /* handle all cases! */
798 static int parseDontUseCopy (
803 if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
804 rs->sr_text = "dontUseCopy control specified multiple times";
805 return LDAP_PROTOCOL_ERROR;
808 if ( ctrl->ldctl_value.bv_len ) {
809 rs->sr_text = "dontUseCopy control value not empty";
810 return LDAP_PROTOCOL_ERROR;
813 if ( ctrl->ldctl_iscritical != SLAP_CONTROL_CRITICAL ) {
814 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
815 return LDAP_PROTOCOL_ERROR;
818 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
822 static int parseManageDIT (
827 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
828 rs->sr_text = "manageDIT control specified multiple times";
829 return LDAP_PROTOCOL_ERROR;
832 if ( ctrl->ldctl_value.bv_len ) {
833 rs->sr_text = "manageDIT control value not empty";
834 return LDAP_PROTOCOL_ERROR;
837 op->o_managedit = ctrl->ldctl_iscritical
838 ? SLAP_CONTROL_CRITICAL
839 : SLAP_CONTROL_NONCRITICAL;
845 static int parseManageDSAit (
850 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
851 rs->sr_text = "manageDSAit control specified multiple times";
852 return LDAP_PROTOCOL_ERROR;
855 if ( ctrl->ldctl_value.bv_len ) {
856 rs->sr_text = "manageDSAit control value not empty";
857 return LDAP_PROTOCOL_ERROR;
860 op->o_managedsait = ctrl->ldctl_iscritical
861 ? SLAP_CONTROL_CRITICAL
862 : SLAP_CONTROL_NONCRITICAL;
867 static int parseProxyAuthz (
873 struct berval dn = BER_BVNULL;
875 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
876 rs->sr_text = "proxy authorization control specified multiple times";
877 return LDAP_PROTOCOL_ERROR;
880 if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
881 && BER_BVISEMPTY( &op->o_ndn ) )
883 rs->sr_text = "anonymous proxyAuthz not allowed";
884 return LDAP_PROXY_AUTHZ_FAILURE;
887 op->o_proxy_authz = ctrl->ldctl_iscritical
888 ? SLAP_CONTROL_CRITICAL
889 : SLAP_CONTROL_NONCRITICAL;
891 Debug( LDAP_DEBUG_ARGS,
892 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
894 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
897 if ( ctrl->ldctl_value.bv_len == 0 ) {
898 Debug( LDAP_DEBUG_TRACE,
899 "parseProxyAuthz: conn=%lu anonymous\n",
900 op->o_connid, 0, 0 );
903 /* FIXME: do we let anonymous authz as anonymous?
904 * should we disallow authz at all for anonymous? */
905 if ( !BER_BVISNULL( &op->o_ndn ) ) {
906 op->o_ndn.bv_val[ 0 ] = '\0';
908 op->o_ndn.bv_len = 0;
910 if ( !BER_BVISNULL( &op->o_dn ) ) {
911 op->o_dn.bv_val[ 0 ] = '\0';
918 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
919 NULL, &dn, SLAP_GETDN_AUTHZID );
921 /* FIXME: empty DN in proxyAuthz control should be legal... */
922 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
924 ch_free( dn.bv_val );
926 rs->sr_text = "authzId mapping failed";
927 return LDAP_PROXY_AUTHZ_FAILURE;
930 Debug( LDAP_DEBUG_TRACE,
931 "parseProxyAuthz: conn=%lu \"%s\"\n",
933 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
935 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
938 ch_free( dn.bv_val );
939 rs->sr_text = "not authorized to assume identity";
940 return LDAP_PROXY_AUTHZ_FAILURE;
943 ch_free( op->o_ndn.bv_val );
944 ch_free( op->o_dn.bv_val );
947 * NOTE: since slap_sasl_getdn() returns a normalized dn,
948 * from now on op->o_dn is normalized
951 ber_dupbv( &op->o_dn, &dn );
954 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
955 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
960 static int parseNoOp (
965 if ( op->o_noop != SLAP_CONTROL_NONE ) {
966 rs->sr_text = "noop control specified multiple times";
967 return LDAP_PROTOCOL_ERROR;
970 if ( ctrl->ldctl_value.bv_len ) {
971 rs->sr_text = "noop control value not empty";
972 return LDAP_PROTOCOL_ERROR;
975 op->o_noop = ctrl->ldctl_iscritical
976 ? SLAP_CONTROL_CRITICAL
977 : SLAP_CONTROL_NONCRITICAL;
982 static int parsePagedResults (
987 int rc = LDAP_SUCCESS;
991 struct berval cookie = BER_BVNULL;
992 PagedResultsState *ps;
994 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
995 rs->sr_text = "paged results control specified multiple times";
996 return LDAP_PROTOCOL_ERROR;
999 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1000 rs->sr_text = "paged results control value is empty (or absent)";
1001 return LDAP_PROTOCOL_ERROR;
1004 /* Parse the control value
1005 * realSearchControlValue ::= SEQUENCE {
1006 * size INTEGER (0..maxInt),
1007 * -- requested page size from client
1008 * -- result set size estimate from server
1009 * cookie OCTET STRING
1012 ber = ber_init( &ctrl->ldctl_value );
1013 if ( ber == NULL ) {
1014 rs->sr_text = "internal error";
1018 tag = ber_scanf( ber, "{im}", &size, &cookie );
1020 if ( tag == LBER_ERROR ) {
1021 rs->sr_text = "paged results control could not be decoded";
1022 rc = LDAP_PROTOCOL_ERROR;
1027 rs->sr_text = "paged results control size invalid";
1028 rc = LDAP_PROTOCOL_ERROR;
1032 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1033 *ps = op->o_conn->c_pagedresults_state;
1035 op->o_pagedresults_state = ps;
1037 /* NOTE: according to RFC 2696 3.:
1039 If the page size is greater than or equal to the sizeLimit value, the
1040 server should ignore the control as the request can be satisfied in a
1043 * NOTE: this assumes that the op->ors_slimit be set
1044 * before the controls are parsed.
1047 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1048 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1050 } else if ( ctrl->ldctl_iscritical ) {
1051 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1054 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1058 (void)ber_free( ber, 1 );
1063 static int parseSortedResults (
1068 int rc = LDAP_SUCCESS;
1070 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1071 rs->sr_text = "sorted results control specified multiple times";
1072 return LDAP_PROTOCOL_ERROR;
1075 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1076 rs->sr_text = "sorted results control value is empty (or absent)";
1077 return LDAP_PROTOCOL_ERROR;
1080 /* blow off parsing the value */
1082 op->o_sortedresults = ctrl->ldctl_iscritical
1083 ? SLAP_CONTROL_CRITICAL
1084 : SLAP_CONTROL_NONCRITICAL;
1090 static int parseAssert (
1096 struct berval fstr = BER_BVNULL;
1098 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1099 rs->sr_text = "assert control specified multiple times";
1100 return LDAP_PROTOCOL_ERROR;
1103 if ( ctrl->ldctl_value.bv_len == 0 ) {
1104 rs->sr_text = "assert control value is empty (or absent)";
1105 return LDAP_PROTOCOL_ERROR;
1108 ber = ber_init( &(ctrl->ldctl_value) );
1110 rs->sr_text = "assert control: internal error";
1114 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1116 if( rs->sr_err != LDAP_SUCCESS ) {
1117 if( rs->sr_err == SLAPD_DISCONNECT ) {
1118 rs->sr_err = LDAP_PROTOCOL_ERROR;
1119 send_ldap_disconnect( op, rs );
1120 rs->sr_err = SLAPD_DISCONNECT;
1122 send_ldap_result( op, rs );
1124 if( op->o_assertion != NULL ) {
1125 filter_free_x( op, op->o_assertion );
1131 filter2bv_x( op, op->o_assertion, &fstr );
1133 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1134 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1135 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1138 op->o_assert = ctrl->ldctl_iscritical
1139 ? SLAP_CONTROL_CRITICAL
1140 : SLAP_CONTROL_NONCRITICAL;
1142 rs->sr_err = LDAP_SUCCESS;
1143 return LDAP_SUCCESS;
1146 static int parsePreRead (
1151 ber_len_t siz, off, i;
1152 AttributeName *an = NULL;
1155 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1156 rs->sr_text = "preread control specified multiple times";
1157 return LDAP_PROTOCOL_ERROR;
1160 if ( ctrl->ldctl_value.bv_len == 0 ) {
1161 rs->sr_text = "preread control value is empty (or absent)";
1162 return LDAP_PROTOCOL_ERROR;
1165 ber = ber_init( &(ctrl->ldctl_value) );
1167 rs->sr_text = "preread control: internal error";
1171 siz = sizeof( AttributeName );
1172 off = offsetof( AttributeName, an_name );
1173 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1174 rs->sr_text = "preread control: decoding error";
1175 return LDAP_PROTOCOL_ERROR;
1178 for( i=0; i<siz; i++ ) {
1179 int rc = LDAP_SUCCESS;
1180 const char *dummy = NULL;
1182 an[i].an_desc = NULL;
1184 an[i].an_oc_exclude = 0;
1185 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1186 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1189 : "postread control: unknown attributeType";
1194 op->o_preread = ctrl->ldctl_iscritical
1195 ? SLAP_CONTROL_CRITICAL
1196 : SLAP_CONTROL_NONCRITICAL;
1198 op->o_preread_attrs = an;
1200 rs->sr_err = LDAP_SUCCESS;
1201 return LDAP_SUCCESS;
1204 static int parsePostRead (
1209 ber_len_t siz, off, i;
1210 AttributeName *an = NULL;
1213 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1214 rs->sr_text = "postread control specified multiple times";
1215 return LDAP_PROTOCOL_ERROR;
1218 if ( ctrl->ldctl_value.bv_len == 0 ) {
1219 rs->sr_text = "postread control value is empty (or absent)";
1220 return LDAP_PROTOCOL_ERROR;
1223 ber = ber_init( &(ctrl->ldctl_value) );
1225 rs->sr_text = "postread control: internal error";
1229 siz = sizeof( AttributeName );
1230 off = offsetof( AttributeName, an_name );
1231 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1232 rs->sr_text = "postread control: decoding error";
1233 return LDAP_PROTOCOL_ERROR;
1236 for( i=0; i<siz; i++ ) {
1237 int rc = LDAP_SUCCESS;
1238 const char *dummy = NULL;
1240 an[i].an_desc = NULL;
1242 an[i].an_oc_exclude = 0;
1243 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1244 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1247 : "postread control: unknown attributeType";
1252 op->o_postread = ctrl->ldctl_iscritical
1253 ? SLAP_CONTROL_CRITICAL
1254 : SLAP_CONTROL_NONCRITICAL;
1256 op->o_postread_attrs = an;
1258 rs->sr_err = LDAP_SUCCESS;
1259 return LDAP_SUCCESS;
1262 static int parseValuesReturnFilter (
1268 struct berval fstr = BER_BVNULL;
1270 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1271 rs->sr_text = "valuesReturnFilter control specified multiple times";
1272 return LDAP_PROTOCOL_ERROR;
1275 if ( ctrl->ldctl_value.bv_len == 0 ) {
1276 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1277 return LDAP_PROTOCOL_ERROR;
1280 ber = ber_init( &(ctrl->ldctl_value) );
1282 rs->sr_text = "internal error";
1286 rs->sr_err = get_vrFilter( op, ber,
1287 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1289 if( rs->sr_err != LDAP_SUCCESS ) {
1290 if( rs->sr_err == SLAPD_DISCONNECT ) {
1291 rs->sr_err = LDAP_PROTOCOL_ERROR;
1292 send_ldap_disconnect( op, rs );
1293 rs->sr_err = SLAPD_DISCONNECT;
1295 send_ldap_result( op, rs );
1297 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1301 vrFilter2bv( op, op->o_vrFilter, &fstr );
1304 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1305 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1306 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1309 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1310 ? SLAP_CONTROL_CRITICAL
1311 : SLAP_CONTROL_NONCRITICAL;
1313 rs->sr_err = LDAP_SUCCESS;
1314 return LDAP_SUCCESS;
1317 static int parseSubentries (
1322 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1323 rs->sr_text = "subentries control specified multiple times";
1324 return LDAP_PROTOCOL_ERROR;
1327 /* FIXME: should use BER library */
1328 if( ( ctrl->ldctl_value.bv_len != 3 )
1329 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1330 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1332 rs->sr_text = "subentries control value encoding is bogus";
1333 return LDAP_PROTOCOL_ERROR;
1336 op->o_subentries = ctrl->ldctl_iscritical
1337 ? SLAP_CONTROL_CRITICAL
1338 : SLAP_CONTROL_NONCRITICAL;
1340 if (ctrl->ldctl_value.bv_val[2]) {
1341 set_subentries_visibility( op );
1344 return LDAP_SUCCESS;
1347 static int parsePermissiveModify (
1352 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1353 rs->sr_text = "permissiveModify control specified multiple times";
1354 return LDAP_PROTOCOL_ERROR;
1357 if ( ctrl->ldctl_value.bv_len ) {
1358 rs->sr_text = "permissiveModify control value not empty";
1359 return LDAP_PROTOCOL_ERROR;
1362 op->o_permissive_modify = ctrl->ldctl_iscritical
1363 ? SLAP_CONTROL_CRITICAL
1364 : SLAP_CONTROL_NONCRITICAL;
1366 return LDAP_SUCCESS;
1369 static int parseDomainScope (
1374 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1375 rs->sr_text = "domainScope control specified multiple times";
1376 return LDAP_PROTOCOL_ERROR;
1379 if ( ctrl->ldctl_value.bv_len ) {
1380 rs->sr_text = "domainScope control value not empty";
1381 return LDAP_PROTOCOL_ERROR;
1384 op->o_domain_scope = ctrl->ldctl_iscritical
1385 ? SLAP_CONTROL_CRITICAL
1386 : SLAP_CONTROL_NONCRITICAL;
1388 return LDAP_SUCCESS;
1391 #ifdef SLAP_CONTROL_X_TREE_DELETE
1392 static int parseTreeDelete (
1397 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1398 rs->sr_text = "treeDelete control specified multiple times";
1399 return LDAP_PROTOCOL_ERROR;
1402 if ( ctrl->ldctl_value.bv_len ) {
1403 rs->sr_text = "treeDelete control value not empty";
1404 return LDAP_PROTOCOL_ERROR;
1407 op->o_tree_delete = ctrl->ldctl_iscritical
1408 ? SLAP_CONTROL_CRITICAL
1409 : SLAP_CONTROL_NONCRITICAL;
1411 return LDAP_SUCCESS;
1415 static int parseSearchOptions (
1421 ber_int_t search_flags;
1424 if ( ctrl->ldctl_value.bv_len == 0 ) {
1425 rs->sr_text = "searchOptions control value is empty (or absent)";
1426 return LDAP_PROTOCOL_ERROR;
1429 ber = ber_init( &ctrl->ldctl_value );
1431 rs->sr_text = "internal error";
1435 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1436 rs->sr_text = "searchOptions control decoding error";
1437 return LDAP_PROTOCOL_ERROR;
1440 (void) ber_free( ber, 1 );
1442 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1443 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1444 rs->sr_text = "searchOptions control specified multiple times "
1445 "or with domainScope control";
1446 return LDAP_PROTOCOL_ERROR;
1449 op->o_domain_scope = ctrl->ldctl_iscritical
1450 ? SLAP_CONTROL_CRITICAL
1451 : SLAP_CONTROL_NONCRITICAL;
1454 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1455 /* Other search flags not recognised so far,
1457 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1459 rs->sr_text = "searchOptions contained unrecognized flag";
1460 return LDAP_UNWILLING_TO_PERFORM;
1463 return LDAP_SUCCESS;