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