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