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