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,
75 SLAP_CTRL_ACCESS, NULL,
77 { LDAP_CONTROL_PAGEDRESULTS,
78 SLAP_CTRL_SEARCH, NULL,
80 { LDAP_CONTROL_VALUESRETURNFILTER,
81 SLAP_CTRL_SEARCH, NULL,
82 parseValuesReturnFilter },
83 #ifdef LDAP_CLIENT_UPDATE
84 { LDAP_CONTROL_CLIENT_UPDATE,
85 SLAP_CTRL_SEARCH, NULL,
87 #endif /* LDAP_CLIENT_UPDATE */
92 get_supported_ctrl(int index)
94 return supportedControls[index].sc_oid;
97 static struct slap_control *
98 find_ctrl( const char *oid )
101 for( i=0; supportedControls[i].sc_oid; i++ ) {
102 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
103 return &supportedControls[i];
118 BerElement *ber = op->o_ber;
119 struct slap_control *sc;
120 int rc = LDAP_SUCCESS;
121 const char *errmsg = NULL;
123 len = ber_pvt_ber_remaining(ber);
131 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
132 if( tag == LBER_ERROR ) {
133 rc = SLAPD_DISCONNECT;
134 errmsg = "unexpected data in PDU";
141 LDAP_LOG( OPERATION, ENTRY, "get_ctrls: conn %lu\n", conn->c_connid, 0, 0 );
143 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
145 if( op->o_protocol < LDAP_VERSION3 ) {
146 rc = SLAPD_DISCONNECT;
147 errmsg = "controls require LDAPv3";
151 /* one for first control, one for termination */
152 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
155 if( op->ctrls == NULL ) {
157 errmsg = "no memory";
162 op->o_ctrls[nctrls] = NULL;
164 /* step through each element */
165 for( tag = ber_first_element( ber, &len, &opaque );
167 tag = ber_next_element( ber, &len, opaque ) )
170 LDAPControl **tctrls;
172 c = ch_calloc( 1, sizeof(LDAPControl) );
176 ldap_controls_free(op->o_ctrls);
180 errmsg = "no memory";
185 /* allocate pointer space for current controls (nctrls)
186 * + this control + extra NULL
188 tctrls = ch_realloc( op->o_ctrls,
189 (nctrls+2) * sizeof(LDAPControl *));
192 if( tctrls == NULL ) {
194 ldap_controls_free(op->o_ctrls);
198 errmsg = "no memory";
202 op->o_ctrls = tctrls;
204 op->o_ctrls[nctrls++] = c;
205 op->o_ctrls[nctrls] = NULL;
207 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
209 if( tag == LBER_ERROR ) {
211 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu get OID failed.\n",
212 conn->c_connid, 0, 0 );
214 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
217 ldap_controls_free( op->o_ctrls );
219 rc = SLAPD_DISCONNECT;
220 errmsg = "decoding controls error";
224 tag = ber_peek_tag( ber, &len );
226 if( tag == LBER_BOOLEAN ) {
228 tag = ber_scanf( ber, "b", &crit );
230 if( tag == LBER_ERROR ) {
232 LDAP_LOG( OPERATION, INFO,
233 "get_ctrls: conn %lu get crit failed.\n",
234 conn->c_connid, 0, 0 );
236 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
239 ldap_controls_free( op->o_ctrls );
241 rc = SLAPD_DISCONNECT;
242 errmsg = "decoding controls error";
246 c->ldctl_iscritical = (crit != 0);
247 tag = ber_peek_tag( ber, &len );
250 if( tag == LBER_OCTETSTRING ) {
251 tag = ber_scanf( ber, "o", &c->ldctl_value );
253 if( tag == LBER_ERROR ) {
255 LDAP_LOG( OPERATION, INFO, "get_ctrls: conn %lu: "
256 "%s (%scritical): get value failed.\n",
257 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
258 c->ldctl_iscritical ? "" : "non" );
260 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
261 "%s (%scritical): get value failed.\n",
263 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
264 c->ldctl_iscritical ? "" : "non" );
266 ldap_controls_free( op->o_ctrls );
268 rc = SLAPD_DISCONNECT;
269 errmsg = "decoding controls error";
275 LDAP_LOG( OPERATION, INFO,
276 "get_ctrls: conn %lu oid=\"%s\" (%scritical)\n",
277 conn->c_connid, c->ldctl_oid ? c->ldctl_oid : "(NULL)",
278 c->ldctl_iscritical ? "" : "non" );
280 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
281 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
282 c->ldctl_iscritical ? "" : "non",
286 sc = find_ctrl( c->ldctl_oid );
288 /* recognized control */
290 switch( op->o_tag ) {
292 tagmask = SLAP_CTRL_ADD;
295 tagmask = SLAP_CTRL_BIND;
297 case LDAP_REQ_COMPARE:
298 tagmask = SLAP_CTRL_COMPARE;
300 case LDAP_REQ_DELETE:
301 tagmask = SLAP_CTRL_DELETE;
303 case LDAP_REQ_MODIFY:
304 tagmask = SLAP_CTRL_MODIFY;
306 case LDAP_REQ_RENAME:
307 tagmask = SLAP_CTRL_RENAME;
309 case LDAP_REQ_SEARCH:
310 tagmask = SLAP_CTRL_SEARCH;
312 case LDAP_REQ_UNBIND:
313 tagmask = SLAP_CTRL_UNBIND;
315 case LDAP_REQ_ABANDON:
316 tagmask = SLAP_CTRL_ABANDON;
318 case LDAP_REQ_EXTENDED:
319 /* FIXME: check list of extended operations */
324 errmsg = "controls internal error";
328 if (( sc->sc_mask & tagmask ) == tagmask ) {
329 /* available extension */
331 if( !sc->sc_parse ) {
333 errmsg = "not yet implemented";
337 rc = sc->sc_parse( conn, op, c, &errmsg );
339 if( rc != LDAP_SUCCESS ) goto return_results;
341 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
342 /* kludge to disable backend_control() check */
343 c->ldctl_iscritical = 0;
345 } else if ( tagmask == SLAP_CTRL_SEARCH &&
346 sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
348 /* kludge to disable backend_control() check */
349 c->ldctl_iscritical = 0;
352 } else if( c->ldctl_iscritical ) {
353 /* unavailable CRITICAL control */
354 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
355 errmsg = "critical extension is unavailable";
359 } else if( c->ldctl_iscritical ) {
360 /* unrecognized CRITICAL control */
361 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
362 errmsg = "critical extension is not recognized";
369 LDAP_LOG( OPERATION, RESULTS,
370 "get_ctrls: n=%d rc=%d err=%s\n", nctrls, rc, errmsg ? errmsg : "" );
372 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: n=%d rc=%d err=%s\n",
373 nctrls, rc, errmsg ? errmsg : "");
376 if( sendres && rc != LDAP_SUCCESS ) {
377 if( rc == SLAPD_DISCONNECT ) {
378 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
380 send_ldap_result( conn, op, rc,
381 NULL, errmsg, NULL, NULL );
388 static int parseManageDSAit (
394 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
395 *text = "manageDSAit control specified multiple times";
396 return LDAP_PROTOCOL_ERROR;
399 if ( ctrl->ldctl_value.bv_len ) {
400 *text = "manageDSAit control value not empty";
401 return LDAP_PROTOCOL_ERROR;
404 op->o_managedsait = ctrl->ldctl_iscritical
405 ? SLAP_CRITICAL_CONTROL
406 : SLAP_NONCRITICAL_CONTROL;
411 #ifdef LDAP_CONTROL_SUBENTRIES
412 static int parseSubentries (
418 if ( op->o_subentries != SLAP_NO_CONTROL ) {
419 *text = "subentries control specified multiple times";
420 return LDAP_PROTOCOL_ERROR;
423 /* FIXME: should use BER library */
424 if( ( ctrl->ldctl_value.bv_len != 3 )
425 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
426 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
428 *text = "subentries control value encoding is bogus";
429 return LDAP_PROTOCOL_ERROR;
432 op->o_subentries = ctrl->ldctl_iscritical
433 ? SLAP_CRITICAL_CONTROL
434 : SLAP_NONCRITICAL_CONTROL;
436 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
442 static int parseNoOp (
448 if ( op->o_noop != SLAP_NO_CONTROL ) {
449 *text = "noop control specified multiple times";
450 return LDAP_PROTOCOL_ERROR;
453 if ( ctrl->ldctl_value.bv_len ) {
454 *text = "noop control value not empty";
455 return LDAP_PROTOCOL_ERROR;
458 op->o_noop = ctrl->ldctl_iscritical
459 ? SLAP_CRITICAL_CONTROL
460 : SLAP_NONCRITICAL_CONTROL;
465 static int parsePagedResults (
474 struct berval cookie = { 0, NULL };
476 if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
477 *text = "paged results control specified multiple times";
478 return LDAP_PROTOCOL_ERROR;
481 if ( ctrl->ldctl_value.bv_len == 0 ) {
482 *text = "paged results control value is empty";
483 return LDAP_PROTOCOL_ERROR;
486 /* Parse the control value
487 * realSearchControlValue ::= SEQUENCE {
488 * size INTEGER (0..maxInt),
489 * -- requested page size from client
490 * -- result set size estimate from server
491 * cookie OCTET STRING
493 ber = ber_init( &ctrl->ldctl_value );
495 *text = "internal error";
499 tag = ber_scanf( ber, "{im}", &size, &cookie );
500 (void) ber_free( ber, 1 );
502 if( tag == LBER_ERROR ) {
503 *text = "paged results control could not be decoded";
504 return LDAP_PROTOCOL_ERROR;
508 *text = "paged results control size invalid";
509 return LDAP_PROTOCOL_ERROR;
512 if( cookie.bv_len ) {
513 PagedResultsCookie reqcookie;
514 if( cookie.bv_len != sizeof( reqcookie ) ) {
516 *text = "paged results cookie is invalid";
517 return LDAP_PROTOCOL_ERROR;
520 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
522 if( reqcookie > op->o_pagedresults_state.ps_cookie ) {
524 *text = "paged results cookie is invalid";
525 return LDAP_PROTOCOL_ERROR;
527 } else if( reqcookie < op->o_pagedresults_state.ps_cookie ) {
528 *text = "paged results cookie is invalid or old";
529 return LDAP_UNWILLING_TO_PERFORM;
532 /* Initial request. Initialize state. */
533 op->o_pagedresults_state.ps_cookie = 0;
534 op->o_pagedresults_state.ps_id = NOID;
537 op->o_pagedresults_size = size;
539 op->o_pagedresults = ctrl->ldctl_iscritical
540 ? SLAP_CRITICAL_CONTROL
541 : SLAP_NONCRITICAL_CONTROL;
546 int parseValuesReturnFilter (
554 struct berval fstr = { 0, NULL };
555 const char *err_msg = "";
557 if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
558 *text = "valuesreturnfilter control specified multiple times";
559 return LDAP_PROTOCOL_ERROR;
562 ber = ber_init( &(ctrl->ldctl_value) );
564 *text = "internal error";
568 rc = get_vrFilter( conn, ber, &(op->vrFilter), &err_msg);
570 if( rc != LDAP_SUCCESS ) {
572 if( rc == SLAPD_DISCONNECT ) {
573 send_ldap_disconnect( conn, op,
574 LDAP_PROTOCOL_ERROR, *text );
576 send_ldap_result( conn, op, rc,
577 NULL, *text, NULL, NULL );
579 if( fstr.bv_val != NULL) free( fstr.bv_val );
580 if( op->vrFilter != NULL) vrFilter_free( op->vrFilter );
583 vrFilter2bv( op->vrFilter, &fstr );
587 LDAP_LOG( OPERATION, ARGS,
588 "parseValuesReturnFilter: conn %d vrFilter: %s\n",
589 conn->c_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
591 Debug( LDAP_DEBUG_ARGS, " vrFilter: %s\n",
592 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
595 op->o_valuesreturnfilter = ctrl->ldctl_iscritical
596 ? SLAP_CRITICAL_CONTROL
597 : SLAP_NONCRITICAL_CONTROL;
602 #ifdef LDAP_CLIENT_UPDATE
603 static int parseClientUpdate (
614 struct berval scheme = { 0, NULL };
615 struct berval cookie = { 0, NULL };
617 if ( op->o_clientupdate != SLAP_NO_CONTROL ) {
618 *text = "LCUP client update control specified multiple times";
619 return LDAP_PROTOCOL_ERROR;
622 if ( ctrl->ldctl_value.bv_len == 0 ) {
623 *text = "LCUP client update control value is empty";
624 return LDAP_PROTOCOL_ERROR;
627 /* Parse the control value
628 * ClientUpdateControlValue ::= SEQUENCE {
629 * updateType ENUMERATED {
630 * synchronizeOnly {0},
631 * synchronizeAndPersist {1},
633 * sendCookieInterval INTEGER OPTIONAL,
634 * cookie LCUPCookie OPTIONAL
638 ber = ber_init( &ctrl->ldctl_value );
640 *text = "internal error";
644 if ( (tag = ber_scanf( ber, "{i" /*}*/, &type )) == LBER_ERROR ) {
645 *text = "LCUP client update control : decoding error";
646 return LDAP_PROTOCOL_ERROR;
650 case LDAP_CUP_SYNC_ONLY:
651 type = SLAP_LCUP_SYNC;
653 case LDAP_CUP_SYNC_AND_PERSIST:
654 type = SLAP_LCUP_SYNC_AND_PERSIST;
656 case LDAP_CUP_PERSIST_ONLY:
657 type = SLAP_LCUP_PERSIST;
660 *text = "LCUP client update control : unknown update type";
661 return LDAP_PROTOCOL_ERROR;
664 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
665 *text = "LCUP client update control : decoding error";
666 return LDAP_PROTOCOL_ERROR;
669 if ( tag == LDAP_TAG_INTERVAL ) {
670 if ( (tag = ber_scanf( ber, "i", &interval )) == LBER_ERROR ) {
671 *text = "LCUP client update control : decoding error";
672 return LDAP_PROTOCOL_ERROR;
675 if ( interval <= 0 ) {
676 /* server chooses interval */
677 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
681 /* server chooses interval */
682 interval = LDAP_CUP_DEFAULT_SEND_COOKIE_INTERVAL;
685 if ( (tag = ber_peek_tag( ber, &len )) == LBER_DEFAULT ) {
686 *text = "LCUP client update control : decoding error";
687 return LDAP_PROTOCOL_ERROR;
690 if ( tag == LDAP_TAG_COOKIE ) {
691 if ( (tag = ber_scanf( ber, /*{*/ "{mm}}",
692 &scheme, &cookie )) == LBER_ERROR ) {
693 *text = "LCUP client update control : decoding error";
694 return LDAP_PROTOCOL_ERROR;
698 /* TODO : Cookie Scheme Validation */
700 if ( lcup_cookie_scheme_validate(scheme) != LDAP_SUCCESS ) {
701 *text = "Unsupported LCUP cookie scheme";
702 return LCUP_UNSUPPORTED_SCHEME;
705 if ( lcup_cookie_validate(scheme, cookie) != LDAP_SUCCESS ) {
706 *text = "Invalid LCUP cookie";
707 return LCUP_INVALID_COOKIE;
711 ber_dupbv( &op->o_clientupdate_state, &cookie );
713 (void) ber_free( ber, 1 );
715 op->o_clientupdate_type = (char) type;
716 op->o_clientupdate_interval = interval;
718 op->o_clientupdate = ctrl->ldctl_iscritical
719 ? SLAP_CRITICAL_CONTROL
720 : SLAP_NONCRITICAL_CONTROL;
724 #endif /* LDAP_CLIENT_UPDATE */