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