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
24 #define SLAP_CTRL_OPFLAGS 0x0000FFFFU
25 #define SLAP_CTRL_ABANDON 0x00000001U
26 #define SLAP_CTRL_ADD 0x00002002U
27 #define SLAP_CTRL_BIND 0x00000004U
28 #define SLAP_CTRL_COMPARE 0x00001008U
29 #define SLAP_CTRL_DELETE 0x00002010U
30 #define SLAP_CTRL_MODIFY 0x00002020U
31 #define SLAP_CTRL_RENAME 0x00002040U
32 #define SLAP_CTRL_SEARCH 0x00001080U
33 #define SLAP_CTRL_UNBIND 0x00000100U
35 #define SLAP_CTRL_INTROGATE (SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH)
36 #define SLAP_CTRL_UPDATE \
37 (SLAP_CTRL_ADD|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME)
38 #define SLAP_CTRL_ACCESS (SLAP_CTRL_INTROGATE|SLAP_CTRL_UPDATE)
40 typedef int (SLAP_CTRL_PARSE_FN) LDAP_P((
46 static SLAP_CTRL_PARSE_FN parseManageDSAit;
47 static SLAP_CTRL_PARSE_FN parseSubentries;
48 static SLAP_CTRL_PARSE_FN parseNoOp;
50 static struct slap_control {
53 char **sc_extendedops;
54 SLAP_CTRL_PARSE_FN *sc_parse;
56 } supportedControls[] = {
57 { LDAP_CONTROL_MANAGEDSAIT,
58 SLAP_CTRL_ACCESS, NULL,
60 #ifdef LDAP_CONTROL_SUBENTRIES
61 { LDAP_CONTROL_SUBENTRIES,
62 SLAP_CTRL_SEARCH, NULL,
65 #ifdef LDAP_CONTROL_NOOP
67 SLAP_CTRL_UPDATE, NULL,
74 get_supported_ctrl(int index)
76 return supportedControls[index].sc_oid;
79 static struct slap_control *
80 find_ctrl( const char *oid )
83 for( i=0; supportedControls[i].sc_oid; i++ ) {
84 if( strcmp( oid, supportedControls[i].sc_oid ) == 0 ) {
85 return &supportedControls[i];
100 BerElement *ber = op->o_ber;
101 struct slap_control *sc;
102 int rc = LDAP_SUCCESS;
103 const char *errmsg = NULL;
105 len = ber_pvt_ber_remaining(ber);
113 if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
114 if( tag == LBER_ERROR ) {
115 rc = SLAPD_DISCONNECT;
116 errmsg = "unexpected data in PDU";
123 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
124 "get_ctrls: conn %d\n", conn->c_connid ));
126 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls\n", 0, 0, 0 );
128 if( op->o_protocol < LDAP_VERSION3 ) {
129 rc = SLAPD_DISCONNECT;
130 errmsg = "controls require LDAPv3";
134 /* one for first control, one for termination */
135 op->o_ctrls = ch_malloc( 2 * sizeof(LDAPControl *) );
138 if( op->ctrls == NULL ) {
140 errmsg = "no memory";
145 op->o_ctrls[nctrls=0] = NULL;
147 /* step through each element */
148 for( tag = ber_first_element( ber, &len, &opaque );
150 tag = ber_next_element( ber, &len, opaque ) )
153 LDAPControl **tctrls;
155 c = ch_calloc( 1, sizeof(LDAPControl) );
159 ldap_controls_free(op->o_ctrls);
163 errmsg = "no memory";
168 /* allocate pointer space for current controls (nctrls)
169 * + this control + extra NULL
171 tctrls = ch_realloc( op->o_ctrls,
172 (nctrls+2) * sizeof(LDAPControl *));
175 if( tctrls == NULL ) {
177 ldap_controls_free(op->o_ctrls);
181 errmsg = "no memory";
185 op->o_ctrls = tctrls;
187 op->o_ctrls[nctrls++] = c;
188 op->o_ctrls[nctrls] = NULL;
190 tag = ber_scanf( ber, "{a" /*}*/, &c->ldctl_oid );
192 if( tag == LBER_ERROR ) {
194 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
195 "get_ctrls: conn %d get OID failed.\n",
198 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
201 ldap_controls_free( op->o_ctrls );
203 rc = SLAPD_DISCONNECT;
204 errmsg = "decoding controls error";
208 tag = ber_peek_tag( ber, &len );
210 if( tag == LBER_BOOLEAN ) {
212 tag = ber_scanf( ber, "b", &crit );
214 if( tag == LBER_ERROR ) {
216 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
217 "get_ctrls: conn %d get crit failed.\n",
220 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
223 ldap_controls_free( op->o_ctrls );
225 rc = SLAPD_DISCONNECT;
226 errmsg = "decoding controls error";
230 c->ldctl_iscritical = (crit != 0);
231 tag = ber_peek_tag( ber, &len );
234 if( tag == LBER_OCTETSTRING ) {
235 tag = ber_scanf( ber, "o", &c->ldctl_value );
237 if( tag == LBER_ERROR ) {
239 LDAP_LOG(( "operation", LDAP_LEVEL_INFO, "get_ctrls: conn %d: "
240 "%s (%scritical): get value failed.\n",
242 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
243 c->ldctl_iscritical ? "" : "non" ));
245 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %d: "
246 "%s (%scritical): get value failed.\n",
248 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
249 c->ldctl_iscritical ? "" : "non" );
251 ldap_controls_free( op->o_ctrls );
253 rc = SLAPD_DISCONNECT;
254 errmsg = "decoding controls error";
260 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
261 "get_ctrls: conn %d oid=\"%s\" (%scritical)\n",
263 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
264 c->ldctl_iscritical ? "" : "non" ));
266 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: oid=\"%s\" (%scritical)\n",
267 c->ldctl_oid ? c->ldctl_oid : "(NULL)",
268 c->ldctl_iscritical ? "" : "non",
272 sc = find_ctrl( c->ldctl_oid );
274 /* recognized control */
276 switch( op->o_tag ) {
278 tagmask = SLAP_CTRL_ADD;
281 tagmask = SLAP_CTRL_BIND;
283 case LDAP_REQ_COMPARE:
284 tagmask = SLAP_CTRL_COMPARE;
286 case LDAP_REQ_DELETE:
287 tagmask = SLAP_CTRL_DELETE;
289 case LDAP_REQ_MODIFY:
290 tagmask = SLAP_CTRL_MODIFY;
292 case LDAP_REQ_RENAME:
293 tagmask = SLAP_CTRL_RENAME;
295 case LDAP_REQ_SEARCH:
296 tagmask = SLAP_CTRL_SEARCH;
298 case LDAP_REQ_UNBIND:
299 tagmask = SLAP_CTRL_UNBIND;
301 case LDAP_REQ_EXTENDED:
302 /* FIXME: check list of extended operations */
307 errmsg = "controls internal error";
311 if (( sc->sc_mask & tagmask ) == tagmask ) {
312 /* available extension */
314 if( !sc->sc_parse ) {
316 errmsg = "not yet implemented";
320 rc = sc->sc_parse( conn, op, c, &errmsg );
322 if( rc != LDAP_SUCCESS ) goto return_results;
324 if( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
325 /* kludge to disable backend_control() check */
326 c->ldctl_iscritical = 0;
329 } else if( c->ldctl_iscritical ) {
330 /* unavailable CRITICAL control */
331 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
332 errmsg = "critical extension is unavailable";
336 } else if( c->ldctl_iscritical ) {
337 /* unrecognized CRITICAL control */
338 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
339 errmsg = "critical extension is not recognized";
346 LDAP_LOG(( "operation", LDAP_LEVEL_RESULTS,
347 "get_ctrls: conn=%d n=%d rc=%d err=%s\n",
348 conn->c_connid, nctrls, rc, errmsg ? errmsg : "" ));
350 Debug( LDAP_DEBUG_TRACE, "<= get_ctrls: n=%d rc=%d err=%s\n",
351 nctrls, rc, errmsg ? errmsg : "");
354 if( sendres && rc != LDAP_SUCCESS ) {
355 if( rc == SLAPD_DISCONNECT ) {
356 send_ldap_disconnect( conn, op, LDAP_PROTOCOL_ERROR, errmsg );
358 send_ldap_result( conn, op, rc,
359 NULL, errmsg, NULL, NULL );
366 static int parseManageDSAit (
372 if ( op->o_managedsait != SLAP_NO_CONTROL ) {
373 *text = "manageDSAit control specified multiple times";
374 return LDAP_PROTOCOL_ERROR;
377 if ( ctrl->ldctl_value.bv_len ) {
378 *text = "manageDSAit control value not empty";
379 return LDAP_PROTOCOL_ERROR;
382 op->o_managedsait = ctrl->ldctl_iscritical
383 ? SLAP_CRITICAL_CONTROL
384 : SLAP_NONCRITICAL_CONTROL;
389 #ifdef LDAP_CONTROL_SUBENTRIES
390 static int parseSubentries (
396 if ( op->o_subentries != SLAP_NO_CONTROL ) {
397 *text = "subentries control specified multiple times";
398 return LDAP_PROTOCOL_ERROR;
401 /* FIXME: should use BER library */
402 if( ( ctrl->ldctl_value.bv_len != 3 )
403 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
404 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
406 *text = "subentries control value encoding is bogus";
407 return LDAP_PROTOCOL_ERROR;
410 op->o_subentries = ctrl->ldctl_iscritical
411 ? SLAP_CRITICAL_CONTROL
412 : SLAP_NONCRITICAL_CONTROL;
414 op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
420 #ifdef LDAP_CONTROL_NOOP
421 static int parseNoOp (
427 if ( op->o_noop != SLAP_NO_CONTROL ) {
428 *text = "noop control specified multiple times";
429 return LDAP_PROTOCOL_ERROR;
432 if ( ctrl->ldctl_value.bv_len ) {
433 *text = "noop control value not empty";
434 return LDAP_PROTOCOL_ERROR;
437 op->o_noop = ctrl->ldctl_iscritical
438 ? SLAP_CRITICAL_CONTROL
439 : SLAP_NONCRITICAL_CONTROL;