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