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