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;
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 { LDAP_CONTROL_PAGEDRESULTS,
93 SLAP_CTRL_SEARCH, NULL,
94 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
95 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
96 { LDAP_CONTROL_X_DOMAIN_SCOPE,
97 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
98 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
100 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
101 { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
102 SLAP_CTRL_MODIFY, NULL,
103 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
105 #ifdef LDAP_CONTROL_SUBENTRIES
106 { LDAP_CONTROL_SUBENTRIES,
107 SLAP_CTRL_SEARCH, NULL,
108 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
111 SLAP_CTRL_ACCESS, NULL,
112 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
114 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
115 parseLDAPsync, LDAP_SLIST_ENTRY_INITIALIZER(next) },
116 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
117 { LDAP_CONTROL_MODIFY_INCREMENT,
118 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
119 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;
767 LDAP_LOG( OPERATION, RESULTS,
768 "parseProxyAuthz: conn=%lu \"%s\"\n",
770 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
772 Debug( LDAP_DEBUG_TRACE,
773 "parseProxyAuthz: conn=%lu \"%s\"\n",
775 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
778 rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
781 ch_free( dn.bv_val );
782 rs->sr_text = "not authorized to assume identity";
783 return LDAP_PROXY_AUTHZ_FAILURE;
786 ch_free( op->o_dn.bv_val );
787 ch_free( op->o_ndn.bv_val );
789 op->o_dn.bv_val = NULL;
793 * NOTE: since slap_sasl_getdn() returns a normalized dn,
794 * from now on op->o_dn is normalized
796 ber_dupbv( &op->o_dn, &dn );
801 static int parseNoOp (
806 if ( op->o_noop != SLAP_NO_CONTROL ) {
807 rs->sr_text = "noop control specified multiple times";
808 return LDAP_PROTOCOL_ERROR;
811 if ( ctrl->ldctl_value.bv_len ) {
812 rs->sr_text = "noop control value not empty";
813 return LDAP_PROTOCOL_ERROR;
816 op->o_noop = ctrl->ldctl_iscritical
817 ? SLAP_CRITICAL_CONTROL
818 : SLAP_NONCRITICAL_CONTROL;
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;
904 static int parseAssert (
910 struct berval fstr = { 0, NULL };
911 const char *err_msg = "";
913 if ( op->o_assert != SLAP_NO_CONTROL ) {
914 rs->sr_text = "assert control specified multiple times";
915 return LDAP_PROTOCOL_ERROR;
918 if ( ctrl->ldctl_value.bv_len == 0 ) {
919 rs->sr_text = "assert control value is empty (or absent)";
920 return LDAP_PROTOCOL_ERROR;
923 ber = ber_init( &(ctrl->ldctl_value) );
925 rs->sr_text = "assert control: internal error";
929 rs->sr_err = get_filter( op, ber, &(op->o_assertion), &rs->sr_text);
931 if( rs->sr_err != LDAP_SUCCESS ) {
932 if( rs->sr_err == SLAPD_DISCONNECT ) {
933 rs->sr_err = LDAP_PROTOCOL_ERROR;
934 send_ldap_disconnect( op, rs );
935 rs->sr_err = SLAPD_DISCONNECT;
937 send_ldap_result( op, rs );
939 if( op->o_assertion != NULL ) {
940 filter_free_x( op, op->o_assertion );
946 filter2bv_x( op, op->o_assertion, &fstr );
949 LDAP_LOG( OPERATION, ARGS,
950 "parseAssert: conn %ld assert: %s\n",
951 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
953 Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
954 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_CRITICAL_CONTROL
961 : SLAP_NONCRITICAL_CONTROL;
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_NO_CONTROL ) {
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_CRITICAL_CONTROL
1015 : SLAP_NONCRITICAL_CONTROL;
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_NO_CONTROL ) {
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_CRITICAL_CONTROL
1071 : SLAP_NONCRITICAL_CONTROL;
1073 op->o_postread_attrs = an;
1075 rs->sr_err = LDAP_SUCCESS;
1076 return LDAP_SUCCESS;
1079 int parseValuesReturnFilter (
1085 struct berval fstr = { 0, NULL };
1086 const char *err_msg = "";
1088 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
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, &(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 );
1122 LDAP_LOG( OPERATION, ARGS,
1123 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
1124 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1126 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
1127 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1129 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1132 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1133 ? SLAP_CRITICAL_CONTROL
1134 : SLAP_NONCRITICAL_CONTROL;
1136 rs->sr_err = LDAP_SUCCESS;
1137 return LDAP_SUCCESS;
1140 #ifdef LDAP_CONTROL_SUBENTRIES
1141 static int parseSubentries (
1146 if ( op->o_subentries != SLAP_NO_CONTROL ) {
1147 rs->sr_text = "subentries control specified multiple times";
1148 return LDAP_PROTOCOL_ERROR;
1151 /* FIXME: should use BER library */
1152 if( ( ctrl->ldctl_value.bv_len != 3 )
1153 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1154 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1156 rs->sr_text = "subentries control value encoding is bogus";
1157 return LDAP_PROTOCOL_ERROR;
1160 op->o_subentries = ctrl->ldctl_iscritical
1161 ? SLAP_CRITICAL_CONTROL
1162 : SLAP_NONCRITICAL_CONTROL;
1164 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
1166 return LDAP_SUCCESS;
1170 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1171 static int parsePermissiveModify (
1176 if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
1177 rs->sr_text = "permissiveModify control specified multiple times";
1178 return LDAP_PROTOCOL_ERROR;
1181 if ( ctrl->ldctl_value.bv_len ) {
1182 rs->sr_text = "permissiveModify control value not empty";
1183 return LDAP_PROTOCOL_ERROR;
1186 op->o_permissive_modify = ctrl->ldctl_iscritical
1187 ? SLAP_CRITICAL_CONTROL
1188 : SLAP_NONCRITICAL_CONTROL;
1190 return LDAP_SUCCESS;
1194 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1195 static int parseDomainScope (
1200 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1201 rs->sr_text = "domainScope control specified multiple times";
1202 return LDAP_PROTOCOL_ERROR;
1205 if ( ctrl->ldctl_value.bv_len ) {
1206 rs->sr_text = "domainScope control value not empty";
1207 return LDAP_PROTOCOL_ERROR;
1210 op->o_domain_scope = ctrl->ldctl_iscritical
1211 ? SLAP_CRITICAL_CONTROL
1212 : SLAP_NONCRITICAL_CONTROL;
1214 return LDAP_SUCCESS;
1218 static int parseLDAPsync (
1227 struct slap_session_entry *se;
1229 if ( op->o_sync != SLAP_NO_CONTROL ) {
1230 rs->sr_text = "LDAP Sync control specified multiple times";
1231 return LDAP_PROTOCOL_ERROR;
1234 if ( ctrl->ldctl_value.bv_len == 0 ) {
1235 rs->sr_text = "LDAP Sync control value is empty (or absent)";
1236 return LDAP_PROTOCOL_ERROR;
1239 /* Parse the control value
1240 * syncRequestValue ::= SEQUENCE {
1245 * refreshAndPersist (3)
1247 * cookie syncCookie OPTIONAL
1251 ber = ber_init( &ctrl->ldctl_value );
1253 rs->sr_text = "internal error";
1257 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1258 rs->sr_text = "LDAP Sync control : mode decoding error";
1259 return LDAP_PROTOCOL_ERROR;
1263 case LDAP_SYNC_REFRESH_ONLY:
1264 mode = SLAP_SYNC_REFRESH;
1266 case LDAP_SYNC_REFRESH_AND_PERSIST:
1267 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1270 rs->sr_text = "LDAP Sync control : unknown update mode";
1271 return LDAP_PROTOCOL_ERROR;
1274 tag = ber_peek_tag( ber, &len );
1276 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1277 struct berval tmp_bv;
1278 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1279 rs->sr_text = "LDAP Sync control : cookie decoding error";
1280 return LDAP_PROTOCOL_ERROR;
1282 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1283 slap_parse_sync_cookie( &op->o_sync_state );
1285 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1286 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1287 rs->sr_text = "LDAP Sync control : rhint decoding error";
1288 return LDAP_PROTOCOL_ERROR;
1291 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1292 rs->sr_text = "LDAP Sync control : decoding error";
1293 return LDAP_PROTOCOL_ERROR;
1296 (void) ber_free( ber, 1 );
1298 op->o_sync_mode = (char) mode;
1300 op->o_sync = ctrl->ldctl_iscritical
1301 ? SLAP_CRITICAL_CONTROL
1302 : SLAP_NONCRITICAL_CONTROL;
1304 return LDAP_SUCCESS;