]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
minor cleanup
[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 {
52         /* Control OID */
53         char *sc_oid;
54
55         /* Operations supported by control */
56         slap_mask_t sc_mask;
57
58         /* Extended operations supported by control */
59         char **sc_extendedops;
60
61         /* Control parsing callback */
62         SLAP_CTRL_PARSE_FN *sc_parse;
63
64         LDAP_SLIST_ENTRY(slap_control) sc_next;
65 };
66
67 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
68         = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
69
70 /*
71  * all known request control OIDs should be added to this list
72  */
73 char **slap_known_controls = NULL;
74
75 static char *proxy_authz_extops[] = {
76         LDAP_EXOP_MODIFY_PASSWD,
77         LDAP_EXOP_X_WHO_AM_I,
78         NULL
79 };
80
81 static struct slap_control control_defs[] = {
82         { LDAP_CONTROL_ASSERT,
83                 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
84                 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
85         { LDAP_CONTROL_PRE_READ,
86                 SLAP_CTRL_HIDE|SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
87                 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
88         { LDAP_CONTROL_POST_READ,
89                 SLAP_CTRL_HIDE|SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME, NULL,
90                 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
91         { LDAP_CONTROL_VALUESRETURNFILTER,
92                 SLAP_CTRL_SEARCH, NULL,
93                 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
94         { LDAP_CONTROL_PAGEDRESULTS,
95                 SLAP_CTRL_SEARCH, NULL,
96                 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
97 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
98         { LDAP_CONTROL_X_DOMAIN_SCOPE,
99                 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
100                 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
101 #endif
102 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
103         { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
104                 SLAP_CTRL_MODIFY, NULL,
105                 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
106 #endif
107 #ifdef LDAP_CONTROL_X_TREE_DELETE
108         { LDAP_CONTROL_X_TREE_DELETE,
109                 SLAP_CTRL_DELETE, NULL,
110                 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
111 #endif
112 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
113         { LDAP_CONTORL_X_SEARCH_OPTIONS,
114                 SLAP_CTRL_FRONTEND|SLAP_CTRL_SEARCH, NULL,
115                 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
116 #endif
117 #ifdef LDAP_CONTROL_SUBENTRIES
118         { LDAP_CONTROL_SUBENTRIES,
119                 SLAP_CTRL_SEARCH, NULL,
120                 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
121 #endif
122         { LDAP_CONTROL_NOOP,
123                 SLAP_CTRL_HIDE|SLAP_CTRL_ACCESS, NULL,
124                 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
125         { LDAP_CONTROL_SYNC,
126                 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
127                 parseLDAPsync, LDAP_SLIST_ENTRY_INITIALIZER(next) },
128 #ifdef LDAP_CONTROL_MODIFY_INCREMENT
129         { LDAP_CONTROL_MODIFY_INCREMENT,
130                 SLAP_CTRL_HIDE|SLAP_CTRL_MODIFY, NULL,
131                 parseModifyIncrement, LDAP_SLIST_ENTRY_INITIALIZER(next) },
132 #endif
133         { LDAP_CONTROL_MANAGEDSAIT,
134                 SLAP_CTRL_ACCESS, NULL,
135                 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
136         { LDAP_CONTROL_PROXY_AUTHZ,
137                 SLAP_CTRL_FRONTEND|SLAP_CTRL_ACCESS, proxy_authz_extops,
138                 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
139         { NULL, 0, NULL, 0, LDAP_SLIST_ENTRY_INITIALIZER(next) }
140 };
141
142 /*
143  * Register a supported control.
144  *
145  * This can be called by an OpenLDAP plugin or, indirectly, by a
146  * SLAPI plugin calling slapi_register_supported_control().
147  */
148 int
149 register_supported_control(const char *controloid,
150         slap_mask_t controlmask,
151         char **controlexops,
152         SLAP_CTRL_PARSE_FN *controlparsefn)
153 {
154         struct slap_control *sc;
155         int i;
156
157         if ( controloid == NULL ) return LDAP_PARAM_ERROR;
158
159         sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
160         if ( sc == NULL ) return LDAP_NO_MEMORY;
161
162         sc->sc_oid = ch_strdup( controloid );
163         sc->sc_mask = controlmask;
164         if ( controlexops != NULL ) {
165                 sc->sc_extendedops = ldap_charray_dup( controlexops );
166                 if ( sc->sc_extendedops == NULL ) {
167                         ch_free( sc );
168                         return LDAP_NO_MEMORY;
169                 }
170         } else {
171                 sc->sc_extendedops = NULL;
172         }
173         sc->sc_parse = controlparsefn;
174
175         /* Update slap_known_controls, too. */
176         if ( slap_known_controls == NULL ) {
177                 slap_known_controls = (char **)SLAP_MALLOC( 2 * sizeof(char *) );
178                 if ( slap_known_controls == NULL ) {
179                         if ( sc->sc_extendedops != NULL ) {
180                                 ldap_charray_free( sc->sc_extendedops );
181                         }
182                         ch_free( sc );
183                         return LDAP_NO_MEMORY;
184                 }
185                 slap_known_controls[0] = ch_strdup( sc->sc_oid );
186                 slap_known_controls[1] = NULL;
187
188         } else {
189                 char **new_known_controls;
190
191                 for ( i = 0; slap_known_controls[i] != NULL; i++ ) {
192                         /* EMPTY */ ;
193                 }
194
195                 new_known_controls = (char **)SLAP_REALLOC(
196                         slap_known_controls, (i + 2) * sizeof(char *) );
197
198                 if ( new_known_controls == NULL ) {
199                         if ( sc->sc_extendedops != NULL ) {
200                                 ldap_charray_free( sc->sc_extendedops );
201                         }
202                         ch_free( sc );
203                         return LDAP_NO_MEMORY;
204                 }
205                 slap_known_controls = new_known_controls;
206                 slap_known_controls[i++] = ch_strdup( sc->sc_oid );
207                 slap_known_controls[i] = NULL;
208         }
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                 rc = register_supported_control( control_defs[i].sc_oid,
228                         control_defs[i].sc_mask, control_defs[i].sc_extendedops,
229                         control_defs[i].sc_parse );
230                 if ( rc != LDAP_SUCCESS ) break;
231         }
232
233         return rc;
234 }
235
236 /*
237  * Free memory associated with list of supported controls.
238  */
239 void
240 controls_destroy( void )
241 {
242         struct slap_control *sc;
243
244         while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
245                 sc = LDAP_SLIST_FIRST(&controls_list);
246                 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
247
248                 ch_free( sc->sc_oid );
249                 if ( sc->sc_extendedops != NULL ) {
250                         ldap_charray_free( sc->sc_extendedops );
251                 }
252                 ch_free( sc );
253         }
254         ldap_charray_free( slap_known_controls );
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_NO_CONTROL ) {
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_CRITICAL_CONTROL
653                 : SLAP_NONCRITICAL_CONTROL;
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_NO_CONTROL ) {
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_CRITICAL_CONTROL
676                 : SLAP_NONCRITICAL_CONTROL;
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_NO_CONTROL ) {
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_CRITICAL_CONTROL
696                 : SLAP_NONCRITICAL_CONTROL;
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_NO_CONTROL ) {
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_CRITICAL_CONTROL
781                 : SLAP_NONCRITICAL_CONTROL;
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
797         if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
798                 rs->sr_text = "paged results control specified multiple times";
799                 return LDAP_PROTOCOL_ERROR;
800         }
801
802         if ( op->o_sync != SLAP_NO_CONTROL ) {
803                 rs->sr_text = "paged results control specified with sync control";
804                 return LDAP_PROTOCOL_ERROR;
805         }
806
807         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
808                 rs->sr_text = "paged results control value is empty (or absent)";
809                 return LDAP_PROTOCOL_ERROR;
810         }
811
812         /* Parse the control value
813          *      realSearchControlValue ::= SEQUENCE {
814          *              size    INTEGER (0..maxInt),
815          *                              -- requested page size from client
816          *                              -- result set size estimate from server
817          *              cookie  OCTET STRING
818          * }
819          */
820         ber = ber_init( &ctrl->ldctl_value );
821         if ( ber == NULL ) {
822                 rs->sr_text = "internal error";
823                 return LDAP_OTHER;
824         }
825
826         tag = ber_scanf( ber, "{im}", &size, &cookie );
827
828         if ( tag == LBER_ERROR ) {
829                 rs->sr_text = "paged results control could not be decoded";
830                 rc = LDAP_PROTOCOL_ERROR;
831                 goto done;
832         }
833
834         if ( size < 0 ) {
835                 rs->sr_text = "paged results control size invalid";
836                 rc = LDAP_PROTOCOL_ERROR;
837                 goto done;
838         }
839
840 #if 0
841         /* defer cookie decoding/checks to backend... */
842         if ( cookie.bv_len ) {
843                 PagedResultsCookie reqcookie;
844                 if( cookie.bv_len != sizeof( reqcookie ) ) {
845                         /* bad cookie */
846                         rs->sr_text = "paged results cookie is invalid";
847                         rc = LDAP_PROTOCOL_ERROR;
848                         goto done;
849                 }
850
851                 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
852
853                 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
854                         /* bad cookie */
855                         rs->sr_text = "paged results cookie is invalid";
856                         rc = LDAP_PROTOCOL_ERROR;
857                         goto done;
858
859                 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
860                         rs->sr_text = "paged results cookie is invalid or old";
861                         rc = LDAP_UNWILLING_TO_PERFORM;
862                         goto done;
863                 }
864
865         } else {
866                 /* Initial request.  Initialize state. */
867 #if 0
868                 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
869                         /* There's another pagedResults control on the
870                          * same connection; reject new pagedResults controls 
871                          * (allowed by RFC2696) */
872                         rs->sr_text = "paged results cookie unavailable; try later";
873                         rc = LDAP_UNWILLING_TO_PERFORM;
874                         goto done;
875                 }
876 #endif
877                 op->o_pagedresults_state.ps_cookie = 0;
878                 op->o_pagedresults_state.ps_count = 0;
879         }
880 #endif
881
882         op->o_pagedresults_size = size;
883
884         /* NOTE: according to RFC 2696 3.:
885
886     If the page size is greater than or equal to the sizeLimit value, the
887     server should ignore the control as the request can be satisfied in a
888     single page.
889          
890          * NOTE: this assumes that the op->ors_slimit be set
891          * before the controls are parsed.     
892          */
893                 
894         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
895                 op->o_pagedresults = SLAP_IGNORED_CONTROL;
896
897         } else if ( ctrl->ldctl_iscritical ) {
898                 op->o_pagedresults = SLAP_CRITICAL_CONTROL;
899
900         } else {
901                 op->o_pagedresults = SLAP_NONCRITICAL_CONTROL;
902         }
903
904 done:;
905         (void)ber_free( ber, 1 );
906         return rc;
907 }
908
909 static int parseAssert (
910         Operation *op,
911         SlapReply *rs,
912         LDAPControl *ctrl )
913 {
914         BerElement      *ber;
915         struct berval   fstr = BER_BVNULL;
916         const char *err_msg = "";
917
918         if ( op->o_assert != SLAP_NO_CONTROL ) {
919                 rs->sr_text = "assert control specified multiple times";
920                 return LDAP_PROTOCOL_ERROR;
921         }
922
923         if ( ctrl->ldctl_value.bv_len == 0 ) {
924                 rs->sr_text = "assert control value is empty (or absent)";
925                 return LDAP_PROTOCOL_ERROR;
926         }
927
928         ber = ber_init( &(ctrl->ldctl_value) );
929         if (ber == NULL) {
930                 rs->sr_text = "assert control: internal error";
931                 return LDAP_OTHER;
932         }
933         
934         rs->sr_err = get_filter( op, ber, &(op->o_assertion), &rs->sr_text);
935
936         if( rs->sr_err != LDAP_SUCCESS ) {
937                 if( rs->sr_err == SLAPD_DISCONNECT ) {
938                         rs->sr_err = LDAP_PROTOCOL_ERROR;
939                         send_ldap_disconnect( op, rs );
940                         rs->sr_err = SLAPD_DISCONNECT;
941                 } else {
942                         send_ldap_result( op, rs );
943                 }
944                 if( op->o_assertion != NULL ) {
945                         filter_free_x( op, op->o_assertion );
946                 }
947                 return rs->sr_err;
948         }
949
950 #ifdef LDAP_DEBUG
951         filter2bv_x( op, op->o_assertion, &fstr );
952
953         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
954                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
955         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
956 #endif
957
958         op->o_assert = ctrl->ldctl_iscritical
959                 ? SLAP_CRITICAL_CONTROL
960                 : SLAP_NONCRITICAL_CONTROL;
961
962         rs->sr_err = LDAP_SUCCESS;
963         return LDAP_SUCCESS;
964 }
965
966 static int parsePreRead (
967         Operation *op,
968         SlapReply *rs,
969         LDAPControl *ctrl )
970 {
971         ber_len_t siz, off, i;
972         AttributeName *an = NULL;
973         BerElement      *ber;
974
975         if ( op->o_preread != SLAP_NO_CONTROL ) {
976                 rs->sr_text = "preread control specified multiple times";
977                 return LDAP_PROTOCOL_ERROR;
978         }
979
980         if ( ctrl->ldctl_value.bv_len == 0 ) {
981                 rs->sr_text = "preread control value is empty (or absent)";
982                 return LDAP_PROTOCOL_ERROR;
983         }
984
985         ber = ber_init( &(ctrl->ldctl_value) );
986         if (ber == NULL) {
987                 rs->sr_text = "preread control: internal error";
988                 return LDAP_OTHER;
989         }
990
991         siz = sizeof( AttributeName );
992         off = offsetof( AttributeName, an_name );
993         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
994                 rs->sr_text = "preread control: decoding error";
995                 return LDAP_PROTOCOL_ERROR;
996         }
997
998         for( i=0; i<siz; i++ ) {
999                 int             rc = LDAP_SUCCESS;
1000                 const char      *dummy = NULL;
1001
1002                 an[i].an_desc = NULL;
1003                 an[i].an_oc = NULL;
1004                 an[i].an_oc_exclude = 0;
1005                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1006                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1007                         rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1008                         return rc;
1009                 }
1010         }
1011
1012         op->o_preread = ctrl->ldctl_iscritical
1013                 ? SLAP_CRITICAL_CONTROL
1014                 : SLAP_NONCRITICAL_CONTROL;
1015
1016         op->o_preread_attrs = an;
1017
1018         rs->sr_err = LDAP_SUCCESS;
1019         return LDAP_SUCCESS;
1020 }
1021
1022 static int parsePostRead (
1023         Operation *op,
1024         SlapReply *rs,
1025         LDAPControl *ctrl )
1026 {
1027         ber_len_t siz, off, i;
1028         AttributeName *an = NULL;
1029         BerElement      *ber;
1030
1031         if ( op->o_postread != SLAP_NO_CONTROL ) {
1032                 rs->sr_text = "postread control specified multiple times";
1033                 return LDAP_PROTOCOL_ERROR;
1034         }
1035
1036         if ( ctrl->ldctl_value.bv_len == 0 ) {
1037                 rs->sr_text = "postread control value is empty (or absent)";
1038                 return LDAP_PROTOCOL_ERROR;
1039         }
1040
1041         ber = ber_init( &(ctrl->ldctl_value) );
1042         if (ber == NULL) {
1043                 rs->sr_text = "postread control: internal error";
1044                 return LDAP_OTHER;
1045         }
1046
1047         siz = sizeof( AttributeName );
1048         off = offsetof( AttributeName, an_name );
1049         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1050                 rs->sr_text = "postread control: decoding error";
1051                 return LDAP_PROTOCOL_ERROR;
1052         }
1053
1054         for( i=0; i<siz; i++ ) {
1055                 int             rc = LDAP_SUCCESS;
1056                 const char      *dummy = NULL;
1057
1058                 an[i].an_desc = NULL;
1059                 an[i].an_oc = NULL;
1060                 an[i].an_oc_exclude = 0;
1061                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1062                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1063                         rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1064                         return rc;
1065                 }
1066         }
1067
1068         op->o_postread = ctrl->ldctl_iscritical
1069                 ? SLAP_CRITICAL_CONTROL
1070                 : SLAP_NONCRITICAL_CONTROL;
1071
1072         op->o_postread_attrs = an;
1073
1074         rs->sr_err = LDAP_SUCCESS;
1075         return LDAP_SUCCESS;
1076 }
1077
1078 int parseValuesReturnFilter (
1079         Operation *op,
1080         SlapReply *rs,
1081         LDAPControl *ctrl )
1082 {
1083         BerElement      *ber;
1084         struct berval   fstr = BER_BVNULL;
1085         const char *err_msg = "";
1086
1087         if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
1088                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1089                 return LDAP_PROTOCOL_ERROR;
1090         }
1091
1092         if ( ctrl->ldctl_value.bv_len == 0 ) {
1093                 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1094                 return LDAP_PROTOCOL_ERROR;
1095         }
1096
1097         ber = ber_init( &(ctrl->ldctl_value) );
1098         if (ber == NULL) {
1099                 rs->sr_text = "internal error";
1100                 return LDAP_OTHER;
1101         }
1102         
1103         rs->sr_err = get_vrFilter( op, ber, &(op->o_vrFilter), &rs->sr_text);
1104
1105         if( rs->sr_err != LDAP_SUCCESS ) {
1106                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1107                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1108                         send_ldap_disconnect( op, rs );
1109                         rs->sr_err = SLAPD_DISCONNECT;
1110                 } else {
1111                         send_ldap_result( op, rs );
1112                 }
1113                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1114         }
1115 #ifdef LDAP_DEBUG
1116         else {
1117                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1118         }
1119
1120         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1121                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1122         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1123 #endif
1124
1125         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1126                 ? SLAP_CRITICAL_CONTROL
1127                 : SLAP_NONCRITICAL_CONTROL;
1128
1129         rs->sr_err = LDAP_SUCCESS;
1130         return LDAP_SUCCESS;
1131 }
1132
1133 #ifdef LDAP_CONTROL_SUBENTRIES
1134 static int parseSubentries (
1135         Operation *op,
1136         SlapReply *rs,
1137         LDAPControl *ctrl )
1138 {
1139         if ( op->o_subentries != SLAP_NO_CONTROL ) {
1140                 rs->sr_text = "subentries control specified multiple times";
1141                 return LDAP_PROTOCOL_ERROR;
1142         }
1143
1144         /* FIXME: should use BER library */
1145         if( ( ctrl->ldctl_value.bv_len != 3 )
1146                 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1147                 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1148         {
1149                 rs->sr_text = "subentries control value encoding is bogus";
1150                 return LDAP_PROTOCOL_ERROR;
1151         }
1152
1153         op->o_subentries = ctrl->ldctl_iscritical
1154                 ? SLAP_CRITICAL_CONTROL
1155                 : SLAP_NONCRITICAL_CONTROL;
1156
1157         op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
1158
1159         return LDAP_SUCCESS;
1160 }
1161 #endif
1162
1163 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1164 static int parsePermissiveModify (
1165         Operation *op,
1166         SlapReply *rs,
1167         LDAPControl *ctrl )
1168 {
1169         if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
1170                 rs->sr_text = "permissiveModify control specified multiple times";
1171                 return LDAP_PROTOCOL_ERROR;
1172         }
1173
1174         if ( ctrl->ldctl_value.bv_len ) {
1175                 rs->sr_text = "permissiveModify control value not empty";
1176                 return LDAP_PROTOCOL_ERROR;
1177         }
1178
1179         op->o_permissive_modify = ctrl->ldctl_iscritical
1180                 ? SLAP_CRITICAL_CONTROL
1181                 : SLAP_NONCRITICAL_CONTROL;
1182
1183         return LDAP_SUCCESS;
1184 }
1185 #endif
1186
1187 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1188 static int parseDomainScope (
1189         Operation *op,
1190         SlapReply *rs,
1191         LDAPControl *ctrl )
1192 {
1193         if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1194                 rs->sr_text = "domainScope control specified multiple times";
1195                 return LDAP_PROTOCOL_ERROR;
1196         }
1197
1198         if ( ctrl->ldctl_value.bv_len ) {
1199                 rs->sr_text = "domainScope control value not empty";
1200                 return LDAP_PROTOCOL_ERROR;
1201         }
1202
1203         op->o_domain_scope = ctrl->ldctl_iscritical
1204                 ? SLAP_CRITICAL_CONTROL
1205                 : SLAP_NONCRITICAL_CONTROL;
1206
1207         return LDAP_SUCCESS;
1208 }
1209 #endif
1210
1211 #ifdef LDAP_CONTROL_X_TREE_DELETE
1212 static int parseTreeDelete (
1213         Operation *op,
1214         SlapReply *rs,
1215         LDAPControl *ctrl )
1216 {
1217         if ( op->o_tree_delete != SLAP_NO_CONTROL ) {
1218                 rs->sr_text = "treeDelete control specified multiple times";
1219                 return LDAP_PROTOCOL_ERROR;
1220         }
1221
1222         if ( ctrl->ldctl_value.bv_len ) {
1223                 rs->sr_text = "treeDelete control value not empty";
1224                 return LDAP_PROTOCOL_ERROR;
1225         }
1226
1227         op->o_tree_delete = ctrl->ldctl_iscritical
1228                 ? SLAP_CRITICAL_CONTROL
1229                 : SLAP_NONCRITICAL_CONTROL;
1230
1231         return LDAP_SUCCESS;
1232 }
1233 #endif
1234
1235 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1236 static int parseSearchOptions (
1237         Operation *op,
1238         SlapReply *rs,
1239         LDAPControl *ctrl )
1240 {
1241         BerElement *ber;
1242         ber_int_t search_flags;
1243
1244         if ( ctrl->ldctl_value.bv_len == 0 ) {
1245                 rs->sr_text = "searchOptions control value not empty";
1246                 return LDAP_PROTOCOL_ERROR;
1247         }
1248
1249         ber = ber_init( &ctrl->ldctl_value );
1250         if( ber == NULL ) {
1251                 rs->sr_text = "internal error";
1252                 return LDAP_OTHER;
1253         }
1254
1255         if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1256                 rs->sr_text = "searchOptions control decoding error";
1257                 return LDAP_PROTOCOL_ERROR;
1258         }
1259
1260         (void) ber_free( ber, 1 );
1261
1262         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1263                 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1264                         rs->sr_text = "searchOptions control specified multiple times "
1265                                 "or with domainScope control";
1266                         return LDAP_PROTOCOL_ERROR;
1267                 }
1268
1269                 op->o_domain_scope = ctrl->ldctl_iscritical
1270                         ? SLAP_CRITICAL_CONTROL
1271                         : SLAP_NONCRITICAL_CONTROL;
1272         }
1273
1274         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1275                 /* Other search flags not recognised so far */
1276                 rs->sr_text = "searchOptions contained unrecongized flag";
1277                 return LDAP_UNWILLING_TO_PERFORM;
1278         }
1279
1280         return LDAP_SUCCESS;
1281 }
1282 #endif
1283
1284 static int parseLDAPsync (
1285         Operation *op,
1286         SlapReply *rs,
1287         LDAPControl *ctrl )
1288 {
1289         ber_tag_t tag;
1290         BerElement *ber;
1291         ber_int_t mode;
1292         ber_len_t len;
1293         struct slap_session_entry *se;
1294
1295         if ( op->o_sync != SLAP_NO_CONTROL ) {
1296                 rs->sr_text = "Sync control specified multiple times";
1297                 return LDAP_PROTOCOL_ERROR;
1298         }
1299
1300         if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
1301                 rs->sr_text = "Sync control specified with pagedResults control";
1302                 return LDAP_PROTOCOL_ERROR;
1303         }
1304
1305
1306         if ( ctrl->ldctl_value.bv_len == 0 ) {
1307                 rs->sr_text = "Sync control value is empty (or absent)";
1308                 return LDAP_PROTOCOL_ERROR;
1309         }
1310
1311         /* Parse the control value
1312          *      syncRequestValue ::= SEQUENCE {
1313          *              mode   ENUMERATED {
1314          *                      -- 0 unused
1315          *                      refreshOnly             (1),
1316          *                      -- 2 reserved
1317          *                      refreshAndPersist       (3)
1318          *              },
1319          *              cookie  syncCookie OPTIONAL
1320          *      }
1321          */
1322
1323         ber = ber_init( &ctrl->ldctl_value );
1324         if( ber == NULL ) {
1325                 rs->sr_text = "internal error";
1326                 return LDAP_OTHER;
1327         }
1328
1329         if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1330                 rs->sr_text = "Sync control : mode decoding error";
1331                 return LDAP_PROTOCOL_ERROR;
1332         }
1333
1334         switch( mode ) {
1335         case LDAP_SYNC_REFRESH_ONLY:
1336                 mode = SLAP_SYNC_REFRESH;
1337                 break;
1338         case LDAP_SYNC_REFRESH_AND_PERSIST:
1339                 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1340                 break;
1341         default:
1342                 rs->sr_text = "Sync control : unknown update mode";
1343                 return LDAP_PROTOCOL_ERROR;
1344         }
1345
1346         tag = ber_peek_tag( ber, &len );
1347
1348         if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1349                 struct berval tmp_bv;   
1350                 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1351                         rs->sr_text = "Sync control : cookie decoding error";
1352                         return LDAP_PROTOCOL_ERROR;
1353                 }
1354                 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1355                 slap_parse_sync_cookie( &op->o_sync_state );
1356         }
1357         if ( tag == LDAP_TAG_RELOAD_HINT ) {
1358                 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1359                         rs->sr_text = "Sync control : rhint decoding error";
1360                         return LDAP_PROTOCOL_ERROR;
1361                 }
1362         }
1363         if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1364                         rs->sr_text = "Sync control : decoding error";
1365                         return LDAP_PROTOCOL_ERROR;
1366         }
1367
1368         (void) ber_free( ber, 1 );
1369
1370         op->o_sync_mode = (char) mode;
1371
1372         op->o_sync = ctrl->ldctl_iscritical
1373                 ? SLAP_CRITICAL_CONTROL
1374                 : SLAP_NONCRITICAL_CONTROL;
1375
1376         return LDAP_SUCCESS;
1377 }