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