3 * Copyright 1999-2002 The OpenLDAP Foundation.
6 * Redistribution and use in source and binary forms are permitted only
7 * as authorized by the OpenLDAP Public License. A copy of this
8 * license is available at http://www.OpenLDAP.org/license.html or
9 * in file LICENSE in the top-level directory of the distribution.
15 #include <ac/string.h>
16 #include <ac/socket.h>
20 #include "../../libraries/liblber/lber-int.h"
22 #define SLAP_CTRL_FRONTEND 0x80000000U
23 #define SLAP_CTRL_FRONTEND_SEARCH 0x01000000U /* for NOOP */
25 #define SLAP_CTRL_OPFLAGS 0x0000FFFFU
26 #define SLAP_CTRL_ABANDON 0x00000001U
27 #define SLAP_CTRL_ADD 0x00002002U
28 #define SLAP_CTRL_BIND 0x00000004U
29 #define SLAP_CTRL_COMPARE 0x00001008U
30 #define SLAP_CTRL_DELETE 0x00002010U
31 #define SLAP_CTRL_MODIFY 0x00002020U
32 #define SLAP_CTRL_RENAME 0x00002040U
33 #define SLAP_CTRL_SEARCH 0x00001080U
34 #define SLAP_CTRL_UNBIND 0x00000100U
36 #define SLAP_CTRL_INTROGATE (SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH)
37 #define SLAP_CTRL_UPDATE \
38 (SLAP_CTRL_ADD|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME)
39 #define SLAP_CTRL_ACCESS (SLAP_CTRL_INTROGATE|SLAP_CTRL_UPDATE)
41 typedef int (SLAP_CTRL_PARSE_FN) LDAP_P((
47 static SLAP_CTRL_PARSE_FN parseManageDSAit;
48 static SLAP_CTRL_PARSE_FN parseSubentries;
49 static SLAP_CTRL_PARSE_FN parseNoOp;
50 static SLAP_CTRL_PARSE_FN parsePagedResults;
51 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
53 #ifdef LDAP_CLIENT_UPDATE
54 static SLAP_CTRL_PARSE_FN parseClientUpdate;
55 #endif /* LDAP_CLIENT_UPDATE */
57 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
59 static struct slap_control {
62 char **sc_extendedops;
63 SLAP_CTRL_PARSE_FN *sc_parse;
65 } supportedControls[] = {
66 { LDAP_CONTROL_MANAGEDSAIT,
67 SLAP_CTRL_ACCESS, NULL,
69 #ifdef LDAP_CONTROL_SUBENTRIES
70 { LDAP_CONTROL_SUBENTRIES,
71 SLAP_CTRL_SEARCH, NULL,
74 #ifdef LDAP_CONTROL_NOOP
76 SLAP_CTRL_ACCESS, NULL,
79 #ifdef LDAP_CONTROL_PAGEDRESULTS
80 { LDAP_CONTROL_PAGEDRESULTS,
81 SLAP_CTRL_SEARCH, NULL,
83 #endif /* LDAP_CONTROL_PAGEDRESULTS */
84 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
85 { LDAP_CONTROL_VALUESRETURNFILTER,
86 SLAP_CTRL_SEARCH, NULL,
87 parseValuesReturnFilter },
89 #ifdef LDAP_CLIENT_UPDATE
90 { LDAP_CONTROL_CLIENT_UPDATE,
91 SLAP_CTRL_SEARCH, NULL,
93 #endif /* LDAP_CLIENT_UPDATE */
98 get_supported_ctrl(int index)
100 return supportedControls[index].sc_oid;
103 static struct slap_control *
104 find_ctrl( const char *oid )
107 for( i=0; supportedControls[i].sc_oid; i++ ) {
108 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
109 return &supportedControls[i];
124 BerElement *ber = op->o_ber;
125 struct slap_control *sc;
126 int rc = LDAP_SUCCESS;
127 const char *errmsg = NULL;
129 len = ber_pvt_ber_remaining(ber);
137 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
138 if( tag == LBER_ERROR ) {
139 rc = SLAPD_DISCONNECT;
140 errmsg = "unexpected data in PDU";
147 LDAP_LOG( OPERATION, ENTRY, "get_ctrls: conn %lu\n", conn->c_connid, 0, 0 );
149 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
151 if( op->o_protocol < LDAP_VERSION3 ) {
152 rc = SLAPD_DISCONNECT;
153 errmsg = "controls require LDAPv3";
157 /* one for first control, one for termination */
158 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
161 if( op->ctrls == NULL ) {
163 errmsg = "no memory";
168 op->o_ctrls[nctrls] = NULL;
170 /* step through each element */
171 for( tag = ber_first_element( ber, &len, &opaque );
173 tag = ber_next_element( ber, &len, opaque ) )
176 LDAPControl **tctrls;
178 c = ch_calloc( 1, sizeof(LDAPControl) );
182 ldap_controls_free(op->o_ctrls);
186 errmsg = "no memory";
191 /* allocate pointer space for current controls (nctrls)
192 * + this control + extra NULL
194 tctrls = ch_realloc( op->o_ctrls,
195 (nctrls+2) * sizeof(LDAPControl *));
198 if( tctrls == NULL ) {
200 ldap_controls_free(op->o_ctrls);
204 errmsg = "no memory";
208 op->o_ctrls = tctrls;
210 op->o_ctrls[nctrls++] = c;
211 op->o_ctrls[nctrls] = NULL;
213 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
215 if( tag == LBER_ERROR ) {
217 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
218 conn->c_connid, 0, 0 );
220 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
223 ldap_controls_free( op->o_ctrls );
225 rc = SLAPD_DISCONNECT;
226 errmsg = "decoding controls error";
230 tag = ber_peek_tag( ber, &len );
232 if( tag == LBER_BOOLEAN ) {
234 tag = ber_scanf( ber, "b", &crit );
236 if( tag == LBER_ERROR ) {
238 LDAP_LOG( OPERATION, INFO,
239 "get_ctrls: conn %lu get crit failed.\n",
240 conn->c_connid, 0, 0 );
242 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
245 ldap_controls_free( op->o_ctrls );
247 rc = SLAPD_DISCONNECT;
248 errmsg = "decoding controls error";
252 c->ldctl_iscritical = (crit != 0);
253 tag = ber_peek_tag( ber, &len );
256 if( tag == LBER_OCTETSTRING ) {
257 tag = ber_scanf( ber, "o", &c->ldctl_value );
259 if( tag == LBER_ERROR ) {
261 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
262 "%s (%scritical): get value failed.\n",
263 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
264 c->ldctl_iscritical ? "" : "non" );
266 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
267 "%s (%scritical): get value failed.\n",
269 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
270 c->ldctl_iscritical ? "" : "non" );
272 ldap_controls_free( op->o_ctrls );
274 rc = SLAPD_DISCONNECT;
275 errmsg = "decoding controls error";
281 LDAP_LOG( OPERATION, INFO,
282 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
283 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
284 c->ldctl_iscritical ? "" : "non" );
286 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
287 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
288 c->ldctl_iscritical ? "" : "non",
292 sc = find_ctrl( c->ldctl_oid );
294 /* recognized control */
296 switch( op->o_tag ) {
298 tagmask = SLAP_CTRL_ADD;
301 tagmask = SLAP_CTRL_BIND;
303 case LDAP_REQ_COMPARE:
304 tagmask = SLAP_CTRL_COMPARE;
306 case LDAP_REQ_DELETE:
307 tagmask = SLAP_CTRL_DELETE;
309 case LDAP_REQ_MODIFY:
310 tagmask = SLAP_CTRL_MODIFY;
312 case LDAP_REQ_RENAME:
313 tagmask = SLAP_CTRL_RENAME;
315 case LDAP_REQ_SEARCH:
316 tagmask = SLAP_CTRL_SEARCH;
318 case LDAP_REQ_UNBIND:
319 tagmask = SLAP_CTRL_UNBIND;
321 case LDAP_REQ_ABANDON:
322 tagmask = SLAP_CTRL_ABANDON;
324 case LDAP_REQ_EXTENDED:
325 /* FIXME: check list of extended operations */
330 errmsg = "controls internal error";
334 if (( sc->sc_mask & tagmask ) == tagmask ) {
335 /* available extension */
337 if( !sc->sc_parse ) {
339 errmsg = "not yet implemented";
343 rc = sc->sc_parse( conn, op, c, &errmsg );
345 if( rc != LDAP_SUCCESS ) goto return_results;
347 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
348 /* kludge to disable backend_control() check */
349 c->ldctl_iscritical = 0;
351 } else if ( tagmask == SLAP_CTRL_SEARCH &&
352 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
354 /* kludge to disable backend_control() check */
355 c->ldctl_iscritical = 0;
358 } else if( c->ldctl_iscritical ) {
359 /* unavailable CRITICAL control */
360 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
361 errmsg = "critical extension is unavailable";
365 } else if( c->ldctl_iscritical ) {
366 /* unrecognized CRITICAL control */
367 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
368 errmsg = "critical extension is not recognized";
375 LDAP_LOG( OPERATION, RESULTS,
376 "get_ctrls: n=%d rc=%d err=%s\n", nctrls, rc, errmsg ? errmsg : "" );
378 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: n=%d rc=%d err=%s\n",
379 nctrls, rc, errmsg ? errmsg : "");
382 if( sendres && rc != LDAP_SUCCESS ) {
383 if( rc == SLAPD_DISCONNECT ) {
384 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
386 send_ldap_result( conn, op, rc,
387 NULL, errmsg, NULL, NULL );
394 static int parseManageDSAit (
400 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
401 *text = "manageDSAit control specified multiple times";
402 return LDAP_PROTOCOL_ERROR;
405 if ( ctrl->ldctl_value.bv_len ) {
406 *text = "manageDSAit control value not empty";
407 return LDAP_PROTOCOL_ERROR;
410 op->o_managedsait = ctrl->ldctl_iscritical
411 ? SLAP_CRITICAL_CONTROL
412 : SLAP_NONCRITICAL_CONTROL;
417 #ifdef LDAP_CONTROL_SUBENTRIES
418 static int parseSubentries (
424 if ( op->o_subentries != SLAP_NO_CONTROL ) {
425 *text = "subentries control specified multiple times";
426 return LDAP_PROTOCOL_ERROR;
429 /* FIXME: should use BER library */
430 if( ( ctrl->ldctl_value.bv_len != 3 )
431 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
432 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
434 *text = "subentries control value encoding is bogus";
435 return LDAP_PROTOCOL_ERROR;
438 op->o_subentries = ctrl->ldctl_iscritical
439 ? SLAP_CRITICAL_CONTROL
440 : SLAP_NONCRITICAL_CONTROL;
442 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
448 #ifdef LDAP_CONTROL_NOOP
449 static int parseNoOp (
455 if ( op->o_noop != SLAP_NO_CONTROL ) {
456 *text = "noop control specified multiple times";
457 return LDAP_PROTOCOL_ERROR;
460 if ( ctrl->ldctl_value.bv_len ) {
461 *text = "noop control value not empty";
462 return LDAP_PROTOCOL_ERROR;
465 op->o_noop = ctrl->ldctl_iscritical
466 ? SLAP_CRITICAL_CONTROL
467 : SLAP_NONCRITICAL_CONTROL;
473 #ifdef LDAP_CONTROL_PAGEDRESULTS
474 static int parsePagedResults (
483 struct berval cookie = { 0, NULL };
485 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
486 *text = "paged results control specified multiple times";
487 return LDAP_PROTOCOL_ERROR;
490 if ( ctrl->ldctl_value.bv_len == 0 ) {
491 *text = "paged results control value is empty";
492 return LDAP_PROTOCOL_ERROR;
495 /* Parse the control value
496 * realSearchControlValue ::= SEQUENCE {
497 * size INTEGER (0..maxInt),
498 * -- requested page size from client
499 * -- result set size estimate from server
500 * cookie OCTET STRING
502 ber = ber_init( &ctrl->ldctl_value );
504 *text = "internal error";
508 tag = ber_scanf( ber, "{im}", &size, &cookie );
509 (void) ber_free( ber, 1 );
511 if( tag == LBER_ERROR ) {
512 *text = "paged results control could not be decoded";
513 return LDAP_PROTOCOL_ERROR;
517 *text = "paged results control size invalid";
518 return LDAP_PROTOCOL_ERROR;
521 if( cookie.bv_len ) {
522 PagedResultsCookie reqcookie;
523 if( cookie.bv_len != sizeof( reqcookie ) ) {
525 *text = "paged results cookie is invalid";
526 return LDAP_PROTOCOL_ERROR;
529 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
531 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
533 *text = "paged results cookie is invalid";
534 return LDAP_PROTOCOL_ERROR;
536 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
537 *text = "paged results cookie is invalid or old";
538 return LDAP_UNWILLING_TO_PERFORM;
541 /* Initial request. Initialize state. */
542 op->o_pagedresults_state.ps_cookie = 0;
543 op->o_pagedresults_state.ps_id = NOID;
546 op->o_pagedresults_size = size;
548 op->o_pagedresults = ctrl->ldctl_iscritical
549 ? SLAP_CRITICAL_CONTROL
550 : SLAP_NONCRITICAL_CONTROL;
554 #endif /* LDAP_CONTROL_PAGEDRESULTS */
556 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
557 int parseValuesReturnFilter (
565 struct berval fstr = { 0, NULL };
566 const char *err_msg = "";
568 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
569 *text = "valuesreturnfilter control specified multiple times";
570 return LDAP_PROTOCOL_ERROR;
573 ber = ber_init( &(ctrl->ldctl_value) );
575 *text = "internal error";
579 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
581 if( rc != LDAP_SUCCESS ) {
583 if( rc == SLAPD_DISCONNECT ) {
584 send_ldap_disconnect( conn, op,
585 LDAP_PROTOCOL_ERROR, *text );
587 send_ldap_result( conn, op, rc,
588 NULL, *text, NULL, NULL );
590 if( fstr.bv_val != NULL) free( fstr.bv_val );
591 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
594 vrFilter2bv( op->vrFilter, &fstr );
598 LDAP_LOG( OPERATION, ARGS,
599 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
600 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
602 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
603 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
606 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
607 ? SLAP_CRITICAL_CONTROL
608 : SLAP_NONCRITICAL_CONTROL;
614 #ifdef LDAP_CLIENT_UPDATE
615 static int parseClientUpdate (
626 struct berval scheme = { 0, NULL };
627 struct berval cookie = { 0, NULL };
629 if ( op->o_clientupdate != SLAP_NO_CONTROL ) {
630 *text = "LCUP client update control specified multiple times";
631 return LDAP_PROTOCOL_ERROR;
634 if ( ctrl->ldctl_value.bv_len == 0 ) {
635 *text = "LCUP client update control value is empty";
636 return LDAP_PROTOCOL_ERROR;
639 /* Parse the control value
640 * ClientUpdateControlValue ::= SEQUENCE {
641 * updateType ENUMERATED {
642 * synchronizeOnly {0},
643 * synchronizeAndPersist {1},
645 * sendCookieInterval INTEGER OPTIONAL,
646 * cookie LCUPCookie OPTIONAL
650 ber = ber_init( &ctrl->ldctl_value );
652 *text = "internal error";
656 if ( (tag = ber_scanf( ber, "{i" /*}*/, &type )) == LBER_ERROR ) {
657 *text = "LCUP client update control : decoding error";
658 return LDAP_PROTOCOL_ERROR;
662 case LDAP_CUP_SYNC_ONLY:
663 type = SLAP_LCUP_SYNC;
665 case LDAP_CUP_SYNC_AND_PERSIST:
666 type = SLAP_LCUP_SYNC_AND_PERSIST;
668 case LDAP_CUP_PERSIST_ONLY:
669 type = SLAP_LCUP_PERSIST;
672 *text = "LCUP client update control : unknown update type";
673 return LDAP_PROTOCOL_ERROR;
676 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
677 *text = "LCUP client update control : decoding error";
678 return LDAP_PROTOCOL_ERROR;
681 if ( tag == LDAP_TAG_INTERVAL ) {
682 if ( (tag = ber_scanf( ber, "i", &interval )) == LBER_ERROR ) {
683 *text = "LCUP client update control : decoding error";
684 return LDAP_PROTOCOL_ERROR;
687 if ( interval <= 0 ) {
688 /* server chooses interval */
689 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
693 /* server chooses interval */
694 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
697 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
698 *text = "LCUP client update control : decoding error";
699 return LDAP_PROTOCOL_ERROR;
702 if ( tag == LDAP_TAG_COOKIE ) {
703 if ( (tag = ber_scanf( ber, /*{*/ "{mm}}",
704 &scheme, &cookie )) == LBER_ERROR ) {
705 *text = "LCUP client update control : decoding error";
706 return LDAP_PROTOCOL_ERROR;
710 /* TODO : Cookie Scheme Validation */
712 if ( lcup_cookie_scheme_validate(scheme) != LDAP_SUCCESS ) {
713 *text = "Unsupported LCUP cookie scheme";
714 return LCUP_UNSUPPORTED_SCHEME;
717 if ( lcup_cookie_validate(scheme, cookie) != LDAP_SUCCESS ) {
718 *text = "Invalid LCUP cookie";
719 return LCUP_INVALID_COOKIE;
723 ber_dupbv( &op->o_clientupdate_state, &cookie );
725 (void) ber_free( ber, 1 );
727 op->o_clientupdate_type = (char) type;
728 op->o_clientupdate_interval = interval;
730 op->o_clientupdate = ctrl->ldctl_iscritical
731 ? SLAP_CRITICAL_CONTROL
732 : SLAP_NONCRITICAL_CONTROL;
736 #endif /* LDAP_CLIENT_UPDATE */