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