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