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);
55 /* Operations supported by control */
58 /* Extended operations supported by control */
59 char **sc_extendedops;
61 /* Control parsing callback */
62 SLAP_CTRL_PARSE_FN *sc_parse;
64 LDAP_SLIST_ENTRY(slap_control) sc_next;
67 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
68 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
71 * all known request control OIDs should be added to this list
73 char **slap_known_controls = NULL;
75 static char *proxy_authz_extops[] = {
76 LDAP_EXOP_MODIFY_PASSWD,
81 static struct slap_control control_defs[] = {
82 { LDAP_CONTROL_ASSERT,
83 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
84 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
85 { LDAP_CONTROL_PRE_READ,
86 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
87 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
88 { LDAP_CONTROL_POST_READ,
89 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
90 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
91 { LDAP_CONTROL_VALUESRETURNFILTER,
92 SLAP_CTRL_SEARCH, NULL,
93 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
94 { LDAP_CONTROL_PAGEDRESULTS,
95 SLAP_CTRL_SEARCH, NULL,
96 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
97 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
98 { LDAP_CONTROL_X_DOMAIN_SCOPE,
99 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
100 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
102 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
103 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
104 SLAP_CTRL_MODIFY, NULL,
105 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
107 #ifdef LDAP_CONTROL_X_TREE_DELETE
108 { LDAP_CONTROL_X_TREE_DELETE,
109 SLAP_CTRL_DELETE, NULL,
110 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
112 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
113 { LDAP_CONTORL_X_SEARCH_OPTIONS,
114 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
115 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
117 #ifdef LDAP_CONTROL_SUBENTRIES
118 { LDAP_CONTROL_SUBENTRIES,
119 SLAP_CTRL_SEARCH, NULL,
120 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
123 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
124 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
126 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
127 parseLDAPsync, LDAP_SLIST_ENTRY_INITIALIZER(next) },
128 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
129 { LDAP_CONTROL_MODIFY_INCREMENT,
130 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
131 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
133 { LDAP_CONTROL_MANAGEDSAIT,
134 SLAP_CTRL_ACCESS, NULL,
135 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
136 { LDAP_CONTROL_PROXY_AUTHZ,
137 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
138 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139 { NULL, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
143 * Register a supported control.
145 * This can be called by an OpenLDAP plugin or, indirectly, by a
146 * SLAPI plugin calling slapi_register_supported_control().
149 register_supported_control(const char *controloid,
150 slap_mask_t controlmask,
152 SLAP_CTRL_PARSE_FN *controlparsefn)
154 struct slap_control *sc;
157 if ( controloid == NULL ) {
158 return LDAP_PARAM_ERROR;
161 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
163 return LDAP_NO_MEMORY;
165 sc->sc_oid = ch_strdup( controloid );
166 sc->sc_mask = controlmask;
167 if ( controlexops != NULL ) {
168 sc->sc_extendedops = ldap_charray_dup( controlexops );
169 if ( sc->sc_extendedops == NULL ) {
171 return LDAP_NO_MEMORY;
174 sc->sc_extendedops = NULL;
176 sc->sc_parse = controlparsefn;
178 /* Update slap_known_controls, too. */
179 if ( slap_known_controls == NULL ) {
180 slap_known_controls = (char **)SLAP_MALLOC( 2 * sizeof(char *) );
181 if ( slap_known_controls == NULL ) {
182 if ( sc->sc_extendedops != NULL ) ldap_charray_free( sc->sc_extendedops );
184 return LDAP_NO_MEMORY;
186 slap_known_controls[0] = ch_strdup( sc->sc_oid );
187 slap_known_controls[1] = NULL;
189 for ( i = 0; slap_known_controls[i] != NULL; i++ )
191 slap_known_controls = (char **)SLAP_REALLOC( slap_known_controls, (i + 2) * sizeof(char *) );
192 if ( slap_known_controls == NULL ) {
193 if ( sc->sc_extendedops != NULL ) ldap_charray_free( sc->sc_extendedops );
195 return LDAP_NO_MEMORY;
197 slap_known_controls[i++] = ch_strdup( sc->sc_oid );
198 slap_known_controls[i] = NULL;
201 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
202 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
208 * One-time initialization of internal controls.
211 slap_controls_init( void )
214 struct slap_control *sc;
218 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
219 rc = register_supported_control( control_defs[i].sc_oid,
220 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
221 control_defs[i].sc_parse );
222 if ( rc != LDAP_SUCCESS )
230 * Free memory associated with list of supported controls.
233 controls_destroy( void )
235 struct slap_control *sc;
237 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
238 sc = LDAP_SLIST_FIRST(&controls_list);
239 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
241 ch_free( sc->sc_oid );
242 if ( sc->sc_extendedops != NULL ) {
243 ldap_charray_free( sc->sc_extendedops );
247 ldap_charray_free( slap_known_controls );
251 * Format the supportedControl attribute of the root DSE,
252 * detailing which controls are supported by the directory
256 controls_root_dse_info( Entry *e )
258 AttributeDescription *ad_supportedControl
259 = slap_schema.si_ad_supportedControl;
260 struct berval vals[2];
261 struct slap_control *sc;
263 vals[1].bv_val = NULL;
266 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
267 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
269 vals[0].bv_val = sc->sc_oid;
270 vals[0].bv_len = strlen( sc->sc_oid );
272 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
281 * Return a list of OIDs and operation masks for supported
282 * controls. Used by SLAPI.
285 get_supported_controls(char ***ctrloidsp,
286 slap_mask_t **ctrlmasks)
292 struct slap_control *sc;
296 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
306 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
307 if ( oids == NULL ) {
308 return LDAP_NO_MEMORY;
310 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
311 if ( masks == NULL ) {
313 return LDAP_NO_MEMORY;
318 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
319 oids[n] = ch_strdup( sc->sc_oid );
320 masks[n] = sc->sc_mask;
333 * Find a control given its OID.
335 static struct slap_control *
336 find_ctrl( const char *oid )
338 struct slap_control *sc;
340 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
341 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
349 void slap_free_ctrls(
356 for (i=0; ctrls[i]; i++) {
357 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
359 op->o_tmpfree( ctrls, op->o_tmpmemctx );
371 BerElement *ber = op->o_ber;
372 struct slap_control *sc;
375 len = ber_pvt_ber_remaining(ber);
379 rs->sr_err = LDAP_SUCCESS;
383 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
384 if( tag == LBER_ERROR ) {
385 rs->sr_err = SLAPD_DISCONNECT;
386 rs->sr_text = "unexpected data in PDU";
393 LDAP_LOG( OPERATION, ENTRY,
394 "get_ctrls: conn %lu\n", op->o_connid, 0, 0 );
396 Debug( LDAP_DEBUG_TRACE,
397 "=> get_ctrls\n", 0, 0, 0 );
400 if( op->o_protocol < LDAP_VERSION3 ) {
401 rs->sr_err = SLAPD_DISCONNECT;
402 rs->sr_text = "controls require LDAPv3";
406 /* one for first control, one for termination */
407 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
410 if( op->ctrls == NULL ) {
411 rs->sr_err = LDAP_NO_MEMORY;
412 rs->sr_text = "no memory";
417 op->o_ctrls[nctrls] = NULL;
419 /* step through each element */
420 for( tag = ber_first_element( ber, &len, &opaque );
422 tag = ber_next_element( ber, &len, opaque ) )
425 LDAPControl **tctrls;
427 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
428 memset(c, 0, sizeof(LDAPControl));
430 /* allocate pointer space for current controls (nctrls)
431 * + this control + extra NULL
433 tctrls = op->o_tmprealloc( op->o_ctrls,
434 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
437 if( tctrls == NULL ) {
439 ldap_controls_free(op->o_ctrls);
442 rs->sr_err = LDAP_NO_MEMORY;
443 rs->sr_text = "no memory";
447 op->o_ctrls = tctrls;
449 op->o_ctrls[nctrls++] = c;
450 op->o_ctrls[nctrls] = NULL;
452 tag = ber_scanf( ber, "{m" /*}*/, &bv );
453 c->ldctl_oid = bv.bv_val;
455 if( tag == LBER_ERROR ) {
457 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
458 op->o_connid, 0, 0 );
460 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
464 slap_free_ctrls( op, op->o_ctrls );
466 rs->sr_err = SLAPD_DISCONNECT;
467 rs->sr_text = "decoding controls error";
470 } else if( c->ldctl_oid == NULL ) {
472 LDAP_LOG( OPERATION, INFO,
473 "get_ctrls: conn %lu got emtpy OID.\n",
474 op->o_connid, 0, 0 );
476 Debug( LDAP_DEBUG_TRACE,
477 "get_ctrls: conn %lu got emtpy OID.\n",
478 op->o_connid, 0, 0 );
481 slap_free_ctrls( op, op->o_ctrls );
483 rs->sr_err = LDAP_PROTOCOL_ERROR;
484 rs->sr_text = "OID field is empty";
488 tag = ber_peek_tag( ber, &len );
490 if( tag == LBER_BOOLEAN ) {
492 tag = ber_scanf( ber, "b", &crit );
494 if( tag == LBER_ERROR ) {
496 LDAP_LOG( OPERATION, INFO,
497 "get_ctrls: conn %lu get crit failed.\n",
498 op->o_connid, 0, 0 );
500 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
503 slap_free_ctrls( op, op->o_ctrls );
505 rs->sr_err = SLAPD_DISCONNECT;
506 rs->sr_text = "decoding controls error";
510 c->ldctl_iscritical = (crit != 0);
511 tag = ber_peek_tag( ber, &len );
514 if( tag == LBER_OCTETSTRING ) {
515 tag = ber_scanf( ber, "m", &c->ldctl_value );
517 if( tag == LBER_ERROR ) {
519 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
520 "%s (%scritical): get value failed.\n",
521 op->o_connid, c->ldctl_oid,
522 c->ldctl_iscritical ? "" : "non" );
524 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
525 "%s (%scritical): get value failed.\n",
526 op->o_connid, c->ldctl_oid,
527 c->ldctl_iscritical ? "" : "non" );
529 slap_free_ctrls( op, op->o_ctrls );
531 rs->sr_err = SLAPD_DISCONNECT;
532 rs->sr_text = "decoding controls error";
538 LDAP_LOG( OPERATION, INFO,
539 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
540 op->o_connid, c->ldctl_oid, c->ldctl_iscritical ? "" : "non" );
542 Debug( LDAP_DEBUG_TRACE,
543 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
544 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
547 sc = find_ctrl( c->ldctl_oid );
549 /* recognized control */
551 switch( op->o_tag ) {
553 tagmask = SLAP_CTRL_ADD;
556 tagmask = SLAP_CTRL_BIND;
558 case LDAP_REQ_COMPARE:
559 tagmask = SLAP_CTRL_COMPARE;
561 case LDAP_REQ_DELETE:
562 tagmask = SLAP_CTRL_DELETE;
564 case LDAP_REQ_MODIFY:
565 tagmask = SLAP_CTRL_MODIFY;
567 case LDAP_REQ_RENAME:
568 tagmask = SLAP_CTRL_RENAME;
570 case LDAP_REQ_SEARCH:
571 tagmask = SLAP_CTRL_SEARCH;
573 case LDAP_REQ_UNBIND:
574 tagmask = SLAP_CTRL_UNBIND;
576 case LDAP_REQ_ABANDON:
577 tagmask = SLAP_CTRL_ABANDON;
579 case LDAP_REQ_EXTENDED:
581 assert( op->ore_reqoid.bv_val != NULL );
582 if( sc->sc_extendedops != NULL ) {
584 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
585 if( strcmp( op->ore_reqoid.bv_val, sc->sc_extendedops[i] )
595 rs->sr_err = LDAP_OTHER;
596 rs->sr_text = "controls internal error";
600 if (( sc->sc_mask & tagmask ) == tagmask ) {
601 /* available extension */
603 if( !sc->sc_parse ) {
604 rs->sr_err = LDAP_OTHER;
605 rs->sr_text = "not yet implemented";
609 rs->sr_err = sc->sc_parse( op, rs, c );
611 if( rs->sr_err != LDAP_SUCCESS ) goto return_results;
613 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
614 /* kludge to disable backend_control() check */
615 c->ldctl_iscritical = 0;
617 } else if ( tagmask == SLAP_CTRL_SEARCH &&
618 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
620 /* kludge to disable backend_control() check */
621 c->ldctl_iscritical = 0;
624 } else if( c->ldctl_iscritical ) {
625 /* unavailable CRITICAL control */
626 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
627 rs->sr_text = "critical extension is unavailable";
631 } else if( c->ldctl_iscritical ) {
632 /* unrecognized CRITICAL control */
633 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
634 rs->sr_text = "critical extension is not recognized";
641 LDAP_LOG( OPERATION, RESULTS,
642 "get_ctrls: n=%d rc=%d err=\"%s\"\n",
643 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
645 Debug( LDAP_DEBUG_TRACE,
646 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
647 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
650 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
651 if( rs->sr_err == SLAPD_DISCONNECT ) {
652 rs->sr_err = LDAP_PROTOCOL_ERROR;
653 send_ldap_disconnect( op, rs );
654 rs->sr_err = SLAPD_DISCONNECT;
656 send_ldap_result( op, rs );
663 static int parseModifyIncrement (
669 if ( op->o_parseModifyIncrement != SLAP_NO_CONTROL ) {
670 rs->sr_text = "modifyIncrement control specified multiple times";
671 return LDAP_PROTOCOL_ERROR;
675 if ( ctrl->ldctl_value.bv_len ) {
676 rs->sr_text = "modifyIncrement control value not empty";
677 return LDAP_PROTOCOL_ERROR;
681 op->o_parseModifyIncrement = ctrl->ldctl_iscritical
682 ? SLAP_CRITICAL_CONTROL
683 : SLAP_NONCRITICAL_CONTROL;
689 static int parseManageDSAit (
694 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
695 rs->sr_text = "manageDSAit control specified multiple times";
696 return LDAP_PROTOCOL_ERROR;
699 if ( ctrl->ldctl_value.bv_len ) {
700 rs->sr_text = "manageDSAit control value not empty";
701 return LDAP_PROTOCOL_ERROR;
704 op->o_managedsait = ctrl->ldctl_iscritical
705 ? SLAP_CRITICAL_CONTROL
706 : SLAP_NONCRITICAL_CONTROL;
711 static int parseProxyAuthz (
717 struct berval dn = { 0, NULL };
719 if ( op->o_proxy_authz != SLAP_NO_CONTROL ) {
720 rs->sr_text = "proxy authorization control specified multiple times";
721 return LDAP_PROTOCOL_ERROR;
724 op->o_proxy_authz = ctrl->ldctl_iscritical
725 ? SLAP_CRITICAL_CONTROL
726 : SLAP_NONCRITICAL_CONTROL;
729 LDAP_LOG( OPERATION, ARGS,
730 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
732 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
735 Debug( LDAP_DEBUG_ARGS,
736 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
738 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
742 if( ctrl->ldctl_value.bv_len == 0 ) {
744 LDAP_LOG( OPERATION, RESULTS,
745 "parseProxyAuthz: conn=%lu anonymous\n",
746 op->o_connid, 0, 0 );
748 Debug( LDAP_DEBUG_TRACE,
749 "parseProxyAuthz: conn=%lu anonymous\n",
750 op->o_connid, 0, 0 );
754 free( op->o_dn.bv_val );
756 op->o_dn.bv_val = ch_strdup( "" );
758 free( op->o_ndn.bv_val );
759 op->o_ndn.bv_len = 0;
760 op->o_ndn.bv_val = ch_strdup( "" );
765 rc = slap_sasl_getdn( op->o_conn, op,
766 ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len,
767 NULL, &dn, SLAP_GETDN_AUTHZID );
769 if( rc != LDAP_SUCCESS || !dn.bv_len ) {
771 ch_free( dn.bv_val );
773 rs->sr_text = "authzId mapping failed";
774 return LDAP_PROXY_AUTHZ_FAILURE;
779 LDAP_LOG( OPERATION, RESULTS,
780 "parseProxyAuthz: conn=%lu \"%s\"\n",
782 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
784 Debug( LDAP_DEBUG_TRACE,
785 "parseProxyAuthz: conn=%lu \"%s\"\n",
787 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
790 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
793 ch_free( dn.bv_val );
794 rs->sr_text = "not authorized to assume identity";
795 return LDAP_PROXY_AUTHZ_FAILURE;
798 ch_free( op->o_dn.bv_val );
799 ch_free( op->o_ndn.bv_val );
801 op->o_dn.bv_val = NULL;
805 * NOTE: since slap_sasl_getdn() returns a normalized dn,
806 * from now on op->o_dn is normalized
808 ber_dupbv( &op->o_dn, &dn );
813 static int parseNoOp (
818 if ( op->o_noop != SLAP_NO_CONTROL ) {
819 rs->sr_text = "noop control specified multiple times";
820 return LDAP_PROTOCOL_ERROR;
823 if ( ctrl->ldctl_value.bv_len ) {
824 rs->sr_text = "noop control value not empty";
825 return LDAP_PROTOCOL_ERROR;
828 op->o_noop = ctrl->ldctl_iscritical
829 ? SLAP_CRITICAL_CONTROL
830 : SLAP_NONCRITICAL_CONTROL;
835 static int parsePagedResults (
843 struct berval cookie = { 0, NULL };
845 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
846 rs->sr_text = "paged results control specified multiple times";
847 return LDAP_PROTOCOL_ERROR;
850 if ( ctrl->ldctl_value.bv_len == 0 ) {
851 rs->sr_text = "paged results control value is empty (or absent)";
852 return LDAP_PROTOCOL_ERROR;
855 /* Parse the control value
856 * realSearchControlValue ::= SEQUENCE {
857 * size INTEGER (0..maxInt),
858 * -- requested page size from client
859 * -- result set size estimate from server
860 * cookie OCTET STRING
863 ber = ber_init( &ctrl->ldctl_value );
865 rs->sr_text = "internal error";
869 tag = ber_scanf( ber, "{im}", &size, &cookie );
870 (void) ber_free( ber, 1 );
872 if( tag == LBER_ERROR ) {
873 rs->sr_text = "paged results control could not be decoded";
874 return LDAP_PROTOCOL_ERROR;
878 rs->sr_text = "paged results control size invalid";
879 return LDAP_PROTOCOL_ERROR;
882 if( cookie.bv_len ) {
883 PagedResultsCookie reqcookie;
884 if( cookie.bv_len != sizeof( reqcookie ) ) {
886 rs->sr_text = "paged results cookie is invalid";
887 return LDAP_PROTOCOL_ERROR;
890 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
892 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
894 rs->sr_text = "paged results cookie is invalid";
895 return LDAP_PROTOCOL_ERROR;
897 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
898 rs->sr_text = "paged results cookie is invalid or old";
899 return LDAP_UNWILLING_TO_PERFORM;
902 /* Initial request. Initialize state. */
903 op->o_pagedresults_state.ps_cookie = 0;
904 op->o_pagedresults_state.ps_id = NOID;
907 op->o_pagedresults_size = size;
909 op->o_pagedresults = ctrl->ldctl_iscritical
910 ? SLAP_CRITICAL_CONTROL
911 : SLAP_NONCRITICAL_CONTROL;
916 static int parseAssert (
922 struct berval fstr = { 0, NULL };
923 const char *err_msg = "";
925 if ( op->o_assert != SLAP_NO_CONTROL ) {
926 rs->sr_text = "assert control specified multiple times";
927 return LDAP_PROTOCOL_ERROR;
930 if ( ctrl->ldctl_value.bv_len == 0 ) {
931 rs->sr_text = "assert control value is empty (or absent)";
932 return LDAP_PROTOCOL_ERROR;
935 ber = ber_init( &(ctrl->ldctl_value) );
937 rs->sr_text = "assert control: internal error";
941 rs->sr_err = get_filter( op, ber, &(op->o_assertion), &rs->sr_text);
943 if( rs->sr_err != LDAP_SUCCESS ) {
944 if( rs->sr_err == SLAPD_DISCONNECT ) {
945 rs->sr_err = LDAP_PROTOCOL_ERROR;
946 send_ldap_disconnect( op, rs );
947 rs->sr_err = SLAPD_DISCONNECT;
949 send_ldap_result( op, rs );
951 if( op->o_assertion != NULL ) {
952 filter_free_x( op, op->o_assertion );
958 filter2bv_x( op, op->o_assertion, &fstr );
961 LDAP_LOG( OPERATION, ARGS,
962 "parseAssert: conn %ld assert: %s\n",
963 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
965 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
966 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
968 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
971 op->o_assert = ctrl->ldctl_iscritical
972 ? SLAP_CRITICAL_CONTROL
973 : SLAP_NONCRITICAL_CONTROL;
975 rs->sr_err = LDAP_SUCCESS;
979 static int parsePreRead (
984 ber_len_t siz, off, i;
985 AttributeName *an = NULL;
988 if ( op->o_preread != SLAP_NO_CONTROL ) {
989 rs->sr_text = "preread control specified multiple times";
990 return LDAP_PROTOCOL_ERROR;
993 if ( ctrl->ldctl_value.bv_len == 0 ) {
994 rs->sr_text = "preread control value is empty (or absent)";
995 return LDAP_PROTOCOL_ERROR;
998 ber = ber_init( &(ctrl->ldctl_value) );
1000 rs->sr_text = "preread control: internal error";
1004 siz = sizeof( AttributeName );
1005 off = offsetof( AttributeName, an_name );
1006 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1007 rs->sr_text = "preread control: decoding error";
1008 return LDAP_PROTOCOL_ERROR;
1011 for( i=0; i<siz; i++ ) {
1012 int rc = LDAP_SUCCESS;
1013 const char *dummy = NULL;
1015 an[i].an_desc = NULL;
1017 an[i].an_oc_exclude = 0;
1018 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1019 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1020 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1025 op->o_preread = ctrl->ldctl_iscritical
1026 ? SLAP_CRITICAL_CONTROL
1027 : SLAP_NONCRITICAL_CONTROL;
1029 op->o_preread_attrs = an;
1031 rs->sr_err = LDAP_SUCCESS;
1032 return LDAP_SUCCESS;
1035 static int parsePostRead (
1040 ber_len_t siz, off, i;
1041 AttributeName *an = NULL;
1044 if ( op->o_postread != SLAP_NO_CONTROL ) {
1045 rs->sr_text = "postread control specified multiple times";
1046 return LDAP_PROTOCOL_ERROR;
1049 if ( ctrl->ldctl_value.bv_len == 0 ) {
1050 rs->sr_text = "postread control value is empty (or absent)";
1051 return LDAP_PROTOCOL_ERROR;
1054 ber = ber_init( &(ctrl->ldctl_value) );
1056 rs->sr_text = "postread control: internal error";
1060 siz = sizeof( AttributeName );
1061 off = offsetof( AttributeName, an_name );
1062 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1063 rs->sr_text = "postread control: decoding error";
1064 return LDAP_PROTOCOL_ERROR;
1067 for( i=0; i<siz; i++ ) {
1068 int rc = LDAP_SUCCESS;
1069 const char *dummy = NULL;
1071 an[i].an_desc = NULL;
1073 an[i].an_oc_exclude = 0;
1074 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1075 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1076 rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1081 op->o_postread = ctrl->ldctl_iscritical
1082 ? SLAP_CRITICAL_CONTROL
1083 : SLAP_NONCRITICAL_CONTROL;
1085 op->o_postread_attrs = an;
1087 rs->sr_err = LDAP_SUCCESS;
1088 return LDAP_SUCCESS;
1091 int parseValuesReturnFilter (
1097 struct berval fstr = { 0, NULL };
1098 const char *err_msg = "";
1100 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
1101 rs->sr_text = "valuesReturnFilter control specified multiple times";
1102 return LDAP_PROTOCOL_ERROR;
1105 if ( ctrl->ldctl_value.bv_len == 0 ) {
1106 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1107 return LDAP_PROTOCOL_ERROR;
1110 ber = ber_init( &(ctrl->ldctl_value) );
1112 rs->sr_text = "internal error";
1116 rs->sr_err = get_vrFilter( op, ber, &(op->o_vrFilter), &rs->sr_text);
1118 if( rs->sr_err != LDAP_SUCCESS ) {
1119 if( rs->sr_err == SLAPD_DISCONNECT ) {
1120 rs->sr_err = LDAP_PROTOCOL_ERROR;
1121 send_ldap_disconnect( op, rs );
1122 rs->sr_err = SLAPD_DISCONNECT;
1124 send_ldap_result( op, rs );
1126 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1130 vrFilter2bv( op, op->o_vrFilter, &fstr );
1134 LDAP_LOG( OPERATION, ARGS,
1135 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
1136 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1138 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1139 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1141 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1144 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1145 ? SLAP_CRITICAL_CONTROL
1146 : SLAP_NONCRITICAL_CONTROL;
1148 rs->sr_err = LDAP_SUCCESS;
1149 return LDAP_SUCCESS;
1152 #ifdef LDAP_CONTROL_SUBENTRIES
1153 static int parseSubentries (
1158 if ( op->o_subentries != SLAP_NO_CONTROL ) {
1159 rs->sr_text = "subentries control specified multiple times";
1160 return LDAP_PROTOCOL_ERROR;
1163 /* FIXME: should use BER library */
1164 if( ( ctrl->ldctl_value.bv_len != 3 )
1165 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1166 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1168 rs->sr_text = "subentries control value encoding is bogus";
1169 return LDAP_PROTOCOL_ERROR;
1172 op->o_subentries = ctrl->ldctl_iscritical
1173 ? SLAP_CRITICAL_CONTROL
1174 : SLAP_NONCRITICAL_CONTROL;
1176 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
1178 return LDAP_SUCCESS;
1182 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1183 static int parsePermissiveModify (
1188 if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
1189 rs->sr_text = "permissiveModify control specified multiple times";
1190 return LDAP_PROTOCOL_ERROR;
1193 if ( ctrl->ldctl_value.bv_len ) {
1194 rs->sr_text = "permissiveModify control value not empty";
1195 return LDAP_PROTOCOL_ERROR;
1198 op->o_permissive_modify = ctrl->ldctl_iscritical
1199 ? SLAP_CRITICAL_CONTROL
1200 : SLAP_NONCRITICAL_CONTROL;
1202 return LDAP_SUCCESS;
1206 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1207 static int parseDomainScope (
1212 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1213 rs->sr_text = "domainScope control specified multiple times";
1214 return LDAP_PROTOCOL_ERROR;
1217 if ( ctrl->ldctl_value.bv_len ) {
1218 rs->sr_text = "domainScope control value not empty";
1219 return LDAP_PROTOCOL_ERROR;
1222 op->o_domain_scope = ctrl->ldctl_iscritical
1223 ? SLAP_CRITICAL_CONTROL
1224 : SLAP_NONCRITICAL_CONTROL;
1226 return LDAP_SUCCESS;
1230 #ifdef LDAP_CONTROL_X_TREE_DELETE
1231 static int parseTreeDelete (
1236 if ( op->o_tree_delete != SLAP_NO_CONTROL ) {
1237 rs->sr_text = "treeDelete control specified multiple times";
1238 return LDAP_PROTOCOL_ERROR;
1241 if ( ctrl->ldctl_value.bv_len ) {
1242 rs->sr_text = "treeDelete control value not empty";
1243 return LDAP_PROTOCOL_ERROR;
1246 op->o_tree_delete = ctrl->ldctl_iscritical
1247 ? SLAP_CRITICAL_CONTROL
1248 : SLAP_NONCRITICAL_CONTROL;
1250 return LDAP_SUCCESS;
1254 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1255 static int parseSearchOptions (
1261 ber_int_t search_flags;
1263 if ( ctrl->ldctl_value.bv_len == 0 ) {
1264 rs->sr_text = "searchOptions control value not empty";
1265 return LDAP_PROTOCOL_ERROR;
1268 ber = ber_init( &ctrl->ldctl_value );
1270 rs->sr_text = "internal error";
1274 if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1275 rs->sr_text = "searchOptions control decoding error";
1276 return LDAP_PROTOCOL_ERROR;
1279 (void) ber_free( ber, 1 );
1281 if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1282 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1283 rs->sr_text = "searchOptions control specified multiple times or with domainScope control";
1284 return LDAP_PROTOCOL_ERROR;
1287 op->o_domain_scope = ctrl->ldctl_iscritical
1288 ? SLAP_CRITICAL_CONTROL
1289 : SLAP_NONCRITICAL_CONTROL;
1292 if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1293 /* Other search flags not recognised so far */
1294 rs->sr_text = "searchOptions contained invalid flag";
1295 return LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
1298 return LDAP_SUCCESS;
1302 static int parseLDAPsync (
1311 struct slap_session_entry *se;
1313 if ( op->o_sync != SLAP_NO_CONTROL ) {
1314 rs->sr_text = "LDAP Sync control specified multiple times";
1315 return LDAP_PROTOCOL_ERROR;
1318 if ( ctrl->ldctl_value.bv_len == 0 ) {
1319 rs->sr_text = "LDAP Sync control value is empty (or absent)";
1320 return LDAP_PROTOCOL_ERROR;
1323 /* Parse the control value
1324 * syncRequestValue ::= SEQUENCE {
1329 * refreshAndPersist (3)
1331 * cookie syncCookie OPTIONAL
1335 ber = ber_init( &ctrl->ldctl_value );
1337 rs->sr_text = "internal error";
1341 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1342 rs->sr_text = "LDAP Sync control : mode decoding error";
1343 return LDAP_PROTOCOL_ERROR;
1347 case LDAP_SYNC_REFRESH_ONLY:
1348 mode = SLAP_SYNC_REFRESH;
1350 case LDAP_SYNC_REFRESH_AND_PERSIST:
1351 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1354 rs->sr_text = "LDAP Sync control : unknown update mode";
1355 return LDAP_PROTOCOL_ERROR;
1358 tag = ber_peek_tag( ber, &len );
1360 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1361 struct berval tmp_bv;
1362 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1363 rs->sr_text = "LDAP Sync control : cookie decoding error";
1364 return LDAP_PROTOCOL_ERROR;
1366 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1367 slap_parse_sync_cookie( &op->o_sync_state );
1369 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1370 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1371 rs->sr_text = "LDAP Sync control : rhint decoding error";
1372 return LDAP_PROTOCOL_ERROR;
1375 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1376 rs->sr_text = "LDAP Sync control : decoding error";
1377 return LDAP_PROTOCOL_ERROR;
1380 (void) ber_free( ber, 1 );
1382 op->o_sync_mode = (char) mode;
1384 op->o_sync = ctrl->ldctl_iscritical
1385 ? SLAP_CRITICAL_CONTROL
1386 : SLAP_NONCRITICAL_CONTROL;
1388 return LDAP_SUCCESS;