]> git.sur5r.net Git - openldap/blob - servers/slapd/controls.c
5599bd7b9d23092d2976390edb9a9297176a302a
[openldap] / servers / slapd / controls.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2018 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 #include "ldif.h"
25 #include "lutil.h"
26
27 #include "../../libraries/liblber/lber-int.h"
28
29 static SLAP_CTRL_PARSE_FN parseAssert;
30 static SLAP_CTRL_PARSE_FN parseDomainScope;
31 static SLAP_CTRL_PARSE_FN parseDontUseCopy;
32 static SLAP_CTRL_PARSE_FN parseManageDSAit;
33 static SLAP_CTRL_PARSE_FN parseNoOp;
34 static SLAP_CTRL_PARSE_FN parsePagedResults;
35 static SLAP_CTRL_PARSE_FN parsePermissiveModify;
36 static SLAP_CTRL_PARSE_FN parsePreRead, parsePostRead;
37 static SLAP_CTRL_PARSE_FN parseProxyAuthz;
38 static SLAP_CTRL_PARSE_FN parseRelax;
39 static SLAP_CTRL_PARSE_FN parseSearchOptions;
40 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
41 static SLAP_CTRL_PARSE_FN parseSortedResults;
42 #endif
43 static SLAP_CTRL_PARSE_FN parseSubentries;
44 #ifdef SLAP_CONTROL_X_TREE_DELETE
45 static SLAP_CTRL_PARSE_FN parseTreeDelete;
46 #endif
47 static SLAP_CTRL_PARSE_FN parseValuesReturnFilter;
48 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
49 static SLAP_CTRL_PARSE_FN parseSessionTracking;
50 #endif
51 #ifdef SLAP_CONTROL_X_WHATFAILED
52 static SLAP_CTRL_PARSE_FN parseWhatFailed;
53 #endif
54 #ifdef SLAP_CONTROL_X_LAZY_COMMIT
55 static SLAP_CTRL_PARSE_FN parseLazyCommit;
56 #endif
57
58 #undef sc_mask /* avoid conflict with Irix 6.5 <sys/signal.h> */
59
60 const struct berval slap_pre_read_bv = BER_BVC(LDAP_CONTROL_PRE_READ);
61 const struct berval slap_post_read_bv = BER_BVC(LDAP_CONTROL_POST_READ);
62
63 struct slap_control_ids slap_cids;
64
65 struct slap_control {
66         /* Control OID */
67         char *sc_oid;
68
69         /* The controlID for this control */
70         int sc_cid;
71
72         /* Operations supported by control */
73         slap_mask_t sc_mask;
74
75         /* Extended operations supported by control */
76         char **sc_extendedops;          /* input */
77         BerVarray sc_extendedopsbv;     /* run-time use */
78
79         /* Control parsing callback */
80         SLAP_CTRL_PARSE_FN *sc_parse;
81
82         LDAP_SLIST_ENTRY(slap_control) sc_next;
83 };
84
85 static LDAP_SLIST_HEAD(ControlsList, slap_control) controls_list
86         = LDAP_SLIST_HEAD_INITIALIZER(&controls_list);
87
88 /*
89  * all known request control OIDs should be added to this list
90  */
91 /*
92  * NOTE: initialize num_known_controls to 1 so that cid = 0 always
93  * addresses an undefined control; this allows to safely test for 
94  * well known controls even if they are not registered, e.g. if 
95  * they get moved to modules.  An example is sc_LDAPsync, which 
96  * is implemented in the syncprov overlay and thus, if configured 
97  * as dynamic module, may not be registered.  One side effect is that 
98  * slap_known_controls[0] == NULL, so it should always be used 
99  * starting from 1.
100  * FIXME: should we define the "undefined control" oid?
101  */
102 char *slap_known_controls[SLAP_MAX_CIDS+1];
103 static int num_known_controls = 1;
104
105 static char *proxy_authz_extops[] = {
106         LDAP_EXOP_MODIFY_PASSWD,
107         LDAP_EXOP_WHO_AM_I,
108         LDAP_EXOP_REFRESH,
109         NULL
110 };
111
112 static char *manageDSAit_extops[] = {
113         LDAP_EXOP_REFRESH,
114         NULL
115 };
116
117 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
118 static char *session_tracking_extops[] = {
119         LDAP_EXOP_MODIFY_PASSWD,
120         LDAP_EXOP_WHO_AM_I,
121         LDAP_EXOP_REFRESH,
122         NULL
123 };
124 #endif
125
126 static struct slap_control control_defs[] = {
127         {  LDAP_CONTROL_ASSERT,
128                 (int)offsetof(struct slap_control_ids, sc_assert),
129                 SLAP_CTRL_UPDATE|SLAP_CTRL_COMPARE|SLAP_CTRL_SEARCH,
130                 NULL, NULL,
131                 parseAssert, LDAP_SLIST_ENTRY_INITIALIZER(next) },
132         { LDAP_CONTROL_PRE_READ,
133                 (int)offsetof(struct slap_control_ids, sc_preRead),
134                 SLAP_CTRL_DELETE|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
135                 NULL, NULL,
136                 parsePreRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
137         { LDAP_CONTROL_POST_READ,
138                 (int)offsetof(struct slap_control_ids, sc_postRead),
139                 SLAP_CTRL_ADD|SLAP_CTRL_MODIFY|SLAP_CTRL_RENAME,
140                 NULL, NULL,
141                 parsePostRead, LDAP_SLIST_ENTRY_INITIALIZER(next) },
142         { LDAP_CONTROL_VALUESRETURNFILTER,
143                 (int)offsetof(struct slap_control_ids, sc_valuesReturnFilter),
144                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH,
145                 NULL, NULL,
146                 parseValuesReturnFilter, LDAP_SLIST_ENTRY_INITIALIZER(next) },
147         { LDAP_CONTROL_PAGEDRESULTS,
148                 (int)offsetof(struct slap_control_ids, sc_pagedResults),
149                 SLAP_CTRL_SEARCH,
150                 NULL, NULL,
151                 parsePagedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
152 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
153         { LDAP_CONTROL_SORTREQUEST,
154                 (int)offsetof(struct slap_control_ids, sc_sortedResults),
155                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
156                 NULL, NULL,
157                 parseSortedResults, LDAP_SLIST_ENTRY_INITIALIZER(next) },
158 #endif
159         { LDAP_CONTROL_X_DOMAIN_SCOPE,
160                 (int)offsetof(struct slap_control_ids, sc_domainScope),
161                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
162                 NULL, NULL,
163                 parseDomainScope, LDAP_SLIST_ENTRY_INITIALIZER(next) },
164         { LDAP_CONTROL_DONTUSECOPY,
165                 (int)offsetof(struct slap_control_ids, sc_dontUseCopy),
166                 SLAP_CTRL_GLOBAL|SLAP_CTRL_INTROGATE,
167                 NULL, NULL,
168                 parseDontUseCopy, LDAP_SLIST_ENTRY_INITIALIZER(next) },
169         { LDAP_CONTROL_X_PERMISSIVE_MODIFY,
170                 (int)offsetof(struct slap_control_ids, sc_permissiveModify),
171                 SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE,
172                 NULL, NULL,
173                 parsePermissiveModify, LDAP_SLIST_ENTRY_INITIALIZER(next) },
174 #ifdef SLAP_CONTROL_X_TREE_DELETE
175         { LDAP_CONTROL_X_TREE_DELETE,
176                 (int)offsetof(struct slap_control_ids, sc_treeDelete),
177                 SLAP_CTRL_DELETE|SLAP_CTRL_HIDE,
178                 NULL, NULL,
179                 parseTreeDelete, LDAP_SLIST_ENTRY_INITIALIZER(next) },
180 #endif
181         { LDAP_CONTROL_X_SEARCH_OPTIONS,
182                 (int)offsetof(struct slap_control_ids, sc_searchOptions),
183                 SLAP_CTRL_GLOBAL|SLAP_CTRL_SEARCH|SLAP_CTRL_HIDE,
184                 NULL, NULL,
185                 parseSearchOptions, LDAP_SLIST_ENTRY_INITIALIZER(next) },
186         { LDAP_CONTROL_SUBENTRIES,
187                 (int)offsetof(struct slap_control_ids, sc_subentries),
188                 SLAP_CTRL_SEARCH,
189                 NULL, NULL,
190                 parseSubentries, LDAP_SLIST_ENTRY_INITIALIZER(next) },
191         { LDAP_CONTROL_NOOP,
192                 (int)offsetof(struct slap_control_ids, sc_noOp),
193                 SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
194                 NULL, NULL,
195                 parseNoOp, LDAP_SLIST_ENTRY_INITIALIZER(next) },
196         { LDAP_CONTROL_RELAX,
197                 (int)offsetof(struct slap_control_ids, sc_relax),
198                 SLAP_CTRL_GLOBAL|SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
199                 NULL, NULL,
200                 parseRelax, LDAP_SLIST_ENTRY_INITIALIZER(next) },
201 #ifdef LDAP_X_TXN
202         { LDAP_CONTROL_X_TXN_SPEC,
203                 (int)offsetof(struct slap_control_ids, sc_txnSpec),
204                 SLAP_CTRL_UPDATE|SLAP_CTRL_HIDE,
205                 NULL, NULL,
206                 txn_spec_ctrl, LDAP_SLIST_ENTRY_INITIALIZER(next) },
207 #endif
208         { LDAP_CONTROL_MANAGEDSAIT,
209                 (int)offsetof(struct slap_control_ids, sc_manageDSAit),
210                 SLAP_CTRL_ACCESS,
211                 manageDSAit_extops, NULL,
212                 parseManageDSAit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
213         { LDAP_CONTROL_PROXY_AUTHZ,
214                 (int)offsetof(struct slap_control_ids, sc_proxyAuthz),
215                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS,
216                 proxy_authz_extops, NULL,
217                 parseProxyAuthz, LDAP_SLIST_ENTRY_INITIALIZER(next) },
218 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
219         { LDAP_CONTROL_X_SESSION_TRACKING,
220                 (int)offsetof(struct slap_control_ids, sc_sessionTracking),
221                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_BIND|SLAP_CTRL_HIDE,
222                 session_tracking_extops, NULL,
223                 parseSessionTracking, LDAP_SLIST_ENTRY_INITIALIZER(next) },
224 #endif
225 #ifdef SLAP_CONTROL_X_WHATFAILED
226         { LDAP_CONTROL_X_WHATFAILED,
227                 (int)offsetof(struct slap_control_ids, sc_whatFailed),
228                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
229                 NULL, NULL,
230                 parseWhatFailed, LDAP_SLIST_ENTRY_INITIALIZER(next) },
231 #endif
232 #ifdef SLAP_CONTROL_X_LAZY_COMMIT
233         { LDAP_CONTROL_X_LAZY_COMMIT,
234                 (int)offsetof(struct slap_control_ids, sc_lazyCommit),
235                 SLAP_CTRL_GLOBAL|SLAP_CTRL_ACCESS|SLAP_CTRL_HIDE,
236                 NULL, NULL,
237                 parseLazyCommit, LDAP_SLIST_ENTRY_INITIALIZER(next) },
238 #endif
239
240         { NULL, 0, 0, NULL, 0, NULL, LDAP_SLIST_ENTRY_INITIALIZER(next) }
241 };
242
243 static struct slap_control *
244 find_ctrl( const char *oid );
245
246 /*
247  * Register a supported control.
248  *
249  * This can be called by an OpenLDAP plugin or, indirectly, by a
250  * SLAPI plugin calling slapi_register_supported_control().
251  *
252  * NOTE: if flags == 1 the control is replaced if already registered;
253  * otherwise registering an already registered control is not allowed.
254  */
255 int
256 register_supported_control2(const char *controloid,
257         slap_mask_t controlmask,
258         char **controlexops,
259         SLAP_CTRL_PARSE_FN *controlparsefn,
260         unsigned flags,
261         int *controlcid)
262 {
263         struct slap_control *sc = NULL;
264         int i;
265         BerVarray extendedopsbv = NULL;
266
267         if ( num_known_controls >= SLAP_MAX_CIDS ) {
268                 Debug( LDAP_DEBUG_ANY, "Too many controls registered."
269                         " Recompile slapd with SLAP_MAX_CIDS defined > %d\n",
270                 SLAP_MAX_CIDS, 0, 0 );
271                 return LDAP_OTHER;
272         }
273
274         if ( controloid == NULL ) {
275                 return LDAP_PARAM_ERROR;
276         }
277
278         /* check if already registered */
279         for ( i = 0; slap_known_controls[ i ]; i++ ) {
280                 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
281                         if ( flags == 1 ) {
282                                 Debug( LDAP_DEBUG_TRACE,
283                                         "Control %s already registered; replacing.\n",
284                                         controloid, 0, 0 );
285                                 /* (find and) replace existing handler */
286                                 sc = find_ctrl( controloid );
287                                 assert( sc != NULL );
288                                 break;
289                         }
290
291                         Debug( LDAP_DEBUG_ANY,
292                                 "Control %s already registered.\n",
293                                 controloid, 0, 0 );
294                         return LDAP_PARAM_ERROR;
295                 }
296         }
297
298         /* turn compatible extended operations into bervals */
299         if ( controlexops != NULL ) {
300                 int i;
301
302                 for ( i = 0; controlexops[ i ]; i++ );
303
304                 extendedopsbv = ber_memcalloc( i + 1, sizeof( struct berval ) );
305                 if ( extendedopsbv == NULL ) {
306                         return LDAP_NO_MEMORY;
307                 }
308
309                 for ( i = 0; controlexops[ i ]; i++ ) {
310                         ber_str2bv( controlexops[ i ], 0, 1, &extendedopsbv[ i ] );
311                 }
312         }
313
314         if ( sc == NULL ) {
315                 sc = (struct slap_control *)SLAP_MALLOC( sizeof( *sc ) );
316                 if ( sc == NULL ) {
317                         ber_bvarray_free( extendedopsbv );
318                         return LDAP_NO_MEMORY;
319                 }
320
321                 sc->sc_oid = ch_strdup( controloid );
322                 sc->sc_cid = num_known_controls;
323
324                 /* Update slap_known_controls, too. */
325                 slap_known_controls[num_known_controls - 1] = sc->sc_oid;
326                 slap_known_controls[num_known_controls++] = NULL;
327
328                 LDAP_SLIST_NEXT( sc, sc_next ) = NULL;
329                 LDAP_SLIST_INSERT_HEAD( &controls_list, sc, sc_next );
330
331         } else {
332                 if ( sc->sc_extendedopsbv ) {
333                         /* FIXME: in principle, we should rather merge
334                          * existing extops with those supported by the
335                          * new control handling implementation.
336                          * In fact, whether a control is compatible with
337                          * an extop should not be a matter of implementation.
338                          * We likely also need a means for a newly
339                          * registered extop to declare that it is
340                          * comptible with an already registered control.
341                          */
342                         ber_bvarray_free( sc->sc_extendedopsbv );
343                         sc->sc_extendedopsbv = NULL;
344                         sc->sc_extendedops = NULL;
345                 }
346         }
347
348         sc->sc_extendedopsbv = extendedopsbv;
349         sc->sc_mask = controlmask;
350         sc->sc_parse = controlparsefn;
351         if ( controlcid ) {
352                 *controlcid = sc->sc_cid;
353         }
354
355         return LDAP_SUCCESS;
356 }
357
358 #ifdef SLAP_CONFIG_DELETE
359 int
360 unregister_supported_control( const char *controloid )
361 {
362         struct slap_control *sc;
363         int i;
364
365         if ( controloid == NULL || (sc = find_ctrl( controloid )) == NULL ){
366                 return -1;
367         }
368
369         for ( i = 0; slap_known_controls[ i ]; i++ ) {
370                 if ( strcmp( controloid, slap_known_controls[ i ] ) == 0 ) {
371                         do {
372                                 slap_known_controls[ i ] = slap_known_controls[ i+1 ];
373                         } while ( slap_known_controls[ i++ ] );
374                         num_known_controls--;
375                         break;
376                 }
377         }
378
379         LDAP_SLIST_REMOVE(&controls_list, sc, slap_control, sc_next);
380         ch_free( sc->sc_oid );
381         if ( sc->sc_extendedopsbv != NULL ) {
382                 ber_bvarray_free( sc->sc_extendedopsbv );
383         }
384         ch_free( sc );
385
386         return 0;
387 }
388 #endif /* SLAP_CONFIG_DELETE */
389
390 /*
391  * One-time initialization of internal controls.
392  */
393 int
394 slap_controls_init( void )
395 {
396         int i, rc;
397
398         rc = LDAP_SUCCESS;
399
400         for ( i = 0; control_defs[i].sc_oid != NULL; i++ ) {
401                 int *cid = (int *)(((char *)&slap_cids) + control_defs[i].sc_cid );
402                 rc = register_supported_control( control_defs[i].sc_oid,
403                         control_defs[i].sc_mask, control_defs[i].sc_extendedops,
404                         control_defs[i].sc_parse, cid );
405                 if ( rc != LDAP_SUCCESS ) break;
406         }
407
408         return rc;
409 }
410
411 /*
412  * Free memory associated with list of supported controls.
413  */
414 void
415 controls_destroy( void )
416 {
417         struct slap_control *sc;
418
419         while ( !LDAP_SLIST_EMPTY(&controls_list) ) {
420                 sc = LDAP_SLIST_FIRST(&controls_list);
421                 LDAP_SLIST_REMOVE_HEAD(&controls_list, sc_next);
422
423                 ch_free( sc->sc_oid );
424                 if ( sc->sc_extendedopsbv != NULL ) {
425                         ber_bvarray_free( sc->sc_extendedopsbv );
426                 }
427                 ch_free( sc );
428         }
429 }
430
431 /*
432  * Format the supportedControl attribute of the root DSE,
433  * detailing which controls are supported by the directory
434  * server.
435  */
436 int
437 controls_root_dse_info( Entry *e )
438 {
439         AttributeDescription *ad_supportedControl
440                 = slap_schema.si_ad_supportedControl;
441         struct berval vals[2];
442         struct slap_control *sc;
443
444         vals[1].bv_val = NULL;
445         vals[1].bv_len = 0;
446
447         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
448                 if( sc->sc_mask & SLAP_CTRL_HIDE ) continue;
449
450                 vals[0].bv_val = sc->sc_oid;
451                 vals[0].bv_len = strlen( sc->sc_oid );
452
453                 if ( attr_merge( e, ad_supportedControl, vals, NULL ) ) {
454                         return -1;
455                 }
456         }
457
458         return 0;
459 }
460
461 /*
462  * Return a list of OIDs and operation masks for supported
463  * controls. Used by SLAPI.
464  */
465 int
466 get_supported_controls(char ***ctrloidsp,
467         slap_mask_t **ctrlmasks)
468 {
469         int n;
470         char **oids;
471         slap_mask_t *masks;
472         struct slap_control *sc;
473
474         n = 0;
475
476         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
477                 n++;
478         }
479
480         if ( n == 0 ) {
481                 *ctrloidsp = NULL;
482                 *ctrlmasks = NULL;
483                 return LDAP_SUCCESS;
484         }
485
486         oids = (char **)SLAP_MALLOC( (n + 1) * sizeof(char *) );
487         if ( oids == NULL ) {
488                 return LDAP_NO_MEMORY;
489         }
490         masks = (slap_mask_t *)SLAP_MALLOC( (n + 1) * sizeof(slap_mask_t) );
491         if  ( masks == NULL ) {
492                 SLAP_FREE( oids );
493                 return LDAP_NO_MEMORY;
494         }
495
496         n = 0;
497
498         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
499                 oids[n] = ch_strdup( sc->sc_oid );
500                 masks[n] = sc->sc_mask;
501                 n++;
502         }
503         oids[n] = NULL;
504         masks[n] = 0;
505
506         *ctrloidsp = oids;
507         *ctrlmasks = masks;
508
509         return LDAP_SUCCESS;
510 }
511
512 /*
513  * Find a control given its OID.
514  */
515 static struct slap_control *
516 find_ctrl( const char *oid )
517 {
518         struct slap_control *sc;
519
520         LDAP_SLIST_FOREACH( sc, &controls_list, sc_next ) {
521                 if ( strcmp( oid, sc->sc_oid ) == 0 ) {
522                         return sc;
523                 }
524         }
525
526         return NULL;
527 }
528
529 int
530 slap_find_control_id(
531         const char *oid,
532         int *cid )
533 {
534         struct slap_control *ctrl = find_ctrl( oid );
535         if ( ctrl ) {
536                 if ( cid ) *cid = ctrl->sc_cid;
537                 return LDAP_SUCCESS;
538         }
539         return LDAP_CONTROL_NOT_FOUND;
540 }
541
542 int
543 slap_global_control( Operation *op, const char *oid, int *cid )
544 {
545         struct slap_control *ctrl = find_ctrl( oid );
546
547         if ( ctrl == NULL ) {
548                 /* should not be reachable */
549                 Debug( LDAP_DEBUG_ANY,
550                         "slap_global_control: unrecognized control: %s\n",      
551                         oid, 0, 0 );
552                 return LDAP_CONTROL_NOT_FOUND;
553         }
554
555         if ( cid ) *cid = ctrl->sc_cid;
556
557         if ( ( ctrl->sc_mask & SLAP_CTRL_GLOBAL ) ||
558                 ( ( op->o_tag & LDAP_REQ_SEARCH ) &&
559                 ( ctrl->sc_mask & SLAP_CTRL_GLOBAL_SEARCH ) ) )
560         {
561                 return LDAP_COMPARE_TRUE;
562         }
563
564 #if 0
565         Debug( LDAP_DEBUG_TRACE,
566                 "slap_global_control: unavailable control: %s\n",      
567                 oid, 0, 0 );
568 #endif
569
570         return LDAP_COMPARE_FALSE;
571 }
572
573 void slap_free_ctrls(
574         Operation *op,
575         LDAPControl **ctrls )
576 {
577         int i;
578
579         if( ctrls == op->o_ctrls ) {
580                 if( op->o_assertion != NULL ) {
581                         filter_free_x( op, op->o_assertion, 1 );
582                         op->o_assertion = NULL;
583                 }
584                 if( op->o_vrFilter != NULL) {
585                         vrFilter_free( op, op->o_vrFilter );
586                         op->o_vrFilter = NULL;
587                 }
588                 if( op->o_preread_attrs != NULL ) {
589                         op->o_tmpfree( op->o_preread_attrs, op->o_tmpmemctx );
590                         op->o_preread_attrs = NULL;
591                 }
592                 if( op->o_postread_attrs != NULL ) {
593                         op->o_tmpfree( op->o_postread_attrs, op->o_tmpmemctx );
594                         op->o_postread_attrs = NULL;
595                 }
596                 if( op->o_pagedresults_state != NULL ) {
597                         op->o_tmpfree( op->o_pagedresults_state, op->o_tmpmemctx );
598                         op->o_pagedresults_state = NULL;
599                 }
600         }
601
602         for (i=0; ctrls[i]; i++) {
603                 op->o_tmpfree(ctrls[i], op->o_tmpmemctx );
604         }
605         op->o_tmpfree( ctrls, op->o_tmpmemctx );
606 }
607
608 int slap_add_ctrls(
609         Operation *op,
610         SlapReply *rs,
611         LDAPControl **ctrls )
612 {
613         int i = 0, j;
614         LDAPControl **ctrlsp;
615
616         if ( rs->sr_ctrls ) {
617                 for ( ; rs->sr_ctrls[ i ]; i++ ) ;
618         }
619
620         for ( j=0; ctrls[j]; j++ ) ;
621
622         ctrlsp = op->o_tmpalloc(( i+j+1 )*sizeof(LDAPControl *), op->o_tmpmemctx );
623         i = 0;
624         if ( rs->sr_ctrls ) {
625                 for ( ; rs->sr_ctrls[i]; i++ )
626                         ctrlsp[i] = rs->sr_ctrls[i];
627         }
628         for ( j=0; ctrls[j]; j++)
629                 ctrlsp[i++] = ctrls[j];
630         ctrlsp[i] = NULL;
631
632         if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED )
633                 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
634         rs->sr_ctrls = ctrlsp;
635         rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
636         return i;
637 }
638
639 int slap_parse_ctrl(
640         Operation *op,
641         SlapReply *rs,
642         LDAPControl *control,
643         const char **text )
644 {
645         struct slap_control *sc;
646         int rc = LDAP_SUCCESS;
647
648         sc = find_ctrl( control->ldctl_oid );
649         if( sc != NULL ) {
650                 /* recognized control */
651                 slap_mask_t tagmask;
652                 switch( op->o_tag ) {
653                 case LDAP_REQ_ADD:
654                         tagmask = SLAP_CTRL_ADD;
655                         break;
656                 case LDAP_REQ_BIND:
657                         tagmask = SLAP_CTRL_BIND;
658                         break;
659                 case LDAP_REQ_COMPARE:
660                         tagmask = SLAP_CTRL_COMPARE;
661                         break;
662                 case LDAP_REQ_DELETE:
663                         tagmask = SLAP_CTRL_DELETE;
664                         break;
665                 case LDAP_REQ_MODIFY:
666                         tagmask = SLAP_CTRL_MODIFY;
667                         break;
668                 case LDAP_REQ_RENAME:
669                         tagmask = SLAP_CTRL_RENAME;
670                         break;
671                 case LDAP_REQ_SEARCH:
672                         tagmask = SLAP_CTRL_SEARCH;
673                         break;
674                 case LDAP_REQ_UNBIND:
675                         tagmask = SLAP_CTRL_UNBIND;
676                         break;
677                 case LDAP_REQ_ABANDON:
678                         tagmask = SLAP_CTRL_ABANDON;
679                         break;
680                 case LDAP_REQ_EXTENDED:
681                         tagmask=~0L;
682                         assert( op->ore_reqoid.bv_val != NULL );
683                         if( sc->sc_extendedopsbv != NULL ) {
684                                 int i;
685                                 for( i=0; !BER_BVISNULL( &sc->sc_extendedopsbv[i] ); i++ ) {
686                                         if( bvmatch( &op->ore_reqoid,
687                                                 &sc->sc_extendedopsbv[i] ) )
688                                         {
689                                                 tagmask=0L;
690                                                 break;
691                                         }
692                                 }
693                         }
694                         break;
695                 default:
696                         *text = "controls internal error";
697                         return LDAP_OTHER;
698                 }
699
700                 if (( sc->sc_mask & tagmask ) == tagmask ) {
701                         /* available extension */
702                         if ( sc->sc_parse ) {
703                                 rc = sc->sc_parse( op, rs, control );
704                                 assert( rc != LDAP_UNAVAILABLE_CRITICAL_EXTENSION );
705
706                         } else if ( control->ldctl_iscritical ) {
707                                 *text = "not yet implemented";
708                                 rc = LDAP_OTHER;
709                         }
710
711
712                 } else if ( control->ldctl_iscritical ) {
713                         /* unavailable CRITICAL control */
714                         *text = "critical extension is unavailable";
715                         rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
716                 }
717
718         } else if ( control->ldctl_iscritical ) {
719                 /* unrecognized CRITICAL control */
720                 *text = "critical extension is not recognized";
721                 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
722         }
723
724         return rc;
725 }
726
727 int
728 get_ctrls(
729         Operation *op,
730         SlapReply *rs,
731         int sendres )
732 {
733         return get_ctrls2( op, rs, sendres, LDAP_TAG_CONTROLS );
734 }
735
736 int
737 get_ctrls2(
738         Operation *op,
739         SlapReply *rs,
740         int sendres,
741         ber_tag_t ctag )
742 {
743         int nctrls = 0;
744         ber_tag_t tag;
745         ber_len_t len;
746         char *opaque;
747         BerElement *ber = op->o_ber;
748         struct berval bv;
749 #ifdef SLAP_CONTROL_X_WHATFAILED
750         /* NOTE: right now, slapd checks the validity of each control
751          * while parsing.  As a consequence, it can only detect one
752          * cause of failure at a time.  This results in returning
753          * exactly one OID with the whatFailed control, or no control
754          * at all.
755          */
756         char *failed_oid = NULL;
757 #endif
758
759         len = ber_pvt_ber_remaining(ber);
760
761         if( len == 0) {
762                 /* no controls */
763                 rs->sr_err = LDAP_SUCCESS;
764                 return rs->sr_err;
765         }
766
767         if(( tag = ber_peek_tag( ber, &len )) != ctag ) {
768                 if( tag == LBER_ERROR ) {
769                         rs->sr_err = SLAPD_DISCONNECT;
770                         rs->sr_text = "unexpected data in PDU";
771                 }
772
773                 goto return_results;
774         }
775
776         Debug( LDAP_DEBUG_TRACE,
777                 "=> get_ctrls\n", 0, 0, 0 );
778
779         if( op->o_protocol < LDAP_VERSION3 ) {
780                 rs->sr_err = SLAPD_DISCONNECT;
781                 rs->sr_text = "controls require LDAPv3";
782                 goto return_results;
783         }
784
785         /* one for first control, one for termination */
786         op->o_ctrls = op->o_tmpalloc( 2 * sizeof(LDAPControl *), op->o_tmpmemctx );
787
788 #if 0
789         if( op->ctrls == NULL ) {
790                 rs->sr_err = LDAP_NO_MEMORY;
791                 rs->sr_text = "no memory";
792                 goto return_results;
793         }
794 #endif
795
796         op->o_ctrls[nctrls] = NULL;
797
798         /* step through each element */
799         for( tag = ber_first_element( ber, &len, &opaque );
800                 tag != LBER_ERROR;
801                 tag = ber_next_element( ber, &len, opaque ) )
802         {
803                 LDAPControl *c;
804                 LDAPControl **tctrls;
805
806                 c = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
807                 memset(c, 0, sizeof(LDAPControl));
808
809                 /* allocate pointer space for current controls (nctrls)
810                  * + this control + extra NULL
811                  */
812                 tctrls = op->o_tmprealloc( op->o_ctrls,
813                         (nctrls+2) * sizeof(LDAPControl *), op->o_tmpmemctx );
814
815 #if 0
816                 if( tctrls == NULL ) {
817                         ch_free( c );
818                         ldap_controls_free(op->o_ctrls);
819                         op->o_ctrls = NULL;
820
821                         rs->sr_err = LDAP_NO_MEMORY;
822                         rs->sr_text = "no memory";
823                         goto return_results;
824                 }
825 #endif
826                 op->o_ctrls = tctrls;
827
828                 op->o_ctrls[nctrls++] = c;
829                 op->o_ctrls[nctrls] = NULL;
830
831                 tag = ber_scanf( ber, "{m" /*}*/, &bv );
832                 c->ldctl_oid = bv.bv_val;
833
834                 if( tag == LBER_ERROR ) {
835                         Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get oid failed.\n",
836                                 0, 0, 0 );
837
838                         slap_free_ctrls( op, op->o_ctrls );
839                         op->o_ctrls = NULL;
840                         rs->sr_err = SLAPD_DISCONNECT;
841                         rs->sr_text = "decoding controls error";
842                         goto return_results;
843
844                 } else if( c->ldctl_oid == NULL ) {
845                         Debug( LDAP_DEBUG_TRACE,
846                                 "get_ctrls: conn %lu got empty OID.\n",
847                                 op->o_connid, 0, 0 );
848
849                         slap_free_ctrls( op, op->o_ctrls );
850                         op->o_ctrls = NULL;
851                         rs->sr_err = LDAP_PROTOCOL_ERROR;
852                         rs->sr_text = "OID field is empty";
853                         goto return_results;
854                 }
855
856                 tag = ber_peek_tag( ber, &len );
857
858                 if( tag == LBER_BOOLEAN ) {
859                         ber_int_t crit;
860                         tag = ber_scanf( ber, "b", &crit );
861
862                         if( tag == LBER_ERROR ) {
863                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: get crit failed.\n",
864                                         0, 0, 0 );
865                                 slap_free_ctrls( op, op->o_ctrls );
866                                 op->o_ctrls = NULL;
867                                 rs->sr_err = SLAPD_DISCONNECT;
868                                 rs->sr_text = "decoding controls error";
869                                 goto return_results;
870                         }
871
872                         c->ldctl_iscritical = (crit != 0);
873                         tag = ber_peek_tag( ber, &len );
874                 }
875
876                 if( tag == LBER_OCTETSTRING ) {
877                         tag = ber_scanf( ber, "m", &c->ldctl_value );
878
879                         if( tag == LBER_ERROR ) {
880                                 Debug( LDAP_DEBUG_TRACE, "=> get_ctrls: conn %lu: "
881                                         "%s (%scritical): get value failed.\n",
882                                         op->o_connid, c->ldctl_oid,
883                                         c->ldctl_iscritical ? "" : "non" );
884                                 slap_free_ctrls( op, op->o_ctrls );
885                                 op->o_ctrls = NULL;
886                                 rs->sr_err = SLAPD_DISCONNECT;
887                                 rs->sr_text = "decoding controls error";
888                                 goto return_results;
889                         }
890                 }
891
892                 Debug( LDAP_DEBUG_TRACE,
893                         "=> get_ctrls: oid=\"%s\" (%scritical)\n",
894                         c->ldctl_oid, c->ldctl_iscritical ? "" : "non", 0 );
895
896                 rs->sr_err = slap_parse_ctrl( op, rs, c, &rs->sr_text );
897                 if ( rs->sr_err != LDAP_SUCCESS ) {
898 #ifdef SLAP_CONTROL_X_WHATFAILED
899                         failed_oid = c->ldctl_oid;
900 #endif
901                         goto return_results;
902                 }
903         }
904
905 return_results:
906         Debug( LDAP_DEBUG_TRACE,
907                 "<= get_ctrls: n=%d rc=%d err=\"%s\"\n",
908                 nctrls, rs->sr_err, rs->sr_text ? rs->sr_text : "");
909
910         if( sendres && rs->sr_err != LDAP_SUCCESS ) {
911                 if( rs->sr_err == SLAPD_DISCONNECT ) {
912                         rs->sr_err = LDAP_PROTOCOL_ERROR;
913                         send_ldap_disconnect( op, rs );
914                         rs->sr_err = SLAPD_DISCONNECT;
915                 } else {
916 #ifdef SLAP_CONTROL_X_WHATFAILED
917                         /* might have not been parsed yet? */
918                         if ( failed_oid != NULL ) {
919                                 if ( !get_whatFailed( op ) ) {
920                                         /* look it up */
921
922                                         /* step through each remaining element */
923                                         for ( ; tag != LBER_ERROR; tag = ber_next_element( ber, &len, opaque ) )
924                                         {
925                                                 LDAPControl c = { 0 };
926
927                                                 tag = ber_scanf( ber, "{m" /*}*/, &bv );
928                                                 c.ldctl_oid = bv.bv_val;
929
930                                                 if ( tag == LBER_ERROR ) {
931                                                         slap_free_ctrls( op, op->o_ctrls );
932                                                         op->o_ctrls = NULL;
933                                                         break;
934
935                                                 } else if ( c.ldctl_oid == NULL ) {
936                                                         slap_free_ctrls( op, op->o_ctrls );
937                                                         op->o_ctrls = NULL;
938                                                         break;
939                                                 }
940
941                                                 tag = ber_peek_tag( ber, &len );
942                                                 if ( tag == LBER_BOOLEAN ) {
943                                                         ber_int_t crit;
944                                                         tag = ber_scanf( ber, "b", &crit );
945                                                         if( tag == LBER_ERROR ) {
946                                                                 slap_free_ctrls( op, op->o_ctrls );
947                                                                 op->o_ctrls = NULL;
948                                                                 break;
949                                                         }
950
951                                                         tag = ber_peek_tag( ber, &len );
952                                                 }
953
954                                                 if ( tag == LBER_OCTETSTRING ) {
955                                                         tag = ber_scanf( ber, "m", &c.ldctl_value );
956
957                                                         if( tag == LBER_ERROR ) {
958                                                                 slap_free_ctrls( op, op->o_ctrls );
959                                                                 op->o_ctrls = NULL;
960                                                                 break;
961                                                         }
962                                                 }
963
964                                                 if ( strcmp( c.ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) == 0 ) {
965                                                         const char *text;
966                                                         slap_parse_ctrl( op, rs, &c, &text );
967                                                         break;
968                                                 }
969                                         }
970                                 }
971
972                                 if ( get_whatFailed( op ) ) {
973                                         char *oids[ 2 ];
974                                         oids[ 0 ] = failed_oid;
975                                         oids[ 1 ] = NULL;
976                                         slap_ctrl_whatFailed_add( op, rs, oids );
977                                 }
978                         }
979 #endif
980
981                         send_ldap_result( op, rs );
982                 }
983         }
984
985         return rs->sr_err;
986 }
987
988 int
989 slap_remove_control(
990         Operation       *op,
991         SlapReply       *rs,
992         int             ctrl,
993         BI_chk_controls fnc )
994 {
995         int             i, j;
996
997         switch ( op->o_ctrlflag[ ctrl ] ) {
998         case SLAP_CONTROL_NONCRITICAL:
999                 for ( i = 0, j = -1; op->o_ctrls[ i ] != NULL; i++ ) {
1000                         if ( strcmp( op->o_ctrls[ i ]->ldctl_oid,
1001                                 slap_known_controls[ ctrl - 1 ] ) == 0 )
1002                         {
1003                                 j = i;
1004                         }
1005                 }
1006
1007                 if ( j == -1 ) {
1008                         rs->sr_err = LDAP_OTHER;
1009                         break;
1010                 }
1011
1012                 if ( fnc ) {
1013                         (void)fnc( op, rs );
1014                 }
1015
1016                 op->o_tmpfree( op->o_ctrls[ j ], op->o_tmpmemctx );
1017
1018                 if ( i > 1 ) {
1019                         AC_MEMCPY( &op->o_ctrls[ j ], &op->o_ctrls[ j + 1 ],
1020                                 ( i - j ) * sizeof( LDAPControl * ) );
1021
1022                 } else {
1023                         op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
1024                         op->o_ctrls = NULL;
1025                 }
1026
1027                 op->o_ctrlflag[ ctrl ] = SLAP_CONTROL_IGNORED;
1028
1029                 Debug( LDAP_DEBUG_ANY, "%s: "
1030                         "non-critical control \"%s\" not supported; stripped.\n",
1031                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1032                 /* fall thru */
1033
1034         case SLAP_CONTROL_IGNORED:
1035         case SLAP_CONTROL_NONE:
1036                 rs->sr_err = SLAP_CB_CONTINUE;
1037                 break;
1038
1039         case SLAP_CONTROL_CRITICAL:
1040                 rs->sr_err = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
1041                 if ( fnc ) {
1042                         (void)fnc( op, rs );
1043                 }
1044                 Debug( LDAP_DEBUG_ANY, "%s: "
1045                         "critical control \"%s\" not supported.\n",
1046                         op->o_log_prefix, slap_known_controls[ ctrl ], 0 );
1047                 break;
1048
1049         default:
1050                 /* handle all cases! */
1051                 assert( 0 );
1052         }
1053
1054         return rs->sr_err;
1055 }
1056
1057 static int parseDontUseCopy (
1058         Operation *op,
1059         SlapReply *rs,
1060         LDAPControl *ctrl )
1061 {
1062         if ( op->o_dontUseCopy != SLAP_CONTROL_NONE ) {
1063                 rs->sr_text = "dontUseCopy control specified multiple times";
1064                 return LDAP_PROTOCOL_ERROR;
1065         }
1066
1067         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1068                 rs->sr_text = "dontUseCopy control value not absent";
1069                 return LDAP_PROTOCOL_ERROR;
1070         }
1071
1072         if ( ( global_disallows & SLAP_DISALLOW_DONTUSECOPY_N_CRIT )
1073                 && !ctrl->ldctl_iscritical )
1074         {
1075                 rs->sr_text = "dontUseCopy criticality of FALSE not allowed";
1076                 return LDAP_PROTOCOL_ERROR;
1077         }
1078
1079         op->o_dontUseCopy = ctrl->ldctl_iscritical
1080                 ? SLAP_CONTROL_CRITICAL
1081                 : SLAP_CONTROL_NONCRITICAL;
1082
1083         return LDAP_SUCCESS;
1084 }
1085
1086 static int parseRelax (
1087         Operation *op,
1088         SlapReply *rs,
1089         LDAPControl *ctrl )
1090 {
1091         if ( op->o_relax != SLAP_CONTROL_NONE ) {
1092                 rs->sr_text = "relax control specified multiple times";
1093                 return LDAP_PROTOCOL_ERROR;
1094         }
1095
1096         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1097                 rs->sr_text = "relax control value not absent";
1098                 return LDAP_PROTOCOL_ERROR;
1099         }
1100
1101         op->o_relax = ctrl->ldctl_iscritical
1102                 ? SLAP_CONTROL_CRITICAL
1103                 : SLAP_CONTROL_NONCRITICAL;
1104
1105         return LDAP_SUCCESS;
1106 }
1107
1108 static int parseManageDSAit (
1109         Operation *op,
1110         SlapReply *rs,
1111         LDAPControl *ctrl )
1112 {
1113         if ( op->o_managedsait != SLAP_CONTROL_NONE ) {
1114                 rs->sr_text = "manageDSAit control specified multiple times";
1115                 return LDAP_PROTOCOL_ERROR;
1116         }
1117
1118         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1119                 rs->sr_text = "manageDSAit control value not absent";
1120                 return LDAP_PROTOCOL_ERROR;
1121         }
1122
1123         op->o_managedsait = ctrl->ldctl_iscritical
1124                 ? SLAP_CONTROL_CRITICAL
1125                 : SLAP_CONTROL_NONCRITICAL;
1126
1127         return LDAP_SUCCESS;
1128 }
1129
1130 static int parseProxyAuthz (
1131         Operation *op,
1132         SlapReply *rs,
1133         LDAPControl *ctrl )
1134 {
1135         int             rc;
1136         struct berval   dn = BER_BVNULL;
1137
1138         if ( op->o_proxy_authz != SLAP_CONTROL_NONE ) {
1139                 rs->sr_text = "proxy authorization control specified multiple times";
1140                 return LDAP_PROTOCOL_ERROR;
1141         }
1142
1143         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1144                 rs->sr_text = "proxy authorization control value absent";
1145                 return LDAP_PROTOCOL_ERROR;
1146         }
1147
1148         if ( ( global_disallows & SLAP_DISALLOW_PROXY_AUTHZ_N_CRIT )
1149                 && !ctrl->ldctl_iscritical )
1150         {
1151                 rs->sr_text = "proxied authorization criticality of FALSE not allowed";
1152                 return LDAP_PROTOCOL_ERROR;
1153         }
1154
1155         if ( !( global_allows & SLAP_ALLOW_PROXY_AUTHZ_ANON )
1156                 && BER_BVISEMPTY( &op->o_ndn ) )
1157         {
1158                 rs->sr_text = "anonymous proxied authorization not allowed";
1159                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1160         }
1161
1162         op->o_proxy_authz = ctrl->ldctl_iscritical
1163                 ? SLAP_CONTROL_CRITICAL
1164                 : SLAP_CONTROL_NONCRITICAL;
1165
1166         Debug( LDAP_DEBUG_ARGS,
1167                 "parseProxyAuthz: conn %lu authzid=\"%s\"\n", 
1168                 op->o_connid,
1169                 ctrl->ldctl_value.bv_len ?  ctrl->ldctl_value.bv_val : "anonymous",
1170                 0 );
1171
1172         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1173                 Debug( LDAP_DEBUG_TRACE,
1174                         "parseProxyAuthz: conn=%lu anonymous\n", 
1175                         op->o_connid, 0, 0 );
1176
1177                 /* anonymous */
1178                 if ( !BER_BVISNULL( &op->o_ndn ) ) {
1179                         op->o_ndn.bv_val[ 0 ] = '\0';
1180                 }
1181                 op->o_ndn.bv_len = 0;
1182
1183                 if ( !BER_BVISNULL( &op->o_dn ) ) {
1184                         op->o_dn.bv_val[ 0 ] = '\0';
1185                 }
1186                 op->o_dn.bv_len = 0;
1187
1188                 return LDAP_SUCCESS;
1189         }
1190
1191         rc = slap_sasl_getdn( op->o_conn, op, &ctrl->ldctl_value,
1192                         NULL, &dn, SLAP_GETDN_AUTHZID );
1193
1194         /* FIXME: empty DN in proxyAuthz control should be legal... */
1195         if( rc != LDAP_SUCCESS /* || !dn.bv_len */ ) {
1196                 if ( dn.bv_val ) {
1197                         ch_free( dn.bv_val );
1198                 }
1199                 rs->sr_text = "authzId mapping failed";
1200                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1201         }
1202
1203         Debug( LDAP_DEBUG_TRACE,
1204                 "parseProxyAuthz: conn=%lu \"%s\"\n", 
1205                 op->o_connid,
1206                 dn.bv_len ? dn.bv_val : "(NULL)", 0 );
1207
1208         rc = slap_sasl_authorized( op, &op->o_ndn, &dn );
1209
1210         if ( rc ) {
1211                 ch_free( dn.bv_val );
1212                 rs->sr_text = "not authorized to assume identity";
1213                 return LDAP_PROXIED_AUTHORIZATION_DENIED;
1214         }
1215
1216         ch_free( op->o_ndn.bv_val );
1217
1218         /*
1219          * NOTE: since slap_sasl_getdn() returns a normalized dn,
1220          * from now on op->o_dn is normalized
1221          */
1222         op->o_ndn = dn;
1223         ber_bvreplace( &op->o_dn, &dn );
1224
1225         Statslog( LDAP_DEBUG_STATS, "%s PROXYAUTHZ dn=\"%s\"\n",
1226             op->o_log_prefix, dn.bv_val, 0, 0, 0 );
1227
1228         return LDAP_SUCCESS;
1229 }
1230
1231 static int parseNoOp (
1232         Operation *op,
1233         SlapReply *rs,
1234         LDAPControl *ctrl )
1235 {
1236         if ( op->o_noop != SLAP_CONTROL_NONE ) {
1237                 rs->sr_text = "noop control specified multiple times";
1238                 return LDAP_PROTOCOL_ERROR;
1239         }
1240
1241         if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
1242                 rs->sr_text = "noop control value not empty";
1243                 return LDAP_PROTOCOL_ERROR;
1244         }
1245
1246         op->o_noop = ctrl->ldctl_iscritical
1247                 ? SLAP_CONTROL_CRITICAL
1248                 : SLAP_CONTROL_NONCRITICAL;
1249
1250         return LDAP_SUCCESS;
1251 }
1252
1253 static int parsePagedResults (
1254         Operation *op,
1255         SlapReply *rs,
1256         LDAPControl *ctrl )
1257 {
1258         BerElementBuffer berbuf;
1259         BerElement      *ber = (BerElement *)&berbuf;
1260         struct berval   cookie;
1261         PagedResultsState       *ps;
1262         int             rc = LDAP_SUCCESS;
1263         ber_tag_t       tag;
1264         ber_int_t       size;
1265
1266         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1267                 rs->sr_text = "paged results control specified multiple times";
1268                 return LDAP_PROTOCOL_ERROR;
1269         }
1270
1271         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1272                 rs->sr_text = "paged results control value is absent";
1273                 return LDAP_PROTOCOL_ERROR;
1274         }
1275
1276         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1277                 rs->sr_text = "paged results control value is empty";
1278                 return LDAP_PROTOCOL_ERROR;
1279         }
1280
1281         /* Parse the control value
1282          *      realSearchControlValue ::= SEQUENCE {
1283          *              size    INTEGER (0..maxInt),
1284          *                              -- requested page size from client
1285          *                              -- result set size estimate from server
1286          *              cookie  OCTET STRING
1287          * }
1288          */
1289         ber_init2( ber, &ctrl->ldctl_value, LBER_USE_DER );
1290
1291         tag = ber_scanf( ber, "{im}", &size, &cookie );
1292
1293         if ( tag == LBER_ERROR ) {
1294                 rs->sr_text = "paged results control could not be decoded";
1295                 rc = LDAP_PROTOCOL_ERROR;
1296                 goto done;
1297         }
1298
1299         if ( size < 0 ) {
1300                 rs->sr_text = "paged results control size invalid";
1301                 rc = LDAP_PROTOCOL_ERROR;
1302                 goto done;
1303         }
1304
1305         ps = op->o_tmpalloc( sizeof(PagedResultsState), op->o_tmpmemctx );
1306         *ps = op->o_conn->c_pagedresults_state;
1307         ps->ps_size = size;
1308         ps->ps_cookieval = cookie;
1309         op->o_pagedresults_state = ps;
1310         if ( !cookie.bv_len ) {
1311                 ps->ps_count = 0;
1312                 ps->ps_cookie = 0;
1313                 /* taint ps_cookie, to detect whether it's set */
1314                 op->o_conn->c_pagedresults_state.ps_cookie = NOID;
1315         }
1316
1317         /* NOTE: according to RFC 2696 3.:
1318
1319     If the page size is greater than or equal to the sizeLimit value, the
1320     server should ignore the control as the request can be satisfied in a
1321     single page.
1322
1323          * NOTE: this assumes that the op->ors_slimit be set
1324          * before the controls are parsed.     
1325          */
1326
1327         if ( op->ors_slimit > 0 && size >= op->ors_slimit ) {
1328                 op->o_pagedresults = SLAP_CONTROL_IGNORED;
1329
1330         } else if ( ctrl->ldctl_iscritical ) {
1331                 op->o_pagedresults = SLAP_CONTROL_CRITICAL;
1332
1333         } else {
1334                 op->o_pagedresults = SLAP_CONTROL_NONCRITICAL;
1335         }
1336
1337 done:;
1338         return rc;
1339 }
1340
1341 #ifdef SLAP_CONTROL_X_SORTEDRESULTS
1342 static int parseSortedResults (
1343         Operation *op,
1344         SlapReply *rs,
1345         LDAPControl *ctrl )
1346 {
1347         int             rc = LDAP_SUCCESS;
1348
1349         if ( op->o_sortedresults != SLAP_CONTROL_NONE ) {
1350                 rs->sr_text = "sorted results control specified multiple times";
1351                 return LDAP_PROTOCOL_ERROR;
1352         }
1353
1354         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1355                 rs->sr_text = "sorted results control value is absent";
1356                 return LDAP_PROTOCOL_ERROR;
1357         }
1358
1359         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1360                 rs->sr_text = "sorted results control value is empty";
1361                 return LDAP_PROTOCOL_ERROR;
1362         }
1363
1364         /* blow off parsing the value */
1365
1366         op->o_sortedresults = ctrl->ldctl_iscritical
1367                 ? SLAP_CONTROL_CRITICAL
1368                 : SLAP_CONTROL_NONCRITICAL;
1369
1370         return rc;
1371 }
1372 #endif
1373
1374 static int parseAssert (
1375         Operation *op,
1376         SlapReply *rs,
1377         LDAPControl *ctrl )
1378 {
1379         BerElement      *ber;
1380         struct berval   fstr = BER_BVNULL;
1381
1382         if ( op->o_assert != SLAP_CONTROL_NONE ) {
1383                 rs->sr_text = "assert control specified multiple times";
1384                 return LDAP_PROTOCOL_ERROR;
1385         }
1386
1387         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1388                 rs->sr_text = "assert control value is absent";
1389                 return LDAP_PROTOCOL_ERROR;
1390         }
1391
1392         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1393                 rs->sr_text = "assert control value is empty";
1394                 return LDAP_PROTOCOL_ERROR;
1395         }
1396
1397         ber = ber_init( &(ctrl->ldctl_value) );
1398         if (ber == NULL) {
1399                 rs->sr_text = "assert control: internal error";
1400                 return LDAP_OTHER;
1401         }
1402
1403         rs->sr_err = get_filter( op, ber, (Filter **)&(op->o_assertion),
1404                 &rs->sr_text);
1405         (void) ber_free( ber, 1 );
1406         if( rs->sr_err != LDAP_SUCCESS ) {
1407                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1408                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1409                         send_ldap_disconnect( op, rs );
1410                         rs->sr_err = SLAPD_DISCONNECT;
1411                 } else {
1412                         send_ldap_result( op, rs );
1413                 }
1414                 if( op->o_assertion != NULL ) {
1415                         filter_free_x( op, op->o_assertion, 1 );
1416                         op->o_assertion = NULL;
1417                 }
1418                 return rs->sr_err;
1419         }
1420
1421 #ifdef LDAP_DEBUG
1422         filter2bv_x( op, op->o_assertion, &fstr );
1423
1424         Debug( LDAP_DEBUG_ARGS, "parseAssert: conn %ld assert: %s\n",
1425                 op->o_connid, fstr.bv_len ? fstr.bv_val : "empty" , 0 );
1426         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1427 #endif
1428
1429         op->o_assert = ctrl->ldctl_iscritical
1430                 ? SLAP_CONTROL_CRITICAL
1431                 : SLAP_CONTROL_NONCRITICAL;
1432
1433         rs->sr_err = LDAP_SUCCESS;
1434         return LDAP_SUCCESS;
1435 }
1436
1437 #define READMSG(post, msg) \
1438         ( post ? "postread control: " msg : "preread control: " msg )
1439
1440 static int
1441 parseReadAttrs(
1442         Operation *op,
1443         SlapReply *rs,
1444         LDAPControl *ctrl,
1445         int post )
1446 {
1447         ber_len_t       siz, off, i;
1448         BerElement      *ber;
1449         AttributeName   *an = NULL;
1450
1451         if ( ( post && op->o_postread != SLAP_CONTROL_NONE ) ||
1452                 ( !post && op->o_preread != SLAP_CONTROL_NONE ) )
1453         {
1454                 rs->sr_text = READMSG( post, "specified multiple times" );
1455                 return LDAP_PROTOCOL_ERROR;
1456         }
1457
1458         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1459                 rs->sr_text = READMSG( post, "value is absent" );
1460                 return LDAP_PROTOCOL_ERROR;
1461         }
1462
1463         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1464                 rs->sr_text = READMSG( post, "value is empty" );
1465                 return LDAP_PROTOCOL_ERROR;
1466         }
1467
1468 #ifdef LDAP_X_TXN
1469         if ( op->o_txnSpec ) { /* temporary limitation */
1470                 rs->sr_text = READMSG( post, "cannot perform in transaction" );
1471                 return LDAP_UNWILLING_TO_PERFORM;
1472         }
1473 #endif
1474
1475         ber = ber_init( &ctrl->ldctl_value );
1476         if ( ber == NULL ) {
1477                 rs->sr_text = READMSG( post, "internal error" );
1478                 return LDAP_OTHER;
1479         }
1480
1481         rs->sr_err = LDAP_SUCCESS;
1482         siz = sizeof( AttributeName );
1483         off = offsetof( AttributeName, an_name );
1484         if ( ber_scanf( ber, "{M}", &an, &siz, off ) == LBER_ERROR ) {
1485                 rs->sr_text = READMSG( post, "decoding error" );
1486                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1487                 goto done;
1488         }
1489
1490         for ( i = 0; i < siz; i++ ) {
1491                 const char      *dummy = NULL;
1492                 int             rc;
1493
1494                 an[i].an_desc = NULL;
1495                 an[i].an_oc = NULL;
1496                 an[i].an_flags = 0;
1497                 rc = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &dummy );
1498                 if ( rc == LDAP_SUCCESS ) {
1499                         an[i].an_name = an[i].an_desc->ad_cname;
1500
1501                 } else {
1502                         int                     j;
1503                         static struct berval    special_attrs[] = {
1504                                 BER_BVC( LDAP_NO_ATTRS ),
1505                                 BER_BVC( LDAP_ALL_USER_ATTRIBUTES ),
1506                                 BER_BVC( LDAP_ALL_OPERATIONAL_ATTRIBUTES ),
1507                                 BER_BVNULL
1508                         };
1509
1510                         /* deal with special attribute types */
1511                         for ( j = 0; !BER_BVISNULL( &special_attrs[ j ] ); j++ ) {
1512                                 if ( bvmatch( &an[i].an_name, &special_attrs[ j ] ) ) {
1513                                         an[i].an_name = special_attrs[ j ];
1514                                         break;
1515                                 }
1516                         }
1517
1518                         if ( BER_BVISNULL( &special_attrs[ j ] ) && ctrl->ldctl_iscritical ) {
1519                                 rs->sr_err = rc;
1520                                 rs->sr_text = dummy ? dummy
1521                                         : READMSG( post, "unknown attributeType" );
1522                                 goto done;
1523                         }
1524                 }
1525         }
1526
1527         if ( post ) {
1528                 op->o_postread_attrs = an;
1529                 op->o_postread = ctrl->ldctl_iscritical
1530                         ? SLAP_CONTROL_CRITICAL
1531                         : SLAP_CONTROL_NONCRITICAL;
1532         } else {
1533                 op->o_preread_attrs = an;
1534                 op->o_preread = ctrl->ldctl_iscritical
1535                         ? SLAP_CONTROL_CRITICAL
1536                         : SLAP_CONTROL_NONCRITICAL;
1537         }
1538
1539 done:
1540         (void) ber_free( ber, 1 );
1541         return rs->sr_err;
1542 }
1543
1544 static int parsePreRead (
1545         Operation *op,
1546         SlapReply *rs,
1547         LDAPControl *ctrl )
1548 {
1549         return parseReadAttrs( op, rs, ctrl, 0 );
1550 }
1551
1552 static int parsePostRead (
1553         Operation *op,
1554         SlapReply *rs,
1555         LDAPControl *ctrl )
1556 {
1557         return parseReadAttrs( op, rs, ctrl, 1 );
1558 }
1559
1560 static int parseValuesReturnFilter (
1561         Operation *op,
1562         SlapReply *rs,
1563         LDAPControl *ctrl )
1564 {
1565         BerElement      *ber;
1566         struct berval   fstr = BER_BVNULL;
1567
1568         if ( op->o_valuesreturnfilter != SLAP_CONTROL_NONE ) {
1569                 rs->sr_text = "valuesReturnFilter control specified multiple times";
1570                 return LDAP_PROTOCOL_ERROR;
1571         }
1572
1573         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1574                 rs->sr_text = "valuesReturnFilter control value is absent";
1575                 return LDAP_PROTOCOL_ERROR;
1576         }
1577
1578         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1579                 rs->sr_text = "valuesReturnFilter control value is empty";
1580                 return LDAP_PROTOCOL_ERROR;
1581         }
1582
1583         ber = ber_init( &(ctrl->ldctl_value) );
1584         if (ber == NULL) {
1585                 rs->sr_text = "internal error";
1586                 return LDAP_OTHER;
1587         }
1588
1589         rs->sr_err = get_vrFilter( op, ber,
1590                 (ValuesReturnFilter **)&(op->o_vrFilter), &rs->sr_text);
1591
1592         (void) ber_free( ber, 1 );
1593
1594         if( rs->sr_err != LDAP_SUCCESS ) {
1595                 if( rs->sr_err == SLAPD_DISCONNECT ) {
1596                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1597                         send_ldap_disconnect( op, rs );
1598                         rs->sr_err = SLAPD_DISCONNECT;
1599                 } else {
1600                         send_ldap_result( op, rs );
1601                 }
1602                 if( op->o_vrFilter != NULL) vrFilter_free( op, op->o_vrFilter ); 
1603         }
1604 #ifdef LDAP_DEBUG
1605         else {
1606                 vrFilter2bv( op, op->o_vrFilter, &fstr );
1607         }
1608
1609         Debug( LDAP_DEBUG_ARGS, "       vrFilter: %s\n",
1610                 fstr.bv_len ? fstr.bv_val : "empty", 0, 0 );
1611         op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1612 #endif
1613
1614         op->o_valuesreturnfilter = ctrl->ldctl_iscritical
1615                 ? SLAP_CONTROL_CRITICAL
1616                 : SLAP_CONTROL_NONCRITICAL;
1617
1618         rs->sr_err = LDAP_SUCCESS;
1619         return LDAP_SUCCESS;
1620 }
1621
1622 static int parseSubentries (
1623         Operation *op,
1624         SlapReply *rs,
1625         LDAPControl *ctrl )
1626 {
1627         if ( op->o_subentries != SLAP_CONTROL_NONE ) {
1628                 rs->sr_text = "subentries control specified multiple times";
1629                 return LDAP_PROTOCOL_ERROR;
1630         }
1631
1632         /* FIXME: should use BER library */
1633         if( ( ctrl->ldctl_value.bv_len != 3 )
1634                 || ( ctrl->ldctl_value.bv_val[0] != 0x01 )
1635                 || ( ctrl->ldctl_value.bv_val[1] != 0x01 ))
1636         {
1637                 rs->sr_text = "subentries control value encoding is bogus";
1638                 return LDAP_PROTOCOL_ERROR;
1639         }
1640
1641         op->o_subentries = ctrl->ldctl_iscritical
1642                 ? SLAP_CONTROL_CRITICAL
1643                 : SLAP_CONTROL_NONCRITICAL;
1644
1645         if (ctrl->ldctl_value.bv_val[2]) {
1646                 set_subentries_visibility( op );
1647         }
1648
1649         return LDAP_SUCCESS;
1650 }
1651
1652 static int parsePermissiveModify (
1653         Operation *op,
1654         SlapReply *rs,
1655         LDAPControl *ctrl )
1656 {
1657         if ( op->o_permissive_modify != SLAP_CONTROL_NONE ) {
1658                 rs->sr_text = "permissiveModify control specified multiple times";
1659                 return LDAP_PROTOCOL_ERROR;
1660         }
1661
1662         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1663                 rs->sr_text = "permissiveModify control value not absent";
1664                 return LDAP_PROTOCOL_ERROR;
1665         }
1666
1667         op->o_permissive_modify = ctrl->ldctl_iscritical
1668                 ? SLAP_CONTROL_CRITICAL
1669                 : SLAP_CONTROL_NONCRITICAL;
1670
1671         return LDAP_SUCCESS;
1672 }
1673
1674 static int parseDomainScope (
1675         Operation *op,
1676         SlapReply *rs,
1677         LDAPControl *ctrl )
1678 {
1679         if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1680                 rs->sr_text = "domainScope control specified multiple times";
1681                 return LDAP_PROTOCOL_ERROR;
1682         }
1683
1684         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1685                 rs->sr_text = "domainScope control value not empty";
1686                 return LDAP_PROTOCOL_ERROR;
1687         }
1688
1689         op->o_domain_scope = ctrl->ldctl_iscritical
1690                 ? SLAP_CONTROL_CRITICAL
1691                 : SLAP_CONTROL_NONCRITICAL;
1692
1693         return LDAP_SUCCESS;
1694 }
1695
1696 #ifdef SLAP_CONTROL_X_TREE_DELETE
1697 static int parseTreeDelete (
1698         Operation *op,
1699         SlapReply *rs,
1700         LDAPControl *ctrl )
1701 {
1702         if ( op->o_tree_delete != SLAP_CONTROL_NONE ) {
1703                 rs->sr_text = "treeDelete control specified multiple times";
1704                 return LDAP_PROTOCOL_ERROR;
1705         }
1706
1707         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
1708                 rs->sr_text = "treeDelete control value not absent";
1709                 return LDAP_PROTOCOL_ERROR;
1710         }
1711
1712         op->o_tree_delete = ctrl->ldctl_iscritical
1713                 ? SLAP_CONTROL_CRITICAL
1714                 : SLAP_CONTROL_NONCRITICAL;
1715
1716         return LDAP_SUCCESS;
1717 }
1718 #endif
1719
1720 static int parseSearchOptions (
1721         Operation *op,
1722         SlapReply *rs,
1723         LDAPControl *ctrl )
1724 {
1725         BerElement *ber;
1726         ber_int_t search_flags;
1727         ber_tag_t tag;
1728
1729         if ( BER_BVISNULL( &ctrl->ldctl_value )) {
1730                 rs->sr_text = "searchOptions control value is absent";
1731                 return LDAP_PROTOCOL_ERROR;
1732         }
1733
1734         if ( BER_BVISEMPTY( &ctrl->ldctl_value )) {
1735                 rs->sr_text = "searchOptions control value is empty";
1736                 return LDAP_PROTOCOL_ERROR;
1737         }
1738
1739         ber = ber_init( &ctrl->ldctl_value );
1740         if( ber == NULL ) {
1741                 rs->sr_text = "internal error";
1742                 return LDAP_OTHER;
1743         }
1744
1745         tag = ber_scanf( ber, "{i}", &search_flags );
1746         (void) ber_free( ber, 1 );
1747
1748         if ( tag == LBER_ERROR ) {
1749                 rs->sr_text = "searchOptions control decoding error";
1750                 return LDAP_PROTOCOL_ERROR;
1751         }
1752
1753         if ( search_flags & ~(LDAP_SEARCH_FLAG_DOMAIN_SCOPE) ) {
1754                 /* Search flags not recognised so far,
1755                  * including:
1756                  *              LDAP_SEARCH_FLAG_PHANTOM_ROOT
1757                  */
1758                 if ( ctrl->ldctl_iscritical ) {
1759                         rs->sr_text = "searchOptions contained unrecognized flag";
1760                         return LDAP_UNWILLING_TO_PERFORM;
1761                 }
1762
1763                 /* Ignore */
1764                 Debug( LDAP_DEBUG_TRACE,
1765                         "searchOptions: conn=%lu unrecognized flag(s) 0x%x (non-critical)\n", 
1766                         op->o_connid, (unsigned)search_flags, 0 );
1767
1768                 return LDAP_SUCCESS;
1769         }
1770
1771         if ( search_flags & LDAP_SEARCH_FLAG_DOMAIN_SCOPE ) {
1772                 if ( op->o_domain_scope != SLAP_CONTROL_NONE ) {
1773                         rs->sr_text = "searchOptions control specified multiple times "
1774                                 "or with domainScope control";
1775                         return LDAP_PROTOCOL_ERROR;
1776                 }
1777
1778                 op->o_domain_scope = ctrl->ldctl_iscritical
1779                         ? SLAP_CONTROL_CRITICAL
1780                         : SLAP_CONTROL_NONCRITICAL;
1781         }
1782
1783         return LDAP_SUCCESS;
1784 }
1785
1786 #ifdef SLAP_CONTROL_X_SESSION_TRACKING
1787 struct berval session_tracking_formats[] = {
1788         BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_SESSION_ID ),
1789                 BER_BVC( "RADIUS-Acct-Session-Id" ),
1790         BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_RADIUS_ACCT_MULTI_SESSION_ID ),
1791                 BER_BVC( "RADIUS-Acct-Multi-Session-Id" ),
1792         BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME ),
1793                 BER_BVC( "USERNAME" ),
1794
1795         BER_BVNULL
1796 };
1797
1798 static int parseSessionTracking(
1799         Operation *op,
1800         SlapReply *rs,
1801         LDAPControl *ctrl )
1802 {
1803         BerElement              *ber;
1804         ber_tag_t               tag;
1805         ber_len_t               len;
1806         int                     i, rc;
1807
1808         struct berval           sessionSourceIp = BER_BVNULL,
1809                                 sessionSourceName = BER_BVNULL,
1810                                 formatOID = BER_BVNULL,
1811                                 sessionTrackingIdentifier = BER_BVNULL;
1812
1813         size_t                  st_len, st_pos;
1814
1815         if ( ctrl->ldctl_iscritical ) {
1816                 rs->sr_text = "sessionTracking criticality is TRUE";
1817                 return LDAP_PROTOCOL_ERROR;
1818         }
1819
1820         if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
1821                 rs->sr_text = "sessionTracking control value is absent";
1822                 return LDAP_PROTOCOL_ERROR;
1823         }
1824
1825         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
1826                 rs->sr_text = "sessionTracking control value is empty";
1827                 return LDAP_PROTOCOL_ERROR;
1828         }
1829
1830         /* TODO: add the capability to determine if a client is allowed
1831          * to use this control, based on identity, ip and so */
1832
1833         ber = ber_init( &ctrl->ldctl_value );
1834         if ( ber == NULL ) {
1835                 rs->sr_text = "internal error";
1836                 return LDAP_OTHER;
1837         }
1838
1839         tag = ber_skip_tag( ber, &len );
1840         if ( tag != LBER_SEQUENCE ) {
1841                 tag = LBER_ERROR;
1842                 goto error;
1843         }
1844
1845         /* sessionSourceIp */
1846         tag = ber_peek_tag( ber, &len );
1847         if ( tag == LBER_DEFAULT ) {
1848                 tag = LBER_ERROR;
1849                 goto error;
1850         }
1851
1852         if ( len == 0 ) {
1853                 tag = ber_skip_tag( ber, &len );
1854
1855         } else if ( len > 128 ) {
1856                 rs->sr_text = "sessionTracking.sessionSourceIp too long";
1857                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1858                 goto error;
1859
1860         } else {
1861                 tag = ber_scanf( ber, "m", &sessionSourceIp );
1862         }
1863
1864         if ( ldif_is_not_printable( sessionSourceIp.bv_val, sessionSourceIp.bv_len ) ) {
1865                 BER_BVZERO( &sessionSourceIp );
1866         }
1867
1868         /* sessionSourceName */
1869         tag = ber_peek_tag( ber, &len );
1870         if ( tag == LBER_DEFAULT ) {
1871                 tag = LBER_ERROR;
1872                 goto error;
1873         }
1874
1875         if ( len == 0 ) {
1876                 tag = ber_skip_tag( ber, &len );
1877
1878         } else if ( len > 65536 ) {
1879                 rs->sr_text = "sessionTracking.sessionSourceName too long";
1880                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1881                 goto error;
1882
1883         } else {
1884                 tag = ber_scanf( ber, "m", &sessionSourceName );
1885         }
1886
1887         if ( ldif_is_not_printable( sessionSourceName.bv_val, sessionSourceName.bv_len ) ) {
1888                 BER_BVZERO( &sessionSourceName );
1889         }
1890
1891         /* formatOID */
1892         tag = ber_peek_tag( ber, &len );
1893         if ( tag == LBER_DEFAULT ) {
1894                 tag = LBER_ERROR;
1895                 goto error;
1896         }
1897
1898         if ( len == 0 ) {
1899                 rs->sr_text = "sessionTracking.formatOID empty";
1900                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1901                 goto error;
1902
1903         } else if ( len > 1024 ) {
1904                 rs->sr_text = "sessionTracking.formatOID too long";
1905                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1906                 goto error;
1907
1908         } else {
1909                 tag = ber_scanf( ber, "m", &formatOID );
1910         }
1911
1912         rc = numericoidValidate( NULL, &formatOID );
1913         if ( rc != LDAP_SUCCESS ) {
1914                 rs->sr_text = "sessionTracking.formatOID invalid";
1915                 goto error;
1916         }
1917
1918         for ( i = 0; !BER_BVISNULL( &session_tracking_formats[ i ] ); i += 2 )
1919         {
1920                 if ( bvmatch( &formatOID, &session_tracking_formats[ i ] ) ) {
1921                         formatOID = session_tracking_formats[ i + 1 ];
1922                         break;
1923                 }
1924         }
1925
1926         /* sessionTrackingIdentifier */
1927         tag = ber_peek_tag( ber, &len );
1928         if ( tag == LBER_DEFAULT ) {
1929                 tag = LBER_ERROR;
1930                 goto error;
1931         }
1932
1933         if ( len == 0 ) {
1934                 tag = ber_skip_tag( ber, &len );
1935
1936         } else {
1937                 /* note: should not be more than 65536... */
1938                 tag = ber_scanf( ber, "m", &sessionTrackingIdentifier );
1939                 if ( ldif_is_not_printable( sessionTrackingIdentifier.bv_val, sessionTrackingIdentifier.bv_len ) ) {
1940                         /* we want the OID printed, at least */
1941                         BER_BVSTR( &sessionTrackingIdentifier, "" );
1942                 }
1943         }
1944
1945         /* closure */
1946         tag = ber_skip_tag( ber, &len );
1947         if ( tag != LBER_DEFAULT || len != 0 ) {
1948                 tag = LBER_ERROR;
1949                 goto error;
1950         }
1951         tag = 0;
1952
1953         st_len = 0;
1954         if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1955                 st_len += STRLENOF( "IP=" ) + sessionSourceIp.bv_len;
1956         }
1957         if ( !BER_BVISNULL( &sessionSourceName ) ) {
1958                 if ( st_len ) st_len++;
1959                 st_len += STRLENOF( "NAME=" ) + sessionSourceName.bv_len;
1960         }
1961         if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1962                 if ( st_len ) st_len++;
1963                 st_len += formatOID.bv_len + STRLENOF( "=" )
1964                         + sessionTrackingIdentifier.bv_len;
1965         }
1966
1967         if ( st_len == 0 ) {
1968                 goto error;
1969         }
1970
1971         st_len += STRLENOF( " []" );
1972         st_pos = strlen( op->o_log_prefix );
1973
1974         if ( sizeof( op->o_log_prefix ) - st_pos > st_len ) {
1975                 char    *ptr = &op->o_log_prefix[ st_pos ];
1976
1977                 ptr = lutil_strcopy( ptr, " [" /*]*/ );
1978
1979                 st_len = 0;
1980                 if ( !BER_BVISNULL( &sessionSourceIp ) ) {
1981                         ptr = lutil_strcopy( ptr, "IP=" );
1982                         ptr = lutil_strcopy( ptr, sessionSourceIp.bv_val );
1983                         st_len++;
1984                 }
1985
1986                 if ( !BER_BVISNULL( &sessionSourceName ) ) {
1987                         if ( st_len ) *ptr++ = ' ';
1988                         ptr = lutil_strcopy( ptr, "NAME=" );
1989                         ptr = lutil_strcopy( ptr, sessionSourceName.bv_val );
1990                         st_len++;
1991                 }
1992
1993                 if ( !BER_BVISNULL( &sessionTrackingIdentifier ) ) {
1994                         if ( st_len ) *ptr++ = ' ';
1995                         ptr = lutil_strcopy( ptr, formatOID.bv_val );
1996                         *ptr++ = '=';
1997                         ptr = lutil_strcopy( ptr, sessionTrackingIdentifier.bv_val );
1998                 }
1999
2000                 *ptr++ = /*[*/ ']';
2001                 *ptr = '\0';
2002         }
2003
2004 error:;
2005         (void)ber_free( ber, 1 );
2006
2007         if ( tag == LBER_ERROR ) {
2008                 rs->sr_text = "sessionTracking control decoding error";
2009                 return LDAP_PROTOCOL_ERROR;
2010         }
2011
2012
2013         return rs->sr_err;
2014 }
2015
2016 int
2017 slap_ctrl_session_tracking_add(
2018         Operation *op,
2019         SlapReply *rs,
2020         struct berval *ip,
2021         struct berval *name,
2022         struct berval *id,
2023         LDAPControl *ctrl )
2024 {
2025         BerElementBuffer berbuf;
2026         BerElement      *ber = (BerElement *)&berbuf;
2027
2028         static struct berval    oid = BER_BVC( LDAP_CONTROL_X_SESSION_TRACKING_USERNAME );
2029
2030         assert( ctrl != NULL );
2031
2032         ber_init2( ber, NULL, LBER_USE_DER );
2033
2034         ber_printf( ber, "{OOOO}", ip, name, &oid, id ); 
2035
2036         if ( ber_flatten2( ber, &ctrl->ldctl_value, 0 ) == -1 ) {
2037                 rs->sr_err = LDAP_OTHER;
2038                 goto done;
2039         }
2040
2041         ctrl->ldctl_oid = LDAP_CONTROL_X_SESSION_TRACKING;
2042         ctrl->ldctl_iscritical = 0;
2043
2044         rs->sr_err = LDAP_SUCCESS;
2045
2046 done:;
2047         return rs->sr_err;
2048 }
2049
2050 int
2051 slap_ctrl_session_tracking_request_add( Operation *op, SlapReply *rs, LDAPControl *ctrl )
2052 {
2053         static struct berval    bv_unknown = BER_BVC( SLAP_STRING_UNKNOWN );
2054         struct berval           ip = BER_BVNULL,
2055                                 name = BER_BVNULL,
2056                                 id = BER_BVNULL;
2057
2058         if ( !BER_BVISNULL( &op->o_conn->c_peer_name ) &&
2059                 memcmp( op->o_conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
2060         {
2061                 char    *ptr;
2062
2063                 ip.bv_val = op->o_conn->c_peer_name.bv_val + STRLENOF( "IP=" );
2064                 ip.bv_len = op->o_conn->c_peer_name.bv_len - STRLENOF( "IP=" );
2065
2066                 ptr = ber_bvchr( &ip, ':' );
2067                 if ( ptr ) {
2068                         ip.bv_len = ptr - ip.bv_val;
2069                 }
2070         }
2071
2072         if ( !BER_BVISNULL( &op->o_conn->c_peer_domain ) &&
2073                 !bvmatch( &op->o_conn->c_peer_domain, &bv_unknown ) )
2074         {
2075                 name = op->o_conn->c_peer_domain;
2076         }
2077
2078         if ( !BER_BVISNULL( &op->o_dn ) && !BER_BVISEMPTY( &op->o_dn ) ) {
2079                 id = op->o_dn;
2080         }
2081
2082         return slap_ctrl_session_tracking_add( op, rs, &ip, &name, &id, ctrl );
2083 }
2084 #endif
2085
2086 #ifdef SLAP_CONTROL_X_WHATFAILED
2087 static int parseWhatFailed(
2088         Operation *op,
2089         SlapReply *rs,
2090         LDAPControl *ctrl )
2091 {
2092         if ( op->o_whatFailed != SLAP_CONTROL_NONE ) {
2093                 rs->sr_text = "\"WHat Failed?\" control specified multiple times";
2094                 return LDAP_PROTOCOL_ERROR;
2095         }
2096
2097         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2098                 rs->sr_text = "\"What Failed?\" control value not absent";
2099                 return LDAP_PROTOCOL_ERROR;
2100         }
2101
2102         op->o_whatFailed = ctrl->ldctl_iscritical
2103                 ? SLAP_CONTROL_CRITICAL
2104                 : SLAP_CONTROL_NONCRITICAL;
2105
2106         return LDAP_SUCCESS;
2107 }
2108
2109 int
2110 slap_ctrl_whatFailed_add(
2111         Operation *op,
2112         SlapReply *rs,
2113         char **oids )
2114 {
2115         BerElementBuffer berbuf;
2116         BerElement *ber = (BerElement *) &berbuf;
2117         LDAPControl **ctrls = NULL;
2118         struct berval ctrlval;
2119         int i, rc = LDAP_SUCCESS;
2120
2121         ber_init2( ber, NULL, LBER_USE_DER );
2122         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
2123         ber_printf( ber, "[" /*]*/ );
2124         for ( i = 0; oids[ i ] != NULL; i++ ) {
2125                 ber_printf( ber, "s", oids[ i ] );
2126         }
2127         ber_printf( ber, /*[*/ "]" );
2128
2129         if ( ber_flatten2( ber, &ctrlval, 0 ) == -1 ) {
2130                 rc = LDAP_OTHER;
2131                 goto done;
2132         }
2133
2134         i = 0;
2135         if ( rs->sr_ctrls != NULL ) {
2136                 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) {
2137                         if ( strcmp( rs->sr_ctrls[ i ]->ldctl_oid, LDAP_CONTROL_X_WHATFAILED ) != 0 ) {
2138                                 /* TODO: add */
2139                                 assert( 0 );
2140                         }
2141                 }
2142         }
2143
2144         ctrls = op->o_tmprealloc( rs->sr_ctrls,
2145                         sizeof(LDAPControl *)*( i + 2 )
2146                         + sizeof(LDAPControl)
2147                         + ctrlval.bv_len + 1,
2148                         op->o_tmpmemctx );
2149         if ( ctrls == NULL ) {
2150                 rc = LDAP_OTHER;
2151                 goto done;
2152         }
2153         ctrls[ i + 1 ] = NULL;
2154         ctrls[ i ] = (LDAPControl *)&ctrls[ i + 2 ];
2155         ctrls[ i ]->ldctl_oid = LDAP_CONTROL_X_WHATFAILED;
2156         ctrls[ i ]->ldctl_iscritical = 0;
2157         ctrls[ i ]->ldctl_value.bv_val = (char *)&ctrls[ i ][ 1 ];
2158         AC_MEMCPY( ctrls[ i ]->ldctl_value.bv_val, ctrlval.bv_val, ctrlval.bv_len + 1 );
2159         ctrls[ i ]->ldctl_value.bv_len = ctrlval.bv_len;
2160
2161         ber_free_buf( ber );
2162
2163         rs->sr_ctrls = ctrls;
2164
2165 done:;
2166         return rc;
2167 }
2168 #endif
2169
2170 #ifdef SLAP_CONTROL_X_LAZY_COMMIT
2171 static int parseLazyCommit(
2172         Operation *op,
2173         SlapReply *rs,
2174         LDAPControl *ctrl )
2175 {
2176         if ( op->o_lazyCommit != SLAP_CONTROL_NONE ) {
2177                 rs->sr_text = "\"Lazy Commit?\" control specified multiple times";
2178                 return LDAP_PROTOCOL_ERROR;
2179         }
2180
2181         if ( !BER_BVISNULL( &ctrl->ldctl_value )) {
2182                 rs->sr_text = "\"Lazy Commit?\" control value not absent";
2183                 return LDAP_PROTOCOL_ERROR;
2184         }
2185
2186         op->o_lazyCommit = ctrl->ldctl_iscritical
2187                 ? SLAP_CONTROL_CRITICAL
2188                 : SLAP_CONTROL_NONCRITICAL;
2189
2190         return LDAP_SUCCESS;
2191 }
2192 #endif