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