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