]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
21818c04968ecfa291fc658a2341c438d99d1d03
[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         int             rc = LDAP_SUCCESS;
847         ber_tag_t       tag;
848         ber_int_t       size;
849         BerElement      *ber;
850         struct berval   cookie = BER_BVNULL;
851
852         if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
853                 rs->sr_text = "paged results control specified multiple times";
854                 return LDAP_PROTOCOL_ERROR;
855         }
856
857         if ( op->o_sync != SLAP_NO_CONTROL ) {
858                 rs->sr_text = "paged results control specified with sync control";
859                 return LDAP_PROTOCOL_ERROR;
860         }
861
862         if ( ctrl->ldctl_value.bv_len == 0 ) {
863                 rs->sr_text = "paged results control value is empty (or absent)";
864                 return LDAP_PROTOCOL_ERROR;
865         }
866
867         /* Parse the control value
868          *      realSearchControlValue ::= SEQUENCE {
869          *              size    INTEGER (0..maxInt),
870          *                              -- requested page size from client
871          *                              -- result set size estimate from server
872          *              cookie  OCTET STRING
873          * }
874          */
875         ber = ber_init( &ctrl->ldctl_value );
876         if ( ber == NULL ) {
877                 rs->sr_text = "internal error";
878                 return LDAP_OTHER;
879         }
880
881         tag = ber_scanf( ber, "{im}", &size, &cookie );
882
883         if ( tag == LBER_ERROR ) {
884                 rs->sr_text = "paged results control could not be decoded";
885                 rc = LDAP_PROTOCOL_ERROR;
886                 goto done;
887         }
888
889         if ( size < 0 ) {
890                 rs->sr_text = "paged results control size invalid";
891                 rc = LDAP_PROTOCOL_ERROR;
892                 goto done;
893         }
894
895 #if 0
896         /* defer cookie decoding/checks to backend... */
897         if ( cookie.bv_len ) {
898                 PagedResultsCookie reqcookie;
899                 if( cookie.bv_len != sizeof( reqcookie ) ) {
900                         /* bad cookie */
901                         rs->sr_text = "paged results cookie is invalid";
902                         rc = LDAP_PROTOCOL_ERROR;
903                         goto done;
904                 }
905
906                 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
907
908                 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
909                         /* bad cookie */
910                         rs->sr_text = "paged results cookie is invalid";
911                         rc = LDAP_PROTOCOL_ERROR;
912                         goto done;
913
914                 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
915                         rs->sr_text = "paged results cookie is invalid or old";
916                         rc = LDAP_UNWILLING_TO_PERFORM;
917                         goto done;
918                 }
919
920         } else {
921                 /* Initial request.  Initialize state. */
922 #if 0
923                 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
924                         /* There's another pagedResults control on the
925                          * same connection; reject new pagedResults controls 
926                          * (allowed by RFC2696) */
927                         rs->sr_text = "paged results cookie unavailable; try later";
928                         rc = LDAP_UNWILLING_TO_PERFORM;
929                         goto done;
930                 }
931 #endif
932                 op->o_pagedresults_state.ps_cookie = 0;
933                 op->o_pagedresults_state.ps_count = 0;
934         }
935 #endif
936
937         op->o_pagedresults_size = size;
938
939         /* NOTE: according to RFC 2696 3.:
940
941     If the page size is greater than or equal to the sizeLimit value, the
942     server should ignore the control as the request can be satisfied in a
943     single page.
944          
945          * NOTE: this assumes that the op->ors_slimit be set
946          * before the controls are parsed.     
947          */
948         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
949                 op->o_pagedresults = SLAP_IGNORED_CONTROL;
950
951         } else if ( ctrl->ldctl_iscritical ) {
952                 op->o_pagedresults = SLAP_CRITICAL_CONTROL;
953
954         } else {
955                 op->o_pagedresults = SLAP_NONCRITICAL_CONTROL;
956         }
957
958 done:;
959         (void)ber_free( ber, 1 );
960         return rc;
961 }
962
963 static int parseAssert (
964         Operation *op,
965         SlapReply *rs,
966         LDAPControl *ctrl )
967 {
968         BerElement      *ber;
969         struct berval   fstr = BER_BVNULL;
970         const char *err_msg = "";
971
972         if ( op->o_assert != SLAP_NO_CONTROL ) {
973                 rs->sr_text = "assert control specified multiple times";
974                 return LDAP_PROTOCOL_ERROR;
975         }
976
977         if ( ctrl->ldctl_value.bv_len == 0 ) {
978                 rs->sr_text = "assert control value is empty (or absent)";
979                 return LDAP_PROTOCOL_ERROR;
980         }
981
982         ber = ber_init( &(ctrl->ldctl_value) );
983         if (ber == NULL) {
984                 rs->sr_text = "assert control: internal error";
985                 return LDAP_OTHER;
986         }
987         
988         rs->sr_err = get_filter( op, ber, &(op->o_assertion), &rs->sr_text);
989
990         if( rs->sr_err != LDAP_SUCCESS ) {
991                 if( rs->sr_err == SLAPD_DISCONNECT ) {
992                         rs->sr_err = LDAP_PROTOCOL_ERROR;
993                         send_ldap_disconnect( op, rs );
994                         rs->sr_err = SLAPD_DISCONNECT;
995                 } else {
996                         send_ldap_result( op, rs );
997                 }
998                 if( op->o_assertion != NULL ) {
999                         filter_free_x( op, op->o_assertion );
1000                 }
1001                 return rs->sr_err;
1002         }
1003
1004 #ifdef LDAP_DEBUG
1005         filter2bv_x( op, op->o_assertion, &fstr );
1006
1007 #ifdef NEW_LOGGING
1008         LDAP_LOG( OPERATION, ARGS, 
1009                 "parseAssert: conn %ld assert: %s\n", 
1010                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1011 #else
1012         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1013                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1014 #endif
1015         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1016 #endif
1017
1018         op->o_assert = ctrl->ldctl_iscritical
1019                 ? SLAP_CRITICAL_CONTROL
1020                 : SLAP_NONCRITICAL_CONTROL;
1021
1022         rs->sr_err = LDAP_SUCCESS;
1023         return LDAP_SUCCESS;
1024 }
1025
1026 static int parsePreRead (
1027         Operation *op,
1028         SlapReply *rs,
1029         LDAPControl *ctrl )
1030 {
1031         ber_len_t siz, off, i;
1032         AttributeName *an = NULL;
1033         BerElement      *ber;
1034
1035         if ( op->o_preread != SLAP_NO_CONTROL ) {
1036                 rs->sr_text = "preread control specified multiple times";
1037                 return LDAP_PROTOCOL_ERROR;
1038         }
1039
1040         if ( ctrl->ldctl_value.bv_len == 0 ) {
1041                 rs->sr_text = "preread control value is empty (or absent)";
1042                 return LDAP_PROTOCOL_ERROR;
1043         }
1044
1045         ber = ber_init( &(ctrl->ldctl_value) );
1046         if (ber == NULL) {
1047                 rs->sr_text = "preread control: internal error";
1048                 return LDAP_OTHER;
1049         }
1050
1051         siz = sizeof( AttributeName );
1052         off = offsetof( AttributeName, an_name );
1053         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1054                 rs->sr_text = "preread control: decoding error";
1055                 return LDAP_PROTOCOL_ERROR;
1056         }
1057
1058         for( i=0; i<siz; i++ ) {
1059                 int             rc = LDAP_SUCCESS;
1060                 const char      *dummy = NULL;
1061
1062                 an[i].an_desc = NULL;
1063                 an[i].an_oc = NULL;
1064                 an[i].an_oc_exclude = 0;
1065                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1066                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1067                         rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1068                         return rc;
1069                 }
1070         }
1071
1072         op->o_preread = ctrl->ldctl_iscritical
1073                 ? SLAP_CRITICAL_CONTROL
1074                 : SLAP_NONCRITICAL_CONTROL;
1075
1076         op->o_preread_attrs = an;
1077
1078         rs->sr_err = LDAP_SUCCESS;
1079         return LDAP_SUCCESS;
1080 }
1081
1082 static int parsePostRead (
1083         Operation *op,
1084         SlapReply *rs,
1085         LDAPControl *ctrl )
1086 {
1087         ber_len_t siz, off, i;
1088         AttributeName *an = NULL;
1089         BerElement      *ber;
1090
1091         if ( op->o_postread != SLAP_NO_CONTROL ) {
1092                 rs->sr_text = "postread control specified multiple times";
1093                 return LDAP_PROTOCOL_ERROR;
1094         }
1095
1096         if ( ctrl->ldctl_value.bv_len == 0 ) {
1097                 rs->sr_text = "postread control value is empty (or absent)";
1098                 return LDAP_PROTOCOL_ERROR;
1099         }
1100
1101         ber = ber_init( &(ctrl->ldctl_value) );
1102         if (ber == NULL) {
1103                 rs->sr_text = "postread control: internal error";
1104                 return LDAP_OTHER;
1105         }
1106
1107         siz = sizeof( AttributeName );
1108         off = offsetof( AttributeName, an_name );
1109         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1110                 rs->sr_text = "postread control: decoding error";
1111                 return LDAP_PROTOCOL_ERROR;
1112         }
1113
1114         for( i=0; i<siz; i++ ) {
1115                 int             rc = LDAP_SUCCESS;
1116                 const char      *dummy = NULL;
1117
1118                 an[i].an_desc = NULL;
1119                 an[i].an_oc = NULL;
1120                 an[i].an_oc_exclude = 0;
1121                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1122                 if ( rc != LDAP_SUCCESS && ctrl->ldctl_iscritical ) {
1123                         rs->sr_text = dummy ? dummy : "postread control: unknown attributeType";
1124                         return rc;
1125                 }
1126         }
1127
1128         op->o_postread = ctrl->ldctl_iscritical
1129                 ? SLAP_CRITICAL_CONTROL
1130                 : SLAP_NONCRITICAL_CONTROL;
1131
1132         op->o_postread_attrs = an;
1133
1134         rs->sr_err = LDAP_SUCCESS;
1135         return LDAP_SUCCESS;
1136 }
1137
1138 int parseValuesReturnFilter (
1139         Operation *op,
1140         SlapReply *rs,
1141         LDAPControl *ctrl )
1142 {
1143         BerElement      *ber;
1144         struct berval   fstr = BER_BVNULL;
1145         const char *err_msg = "";
1146
1147         if ( op->o_valuesreturnfilter != SLAP_NO_CONTROL ) {
1148                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1149                 return LDAP_PROTOCOL_ERROR;
1150         }
1151
1152         if ( ctrl->ldctl_value.bv_len == 0 ) {
1153                 rs->sr_text = "valuesReturnFilter control value is empty (or absent)";
1154                 return LDAP_PROTOCOL_ERROR;
1155         }
1156
1157         ber = ber_init( &(ctrl->ldctl_value) );
1158         if (ber == NULL) {
1159                 rs->sr_text = "internal error";
1160                 return LDAP_OTHER;
1161         }
1162         
1163         rs->sr_err = get_vrFilter( op, ber, &(op->o_vrFilter), &rs->sr_text);
1164
1165         if( rs->sr_err != LDAP_SUCCESS ) {
1166                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1167                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1168                         send_ldap_disconnect( op, rs );
1169                         rs->sr_err = SLAPD_DISCONNECT;
1170                 } else {
1171                         send_ldap_result( op, rs );
1172                 }
1173                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1174         }
1175 #ifdef LDAP_DEBUG
1176         else {
1177                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1178         }
1179
1180 #ifdef NEW_LOGGING
1181         LDAP_LOG( OPERATION, ARGS, 
1182                 "parseValuesReturnFilter: conn %d       vrFilter: %s\n", 
1183                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1184 #else
1185         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1186                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1187 #endif
1188         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1189 #endif
1190
1191         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1192                 ? SLAP_CRITICAL_CONTROL
1193                 : SLAP_NONCRITICAL_CONTROL;
1194
1195         rs->sr_err = LDAP_SUCCESS;
1196         return LDAP_SUCCESS;
1197 }
1198
1199 #ifdef LDAP_CONTROL_SUBENTRIES
1200 static int parseSubentries (
1201         Operation *op,
1202         SlapReply *rs,
1203         LDAPControl *ctrl )
1204 {
1205         if ( op->o_subentries != SLAP_NO_CONTROL ) {
1206                 rs->sr_text = "subentries control specified multiple times";
1207                 return LDAP_PROTOCOL_ERROR;
1208         }
1209
1210         /* FIXME: should use BER library */
1211         if( ( ctrl->ldctl_value.bv_len != 3 )
1212                 && ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1213                 && ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1214         {
1215                 rs->sr_text = "subentries control value encoding is bogus";
1216                 return LDAP_PROTOCOL_ERROR;
1217         }
1218
1219         op->o_subentries = ctrl->ldctl_iscritical
1220                 ? SLAP_CRITICAL_CONTROL
1221                 : SLAP_NONCRITICAL_CONTROL;
1222
1223         op->o_subentries_visibility = (ctrl->ldctl_value.bv_val[2] != 0x00);
1224
1225         return LDAP_SUCCESS;
1226 }
1227 #endif
1228
1229 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
1230 static int parsePermissiveModify (
1231         Operation *op,
1232         SlapReply *rs,
1233         LDAPControl *ctrl )
1234 {
1235         if ( op->o_permissive_modify != SLAP_NO_CONTROL ) {
1236                 rs->sr_text = "permissiveModify control specified multiple times";
1237                 return LDAP_PROTOCOL_ERROR;
1238         }
1239
1240         if ( ctrl->ldctl_value.bv_len ) {
1241                 rs->sr_text = "permissiveModify control value not empty";
1242                 return LDAP_PROTOCOL_ERROR;
1243         }
1244
1245         op->o_permissive_modify = ctrl->ldctl_iscritical
1246                 ? SLAP_CRITICAL_CONTROL
1247                 : SLAP_NONCRITICAL_CONTROL;
1248
1249         return LDAP_SUCCESS;
1250 }
1251 #endif
1252
1253 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1254 static int parseDomainScope (
1255         Operation *op,
1256         SlapReply *rs,
1257         LDAPControl *ctrl )
1258 {
1259         if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1260                 rs->sr_text = "domainScope control specified multiple times";
1261                 return LDAP_PROTOCOL_ERROR;
1262         }
1263
1264         if ( ctrl->ldctl_value.bv_len ) {
1265                 rs->sr_text = "domainScope control value not empty";
1266                 return LDAP_PROTOCOL_ERROR;
1267         }
1268
1269         op->o_domain_scope = ctrl->ldctl_iscritical
1270                 ? SLAP_CRITICAL_CONTROL
1271                 : SLAP_NONCRITICAL_CONTROL;
1272
1273         return LDAP_SUCCESS;
1274 }
1275 #endif
1276
1277 #ifdef LDAP_CONTROL_X_TREE_DELETE
1278 static int parseTreeDelete (
1279         Operation *op,
1280         SlapReply *rs,
1281         LDAPControl *ctrl )
1282 {
1283         if ( op->o_tree_delete != SLAP_NO_CONTROL ) {
1284                 rs->sr_text = "treeDelete control specified multiple times";
1285                 return LDAP_PROTOCOL_ERROR;
1286         }
1287
1288         if ( ctrl->ldctl_value.bv_len ) {
1289                 rs->sr_text = "treeDelete control value not empty";
1290                 return LDAP_PROTOCOL_ERROR;
1291         }
1292
1293         op->o_tree_delete = ctrl->ldctl_iscritical
1294                 ? SLAP_CRITICAL_CONTROL
1295                 : SLAP_NONCRITICAL_CONTROL;
1296
1297         return LDAP_SUCCESS;
1298 }
1299 #endif
1300
1301 #ifdef LDAP_CONTORL_X_SEARCH_OPTIONS
1302 static int parseSearchOptions (
1303         Operation *op,
1304         SlapReply *rs,
1305         LDAPControl *ctrl )
1306 {
1307         BerElement *ber;
1308         ber_int_t search_flags;
1309
1310         if ( ctrl->ldctl_value.bv_len == 0 ) {
1311                 rs->sr_text = "searchOptions control value not empty";
1312                 return LDAP_PROTOCOL_ERROR;
1313         }
1314
1315         ber = ber_init( &ctrl->ldctl_value );
1316         if( ber == NULL ) {
1317                 rs->sr_text = "internal error";
1318                 return LDAP_OTHER;
1319         }
1320
1321         if ( (tag = ber_scanf( ber, "{i}", &search_flags )) == LBER_ERROR ) {
1322                 rs->sr_text = "searchOptions control decoding error";
1323                 return LDAP_PROTOCOL_ERROR;
1324         }
1325
1326         (void) ber_free( ber, 1 );
1327
1328         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1329                 if ( op->o_domain_scope != SLAP_NO_CONTROL ) {
1330                         rs->sr_text = "searchOptions control specified multiple times "
1331                                 "or with domainScope control";
1332                         return LDAP_PROTOCOL_ERROR;
1333                 }
1334
1335                 op->o_domain_scope = ctrl->ldctl_iscritical
1336                         ? SLAP_CRITICAL_CONTROL
1337                         : SLAP_NONCRITICAL_CONTROL;
1338         }
1339
1340         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1341                 /* Other search flags not recognised so far */
1342                 rs->sr_text = "searchOptions contained unrecongized flag";
1343                 return LDAP_UNWILLING_TO_PERFORM;
1344         }
1345
1346         return LDAP_SUCCESS;
1347 }
1348 #endif
1349
1350 static int parseLDAPsync (
1351         Operation *op,
1352         SlapReply *rs,
1353         LDAPControl *ctrl )
1354 {
1355         ber_tag_t tag;
1356         BerElement *ber;
1357         ber_int_t mode;
1358         ber_len_t len;
1359         struct slap_session_entry *se;
1360
1361         if ( op->o_sync != SLAP_NO_CONTROL ) {
1362                 rs->sr_text = "Sync control specified multiple times";
1363                 return LDAP_PROTOCOL_ERROR;
1364         }
1365
1366         if ( op->o_pagedresults != SLAP_NO_CONTROL ) {
1367                 rs->sr_text = "Sync control specified with pagedResults control";
1368                 return LDAP_PROTOCOL_ERROR;
1369         }
1370
1371
1372         if ( ctrl->ldctl_value.bv_len == 0 ) {
1373                 rs->sr_text = "Sync control value is empty (or absent)";
1374                 return LDAP_PROTOCOL_ERROR;
1375         }
1376
1377         /* Parse the control value
1378          *      syncRequestValue ::= SEQUENCE {
1379          *              mode   ENUMERATED {
1380          *                      -- 0 unused
1381          *                      refreshOnly             (1),
1382          *                      -- 2 reserved
1383          *                      refreshAndPersist       (3)
1384          *              },
1385          *              cookie  syncCookie OPTIONAL
1386          *      }
1387          */
1388
1389         ber = ber_init( &ctrl->ldctl_value );
1390         if( ber == NULL ) {
1391                 rs->sr_text = "internal error";
1392                 return LDAP_OTHER;
1393         }
1394
1395         if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1396                 rs->sr_text = "Sync control : mode decoding error";
1397                 return LDAP_PROTOCOL_ERROR;
1398         }
1399
1400         switch( mode ) {
1401         case LDAP_SYNC_REFRESH_ONLY:
1402                 mode = SLAP_SYNC_REFRESH;
1403                 break;
1404         case LDAP_SYNC_REFRESH_AND_PERSIST:
1405                 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1406                 break;
1407         default:
1408                 rs->sr_text = "Sync control : unknown update mode";
1409                 return LDAP_PROTOCOL_ERROR;
1410         }
1411
1412         tag = ber_peek_tag( ber, &len );
1413
1414         if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1415                 struct berval tmp_bv;   
1416                 if (( ber_scanf( ber, /*{*/ "o", &tmp_bv )) == LBER_ERROR ) {
1417                         rs->sr_text = "Sync control : cookie decoding error";
1418                         return LDAP_PROTOCOL_ERROR;
1419                 }
1420                 ber_bvarray_add( &op->o_sync_state.octet_str, &tmp_bv );
1421                 slap_parse_sync_cookie( &op->o_sync_state );
1422         }
1423         if ( tag == LDAP_TAG_RELOAD_HINT ) {
1424                 if (( ber_scanf( ber, /*{*/ "b", &op->o_sync_rhint )) == LBER_ERROR ) {
1425                         rs->sr_text = "Sync control : rhint decoding error";
1426                         return LDAP_PROTOCOL_ERROR;
1427                 }
1428         }
1429         if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1430                         rs->sr_text = "Sync control : decoding error";
1431                         return LDAP_PROTOCOL_ERROR;
1432         }
1433
1434         (void) ber_free( ber, 1 );
1435
1436         op->o_sync_mode = (char) mode;
1437
1438         op->o_sync = ctrl->ldctl_iscritical
1439                 ? SLAP_CRITICAL_CONTROL
1440                 : SLAP_NONCRITICAL_CONTROL;
1441
1442         return LDAP_SUCCESS;
1443 }