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