]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
preformat "conn=%lu op=%lu"
[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 ( ctrl->ldctl_value.bv_len == 0 ) {
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         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
894                 op->o_pagedresults = SLAP_IGNORED_CONTROL;
895
896         } else if ( ctrl->ldctl_iscritical ) {
897                 op->o_pagedresults = SLAP_CRITICAL_CONTROL;
898
899         } else {
900                 op->o_pagedresults = SLAP_NONCRITICAL_CONTROL;
901         }
902
903 done:;
904         (void)ber_free( ber, 1 );
905         return rc;
906 }
907
908 static int parseAssert (
909         Operation *op,
910         SlapReply *rs,
911         LDAPControl *ctrl )
912 {
913         BerElement      *ber;
914         struct berval   fstr = BER_BVNULL;
915         const char *err_msg = "";
916
917         if ( op->o_assert != SLAP_NO_CONTROL ) {
918                 rs->sr_text = "assert control specified multiple times";
919                 return LDAP_PROTOCOL_ERROR;
920         }
921
922         if ( ctrl->ldctl_value.bv_len == 0 ) {
923                 rs->sr_text = "assert control value is empty (or absent)";
924                 return LDAP_PROTOCOL_ERROR;
925         }
926
927         ber = ber_init( &(ctrl->ldctl_value) );
928         if (ber == NULL) {
929                 rs->sr_text = "assert control: internal error";
930                 return LDAP_OTHER;
931         }
932         
933         rs->sr_err = get_filter( op, ber, &(op->o_assertion), &rs->sr_text);
934
935         if( rs->sr_err != LDAP_SUCCESS ) {
936                 if( rs->sr_err == SLAPD_DISCONNECT ) {
937                         rs->sr_err = LDAP_PROTOCOL_ERROR;
938                         send_ldap_disconnect( op, rs );
939                         rs->sr_err = SLAPD_DISCONNECT;
940                 } else {
941                         send_ldap_result( op, rs );
942                 }
943                 if( op->o_assertion != NULL ) {
944                         filter_free_x( op, op->o_assertion );
945                 }
946                 return rs->sr_err;
947         }
948
949 #ifdef LDAP_DEBUG
950         filter2bv_x( op, op->o_assertion, &fstr );
951
952         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
953                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
954         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
955 #endif
956
957         op->o_assert = ctrl->ldctl_iscritical
958                 ? SLAP_CRITICAL_CONTROL
959                 : SLAP_NONCRITICAL_CONTROL;
960
961         rs->sr_err = LDAP_SUCCESS;
962         return LDAP_SUCCESS;
963 }
964
965 static int parsePreRead (
966         Operation *op,
967         SlapReply *rs,
968         LDAPControl *ctrl )
969 {
970         ber_len_t siz, off, i;
971         AttributeName *an = NULL;
972         BerElement      *ber;
973
974         if ( op->o_preread != SLAP_NO_CONTROL ) {
975                 rs->sr_text = "preread control specified multiple times";
976                 return LDAP_PROTOCOL_ERROR;
977         }
978
979         if ( ctrl->ldctl_value.bv_len == 0 ) {
980                 rs->sr_text = "preread control value is empty (or absent)";
981                 return LDAP_PROTOCOL_ERROR;
982         }
983
984         ber = ber_init( &(ctrl->ldctl_value) );
985         if (ber == NULL) {
986                 rs->sr_text = "preread control: internal error";
987                 return LDAP_OTHER;
988         }
989
990         siz = sizeof( AttributeName );
991         off = offsetof( AttributeName, an_name );
992         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
993                 rs->sr_text = "preread control: decoding error";
994                 return LDAP_PROTOCOL_ERROR;
995         }
996
997         for( i=0; i<siz; i++ ) {
998                 int             rc = LDAP_SUCCESS;
999                 const char      *dummy = NULL;
1000
1001                 an[i].an_desc = NULL;
1002                 an[i].an_oc = NULL;
1003                 an[i].an_oc_exclude = 0;
1004                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1005                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1006                         rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1007                         return rc;
1008                 }
1009         }
1010
1011         op->o_preread = ctrl->ldctl_iscritical
1012                 ? SLAP_CRITICAL_CONTROL
1013                 : SLAP_NONCRITICAL_CONTROL;
1014
1015         op->o_preread_attrs = an;
1016
1017         rs->sr_err = LDAP_SUCCESS;
1018         return LDAP_SUCCESS;
1019 }
1020
1021 static int parsePostRead (
1022         Operation *op,
1023         SlapReply *rs,
1024         LDAPControl *ctrl )
1025 {
1026         ber_len_t siz, off, i;
1027         AttributeName *an = NULL;
1028         BerElement      *ber;
1029
1030         if ( op->o_postread != SLAP_NO_CONTROL ) {
1031                 rs->sr_text = "postread control specified multiple times";
1032                 return LDAP_PROTOCOL_ERROR;
1033         }
1034
1035         if ( ctrl->ldctl_value.bv_len == 0 ) {
1036                 rs->sr_text = "postread control value is empty (or absent)";
1037                 return LDAP_PROTOCOL_ERROR;
1038         }
1039
1040         ber = ber_init( &(ctrl->ldctl_value) );
1041         if (ber == NULL) {
1042                 rs->sr_text = "postread control: internal error";
1043                 return LDAP_OTHER;
1044         }
1045
1046         siz = sizeof( AttributeName );
1047         off = offsetof( AttributeName, an_name );
1048         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1049                 rs->sr_text = "postread control: decoding error";
1050                 return LDAP_PROTOCOL_ERROR;
1051         }
1052
1053         for( i=0; i<siz; i++ ) {
1054                 int             rc = LDAP_SUCCESS;
1055                 const char      *dummy = NULL;
1056
1057                 an[i].an_desc = NULL;
1058                 an[i].an_oc = NULL;
1059                 an[i].an_oc_exclude = 0;
1060                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1061                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1062                         rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1063                         return rc;
1064                 }
1065         }
1066
1067         op->o_postread = ctrl->ldctl_iscritical
1068                 ? SLAP_CRITICAL_CONTROL
1069                 : SLAP_NONCRITICAL_CONTROL;
1070
1071         op->o_postread_attrs = an;
1072
1073         rs->sr_err = LDAP_SUCCESS;
1074         return LDAP_SUCCESS;
1075 }
1076
1077 int parseValuesReturnFilter (
1078         Operation *op,
1079         SlapReply *rs,
1080         LDAPControl *ctrl )
1081 {
1082         BerElement      *ber;
1083         struct berval   fstr = BER_BVNULL;
1084         const char *err_msg = "";
1085
1086         if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
1087                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1088                 return LDAP_PROTOCOL_ERROR;
1089         }
1090
1091         if ( ctrl->ldctl_value.bv_len == 0 ) {
1092                 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1093                 return LDAP_PROTOCOL_ERROR;
1094         }
1095
1096         ber = ber_init( &(ctrl->ldctl_value) );
1097         if (ber == NULL) {
1098                 rs->sr_text = "internal error";
1099                 return LDAP_OTHER;
1100         }
1101         
1102         rs->sr_err = get_vrFilter( op, ber, &(op->o_vrFilter), &rs->sr_text);
1103
1104         if( rs->sr_err != LDAP_SUCCESS ) {
1105                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1106                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1107                         send_ldap_disconnect( op, rs );
1108                         rs->sr_err = SLAPD_DISCONNECT;
1109                 } else {
1110                         send_ldap_result( op, rs );
1111                 }
1112                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1113         }
1114 #ifdef LDAP_DEBUG
1115         else {
1116                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1117         }
1118
1119         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1120                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1121         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1122 #endif
1123
1124         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1125                 ? SLAP_CRITICAL_CONTROL
1126                 : SLAP_NONCRITICAL_CONTROL;
1127
1128         rs->sr_err = LDAP_SUCCESS;
1129         return LDAP_SUCCESS;
1130 }
1131
1132 #ifdef LDAP_CONTROL_SUBENTRIES
1133 static int parseSubentries (
1134         Operation *op,
1135         SlapReply *rs,
1136         LDAPControl *ctrl )
1137 {
1138         if ( op->o_subentries != SLAP_NO_CONTROL ) {
1139                 rs->sr_text = "subentries control specified multiple times";
1140                 return LDAP_PROTOCOL_ERROR;
1141         }
1142
1143         /* FIXME: should use BER library */
1144         if( ( ctrl->ldctl_value.bv_len != 3 )
1145                 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1146                 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1147         {
1148                 rs->sr_text = "subentries control value encoding is bogus";
1149                 return LDAP_PROTOCOL_ERROR;
1150         }
1151
1152         op->o_subentries = ctrl->ldctl_iscritical
1153                 ? SLAP_CRITICAL_CONTROL
1154                 : SLAP_NONCRITICAL_CONTROL;
1155
1156         op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
1157
1158         return LDAP_SUCCESS;
1159 }
1160 #endif
1161
1162 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1163 static int parsePermissiveModify (
1164         Operation *op,
1165         SlapReply *rs,
1166         LDAPControl *ctrl )
1167 {
1168         if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
1169                 rs->sr_text = "permissiveModify control specified multiple times";
1170                 return LDAP_PROTOCOL_ERROR;
1171         }
1172
1173         if ( ctrl->ldctl_value.bv_len ) {
1174                 rs->sr_text = "permissiveModify control value not empty";
1175                 return LDAP_PROTOCOL_ERROR;
1176         }
1177
1178         op->o_permissive_modify = ctrl->ldctl_iscritical
1179                 ? SLAP_CRITICAL_CONTROL
1180                 : SLAP_NONCRITICAL_CONTROL;
1181
1182         return LDAP_SUCCESS;
1183 }
1184 #endif
1185
1186 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1187 static int parseDomainScope (
1188         Operation *op,
1189         SlapReply *rs,
1190         LDAPControl *ctrl )
1191 {
1192         if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1193                 rs->sr_text = "domainScope control specified multiple times";
1194                 return LDAP_PROTOCOL_ERROR;
1195         }
1196
1197         if ( ctrl->ldctl_value.bv_len ) {
1198                 rs->sr_text = "domainScope control value not empty";
1199                 return LDAP_PROTOCOL_ERROR;
1200         }
1201
1202         op->o_domain_scope = ctrl->ldctl_iscritical
1203                 ? SLAP_CRITICAL_CONTROL
1204                 : SLAP_NONCRITICAL_CONTROL;
1205
1206         return LDAP_SUCCESS;
1207 }
1208 #endif
1209
1210 #ifdef LDAP_CONTROL_X_TREE_DELETE
1211 static int parseTreeDelete (
1212         Operation *op,
1213         SlapReply *rs,
1214         LDAPControl *ctrl )
1215 {
1216         if ( op->o_tree_delete != SLAP_NO_CONTROL ) {
1217                 rs->sr_text = "treeDelete control specified multiple times";
1218                 return LDAP_PROTOCOL_ERROR;
1219         }
1220
1221         if ( ctrl->ldctl_value.bv_len ) {
1222                 rs->sr_text = "treeDelete control value not empty";
1223                 return LDAP_PROTOCOL_ERROR;
1224         }
1225
1226         op->o_tree_delete = ctrl->ldctl_iscritical
1227                 ? SLAP_CRITICAL_CONTROL
1228                 : SLAP_NONCRITICAL_CONTROL;
1229
1230         return LDAP_SUCCESS;
1231 }
1232 #endif
1233
1234 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1235 static int parseSearchOptions (
1236         Operation *op,
1237         SlapReply *rs,
1238         LDAPControl *ctrl )
1239 {
1240         BerElement *ber;
1241         ber_int_t search_flags;
1242
1243         if ( ctrl->ldctl_value.bv_len == 0 ) {
1244                 rs->sr_text = "searchOptions control value not empty";
1245                 return LDAP_PROTOCOL_ERROR;
1246         }
1247
1248         ber = ber_init( &ctrl->ldctl_value );
1249         if( ber == NULL ) {
1250                 rs->sr_text = "internal error";
1251                 return LDAP_OTHER;
1252         }
1253
1254         if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1255                 rs->sr_text = "searchOptions control decoding error";
1256                 return LDAP_PROTOCOL_ERROR;
1257         }
1258
1259         (void) ber_free( ber, 1 );
1260
1261         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1262                 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1263                         rs->sr_text = "searchOptions control specified multiple times "
1264                                 "or with domainScope control";
1265                         return LDAP_PROTOCOL_ERROR;
1266                 }
1267
1268                 op->o_domain_scope = ctrl->ldctl_iscritical
1269                         ? SLAP_CRITICAL_CONTROL
1270                         : SLAP_NONCRITICAL_CONTROL;
1271         }
1272
1273         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1274                 /* Other search flags not recognised so far */
1275                 rs->sr_text = "searchOptions contained unrecongized flag";
1276                 return LDAP_UNWILLING_TO_PERFORM;
1277         }
1278
1279         return LDAP_SUCCESS;
1280 }
1281 #endif
1282
1283 static int parseLDAPsync (
1284         Operation *op,
1285         SlapReply *rs,
1286         LDAPControl *ctrl )
1287 {
1288         ber_tag_t tag;
1289         BerElement *ber;
1290         ber_int_t mode;
1291         ber_len_t len;
1292         struct slap_session_entry *se;
1293
1294         if ( op->o_sync != SLAP_NO_CONTROL ) {
1295                 rs->sr_text = "Sync control specified multiple times";
1296                 return LDAP_PROTOCOL_ERROR;
1297         }
1298
1299         if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
1300                 rs->sr_text = "Sync control specified with pagedResults control";
1301                 return LDAP_PROTOCOL_ERROR;
1302         }
1303
1304
1305         if ( ctrl->ldctl_value.bv_len == 0 ) {
1306                 rs->sr_text = "Sync control value is empty (or absent)";
1307                 return LDAP_PROTOCOL_ERROR;
1308         }
1309
1310         /* Parse the control value
1311          *      syncRequestValue ::= SEQUENCE {
1312          *              mode   ENUMERATED {
1313          *                      -- 0 unused
1314          *                      refreshOnly             (1),
1315          *                      -- 2 reserved
1316          *                      refreshAndPersist       (3)
1317          *              },
1318          *              cookie  syncCookie OPTIONAL
1319          *      }
1320          */
1321
1322         ber = ber_init( &ctrl->ldctl_value );
1323         if( ber == NULL ) {
1324                 rs->sr_text = "internal error";
1325                 return LDAP_OTHER;
1326         }
1327
1328         if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1329                 rs->sr_text = "Sync control : mode decoding error";
1330                 return LDAP_PROTOCOL_ERROR;
1331         }
1332
1333         switch( mode ) {
1334         case LDAP_SYNC_REFRESH_ONLY:
1335                 mode = SLAP_SYNC_REFRESH;
1336                 break;
1337         case LDAP_SYNC_REFRESH_AND_PERSIST:
1338                 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1339                 break;
1340         default:
1341                 rs->sr_text = "Sync control : unknown update mode";
1342                 return LDAP_PROTOCOL_ERROR;
1343         }
1344
1345         tag = ber_peek_tag( ber, &len );
1346
1347         if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1348                 struct berval tmp_bv;   
1349                 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1350                         rs->sr_text = "Sync control : cookie decoding error";
1351                         return LDAP_PROTOCOL_ERROR;
1352                 }
1353                 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1354                 slap_parse_sync_cookie( &op->o_sync_state );
1355         }
1356         if ( tag == LDAP_TAG_RELOAD_HINT ) {
1357                 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1358                         rs->sr_text = "Sync control : rhint decoding error";
1359                         return LDAP_PROTOCOL_ERROR;
1360                 }
1361         }
1362         if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1363                         rs->sr_text = "Sync control : decoding error";
1364                         return LDAP_PROTOCOL_ERROR;
1365         }
1366
1367         (void) ber_free( ber, 1 );
1368
1369         op->o_sync_mode = (char) mode;
1370
1371         op->o_sync = ctrl->ldctl_iscritical
1372                 ? SLAP_CRITICAL_CONTROL
1373                 : SLAP_NONCRITICAL_CONTROL;
1374
1375         return LDAP_SUCCESS;
1376 }