]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
88c968ce749745d65b9c89b714985f8feae828fa
[openldap] / servers / slapd / controls.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
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>.
14  */
15
16 #include "portable.h"
17
18 #include <stdio.h>
19
20 #include <ac/string.h>
21 #include <ac/socket.h>
22
23 #include "slap.h"
24
25 #include "../../libraries/liblber/lber-int.h"
26
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;
38 static SLAP_CTRL_PARSE_FN parseTreeDelete;
39 static SLAP_CTRL_PARSE_FN parseSearchOptions;
40 static SLAP_CTRL_PARSE_FN parseSubentries;
41
42 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
43
44 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
45 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
46
47 struct slap_control_ids slap_cids;
48
49 struct slap_control {
50         /* Control OID */
51         char *sc_oid;
52
53         /* The controlID for this control */
54         int sc_cid;
55
56         /* Operations supported by control */
57         slap_mask_t sc_mask;
58
59         /* Extended operations supported by control */
60         char **sc_extendedops;
61
62         /* Control parsing callback */
63         SLAP_CTRL_PARSE_FN *sc_parse;
64
65         LDAP_SLIST_ENTRY(slap_control) sc_next;
66 };
67
68 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
69         = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
70
71 /*
72  * all known request control OIDs should be added to this list
73  */
74 /*
75  * NOTE: initialize num_known_controls to 1 so that cid = 0 always
76  * addresses an undefined control; this allows to safely test for 
77  * well known controls even if they are not registered, e.g. if 
78  * they get moved to modules.  An example is sc_LDAPsync, which 
79  * is implemented in the syncprov overlay and thus, if configured 
80  * as dynamic module, may not be registered.  One side effect is that 
81  * slap_known_controls[0] == NULL, so it should always be used 
82  * starting from 1.
83  * FIXME: should we define the "undefined control" oid?
84  */
85 char *slap_known_controls[SLAP_MAX_CIDS+1];
86 static int num_known_controls = 1;
87
88 static char *proxy_authz_extops[] = {
89         LDAP_EXOP_MODIFY_PASSWD,
90         LDAP_EXOP_X_WHO_AM_I,
91         NULL
92 };
93
94 static struct slap_control control_defs[] = {
95         {  LDAP_CONTROL_ASSERT,
96                 (int)offsetof(struct slap_control_ids, sc_assert),
97                 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
98                 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
99         { LDAP_CONTROL_PRE_READ,
100                 (int)offsetof(struct slap_control_ids, sc_preRead),
101                 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
102                 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
103         { LDAP_CONTROL_POST_READ,
104                 (int)offsetof(struct slap_control_ids, sc_postRead),
105                 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
106                 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
107         { LDAP_CONTROL_VALUESRETURNFILTER,
108                 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
109                 SLAP_CTRL_SEARCH, NULL,
110                 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
111         { LDAP_CONTROL_PAGEDRESULTS,
112                 (int)offsetof(struct slap_control_ids, sc_pagedResults),
113                 SLAP_CTRL_SEARCH, NULL,
114                 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
115 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
116         { LDAP_CONTROL_X_DOMAIN_SCOPE,
117                 (int)offsetof(struct slap_control_ids, sc_domainScope),
118                 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
119                 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
120 #endif
121 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
122         { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
123                 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
124                 SLAP_CTRL_MODIFY, NULL,
125                 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
126 #endif
127 #ifdef SLAP_CONTROL_X_TREE_DELETE
128         { LDAP_CONTROL_X_TREE_DELETE,
129                 (int)offsetof(struct slap_control_ids, sc_treeDelete),
130                 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
131                 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
132 #endif
133 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
134         { LDAP_CONTORL_X_SEARCH_OPTIONS,
135                 (int)offsetof(struct slap_control_ids, sc_searchOptions),
136                 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
137                 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
138 #endif
139 #ifdef LDAP_CONTROL_SUBENTRIES
140         { LDAP_CONTROL_SUBENTRIES,
141                 (int)offsetof(struct slap_control_ids, sc_subentries),
142                 SLAP_CTRL_SEARCH, NULL,
143                 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
144 #endif
145         { LDAP_CONTROL_NOOP,
146                 (int)offsetof(struct slap_control_ids, sc_noOp),
147                 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
148                 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
149 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
150         { LDAP_CONTROL_MODIFY_INCREMENT,
151                 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
152                 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
153                 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
154 #endif
155         { LDAP_CONTROL_MANAGEDSAIT,
156                 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
157                 SLAP_CTRL_ACCESS, NULL,
158                 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
159         { LDAP_CONTROL_PROXY_AUTHZ,
160                 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
161                 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
162                 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
163         { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
164 };
165
166 /*
167  * Register a supported control.
168  *
169  * This can be called by an OpenLDAP plugin or, indirectly, by a
170  * SLAPI plugin calling slapi_register_supported_control().
171  */
172 int
173 register_supported_control(const char *controloid,
174         slap_mask_t controlmask,
175         char **controlexops,
176         SLAP_CTRL_PARSE_FN *controlparsefn,
177         int *controlcid)
178 {
179         struct slap_control *sc;
180         int i;
181
182         if ( num_known_controls >= SLAP_MAX_CIDS ) {
183                 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
184                         " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
185                 SLAP_MAX_CIDS, 0, 0 );
186                 return LDAP_OTHER;
187         }
188
189         if ( controloid == NULL ) return LDAP_PARAM_ERROR;
190
191         /* sanity check - should never happen */
192         for ( i = 0; slap_known_controls[ i ]; i++ ) {
193                 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
194                         Debug( LDAP_DEBUG_ANY,
195                                 "Control %s already registered.\n",
196                                 controloid, 0, 0 );
197                         return LDAP_PARAM_ERROR;
198                 }
199         }
200
201         sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
202         if ( sc == NULL ) return LDAP_NO_MEMORY;
203
204         sc->sc_oid = ch_strdup( controloid );
205         sc->sc_mask = controlmask;
206         if ( controlexops != NULL ) {
207                 sc->sc_extendedops = ldap_charray_dup( controlexops );
208                 if ( sc->sc_extendedops == NULL ) {
209                         ch_free( sc );
210                         return LDAP_NO_MEMORY;
211                 }
212         } else {
213                 sc->sc_extendedops = NULL;
214         }
215         sc->sc_parse = controlparsefn;
216
217         if ( controlcid ) *controlcid = num_known_controls;
218         sc->sc_cid = num_known_controls;
219
220         /* Update slap_known_controls, too. */
221         slap_known_controls[num_known_controls-1] = sc->sc_oid;
222         slap_known_controls[num_known_controls++] = NULL;
223
224         LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
225         LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
226         return LDAP_SUCCESS;
227 }
228
229 /*
230  * One-time initialization of internal controls.
231  */
232 int
233 slap_controls_init( void )
234 {
235         int i, rc;
236         struct slap_control *sc;
237
238         rc = LDAP_SUCCESS;
239
240         for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
241                 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
242                 rc = register_supported_control( control_defs[i].sc_oid,
243                         control_defs[i].sc_mask, control_defs[i].sc_extendedops,
244                         control_defs[i].sc_parse, cid );
245                 if ( rc != LDAP_SUCCESS ) break;
246         }
247
248         return rc;
249 }
250
251 /*
252  * Free memory associated with list of supported controls.
253  */
254 void
255 controls_destroy( void )
256 {
257         struct slap_control *sc;
258
259         while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
260                 sc = LDAP_SLIST_FIRST(&controls_list);
261                 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
262
263                 ch_free( sc->sc_oid );
264                 if ( sc->sc_extendedops != NULL ) {
265                         ldap_charray_free( sc->sc_extendedops );
266                 }
267                 ch_free( sc );
268         }
269 }
270
271 /*
272  * Format the supportedControl attribute of the root DSE,
273  * detailing which controls are supported by the directory
274  * server.
275  */
276 int
277 controls_root_dse_info( Entry *e )
278 {
279         AttributeDescription *ad_supportedControl
280                 = slap_schema.si_ad_supportedControl;
281         struct berval vals[2];
282         struct slap_control *sc;
283
284         vals[1].bv_val = NULL;
285         vals[1].bv_len = 0;
286
287         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
288                 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
289
290                 vals[0].bv_val = sc->sc_oid;
291                 vals[0].bv_len = strlen( sc->sc_oid );
292
293                 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
294                         return -1;
295                 }
296         }
297
298         return 0;
299 }
300
301 /*
302  * Return a list of OIDs and operation masks for supported
303  * controls. Used by SLAPI.
304  */
305 int
306 get_supported_controls(char ***ctrloidsp,
307         slap_mask_t **ctrlmasks)
308 {
309         int i, n;
310         char **oids;
311         slap_mask_t *masks;
312         int rc;
313         struct slap_control *sc;
314
315         n = 0;
316
317         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
318                 n++;
319         }
320
321         if ( n == 0 ) {
322                 *ctrloidsp = NULL;
323                 *ctrlmasks = NULL;
324                 return LDAP_SUCCESS;
325         }
326
327         oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
328         if ( oids == NULL ) {
329                 return LDAP_NO_MEMORY;
330         }
331         masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
332         if  ( masks == NULL ) {
333                 ch_free( oids );
334                 return LDAP_NO_MEMORY;
335         }
336
337         n = 0;
338
339         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
340                 oids[n] = ch_strdup( sc->sc_oid );
341                 masks[n] = sc->sc_mask;
342                 n++;
343         }
344         oids[n] = NULL;
345         masks[n] = 0;
346
347         *ctrloidsp = oids;
348         *ctrlmasks = masks;
349
350         return LDAP_SUCCESS;
351 }
352
353 /*
354  * Find a control given its OID.
355  */
356 static struct slap_control *
357 find_ctrl( const char *oid )
358 {
359         struct slap_control *sc;
360
361         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
362                 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
363                         return sc;
364                 }
365         }
366
367         return NULL;
368 }
369
370 int
371 slap_find_control_id(
372         const char *oid,
373         int *cid )
374 {
375         struct slap_control *ctrl = find_ctrl( oid );
376         if ( ctrl ) {
377                 if ( cid ) *cid = ctrl->sc_cid;
378                 return LDAP_SUCCESS;
379         }
380         return LDAP_CONTROL_NOT_FOUND;
381 }
382
383 void slap_free_ctrls(
384         Operation *op,
385         LDAPControl **ctrls )
386 {
387         int i;
388
389         for (i=0; ctrls[i]; i++) {
390                 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
391         }
392         op->o_tmpfree( ctrls, op->o_tmpmemctx );
393 }
394
395 int get_ctrls(
396         Operation *op,
397         SlapReply *rs,
398         int sendres )
399 {
400         int nctrls = 0;
401         ber_tag_t tag;
402         ber_len_t len;
403         char *opaque;
404         BerElement *ber = op->o_ber;
405         struct slap_control *sc;
406         struct berval bv;
407
408         len = ber_pvt_ber_remaining(ber);
409
410         if( len == 0) {
411                 /* no controls */
412                 rs->sr_err = LDAP_SUCCESS;
413                 return rs->sr_err;
414         }
415
416         if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
417                 if( tag == LBER_ERROR ) {
418                         rs->sr_err = SLAPD_DISCONNECT;
419                         rs->sr_text = "unexpected data in PDU";
420                 }
421
422                 goto return_results;
423         }
424
425         Debug( LDAP_DEBUG_TRACE,
426                 "=> get_ctrls\n", 0, 0, 0 );
427
428         if( op->o_protocol < LDAP_VERSION3 ) {
429                 rs->sr_err = SLAPD_DISCONNECT;
430                 rs->sr_text = "controls require LDAPv3";
431                 goto return_results;
432         }
433
434         /* one for first control, one for termination */
435         op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
436
437 #if 0
438         if( op->ctrls == NULL ) {
439                 rs->sr_err = LDAP_NO_MEMORY;
440                 rs->sr_text = "no memory";
441                 goto return_results;
442         }
443 #endif
444
445         op->o_ctrls[nctrls] = NULL;
446
447         /* step through each element */
448         for( tag = ber_first_element( ber, &len, &opaque );
449                 tag != LBER_ERROR;
450                 tag = ber_next_element( ber, &len, opaque ) )
451         {
452                 LDAPControl *c;
453                 LDAPControl **tctrls;
454
455                 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
456                 memset(c, 0, sizeof(LDAPControl));
457
458                 /* allocate pointer space for current controls (nctrls)
459                  * + this control + extra NULL
460                  */
461                 tctrls = op->o_tmprealloc( op->o_ctrls,
462                         (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
463
464 #if 0
465                 if( tctrls == NULL ) {
466                         ch_free( c );
467                         ldap_controls_free(op->o_ctrls);
468                         op->o_ctrls = NULL;
469
470                         rs->sr_err = LDAP_NO_MEMORY;
471                         rs->sr_text = "no memory";
472                         goto return_results;
473                 }
474 #endif
475                 op->o_ctrls = tctrls;
476
477                 op->o_ctrls[nctrls++] = c;
478                 op->o_ctrls[nctrls] = NULL;
479
480                 tag = ber_scanf( ber, "{m" /*}*/, &bv );
481                 c->ldctl_oid = bv.bv_val;
482
483                 if( tag == LBER_ERROR ) {
484                         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
485                                 0, 0, 0 );
486
487                         slap_free_ctrls( op, op->o_ctrls );
488                         op->o_ctrls = NULL;
489                         rs->sr_err = SLAPD_DISCONNECT;
490                         rs->sr_text = "decoding controls error";
491                         goto return_results;
492
493                 } else if( c->ldctl_oid == NULL ) {
494                         Debug( LDAP_DEBUG_TRACE,
495                                 "get_ctrls: conn %lu got emtpy OID.\n",
496                                 op->o_connid, 0, 0 );
497
498                         slap_free_ctrls( op, op->o_ctrls );
499                         op->o_ctrls = NULL;
500                         rs->sr_err = LDAP_PROTOCOL_ERROR;
501                         rs->sr_text = "OID field is empty";
502                         goto return_results;
503                 }
504
505                 tag = ber_peek_tag( ber, &len );
506
507                 if( tag == LBER_BOOLEAN ) {
508                         ber_int_t crit;
509                         tag = ber_scanf( ber, "b", &crit );
510
511                         if( tag == LBER_ERROR ) {
512                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
513                                         0, 0, 0 );
514                                 slap_free_ctrls( op, op->o_ctrls );
515                                 op->o_ctrls = NULL;
516                                 rs->sr_err = SLAPD_DISCONNECT;
517                                 rs->sr_text = "decoding controls error";
518                                 goto return_results;
519                         }
520
521                         c->ldctl_iscritical = (crit != 0);
522                         tag = ber_peek_tag( ber, &len );
523                 }
524
525                 if( tag == LBER_OCTETSTRING ) {
526                         tag = ber_scanf( ber, "m", &c->ldctl_value );
527
528                         if( tag == LBER_ERROR ) {
529                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
530                                         "%s (%scritical): get value failed.\n",
531                                         op->o_connid, c->ldctl_oid,
532                                         c->ldctl_iscritical ? "" : "non" );
533                                 slap_free_ctrls( op, op->o_ctrls );
534                                 op->o_ctrls = NULL;
535                                 rs->sr_err = SLAPD_DISCONNECT;
536                                 rs->sr_text = "decoding controls error";
537                                 goto return_results;
538                         }
539                 }
540
541                 Debug( LDAP_DEBUG_TRACE,
542                         "=> get_ctrls: oid=\"%s\" (%scritical)\n",
543                         c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
544
545                 sc = find_ctrl( c->ldctl_oid );
546                 if( sc != NULL ) {
547                         /* recognized control */
548                         slap_mask_t tagmask;
549                         switch( op->o_tag ) {
550                         case LDAP_REQ_ADD:
551                                 tagmask = SLAP_CTRL_ADD;
552                                 break;
553                         case LDAP_REQ_BIND:
554                                 tagmask = SLAP_CTRL_BIND;
555                                 break;
556                         case LDAP_REQ_COMPARE:
557                                 tagmask = SLAP_CTRL_COMPARE;
558                                 break;
559                         case LDAP_REQ_DELETE:
560                                 tagmask = SLAP_CTRL_DELETE;
561                                 break;
562                         case LDAP_REQ_MODIFY:
563                                 tagmask = SLAP_CTRL_MODIFY;
564                                 break;
565                         case LDAP_REQ_RENAME:
566                                 tagmask = SLAP_CTRL_RENAME;
567                                 break;
568                         case LDAP_REQ_SEARCH:
569                                 tagmask = SLAP_CTRL_SEARCH;
570                                 break;
571                         case LDAP_REQ_UNBIND:
572                                 tagmask = SLAP_CTRL_UNBIND;
573                                 break;
574                         case LDAP_REQ_ABANDON:
575                                 tagmask = SLAP_CTRL_ABANDON;
576                                 break;
577                         case LDAP_REQ_EXTENDED:
578                                 tagmask=~0L;
579                                 assert( op->ore_reqoid.bv_val != NULL );
580                                 if( sc->sc_extendedops != NULL ) {
581                                         int i;
582                                         for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
583                                                 if( strcmp( op->ore_reqoid.bv_val,
584                                                         sc->sc_extendedops[i] ) == 0 )
585                                                 {
586                                                         tagmask=0L;
587                                                         break;
588                                                 }
589                                         }
590                                 }
591                                 break;
592                         default:
593                                 rs->sr_err = LDAP_OTHER;
594                                 rs->sr_text = "controls internal error";
595                                 goto return_results;
596                         }
597
598                         if (( sc->sc_mask & tagmask ) == tagmask ) {
599                                 /* available extension */
600                                 int     rc;
601
602                                 if( !sc->sc_parse ) {
603                                         rs->sr_err = LDAP_OTHER;
604                                         rs->sr_text = "not yet implemented";
605                                         goto return_results;
606                                 }
607
608                                 rc = sc->sc_parse( op, rs, c );
609                                 if ( rc ) {
610                                         assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
611                                         rs->sr_err = rc;
612                                         goto return_results;
613                                 }
614
615 #ifdef SLAP_CONTROL_AVAILABILITY_KLUDGE
616                                 if ( sc->sc_mask & SLAP_CTRL_FRONTEND ) {
617                                         /* KLUDGE: disable backend_control() check */
618                                         c->ldctl_iscritical = 0;
619
620                                 } else if ( tagmask == SLAP_CTRL_SEARCH &&
621                                         sc->sc_mask & SLAP_CTRL_FRONTEND_SEARCH )
622                                 {
623                                         /* KLUDGE: disable backend_control() check */
624                                         c->ldctl_iscritical = 0;
625
626                                 } else {
627                                         /* KLUDGE: enable backend_control() check */
628                                         c->ldctl_iscritical = 1;
629                                 }
630 #endif
631
632                         } else if( c->ldctl_iscritical ) {
633                                 /* unavailable CRITICAL control */
634                                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
635                                 rs->sr_text = "critical extension is unavailable";
636                                 goto return_results;
637                         }
638
639                 } else if( c->ldctl_iscritical ) {
640                         /* unrecognized CRITICAL control */
641                         rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
642                         rs->sr_text = "critical extension is not recognized";
643                         goto return_results;
644                 }
645 next_ctrl:;
646         }
647
648 return_results:
649         Debug( LDAP_DEBUG_TRACE,
650                 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
651                 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
652
653         if( sendres && rs->sr_err != LDAP_SUCCESS ) {
654                 if( rs->sr_err == SLAPD_DISCONNECT ) {
655                         rs->sr_err = LDAP_PROTOCOL_ERROR;
656                         send_ldap_disconnect( op, rs );
657                         rs->sr_err = SLAPD_DISCONNECT;
658                 } else {
659                         send_ldap_result( op, rs );
660                 }
661         }
662
663         return rs->sr_err;
664 }
665
666 static int parseModifyIncrement (
667         Operation *op,
668         SlapReply *rs,
669         LDAPControl *ctrl )
670 {
671 #if 0
672         if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
673                 rs->sr_text = "modifyIncrement control specified multiple times";
674                 return LDAP_PROTOCOL_ERROR;
675         }
676 #endif
677
678         if ( ctrl->ldctl_value.bv_len ) {
679                 rs->sr_text = "modifyIncrement control value not empty";
680                 return LDAP_PROTOCOL_ERROR;
681         }
682
683 #if 0
684         op->o_modifyIncrement = ctrl->ldctl_iscritical
685                 ? SLAP_CONTROL_CRITICAL
686                 : SLAP_CONTROL_NONCRITICAL;
687 #endif
688
689         return LDAP_SUCCESS;
690 }
691
692 static int parseManageDSAit (
693         Operation *op,
694         SlapReply *rs,
695         LDAPControl *ctrl )
696 {
697         if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
698                 rs->sr_text = "manageDSAit control specified multiple times";
699                 return LDAP_PROTOCOL_ERROR;
700         }
701
702         if ( ctrl->ldctl_value.bv_len ) {
703                 rs->sr_text = "manageDSAit control value not empty";
704                 return LDAP_PROTOCOL_ERROR;
705         }
706
707         op->o_managedsait = ctrl->ldctl_iscritical
708                 ? SLAP_CONTROL_CRITICAL
709                 : SLAP_CONTROL_NONCRITICAL;
710
711         return LDAP_SUCCESS;
712 }
713
714 static int parseProxyAuthz (
715         Operation *op,
716         SlapReply *rs,
717         LDAPControl *ctrl )
718 {
719         int             rc;
720         struct berval   dn = BER_BVNULL;
721
722         if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
723                 rs->sr_text = "proxy authorization control specified multiple times";
724                 return LDAP_PROTOCOL_ERROR;
725         }
726
727         op->o_proxy_authz = ctrl->ldctl_iscritical
728                 ? SLAP_CONTROL_CRITICAL
729                 : SLAP_CONTROL_NONCRITICAL;
730
731         Debug( LDAP_DEBUG_ARGS,
732                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
733                 op->o_connid,
734                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
735                 0 );
736
737         if( ctrl->ldctl_value.bv_len == 0 ) {
738                 Debug( LDAP_DEBUG_TRACE,
739                         "parseProxyAuthz: conn=%lu anonymous\n", 
740                         op->o_connid, 0, 0 );
741
742                 /* anonymous */
743                 free( op->o_dn.bv_val );
744                 op->o_dn.bv_len = 0;
745                 op->o_dn.bv_val = ch_strdup( "" );
746
747                 free( op->o_ndn.bv_val );
748                 op->o_ndn.bv_len = 0;
749                 op->o_ndn.bv_val = ch_strdup( "" );
750
751                 return LDAP_SUCCESS;
752         }
753
754         rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
755                         NULL, &dn, SLAP_GETDN_AUTHZID );
756
757         /* FIXME: empty DN in proxyAuthz control should be legal... */
758         if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
759                 if ( dn.bv_val ) {
760                         ch_free( dn.bv_val );
761                 }
762                 rs->sr_text = "authzId mapping failed";
763                 return LDAP_PROXY_AUTHZ_FAILURE;
764         }
765
766         Debug( LDAP_DEBUG_TRACE,
767                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
768                 op->o_connid,
769                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
770
771         rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
772
773         if( rc ) {
774                 ch_free( dn.bv_val );
775                 rs->sr_text = "not authorized to assume identity";
776                 return LDAP_PROXY_AUTHZ_FAILURE;
777         }
778
779         ch_free( op->o_dn.bv_val );
780         ch_free( op->o_ndn.bv_val );
781
782         op->o_dn.bv_val = NULL;
783         op->o_ndn = dn;
784
785         Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
786             op->o_log_prefix, dn.bv_val, 0, 0, 0 );
787
788         /*
789          * NOTE: since slap_sasl_getdn() returns a normalized dn,
790          * from now on op->o_dn is normalized
791          */
792         ber_dupbv( &op->o_dn, &dn );
793
794         return LDAP_SUCCESS;
795 }
796
797 static int parseNoOp (
798         Operation *op,
799         SlapReply *rs,
800         LDAPControl *ctrl )
801 {
802         if ( op->o_noop != SLAP_CONTROL_NONE ) {
803                 rs->sr_text = "noop control specified multiple times";
804                 return LDAP_PROTOCOL_ERROR;
805         }
806
807         if ( ctrl->ldctl_value.bv_len ) {
808                 rs->sr_text = "noop control value not empty";
809                 return LDAP_PROTOCOL_ERROR;
810         }
811
812         op->o_noop = ctrl->ldctl_iscritical
813                 ? SLAP_CONTROL_CRITICAL
814                 : SLAP_CONTROL_NONCRITICAL;
815
816         return LDAP_SUCCESS;
817 }
818
819 static int parsePagedResults (
820         Operation *op,
821         SlapReply *rs,
822         LDAPControl *ctrl )
823 {
824         int             rc = LDAP_SUCCESS;
825         ber_tag_t       tag;
826         ber_int_t       size;
827         BerElement      *ber;
828         struct berval   cookie = BER_BVNULL;
829         PagedResultsState       *ps;
830
831         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
832                 rs->sr_text = "paged results control specified multiple times";
833                 return LDAP_PROTOCOL_ERROR;
834         }
835
836         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
837                 rs->sr_text = "paged results control value is empty (or absent)";
838                 return LDAP_PROTOCOL_ERROR;
839         }
840
841         /* Parse the control value
842          *      realSearchControlValue ::= SEQUENCE {
843          *              size    INTEGER (0..maxInt),
844          *                              -- requested page size from client
845          *                              -- result set size estimate from server
846          *              cookie  OCTET STRING
847          * }
848          */
849         ber = ber_init( &ctrl->ldctl_value );
850         if ( ber == NULL ) {
851                 rs->sr_text = "internal error";
852                 return LDAP_OTHER;
853         }
854
855         tag = ber_scanf( ber, "{im}", &size, &cookie );
856
857         if ( tag == LBER_ERROR ) {
858                 rs->sr_text = "paged results control could not be decoded";
859                 rc = LDAP_PROTOCOL_ERROR;
860                 goto done;
861         }
862
863         if ( size < 0 ) {
864                 rs->sr_text = "paged results control size invalid";
865                 rc = LDAP_PROTOCOL_ERROR;
866                 goto done;
867         }
868
869 #if 0
870         /* defer cookie decoding/checks to backend... */
871         if ( cookie.bv_len ) {
872                 PagedResultsCookie reqcookie;
873                 if( cookie.bv_len != sizeof( reqcookie ) ) {
874                         /* bad cookie */
875                         rs->sr_text = "paged results cookie is invalid";
876                         rc = LDAP_PROTOCOL_ERROR;
877                         goto done;
878                 }
879
880                 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
881
882                 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
883                         /* bad cookie */
884                         rs->sr_text = "paged results cookie is invalid";
885                         rc = LDAP_PROTOCOL_ERROR;
886                         goto done;
887
888                 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
889                         rs->sr_text = "paged results cookie is invalid or old";
890                         rc = LDAP_UNWILLING_TO_PERFORM;
891                         goto done;
892                 }
893
894         } else {
895                 /* Initial request.  Initialize state. */
896 #if 0
897                 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
898                         /* There's another pagedResults control on the
899                          * same connection; reject new pagedResults controls 
900                          * (allowed by RFC2696) */
901                         rs->sr_text = "paged results cookie unavailable; try later";
902                         rc = LDAP_UNWILLING_TO_PERFORM;
903                         goto done;
904                 }
905 #endif
906                 op->o_pagedresults_state.ps_cookie = 0;
907                 op->o_pagedresults_state.ps_count = 0;
908         }
909 #endif
910
911         ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
912         *ps = op->o_conn->c_pagedresults_state;
913         ps->ps_size = size;
914         op->o_pagedresults_state = ps;
915
916         /* NOTE: according to RFC 2696 3.:
917
918     If the page size is greater than or equal to the sizeLimit value, the
919     server should ignore the control as the request can be satisfied in a
920     single page.
921          
922          * NOTE: this assumes that the op->ors_slimit be set
923          * before the controls are parsed.     
924          */
925                 
926         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
927                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
928
929         } else if ( ctrl->ldctl_iscritical ) {
930                 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
931
932         } else {
933                 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
934         }
935
936 done:;
937         (void)ber_free( ber, 1 );
938         return rc;
939 }
940
941 static int parseAssert (
942         Operation *op,
943         SlapReply *rs,
944         LDAPControl *ctrl )
945 {
946         BerElement      *ber;
947         struct berval   fstr = BER_BVNULL;
948         const char *err_msg = "";
949
950         if ( op->o_assert != SLAP_CONTROL_NONE ) {
951                 rs->sr_text = "assert control specified multiple times";
952                 return LDAP_PROTOCOL_ERROR;
953         }
954
955         if ( ctrl->ldctl_value.bv_len == 0 ) {
956                 rs->sr_text = "assert control value is empty (or absent)";
957                 return LDAP_PROTOCOL_ERROR;
958         }
959
960         ber = ber_init( &(ctrl->ldctl_value) );
961         if (ber == NULL) {
962                 rs->sr_text = "assert control: internal error";
963                 return LDAP_OTHER;
964         }
965         
966         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
967
968         if( rs->sr_err != LDAP_SUCCESS ) {
969                 if( rs->sr_err == SLAPD_DISCONNECT ) {
970                         rs->sr_err = LDAP_PROTOCOL_ERROR;
971                         send_ldap_disconnect( op, rs );
972                         rs->sr_err = SLAPD_DISCONNECT;
973                 } else {
974                         send_ldap_result( op, rs );
975                 }
976                 if( op->o_assertion != NULL ) {
977                         filter_free_x( op, op->o_assertion );
978                 }
979                 return rs->sr_err;
980         }
981
982 #ifdef LDAP_DEBUG
983         filter2bv_x( op, op->o_assertion, &fstr );
984
985         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
986                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
987         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
988 #endif
989
990         op->o_assert = ctrl->ldctl_iscritical
991                 ? SLAP_CONTROL_CRITICAL
992                 : SLAP_CONTROL_NONCRITICAL;
993
994         rs->sr_err = LDAP_SUCCESS;
995         return LDAP_SUCCESS;
996 }
997
998 static int parsePreRead (
999         Operation *op,
1000         SlapReply *rs,
1001         LDAPControl *ctrl )
1002 {
1003         ber_len_t siz, off, i;
1004         AttributeName *an = NULL;
1005         BerElement      *ber;
1006
1007         if ( op->o_preread != SLAP_CONTROL_NONE ) {
1008                 rs->sr_text = "preread control specified multiple times";
1009                 return LDAP_PROTOCOL_ERROR;
1010         }
1011
1012         if ( ctrl->ldctl_value.bv_len == 0 ) {
1013                 rs->sr_text = "preread control value is empty (or absent)";
1014                 return LDAP_PROTOCOL_ERROR;
1015         }
1016
1017         ber = ber_init( &(ctrl->ldctl_value) );
1018         if (ber == NULL) {
1019                 rs->sr_text = "preread control: internal error";
1020                 return LDAP_OTHER;
1021         }
1022
1023         siz = sizeof( AttributeName );
1024         off = offsetof( AttributeName, an_name );
1025         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1026                 rs->sr_text = "preread control: decoding error";
1027                 return LDAP_PROTOCOL_ERROR;
1028         }
1029
1030         for( i=0; i<siz; i++ ) {
1031                 int             rc = LDAP_SUCCESS;
1032                 const char      *dummy = NULL;
1033
1034                 an[i].an_desc = NULL;
1035                 an[i].an_oc = NULL;
1036                 an[i].an_oc_exclude = 0;
1037                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1038                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1039                         rs->sr_text = dummy
1040                                 ? dummy
1041                                 : "postread control: unknown attributeType";
1042                         return rc;
1043                 }
1044         }
1045
1046         op->o_preread = ctrl->ldctl_iscritical
1047                 ? SLAP_CONTROL_CRITICAL
1048                 : SLAP_CONTROL_NONCRITICAL;
1049
1050         op->o_preread_attrs = an;
1051
1052         rs->sr_err = LDAP_SUCCESS;
1053         return LDAP_SUCCESS;
1054 }
1055
1056 static int parsePostRead (
1057         Operation *op,
1058         SlapReply *rs,
1059         LDAPControl *ctrl )
1060 {
1061         ber_len_t siz, off, i;
1062         AttributeName *an = NULL;
1063         BerElement      *ber;
1064
1065         if ( op->o_postread != SLAP_CONTROL_NONE ) {
1066                 rs->sr_text = "postread control specified multiple times";
1067                 return LDAP_PROTOCOL_ERROR;
1068         }
1069
1070         if ( ctrl->ldctl_value.bv_len == 0 ) {
1071                 rs->sr_text = "postread control value is empty (or absent)";
1072                 return LDAP_PROTOCOL_ERROR;
1073         }
1074
1075         ber = ber_init( &(ctrl->ldctl_value) );
1076         if (ber == NULL) {
1077                 rs->sr_text = "postread control: internal error";
1078                 return LDAP_OTHER;
1079         }
1080
1081         siz = sizeof( AttributeName );
1082         off = offsetof( AttributeName, an_name );
1083         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1084                 rs->sr_text = "postread control: decoding error";
1085                 return LDAP_PROTOCOL_ERROR;
1086         }
1087
1088         for( i=0; i<siz; i++ ) {
1089                 int             rc = LDAP_SUCCESS;
1090                 const char      *dummy = NULL;
1091
1092                 an[i].an_desc = NULL;
1093                 an[i].an_oc = NULL;
1094                 an[i].an_oc_exclude = 0;
1095                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1096                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1097                         rs->sr_text = dummy
1098                                 ? dummy
1099                                 : "postread control: unknown attributeType";
1100                         return rc;
1101                 }
1102         }
1103
1104         op->o_postread = ctrl->ldctl_iscritical
1105                 ? SLAP_CONTROL_CRITICAL
1106                 : SLAP_CONTROL_NONCRITICAL;
1107
1108         op->o_postread_attrs = an;
1109
1110         rs->sr_err = LDAP_SUCCESS;
1111         return LDAP_SUCCESS;
1112 }
1113
1114 int parseValuesReturnFilter (
1115         Operation *op,
1116         SlapReply *rs,
1117         LDAPControl *ctrl )
1118 {
1119         BerElement      *ber;
1120         struct berval   fstr = BER_BVNULL;
1121         const char *err_msg = "";
1122
1123         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1124                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1125                 return LDAP_PROTOCOL_ERROR;
1126         }
1127
1128         if ( ctrl->ldctl_value.bv_len == 0 ) {
1129                 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1130                 return LDAP_PROTOCOL_ERROR;
1131         }
1132
1133         ber = ber_init( &(ctrl->ldctl_value) );
1134         if (ber == NULL) {
1135                 rs->sr_text = "internal error";
1136                 return LDAP_OTHER;
1137         }
1138         
1139         rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1140
1141         if( rs->sr_err != LDAP_SUCCESS ) {
1142                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1143                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1144                         send_ldap_disconnect( op, rs );
1145                         rs->sr_err = SLAPD_DISCONNECT;
1146                 } else {
1147                         send_ldap_result( op, rs );
1148                 }
1149                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1150         }
1151 #ifdef LDAP_DEBUG
1152         else {
1153                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1154         }
1155
1156         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1157                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1158         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1159 #endif
1160
1161         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1162                 ? SLAP_CONTROL_CRITICAL
1163                 : SLAP_CONTROL_NONCRITICAL;
1164
1165         rs->sr_err = LDAP_SUCCESS;
1166         return LDAP_SUCCESS;
1167 }
1168
1169 #ifdef LDAP_CONTROL_SUBENTRIES
1170 static int parseSubentries (
1171         Operation *op,
1172         SlapReply *rs,
1173         LDAPControl *ctrl )
1174 {
1175         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1176                 rs->sr_text = "subentries control specified multiple times";
1177                 return LDAP_PROTOCOL_ERROR;
1178         }
1179
1180         /* FIXME: should use BER library */
1181         if( ( ctrl->ldctl_value.bv_len != 3 )
1182                 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1183                 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1184         {
1185                 rs->sr_text = "subentries control value encoding is bogus";
1186                 return LDAP_PROTOCOL_ERROR;
1187         }
1188
1189         op->o_subentries = ctrl->ldctl_iscritical
1190                 ? SLAP_CONTROL_CRITICAL
1191                 : SLAP_CONTROL_NONCRITICAL;
1192
1193         if ( (void *)(ctrl->ldctl_value.bv_val[2] != 0x00))
1194                 set_subentries_visibility( op );
1195
1196         return LDAP_SUCCESS;
1197 }
1198 #endif
1199
1200 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1201 static int parsePermissiveModify (
1202         Operation *op,
1203         SlapReply *rs,
1204         LDAPControl *ctrl )
1205 {
1206         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1207                 rs->sr_text = "permissiveModify control specified multiple times";
1208                 return LDAP_PROTOCOL_ERROR;
1209         }
1210
1211         if ( ctrl->ldctl_value.bv_len ) {
1212                 rs->sr_text = "permissiveModify control value not empty";
1213                 return LDAP_PROTOCOL_ERROR;
1214         }
1215
1216         op->o_permissive_modify = ctrl->ldctl_iscritical
1217                 ? SLAP_CONTROL_CRITICAL
1218                 : SLAP_CONTROL_NONCRITICAL;
1219
1220         return LDAP_SUCCESS;
1221 }
1222 #endif
1223
1224 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1225 static int parseDomainScope (
1226         Operation *op,
1227         SlapReply *rs,
1228         LDAPControl *ctrl )
1229 {
1230         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1231                 rs->sr_text = "domainScope control specified multiple times";
1232                 return LDAP_PROTOCOL_ERROR;
1233         }
1234
1235         if ( ctrl->ldctl_value.bv_len ) {
1236                 rs->sr_text = "domainScope control value not empty";
1237                 return LDAP_PROTOCOL_ERROR;
1238         }
1239
1240         op->o_domain_scope = ctrl->ldctl_iscritical
1241                 ? SLAP_CONTROL_CRITICAL
1242                 : SLAP_CONTROL_NONCRITICAL;
1243
1244         return LDAP_SUCCESS;
1245 }
1246 #endif
1247
1248 #ifdef LDAP_CONTROL_X_TREE_DELETE
1249 static int parseTreeDelete (
1250         Operation *op,
1251         SlapReply *rs,
1252         LDAPControl *ctrl )
1253 {
1254         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1255                 rs->sr_text = "treeDelete control specified multiple times";
1256                 return LDAP_PROTOCOL_ERROR;
1257         }
1258
1259         if ( ctrl->ldctl_value.bv_len ) {
1260                 rs->sr_text = "treeDelete control value not empty";
1261                 return LDAP_PROTOCOL_ERROR;
1262         }
1263
1264         op->o_tree_delete = ctrl->ldctl_iscritical
1265                 ? SLAP_CONTROL_CRITICAL
1266                 : SLAP_CONTROL_NONCRITICAL;
1267
1268         return LDAP_SUCCESS;
1269 }
1270 #endif
1271
1272 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1273 static int parseSearchOptions (
1274         Operation *op,
1275         SlapReply *rs,
1276         LDAPControl *ctrl )
1277 {
1278         BerElement *ber;
1279         ber_int_t search_flags;
1280
1281         if ( ctrl->ldctl_value.bv_len == 0 ) {
1282                 rs->sr_text = "searchOptions control value not empty";
1283                 return LDAP_PROTOCOL_ERROR;
1284         }
1285
1286         ber = ber_init( &ctrl->ldctl_value );
1287         if( ber == NULL ) {
1288                 rs->sr_text = "internal error";
1289                 return LDAP_OTHER;
1290         }
1291
1292         if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1293                 rs->sr_text = "searchOptions control decoding error";
1294                 return LDAP_PROTOCOL_ERROR;
1295         }
1296
1297         (void) ber_free( ber, 1 );
1298
1299         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1300                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1301                         rs->sr_text = "searchOptions control specified multiple times "
1302                                 "or with domainScope control";
1303                         return LDAP_PROTOCOL_ERROR;
1304                 }
1305
1306                 op->o_domain_scope = ctrl->ldctl_iscritical
1307                         ? SLAP_CONTROL_CRITICAL
1308                         : SLAP_CONTROL_NONCRITICAL;
1309         }
1310
1311         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1312                 /* Other search flags not recognised so far,
1313                  * including:
1314                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOM
1315                  */
1316                 rs->sr_text = "searchOptions contained unrecongized flag";
1317                 return LDAP_UNWILLING_TO_PERFORM;
1318         }
1319
1320         return LDAP_SUCCESS;
1321 }
1322 #endif
1323