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;
45 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
47 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
48 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
50 struct slap_control_ids slap_cids;
56 /* The controlID for this control */
59 /* Operations supported by control */
62 /* Extended operations supported by control */
63 char **sc_extendedops;
65 /* Control parsing callback */
66 SLAP_CTRL_PARSE_FN *sc_parse;
68 LDAP_SLIST_ENTRY(slap_control) sc_next;
71 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
72 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
75 * all known request control OIDs should be added to this list
77 char *slap_known_controls[SLAP_MAX_CIDS+1];
78 static int num_known_controls;
80 static char *proxy_authz_extops[] = {
81 LDAP_EXOP_MODIFY_PASSWD,
86 static struct slap_control control_defs[] = {
87 { LDAP_CONTROL_ASSERT,
88 (int)offsetof(struct slap_control_ids, sc_assert),
89 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
90 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
91 { LDAP_CONTROL_PRE_READ,
92 (int)offsetof(struct slap_control_ids, sc_preRead),
93 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
94 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
95 { LDAP_CONTROL_POST_READ,
96 (int)offsetof(struct slap_control_ids, sc_postRead),
97 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
98 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
99 { LDAP_CONTROL_VALUESRETURNFILTER,
100 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
101 SLAP_CTRL_SEARCH, NULL,
102 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
103 { LDAP_CONTROL_PAGEDRESULTS,
104 (int)offsetof(struct slap_control_ids, sc_pagedResults),
105 SLAP_CTRL_SEARCH, NULL,
106 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
107 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
108 { LDAP_CONTROL_X_DOMAIN_SCOPE,
109 (int)offsetof(struct slap_control_ids, sc_domainScope),
110 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
111 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
113 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
114 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
115 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
116 SLAP_CTRL_MODIFY, NULL,
117 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
119 #ifdef LDAP_CONTROL_X_TREE_DELETE
120 { LDAP_CONTROL_X_TREE_DELETE,
121 (int)offsetof(struct slap_control_ids, sc_treeDelete),
122 SLAP_CTRL_DELETE, NULL,
123 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
125 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
126 { LDAP_CONTORL_X_SEARCH_OPTIONS,
127 (int)offsetof(struct slap_control_ids, sc_searchOptions),
128 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
129 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
131 #ifdef LDAP_CONTROL_SUBENTRIES
132 { LDAP_CONTROL_SUBENTRIES,
133 (int)offsetof(struct slap_control_ids, sc_subentries),
134 SLAP_CTRL_SEARCH, NULL,
135 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
138 (int)offsetof(struct slap_control_ids, sc_noOp),
139 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
140 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
141 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
142 { LDAP_CONTROL_MODIFY_INCREMENT,
143 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
144 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
145 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
147 { LDAP_CONTROL_MANAGEDSAIT,
148 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
149 SLAP_CTRL_ACCESS, NULL,
150 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
151 { LDAP_CONTROL_PROXY_AUTHZ,
152 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
153 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
154 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
155 { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
159 * Register a supported control.
161 * This can be called by an OpenLDAP plugin or, indirectly, by a
162 * SLAPI plugin calling slapi_register_supported_control().
165 register_supported_control(const char *controloid,
166 slap_mask_t controlmask,
168 SLAP_CTRL_PARSE_FN *controlparsefn,
171 struct slap_control *sc;
173 if ( num_known_controls >= SLAP_MAX_CIDS ) {
174 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
175 " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
176 SLAP_MAX_CIDS, 0, 0 );
180 if ( controloid == NULL ) return LDAP_PARAM_ERROR;
182 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
183 if ( sc == NULL ) return LDAP_NO_MEMORY;
185 sc->sc_oid = ch_strdup( controloid );
186 sc->sc_mask = controlmask;
187 if ( controlexops != NULL ) {
188 sc->sc_extendedops = ldap_charray_dup( controlexops );
189 if ( sc->sc_extendedops == NULL ) {
191 return LDAP_NO_MEMORY;
194 sc->sc_extendedops = NULL;
196 sc->sc_parse = controlparsefn;
198 if ( controlcid ) *controlcid = num_known_controls;
199 sc->sc_cid = num_known_controls;
201 /* Update slap_known_controls, too. */
202 slap_known_controls[num_known_controls++] = sc->sc_oid;
203 slap_known_controls[num_known_controls] = NULL;
205 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
206 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
211 * One-time initialization of internal controls.
214 slap_controls_init( void )
217 struct slap_control *sc;
221 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
222 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
223 rc = register_supported_control( control_defs[i].sc_oid,
224 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
225 control_defs[i].sc_parse, cid );
226 if ( rc != LDAP_SUCCESS ) break;
233 * Free memory associated with list of supported controls.
236 controls_destroy( void )
238 struct slap_control *sc;
240 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
241 sc = LDAP_SLIST_FIRST(&controls_list);
242 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
244 ch_free( sc->sc_oid );
245 if ( sc->sc_extendedops != NULL ) {
246 ldap_charray_free( sc->sc_extendedops );
253 * Format the supportedControl attribute of the root DSE,
254 * detailing which controls are supported by the directory
258 controls_root_dse_info( Entry *e )
260 AttributeDescription *ad_supportedControl
261 = slap_schema.si_ad_supportedControl;
262 struct berval vals[2];
263 struct slap_control *sc;
265 vals[1].bv_val = NULL;
268 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
269 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
271 vals[0].bv_val = sc->sc_oid;
272 vals[0].bv_len = strlen( sc->sc_oid );
274 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
283 * Return a list of OIDs and operation masks for supported
284 * controls. Used by SLAPI.
287 get_supported_controls(char ***ctrloidsp,
288 slap_mask_t **ctrlmasks)
294 struct slap_control *sc;
298 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
308 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
309 if ( oids == NULL ) {
310 return LDAP_NO_MEMORY;
312 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
313 if ( masks == NULL ) {
315 return LDAP_NO_MEMORY;
320 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
321 oids[n] = ch_strdup( sc->sc_oid );
322 masks[n] = sc->sc_mask;
335 * Find a control given its OID.
337 static struct slap_control *
338 find_ctrl( const char *oid )
340 struct slap_control *sc;
342 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
343 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
351 void slap_free_ctrls(
353 LDAPControl **ctrls )
357 for (i=0; ctrls[i]; i++) {
358 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
360 op->o_tmpfree( ctrls, op->o_tmpmemctx );
372 BerElement *ber = op->o_ber;
373 struct slap_control *sc;
376 len = ber_pvt_ber_remaining(ber);
380 rs->sr_err = LDAP_SUCCESS;
384 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
385 if( tag == LBER_ERROR ) {
386 rs->sr_err = SLAPD_DISCONNECT;
387 rs->sr_text = "unexpected data in PDU";
393 Debug( LDAP_DEBUG_TRACE,
394 "=> get_ctrls\n", 0, 0, 0 );
396 if( op->o_protocol < LDAP_VERSION3 ) {
397 rs->sr_err = SLAPD_DISCONNECT;
398 rs->sr_text = "controls require LDAPv3";
402 /* one for first control, one for termination */
403 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
406 if( op->ctrls == NULL ) {
407 rs->sr_err = LDAP_NO_MEMORY;
408 rs->sr_text = "no memory";
413 op->o_ctrls[nctrls] = NULL;
415 /* step through each element */
416 for( tag = ber_first_element( ber, &len, &opaque );
418 tag = ber_next_element( ber, &len, opaque ) )
421 LDAPControl **tctrls;
423 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
424 memset(c, 0, sizeof(LDAPControl));
426 /* allocate pointer space for current controls (nctrls)
427 * + this control + extra NULL
429 tctrls = op->o_tmprealloc( op->o_ctrls,
430 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
433 if( tctrls == NULL ) {
435 ldap_controls_free(op->o_ctrls);
438 rs->sr_err = LDAP_NO_MEMORY;
439 rs->sr_text = "no memory";
443 op->o_ctrls = tctrls;
445 op->o_ctrls[nctrls++] = c;
446 op->o_ctrls[nctrls] = NULL;
448 tag = ber_scanf( ber, "{m" /*}*/, &bv );
449 c->ldctl_oid = bv.bv_val;
451 if( tag == LBER_ERROR ) {
452 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
455 slap_free_ctrls( op, op->o_ctrls );
457 rs->sr_err = SLAPD_DISCONNECT;
458 rs->sr_text = "decoding controls error";
461 } else if( c->ldctl_oid == NULL ) {
462 Debug( LDAP_DEBUG_TRACE,
463 "get_ctrls: conn %lu got emtpy OID.\n",
464 op->o_connid, 0, 0 );
466 slap_free_ctrls( op, op->o_ctrls );
468 rs->sr_err = LDAP_PROTOCOL_ERROR;
469 rs->sr_text = "OID field is empty";
473 tag = ber_peek_tag( ber, &len );
475 if( tag == LBER_BOOLEAN ) {
477 tag = ber_scanf( ber, "b", &crit );
479 if( tag == LBER_ERROR ) {
480 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
482 slap_free_ctrls( op, op->o_ctrls );
484 rs->sr_err = SLAPD_DISCONNECT;
485 rs->sr_text = "decoding controls error";
489 c->ldctl_iscritical = (crit != 0);
490 tag = ber_peek_tag( ber, &len );
493 if( tag == LBER_OCTETSTRING ) {
494 tag = ber_scanf( ber, "m", &c->ldctl_value );
496 if( tag == LBER_ERROR ) {
497 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
498 "%s (%scritical): get value failed.\n",
499 op->o_connid, c->ldctl_oid,
500 c->ldctl_iscritical ? "" : "non" );
501 slap_free_ctrls( op, op->o_ctrls );
503 rs->sr_err = SLAPD_DISCONNECT;
504 rs->sr_text = "decoding controls error";
509 Debug( LDAP_DEBUG_TRACE,
510 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
511 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
513 sc = find_ctrl( c->ldctl_oid );
515 /* recognized control */
517 switch( op->o_tag ) {
519 tagmask = SLAP_CTRL_ADD;
522 tagmask = SLAP_CTRL_BIND;
524 case LDAP_REQ_COMPARE:
525 tagmask = SLAP_CTRL_COMPARE;
527 case LDAP_REQ_DELETE:
528 tagmask = SLAP_CTRL_DELETE;
530 case LDAP_REQ_MODIFY:
531 tagmask = SLAP_CTRL_MODIFY;
533 case LDAP_REQ_RENAME:
534 tagmask = SLAP_CTRL_RENAME;
536 case LDAP_REQ_SEARCH:
537 tagmask = SLAP_CTRL_SEARCH;
539 case LDAP_REQ_UNBIND:
540 tagmask = SLAP_CTRL_UNBIND;
542 case LDAP_REQ_ABANDON:
543 tagmask = SLAP_CTRL_ABANDON;
545 case LDAP_REQ_EXTENDED:
547 assert( op->ore_reqoid.bv_val != NULL );
548 if( sc->sc_extendedops != NULL ) {
550 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
551 if( strcmp( op->ore_reqoid.bv_val,
552 sc->sc_extendedops[i] ) == 0 )
561 rs->sr_err = LDAP_OTHER;
562 rs->sr_text = "controls internal error";
566 if (( sc->sc_mask & tagmask ) == tagmask ) {
567 /* available extension */
570 if( !sc->sc_parse ) {
571 rs->sr_err = LDAP_OTHER;
572 rs->sr_text = "not yet implemented";
576 rc = sc->sc_parse( op, rs, c );
578 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
583 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
584 /* kludge to disable backend_control() check */
585 c->ldctl_iscritical = 0;
587 } else if ( tagmask == SLAP_CTRL_SEARCH &&
588 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
590 /* kludge to disable backend_control() check */
591 c->ldctl_iscritical = 0;
594 } else if( c->ldctl_iscritical ) {
595 /* unavailable CRITICAL control */
596 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
597 rs->sr_text = "critical extension is unavailable";
601 } else if( c->ldctl_iscritical ) {
602 /* unrecognized CRITICAL control */
603 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
604 rs->sr_text = "critical extension is not recognized";
611 Debug( LDAP_DEBUG_TRACE,
612 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
613 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
615 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
616 if( rs->sr_err == SLAPD_DISCONNECT ) {
617 rs->sr_err = LDAP_PROTOCOL_ERROR;
618 send_ldap_disconnect( op, rs );
619 rs->sr_err = SLAPD_DISCONNECT;
621 send_ldap_result( op, rs );
628 static int parseModifyIncrement (
634 if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
635 rs->sr_text = "modifyIncrement control specified multiple times";
636 return LDAP_PROTOCOL_ERROR;
640 if ( ctrl->ldctl_value.bv_len ) {
641 rs->sr_text = "modifyIncrement control value not empty";
642 return LDAP_PROTOCOL_ERROR;
646 op->o_modifyIncrement = ctrl->ldctl_iscritical
647 ? SLAP_CONTROL_CRITICAL
648 : SLAP_CONTROL_NONCRITICAL;
654 static int parseManageDSAit (
659 if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
660 rs->sr_text = "manageDSAit control specified multiple times";
661 return LDAP_PROTOCOL_ERROR;
664 if ( ctrl->ldctl_value.bv_len ) {
665 rs->sr_text = "manageDSAit control value not empty";
666 return LDAP_PROTOCOL_ERROR;
669 op->o_managedsait = ctrl->ldctl_iscritical
670 ? SLAP_CONTROL_CRITICAL
671 : SLAP_CONTROL_NONCRITICAL;
676 static int parseProxyAuthz (
682 struct berval dn = BER_BVNULL;
684 if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
685 rs->sr_text = "proxy authorization control specified multiple times";
686 return LDAP_PROTOCOL_ERROR;
689 op->o_proxy_authz = ctrl->ldctl_iscritical
690 ? SLAP_CONTROL_CRITICAL
691 : SLAP_CONTROL_NONCRITICAL;
693 Debug( LDAP_DEBUG_ARGS,
694 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
696 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
699 if( ctrl->ldctl_value.bv_len == 0 ) {
700 Debug( LDAP_DEBUG_TRACE,
701 "parseProxyAuthz: conn=%lu anonymous\n",
702 op->o_connid, 0, 0 );
705 free( op->o_dn.bv_val );
707 op->o_dn.bv_val = ch_strdup( "" );
709 free( op->o_ndn.bv_val );
710 op->o_ndn.bv_len = 0;
711 op->o_ndn.bv_val = ch_strdup( "" );
716 rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
717 NULL, &dn, SLAP_GETDN_AUTHZID );
719 /* FIXME: empty DN in proxyAuthz control should be legal... */
720 if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
722 ch_free( dn.bv_val );
724 rs->sr_text = "authzId mapping failed";
725 return LDAP_PROXY_AUTHZ_FAILURE;
728 Debug( LDAP_DEBUG_TRACE,
729 "parseProxyAuthz: conn=%lu \"%s\"\n",
731 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
733 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
736 ch_free( dn.bv_val );
737 rs->sr_text = "not authorized to assume identity";
738 return LDAP_PROXY_AUTHZ_FAILURE;
741 ch_free( op->o_dn.bv_val );
742 ch_free( op->o_ndn.bv_val );
744 op->o_dn.bv_val = NULL;
747 Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
748 op->o_log_prefix, dn.bv_val, 0, 0, 0 );
751 * NOTE: since slap_sasl_getdn() returns a normalized dn,
752 * from now on op->o_dn is normalized
754 ber_dupbv( &op->o_dn, &dn );
759 static int parseNoOp (
764 if ( op->o_noop != SLAP_CONTROL_NONE ) {
765 rs->sr_text = "noop control specified multiple times";
766 return LDAP_PROTOCOL_ERROR;
769 if ( ctrl->ldctl_value.bv_len ) {
770 rs->sr_text = "noop control value not empty";
771 return LDAP_PROTOCOL_ERROR;
774 op->o_noop = ctrl->ldctl_iscritical
775 ? SLAP_CONTROL_CRITICAL
776 : SLAP_CONTROL_NONCRITICAL;
781 static int parsePagedResults (
786 int rc = LDAP_SUCCESS;
790 struct berval cookie = BER_BVNULL;
791 PagedResultsState *ps;
793 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
794 rs->sr_text = "paged results control specified multiple times";
795 return LDAP_PROTOCOL_ERROR;
798 #if 0 /* DELETE ME */
799 if ( op->o_sync != SLAP_CONTROL_NONE ) {
800 rs->sr_text = "paged results control specified with sync control";
801 return LDAP_PROTOCOL_ERROR;
805 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
806 rs->sr_text = "paged results control value is empty (or absent)";
807 return LDAP_PROTOCOL_ERROR;
810 /* Parse the control value
811 * realSearchControlValue ::= SEQUENCE {
812 * size INTEGER (0..maxInt),
813 * -- requested page size from client
814 * -- result set size estimate from server
815 * cookie OCTET STRING
818 ber = ber_init( &ctrl->ldctl_value );
820 rs->sr_text = "internal error";
824 tag = ber_scanf( ber, "{im}", &size, &cookie );
826 if ( tag == LBER_ERROR ) {
827 rs->sr_text = "paged results control could not be decoded";
828 rc = LDAP_PROTOCOL_ERROR;
833 rs->sr_text = "paged results control size invalid";
834 rc = LDAP_PROTOCOL_ERROR;
839 /* defer cookie decoding/checks to backend... */
840 if ( cookie.bv_len ) {
841 PagedResultsCookie reqcookie;
842 if( cookie.bv_len != sizeof( reqcookie ) ) {
844 rs->sr_text = "paged results cookie is invalid";
845 rc = LDAP_PROTOCOL_ERROR;
849 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
851 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
853 rs->sr_text = "paged results cookie is invalid";
854 rc = LDAP_PROTOCOL_ERROR;
857 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
858 rs->sr_text = "paged results cookie is invalid or old";
859 rc = LDAP_UNWILLING_TO_PERFORM;
864 /* Initial request. Initialize state. */
866 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
867 /* There's another pagedResults control on the
868 * same connection; reject new pagedResults controls
869 * (allowed by RFC2696) */
870 rs->sr_text = "paged results cookie unavailable; try later";
871 rc = LDAP_UNWILLING_TO_PERFORM;
875 op->o_pagedresults_state.ps_cookie = 0;
876 op->o_pagedresults_state.ps_count = 0;
880 ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
881 *ps = op->o_conn->c_pagedresults_state;
883 op->o_pagedresults_state = ps;
885 /* NOTE: according to RFC 2696 3.:
887 If the page size is greater than or equal to the sizeLimit value, the
888 server should ignore the control as the request can be satisfied in a
891 * NOTE: this assumes that the op->ors_slimit be set
892 * before the controls are parsed.
895 if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
896 op->o_pagedresults = SLAP_CONTROL_IGNORED;
898 } else if ( ctrl->ldctl_iscritical ) {
899 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
902 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
906 (void)ber_free( ber, 1 );
910 static int parseAssert (
916 struct berval fstr = BER_BVNULL;
917 const char *err_msg = "";
919 if ( op->o_assert != SLAP_CONTROL_NONE ) {
920 rs->sr_text = "assert control specified multiple times";
921 return LDAP_PROTOCOL_ERROR;
924 if ( ctrl->ldctl_value.bv_len == 0 ) {
925 rs->sr_text = "assert control value is empty (or absent)";
926 return LDAP_PROTOCOL_ERROR;
929 ber = ber_init( &(ctrl->ldctl_value) );
931 rs->sr_text = "assert control: internal error";
935 rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
937 if( rs->sr_err != LDAP_SUCCESS ) {
938 if( rs->sr_err == SLAPD_DISCONNECT ) {
939 rs->sr_err = LDAP_PROTOCOL_ERROR;
940 send_ldap_disconnect( op, rs );
941 rs->sr_err = SLAPD_DISCONNECT;
943 send_ldap_result( op, rs );
945 if( op->o_assertion != NULL ) {
946 filter_free_x( op, op->o_assertion );
952 filter2bv_x( op, op->o_assertion, &fstr );
954 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
955 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
956 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
959 op->o_assert = ctrl->ldctl_iscritical
960 ? SLAP_CONTROL_CRITICAL
961 : SLAP_CONTROL_NONCRITICAL;
963 rs->sr_err = LDAP_SUCCESS;
967 static int parsePreRead (
972 ber_len_t siz, off, i;
973 AttributeName *an = NULL;
976 if ( op->o_preread != SLAP_CONTROL_NONE ) {
977 rs->sr_text = "preread control specified multiple times";
978 return LDAP_PROTOCOL_ERROR;
981 if ( ctrl->ldctl_value.bv_len == 0 ) {
982 rs->sr_text = "preread control value is empty (or absent)";
983 return LDAP_PROTOCOL_ERROR;
986 ber = ber_init( &(ctrl->ldctl_value) );
988 rs->sr_text = "preread control: internal error";
992 siz = sizeof( AttributeName );
993 off = offsetof( AttributeName, an_name );
994 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
995 rs->sr_text = "preread control: decoding error";
996 return LDAP_PROTOCOL_ERROR;
999 for( i=0; i<siz; i++ ) {
1000 int rc = LDAP_SUCCESS;
1001 const char *dummy = NULL;
1003 an[i].an_desc = NULL;
1005 an[i].an_oc_exclude = 0;
1006 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1007 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1008 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1013 op->o_preread = ctrl->ldctl_iscritical
1014 ? SLAP_CONTROL_CRITICAL
1015 : SLAP_CONTROL_NONCRITICAL;
1017 op->o_preread_attrs = an;
1019 rs->sr_err = LDAP_SUCCESS;
1020 return LDAP_SUCCESS;
1023 static int parsePostRead (
1028 ber_len_t siz, off, i;
1029 AttributeName *an = NULL;
1032 if ( op->o_postread != SLAP_CONTROL_NONE ) {
1033 rs->sr_text = "postread control specified multiple times";
1034 return LDAP_PROTOCOL_ERROR;
1037 if ( ctrl->ldctl_value.bv_len == 0 ) {
1038 rs->sr_text = "postread control value is empty (or absent)";
1039 return LDAP_PROTOCOL_ERROR;
1042 ber = ber_init( &(ctrl->ldctl_value) );
1044 rs->sr_text = "postread control: internal error";
1048 siz = sizeof( AttributeName );
1049 off = offsetof( AttributeName, an_name );
1050 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1051 rs->sr_text = "postread control: decoding error";
1052 return LDAP_PROTOCOL_ERROR;
1055 for( i=0; i<siz; i++ ) {
1056 int rc = LDAP_SUCCESS;
1057 const char *dummy = NULL;
1059 an[i].an_desc = NULL;
1061 an[i].an_oc_exclude = 0;
1062 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1063 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1064 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1069 op->o_postread = ctrl->ldctl_iscritical
1070 ? SLAP_CONTROL_CRITICAL
1071 : SLAP_CONTROL_NONCRITICAL;
1073 op->o_postread_attrs = an;
1075 rs->sr_err = LDAP_SUCCESS;
1076 return LDAP_SUCCESS;
1079 int parseValuesReturnFilter (
1085 struct berval fstr = BER_BVNULL;
1086 const char *err_msg = "";
1088 if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1089 rs->sr_text = "valuesReturnFilter control specified multiple times";
1090 return LDAP_PROTOCOL_ERROR;
1093 if ( ctrl->ldctl_value.bv_len == 0 ) {
1094 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1095 return LDAP_PROTOCOL_ERROR;
1098 ber = ber_init( &(ctrl->ldctl_value) );
1100 rs->sr_text = "internal error";
1104 rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1106 if( rs->sr_err != LDAP_SUCCESS ) {
1107 if( rs->sr_err == SLAPD_DISCONNECT ) {
1108 rs->sr_err = LDAP_PROTOCOL_ERROR;
1109 send_ldap_disconnect( op, rs );
1110 rs->sr_err = SLAPD_DISCONNECT;
1112 send_ldap_result( op, rs );
1114 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1118 vrFilter2bv( op, op->o_vrFilter, &fstr );
1121 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1122 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1123 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1126 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1127 ? SLAP_CONTROL_CRITICAL
1128 : SLAP_CONTROL_NONCRITICAL;
1130 rs->sr_err = LDAP_SUCCESS;
1131 return LDAP_SUCCESS;
1134 #ifdef LDAP_CONTROL_SUBENTRIES
1135 static int parseSubentries (
1140 if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1141 rs->sr_text = "subentries control specified multiple times";
1142 return LDAP_PROTOCOL_ERROR;
1145 /* FIXME: should use BER library */
1146 if( ( ctrl->ldctl_value.bv_len != 3 )
1147 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1148 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1150 rs->sr_text = "subentries control value encoding is bogus";
1151 return LDAP_PROTOCOL_ERROR;
1154 op->o_subentries = ctrl->ldctl_iscritical
1155 ? SLAP_CONTROL_CRITICAL
1156 : SLAP_CONTROL_NONCRITICAL;
1158 if ( (void *)(ctrl->ldctl_value.bv_val[2] != 0x00))
1159 set_subentries_visibility( op );
1161 return LDAP_SUCCESS;
1165 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1166 static int parsePermissiveModify (
1171 if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1172 rs->sr_text = "permissiveModify control specified multiple times";
1173 return LDAP_PROTOCOL_ERROR;
1176 if ( ctrl->ldctl_value.bv_len ) {
1177 rs->sr_text = "permissiveModify control value not empty";
1178 return LDAP_PROTOCOL_ERROR;
1181 op->o_permissive_modify = ctrl->ldctl_iscritical
1182 ? SLAP_CONTROL_CRITICAL
1183 : SLAP_CONTROL_NONCRITICAL;
1185 return LDAP_SUCCESS;
1189 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1190 static int parseDomainScope (
1195 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1196 rs->sr_text = "domainScope control specified multiple times";
1197 return LDAP_PROTOCOL_ERROR;
1200 if ( ctrl->ldctl_value.bv_len ) {
1201 rs->sr_text = "domainScope control value not empty";
1202 return LDAP_PROTOCOL_ERROR;
1205 op->o_domain_scope = ctrl->ldctl_iscritical
1206 ? SLAP_CONTROL_CRITICAL
1207 : SLAP_CONTROL_NONCRITICAL;
1209 return LDAP_SUCCESS;
1213 #ifdef LDAP_CONTROL_X_TREE_DELETE
1214 static int parseTreeDelete (
1219 if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1220 rs->sr_text = "treeDelete control specified multiple times";
1221 return LDAP_PROTOCOL_ERROR;
1224 if ( ctrl->ldctl_value.bv_len ) {
1225 rs->sr_text = "treeDelete control value not empty";
1226 return LDAP_PROTOCOL_ERROR;
1229 op->o_tree_delete = ctrl->ldctl_iscritical
1230 ? SLAP_CONTROL_CRITICAL
1231 : SLAP_CONTROL_NONCRITICAL;
1233 return LDAP_SUCCESS;
1237 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1238 static int parseSearchOptions (
1244 ber_int_t search_flags;
1246 if ( ctrl->ldctl_value.bv_len == 0 ) {
1247 rs->sr_text = "searchOptions control value not empty";
1248 return LDAP_PROTOCOL_ERROR;
1251 ber = ber_init( &ctrl->ldctl_value );
1253 rs->sr_text = "internal error";
1257 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1258 rs->sr_text = "searchOptions control decoding error";
1259 return LDAP_PROTOCOL_ERROR;
1262 (void) ber_free( ber, 1 );
1264 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1265 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1266 rs->sr_text = "searchOptions control specified multiple times "
1267 "or with domainScope control";
1268 return LDAP_PROTOCOL_ERROR;
1271 op->o_domain_scope = ctrl->ldctl_iscritical
1272 ? SLAP_CONTROL_CRITICAL
1273 : SLAP_CONTROL_NONCRITICAL;
1276 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1277 /* Other search flags not recognised so far */
1278 rs->sr_text = "searchOptions contained unrecongized flag";
1279 return LDAP_UNWILLING_TO_PERFORM;
1282 return LDAP_SUCCESS;