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