2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/socket.h>
25 #include "../../libraries/liblber/lber-int.h"
27 static SLAP_CTRL_PARSE_FN parseAssert;
28 static SLAP_CTRL_PARSE_FN parsePreRead;
29 static SLAP_CTRL_PARSE_FN parsePostRead;
30 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
31 static SLAP_CTRL_PARSE_FN parseManageDSAit;
32 static SLAP_CTRL_PARSE_FN parseModifyIncrement;
33 static SLAP_CTRL_PARSE_FN parseNoOp;
34 static SLAP_CTRL_PARSE_FN parsePagedResults;
35 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
36 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
37 static SLAP_CTRL_PARSE_FN parseDomainScope;
38 static SLAP_CTRL_PARSE_FN parseTreeDelete;
39 static SLAP_CTRL_PARSE_FN parseSearchOptions;
41 #ifdef LDAP_CONTROL_SUBENTRIES
42 static SLAP_CTRL_PARSE_FN parseSubentries;
44 static SLAP_CTRL_PARSE_FN parseLDAPsync;
46 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
48 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
49 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
51 struct slap_control_ids slap_cids;
57 /* The controlID for this control */
60 /* Operations supported by control */
63 /* Extended operations supported by control */
64 char **sc_extendedops;
66 /* Control parsing callback */
67 SLAP_CTRL_PARSE_FN *sc_parse;
69 LDAP_SLIST_ENTRY(slap_control) sc_next;
72 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
73 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
76 * all known request control OIDs should be added to this list
78 char *slap_known_controls[SLAP_MAX_CIDS+1];
79 static int num_known_controls;
81 static char *proxy_authz_extops[] = {
82 LDAP_EXOP_MODIFY_PASSWD,
87 static struct slap_control control_defs[] = {
88 { LDAP_CONTROL_ASSERT,
89 (int)offsetof(struct slap_control_ids, sc_assert),
90 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
91 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
92 { LDAP_CONTROL_PRE_READ,
93 (int)offsetof(struct slap_control_ids, sc_preRead),
94 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
95 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
96 { LDAP_CONTROL_POST_READ,
97 (int)offsetof(struct slap_control_ids, sc_postRead),
98 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
99 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
100 { LDAP_CONTROL_VALUESRETURNFILTER,
101 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
102 SLAP_CTRL_SEARCH, NULL,
103 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
104 { LDAP_CONTROL_PAGEDRESULTS,
105 (int)offsetof(struct slap_control_ids, sc_pagedResults),
106 SLAP_CTRL_SEARCH, NULL,
107 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
108 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
109 { LDAP_CONTROL_X_DOMAIN_SCOPE,
110 (int)offsetof(struct slap_control_ids, sc_domainScope),
111 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
112 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
114 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
115 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
116 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
117 SLAP_CTRL_MODIFY, NULL,
118 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
120 #ifdef LDAP_CONTROL_X_TREE_DELETE
121 { LDAP_CONTROL_X_TREE_DELETE,
122 (int)offsetof(struct slap_control_ids, sc_treeDelete),
123 SLAP_CTRL_DELETE, NULL,
124 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
126 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
127 { LDAP_CONTORL_X_SEARCH_OPTIONS,
128 (int)offsetof(struct slap_control_ids, sc_searchOptions),
129 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
130 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
132 #ifdef LDAP_CONTROL_SUBENTRIES
133 { LDAP_CONTROL_SUBENTRIES,
134 (int)offsetof(struct slap_control_ids, sc_subentries),
135 SLAP_CTRL_SEARCH, NULL,
136 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 (int)offsetof(struct slap_control_ids, sc_noOp),
140 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
141 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
143 (int)offsetof(struct slap_control_ids, sc_LDAPsync),
144 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
145 parseLDAPsync, LDAP_SLIST_ENTRY_INITIALIZER(next) },
146 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
147 { LDAP_CONTROL_MODIFY_INCREMENT,
148 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
149 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
150 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
152 { LDAP_CONTROL_MANAGEDSAIT,
153 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
154 SLAP_CTRL_ACCESS, NULL,
155 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
156 { LDAP_CONTROL_PROXY_AUTHZ,
157 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
158 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
159 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
160 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
164 * Register a supported control.
166 * This can be called by an OpenLDAP plugin or, indirectly, by a
167 * SLAPI plugin calling slapi_register_supported_control().
170 register_supported_control(const char *controloid,
171 slap_mask_t controlmask,
173 SLAP_CTRL_PARSE_FN *controlparsefn,
176 struct slap_control *sc;
178 if ( num_known_controls >= SLAP_MAX_CIDS ) {
179 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
180 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
181 SLAP_MAX_CIDS, 0, 0 );
185 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
187 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
188 if ( sc == NULL ) return LDAP_NO_MEMORY;
190 sc->sc_oid = ch_strdup( controloid );
191 sc->sc_mask = controlmask;
192 if ( controlexops != NULL ) {
193 sc->sc_extendedops = ldap_charray_dup( controlexops );
194 if ( sc->sc_extendedops == NULL ) {
196 return LDAP_NO_MEMORY;
199 sc->sc_extendedops = NULL;
201 sc->sc_parse = controlparsefn;
203 if ( controlcid ) *controlcid = num_known_controls;
204 sc->sc_cid = num_known_controls;
206 /* Update slap_known_controls, too. */
207 slap_known_controls[num_known_controls++] = sc->sc_oid;
208 slap_known_controls[num_known_controls] = NULL;
210 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
211 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
216 * One-time initialization of internal controls.
219 slap_controls_init( void )
222 struct slap_control *sc;
226 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
227 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
228 rc = register_supported_control( control_defs[i].sc_oid,
229 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
230 control_defs[i].sc_parse, cid );
231 if ( rc != LDAP_SUCCESS ) break;
238 * Free memory associated with list of supported controls.
241 controls_destroy( void )
243 struct slap_control *sc;
245 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
246 sc = LDAP_SLIST_FIRST(&controls_list);
247 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
249 ch_free( sc->sc_oid );
250 if ( sc->sc_extendedops != NULL ) {
251 ldap_charray_free( sc->sc_extendedops );
258 * Format the supportedControl attribute of the root DSE,
259 * detailing which controls are supported by the directory
263 controls_root_dse_info( Entry *e )
265 AttributeDescription *ad_supportedControl
266 = slap_schema.si_ad_supportedControl;
267 struct berval vals[2];
268 struct slap_control *sc;
270 vals[1].bv_val = NULL;
273 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
274 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
276 vals[0].bv_val = sc->sc_oid;
277 vals[0].bv_len = strlen( sc->sc_oid );
279 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
288 * Return a list of OIDs and operation masks for supported
289 * controls. Used by SLAPI.
292 get_supported_controls(char ***ctrloidsp,
293 slap_mask_t **ctrlmasks)
299 struct slap_control *sc;
303 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
313 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
314 if ( oids == NULL ) {
315 return LDAP_NO_MEMORY;
317 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
318 if ( masks == NULL ) {
320 return LDAP_NO_MEMORY;
325 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
326 oids[n] = ch_strdup( sc->sc_oid );
327 masks[n] = sc->sc_mask;
340 * Find a control given its OID.
342 static struct slap_control *
343 find_ctrl( const char *oid )
345 struct slap_control *sc;
347 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
348 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
356 void slap_free_ctrls(
358 LDAPControl **ctrls )
362 for (i=0; ctrls[i]; i++) {
363 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
365 op->o_tmpfree( ctrls, op->o_tmpmemctx );
377 BerElement *ber = op->o_ber;
378 struct slap_control *sc;
381 len = ber_pvt_ber_remaining(ber);
385 rs->sr_err = LDAP_SUCCESS;
389 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
390 if( tag == LBER_ERROR ) {
391 rs->sr_err = SLAPD_DISCONNECT;
392 rs->sr_text = "unexpected data in PDU";
398 Debug( LDAP_DEBUG_TRACE,
399 "=> get_ctrls\n", 0, 0, 0 );
401 if( op->o_protocol < LDAP_VERSION3 ) {
402 rs->sr_err = SLAPD_DISCONNECT;
403 rs->sr_text = "controls require LDAPv3";
407 /* one for first control, one for termination */
408 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
411 if( op->ctrls == NULL ) {
412 rs->sr_err = LDAP_NO_MEMORY;
413 rs->sr_text = "no memory";
418 op->o_ctrls[nctrls] = NULL;
420 /* step through each element */
421 for( tag = ber_first_element( ber, &len, &opaque );
423 tag = ber_next_element( ber, &len, opaque ) )
426 LDAPControl **tctrls;
428 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
429 memset(c, 0, sizeof(LDAPControl));
431 /* allocate pointer space for current controls (nctrls)
432 * + this control + extra NULL
434 tctrls = op->o_tmprealloc( op->o_ctrls,
435 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
438 if( tctrls == NULL ) {
440 ldap_controls_free(op->o_ctrls);
443 rs->sr_err = LDAP_NO_MEMORY;
444 rs->sr_text = "no memory";
448 op->o_ctrls = tctrls;
450 op->o_ctrls[nctrls++] = c;
451 op->o_ctrls[nctrls] = NULL;
453 tag = ber_scanf( ber, "{m" /*}*/, &bv );
454 c->ldctl_oid = bv.bv_val;
456 if( tag == LBER_ERROR ) {
457 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
460 slap_free_ctrls( op, op->o_ctrls );
462 rs->sr_err = SLAPD_DISCONNECT;
463 rs->sr_text = "decoding controls error";
466 } else if( c->ldctl_oid == NULL ) {
467 Debug( LDAP_DEBUG_TRACE,
468 "get_ctrls: conn %lu got emtpy OID.\n",
469 op->o_connid, 0, 0 );
471 slap_free_ctrls( op, op->o_ctrls );
473 rs->sr_err = LDAP_PROTOCOL_ERROR;
474 rs->sr_text = "OID field is empty";
478 tag = ber_peek_tag( ber, &len );
480 if( tag == LBER_BOOLEAN ) {
482 tag = ber_scanf( ber, "b", &crit );
484 if( tag == LBER_ERROR ) {
485 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
487 slap_free_ctrls( op, op->o_ctrls );
489 rs->sr_err = SLAPD_DISCONNECT;
490 rs->sr_text = "decoding controls error";
494 c->ldctl_iscritical = (crit != 0);
495 tag = ber_peek_tag( ber, &len );
498 if( tag == LBER_OCTETSTRING ) {
499 tag = ber_scanf( ber, "m", &c->ldctl_value );
501 if( tag == LBER_ERROR ) {
502 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
503 "%s (%scritical): get value failed.\n",
504 op->o_connid, c->ldctl_oid,
505 c->ldctl_iscritical ? "" : "non" );
506 slap_free_ctrls( op, op->o_ctrls );
508 rs->sr_err = SLAPD_DISCONNECT;
509 rs->sr_text = "decoding controls error";
514 Debug( LDAP_DEBUG_TRACE,
515 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
516 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
518 sc = find_ctrl( c->ldctl_oid );
520 /* recognized control */
522 switch( op->o_tag ) {
524 tagmask = SLAP_CTRL_ADD;
527 tagmask = SLAP_CTRL_BIND;
529 case LDAP_REQ_COMPARE:
530 tagmask = SLAP_CTRL_COMPARE;
532 case LDAP_REQ_DELETE:
533 tagmask = SLAP_CTRL_DELETE;
535 case LDAP_REQ_MODIFY:
536 tagmask = SLAP_CTRL_MODIFY;
538 case LDAP_REQ_RENAME:
539 tagmask = SLAP_CTRL_RENAME;
541 case LDAP_REQ_SEARCH:
542 tagmask = SLAP_CTRL_SEARCH;
544 case LDAP_REQ_UNBIND:
545 tagmask = SLAP_CTRL_UNBIND;
547 case LDAP_REQ_ABANDON:
548 tagmask = SLAP_CTRL_ABANDON;
550 case LDAP_REQ_EXTENDED:
552 assert( op->ore_reqoid.bv_val != NULL );
553 if( sc->sc_extendedops != NULL ) {
555 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
556 if( strcmp( op->ore_reqoid.bv_val,
557 sc->sc_extendedops[i] ) == 0 )
566 rs->sr_err = LDAP_OTHER;
567 rs->sr_text = "controls internal error";
571 if (( sc->sc_mask & tagmask ) == tagmask ) {
572 /* available extension */
575 if( !sc->sc_parse ) {
576 rs->sr_err = LDAP_OTHER;
577 rs->sr_text = "not yet implemented";
581 rc = sc->sc_parse( op, rs, c );
583 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
588 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
589 /* kludge to disable backend_control() check */
590 c->ldctl_iscritical = 0;
592 } else if ( tagmask == SLAP_CTRL_SEARCH &&
593 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
595 /* kludge to disable backend_control() check */
596 c->ldctl_iscritical = 0;
599 } else if( c->ldctl_iscritical ) {
600 /* unavailable CRITICAL control */
601 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
602 rs->sr_text = "critical extension is unavailable";
606 } else if( c->ldctl_iscritical ) {
607 /* unrecognized CRITICAL control */
608 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
609 rs->sr_text = "critical extension is not recognized";
616 Debug( LDAP_DEBUG_TRACE,
617 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
618 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
620 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
621 if( rs->sr_err == SLAPD_DISCONNECT ) {
622 rs->sr_err = LDAP_PROTOCOL_ERROR;
623 send_ldap_disconnect( op, rs );
624 rs->sr_err = SLAPD_DISCONNECT;
626 send_ldap_result( op, rs );
633 static int parseModifyIncrement (
639 if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
640 rs->sr_text = "modifyIncrement control specified multiple times";
641 return LDAP_PROTOCOL_ERROR;
645 if ( ctrl->ldctl_value.bv_len ) {
646 rs->sr_text = "modifyIncrement control value not empty";
647 return LDAP_PROTOCOL_ERROR;
651 op->o_modifyIncrement = ctrl->ldctl_iscritical
652 ? SLAP_CONTROL_CRITICAL
653 : SLAP_CONTROL_NONCRITICAL;
659 static int parseManageDSAit (
664 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
665 rs->sr_text = "manageDSAit control specified multiple times";
666 return LDAP_PROTOCOL_ERROR;
669 if ( ctrl->ldctl_value.bv_len ) {
670 rs->sr_text = "manageDSAit control value not empty";
671 return LDAP_PROTOCOL_ERROR;
674 op->o_managedsait = ctrl->ldctl_iscritical
675 ? SLAP_CONTROL_CRITICAL
676 : SLAP_CONTROL_NONCRITICAL;
681 static int parseProxyAuthz (
687 struct berval dn = BER_BVNULL;
689 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
690 rs->sr_text = "proxy authorization control specified multiple times";
691 return LDAP_PROTOCOL_ERROR;
694 op->o_proxy_authz = ctrl->ldctl_iscritical
695 ? SLAP_CONTROL_CRITICAL
696 : SLAP_CONTROL_NONCRITICAL;
698 Debug( LDAP_DEBUG_ARGS,
699 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
701 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
704 if( ctrl->ldctl_value.bv_len == 0 ) {
705 Debug( LDAP_DEBUG_TRACE,
706 "parseProxyAuthz: conn=%lu anonymous\n",
707 op->o_connid, 0, 0 );
710 free( op->o_dn.bv_val );
712 op->o_dn.bv_val = ch_strdup( "" );
714 free( op->o_ndn.bv_val );
715 op->o_ndn.bv_len = 0;
716 op->o_ndn.bv_val = ch_strdup( "" );
721 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
722 NULL, &dn, SLAP_GETDN_AUTHZID );
724 /* FIXME: empty DN in proxyAuthz control should be legal... */
725 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
727 ch_free( dn.bv_val );
729 rs->sr_text = "authzId mapping failed";
730 return LDAP_PROXY_AUTHZ_FAILURE;
733 Debug( LDAP_DEBUG_TRACE,
734 "parseProxyAuthz: conn=%lu \"%s\"\n",
736 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
738 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
741 ch_free( dn.bv_val );
742 rs->sr_text = "not authorized to assume identity";
743 return LDAP_PROXY_AUTHZ_FAILURE;
746 ch_free( op->o_dn.bv_val );
747 ch_free( op->o_ndn.bv_val );
749 op->o_dn.bv_val = NULL;
752 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
753 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
756 * NOTE: since slap_sasl_getdn() returns a normalized dn,
757 * from now on op->o_dn is normalized
759 ber_dupbv( &op->o_dn, &dn );
764 static int parseNoOp (
769 if ( op->o_noop != SLAP_CONTROL_NONE ) {
770 rs->sr_text = "noop control specified multiple times";
771 return LDAP_PROTOCOL_ERROR;
774 if ( ctrl->ldctl_value.bv_len ) {
775 rs->sr_text = "noop control value not empty";
776 return LDAP_PROTOCOL_ERROR;
779 op->o_noop = ctrl->ldctl_iscritical
780 ? SLAP_CONTROL_CRITICAL
781 : SLAP_CONTROL_NONCRITICAL;
786 static int parsePagedResults (
791 int rc = LDAP_SUCCESS;
795 struct berval cookie = BER_BVNULL;
796 PagedResultsState *ps;
798 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
799 rs->sr_text = "paged results control specified multiple times";
800 return LDAP_PROTOCOL_ERROR;
803 if ( op->o_sync != SLAP_CONTROL_NONE ) {
804 rs->sr_text = "paged results control specified with sync control";
805 return LDAP_PROTOCOL_ERROR;
808 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
809 rs->sr_text = "paged results control value is empty (or absent)";
810 return LDAP_PROTOCOL_ERROR;
813 /* Parse the control value
814 * realSearchControlValue ::= SEQUENCE {
815 * size INTEGER (0..maxInt),
816 * -- requested page size from client
817 * -- result set size estimate from server
818 * cookie OCTET STRING
821 ber = ber_init( &ctrl->ldctl_value );
823 rs->sr_text = "internal error";
827 tag = ber_scanf( ber, "{im}", &size, &cookie );
829 if ( tag == LBER_ERROR ) {
830 rs->sr_text = "paged results control could not be decoded";
831 rc = LDAP_PROTOCOL_ERROR;
836 rs->sr_text = "paged results control size invalid";
837 rc = LDAP_PROTOCOL_ERROR;
842 /* defer cookie decoding/checks to backend... */
843 if ( cookie.bv_len ) {
844 PagedResultsCookie reqcookie;
845 if( cookie.bv_len != sizeof( reqcookie ) ) {
847 rs->sr_text = "paged results cookie is invalid";
848 rc = LDAP_PROTOCOL_ERROR;
852 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
854 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
856 rs->sr_text = "paged results cookie is invalid";
857 rc = LDAP_PROTOCOL_ERROR;
860 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
861 rs->sr_text = "paged results cookie is invalid or old";
862 rc = LDAP_UNWILLING_TO_PERFORM;
867 /* Initial request. Initialize state. */
869 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
870 /* There's another pagedResults control on the
871 * same connection; reject new pagedResults controls
872 * (allowed by RFC2696) */
873 rs->sr_text = "paged results cookie unavailable; try later";
874 rc = LDAP_UNWILLING_TO_PERFORM;
878 op->o_pagedresults_state.ps_cookie = 0;
879 op->o_pagedresults_state.ps_count = 0;
883 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
884 *ps = op->o_conn->c_pagedresults_state;
886 op->o_pagedresults_state = ps;
888 /* NOTE: according to RFC 2696 3.:
890 If the page size is greater than or equal to the sizeLimit value, the
891 server should ignore the control as the request can be satisfied in a
894 * NOTE: this assumes that the op->ors_slimit be set
895 * before the controls are parsed.
898 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
899 op->o_pagedresults = SLAP_CONTROL_IGNORED;
901 } else if ( ctrl->ldctl_iscritical ) {
902 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
905 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
909 (void)ber_free( ber, 1 );
913 static int parseAssert (
919 struct berval fstr = BER_BVNULL;
920 const char *err_msg = "";
922 if ( op->o_assert != SLAP_CONTROL_NONE ) {
923 rs->sr_text = "assert control specified multiple times";
924 return LDAP_PROTOCOL_ERROR;
927 if ( ctrl->ldctl_value.bv_len == 0 ) {
928 rs->sr_text = "assert control value is empty (or absent)";
929 return LDAP_PROTOCOL_ERROR;
932 ber = ber_init( &(ctrl->ldctl_value) );
934 rs->sr_text = "assert control: internal error";
938 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
940 if( rs->sr_err != LDAP_SUCCESS ) {
941 if( rs->sr_err == SLAPD_DISCONNECT ) {
942 rs->sr_err = LDAP_PROTOCOL_ERROR;
943 send_ldap_disconnect( op, rs );
944 rs->sr_err = SLAPD_DISCONNECT;
946 send_ldap_result( op, rs );
948 if( op->o_assertion != NULL ) {
949 filter_free_x( op, op->o_assertion );
955 filter2bv_x( op, op->o_assertion, &fstr );
957 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
958 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
959 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
962 op->o_assert = ctrl->ldctl_iscritical
963 ? SLAP_CONTROL_CRITICAL
964 : SLAP_CONTROL_NONCRITICAL;
966 rs->sr_err = LDAP_SUCCESS;
970 static int parsePreRead (
975 ber_len_t siz, off, i;
976 AttributeName *an = NULL;
979 if ( op->o_preread != SLAP_CONTROL_NONE ) {
980 rs->sr_text = "preread control specified multiple times";
981 return LDAP_PROTOCOL_ERROR;
984 if ( ctrl->ldctl_value.bv_len == 0 ) {
985 rs->sr_text = "preread control value is empty (or absent)";
986 return LDAP_PROTOCOL_ERROR;
989 ber = ber_init( &(ctrl->ldctl_value) );
991 rs->sr_text = "preread control: internal error";
995 siz = sizeof( AttributeName );
996 off = offsetof( AttributeName, an_name );
997 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
998 rs->sr_text = "preread control: decoding error";
999 return LDAP_PROTOCOL_ERROR;
1002 for( i=0; i<siz; i++ ) {
1003 int rc = LDAP_SUCCESS;
1004 const char *dummy = NULL;
1006 an[i].an_desc = NULL;
1008 an[i].an_oc_exclude = 0;
1009 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1010 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1011 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1016 op->o_preread = ctrl->ldctl_iscritical
1017 ? SLAP_CONTROL_CRITICAL
1018 : SLAP_CONTROL_NONCRITICAL;
1020 op->o_preread_attrs = an;
1022 rs->sr_err = LDAP_SUCCESS;
1023 return LDAP_SUCCESS;
1026 static int parsePostRead (
1031 ber_len_t siz, off, i;
1032 AttributeName *an = NULL;
1035 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1036 rs->sr_text = "postread control specified multiple times";
1037 return LDAP_PROTOCOL_ERROR;
1040 if ( ctrl->ldctl_value.bv_len == 0 ) {
1041 rs->sr_text = "postread control value is empty (or absent)";
1042 return LDAP_PROTOCOL_ERROR;
1045 ber = ber_init( &(ctrl->ldctl_value) );
1047 rs->sr_text = "postread control: internal error";
1051 siz = sizeof( AttributeName );
1052 off = offsetof( AttributeName, an_name );
1053 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1054 rs->sr_text = "postread control: decoding error";
1055 return LDAP_PROTOCOL_ERROR;
1058 for( i=0; i<siz; i++ ) {
1059 int rc = LDAP_SUCCESS;
1060 const char *dummy = NULL;
1062 an[i].an_desc = NULL;
1064 an[i].an_oc_exclude = 0;
1065 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1066 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1067 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1072 op->o_postread = ctrl->ldctl_iscritical
1073 ? SLAP_CONTROL_CRITICAL
1074 : SLAP_CONTROL_NONCRITICAL;
1076 op->o_postread_attrs = an;
1078 rs->sr_err = LDAP_SUCCESS;
1079 return LDAP_SUCCESS;
1082 int parseValuesReturnFilter (
1088 struct berval fstr = BER_BVNULL;
1089 const char *err_msg = "";
1091 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1092 rs->sr_text = "valuesReturnFilter control specified multiple times";
1093 return LDAP_PROTOCOL_ERROR;
1096 if ( ctrl->ldctl_value.bv_len == 0 ) {
1097 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1098 return LDAP_PROTOCOL_ERROR;
1101 ber = ber_init( &(ctrl->ldctl_value) );
1103 rs->sr_text = "internal error";
1107 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1109 if( rs->sr_err != LDAP_SUCCESS ) {
1110 if( rs->sr_err == SLAPD_DISCONNECT ) {
1111 rs->sr_err = LDAP_PROTOCOL_ERROR;
1112 send_ldap_disconnect( op, rs );
1113 rs->sr_err = SLAPD_DISCONNECT;
1115 send_ldap_result( op, rs );
1117 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1121 vrFilter2bv( op, op->o_vrFilter, &fstr );
1124 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1125 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1126 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1129 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1130 ? SLAP_CONTROL_CRITICAL
1131 : SLAP_CONTROL_NONCRITICAL;
1133 rs->sr_err = LDAP_SUCCESS;
1134 return LDAP_SUCCESS;
1137 #ifdef LDAP_CONTROL_SUBENTRIES
1138 static int parseSubentries (
1143 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1144 rs->sr_text = "subentries control specified multiple times";
1145 return LDAP_PROTOCOL_ERROR;
1148 /* FIXME: should use BER library */
1149 if( ( ctrl->ldctl_value.bv_len != 3 )
1150 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1151 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1153 rs->sr_text = "subentries control value encoding is bogus";
1154 return LDAP_PROTOCOL_ERROR;
1157 op->o_subentries = ctrl->ldctl_iscritical
1158 ? SLAP_CONTROL_CRITICAL
1159 : SLAP_CONTROL_NONCRITICAL;
1161 if ( (void *)(ctrl->ldctl_value.bv_val[2] != 0x00))
1162 set_subentries_visibility( op );
1164 return LDAP_SUCCESS;
1168 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1169 static int parsePermissiveModify (
1174 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1175 rs->sr_text = "permissiveModify control specified multiple times";
1176 return LDAP_PROTOCOL_ERROR;
1179 if ( ctrl->ldctl_value.bv_len ) {
1180 rs->sr_text = "permissiveModify control value not empty";
1181 return LDAP_PROTOCOL_ERROR;
1184 op->o_permissive_modify = ctrl->ldctl_iscritical
1185 ? SLAP_CONTROL_CRITICAL
1186 : SLAP_CONTROL_NONCRITICAL;
1188 return LDAP_SUCCESS;
1192 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1193 static int parseDomainScope (
1198 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1199 rs->sr_text = "domainScope control specified multiple times";
1200 return LDAP_PROTOCOL_ERROR;
1203 if ( ctrl->ldctl_value.bv_len ) {
1204 rs->sr_text = "domainScope control value not empty";
1205 return LDAP_PROTOCOL_ERROR;
1208 op->o_domain_scope = ctrl->ldctl_iscritical
1209 ? SLAP_CONTROL_CRITICAL
1210 : SLAP_CONTROL_NONCRITICAL;
1212 return LDAP_SUCCESS;
1216 #ifdef LDAP_CONTROL_X_TREE_DELETE
1217 static int parseTreeDelete (
1222 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1223 rs->sr_text = "treeDelete control specified multiple times";
1224 return LDAP_PROTOCOL_ERROR;
1227 if ( ctrl->ldctl_value.bv_len ) {
1228 rs->sr_text = "treeDelete control value not empty";
1229 return LDAP_PROTOCOL_ERROR;
1232 op->o_tree_delete = ctrl->ldctl_iscritical
1233 ? SLAP_CONTROL_CRITICAL
1234 : SLAP_CONTROL_NONCRITICAL;
1236 return LDAP_SUCCESS;
1240 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1241 static int parseSearchOptions (
1247 ber_int_t search_flags;
1249 if ( ctrl->ldctl_value.bv_len == 0 ) {
1250 rs->sr_text = "searchOptions control value not empty";
1251 return LDAP_PROTOCOL_ERROR;
1254 ber = ber_init( &ctrl->ldctl_value );
1256 rs->sr_text = "internal error";
1260 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1261 rs->sr_text = "searchOptions control decoding error";
1262 return LDAP_PROTOCOL_ERROR;
1265 (void) ber_free( ber, 1 );
1267 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1268 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1269 rs->sr_text = "searchOptions control specified multiple times "
1270 "or with domainScope control";
1271 return LDAP_PROTOCOL_ERROR;
1274 op->o_domain_scope = ctrl->ldctl_iscritical
1275 ? SLAP_CONTROL_CRITICAL
1276 : SLAP_CONTROL_NONCRITICAL;
1279 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1280 /* Other search flags not recognised so far */
1281 rs->sr_text = "searchOptions contained unrecongized flag";
1282 return LDAP_UNWILLING_TO_PERFORM;
1285 return LDAP_SUCCESS;
1289 static int parseLDAPsync (
1298 struct slap_session_entry *se;
1299 struct berval cookie = BER_BVNULL;
1303 if ( op->o_sync != SLAP_CONTROL_NONE ) {
1304 rs->sr_text = "Sync control specified multiple times";
1305 return LDAP_PROTOCOL_ERROR;
1308 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1309 rs->sr_text = "Sync control specified with pagedResults control";
1310 return LDAP_PROTOCOL_ERROR;
1314 if ( ctrl->ldctl_value.bv_len == 0 ) {
1315 rs->sr_text = "Sync control value is empty (or absent)";
1316 return LDAP_PROTOCOL_ERROR;
1319 /* Parse the control value
1320 * syncRequestValue ::= SEQUENCE {
1325 * refreshAndPersist (3)
1327 * cookie syncCookie OPTIONAL
1331 ber = ber_init( &ctrl->ldctl_value );
1333 rs->sr_text = "internal error";
1337 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1338 rs->sr_text = "Sync control : mode decoding error";
1339 return LDAP_PROTOCOL_ERROR;
1343 case LDAP_SYNC_REFRESH_ONLY:
1344 mode = SLAP_SYNC_REFRESH;
1346 case LDAP_SYNC_REFRESH_AND_PERSIST:
1347 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1350 rs->sr_text = "Sync control : unknown update mode";
1351 return LDAP_PROTOCOL_ERROR;
1354 tag = ber_peek_tag( ber, &len );
1356 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1357 if (( ber_scanf( ber, /*{*/ "o", &cookie )) == LBER_ERROR ) {
1358 rs->sr_text = "Sync control : cookie decoding error";
1359 return LDAP_PROTOCOL_ERROR;
1362 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1363 if (( ber_scanf( ber, /*{*/ "b", &rhint )) == LBER_ERROR ) {
1364 rs->sr_text = "Sync control : rhint decoding error";
1365 return LDAP_PROTOCOL_ERROR;
1368 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1369 rs->sr_text = "Sync control : decoding error";
1370 return LDAP_PROTOCOL_ERROR;
1372 sr = op->o_tmpcalloc( 1, sizeof(struct syncrepl_state), op->o_tmpmemctx );
1373 sr->sr_rhint = rhint;
1374 if (!BER_BVISNULL(&cookie)) {
1375 ber_bvarray_add( &sr->sr_state.octet_str, &cookie );
1376 slap_parse_sync_cookie( &sr->sr_state );
1379 op->o_controls[slap_cids.sc_LDAPsync] = sr;
1381 (void) ber_free( ber, 1 );
1383 op->o_sync = ctrl->ldctl_iscritical
1384 ? SLAP_CONTROL_CRITICAL
1385 : SLAP_CONTROL_NONCRITICAL;
1387 op->o_sync_mode |= mode; /* o_sync_mode shares o_sync */
1389 return LDAP_SUCCESS;