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 #ifndef LDAP_CONTROL_PAGEDRESULTS
159 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
160 #else /* LDAP_CONTROL_PAGEDRESULTS */
161 /* FIXME: are we sure we need this? */
162 op->o_ctrls = ch_malloc( 3 * sizeof(LDAPControl *) );
163 #endif /* LDAP_CONTROL_PAGEDRESULTS */
166 if( op->ctrls == NULL ) {
168 errmsg = "no memory";
173 op->o_ctrls[nctrls] = NULL;
175 /* step through each element */
176 for( tag = ber_first_element( ber, &len, &opaque );
178 tag = ber_next_element( ber, &len, opaque ) )
181 LDAPControl **tctrls;
183 c = ch_calloc( 1, sizeof(LDAPControl) );
187 ldap_controls_free(op->o_ctrls);
191 errmsg = "no memory";
196 /* allocate pointer space for current controls (nctrls)
197 * + this control + extra NULL
199 tctrls = ch_realloc( op->o_ctrls,
200 (nctrls+2) * sizeof(LDAPControl *));
203 if( tctrls == NULL ) {
205 ldap_controls_free(op->o_ctrls);
209 errmsg = "no memory";
213 op->o_ctrls = tctrls;
215 op->o_ctrls[nctrls++] = c;
216 op->o_ctrls[nctrls] = NULL;
218 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
220 if( tag == LBER_ERROR ) {
222 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
223 conn->c_connid, 0, 0 );
225 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
228 ldap_controls_free( op->o_ctrls );
230 rc = SLAPD_DISCONNECT;
231 errmsg = "decoding controls error";
235 tag = ber_peek_tag( ber, &len );
237 if( tag == LBER_BOOLEAN ) {
239 tag = ber_scanf( ber, "b", &crit );
241 if( tag == LBER_ERROR ) {
243 LDAP_LOG( OPERATION, INFO,
244 "get_ctrls: conn %lu get crit failed.\n",
245 conn->c_connid, 0, 0 );
247 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
250 ldap_controls_free( op->o_ctrls );
252 rc = SLAPD_DISCONNECT;
253 errmsg = "decoding controls error";
257 c->ldctl_iscritical = (crit != 0);
258 tag = ber_peek_tag( ber, &len );
261 if( tag == LBER_OCTETSTRING ) {
262 tag = ber_scanf( ber, "o", &c->ldctl_value );
264 if( tag == LBER_ERROR ) {
266 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
267 "%s (%scritical): get value failed.\n",
268 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
269 c->ldctl_iscritical ? "" : "non" );
271 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
272 "%s (%scritical): get value failed.\n",
274 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
275 c->ldctl_iscritical ? "" : "non" );
277 ldap_controls_free( op->o_ctrls );
279 rc = SLAPD_DISCONNECT;
280 errmsg = "decoding controls error";
286 LDAP_LOG( OPERATION, INFO,
287 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
288 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
289 c->ldctl_iscritical ? "" : "non" );
291 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
292 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
293 c->ldctl_iscritical ? "" : "non",
297 sc = find_ctrl( c->ldctl_oid );
299 /* recognized control */
301 switch( op->o_tag ) {
303 tagmask = SLAP_CTRL_ADD;
306 tagmask = SLAP_CTRL_BIND;
308 case LDAP_REQ_COMPARE:
309 tagmask = SLAP_CTRL_COMPARE;
311 case LDAP_REQ_DELETE:
312 tagmask = SLAP_CTRL_DELETE;
314 case LDAP_REQ_MODIFY:
315 tagmask = SLAP_CTRL_MODIFY;
317 case LDAP_REQ_RENAME:
318 tagmask = SLAP_CTRL_RENAME;
320 case LDAP_REQ_SEARCH:
321 tagmask = SLAP_CTRL_SEARCH;
323 case LDAP_REQ_UNBIND:
324 tagmask = SLAP_CTRL_UNBIND;
326 case LDAP_REQ_ABANDON:
327 tagmask = SLAP_CTRL_ABANDON;
329 case LDAP_REQ_EXTENDED:
330 /* FIXME: check list of extended operations */
335 errmsg = "controls internal error";
339 if (( sc->sc_mask & tagmask ) == tagmask ) {
340 /* available extension */
342 if( !sc->sc_parse ) {
344 errmsg = "not yet implemented";
348 rc = sc->sc_parse( conn, op, c, &errmsg );
350 if( rc != LDAP_SUCCESS ) goto return_results;
352 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
353 /* kludge to disable backend_control() check */
354 c->ldctl_iscritical = 0;
356 } else if ( tagmask == SLAP_CTRL_SEARCH &&
357 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
359 /* kludge to disable backend_control() check */
360 c->ldctl_iscritical = 0;
363 } else if( c->ldctl_iscritical ) {
364 /* unavailable CRITICAL control */
365 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
366 errmsg = "critical extension is unavailable";
370 } else if( c->ldctl_iscritical ) {
371 /* unrecognized CRITICAL control */
372 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
373 errmsg = "critical extension is not recognized";
380 LDAP_LOG( OPERATION, RESULTS,
381 "get_ctrls: n=%d rc=%d err=%s\n", nctrls, rc, errmsg ? errmsg : "" );
383 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: n=%d rc=%d err=%s\n",
384 nctrls, rc, errmsg ? errmsg : "");
387 if( sendres && rc != LDAP_SUCCESS ) {
388 if( rc == SLAPD_DISCONNECT ) {
389 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
391 send_ldap_result( conn, op, rc,
392 NULL, errmsg, NULL, NULL );
399 static int parseManageDSAit (
405 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
406 *text = "manageDSAit control specified multiple times";
407 return LDAP_PROTOCOL_ERROR;
410 if ( ctrl->ldctl_value.bv_len ) {
411 *text = "manageDSAit control value not empty";
412 return LDAP_PROTOCOL_ERROR;
415 op->o_managedsait = ctrl->ldctl_iscritical
416 ? SLAP_CRITICAL_CONTROL
417 : SLAP_NONCRITICAL_CONTROL;
422 #ifdef LDAP_CONTROL_SUBENTRIES
423 static int parseSubentries (
429 if ( op->o_subentries != SLAP_NO_CONTROL ) {
430 *text = "subentries control specified multiple times";
431 return LDAP_PROTOCOL_ERROR;
434 /* FIXME: should use BER library */
435 if( ( ctrl->ldctl_value.bv_len != 3 )
436 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
437 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
439 *text = "subentries control value encoding is bogus";
440 return LDAP_PROTOCOL_ERROR;
443 op->o_subentries = ctrl->ldctl_iscritical
444 ? SLAP_CRITICAL_CONTROL
445 : SLAP_NONCRITICAL_CONTROL;
447 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
453 #ifdef LDAP_CONTROL_NOOP
454 static int parseNoOp (
460 if ( op->o_noop != SLAP_NO_CONTROL ) {
461 *text = "noop control specified multiple times";
462 return LDAP_PROTOCOL_ERROR;
465 if ( ctrl->ldctl_value.bv_len ) {
466 *text = "noop control value not empty";
467 return LDAP_PROTOCOL_ERROR;
470 op->o_noop = ctrl->ldctl_iscritical
471 ? SLAP_CRITICAL_CONTROL
472 : SLAP_NONCRITICAL_CONTROL;
478 #ifdef LDAP_CONTROL_PAGEDRESULTS
479 static int parsePagedResults (
488 struct berval cookie = { 0, NULL };
490 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
491 *text = "paged results control specified multiple times";
492 return LDAP_PROTOCOL_ERROR;
495 if ( ctrl->ldctl_value.bv_len == 0 ) {
496 *text = "paged results control value is empty";
497 return LDAP_PROTOCOL_ERROR;
500 /* Parse the control value
501 * realSearchControlValue ::= SEQUENCE {
502 * size INTEGER (0..maxInt),
503 * -- requested page size from client
504 * -- result set size estimate from server
505 * cookie OCTET STRING
507 ber = ber_init( &ctrl->ldctl_value );
509 *text = "internal error";
513 tag = ber_scanf( ber, "{im}", &size, &cookie );
514 (void) ber_free( ber, 1 );
516 if( tag == LBER_ERROR ) {
517 *text = "paged results control could not be decoded";
518 return LDAP_PROTOCOL_ERROR;
522 *text = "paged results control size invalid";
523 return LDAP_PROTOCOL_ERROR;
526 if( cookie.bv_len ) {
527 PagedResultsCookie reqcookie;
528 if( cookie.bv_len != sizeof( reqcookie ) ) {
530 *text = "paged results cookie is invalid";
531 return LDAP_PROTOCOL_ERROR;
534 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
536 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
538 *text = "paged results cookie is invalid";
539 return LDAP_PROTOCOL_ERROR;
541 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
542 *text = "paged results cookie is invalid or old";
543 return LDAP_UNWILLING_TO_PERFORM;
546 /* Initial request. Initialize state. */
547 op->o_pagedresults_state.ps_cookie = 0;
548 op->o_pagedresults_state.ps_id = NOID;
551 op->o_pagedresults_size = size;
553 op->o_pagedresults = ctrl->ldctl_iscritical
554 ? SLAP_CRITICAL_CONTROL
555 : SLAP_NONCRITICAL_CONTROL;
559 #endif /* LDAP_CONTROL_PAGEDRESULTS */
561 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
562 int parseValuesReturnFilter (
570 struct berval fstr = { 0, NULL };
571 const char *err_msg = "";
573 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
574 *text = "valuesreturnfilter control specified multiple times";
575 return LDAP_PROTOCOL_ERROR;
578 ber = ber_init( &(ctrl->ldctl_value) );
580 *text = "internal error";
584 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
586 if( rc != LDAP_SUCCESS ) {
588 if( rc == SLAPD_DISCONNECT ) {
589 send_ldap_disconnect( conn, op,
590 LDAP_PROTOCOL_ERROR, *text );
592 send_ldap_result( conn, op, rc,
593 NULL, *text, NULL, NULL );
595 if( fstr.bv_val != NULL) free( fstr.bv_val );
596 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
599 vrFilter2bv( op->vrFilter, &fstr );
603 LDAP_LOG( OPERATION, ARGS,
604 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
605 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
607 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
608 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
611 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
612 ? SLAP_CRITICAL_CONTROL
613 : SLAP_NONCRITICAL_CONTROL;
619 #ifdef LDAP_CLIENT_UPDATE
620 static int parseClientUpdate (
631 struct berval scheme = { 0, NULL };
632 struct berval cookie = { 0, NULL };
634 if ( op->o_clientupdate != SLAP_NO_CONTROL ) {
635 *text = "LCUP client update control specified multiple times";
636 return LDAP_PROTOCOL_ERROR;
639 if ( ctrl->ldctl_value.bv_len == 0 ) {
640 *text = "LCUP client update control value is empty";
641 return LDAP_PROTOCOL_ERROR;
644 /* Parse the control value
645 * ClientUpdateControlValue ::= SEQUENCE {
646 * updateType ENUMERATED {
647 * synchronizeOnly {0},
648 * synchronizeAndPersist {1},
650 * sendCookieInterval INTEGER OPTIONAL,
651 * cookie LCUPCookie OPTIONAL
655 ber = ber_init( &ctrl->ldctl_value );
657 *text = "internal error";
661 if ( (tag = ber_scanf( ber, "{i" /*}*/, &type )) == LBER_ERROR ) {
662 *text = "LCUP client update control : decoding error";
663 return LDAP_PROTOCOL_ERROR;
667 case LDAP_CUP_SYNC_ONLY:
668 type = SLAP_LCUP_SYNC;
670 case LDAP_CUP_SYNC_AND_PERSIST:
671 type = SLAP_LCUP_SYNC_AND_PERSIST;
673 case LDAP_CUP_PERSIST_ONLY:
674 type = SLAP_LCUP_PERSIST;
677 *text = "LCUP client update control : unknown update type";
678 return LDAP_PROTOCOL_ERROR;
681 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
682 *text = "LCUP client update control : decoding error";
683 return LDAP_PROTOCOL_ERROR;
686 if ( tag == LDAP_TAG_INTERVAL ) {
687 if ( (tag = ber_scanf( ber, "i", &interval )) == LBER_ERROR ) {
688 *text = "LCUP client update control : decoding error";
689 return LDAP_PROTOCOL_ERROR;
692 if ( interval <= 0 ) {
693 /* server chooses interval */
694 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
698 /* server chooses interval */
699 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
702 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
703 *text = "LCUP client update control : decoding error";
704 return LDAP_PROTOCOL_ERROR;
707 if ( tag == LDAP_TAG_COOKIE ) {
708 if ( (tag = ber_scanf( ber, /*{*/ "{mm}}",
709 &scheme, &cookie )) == LBER_ERROR ) {
710 *text = "LCUP client update control : decoding error";
711 return LDAP_PROTOCOL_ERROR;
715 /* TODO : Cookie Scheme Validation */
717 if ( lcup_cookie_scheme_validate(scheme) != LDAP_SUCCESS ) {
718 *text = "Unsupported LCUP cookie scheme";
719 return LCUP_UNSUPPORTED_SCHEME;
722 if ( lcup_cookie_validate(scheme, cookie) != LDAP_SUCCESS ) {
723 *text = "Invalid LCUP cookie";
724 return LCUP_INVALID_COOKIE;
728 ber_dupbv( &op->o_clientupdate_state, &cookie );
730 (void) ber_free( ber, 1 );
732 op->o_clientupdate_type = (char) type;
733 op->o_clientupdate_interval = interval;
735 op->o_clientupdate = ctrl->ldctl_iscritical
736 ? SLAP_CRITICAL_CONTROL
737 : SLAP_NONCRITICAL_CONTROL;
741 #endif /* LDAP_CLIENT_UPDATE */