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 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
36 static SLAP_CTRL_PARSE_FN parseModifyIncrement;
38 static SLAP_CTRL_PARSE_FN parseNoOp;
39 static SLAP_CTRL_PARSE_FN parsePagedResults;
41 static SLAP_CTRL_PARSE_FN parseSortedResults;
43 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
44 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
45 static SLAP_CTRL_PARSE_FN parseDomainScope;
46 #ifdef SLAP_CONTROL_X_TREE_DELETE
47 static SLAP_CTRL_PARSE_FN parseTreeDelete;
49 static SLAP_CTRL_PARSE_FN parseSearchOptions;
50 static SLAP_CTRL_PARSE_FN parseSubentries;
52 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
54 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
55 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
57 struct slap_control_ids slap_cids;
63 /* The controlID for this control */
66 /* Operations supported by control */
69 /* Extended operations supported by control */
70 char **sc_extendedops;
72 /* Control parsing callback */
73 SLAP_CTRL_PARSE_FN *sc_parse;
75 LDAP_SLIST_ENTRY(slap_control) sc_next;
78 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
79 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
82 * all known request control OIDs should be added to this list
85 * NOTE: initialize num_known_controls to 1 so that cid = 0 always
86 * addresses an undefined control; this allows to safely test for
87 * well known controls even if they are not registered, e.g. if
88 * they get moved to modules. An example is sc_LDAPsync, which
89 * is implemented in the syncprov overlay and thus, if configured
90 * as dynamic module, may not be registered. One side effect is that
91 * slap_known_controls[0] == NULL, so it should always be used
93 * FIXME: should we define the "undefined control" oid?
95 char *slap_known_controls[SLAP_MAX_CIDS+1];
96 static int num_known_controls = 1;
98 static char *proxy_authz_extops[] = {
99 LDAP_EXOP_MODIFY_PASSWD,
100 LDAP_EXOP_X_WHO_AM_I,
104 static struct slap_control control_defs[] = {
105 { LDAP_CONTROL_ASSERT,
106 (int)offsetof(struct slap_control_ids, sc_assert),
107 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
108 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
109 { LDAP_CONTROL_PRE_READ,
110 (int)offsetof(struct slap_control_ids, sc_preRead),
111 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
112 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
113 { LDAP_CONTROL_POST_READ,
114 (int)offsetof(struct slap_control_ids, sc_postRead),
115 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
116 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
117 { LDAP_CONTROL_VALUESRETURNFILTER,
118 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
119 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
120 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
121 { LDAP_CONTROL_PAGEDRESULTS,
122 (int)offsetof(struct slap_control_ids, sc_pagedResults),
123 SLAP_CTRL_SEARCH, NULL,
124 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
126 { LDAP_CONTROL_SORTREQUEST,
127 (int)offsetof(struct slap_control_ids, sc_sortedResults),
128 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE, NULL,
129 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
131 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
132 { LDAP_CONTROL_X_DOMAIN_SCOPE,
133 (int)offsetof(struct slap_control_ids, sc_domainScope),
134 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
135 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
137 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
138 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
139 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
140 SLAP_CTRL_MODIFY, NULL,
141 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
143 #ifdef SLAP_CONTROL_X_TREE_DELETE
144 { LDAP_CONTROL_X_TREE_DELETE,
145 (int)offsetof(struct slap_control_ids, sc_treeDelete),
146 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
147 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
150 { LDAP_CONTROL_X_SEARCH_OPTIONS,
151 (int)offsetof(struct slap_control_ids, sc_searchOptions),
152 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
153 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
155 #ifdef LDAP_CONTROL_SUBENTRIES
156 { LDAP_CONTROL_SUBENTRIES,
157 (int)offsetof(struct slap_control_ids, sc_subentries),
158 SLAP_CTRL_SEARCH, NULL,
159 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
162 (int)offsetof(struct slap_control_ids, sc_noOp),
163 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
164 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
165 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
166 { LDAP_CONTROL_MODIFY_INCREMENT,
167 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
168 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
169 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
172 { LDAP_CONTROL_MANAGEDIT,
173 (int)offsetof(struct slap_control_ids, sc_manageDIT),
174 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE, NULL,
175 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
177 { LDAP_CONTROL_MANAGEDSAIT,
178 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
179 SLAP_CTRL_ACCESS, NULL,
180 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
181 { LDAP_CONTROL_PROXY_AUTHZ,
182 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
183 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS, proxy_authz_extops,
184 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
185 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
189 * Register a supported control.
191 * This can be called by an OpenLDAP plugin or, indirectly, by a
192 * SLAPI plugin calling slapi_register_supported_control().
195 register_supported_control(const char *controloid,
196 slap_mask_t controlmask,
198 SLAP_CTRL_PARSE_FN *controlparsefn,
201 struct slap_control *sc;
204 if ( num_known_controls >= SLAP_MAX_CIDS ) {
205 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
206 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
207 SLAP_MAX_CIDS, 0, 0 );
211 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
213 /* sanity check - should never happen */
214 for ( i = 0; slap_known_controls[ i ]; i++ ) {
215 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
216 Debug( LDAP_DEBUG_ANY,
217 "Control %s already registered.\n",
219 return LDAP_PARAM_ERROR;
223 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
224 if ( sc == NULL ) return LDAP_NO_MEMORY;
226 sc->sc_oid = ch_strdup( controloid );
227 sc->sc_mask = controlmask;
228 if ( controlexops != NULL ) {
229 sc->sc_extendedops = ldap_charray_dup( controlexops );
230 if ( sc->sc_extendedops == NULL ) {
232 return LDAP_NO_MEMORY;
235 sc->sc_extendedops = NULL;
237 sc->sc_parse = controlparsefn;
239 if ( controlcid ) *controlcid = num_known_controls;
240 sc->sc_cid = num_known_controls;
242 /* Update slap_known_controls, too. */
243 slap_known_controls[num_known_controls-1] = sc->sc_oid;
244 slap_known_controls[num_known_controls++] = NULL;
246 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
247 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
252 * One-time initialization of internal controls.
255 slap_controls_init( void )
261 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
262 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
263 rc = register_supported_control( control_defs[i].sc_oid,
264 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
265 control_defs[i].sc_parse, cid );
266 if ( rc != LDAP_SUCCESS ) break;
273 * Free memory associated with list of supported controls.
276 controls_destroy( void )
278 struct slap_control *sc;
280 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
281 sc = LDAP_SLIST_FIRST(&controls_list);
282 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
284 ch_free( sc->sc_oid );
285 if ( sc->sc_extendedops != NULL ) {
286 ldap_charray_free( sc->sc_extendedops );
293 * Format the supportedControl attribute of the root DSE,
294 * detailing which controls are supported by the directory
298 controls_root_dse_info( Entry *e )
300 AttributeDescription *ad_supportedControl
301 = slap_schema.si_ad_supportedControl;
302 struct berval vals[2];
303 struct slap_control *sc;
305 vals[1].bv_val = NULL;
308 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
309 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
311 vals[0].bv_val = sc->sc_oid;
312 vals[0].bv_len = strlen( sc->sc_oid );
314 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
323 * Return a list of OIDs and operation masks for supported
324 * controls. Used by SLAPI.
327 get_supported_controls(char ***ctrloidsp,
328 slap_mask_t **ctrlmasks)
333 struct slap_control *sc;
337 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
347 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
348 if ( oids == NULL ) {
349 return LDAP_NO_MEMORY;
351 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
352 if ( masks == NULL ) {
354 return LDAP_NO_MEMORY;
359 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
360 oids[n] = ch_strdup( sc->sc_oid );
361 masks[n] = sc->sc_mask;
374 * Find a control given its OID.
376 static struct slap_control *
377 find_ctrl( const char *oid )
379 struct slap_control *sc;
381 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
382 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
391 slap_find_control_id(
395 struct slap_control *ctrl = find_ctrl( oid );
397 if ( cid ) *cid = ctrl->sc_cid;
400 return LDAP_CONTROL_NOT_FOUND;
404 slap_global_control( Operation *op, const char *oid, int *cid )
406 struct slap_control *ctrl = find_ctrl( oid );
408 if ( ctrl == NULL ) {
409 /* should not be reachable */
410 Debug( LDAP_DEBUG_ANY,
411 "slap_global_control: unrecognized control: %s\n",
413 return LDAP_CONTROL_NOT_FOUND;
416 if ( cid ) *cid = ctrl->sc_cid;
418 if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
419 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
420 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
422 return LDAP_COMPARE_TRUE;
425 Debug( LDAP_DEBUG_TRACE,
426 "slap_global_control: unavailable control: %s\n",
429 return LDAP_COMPARE_FALSE;
432 void slap_free_ctrls(
434 LDAPControl **ctrls )
438 for (i=0; ctrls[i]; i++) {
439 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
441 op->o_tmpfree( ctrls, op->o_tmpmemctx );
453 BerElement *ber = op->o_ber;
454 struct slap_control *sc;
457 len = ber_pvt_ber_remaining(ber);
461 rs->sr_err = LDAP_SUCCESS;
465 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
466 if( tag == LBER_ERROR ) {
467 rs->sr_err = SLAPD_DISCONNECT;
468 rs->sr_text = "unexpected data in PDU";
474 Debug( LDAP_DEBUG_TRACE,
475 "=> get_ctrls\n", 0, 0, 0 );
477 if( op->o_protocol < LDAP_VERSION3 ) {
478 rs->sr_err = SLAPD_DISCONNECT;
479 rs->sr_text = "controls require LDAPv3";
483 /* one for first control, one for termination */
484 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
487 if( op->ctrls == NULL ) {
488 rs->sr_err = LDAP_NO_MEMORY;
489 rs->sr_text = "no memory";
494 op->o_ctrls[nctrls] = NULL;
496 /* step through each element */
497 for( tag = ber_first_element( ber, &len, &opaque );
499 tag = ber_next_element( ber, &len, opaque ) )
502 LDAPControl **tctrls;
504 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
505 memset(c, 0, sizeof(LDAPControl));
507 /* allocate pointer space for current controls (nctrls)
508 * + this control + extra NULL
510 tctrls = op->o_tmprealloc( op->o_ctrls,
511 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
514 if( tctrls == NULL ) {
516 ldap_controls_free(op->o_ctrls);
519 rs->sr_err = LDAP_NO_MEMORY;
520 rs->sr_text = "no memory";
524 op->o_ctrls = tctrls;
526 op->o_ctrls[nctrls++] = c;
527 op->o_ctrls[nctrls] = NULL;
529 tag = ber_scanf( ber, "{m" /*}*/, &bv );
530 c->ldctl_oid = bv.bv_val;
532 if( tag == LBER_ERROR ) {
533 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
536 slap_free_ctrls( op, op->o_ctrls );
538 rs->sr_err = SLAPD_DISCONNECT;
539 rs->sr_text = "decoding controls error";
542 } else if( c->ldctl_oid == NULL ) {
543 Debug( LDAP_DEBUG_TRACE,
544 "get_ctrls: conn %lu got emtpy OID.\n",
545 op->o_connid, 0, 0 );
547 slap_free_ctrls( op, op->o_ctrls );
549 rs->sr_err = LDAP_PROTOCOL_ERROR;
550 rs->sr_text = "OID field is empty";
554 tag = ber_peek_tag( ber, &len );
556 if( tag == LBER_BOOLEAN ) {
558 tag = ber_scanf( ber, "b", &crit );
560 if( tag == LBER_ERROR ) {
561 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
563 slap_free_ctrls( op, op->o_ctrls );
565 rs->sr_err = SLAPD_DISCONNECT;
566 rs->sr_text = "decoding controls error";
570 c->ldctl_iscritical = (crit != 0);
571 tag = ber_peek_tag( ber, &len );
574 if( tag == LBER_OCTETSTRING ) {
575 tag = ber_scanf( ber, "m", &c->ldctl_value );
577 if( tag == LBER_ERROR ) {
578 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
579 "%s (%scritical): get value failed.\n",
580 op->o_connid, c->ldctl_oid,
581 c->ldctl_iscritical ? "" : "non" );
582 slap_free_ctrls( op, op->o_ctrls );
584 rs->sr_err = SLAPD_DISCONNECT;
585 rs->sr_text = "decoding controls error";
590 Debug( LDAP_DEBUG_TRACE,
591 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
592 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
594 sc = find_ctrl( c->ldctl_oid );
596 /* recognized control */
598 switch( op->o_tag ) {
600 tagmask = SLAP_CTRL_ADD;
603 tagmask = SLAP_CTRL_BIND;
605 case LDAP_REQ_COMPARE:
606 tagmask = SLAP_CTRL_COMPARE;
608 case LDAP_REQ_DELETE:
609 tagmask = SLAP_CTRL_DELETE;
611 case LDAP_REQ_MODIFY:
612 tagmask = SLAP_CTRL_MODIFY;
614 case LDAP_REQ_RENAME:
615 tagmask = SLAP_CTRL_RENAME;
617 case LDAP_REQ_SEARCH:
618 tagmask = SLAP_CTRL_SEARCH;
620 case LDAP_REQ_UNBIND:
621 tagmask = SLAP_CTRL_UNBIND;
623 case LDAP_REQ_ABANDON:
624 tagmask = SLAP_CTRL_ABANDON;
626 case LDAP_REQ_EXTENDED:
628 assert( op->ore_reqoid.bv_val != NULL );
629 if( sc->sc_extendedops != NULL ) {
631 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
632 if( strcmp( op->ore_reqoid.bv_val,
633 sc->sc_extendedops[i] ) == 0 )
642 rs->sr_err = LDAP_OTHER;
643 rs->sr_text = "controls internal error";
647 if (( sc->sc_mask & tagmask ) == tagmask ) {
648 /* available extension */
651 if( !sc->sc_parse ) {
652 rs->sr_err = LDAP_OTHER;
653 rs->sr_text = "not yet implemented";
657 rc = sc->sc_parse( op, rs, c );
659 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
664 } else if( c->ldctl_iscritical ) {
665 /* unavailable CRITICAL control */
666 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
667 rs->sr_text = "critical extension is unavailable";
671 } else if( c->ldctl_iscritical ) {
672 /* unrecognized CRITICAL control */
673 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
674 rs->sr_text = "critical extension is not recognized";
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 );
697 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
698 static int parseModifyIncrement (
704 if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
705 rs->sr_text = "modifyIncrement control specified multiple times";
706 return LDAP_PROTOCOL_ERROR;
710 if ( ctrl->ldctl_value.bv_len ) {
711 rs->sr_text = "modifyIncrement control value not empty";
712 return LDAP_PROTOCOL_ERROR;
716 op->o_modifyIncrement = ctrl->ldctl_iscritical
717 ? SLAP_CONTROL_CRITICAL
718 : SLAP_CONTROL_NONCRITICAL;
726 static int parseManageDIT (
731 if ( op->o_managedit != SLAP_CONTROL_NONE ) {
732 rs->sr_text = "manageDIT control specified multiple times";
733 return LDAP_PROTOCOL_ERROR;
736 if ( ctrl->ldctl_value.bv_len ) {
737 rs->sr_text = "manageDIT control value not empty";
738 return LDAP_PROTOCOL_ERROR;
741 op->o_managedit = ctrl->ldctl_iscritical
742 ? SLAP_CONTROL_CRITICAL
743 : SLAP_CONTROL_NONCRITICAL;
749 static int parseManageDSAit (
754 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
755 rs->sr_text = "manageDSAit control specified multiple times";
756 return LDAP_PROTOCOL_ERROR;
759 if ( ctrl->ldctl_value.bv_len ) {
760 rs->sr_text = "manageDSAit control value not empty";
761 return LDAP_PROTOCOL_ERROR;
764 op->o_managedsait = ctrl->ldctl_iscritical
765 ? SLAP_CONTROL_CRITICAL
766 : SLAP_CONTROL_NONCRITICAL;
771 static int parseProxyAuthz (
777 struct berval dn = BER_BVNULL;
779 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
780 rs->sr_text = "proxy authorization control specified multiple times";
781 return LDAP_PROTOCOL_ERROR;
784 op->o_proxy_authz = ctrl->ldctl_iscritical
785 ? SLAP_CONTROL_CRITICAL
786 : SLAP_CONTROL_NONCRITICAL;
788 Debug( LDAP_DEBUG_ARGS,
789 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
791 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
794 if ( ctrl->ldctl_value.bv_len == 0 ) {
795 Debug( LDAP_DEBUG_TRACE,
796 "parseProxyAuthz: conn=%lu anonymous\n",
797 op->o_connid, 0, 0 );
800 op->o_ndn.bv_val[ 0 ] = '\0';
801 op->o_ndn.bv_len = 0;
803 op->o_dn.bv_val[ 0 ] = '\0';
809 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
810 NULL, &dn, SLAP_GETDN_AUTHZID );
812 /* FIXME: empty DN in proxyAuthz control should be legal... */
813 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
815 ch_free( dn.bv_val );
817 rs->sr_text = "authzId mapping failed";
818 return LDAP_PROXY_AUTHZ_FAILURE;
821 Debug( LDAP_DEBUG_TRACE,
822 "parseProxyAuthz: conn=%lu \"%s\"\n",
824 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
826 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
829 ch_free( dn.bv_val );
830 rs->sr_text = "not authorized to assume identity";
831 return LDAP_PROXY_AUTHZ_FAILURE;
834 ch_free( op->o_ndn.bv_val );
835 ch_free( op->o_dn.bv_val );
838 * NOTE: since slap_sasl_getdn() returns a normalized dn,
839 * from now on op->o_dn is normalized
842 ber_dupbv( &op->o_dn, &dn );
845 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
846 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
851 static int parseNoOp (
856 if ( op->o_noop != SLAP_CONTROL_NONE ) {
857 rs->sr_text = "noop control specified multiple times";
858 return LDAP_PROTOCOL_ERROR;
861 if ( ctrl->ldctl_value.bv_len ) {
862 rs->sr_text = "noop control value not empty";
863 return LDAP_PROTOCOL_ERROR;
866 op->o_noop = ctrl->ldctl_iscritical
867 ? SLAP_CONTROL_CRITICAL
868 : SLAP_CONTROL_NONCRITICAL;
873 static int parsePagedResults (
878 int rc = LDAP_SUCCESS;
882 struct berval cookie = BER_BVNULL;
883 PagedResultsState *ps;
885 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
886 rs->sr_text = "paged results control specified multiple times";
887 return LDAP_PROTOCOL_ERROR;
890 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
891 rs->sr_text = "paged results control value is empty (or absent)";
892 return LDAP_PROTOCOL_ERROR;
895 /* Parse the control value
896 * realSearchControlValue ::= SEQUENCE {
897 * size INTEGER (0..maxInt),
898 * -- requested page size from client
899 * -- result set size estimate from server
900 * cookie OCTET STRING
903 ber = ber_init( &ctrl->ldctl_value );
905 rs->sr_text = "internal error";
909 tag = ber_scanf( ber, "{im}", &size, &cookie );
911 if ( tag == LBER_ERROR ) {
912 rs->sr_text = "paged results control could not be decoded";
913 rc = LDAP_PROTOCOL_ERROR;
918 rs->sr_text = "paged results control size invalid";
919 rc = LDAP_PROTOCOL_ERROR;
924 /* defer cookie decoding/checks to backend... */
925 if ( cookie.bv_len ) {
926 PagedResultsCookie reqcookie;
927 if( cookie.bv_len != sizeof( reqcookie ) ) {
929 rs->sr_text = "paged results cookie is invalid";
930 rc = LDAP_PROTOCOL_ERROR;
934 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
936 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
938 rs->sr_text = "paged results cookie is invalid";
939 rc = LDAP_PROTOCOL_ERROR;
942 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
943 rs->sr_text = "paged results cookie is invalid or old";
944 rc = LDAP_UNWILLING_TO_PERFORM;
949 /* Initial request. Initialize state. */
951 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
952 /* There's another pagedResults control on the
953 * same connection; reject new pagedResults controls
954 * (allowed by RFC2696) */
955 rs->sr_text = "paged results cookie unavailable; try later";
956 rc = LDAP_UNWILLING_TO_PERFORM;
960 op->o_pagedresults_state.ps_cookie = 0;
961 op->o_pagedresults_state.ps_count = 0;
965 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
966 *ps = op->o_conn->c_pagedresults_state;
968 op->o_pagedresults_state = ps;
970 /* NOTE: according to RFC 2696 3.:
972 If the page size is greater than or equal to the sizeLimit value, the
973 server should ignore the control as the request can be satisfied in a
976 * NOTE: this assumes that the op->ors_slimit be set
977 * before the controls are parsed.
980 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
981 op->o_pagedresults = SLAP_CONTROL_IGNORED;
983 } else if ( ctrl->ldctl_iscritical ) {
984 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
987 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
991 (void)ber_free( ber, 1 );
996 static int parseSortedResults (
1001 int rc = LDAP_SUCCESS;
1003 if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1004 rs->sr_text = "sorted results control specified multiple times";
1005 return LDAP_PROTOCOL_ERROR;
1008 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1009 rs->sr_text = "sorted results control value is empty (or absent)";
1010 return LDAP_PROTOCOL_ERROR;
1013 /* blow off parsing the value */
1015 op->o_sortedresults = ctrl->ldctl_iscritical
1016 ? SLAP_CONTROL_CRITICAL
1017 : SLAP_CONTROL_NONCRITICAL;
1023 static int parseAssert (
1029 struct berval fstr = BER_BVNULL;
1031 if ( op->o_assert != SLAP_CONTROL_NONE ) {
1032 rs->sr_text = "assert control specified multiple times";
1033 return LDAP_PROTOCOL_ERROR;
1036 if ( ctrl->ldctl_value.bv_len == 0 ) {
1037 rs->sr_text = "assert control value is empty (or absent)";
1038 return LDAP_PROTOCOL_ERROR;
1041 ber = ber_init( &(ctrl->ldctl_value) );
1043 rs->sr_text = "assert control: internal error";
1047 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
1049 if( rs->sr_err != LDAP_SUCCESS ) {
1050 if( rs->sr_err == SLAPD_DISCONNECT ) {
1051 rs->sr_err = LDAP_PROTOCOL_ERROR;
1052 send_ldap_disconnect( op, rs );
1053 rs->sr_err = SLAPD_DISCONNECT;
1055 send_ldap_result( op, rs );
1057 if( op->o_assertion != NULL ) {
1058 filter_free_x( op, op->o_assertion );
1064 filter2bv_x( op, op->o_assertion, &fstr );
1066 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1067 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1068 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1071 op->o_assert = ctrl->ldctl_iscritical
1072 ? SLAP_CONTROL_CRITICAL
1073 : SLAP_CONTROL_NONCRITICAL;
1075 rs->sr_err = LDAP_SUCCESS;
1076 return LDAP_SUCCESS;
1079 static int parsePreRead (
1084 ber_len_t siz, off, i;
1085 AttributeName *an = NULL;
1088 if ( op->o_preread != SLAP_CONTROL_NONE ) {
1089 rs->sr_text = "preread control specified multiple times";
1090 return LDAP_PROTOCOL_ERROR;
1093 if ( ctrl->ldctl_value.bv_len == 0 ) {
1094 rs->sr_text = "preread control value is empty (or absent)";
1095 return LDAP_PROTOCOL_ERROR;
1098 ber = ber_init( &(ctrl->ldctl_value) );
1100 rs->sr_text = "preread control: internal error";
1104 siz = sizeof( AttributeName );
1105 off = offsetof( AttributeName, an_name );
1106 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1107 rs->sr_text = "preread control: decoding error";
1108 return LDAP_PROTOCOL_ERROR;
1111 for( i=0; i<siz; i++ ) {
1112 int rc = LDAP_SUCCESS;
1113 const char *dummy = NULL;
1115 an[i].an_desc = NULL;
1117 an[i].an_oc_exclude = 0;
1118 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1119 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1122 : "postread control: unknown attributeType";
1127 op->o_preread = ctrl->ldctl_iscritical
1128 ? SLAP_CONTROL_CRITICAL
1129 : SLAP_CONTROL_NONCRITICAL;
1131 op->o_preread_attrs = an;
1133 rs->sr_err = LDAP_SUCCESS;
1134 return LDAP_SUCCESS;
1137 static int parsePostRead (
1142 ber_len_t siz, off, i;
1143 AttributeName *an = NULL;
1146 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1147 rs->sr_text = "postread control specified multiple times";
1148 return LDAP_PROTOCOL_ERROR;
1151 if ( ctrl->ldctl_value.bv_len == 0 ) {
1152 rs->sr_text = "postread control value is empty (or absent)";
1153 return LDAP_PROTOCOL_ERROR;
1156 ber = ber_init( &(ctrl->ldctl_value) );
1158 rs->sr_text = "postread control: internal error";
1162 siz = sizeof( AttributeName );
1163 off = offsetof( AttributeName, an_name );
1164 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1165 rs->sr_text = "postread control: decoding error";
1166 return LDAP_PROTOCOL_ERROR;
1169 for( i=0; i<siz; i++ ) {
1170 int rc = LDAP_SUCCESS;
1171 const char *dummy = NULL;
1173 an[i].an_desc = NULL;
1175 an[i].an_oc_exclude = 0;
1176 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1177 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1180 : "postread control: unknown attributeType";
1185 op->o_postread = ctrl->ldctl_iscritical
1186 ? SLAP_CONTROL_CRITICAL
1187 : SLAP_CONTROL_NONCRITICAL;
1189 op->o_postread_attrs = an;
1191 rs->sr_err = LDAP_SUCCESS;
1192 return LDAP_SUCCESS;
1195 int parseValuesReturnFilter (
1201 struct berval fstr = BER_BVNULL;
1203 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1204 rs->sr_text = "valuesReturnFilter control specified multiple times";
1205 return LDAP_PROTOCOL_ERROR;
1208 if ( ctrl->ldctl_value.bv_len == 0 ) {
1209 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1210 return LDAP_PROTOCOL_ERROR;
1213 ber = ber_init( &(ctrl->ldctl_value) );
1215 rs->sr_text = "internal error";
1219 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1221 if( rs->sr_err != LDAP_SUCCESS ) {
1222 if( rs->sr_err == SLAPD_DISCONNECT ) {
1223 rs->sr_err = LDAP_PROTOCOL_ERROR;
1224 send_ldap_disconnect( op, rs );
1225 rs->sr_err = SLAPD_DISCONNECT;
1227 send_ldap_result( op, rs );
1229 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1233 vrFilter2bv( op, op->o_vrFilter, &fstr );
1236 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1237 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1238 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1241 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1242 ? SLAP_CONTROL_CRITICAL
1243 : SLAP_CONTROL_NONCRITICAL;
1245 rs->sr_err = LDAP_SUCCESS;
1246 return LDAP_SUCCESS;
1249 #ifdef LDAP_CONTROL_SUBENTRIES
1250 static int parseSubentries (
1255 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1256 rs->sr_text = "subentries control specified multiple times";
1257 return LDAP_PROTOCOL_ERROR;
1260 /* FIXME: should use BER library */
1261 if( ( ctrl->ldctl_value.bv_len != 3 )
1262 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1263 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1265 rs->sr_text = "subentries control value encoding is bogus";
1266 return LDAP_PROTOCOL_ERROR;
1269 op->o_subentries = ctrl->ldctl_iscritical
1270 ? SLAP_CONTROL_CRITICAL
1271 : SLAP_CONTROL_NONCRITICAL;
1273 if (ctrl->ldctl_value.bv_val[2]) {
1274 set_subentries_visibility( op );
1277 return LDAP_SUCCESS;
1281 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1282 static int parsePermissiveModify (
1287 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1288 rs->sr_text = "permissiveModify control specified multiple times";
1289 return LDAP_PROTOCOL_ERROR;
1292 if ( ctrl->ldctl_value.bv_len ) {
1293 rs->sr_text = "permissiveModify control value not empty";
1294 return LDAP_PROTOCOL_ERROR;
1297 op->o_permissive_modify = ctrl->ldctl_iscritical
1298 ? SLAP_CONTROL_CRITICAL
1299 : SLAP_CONTROL_NONCRITICAL;
1301 return LDAP_SUCCESS;
1305 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1306 static int parseDomainScope (
1311 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1312 rs->sr_text = "domainScope control specified multiple times";
1313 return LDAP_PROTOCOL_ERROR;
1316 if ( ctrl->ldctl_value.bv_len ) {
1317 rs->sr_text = "domainScope control value not empty";
1318 return LDAP_PROTOCOL_ERROR;
1321 op->o_domain_scope = ctrl->ldctl_iscritical
1322 ? SLAP_CONTROL_CRITICAL
1323 : SLAP_CONTROL_NONCRITICAL;
1325 return LDAP_SUCCESS;
1329 #ifdef SLAP_CONTROL_X_TREE_DELETE
1330 static int parseTreeDelete (
1335 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1336 rs->sr_text = "treeDelete control specified multiple times";
1337 return LDAP_PROTOCOL_ERROR;
1340 if ( ctrl->ldctl_value.bv_len ) {
1341 rs->sr_text = "treeDelete control value not empty";
1342 return LDAP_PROTOCOL_ERROR;
1345 op->o_tree_delete = ctrl->ldctl_iscritical
1346 ? SLAP_CONTROL_CRITICAL
1347 : SLAP_CONTROL_NONCRITICAL;
1349 return LDAP_SUCCESS;
1353 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
1354 static int parseSearchOptions (
1360 ber_int_t search_flags;
1363 if ( ctrl->ldctl_value.bv_len == 0 ) {
1364 rs->sr_text = "searchOptions control value not empty";
1365 return LDAP_PROTOCOL_ERROR;
1368 ber = ber_init( &ctrl->ldctl_value );
1370 rs->sr_text = "internal error";
1374 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1375 rs->sr_text = "searchOptions control decoding error";
1376 return LDAP_PROTOCOL_ERROR;
1379 (void) ber_free( ber, 1 );
1381 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1382 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1383 rs->sr_text = "searchOptions control specified multiple times "
1384 "or with domainScope control";
1385 return LDAP_PROTOCOL_ERROR;
1388 op->o_domain_scope = ctrl->ldctl_iscritical
1389 ? SLAP_CONTROL_CRITICAL
1390 : SLAP_CONTROL_NONCRITICAL;
1393 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1394 /* Other search flags not recognised so far,
1396 * LDAP_SEARCH_FLAG_PHANTOM_ROOM
1398 rs->sr_text = "searchOptions contained unrecongized flag";
1399 return LDAP_UNWILLING_TO_PERFORM;
1402 return LDAP_SUCCESS;