2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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;
39 #ifdef LDAP_CONTROL_SUBENTRIES
40 static SLAP_CTRL_PARSE_FN parseSubentries;
42 static SLAP_CTRL_PARSE_FN parseLDAPsync;
44 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
46 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
47 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
53 /* Operations supported by control */
56 /* Extended operations supported by control */
57 char **sc_extendedops;
59 /* Control parsing callback */
60 SLAP_CTRL_PARSE_FN *sc_parse;
62 LDAP_SLIST_ENTRY(slap_control) sc_next;
65 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
66 = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
69 * all known request control OIDs should be added to this list
71 char **slap_known_controls = NULL;
73 static char *proxy_authz_extops[] = {
74 LDAP_EXOP_MODIFY_PASSWD,
79 static struct slap_control control_defs[] = {
80 { LDAP_CONTROL_ASSERT,
81 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
82 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
83 { LDAP_CONTROL_PRE_READ,
84 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
85 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
86 { LDAP_CONTROL_POST_READ,
87 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
88 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
89 { LDAP_CONTROL_VALUESRETURNFILTER,
90 SLAP_CTRL_SEARCH, NULL,
91 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
92 #ifdef LDAP_CONTROL_PAGEDRESULTS
93 { LDAP_CONTROL_PAGEDRESULTS,
94 SLAP_CTRL_SEARCH, NULL,
95 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_SUBENTRIES
108 { LDAP_CONTROL_SUBENTRIES,
109 SLAP_CTRL_SEARCH, NULL,
110 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
113 SLAP_CTRL_ACCESS, NULL,
114 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
116 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
117 parseLDAPsync, LDAP_SLIST_ENTRY_INITIALIZER(next) },
118 { LDAP_CONTROL_MODIFY_INCREMENT,
119 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
120 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
121 { LDAP_CONTROL_MANAGEDSAIT,
122 SLAP_CTRL_ACCESS, NULL,
123 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
124 { LDAP_CONTROL_PROXY_AUTHZ,
125 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
126 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
127 { NULL, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
131 * Register a supported control.
133 * This can be called by an OpenLDAP plugin or, indirectly, by a
134 * SLAPI plugin calling slapi_register_supported_control().
137 register_supported_control(const char *controloid,
138 slap_mask_t controlmask,
140 SLAP_CTRL_PARSE_FN *controlparsefn)
142 struct slap_control *sc;
145 if ( controloid == NULL ) {
146 return LDAP_PARAM_ERROR;
149 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
151 return LDAP_NO_MEMORY;
153 sc->sc_oid = ch_strdup( controloid );
154 sc->sc_mask = controlmask;
155 if ( controlexops != NULL ) {
156 sc->sc_extendedops = ldap_charray_dup( controlexops );
157 if ( sc->sc_extendedops == NULL ) {
159 return LDAP_NO_MEMORY;
162 sc->sc_extendedops = NULL;
164 sc->sc_parse = controlparsefn;
166 /* Update slap_known_controls, too. */
167 if ( slap_known_controls == NULL ) {
168 slap_known_controls = (char **)SLAP_MALLOC( 2 * sizeof(char *) );
169 if ( slap_known_controls == NULL ) {
170 if ( sc->sc_extendedops != NULL ) ldap_charray_free( sc->sc_extendedops );
172 return LDAP_NO_MEMORY;
174 slap_known_controls[0] = ch_strdup( sc->sc_oid );
175 slap_known_controls[1] = NULL;
177 for ( i = 0; slap_known_controls[i] != NULL; i++ )
179 slap_known_controls = (char **)SLAP_REALLOC( slap_known_controls, (i + 2) * sizeof(char *) );
180 if ( slap_known_controls == NULL ) {
181 if ( sc->sc_extendedops != NULL ) ldap_charray_free( sc->sc_extendedops );
183 return LDAP_NO_MEMORY;
185 slap_known_controls[i++] = ch_strdup( sc->sc_oid );
186 slap_known_controls[i] = NULL;
189 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
190 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
196 * One-time initialization of internal controls.
199 slap_controls_init( void )
202 struct slap_control *sc;
206 for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
207 rc = register_supported_control( control_defs[i].sc_oid,
208 control_defs[i].sc_mask, control_defs[i].sc_extendedops,
209 control_defs[i].sc_parse );
210 if ( rc != LDAP_SUCCESS )
218 * Free memory associated with list of supported controls.
221 controls_destroy( void )
223 struct slap_control *sc;
225 while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
226 sc = LDAP_SLIST_FIRST(&controls_list);
227 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
229 ch_free( sc->sc_oid );
230 if ( sc->sc_extendedops != NULL ) {
231 ldap_charray_free( sc->sc_extendedops );
235 ldap_charray_free( slap_known_controls );
239 * Format the supportedControl attribute of the root DSE,
240 * detailing which controls are supported by the directory
244 controls_root_dse_info( Entry *e )
246 AttributeDescription *ad_supportedControl
247 = slap_schema.si_ad_supportedControl;
248 struct berval vals[2];
249 struct slap_control *sc;
251 vals[1].bv_val = NULL;
254 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
255 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
257 vals[0].bv_val = sc->sc_oid;
258 vals[0].bv_len = strlen( sc->sc_oid );
260 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
269 * Return a list of OIDs and operation masks for supported
270 * controls. Used by SLAPI.
273 get_supported_controls(char ***ctrloidsp,
274 slap_mask_t **ctrlmasks)
280 struct slap_control *sc;
284 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
294 oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
295 if ( oids == NULL ) {
296 return LDAP_NO_MEMORY;
298 masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
299 if ( masks == NULL ) {
301 return LDAP_NO_MEMORY;
306 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
307 oids[n] = ch_strdup( sc->sc_oid );
308 masks[n] = sc->sc_mask;
321 * Find a control given its OID.
323 static struct slap_control *
324 find_ctrl( const char *oid )
326 struct slap_control *sc;
328 LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
329 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
337 void slap_free_ctrls(
344 for (i=0; ctrls[i]; i++) {
345 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
347 op->o_tmpfree( ctrls, op->o_tmpmemctx );
359 BerElement *ber = op->o_ber;
360 struct slap_control *sc;
363 len = ber_pvt_ber_remaining(ber);
367 rs->sr_err = LDAP_SUCCESS;
371 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
372 if( tag == LBER_ERROR ) {
373 rs->sr_err = SLAPD_DISCONNECT;
374 rs->sr_text = "unexpected data in PDU";
381 LDAP_LOG( OPERATION, ENTRY,
382 "get_ctrls: conn %lu\n", op->o_connid, 0, 0 );
384 Debug( LDAP_DEBUG_TRACE,
385 "=> get_ctrls\n", 0, 0, 0 );
388 if( op->o_protocol < LDAP_VERSION3 ) {
389 rs->sr_err = SLAPD_DISCONNECT;
390 rs->sr_text = "controls require LDAPv3";
394 /* one for first control, one for termination */
395 op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
398 if( op->ctrls == NULL ) {
399 rs->sr_err = LDAP_NO_MEMORY;
400 rs->sr_text = "no memory";
405 op->o_ctrls[nctrls] = NULL;
407 /* step through each element */
408 for( tag = ber_first_element( ber, &len, &opaque );
410 tag = ber_next_element( ber, &len, opaque ) )
413 LDAPControl **tctrls;
415 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
416 memset(c, 0, sizeof(LDAPControl));
418 /* allocate pointer space for current controls (nctrls)
419 * + this control + extra NULL
421 tctrls = op->o_tmprealloc( op->o_ctrls,
422 (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
425 if( tctrls == NULL ) {
427 ldap_controls_free(op->o_ctrls);
430 rs->sr_err = LDAP_NO_MEMORY;
431 rs->sr_text = "no memory";
435 op->o_ctrls = tctrls;
437 op->o_ctrls[nctrls++] = c;
438 op->o_ctrls[nctrls] = NULL;
440 tag = ber_scanf( ber, "{m" /*}*/, &bv );
441 c->ldctl_oid = bv.bv_val;
443 if( tag == LBER_ERROR ) {
445 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
446 op->o_connid, 0, 0 );
448 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
452 slap_free_ctrls( op, op->o_ctrls );
454 rs->sr_err = SLAPD_DISCONNECT;
455 rs->sr_text = "decoding controls error";
458 } else if( c->ldctl_oid == NULL ) {
460 LDAP_LOG( OPERATION, INFO,
461 "get_ctrls: conn %lu got emtpy OID.\n",
462 op->o_connid, 0, 0 );
464 Debug( LDAP_DEBUG_TRACE,
465 "get_ctrls: conn %lu got emtpy OID.\n",
466 op->o_connid, 0, 0 );
469 slap_free_ctrls( op, op->o_ctrls );
471 rs->sr_err = LDAP_PROTOCOL_ERROR;
472 rs->sr_text = "OID field is empty";
476 tag = ber_peek_tag( ber, &len );
478 if( tag == LBER_BOOLEAN ) {
480 tag = ber_scanf( ber, "b", &crit );
482 if( tag == LBER_ERROR ) {
484 LDAP_LOG( OPERATION, INFO,
485 "get_ctrls: conn %lu get crit failed.\n",
486 op->o_connid, 0, 0 );
488 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
491 slap_free_ctrls( op, op->o_ctrls );
493 rs->sr_err = SLAPD_DISCONNECT;
494 rs->sr_text = "decoding controls error";
498 c->ldctl_iscritical = (crit != 0);
499 tag = ber_peek_tag( ber, &len );
502 if( tag == LBER_OCTETSTRING ) {
503 tag = ber_scanf( ber, "m", &c->ldctl_value );
505 if( tag == LBER_ERROR ) {
507 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
508 "%s (%scritical): get value failed.\n",
509 op->o_connid, c->ldctl_oid,
510 c->ldctl_iscritical ? "" : "non" );
512 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
513 "%s (%scritical): get value failed.\n",
514 op->o_connid, c->ldctl_oid,
515 c->ldctl_iscritical ? "" : "non" );
517 slap_free_ctrls( op, op->o_ctrls );
519 rs->sr_err = SLAPD_DISCONNECT;
520 rs->sr_text = "decoding controls error";
526 LDAP_LOG( OPERATION, INFO,
527 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
528 op->o_connid, c->ldctl_oid, c->ldctl_iscritical ? "" : "non" );
530 Debug( LDAP_DEBUG_TRACE,
531 "=> get_ctrls: oid=\"%s\" (%scritical)\n",
532 c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
535 sc = find_ctrl( c->ldctl_oid );
537 /* recognized control */
539 switch( op->o_tag ) {
541 tagmask = SLAP_CTRL_ADD;
544 tagmask = SLAP_CTRL_BIND;
546 case LDAP_REQ_COMPARE:
547 tagmask = SLAP_CTRL_COMPARE;
549 case LDAP_REQ_DELETE:
550 tagmask = SLAP_CTRL_DELETE;
552 case LDAP_REQ_MODIFY:
553 tagmask = SLAP_CTRL_MODIFY;
555 case LDAP_REQ_RENAME:
556 tagmask = SLAP_CTRL_RENAME;
558 case LDAP_REQ_SEARCH:
559 tagmask = SLAP_CTRL_SEARCH;
561 case LDAP_REQ_UNBIND:
562 tagmask = SLAP_CTRL_UNBIND;
564 case LDAP_REQ_ABANDON:
565 tagmask = SLAP_CTRL_ABANDON;
567 case LDAP_REQ_EXTENDED:
569 assert( op->ore_reqoid.bv_val != NULL );
570 if( sc->sc_extendedops != NULL ) {
572 for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
573 if( strcmp( op->ore_reqoid.bv_val, sc->sc_extendedops[i] )
583 rs->sr_err = LDAP_OTHER;
584 rs->sr_text = "controls internal error";
588 if (( sc->sc_mask & tagmask ) == tagmask ) {
589 /* available extension */
591 if( !sc->sc_parse ) {
592 rs->sr_err = LDAP_OTHER;
593 rs->sr_text = "not yet implemented";
597 rs->sr_err = sc->sc_parse( op, rs, c );
599 if( rs->sr_err != LDAP_SUCCESS ) goto return_results;
601 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
602 /* kludge to disable backend_control() check */
603 c->ldctl_iscritical = 0;
605 } else if ( tagmask == SLAP_CTRL_SEARCH &&
606 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
608 /* kludge to disable backend_control() check */
609 c->ldctl_iscritical = 0;
612 } else if( c->ldctl_iscritical ) {
613 /* unavailable CRITICAL control */
614 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
615 rs->sr_text = "critical extension is unavailable";
619 } else if( c->ldctl_iscritical ) {
620 /* unrecognized CRITICAL control */
621 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
622 rs->sr_text = "critical extension is not recognized";
629 LDAP_LOG( OPERATION, RESULTS,
630 "get_ctrls: n=%d rc=%d err=\"%s\"\n",
631 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
633 Debug( LDAP_DEBUG_TRACE,
634 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
635 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
638 if( sendres && rs->sr_err != LDAP_SUCCESS ) {
639 if( rs->sr_err == SLAPD_DISCONNECT ) {
640 rs->sr_err = LDAP_PROTOCOL_ERROR;
641 send_ldap_disconnect( op, rs );
642 rs->sr_err = SLAPD_DISCONNECT;
644 send_ldap_result( op, rs );
651 static int parseModifyIncrement (
657 if ( op->o_parseModifyIncrement != SLAP_NO_CONTROL ) {
658 rs->sr_text = "modifyIncrement control specified multiple times";
659 return LDAP_PROTOCOL_ERROR;
663 if ( ctrl->ldctl_value.bv_len ) {
664 rs->sr_text = "modifyIncrement control value not empty";
665 return LDAP_PROTOCOL_ERROR;
669 op->o_parseModifyIncrement = ctrl->ldctl_iscritical
670 ? SLAP_CRITICAL_CONTROL
671 : SLAP_NONCRITICAL_CONTROL;
677 static int parseManageDSAit (
682 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
683 rs->sr_text = "manageDSAit control specified multiple times";
684 return LDAP_PROTOCOL_ERROR;
687 if ( ctrl->ldctl_value.bv_len ) {
688 rs->sr_text = "manageDSAit control value not empty";
689 return LDAP_PROTOCOL_ERROR;
692 op->o_managedsait = ctrl->ldctl_iscritical
693 ? SLAP_CRITICAL_CONTROL
694 : SLAP_NONCRITICAL_CONTROL;
699 static int parseProxyAuthz (
705 struct berval dn = { 0, NULL };
707 if ( op->o_proxy_authz != SLAP_NO_CONTROL ) {
708 rs->sr_text = "proxy authorization control specified multiple times";
709 return LDAP_PROTOCOL_ERROR;
712 op->o_proxy_authz = ctrl->ldctl_iscritical
713 ? SLAP_CRITICAL_CONTROL
714 : SLAP_NONCRITICAL_CONTROL;
717 LDAP_LOG( OPERATION, ARGS,
718 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
720 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
723 Debug( LDAP_DEBUG_ARGS,
724 "parseProxyAuthz: conn %lu authzid=\"%s\"\n",
726 ctrl->ldctl_value.bv_len ? ctrl->ldctl_value.bv_val : "anonymous",
730 if( ctrl->ldctl_value.bv_len == 0 ) {
732 LDAP_LOG( OPERATION, RESULTS,
733 "parseProxyAuthz: conn=%lu anonymous\n",
734 op->o_connid, 0, 0 );
736 Debug( LDAP_DEBUG_TRACE,
737 "parseProxyAuthz: conn=%lu anonymous\n",
738 op->o_connid, 0, 0 );
742 free( op->o_dn.bv_val );
744 op->o_dn.bv_val = ch_strdup( "" );
746 free( op->o_ndn.bv_val );
747 op->o_ndn.bv_len = 0;
748 op->o_ndn.bv_val = ch_strdup( "" );
753 rc = slap_sasl_getdn( op->o_conn, op,
754 ctrl->ldctl_value.bv_val, ctrl->ldctl_value.bv_len,
755 NULL, &dn, SLAP_GETDN_AUTHZID );
757 if( rc != LDAP_SUCCESS || !dn.bv_len ) {
759 ch_free( dn.bv_val );
761 rs->sr_text = "authzId mapping failed";
762 return LDAP_PROXY_AUTHZ_FAILURE;
766 LDAP_LOG( OPERATION, RESULTS,
767 "parseProxyAuthz: conn=%lu \"%s\"\n",
769 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
771 Debug( LDAP_DEBUG_TRACE,
772 "parseProxyAuthz: conn=%lu \"%s\"\n",
774 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
777 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
780 ch_free( dn.bv_val );
781 rs->sr_text = "not authorized to assume identity";
782 return LDAP_PROXY_AUTHZ_FAILURE;
785 ch_free( op->o_dn.bv_val );
786 ch_free( op->o_ndn.bv_val );
788 op->o_dn.bv_val = NULL;
792 * NOTE: since slap_sasl_getdn() returns a normalized dn,
793 * from now on op->o_dn is normalized
795 ber_dupbv( &op->o_dn, &dn );
800 static int parseNoOp (
805 if ( op->o_noop != SLAP_NO_CONTROL ) {
806 rs->sr_text = "noop control specified multiple times";
807 return LDAP_PROTOCOL_ERROR;
810 if ( ctrl->ldctl_value.bv_len ) {
811 rs->sr_text = "noop control value not empty";
812 return LDAP_PROTOCOL_ERROR;
815 op->o_noop = ctrl->ldctl_iscritical
816 ? SLAP_CRITICAL_CONTROL
817 : SLAP_NONCRITICAL_CONTROL;
822 #ifdef LDAP_CONTROL_PAGEDRESULTS
823 static int parsePagedResults (
831 struct berval cookie = { 0, NULL };
833 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
834 rs->sr_text = "paged results control specified multiple times";
835 return LDAP_PROTOCOL_ERROR;
838 if ( ctrl->ldctl_value.bv_len == 0 ) {
839 rs->sr_text = "paged results control value is empty (or absent)";
840 return LDAP_PROTOCOL_ERROR;
843 /* Parse the control value
844 * realSearchControlValue ::= SEQUENCE {
845 * size INTEGER (0..maxInt),
846 * -- requested page size from client
847 * -- result set size estimate from server
848 * cookie OCTET STRING
851 ber = ber_init( &ctrl->ldctl_value );
853 rs->sr_text = "internal error";
857 tag = ber_scanf( ber, "{im}", &size, &cookie );
858 (void) ber_free( ber, 1 );
860 if( tag == LBER_ERROR ) {
861 rs->sr_text = "paged results control could not be decoded";
862 return LDAP_PROTOCOL_ERROR;
866 rs->sr_text = "paged results control size invalid";
867 return LDAP_PROTOCOL_ERROR;
870 if( cookie.bv_len ) {
871 PagedResultsCookie reqcookie;
872 if( cookie.bv_len != sizeof( reqcookie ) ) {
874 rs->sr_text = "paged results cookie is invalid";
875 return LDAP_PROTOCOL_ERROR;
878 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
880 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
882 rs->sr_text = "paged results cookie is invalid";
883 return LDAP_PROTOCOL_ERROR;
885 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
886 rs->sr_text = "paged results cookie is invalid or old";
887 return LDAP_UNWILLING_TO_PERFORM;
890 /* Initial request. Initialize state. */
891 op->o_pagedresults_state.ps_cookie = 0;
892 op->o_pagedresults_state.ps_id = NOID;
895 op->o_pagedresults_size = size;
897 op->o_pagedresults = ctrl->ldctl_iscritical
898 ? SLAP_CRITICAL_CONTROL
899 : SLAP_NONCRITICAL_CONTROL;
905 static int parseAssert (
911 struct berval fstr = { 0, NULL };
912 const char *err_msg = "";
914 if ( op->o_assert != SLAP_NO_CONTROL ) {
915 rs->sr_text = "assert control specified multiple times";
916 return LDAP_PROTOCOL_ERROR;
919 if ( ctrl->ldctl_value.bv_len == 0 ) {
920 rs->sr_text = "assert control value is empty (or absent)";
921 return LDAP_PROTOCOL_ERROR;
924 ber = ber_init( &(ctrl->ldctl_value) );
926 rs->sr_text = "assert control: internal error";
930 rs->sr_err = get_filter( op, ber, &(op->o_assertion), &rs->sr_text);
932 if( rs->sr_err != LDAP_SUCCESS ) {
933 if( rs->sr_err == SLAPD_DISCONNECT ) {
934 rs->sr_err = LDAP_PROTOCOL_ERROR;
935 send_ldap_disconnect( op, rs );
936 rs->sr_err = SLAPD_DISCONNECT;
938 send_ldap_result( op, rs );
940 if( op->o_assertion != NULL ) {
941 filter_free_x( op, op->o_assertion );
947 filter2bv_x( op, op->o_assertion, &fstr );
950 LDAP_LOG( OPERATION, ARGS,
951 "parseAssert: conn %ld assert: %s\n",
952 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
954 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
955 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
957 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
960 op->o_assert = ctrl->ldctl_iscritical
961 ? SLAP_CRITICAL_CONTROL
962 : SLAP_NONCRITICAL_CONTROL;
964 rs->sr_err = LDAP_SUCCESS;
968 static int parsePreRead (
973 ber_len_t siz, off, i;
974 AttributeName *an = NULL;
977 if ( op->o_preread != SLAP_NO_CONTROL ) {
978 rs->sr_text = "preread control specified multiple times";
979 return LDAP_PROTOCOL_ERROR;
982 if ( ctrl->ldctl_value.bv_len == 0 ) {
983 rs->sr_text = "preread control value is empty (or absent)";
984 return LDAP_PROTOCOL_ERROR;
987 ber = ber_init( &(ctrl->ldctl_value) );
989 rs->sr_text = "preread control: internal error";
993 siz = sizeof( AttributeName );
994 off = offsetof( AttributeName, an_name );
995 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
996 rs->sr_text = "preread control: decoding error";
997 return LDAP_PROTOCOL_ERROR;
1000 for( i=0; i<siz; i++ ) {
1002 an[i].an_desc = NULL;
1004 slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1007 op->o_preread = ctrl->ldctl_iscritical
1008 ? SLAP_CRITICAL_CONTROL
1009 : SLAP_NONCRITICAL_CONTROL;
1011 op->o_preread_attrs = an;
1013 rs->sr_err = LDAP_SUCCESS;
1014 return LDAP_SUCCESS;
1017 static int parsePostRead (
1022 ber_len_t siz, off, i;
1023 AttributeName *an = NULL;
1026 if ( op->o_postread != SLAP_NO_CONTROL ) {
1027 rs->sr_text = "postread control specified multiple times";
1028 return LDAP_PROTOCOL_ERROR;
1031 if ( ctrl->ldctl_value.bv_len == 0 ) {
1032 rs->sr_text = "postread control value is empty (or absent)";
1033 return LDAP_PROTOCOL_ERROR;
1036 ber = ber_init( &(ctrl->ldctl_value) );
1038 rs->sr_text = "postread control: internal error";
1042 siz = sizeof( AttributeName );
1043 off = offsetof( AttributeName, an_name );
1044 if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1045 rs->sr_text = "postread control: decoding error";
1046 return LDAP_PROTOCOL_ERROR;
1049 for( i=0; i<siz; i++ ) {
1051 an[i].an_desc = NULL;
1053 slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1056 op->o_postread = ctrl->ldctl_iscritical
1057 ? SLAP_CRITICAL_CONTROL
1058 : SLAP_NONCRITICAL_CONTROL;
1060 op->o_postread_attrs = an;
1062 rs->sr_err = LDAP_SUCCESS;
1063 return LDAP_SUCCESS;
1066 int parseValuesReturnFilter (
1072 struct berval fstr = { 0, NULL };
1073 const char *err_msg = "";
1075 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
1076 rs->sr_text = "valuesReturnFilter control specified multiple times";
1077 return LDAP_PROTOCOL_ERROR;
1080 if ( ctrl->ldctl_value.bv_len == 0 ) {
1081 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1082 return LDAP_PROTOCOL_ERROR;
1085 ber = ber_init( &(ctrl->ldctl_value) );
1087 rs->sr_text = "internal error";
1091 rs->sr_err = get_vrFilter( op, ber, &(op->o_vrFilter), &rs->sr_text);
1093 if( rs->sr_err != LDAP_SUCCESS ) {
1094 if( rs->sr_err == SLAPD_DISCONNECT ) {
1095 rs->sr_err = LDAP_PROTOCOL_ERROR;
1096 send_ldap_disconnect( op, rs );
1097 rs->sr_err = SLAPD_DISCONNECT;
1099 send_ldap_result( op, rs );
1101 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter );
1105 vrFilter2bv( op, op->o_vrFilter, &fstr );
1109 LDAP_LOG( OPERATION, ARGS,
1110 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
1111 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1113 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1114 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1116 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1119 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1120 ? SLAP_CRITICAL_CONTROL
1121 : SLAP_NONCRITICAL_CONTROL;
1123 rs->sr_err = LDAP_SUCCESS;
1124 return LDAP_SUCCESS;
1127 #ifdef LDAP_CONTROL_SUBENTRIES
1128 static int parseSubentries (
1133 if ( op->o_subentries != SLAP_NO_CONTROL ) {
1134 rs->sr_text = "subentries control specified multiple times";
1135 return LDAP_PROTOCOL_ERROR;
1138 /* FIXME: should use BER library */
1139 if( ( ctrl->ldctl_value.bv_len != 3 )
1140 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1141 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1143 rs->sr_text = "subentries control value encoding is bogus";
1144 return LDAP_PROTOCOL_ERROR;
1147 op->o_subentries = ctrl->ldctl_iscritical
1148 ? SLAP_CRITICAL_CONTROL
1149 : SLAP_NONCRITICAL_CONTROL;
1151 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
1153 return LDAP_SUCCESS;
1157 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1158 static int parsePermissiveModify (
1163 if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
1164 rs->sr_text = "permissiveModify control specified multiple times";
1165 return LDAP_PROTOCOL_ERROR;
1168 if ( ctrl->ldctl_value.bv_len ) {
1169 rs->sr_text = "permissiveModify control value not empty";
1170 return LDAP_PROTOCOL_ERROR;
1173 op->o_permissive_modify = ctrl->ldctl_iscritical
1174 ? SLAP_CRITICAL_CONTROL
1175 : SLAP_NONCRITICAL_CONTROL;
1177 return LDAP_SUCCESS;
1181 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1182 static int parseDomainScope (
1187 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1188 rs->sr_text = "domainScope control specified multiple times";
1189 return LDAP_PROTOCOL_ERROR;
1192 if ( ctrl->ldctl_value.bv_len ) {
1193 rs->sr_text = "domainScope control value not empty";
1194 return LDAP_PROTOCOL_ERROR;
1197 op->o_domain_scope = ctrl->ldctl_iscritical
1198 ? SLAP_CRITICAL_CONTROL
1199 : SLAP_NONCRITICAL_CONTROL;
1201 return LDAP_SUCCESS;
1205 static int parseLDAPsync (
1214 struct slap_session_entry *se;
1216 if ( op->o_sync != SLAP_NO_CONTROL ) {
1217 rs->sr_text = "LDAP Sync control specified multiple times";
1218 return LDAP_PROTOCOL_ERROR;
1221 if ( ctrl->ldctl_value.bv_len == 0 ) {
1222 rs->sr_text = "LDAP Sync control value is empty (or absent)";
1223 return LDAP_PROTOCOL_ERROR;
1226 /* Parse the control value
1227 * syncRequestValue ::= SEQUENCE {
1232 * refreshAndPersist (3)
1234 * cookie syncCookie OPTIONAL
1238 ber = ber_init( &ctrl->ldctl_value );
1240 rs->sr_text = "internal error";
1244 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1245 rs->sr_text = "LDAP Sync control : mode decoding error";
1246 return LDAP_PROTOCOL_ERROR;
1250 case LDAP_SYNC_REFRESH_ONLY:
1251 mode = SLAP_SYNC_REFRESH;
1253 case LDAP_SYNC_REFRESH_AND_PERSIST:
1254 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1257 rs->sr_text = "LDAP Sync control : unknown update mode";
1258 return LDAP_PROTOCOL_ERROR;
1261 tag = ber_peek_tag( ber, &len );
1263 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1264 struct berval tmp_bv;
1265 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1266 rs->sr_text = "LDAP Sync control : cookie decoding error";
1267 return LDAP_PROTOCOL_ERROR;
1269 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1270 slap_parse_sync_cookie( &op->o_sync_state );
1272 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1273 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1274 rs->sr_text = "LDAP Sync control : rhint decoding error";
1275 return LDAP_PROTOCOL_ERROR;
1278 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1279 rs->sr_text = "LDAP Sync control : decoding error";
1280 return LDAP_PROTOCOL_ERROR;
1283 (void) ber_free( ber, 1 );
1285 op->o_sync_mode = (char) mode;
1287 op->o_sync = ctrl->ldctl_iscritical
1288 ? SLAP_CRITICAL_CONTROL
1289 : SLAP_NONCRITICAL_CONTROL;
1291 return LDAP_SUCCESS;