]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
ITS#3847 silence warnings
[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 #ifdef SLAP_CONTROL_X_TREE_DELETE
45 static SLAP_CTRL_PARSE_FN parseTreeDelete;
46 #endif
47 static SLAP_CTRL_PARSE_FN parseSearchOptions;
48 static SLAP_CTRL_PARSE_FN parseSubentries;
49
50 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
51
52 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
53 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
54
55 struct slap_control_ids slap_cids;
56
57 struct slap_control {
58         /* Control OID */
59         char *sc_oid;
60
61         /* The controlID for this control */
62         int sc_cid;
63
64         /* Operations supported by control */
65         slap_mask_t sc_mask;
66
67         /* Extended operations supported by control */
68         char **sc_extendedops;
69
70         /* Control parsing callback */
71         SLAP_CTRL_PARSE_FN *sc_parse;
72
73         LDAP_SLIST_ENTRY(slap_control) sc_next;
74 };
75
76 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
77         = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
78
79 /*
80  * all known request control OIDs should be added to this list
81  */
82 /*
83  * NOTE: initialize num_known_controls to 1 so that cid = 0 always
84  * addresses an undefined control; this allows to safely test for 
85  * well known controls even if they are not registered, e.g. if 
86  * they get moved to modules.  An example is sc_LDAPsync, which 
87  * is implemented in the syncprov overlay and thus, if configured 
88  * as dynamic module, may not be registered.  One side effect is that 
89  * slap_known_controls[0] == NULL, so it should always be used 
90  * starting from 1.
91  * FIXME: should we define the "undefined control" oid?
92  */
93 char *slap_known_controls[SLAP_MAX_CIDS+1];
94 static int num_known_controls = 1;
95
96 static char *proxy_authz_extops[] = {
97         LDAP_EXOP_MODIFY_PASSWD,
98         LDAP_EXOP_X_WHO_AM_I,
99         NULL
100 };
101
102 static struct slap_control control_defs[] = {
103         {  LDAP_CONTROL_ASSERT,
104                 (int)offsetof(struct slap_control_ids, sc_assert),
105                 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
106                 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
107         { LDAP_CONTROL_PRE_READ,
108                 (int)offsetof(struct slap_control_ids, sc_preRead),
109                 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
110                 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
111         { LDAP_CONTROL_POST_READ,
112                 (int)offsetof(struct slap_control_ids, sc_postRead),
113                 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
114                 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
115         { LDAP_CONTROL_VALUESRETURNFILTER,
116                 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
117                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
118                 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
119         { LDAP_CONTROL_PAGEDRESULTS,
120                 (int)offsetof(struct slap_control_ids, sc_pagedResults),
121                 SLAP_CTRL_SEARCH, NULL,
122                 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
123 #ifdef LDAP_DEVEL
124         { LDAP_CONTROL_SORTREQUEST,
125                 (int)offsetof(struct slap_control_ids, sc_sortedResults),
126                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE, NULL,
127                 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
128 #endif
129 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
130         { LDAP_CONTROL_X_DOMAIN_SCOPE,
131                 (int)offsetof(struct slap_control_ids, sc_domainScope),
132                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
133                 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
134 #endif
135 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
136         { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
137                 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
138                 SLAP_CTRL_MODIFY, NULL,
139                 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
140 #endif
141 #ifdef SLAP_CONTROL_X_TREE_DELETE
142         { LDAP_CONTROL_X_TREE_DELETE,
143                 (int)offsetof(struct slap_control_ids, sc_treeDelete),
144                 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE, NULL,
145                 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
146 #endif
147 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
148         { LDAP_CONTROL_X_SEARCH_OPTIONS,
149                 (int)offsetof(struct slap_control_ids, sc_searchOptions),
150                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH, NULL,
151                 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
152 #endif
153 #ifdef LDAP_CONTROL_SUBENTRIES
154         { LDAP_CONTROL_SUBENTRIES,
155                 (int)offsetof(struct slap_control_ids, sc_subentries),
156                 SLAP_CTRL_SEARCH, NULL,
157                 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
158 #endif
159         { LDAP_CONTROL_NOOP,
160                 (int)offsetof(struct slap_control_ids, sc_noOp),
161                 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
162                 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
163 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
164         { LDAP_CONTROL_MODIFY_INCREMENT,
165                 (int)offsetof(struct slap_control_ids, sc_modifyIncrement),
166                 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
167                 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
168 #endif
169 #ifdef LDAP_DEVEL
170         { LDAP_CONTROL_MANAGEDIT,
171                 (int)offsetof(struct slap_control_ids, sc_manageDIT),
172                 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE, NULL,
173                 parseManageDIT, LDAP_SLIST_ENTRY_INITIALIZER(next) },
174 #endif
175         { LDAP_CONTROL_MANAGEDSAIT,
176                 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
177                 SLAP_CTRL_ACCESS, NULL,
178                 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
179         { LDAP_CONTROL_PROXY_AUTHZ,
180                 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
181                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS, proxy_authz_extops,
182                 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
183         { NULL, 0, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
184 };
185
186 /*
187  * Register a supported control.
188  *
189  * This can be called by an OpenLDAP plugin or, indirectly, by a
190  * SLAPI plugin calling slapi_register_supported_control().
191  */
192 int
193 register_supported_control(const char *controloid,
194         slap_mask_t controlmask,
195         char **controlexops,
196         SLAP_CTRL_PARSE_FN *controlparsefn,
197         int *controlcid)
198 {
199         struct slap_control *sc;
200         int i;
201
202         if ( num_known_controls >= SLAP_MAX_CIDS ) {
203                 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
204                         " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
205                 SLAP_MAX_CIDS, 0, 0 );
206                 return LDAP_OTHER;
207         }
208
209         if ( controloid == NULL ) return LDAP_PARAM_ERROR;
210
211         /* sanity check - should never happen */
212         for ( i = 0; slap_known_controls[ i ]; i++ ) {
213                 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
214                         Debug( LDAP_DEBUG_ANY,
215                                 "Control %s already registered.\n",
216                                 controloid, 0, 0 );
217                         return LDAP_PARAM_ERROR;
218                 }
219         }
220
221         sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
222         if ( sc == NULL ) return LDAP_NO_MEMORY;
223
224         sc->sc_oid = ch_strdup( controloid );
225         sc->sc_mask = controlmask;
226         if ( controlexops != NULL ) {
227                 sc->sc_extendedops = ldap_charray_dup( controlexops );
228                 if ( sc->sc_extendedops == NULL ) {
229                         ch_free( sc );
230                         return LDAP_NO_MEMORY;
231                 }
232         } else {
233                 sc->sc_extendedops = NULL;
234         }
235         sc->sc_parse = controlparsefn;
236
237         if ( controlcid ) *controlcid = num_known_controls;
238         sc->sc_cid = num_known_controls;
239
240         /* Update slap_known_controls, too. */
241         slap_known_controls[num_known_controls-1] = sc->sc_oid;
242         slap_known_controls[num_known_controls++] = NULL;
243
244         LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
245         LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
246         return LDAP_SUCCESS;
247 }
248
249 /*
250  * One-time initialization of internal controls.
251  */
252 int
253 slap_controls_init( void )
254 {
255         int i, rc;
256
257         rc = LDAP_SUCCESS;
258
259         for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
260                 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
261                 rc = register_supported_control( control_defs[i].sc_oid,
262                         control_defs[i].sc_mask, control_defs[i].sc_extendedops,
263                         control_defs[i].sc_parse, cid );
264                 if ( rc != LDAP_SUCCESS ) break;
265         }
266
267         return rc;
268 }
269
270 /*
271  * Free memory associated with list of supported controls.
272  */
273 void
274 controls_destroy( void )
275 {
276         struct slap_control *sc;
277
278         while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
279                 sc = LDAP_SLIST_FIRST(&controls_list);
280                 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
281
282                 ch_free( sc->sc_oid );
283                 if ( sc->sc_extendedops != NULL ) {
284                         ldap_charray_free( sc->sc_extendedops );
285                 }
286                 ch_free( sc );
287         }
288 }
289
290 /*
291  * Format the supportedControl attribute of the root DSE,
292  * detailing which controls are supported by the directory
293  * server.
294  */
295 int
296 controls_root_dse_info( Entry *e )
297 {
298         AttributeDescription *ad_supportedControl
299                 = slap_schema.si_ad_supportedControl;
300         struct berval vals[2];
301         struct slap_control *sc;
302
303         vals[1].bv_val = NULL;
304         vals[1].bv_len = 0;
305
306         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
307                 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
308
309                 vals[0].bv_val = sc->sc_oid;
310                 vals[0].bv_len = strlen( sc->sc_oid );
311
312                 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
313                         return -1;
314                 }
315         }
316
317         return 0;
318 }
319
320 /*
321  * Return a list of OIDs and operation masks for supported
322  * controls. Used by SLAPI.
323  */
324 int
325 get_supported_controls(char ***ctrloidsp,
326         slap_mask_t **ctrlmasks)
327 {
328         int n;
329         char **oids;
330         slap_mask_t *masks;
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
1028         if ( op->o_assert != SLAP_CONTROL_NONE ) {
1029                 rs->sr_text = "assert control specified multiple times";
1030                 return LDAP_PROTOCOL_ERROR;
1031         }
1032
1033         if ( ctrl->ldctl_value.bv_len == 0 ) {
1034                 rs->sr_text = "assert control value is empty (or absent)";
1035                 return LDAP_PROTOCOL_ERROR;
1036         }
1037
1038         ber = ber_init( &(ctrl->ldctl_value) );
1039         if (ber == NULL) {
1040                 rs->sr_text = "assert control: internal error";
1041                 return LDAP_OTHER;
1042         }
1043         
1044         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion), &rs->sr_text);
1045
1046         if( rs->sr_err != LDAP_SUCCESS ) {
1047                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1048                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1049                         send_ldap_disconnect( op, rs );
1050                         rs->sr_err = SLAPD_DISCONNECT;
1051                 } else {
1052                         send_ldap_result( op, rs );
1053                 }
1054                 if( op->o_assertion != NULL ) {
1055                         filter_free_x( op, op->o_assertion );
1056                 }
1057                 return rs->sr_err;
1058         }
1059
1060 #ifdef LDAP_DEBUG
1061         filter2bv_x( op, op->o_assertion, &fstr );
1062
1063         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1064                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1065         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1066 #endif
1067
1068         op->o_assert = ctrl->ldctl_iscritical
1069                 ? SLAP_CONTROL_CRITICAL
1070                 : SLAP_CONTROL_NONCRITICAL;
1071
1072         rs->sr_err = LDAP_SUCCESS;
1073         return LDAP_SUCCESS;
1074 }
1075
1076 static int parsePreRead (
1077         Operation *op,
1078         SlapReply *rs,
1079         LDAPControl *ctrl )
1080 {
1081         ber_len_t siz, off, i;
1082         AttributeName *an = NULL;
1083         BerElement      *ber;
1084
1085         if ( op->o_preread != SLAP_CONTROL_NONE ) {
1086                 rs->sr_text = "preread control specified multiple times";
1087                 return LDAP_PROTOCOL_ERROR;
1088         }
1089
1090         if ( ctrl->ldctl_value.bv_len == 0 ) {
1091                 rs->sr_text = "preread control value is empty (or absent)";
1092                 return LDAP_PROTOCOL_ERROR;
1093         }
1094
1095         ber = ber_init( &(ctrl->ldctl_value) );
1096         if (ber == NULL) {
1097                 rs->sr_text = "preread control: internal error";
1098                 return LDAP_OTHER;
1099         }
1100
1101         siz = sizeof( AttributeName );
1102         off = offsetof( AttributeName, an_name );
1103         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1104                 rs->sr_text = "preread control: decoding error";
1105                 return LDAP_PROTOCOL_ERROR;
1106         }
1107
1108         for( i=0; i<siz; i++ ) {
1109                 int             rc = LDAP_SUCCESS;
1110                 const char      *dummy = NULL;
1111
1112                 an[i].an_desc = NULL;
1113                 an[i].an_oc = NULL;
1114                 an[i].an_oc_exclude = 0;
1115                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1116                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1117                         rs->sr_text = dummy
1118                                 ? dummy
1119                                 : "postread control: unknown attributeType";
1120                         return rc;
1121                 }
1122         }
1123
1124         op->o_preread = ctrl->ldctl_iscritical
1125                 ? SLAP_CONTROL_CRITICAL
1126                 : SLAP_CONTROL_NONCRITICAL;
1127
1128         op->o_preread_attrs = an;
1129
1130         rs->sr_err = LDAP_SUCCESS;
1131         return LDAP_SUCCESS;
1132 }
1133
1134 static int parsePostRead (
1135         Operation *op,
1136         SlapReply *rs,
1137         LDAPControl *ctrl )
1138 {
1139         ber_len_t siz, off, i;
1140         AttributeName *an = NULL;
1141         BerElement      *ber;
1142
1143         if ( op->o_postread != SLAP_CONTROL_NONE ) {
1144                 rs->sr_text = "postread control specified multiple times";
1145                 return LDAP_PROTOCOL_ERROR;
1146         }
1147
1148         if ( ctrl->ldctl_value.bv_len == 0 ) {
1149                 rs->sr_text = "postread control value is empty (or absent)";
1150                 return LDAP_PROTOCOL_ERROR;
1151         }
1152
1153         ber = ber_init( &(ctrl->ldctl_value) );
1154         if (ber == NULL) {
1155                 rs->sr_text = "postread control: internal error";
1156                 return LDAP_OTHER;
1157         }
1158
1159         siz = sizeof( AttributeName );
1160         off = offsetof( AttributeName, an_name );
1161         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1162                 rs->sr_text = "postread control: decoding error";
1163                 return LDAP_PROTOCOL_ERROR;
1164         }
1165
1166         for( i=0; i<siz; i++ ) {
1167                 int             rc = LDAP_SUCCESS;
1168                 const char      *dummy = NULL;
1169
1170                 an[i].an_desc = NULL;
1171                 an[i].an_oc = NULL;
1172                 an[i].an_oc_exclude = 0;
1173                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1174                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1175                         rs->sr_text = dummy
1176                                 ? dummy
1177                                 : "postread control: unknown attributeType";
1178                         return rc;
1179                 }
1180         }
1181
1182         op->o_postread = ctrl->ldctl_iscritical
1183                 ? SLAP_CONTROL_CRITICAL
1184                 : SLAP_CONTROL_NONCRITICAL;
1185
1186         op->o_postread_attrs = an;
1187
1188         rs->sr_err = LDAP_SUCCESS;
1189         return LDAP_SUCCESS;
1190 }
1191
1192 int parseValuesReturnFilter (
1193         Operation *op,
1194         SlapReply *rs,
1195         LDAPControl *ctrl )
1196 {
1197         BerElement      *ber;
1198         struct berval   fstr = BER_BVNULL;
1199
1200         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1201                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1202                 return LDAP_PROTOCOL_ERROR;
1203         }
1204
1205         if ( ctrl->ldctl_value.bv_len == 0 ) {
1206                 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1207                 return LDAP_PROTOCOL_ERROR;
1208         }
1209
1210         ber = ber_init( &(ctrl->ldctl_value) );
1211         if (ber == NULL) {
1212                 rs->sr_text = "internal error";
1213                 return LDAP_OTHER;
1214         }
1215         
1216         rs->sr_err = get_vrFilter( op, ber, (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1217
1218         if( rs->sr_err != LDAP_SUCCESS ) {
1219                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1220                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1221                         send_ldap_disconnect( op, rs );
1222                         rs->sr_err = SLAPD_DISCONNECT;
1223                 } else {
1224                         send_ldap_result( op, rs );
1225                 }
1226                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1227         }
1228 #ifdef LDAP_DEBUG
1229         else {
1230                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1231         }
1232
1233         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1234                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1235         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1236 #endif
1237
1238         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1239                 ? SLAP_CONTROL_CRITICAL
1240                 : SLAP_CONTROL_NONCRITICAL;
1241
1242         rs->sr_err = LDAP_SUCCESS;
1243         return LDAP_SUCCESS;
1244 }
1245
1246 #ifdef LDAP_CONTROL_SUBENTRIES
1247 static int parseSubentries (
1248         Operation *op,
1249         SlapReply *rs,
1250         LDAPControl *ctrl )
1251 {
1252         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1253                 rs->sr_text = "subentries control specified multiple times";
1254                 return LDAP_PROTOCOL_ERROR;
1255         }
1256
1257         /* FIXME: should use BER library */
1258         if( ( ctrl->ldctl_value.bv_len != 3 )
1259                 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1260                 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1261         {
1262                 rs->sr_text = "subentries control value encoding is bogus";
1263                 return LDAP_PROTOCOL_ERROR;
1264         }
1265
1266         op->o_subentries = ctrl->ldctl_iscritical
1267                 ? SLAP_CONTROL_CRITICAL
1268                 : SLAP_CONTROL_NONCRITICAL;
1269
1270         if (ctrl->ldctl_value.bv_val[2]) {
1271                 set_subentries_visibility( op );
1272         }
1273
1274         return LDAP_SUCCESS;
1275 }
1276 #endif
1277
1278 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1279 static int parsePermissiveModify (
1280         Operation *op,
1281         SlapReply *rs,
1282         LDAPControl *ctrl )
1283 {
1284         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1285                 rs->sr_text = "permissiveModify control specified multiple times";
1286                 return LDAP_PROTOCOL_ERROR;
1287         }
1288
1289         if ( ctrl->ldctl_value.bv_len ) {
1290                 rs->sr_text = "permissiveModify control value not empty";
1291                 return LDAP_PROTOCOL_ERROR;
1292         }
1293
1294         op->o_permissive_modify = ctrl->ldctl_iscritical
1295                 ? SLAP_CONTROL_CRITICAL
1296                 : SLAP_CONTROL_NONCRITICAL;
1297
1298         return LDAP_SUCCESS;
1299 }
1300 #endif
1301
1302 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1303 static int parseDomainScope (
1304         Operation *op,
1305         SlapReply *rs,
1306         LDAPControl *ctrl )
1307 {
1308         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1309                 rs->sr_text = "domainScope control specified multiple times";
1310                 return LDAP_PROTOCOL_ERROR;
1311         }
1312
1313         if ( ctrl->ldctl_value.bv_len ) {
1314                 rs->sr_text = "domainScope control value not empty";
1315                 return LDAP_PROTOCOL_ERROR;
1316         }
1317
1318         op->o_domain_scope = ctrl->ldctl_iscritical
1319                 ? SLAP_CONTROL_CRITICAL
1320                 : SLAP_CONTROL_NONCRITICAL;
1321
1322         return LDAP_SUCCESS;
1323 }
1324 #endif
1325
1326 #ifdef SLAP_CONTROL_X_TREE_DELETE
1327 static int parseTreeDelete (
1328         Operation *op,
1329         SlapReply *rs,
1330         LDAPControl *ctrl )
1331 {
1332         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1333                 rs->sr_text = "treeDelete control specified multiple times";
1334                 return LDAP_PROTOCOL_ERROR;
1335         }
1336
1337         if ( ctrl->ldctl_value.bv_len ) {
1338                 rs->sr_text = "treeDelete control value not empty";
1339                 return LDAP_PROTOCOL_ERROR;
1340         }
1341
1342         op->o_tree_delete = ctrl->ldctl_iscritical
1343                 ? SLAP_CONTROL_CRITICAL
1344                 : SLAP_CONTROL_NONCRITICAL;
1345
1346         return LDAP_SUCCESS;
1347 }
1348 #endif
1349
1350 #ifdef LDAP_CONTROL_X_SEARCH_OPTIONS
1351 static int parseSearchOptions (
1352         Operation *op,
1353         SlapReply *rs,
1354         LDAPControl *ctrl )
1355 {
1356         BerElement *ber;
1357         ber_int_t search_flags;
1358         ber_tag_t tag;
1359
1360         if ( ctrl->ldctl_value.bv_len == 0 ) {
1361                 rs->sr_text = "searchOptions control value not empty";
1362                 return LDAP_PROTOCOL_ERROR;
1363         }
1364
1365         ber = ber_init( &ctrl->ldctl_value );
1366         if( ber == NULL ) {
1367                 rs->sr_text = "internal error";
1368                 return LDAP_OTHER;
1369         }
1370
1371         if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1372                 rs->sr_text = "searchOptions control decoding error";
1373                 return LDAP_PROTOCOL_ERROR;
1374         }
1375
1376         (void) ber_free( ber, 1 );
1377
1378         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1379                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1380                         rs->sr_text = "searchOptions control specified multiple times "
1381                                 "or with domainScope control";
1382                         return LDAP_PROTOCOL_ERROR;
1383                 }
1384
1385                 op->o_domain_scope = ctrl->ldctl_iscritical
1386                         ? SLAP_CONTROL_CRITICAL
1387                         : SLAP_CONTROL_NONCRITICAL;
1388         }
1389
1390         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1391                 /* Other search flags not recognised so far,
1392                  * including:
1393                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOM
1394                  */
1395                 rs->sr_text = "searchOptions contained unrecongized flag";
1396                 return LDAP_UNWILLING_TO_PERFORM;
1397         }
1398
1399         return LDAP_SUCCESS;
1400 }
1401 #endif
1402