]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
075cff8c1b61b328d12264df43d99b5d104bee13
[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_GLOBAL|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_GLOBAL|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_GLOBAL|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 int
384 slap_global_control( Operation *op, const char *oid )
385 {
386         struct slap_control *ctrl = find_ctrl( oid );
387
388         if ( ctrl == NULL ) {
389                 /* should not be reachable */
390                 Debug( LDAP_DEBUG_ANY,
391                         "slap_global_control: unrecognized control: %s\n",      
392                         oid, 0, 0 );
393                 assert( 0 );
394                 return 0;
395         }
396
397         if ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) return 1;
398
399         if (( op->o_tag & LDAP_REQ_SEARCH ) &&
400                 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ))
401         {
402                 return 1;
403         }
404
405         Debug( LDAP_DEBUG_ANY,
406                 "slap_global_control: unavailable control: %s\n",      
407                 oid, 0, 0 );
408         return 0;
409 }
410
411 void slap_free_ctrls(
412         Operation *op,
413         LDAPControl **ctrls )
414 {
415         int i;
416
417         for (i=0; ctrls[i]; i++) {
418                 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
419         }
420         op->o_tmpfree( ctrls, op->o_tmpmemctx );
421 }
422
423 int get_ctrls(
424         Operation *op,
425         SlapReply *rs,
426         int sendres )
427 {
428         int nctrls = 0;
429         ber_tag_t tag;
430         ber_len_t len;
431         char *opaque;
432         BerElement *ber = op->o_ber;
433         struct slap_control *sc;
434         struct berval bv;
435
436         len = ber_pvt_ber_remaining(ber);
437
438         if( len == 0) {
439                 /* no controls */
440                 rs->sr_err = LDAP_SUCCESS;
441                 return rs->sr_err;
442         }
443
444         if(( tag = ber_peek_tag( ber, &len )) != LDAP_TAG_CONTROLS ) {
445                 if( tag == LBER_ERROR ) {
446                         rs->sr_err = SLAPD_DISCONNECT;
447                         rs->sr_text = "unexpected data in PDU";
448                 }
449
450                 goto return_results;
451         }
452
453         Debug( LDAP_DEBUG_TRACE,
454                 "=> get_ctrls\n", 0, 0, 0 );
455
456         if( op->o_protocol < LDAP_VERSION3 ) {
457                 rs->sr_err = SLAPD_DISCONNECT;
458                 rs->sr_text = "controls require LDAPv3";
459                 goto return_results;
460         }
461
462         /* one for first control, one for termination */
463         op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
464
465 #if 0
466         if( op->ctrls == NULL ) {
467                 rs->sr_err = LDAP_NO_MEMORY;
468                 rs->sr_text = "no memory";
469                 goto return_results;
470         }
471 #endif
472
473         op->o_ctrls[nctrls] = NULL;
474
475         /* step through each element */
476         for( tag = ber_first_element( ber, &len, &opaque );
477                 tag != LBER_ERROR;
478                 tag = ber_next_element( ber, &len, opaque ) )
479         {
480                 LDAPControl *c;
481                 LDAPControl **tctrls;
482
483                 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
484                 memset(c, 0, sizeof(LDAPControl));
485
486                 /* allocate pointer space for current controls (nctrls)
487                  * + this control + extra NULL
488                  */
489                 tctrls = op->o_tmprealloc( op->o_ctrls,
490                         (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
491
492 #if 0
493                 if( tctrls == NULL ) {
494                         ch_free( c );
495                         ldap_controls_free(op->o_ctrls);
496                         op->o_ctrls = NULL;
497
498                         rs->sr_err = LDAP_NO_MEMORY;
499                         rs->sr_text = "no memory";
500                         goto return_results;
501                 }
502 #endif
503                 op->o_ctrls = tctrls;
504
505                 op->o_ctrls[nctrls++] = c;
506                 op->o_ctrls[nctrls] = NULL;
507
508                 tag = ber_scanf( ber, "{m" /*}*/, &bv );
509                 c->ldctl_oid = bv.bv_val;
510
511                 if( tag == LBER_ERROR ) {
512                         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
513                                 0, 0, 0 );
514
515                         slap_free_ctrls( op, op->o_ctrls );
516                         op->o_ctrls = NULL;
517                         rs->sr_err = SLAPD_DISCONNECT;
518                         rs->sr_text = "decoding controls error";
519                         goto return_results;
520
521                 } else if( c->ldctl_oid == NULL ) {
522                         Debug( LDAP_DEBUG_TRACE,
523                                 "get_ctrls: conn %lu got emtpy OID.\n",
524                                 op->o_connid, 0, 0 );
525
526                         slap_free_ctrls( op, op->o_ctrls );
527                         op->o_ctrls = NULL;
528                         rs->sr_err = LDAP_PROTOCOL_ERROR;
529                         rs->sr_text = "OID field is empty";
530                         goto return_results;
531                 }
532
533                 tag = ber_peek_tag( ber, &len );
534
535                 if( tag == LBER_BOOLEAN ) {
536                         ber_int_t crit;
537                         tag = ber_scanf( ber, "b", &crit );
538
539                         if( tag == LBER_ERROR ) {
540                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
541                                         0, 0, 0 );
542                                 slap_free_ctrls( op, op->o_ctrls );
543                                 op->o_ctrls = NULL;
544                                 rs->sr_err = SLAPD_DISCONNECT;
545                                 rs->sr_text = "decoding controls error";
546                                 goto return_results;
547                         }
548
549                         c->ldctl_iscritical = (crit != 0);
550                         tag = ber_peek_tag( ber, &len );
551                 }
552
553                 if( tag == LBER_OCTETSTRING ) {
554                         tag = ber_scanf( ber, "m", &c->ldctl_value );
555
556                         if( tag == LBER_ERROR ) {
557                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
558                                         "%s (%scritical): get value failed.\n",
559                                         op->o_connid, c->ldctl_oid,
560                                         c->ldctl_iscritical ? "" : "non" );
561                                 slap_free_ctrls( op, op->o_ctrls );
562                                 op->o_ctrls = NULL;
563                                 rs->sr_err = SLAPD_DISCONNECT;
564                                 rs->sr_text = "decoding controls error";
565                                 goto return_results;
566                         }
567                 }
568
569                 Debug( LDAP_DEBUG_TRACE,
570                         "=> get_ctrls: oid=\"%s\" (%scritical)\n",
571                         c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
572
573                 sc = find_ctrl( c->ldctl_oid );
574                 if( sc != NULL ) {
575                         /* recognized control */
576                         slap_mask_t tagmask;
577                         switch( op->o_tag ) {
578                         case LDAP_REQ_ADD:
579                                 tagmask = SLAP_CTRL_ADD;
580                                 break;
581                         case LDAP_REQ_BIND:
582                                 tagmask = SLAP_CTRL_BIND;
583                                 break;
584                         case LDAP_REQ_COMPARE:
585                                 tagmask = SLAP_CTRL_COMPARE;
586                                 break;
587                         case LDAP_REQ_DELETE:
588                                 tagmask = SLAP_CTRL_DELETE;
589                                 break;
590                         case LDAP_REQ_MODIFY:
591                                 tagmask = SLAP_CTRL_MODIFY;
592                                 break;
593                         case LDAP_REQ_RENAME:
594                                 tagmask = SLAP_CTRL_RENAME;
595                                 break;
596                         case LDAP_REQ_SEARCH:
597                                 tagmask = SLAP_CTRL_SEARCH;
598                                 break;
599                         case LDAP_REQ_UNBIND:
600                                 tagmask = SLAP_CTRL_UNBIND;
601                                 break;
602                         case LDAP_REQ_ABANDON:
603                                 tagmask = SLAP_CTRL_ABANDON;
604                                 break;
605                         case LDAP_REQ_EXTENDED:
606                                 tagmask=~0L;
607                                 assert( op->ore_reqoid.bv_val != NULL );
608                                 if( sc->sc_extendedops != NULL ) {
609                                         int i;
610                                         for( i=0; sc->sc_extendedops[i] != NULL; i++ ) {
611                                                 if( strcmp( op->ore_reqoid.bv_val,
612                                                         sc->sc_extendedops[i] ) == 0 )
613                                                 {
614                                                         tagmask=0L;
615                                                         break;
616                                                 }
617                                         }
618                                 }
619                                 break;
620                         default:
621                                 rs->sr_err = LDAP_OTHER;
622                                 rs->sr_text = "controls internal error";
623                                 goto return_results;
624                         }
625
626                         if (( sc->sc_mask & tagmask ) == tagmask ) {
627                                 /* available extension */
628                                 int     rc;
629
630                                 if( !sc->sc_parse ) {
631                                         rs->sr_err = LDAP_OTHER;
632                                         rs->sr_text = "not yet implemented";
633                                         goto return_results;
634                                 }
635
636                                 rc = sc->sc_parse( op, rs, c );
637                                 if ( rc ) {
638                                         assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
639                                         rs->sr_err = rc;
640                                         goto return_results;
641                                 }
642
643 #ifdef SLAP_CONTROL_AVAILABILITY_KLUDGE
644                                 /* backend_check_controls() kludge */
645                                 if ( sc->sc_mask & SLAP_CTRL_GLOBAL ) {
646                                         /* KLUDGE: disable backend_control() check */
647                                         c->ldctl_iscritical = 0;
648
649                                 } else if ( tagmask == SLAP_CTRL_SEARCH &&
650                                         sc->sc_mask & SLAP_CTRL_GLOBAL_SEARCH )
651                                 {
652                                         /* KLUDGE: disable backend_control() check */
653                                         c->ldctl_iscritical = 0;
654
655                                 } else {
656                                         /* KLUDGE: enable backend_control() check */
657                                         c->ldctl_iscritical = 1;
658                                 }
659 #endif
660
661                         } else if( c->ldctl_iscritical ) {
662                                 /* unavailable CRITICAL control */
663                                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
664                                 rs->sr_text = "critical extension is unavailable";
665                                 goto return_results;
666                         }
667
668                 } else if( c->ldctl_iscritical ) {
669                         /* unrecognized CRITICAL control */
670                         rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
671                         rs->sr_text = "critical extension is not recognized";
672                         goto return_results;
673                 }
674 next_ctrl:;
675         }
676
677 return_results:
678         Debug( LDAP_DEBUG_TRACE,
679                 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
680                 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
681
682         if( sendres && rs->sr_err != LDAP_SUCCESS ) {
683                 if( rs->sr_err == SLAPD_DISCONNECT ) {
684                         rs->sr_err = LDAP_PROTOCOL_ERROR;
685                         send_ldap_disconnect( op, rs );
686                         rs->sr_err = SLAPD_DISCONNECT;
687                 } else {
688                         send_ldap_result( op, rs );
689                 }
690         }
691
692         return rs->sr_err;
693 }
694
695 static int parseModifyIncrement (
696         Operation *op,
697         SlapReply *rs,
698         LDAPControl *ctrl )
699 {
700 #if 0
701         if ( op->o_modifyIncrement != SLAP_CONTROL_NONE ) {
702                 rs->sr_text = "modifyIncrement control specified multiple times";
703                 return LDAP_PROTOCOL_ERROR;
704         }
705 #endif
706
707         if ( ctrl->ldctl_value.bv_len ) {
708                 rs->sr_text = "modifyIncrement control value not empty";
709                 return LDAP_PROTOCOL_ERROR;
710         }
711
712 #if 0
713         op->o_modifyIncrement = ctrl->ldctl_iscritical
714                 ? SLAP_CONTROL_CRITICAL
715                 : SLAP_CONTROL_NONCRITICAL;
716 #endif
717
718         return LDAP_SUCCESS;
719 }
720
721 static int parseManageDSAit (
722         Operation *op,
723         SlapReply *rs,
724         LDAPControl *ctrl )
725 {
726         if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
727                 rs->sr_text = "manageDSAit control specified multiple times";
728                 return LDAP_PROTOCOL_ERROR;
729         }
730
731         if ( ctrl->ldctl_value.bv_len ) {
732                 rs->sr_text = "manageDSAit control value not empty";
733                 return LDAP_PROTOCOL_ERROR;
734         }
735
736         op->o_managedsait = ctrl->ldctl_iscritical
737                 ? SLAP_CONTROL_CRITICAL
738                 : SLAP_CONTROL_NONCRITICAL;
739
740         return LDAP_SUCCESS;
741 }
742
743 static int parseProxyAuthz (
744         Operation *op,
745         SlapReply *rs,
746         LDAPControl *ctrl )
747 {
748         int             rc;
749         struct berval   dn = BER_BVNULL;
750
751         if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
752                 rs->sr_text = "proxy authorization control specified multiple times";
753                 return LDAP_PROTOCOL_ERROR;
754         }
755
756         op->o_proxy_authz = ctrl->ldctl_iscritical
757                 ? SLAP_CONTROL_CRITICAL
758                 : SLAP_CONTROL_NONCRITICAL;
759
760         Debug( LDAP_DEBUG_ARGS,
761                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
762                 op->o_connid,
763                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
764                 0 );
765
766         if( ctrl->ldctl_value.bv_len == 0 ) {
767                 Debug( LDAP_DEBUG_TRACE,
768                         "parseProxyAuthz: conn=%lu anonymous\n", 
769                         op->o_connid, 0, 0 );
770
771                 /* anonymous */
772                 free( op->o_dn.bv_val );
773                 op->o_dn.bv_len = 0;
774                 op->o_dn.bv_val = ch_strdup( "" );
775
776                 free( op->o_ndn.bv_val );
777                 op->o_ndn.bv_len = 0;
778                 op->o_ndn.bv_val = ch_strdup( "" );
779
780                 return LDAP_SUCCESS;
781         }
782
783         rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
784                         NULL, &dn, SLAP_GETDN_AUTHZID );
785
786         /* FIXME: empty DN in proxyAuthz control should be legal... */
787         if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
788                 if ( dn.bv_val ) {
789                         ch_free( dn.bv_val );
790                 }
791                 rs->sr_text = "authzId mapping failed";
792                 return LDAP_PROXY_AUTHZ_FAILURE;
793         }
794
795         Debug( LDAP_DEBUG_TRACE,
796                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
797                 op->o_connid,
798                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
799
800         rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
801
802         if( rc ) {
803                 ch_free( dn.bv_val );
804                 rs->sr_text = "not authorized to assume identity";
805                 return LDAP_PROXY_AUTHZ_FAILURE;
806         }
807
808         ch_free( op->o_dn.bv_val );
809         ch_free( op->o_ndn.bv_val );
810
811         op->o_dn.bv_val = NULL;
812         op->o_ndn = dn;
813
814         Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
815             op->o_log_prefix, dn.bv_val, 0, 0, 0 );
816
817         /*
818          * NOTE: since slap_sasl_getdn() returns a normalized dn,
819          * from now on op->o_dn is normalized
820          */
821         ber_dupbv( &op->o_dn, &dn );
822
823         return LDAP_SUCCESS;
824 }
825
826 static int parseNoOp (
827         Operation *op,
828         SlapReply *rs,
829         LDAPControl *ctrl )
830 {
831         if ( op->o_noop != SLAP_CONTROL_NONE ) {
832                 rs->sr_text = "noop control specified multiple times";
833                 return LDAP_PROTOCOL_ERROR;
834         }
835
836         if ( ctrl->ldctl_value.bv_len ) {
837                 rs->sr_text = "noop control value not empty";
838                 return LDAP_PROTOCOL_ERROR;
839         }
840
841         op->o_noop = ctrl->ldctl_iscritical
842                 ? SLAP_CONTROL_CRITICAL
843                 : SLAP_CONTROL_NONCRITICAL;
844
845         return LDAP_SUCCESS;
846 }
847
848 static int parsePagedResults (
849         Operation *op,
850         SlapReply *rs,
851         LDAPControl *ctrl )
852 {
853         int             rc = LDAP_SUCCESS;
854         ber_tag_t       tag;
855         ber_int_t       size;
856         BerElement      *ber;
857         struct berval   cookie = BER_BVNULL;
858         PagedResultsState       *ps;
859
860         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
861                 rs->sr_text = "paged results control specified multiple times";
862                 return LDAP_PROTOCOL_ERROR;
863         }
864
865         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
866                 rs->sr_text = "paged results control value is empty (or absent)";
867                 return LDAP_PROTOCOL_ERROR;
868         }
869
870         /* Parse the control value
871          *      realSearchControlValue ::= SEQUENCE {
872          *              size    INTEGER (0..maxInt),
873          *                              -- requested page size from client
874          *                              -- result set size estimate from server
875          *              cookie  OCTET STRING
876          * }
877          */
878         ber = ber_init( &ctrl->ldctl_value );
879         if ( ber == NULL ) {
880                 rs->sr_text = "internal error";
881                 return LDAP_OTHER;
882         }
883
884         tag = ber_scanf( ber, "{im}", &size, &cookie );
885
886         if ( tag == LBER_ERROR ) {
887                 rs->sr_text = "paged results control could not be decoded";
888                 rc = LDAP_PROTOCOL_ERROR;
889                 goto done;
890         }
891
892         if ( size < 0 ) {
893                 rs->sr_text = "paged results control size invalid";
894                 rc = LDAP_PROTOCOL_ERROR;
895                 goto done;
896         }
897
898 #if 0
899         /* defer cookie decoding/checks to backend... */
900         if ( cookie.bv_len ) {
901                 PagedResultsCookie reqcookie;
902                 if( cookie.bv_len != sizeof( reqcookie ) ) {
903                         /* bad cookie */
904                         rs->sr_text = "paged results cookie is invalid";
905                         rc = LDAP_PROTOCOL_ERROR;
906                         goto done;
907                 }
908
909                 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
910
911                 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
912                         /* bad cookie */
913                         rs->sr_text = "paged results cookie is invalid";
914                         rc = LDAP_PROTOCOL_ERROR;
915                         goto done;
916
917                 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
918                         rs->sr_text = "paged results cookie is invalid or old";
919                         rc = LDAP_UNWILLING_TO_PERFORM;
920                         goto done;
921                 }
922
923         } else {
924                 /* Initial request.  Initialize state. */
925 #if 0
926                 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
927                         /* There's another pagedResults control on the
928                          * same connection; reject new pagedResults controls 
929                          * (allowed by RFC2696) */
930                         rs->sr_text = "paged results cookie unavailable; try later";
931                         rc = LDAP_UNWILLING_TO_PERFORM;
932                         goto done;
933                 }
934 #endif
935                 op->o_pagedresults_state.ps_cookie = 0;
936                 op->o_pagedresults_state.ps_count = 0;
937         }
938 #endif
939
940         ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
941         *ps = op->o_conn->c_pagedresults_state;
942         ps->ps_size = size;
943         op->o_pagedresults_state = ps;
944
945         /* NOTE: according to RFC 2696 3.:
946
947     If the page size is greater than or equal to the sizeLimit value, the
948     server should ignore the control as the request can be satisfied in a
949     single page.
950          
951          * NOTE: this assumes that the op->ors_slimit be set
952          * before the controls are parsed.     
953          */
954                 
955         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
956                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
957
958         } else if ( ctrl->ldctl_iscritical ) {
959                 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
960
961         } else {
962                 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
963         }
964
965 done:;
966         (void)ber_free( ber, 1 );
967         return rc;
968 }
969
970 static int parseAssert (
971         Operation *op,
972         SlapReply *rs,
973         LDAPControl *ctrl )
974 {
975         BerElement      *ber;
976         struct berval   fstr = BER_BVNULL;
977         const char *err_msg = "";
978
979         if ( op->o_assert != SLAP_CONTROL_NONE ) {
980                 rs->sr_text = "assert control specified multiple times";
981                 return LDAP_PROTOCOL_ERROR;
982         }
983
984         if ( ctrl->ldctl_value.bv_len == 0 ) {
985                 rs->sr_text = "assert control value is empty (or absent)";
986                 return LDAP_PROTOCOL_ERROR;
987         }
988
989         ber = ber_init( &(ctrl->ldctl_value) );
990         if (ber == NULL) {
991                 rs->sr_text = "assert control: internal error";
992                 return LDAP_OTHER;
993         }
994         
995         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
996
997         if( rs->sr_err != LDAP_SUCCESS ) {
998                 if( rs->sr_err == SLAPD_DISCONNECT ) {
999                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1000                         send_ldap_disconnect( op, rs );
1001                         rs->sr_err = SLAPD_DISCONNECT;
1002                 } else {
1003                         send_ldap_result( op, rs );
1004                 }
1005                 if( op->o_assertion != NULL ) {
1006                         filter_free_x( op, op->o_assertion );
1007                 }
1008                 return rs->sr_err;
1009         }
1010
1011 #ifdef LDAP_DEBUG
1012         filter2bv_x( op, op->o_assertion, &fstr );
1013
1014         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1015                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1016         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1017 #endif
1018
1019         op->o_assert = ctrl->ldctl_iscritical
1020                 ? SLAP_CONTROL_CRITICAL
1021                 : SLAP_CONTROL_NONCRITICAL;
1022
1023         rs->sr_err = LDAP_SUCCESS;
1024         return LDAP_SUCCESS;
1025 }
1026
1027 static int parsePreRead (
1028         Operation *op,
1029         SlapReply *rs,
1030         LDAPControl *ctrl )
1031 {
1032         ber_len_t siz, off, i;
1033         AttributeName *an = NULL;
1034         BerElement      *ber;
1035
1036         if ( op->o_preread != SLAP_CONTROL_NONE ) {
1037                 rs->sr_text = "preread control specified multiple times";
1038                 return LDAP_PROTOCOL_ERROR;
1039         }
1040
1041         if ( ctrl->ldctl_value.bv_len == 0 ) {
1042                 rs->sr_text = "preread control value is empty (or absent)";
1043                 return LDAP_PROTOCOL_ERROR;
1044         }
1045
1046         ber = ber_init( &(ctrl->ldctl_value) );
1047         if (ber == NULL) {
1048                 rs->sr_text = "preread control: internal error";
1049                 return LDAP_OTHER;
1050         }
1051
1052         siz = sizeof( AttributeName );
1053         off = offsetof( AttributeName, an_name );
1054         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1055                 rs->sr_text = "preread control: decoding error";
1056                 return LDAP_PROTOCOL_ERROR;
1057         }
1058
1059         for( i=0; i<siz; i++ ) {
1060                 int             rc = LDAP_SUCCESS;
1061                 const char      *dummy = NULL;
1062
1063                 an[i].an_desc = NULL;
1064                 an[i].an_oc = NULL;
1065                 an[i].an_oc_exclude = 0;
1066                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1067                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1068                         rs->sr_text = dummy
1069                                 ? dummy
1070                                 : "postread control: unknown attributeType";
1071                         return rc;
1072                 }
1073         }
1074
1075         op->o_preread = ctrl->ldctl_iscritical
1076                 ? SLAP_CONTROL_CRITICAL
1077                 : SLAP_CONTROL_NONCRITICAL;
1078
1079         op->o_preread_attrs = an;
1080
1081         rs->sr_err = LDAP_SUCCESS;
1082         return LDAP_SUCCESS;
1083 }
1084
1085 static int parsePostRead (
1086         Operation *op,
1087         SlapReply *rs,
1088         LDAPControl *ctrl )
1089 {
1090         ber_len_t siz, off, i;
1091         AttributeName *an = NULL;
1092         BerElement      *ber;
1093
1094         if ( op->o_postread != SLAP_CONTROL_NONE ) {
1095                 rs->sr_text = "postread control specified multiple times";
1096                 return LDAP_PROTOCOL_ERROR;
1097         }
1098
1099         if ( ctrl->ldctl_value.bv_len == 0 ) {
1100                 rs->sr_text = "postread control value is empty (or absent)";
1101                 return LDAP_PROTOCOL_ERROR;
1102         }
1103
1104         ber = ber_init( &(ctrl->ldctl_value) );
1105         if (ber == NULL) {
1106                 rs->sr_text = "postread control: internal error";
1107                 return LDAP_OTHER;
1108         }
1109
1110         siz = sizeof( AttributeName );
1111         off = offsetof( AttributeName, an_name );
1112         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1113                 rs->sr_text = "postread control: decoding error";
1114                 return LDAP_PROTOCOL_ERROR;
1115         }
1116
1117         for( i=0; i<siz; i++ ) {
1118                 int             rc = LDAP_SUCCESS;
1119                 const char      *dummy = NULL;
1120
1121                 an[i].an_desc = NULL;
1122                 an[i].an_oc = NULL;
1123                 an[i].an_oc_exclude = 0;
1124                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1125                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1126                         rs->sr_text = dummy
1127                                 ? dummy
1128                                 : "postread control: unknown attributeType";
1129                         return rc;
1130                 }
1131         }
1132
1133         op->o_postread = ctrl->ldctl_iscritical
1134                 ? SLAP_CONTROL_CRITICAL
1135                 : SLAP_CONTROL_NONCRITICAL;
1136
1137         op->o_postread_attrs = an;
1138
1139         rs->sr_err = LDAP_SUCCESS;
1140         return LDAP_SUCCESS;
1141 }
1142
1143 int parseValuesReturnFilter (
1144         Operation *op,
1145         SlapReply *rs,
1146         LDAPControl *ctrl )
1147 {
1148         BerElement      *ber;
1149         struct berval   fstr = BER_BVNULL;
1150         const char *err_msg = "";
1151
1152         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1153                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1154                 return LDAP_PROTOCOL_ERROR;
1155         }
1156
1157         if ( ctrl->ldctl_value.bv_len == 0 ) {
1158                 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1159                 return LDAP_PROTOCOL_ERROR;
1160         }
1161
1162         ber = ber_init( &(ctrl->ldctl_value) );
1163         if (ber == NULL) {
1164                 rs->sr_text = "internal error";
1165                 return LDAP_OTHER;
1166         }
1167         
1168         rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1169
1170         if( rs->sr_err != LDAP_SUCCESS ) {
1171                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1172                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1173                         send_ldap_disconnect( op, rs );
1174                         rs->sr_err = SLAPD_DISCONNECT;
1175                 } else {
1176                         send_ldap_result( op, rs );
1177                 }
1178                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1179         }
1180 #ifdef LDAP_DEBUG
1181         else {
1182                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1183         }
1184
1185         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1186                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1187         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1188 #endif
1189
1190         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1191                 ? SLAP_CONTROL_CRITICAL
1192                 : SLAP_CONTROL_NONCRITICAL;
1193
1194         rs->sr_err = LDAP_SUCCESS;
1195         return LDAP_SUCCESS;
1196 }
1197
1198 #ifdef LDAP_CONTROL_SUBENTRIES
1199 static int parseSubentries (
1200         Operation *op,
1201         SlapReply *rs,
1202         LDAPControl *ctrl )
1203 {
1204         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1205                 rs->sr_text = "subentries control specified multiple times";
1206                 return LDAP_PROTOCOL_ERROR;
1207         }
1208
1209         /* FIXME: should use BER library */
1210         if( ( ctrl->ldctl_value.bv_len != 3 )
1211                 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1212                 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1213         {
1214                 rs->sr_text = "subentries control value encoding is bogus";
1215                 return LDAP_PROTOCOL_ERROR;
1216         }
1217
1218         op->o_subentries = ctrl->ldctl_iscritical
1219                 ? SLAP_CONTROL_CRITICAL
1220                 : SLAP_CONTROL_NONCRITICAL;
1221
1222         if ( (void *)(ctrl->ldctl_value.bv_val[2] != 0x00))
1223                 set_subentries_visibility( op );
1224
1225         return LDAP_SUCCESS;
1226 }
1227 #endif
1228
1229 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1230 static int parsePermissiveModify (
1231         Operation *op,
1232         SlapReply *rs,
1233         LDAPControl *ctrl )
1234 {
1235         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1236                 rs->sr_text = "permissiveModify control specified multiple times";
1237                 return LDAP_PROTOCOL_ERROR;
1238         }
1239
1240         if ( ctrl->ldctl_value.bv_len ) {
1241                 rs->sr_text = "permissiveModify control value not empty";
1242                 return LDAP_PROTOCOL_ERROR;
1243         }
1244
1245         op->o_permissive_modify = ctrl->ldctl_iscritical
1246                 ? SLAP_CONTROL_CRITICAL
1247                 : SLAP_CONTROL_NONCRITICAL;
1248
1249         return LDAP_SUCCESS;
1250 }
1251 #endif
1252
1253 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1254 static int parseDomainScope (
1255         Operation *op,
1256         SlapReply *rs,
1257         LDAPControl *ctrl )
1258 {
1259         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1260                 rs->sr_text = "domainScope control specified multiple times";
1261                 return LDAP_PROTOCOL_ERROR;
1262         }
1263
1264         if ( ctrl->ldctl_value.bv_len ) {
1265                 rs->sr_text = "domainScope control value not empty";
1266                 return LDAP_PROTOCOL_ERROR;
1267         }
1268
1269         op->o_domain_scope = ctrl->ldctl_iscritical
1270                 ? SLAP_CONTROL_CRITICAL
1271                 : SLAP_CONTROL_NONCRITICAL;
1272
1273         return LDAP_SUCCESS;
1274 }
1275 #endif
1276
1277 #ifdef LDAP_CONTROL_X_TREE_DELETE
1278 static int parseTreeDelete (
1279         Operation *op,
1280         SlapReply *rs,
1281         LDAPControl *ctrl )
1282 {
1283         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1284                 rs->sr_text = "treeDelete control specified multiple times";
1285                 return LDAP_PROTOCOL_ERROR;
1286         }
1287
1288         if ( ctrl->ldctl_value.bv_len ) {
1289                 rs->sr_text = "treeDelete control value not empty";
1290                 return LDAP_PROTOCOL_ERROR;
1291         }
1292
1293         op->o_tree_delete = ctrl->ldctl_iscritical
1294                 ? SLAP_CONTROL_CRITICAL
1295                 : SLAP_CONTROL_NONCRITICAL;
1296
1297         return LDAP_SUCCESS;
1298 }
1299 #endif
1300
1301 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1302 static int parseSearchOptions (
1303         Operation *op,
1304         SlapReply *rs,
1305         LDAPControl *ctrl )
1306 {
1307         BerElement *ber;
1308         ber_int_t search_flags;
1309
1310         if ( ctrl->ldctl_value.bv_len == 0 ) {
1311                 rs->sr_text = "searchOptions control value not empty";
1312                 return LDAP_PROTOCOL_ERROR;
1313         }
1314
1315         ber = ber_init( &ctrl->ldctl_value );
1316         if( ber == NULL ) {
1317                 rs->sr_text = "internal error";
1318                 return LDAP_OTHER;
1319         }
1320
1321         if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1322                 rs->sr_text = "searchOptions control decoding error";
1323                 return LDAP_PROTOCOL_ERROR;
1324         }
1325
1326         (void) ber_free( ber, 1 );
1327
1328         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1329                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1330                         rs->sr_text = "searchOptions control specified multiple times "
1331                                 "or with domainScope control";
1332                         return LDAP_PROTOCOL_ERROR;
1333                 }
1334
1335                 op->o_domain_scope = ctrl->ldctl_iscritical
1336                         ? SLAP_CONTROL_CRITICAL
1337                         : SLAP_CONTROL_NONCRITICAL;
1338         }
1339
1340         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1341                 /* Other search flags not recognised so far,
1342                  * including:
1343                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOM
1344                  */
1345                 rs->sr_text = "searchOptions contained unrecongized flag";
1346                 return LDAP_UNWILLING_TO_PERFORM;
1347         }
1348
1349         return LDAP_SUCCESS;
1350 }
1351 #endif
1352