]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwm.c
ITS#6716: Keep sessionlog sorted by csn, compare mincsn with head.
[openldap] / servers / slapd / overlays / rwm.c
1 /* rwm.c - rewrite/remap operations */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2010 The OpenLDAP Foundation.
6  * Portions Copyright 2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17
18 #include "portable.h"
19
20 #ifdef SLAPD_OVER_RWM
21
22 #include <stdio.h>
23
24 #include <ac/string.h>
25
26 #include "slap.h"
27 #include "config.h"
28 #include "lutil.h"
29 #include "rwm.h"
30
31 typedef struct rwm_op_state {
32         ber_tag_t r_tag;
33         struct berval ro_dn;
34         struct berval ro_ndn;
35         struct berval r_dn;
36         struct berval r_ndn;
37         struct berval rx_dn;
38         struct berval rx_ndn;
39         AttributeName *mapped_attrs;
40         OpRequest o_request;
41 } rwm_op_state;
42
43 typedef struct rwm_op_cb {
44         slap_callback cb;
45         rwm_op_state ros;
46 } rwm_op_cb;
47
48 static int
49 rwm_db_destroy( BackendDB *be, ConfigReply *cr );
50
51 static int
52 rwm_send_entry( Operation *op, SlapReply *rs );
53
54 static void
55 rwm_op_rollback( Operation *op, SlapReply *rs, rwm_op_state *ros )
56 {
57         /* in case of successful extended operation cleanup
58          * gets called *after* (ITS#6632); this hack counts
59          * on others to cleanup our o_req_dn/o_req_ndn,
60          * while we cleanup theirs. */
61         if ( ros->r_tag == LDAP_REQ_EXTENDED && rs->sr_err == LDAP_SUCCESS ) {
62                 if ( !BER_BVISNULL( &ros->rx_dn ) ) {
63                         ch_free( ros->rx_dn.bv_val );
64                 }
65                 if ( !BER_BVISNULL( &ros->rx_ndn ) ) {
66                         ch_free( ros->rx_ndn.bv_val );
67                 }
68
69         } else {
70                 if ( !BER_BVISNULL( &ros->ro_dn ) ) {
71                         op->o_req_dn = ros->ro_dn;
72                 }
73                 if ( !BER_BVISNULL( &ros->ro_ndn ) ) {
74                         op->o_req_ndn = ros->ro_ndn;
75                 }
76
77                 if ( !BER_BVISNULL( &ros->r_dn )
78                         && ros->r_dn.bv_val != ros->ro_dn.bv_val )
79                 {
80                         assert( ros->r_dn.bv_val != ros->r_ndn.bv_val );
81                         ch_free( ros->r_dn.bv_val );
82                 }
83
84                 if ( !BER_BVISNULL( &ros->r_ndn )
85                         && ros->r_ndn.bv_val != ros->ro_ndn.bv_val )
86                 {
87                         ch_free( ros->r_ndn.bv_val );
88                 }
89         }
90
91         BER_BVZERO( &ros->r_dn );
92         BER_BVZERO( &ros->r_ndn );
93         BER_BVZERO( &ros->ro_dn );
94         BER_BVZERO( &ros->ro_ndn );
95         BER_BVZERO( &ros->rx_dn );
96         BER_BVZERO( &ros->rx_ndn );
97
98         switch( ros->r_tag ) {
99         case LDAP_REQ_COMPARE:
100                 if ( op->orc_ava->aa_value.bv_val != ros->orc_ava->aa_value.bv_val )
101                         op->o_tmpfree( op->orc_ava->aa_value.bv_val, op->o_tmpmemctx );
102                 op->orc_ava = ros->orc_ava;
103                 break;
104         case LDAP_REQ_MODIFY:
105                 slap_mods_free( op->orm_modlist, 1 );
106                 op->orm_modlist = ros->orm_modlist;
107                 break;
108         case LDAP_REQ_MODRDN:
109                 if ( op->orr_newSup != ros->orr_newSup ) {
110                         ch_free( op->orr_newSup->bv_val );
111                         ch_free( op->orr_nnewSup->bv_val );
112                         op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
113                         op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
114                         op->orr_newSup = ros->orr_newSup;
115                         op->orr_nnewSup = ros->orr_nnewSup;
116                 }
117                 if ( op->orr_newrdn.bv_val != ros->orr_newrdn.bv_val ) {
118                         ch_free( op->orr_newrdn.bv_val );
119                         ch_free( op->orr_nnewrdn.bv_val );
120                         op->orr_newrdn = ros->orr_newrdn;
121                         op->orr_nnewrdn = ros->orr_nnewrdn;
122                 }
123                 break;
124         case LDAP_REQ_SEARCH:
125                 op->o_tmpfree( ros->mapped_attrs, op->o_tmpmemctx );
126                 filter_free_x( op, op->ors_filter, 1 );
127                 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
128                 op->ors_attrs = ros->ors_attrs;
129                 op->ors_filter = ros->ors_filter;
130                 op->ors_filterstr = ros->ors_filterstr;
131                 break;
132         case LDAP_REQ_EXTENDED:
133                 if ( op->ore_reqdata != ros->ore_reqdata ) {
134                         ber_bvfree( op->ore_reqdata );
135                         op->ore_reqdata = ros->ore_reqdata;
136                 }
137                 break;
138         case LDAP_REQ_BIND:
139                 if ( rs->sr_err == LDAP_SUCCESS ) {
140 #if 0
141                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
142                         /* too late, c_mutex released */
143                         Debug( LDAP_DEBUG_ANY, "*** DN: \"%s\" => \"%s\"\n",
144                                 op->o_conn->c_ndn.bv_val,
145                                 op->o_req_ndn.bv_val );
146                         ber_bvreplace( &op->o_conn->c_ndn,
147                                 &op->o_req_ndn );
148                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
149 #endif
150                 }
151                 break;
152         default:        break;
153         }
154 }
155
156 static int
157 rwm_op_cleanup( Operation *op, SlapReply *rs )
158 {
159         slap_callback   *cb = op->o_callback;
160         rwm_op_state *ros = cb->sc_private;
161
162         if ( rs->sr_type == REP_RESULT || rs->sr_type == REP_EXTENDED ||
163                 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
164         {
165                 rwm_op_rollback( op, rs, ros );
166
167                 op->o_callback = op->o_callback->sc_next;
168                 op->o_tmpfree( cb, op->o_tmpmemctx );
169         }
170
171         return SLAP_CB_CONTINUE;
172 }
173
174 static rwm_op_cb *
175 rwm_callback_get( Operation *op, SlapReply *rs )
176 {
177         rwm_op_cb       *roc;
178
179         roc = op->o_tmpalloc( sizeof( struct rwm_op_cb ), op->o_tmpmemctx );
180         roc->cb.sc_cleanup = rwm_op_cleanup;
181         roc->cb.sc_response = NULL;
182         roc->cb.sc_next = op->o_callback;
183         roc->cb.sc_private = &roc->ros;
184         roc->ros.r_tag = op->o_tag;
185         roc->ros.ro_dn = op->o_req_dn;
186         roc->ros.ro_ndn = op->o_req_ndn;
187         BER_BVZERO( &roc->ros.r_dn );
188         BER_BVZERO( &roc->ros.r_ndn );
189         BER_BVZERO( &roc->ros.rx_dn );
190         BER_BVZERO( &roc->ros.rx_ndn );
191         roc->ros.mapped_attrs = NULL;
192         roc->ros.o_request = op->o_request;
193
194         return roc;
195 }
196
197
198 static int
199 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie,
200         rwm_op_state *ros )
201 {
202         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
203         struct ldaprwmap        *rwmap = 
204                         (struct ldaprwmap *)on->on_bi.bi_private;
205
206         struct berval           dn = BER_BVNULL,
207                                 ndn = BER_BVNULL;
208         int                     rc = 0;
209         dncookie                dc;
210
211         /*
212          * Rewrite the dn if needed
213          */
214         dc.rwmap = rwmap;
215         dc.conn = op->o_conn;
216         dc.rs = rs;
217         dc.ctx = (char *)cookie;
218
219         /* NOTE: in those cases where only the ndn is available,
220          * and the caller sets op->o_req_dn = op->o_req_ndn,
221          * only rewrite the op->o_req_ndn and use it as 
222          * op->o_req_dn as well */
223         ndn = op->o_req_ndn;
224         if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
225                 dn = op->o_req_dn;
226                 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
227         } else {
228                 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
229         }
230
231         if ( rc != LDAP_SUCCESS ) {
232                 return rc;
233         }
234
235         if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
236                         || ndn.bv_val == op->o_req_ndn.bv_val )
237         {
238                 return LDAP_SUCCESS;
239         }
240
241         if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
242                 op->o_req_dn = dn;
243                 assert( BER_BVISNULL( &ros->r_dn ) );
244                 ros->r_dn = dn;
245         } else {
246                 op->o_req_dn = ndn;
247         }
248         op->o_req_ndn = ndn;
249         assert( BER_BVISNULL( &ros->r_ndn ) );
250         ros->r_ndn = ndn;
251
252         if ( ros->r_tag == LDAP_REQ_EXTENDED ) {
253                 ros->rx_dn = ros->r_dn;
254                 ros->rx_ndn = ros->r_ndn;
255         }
256
257         return LDAP_SUCCESS;
258 }
259
260 static int
261 rwm_op_add( Operation *op, SlapReply *rs )
262 {
263         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
264         struct ldaprwmap        *rwmap = 
265                         (struct ldaprwmap *)on->on_bi.bi_private;
266
267         int                     rc,
268                                 i;
269         Attribute               **ap = NULL;
270         char                    *olddn = op->o_req_dn.bv_val;
271         int                     isupdate;
272
273         rwm_op_cb               *roc = rwm_callback_get( op, rs );
274
275         rc = rwm_op_dn_massage( op, rs, "addDN", &roc->ros );
276         if ( rc != LDAP_SUCCESS ) {
277                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
278                 send_ldap_error( op, rs, rc, "addDN massage error" );
279                 return -1;
280         }
281
282         if ( olddn != op->o_req_dn.bv_val ) {
283                 ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
284                 ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
285         }
286
287         /* Count number of attributes in entry */ 
288         isupdate = be_shadow_update( op );
289         for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
290                 Attribute       *a;
291
292                 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
293                                 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
294                 {
295                         int             j, last;
296
297                         last = (*ap)->a_numvals - 1;
298                         for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
299                                 struct ldapmapping      *mapping = NULL;
300
301                                 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
302                                                 &mapping, RWM_MAP );
303                                 if ( mapping == NULL ) {
304                                         if ( rwmap->rwm_at.drop_missing ) {
305                                                 /* FIXME: we allow to remove objectClasses as well;
306                                                  * if the resulting entry is inconsistent, that's
307                                                  * the relayed database's business...
308                                                  */
309                                                 ch_free( (*ap)->a_vals[ j ].bv_val );
310                                                 if ( last > j ) {
311                                                         (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
312                                                 }
313                                                 BER_BVZERO( &(*ap)->a_vals[ last ] );
314                                                 (*ap)->a_numvals--;
315                                                 last--;
316                                                 j--;
317                                         }
318
319                                 } else {
320                                         ch_free( (*ap)->a_vals[ j ].bv_val );
321                                         ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
322                                 }
323                         }
324
325                 } else if ( !isupdate && !get_relax( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
326                 {
327                         goto next_attr;
328
329                 } else {
330                         struct ldapmapping      *mapping = NULL;
331
332                         ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
333                                         &mapping, RWM_MAP );
334                         if ( mapping == NULL ) {
335                                 if ( rwmap->rwm_at.drop_missing ) {
336                                         goto cleanup_attr;
337                                 }
338                         }
339
340                         if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
341                                         || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
342                         {
343                                 /*
344                                  * FIXME: rewrite could fail; in this case
345                                  * the operation should give up, right?
346                                  */
347                                 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
348                                                 (*ap)->a_vals,
349                                                 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
350                                 if ( rc ) {
351                                         goto cleanup_attr;
352                                 }
353
354                         } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
355                                 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
356                                                 (*ap)->a_vals,
357                                                 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
358                                 if ( rc != LDAP_SUCCESS ) {
359                                         goto cleanup_attr;
360                                 }
361                         }
362                 
363                         if ( mapping != NULL ) {
364                                 assert( mapping->m_dst_ad != NULL );
365                                 (*ap)->a_desc = mapping->m_dst_ad;
366                         }
367                 }
368
369 next_attr:;
370                 ap = &(*ap)->a_next;
371                 continue;
372
373 cleanup_attr:;
374                 /* FIXME: leaking attribute/values? */
375                 a = *ap;
376
377                 *ap = (*ap)->a_next;
378                 attr_free( a );
379         }
380
381         op->o_callback = &roc->cb;
382
383         return SLAP_CB_CONTINUE;
384 }
385
386 static int
387 rwm_conn_init( BackendDB *be, Connection *conn )
388 {
389         slap_overinst           *on = (slap_overinst *) be->bd_info;
390         struct ldaprwmap        *rwmap = 
391                         (struct ldaprwmap *)on->on_bi.bi_private;
392
393         ( void )rewrite_session_init( rwmap->rwm_rw, conn );
394
395         return SLAP_CB_CONTINUE;
396 }
397
398 static int
399 rwm_conn_destroy( BackendDB *be, Connection *conn )
400 {
401         slap_overinst           *on = (slap_overinst *) be->bd_info;
402         struct ldaprwmap        *rwmap = 
403                         (struct ldaprwmap *)on->on_bi.bi_private;
404
405         ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
406
407         return SLAP_CB_CONTINUE;
408 }
409
410 static int
411 rwm_op_bind( Operation *op, SlapReply *rs )
412 {
413         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
414         int                     rc;
415
416         rwm_op_cb               *roc = rwm_callback_get( op, rs );
417
418         rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
419         if ( rc != LDAP_SUCCESS ) {
420                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
421                 send_ldap_error( op, rs, rc, "bindDN massage error" );
422                 return -1;
423         }
424
425         overlay_callback_after_backover( op, &roc->cb, 1 );
426
427         return SLAP_CB_CONTINUE;
428 }
429
430 static int
431 rwm_op_unbind( Operation *op, SlapReply *rs )
432 {
433         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
434         struct ldaprwmap        *rwmap = 
435                         (struct ldaprwmap *)on->on_bi.bi_private;
436
437         rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
438
439         return SLAP_CB_CONTINUE;
440 }
441
442 static int
443 rwm_op_compare( Operation *op, SlapReply *rs )
444 {
445         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
446         struct ldaprwmap        *rwmap = 
447                         (struct ldaprwmap *)on->on_bi.bi_private;
448
449         int                     rc;
450         struct berval           mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
451
452         rwm_op_cb               *roc = rwm_callback_get( op, rs );
453
454         rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
455         if ( rc != LDAP_SUCCESS ) {
456                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
457                 send_ldap_error( op, rs, rc, "compareDN massage error" );
458                 return -1;
459         }
460
461         /* if the attribute is an objectClass, try to remap its value */
462         if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
463                         || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
464         {
465                 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
466                                 &mapped_vals[0], RWM_MAP );
467                 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
468                 {
469                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
470                         send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
471                         return -1;
472
473                 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
474                         ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
475                                 op->o_tmpmemctx );
476                 }
477
478         } else {
479                 struct ldapmapping      *mapping = NULL;
480                 AttributeDescription    *ad = op->orc_ava->aa_desc;
481
482                 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
483                                 &mapping, RWM_MAP );
484                 if ( mapping == NULL ) {
485                         if ( rwmap->rwm_at.drop_missing ) {
486                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
487                                 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
488                                 return -1;
489                         }
490
491                 } else {
492                         assert( mapping->m_dst_ad != NULL );
493                         ad = mapping->m_dst_ad;
494                 }
495
496                 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
497                                 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
498                 {
499                         struct berval   *mapped_valsp[2];
500                         
501                         mapped_valsp[0] = &mapped_vals[0];
502                         mapped_valsp[1] = &mapped_vals[1];
503
504                         mapped_vals[0] = op->orc_ava->aa_value;
505
506                         rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
507
508                         if ( rc != LDAP_SUCCESS ) {
509                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
510                                 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
511                                 return -1;
512                         }
513
514                         if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
515                                 /* NOTE: if we get here, rwm_dnattr_rewrite()
516                                  * already freed the old value, so now 
517                                  * it's invalid */
518                                 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
519                                         op->o_tmpmemctx );
520                                 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
521                         }
522                 }
523                 op->orc_ava->aa_desc = ad;
524         }
525
526         op->o_callback = &roc->cb;
527
528         return SLAP_CB_CONTINUE;
529 }
530
531 static int
532 rwm_op_delete( Operation *op, SlapReply *rs )
533 {
534         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
535         int                     rc;
536
537         rwm_op_cb               *roc = rwm_callback_get( op, rs );
538
539         rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
540         if ( rc != LDAP_SUCCESS ) {
541                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
542                 send_ldap_error( op, rs, rc, "deleteDN massage error" );
543                 return -1;
544         }
545
546         op->o_callback = &roc->cb;
547
548         return SLAP_CB_CONTINUE;
549 }
550
551 static int
552 rwm_op_modify( Operation *op, SlapReply *rs )
553 {
554         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
555         struct ldaprwmap        *rwmap = 
556                         (struct ldaprwmap *)on->on_bi.bi_private;
557
558         int                     isupdate;
559         Modifications           **mlp;
560         int                     rc;
561
562         rwm_op_cb               *roc = rwm_callback_get( op, rs );
563
564         rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
565         if ( rc != LDAP_SUCCESS ) {
566                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
567                 send_ldap_error( op, rs, rc, "modifyDN massage error" );
568                 return -1;
569         }
570
571         isupdate = be_shadow_update( op );
572         for ( mlp = &op->orm_modlist; *mlp; ) {
573                 int                     is_oc = 0;
574                 Modifications           *ml = *mlp;
575                 struct ldapmapping      *mapping = NULL;
576
577                 /* ml points to a temporary mod until needs duplication */
578                 if ( ml->sml_desc == slap_schema.si_ad_objectClass 
579                                 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
580                 {
581                         is_oc = 1;
582
583                 } else if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod  )
584                 {
585                         ml = ch_malloc( sizeof( Modifications ) );
586                         *ml = **mlp;
587                         if ( (*mlp)->sml_values ) {
588                                 ber_bvarray_dup_x( &ml->sml_values, (*mlp)->sml_values, NULL );
589                                 if ( (*mlp)->sml_nvalues ) {
590                                         ber_bvarray_dup_x( &ml->sml_nvalues, (*mlp)->sml_nvalues, NULL );
591                                 }
592                         }
593                         *mlp = ml;
594                         goto next_mod;
595
596                 } else {
597                         int                     drop_missing;
598
599                         drop_missing = rwm_mapping( &rwmap->rwm_at,
600                                         &ml->sml_desc->ad_cname,
601                                         &mapping, RWM_MAP );
602                         if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
603                         {
604                                 goto skip_mod;
605                         }
606                 }
607
608                 /* duplicate the modlist */
609                 ml = ch_malloc( sizeof( Modifications ));
610                 *ml = **mlp;
611                 *mlp = ml;
612
613                 if ( ml->sml_values != NULL ) {
614                         int i, num;
615                         struct berval *bva;
616
617                         for ( num = 0; !BER_BVISNULL( &ml->sml_values[ num ] ); num++ )
618                                 /* count values */ ;
619
620                         bva = ch_malloc( (num+1) * sizeof( struct berval ));
621                         for (i=0; i<num; i++)
622                                 ber_dupbv( &bva[i], &ml->sml_values[i] );
623                         BER_BVZERO( &bva[i] );
624                         ml->sml_values = bva;
625
626                         if ( ml->sml_nvalues ) {
627                                 bva = ch_malloc( (num+1) * sizeof( struct berval ));
628                                 for (i=0; i<num; i++)
629                                         ber_dupbv( &bva[i], &ml->sml_nvalues[i] );
630                                 BER_BVZERO( &bva[i] );
631                                 ml->sml_nvalues = bva;
632                         }
633
634                         if ( is_oc ) {
635                                 int     last, j;
636
637                                 last = num-1;
638
639                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
640                                         struct ldapmapping      *oc_mapping = NULL;
641                 
642                                         ( void )rwm_mapping( &rwmap->rwm_oc, &ml->sml_values[ j ],
643                                                         &oc_mapping, RWM_MAP );
644                                         if ( oc_mapping == NULL ) {
645                                                 if ( rwmap->rwm_at.drop_missing ) {
646                                                         /* FIXME: we allow to remove objectClasses as well;
647                                                          * if the resulting entry is inconsistent, that's
648                                                          * the relayed database's business...
649                                                          */
650                                                         if ( last > j ) {
651                                                                 ch_free( ml->sml_values[ j ].bv_val );
652                                                                 ml->sml_values[ j ] = ml->sml_values[ last ];
653                                                         }
654                                                         BER_BVZERO( &ml->sml_values[ last ] );
655                                                         last--;
656                                                         j--;
657                                                 }
658         
659                                         } else {
660                                                 ch_free( ml->sml_values[ j ].bv_val );
661                                                 ber_dupbv( &ml->sml_values[ j ], &oc_mapping->m_dst );
662                                         }
663                                 }
664
665                         } else {
666                                 if ( ml->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
667                                                 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
668                                 {
669                                         rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
670                                                         ml->sml_values,
671                                                         ml->sml_nvalues ? &ml->sml_nvalues : NULL );
672
673                                 } else if ( ml->sml_desc == slap_schema.si_ad_ref ) {
674                                         rc = rwm_referral_rewrite( op, rs,
675                                                         "referralAttrDN",
676                                                         ml->sml_values,
677                                                         ml->sml_nvalues ? &ml->sml_nvalues : NULL );
678                                         if ( rc != LDAP_SUCCESS ) {
679                                                 goto cleanup_mod;
680                                         }
681                                 }
682
683                                 if ( rc != LDAP_SUCCESS ) {
684                                         goto cleanup_mod;
685                                 }
686                         }
687                 }
688
689 next_mod:;
690                 if ( mapping != NULL ) {
691                         /* use new attribute description */
692                         assert( mapping->m_dst_ad != NULL );
693                         ml->sml_desc = mapping->m_dst_ad;
694                 }
695
696                 mlp = &ml->sml_next;
697                 continue;
698
699 skip_mod:;
700                 *mlp = (*mlp)->sml_next;
701                 continue;
702
703 cleanup_mod:;
704                 ml = *mlp;
705                 *mlp = (*mlp)->sml_next;
706                 slap_mod_free( &ml->sml_mod, 0 );
707                 free( ml );
708         }
709
710         op->o_callback = &roc->cb;
711
712         return SLAP_CB_CONTINUE;
713 }
714
715 static int
716 rwm_op_modrdn( Operation *op, SlapReply *rs )
717 {
718         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
719         struct ldaprwmap        *rwmap = 
720                         (struct ldaprwmap *)on->on_bi.bi_private;
721         
722         int                     rc;
723         dncookie                dc;
724
725         rwm_op_cb               *roc = rwm_callback_get( op, rs );
726
727         if ( op->orr_newSup ) {
728                 struct berval   nnewSup = BER_BVNULL;
729                 struct berval   newSup = BER_BVNULL;
730
731                 /*
732                  * Rewrite the new superior, if defined and required
733                  */
734                 dc.rwmap = rwmap;
735                 dc.conn = op->o_conn;
736                 dc.rs = rs;
737                 dc.ctx = "newSuperiorDN";
738                 newSup = *op->orr_newSup;
739                 nnewSup = *op->orr_nnewSup;
740                 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
741                 if ( rc != LDAP_SUCCESS ) {
742                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
743                         send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
744                         return -1;
745                 }
746
747                 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
748                         op->orr_newSup = op->o_tmpalloc( sizeof( struct berval ),
749                                 op->o_tmpmemctx );
750                         op->orr_nnewSup = op->o_tmpalloc( sizeof( struct berval ),
751                                 op->o_tmpmemctx );
752                         *op->orr_newSup = newSup;
753                         *op->orr_nnewSup = nnewSup;
754                 }
755         }
756
757         /*
758          * Rewrite the newRDN, if needed
759          */
760         {
761                 struct berval   newrdn = BER_BVNULL;
762                 struct berval   nnewrdn = BER_BVNULL;
763
764                 dc.rwmap = rwmap;
765                 dc.conn = op->o_conn;
766                 dc.rs = rs;
767                 dc.ctx = "newRDN";
768                 newrdn = op->orr_newrdn;
769                 nnewrdn = op->orr_nnewrdn;
770                 rc = rwm_dn_massage_pretty_normalize( &dc, &op->orr_newrdn, &newrdn, &nnewrdn );
771                 if ( rc != LDAP_SUCCESS ) {
772                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
773                         send_ldap_error( op, rs, rc, "newRDN massage error" );
774                         goto err;
775                 }
776
777                 if ( op->orr_newrdn.bv_val != newrdn.bv_val ) {
778                         op->orr_newrdn = newrdn;
779                         op->orr_nnewrdn = nnewrdn;
780                 }
781         }
782
783         /*
784          * Rewrite the dn, if needed
785          */
786         rc = rwm_op_dn_massage( op, rs, "renameDN", &roc->ros );
787         if ( rc != LDAP_SUCCESS ) {
788                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
789                 send_ldap_error( op, rs, rc, "renameDN massage error" );
790                 goto err;
791         }
792
793         op->o_callback = &roc->cb;
794
795         rc = SLAP_CB_CONTINUE;
796
797         if ( 0 ) {
798 err:;
799                 if ( op->orr_newSup != roc->ros.orr_newSup ) {
800                         ch_free( op->orr_newSup->bv_val );
801                         ch_free( op->orr_nnewSup->bv_val );
802                         op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
803                         op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
804                         op->orr_newSup = roc->ros.orr_newSup;
805                         op->orr_nnewSup = roc->ros.orr_nnewSup;
806                 }
807
808                 if ( op->orr_newrdn.bv_val != roc->ros.orr_newrdn.bv_val ) {
809                         ch_free( op->orr_newrdn.bv_val );
810                         ch_free( op->orr_nnewrdn.bv_val );
811                         op->orr_newrdn = roc->ros.orr_newrdn;
812                         op->orr_nnewrdn = roc->ros.orr_nnewrdn;
813                 }
814         }
815
816         return rc;
817 }
818
819
820 static int
821 rwm_swap_attrs( Operation *op, SlapReply *rs )
822 {
823         slap_callback   *cb = op->o_callback;
824         rwm_op_state *ros = cb->sc_private;
825
826         rs->sr_attrs = ros->ors_attrs;
827
828         /* other overlays might have touched op->ors_attrs, 
829          * so we restore the original version here, otherwise
830          * attribute-mapping might fail */
831         op->ors_attrs = ros->mapped_attrs; 
832         
833         return SLAP_CB_CONTINUE;
834 }
835
836 /*
837  * NOTE: this implementation of get/release entry is probably far from
838  * optimal.  The rationale consists in intercepting the request directed
839  * to the underlying database, in order to rewrite/remap the request,
840  * perform it using the modified data, duplicate the resulting entry
841  * and finally free it when release is called.
842  * This implies that subsequent overlays are not called, as the request
843  * is directly shunted to the underlying database.
844  */
845 static int
846 rwm_entry_release_rw( Operation *op, Entry *e, int rw )
847 {
848         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
849
850         /* can't be ours */
851         if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
852                 return SLAP_CB_CONTINUE;
853         }
854
855         /* just free entry if (probably) ours */
856         if ( e->e_private == NULL && BER_BVISNULL( &e->e_bv ) ) {
857                 entry_free( e );
858                 return LDAP_SUCCESS;
859         }
860
861         return SLAP_CB_CONTINUE;
862 }
863
864 static int
865 rwm_entry_get_rw( Operation *op, struct berval *ndn,
866         ObjectClass *oc, AttributeDescription *at, int rw, Entry **ep )
867 {
868         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
869         struct ldaprwmap        *rwmap = 
870                         (struct ldaprwmap *)on->on_bi.bi_private;
871
872         int                     rc;
873         dncookie                dc;
874
875         BackendDB               db;
876         Operation               op2;
877         SlapReply               rs = { REP_SEARCH };
878
879         rwm_op_state            ros = { 0 };
880         struct berval           mndn = BER_BVNULL;
881
882         if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
883                 return SLAP_CB_CONTINUE;
884         }
885
886         /* massage DN */
887         op2.o_tag = LDAP_REQ_SEARCH;
888         op2 = *op;
889         op2.o_req_dn = *ndn;
890         op2.o_req_ndn = *ndn;
891         rc = rwm_op_dn_massage( &op2, &rs, "searchDN", &ros );
892         if ( rc != LDAP_SUCCESS ) {
893                 return LDAP_OTHER;
894         }
895
896         mndn = BER_BVISNULL( &ros.r_ndn ) ? *ndn : ros.r_ndn;
897
898         /* map attribute & objectClass */
899         if ( at != NULL ) {
900         }
901
902         if ( oc != NULL ) {
903         }
904
905         /* fetch entry */
906         db = *op->o_bd;
907         op2.o_bd = &db;
908         op2.o_bd->bd_info = (BackendInfo *)on->on_info->oi_orig;
909         op2.ors_attrs = slap_anlist_all_attributes;
910         rc = op2.o_bd->bd_info->bi_entry_get_rw( &op2, &mndn, oc, at, rw, ep );
911         if ( rc == LDAP_SUCCESS && *ep != NULL ) {
912                 /* we assume be_entry_release() needs to be called */
913                 rs.sr_flags = REP_ENTRY_MUSTRELEASE;
914                 rs.sr_entry = *ep;
915
916                 /* duplicate & release */
917                 op2.o_bd->bd_info = (BackendInfo *)on;
918                 rc = rwm_send_entry( &op2, &rs );
919                 if ( rc == SLAP_CB_CONTINUE ) {
920                         *ep = rs.sr_entry;
921                         rc = LDAP_SUCCESS;
922                 }
923         }
924
925         if ( !BER_BVISNULL( &ros.r_ndn) && ros.r_ndn.bv_val != ndn->bv_val ) {
926                 op->o_tmpfree( ros.r_ndn.bv_val, op->o_tmpmemctx );
927         }
928
929         return rc;
930 }
931
932 static int
933 rwm_op_search( Operation *op, SlapReply *rs )
934 {
935         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
936         struct ldaprwmap        *rwmap = 
937                         (struct ldaprwmap *)on->on_bi.bi_private;
938
939         int                     rc;
940         dncookie                dc;
941
942         struct berval           fstr = BER_BVNULL;
943         Filter                  *f = NULL;
944
945         AttributeName           *an = NULL;
946
947         char                    *text = NULL;
948
949         rwm_op_cb               *roc = rwm_callback_get( op, rs );
950
951         rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
952                 "searchFilter", op->ors_filterstr.bv_val );
953         if ( rc == LDAP_SUCCESS )
954                 rc = rwm_op_dn_massage( op, rs, "searchDN", &roc->ros );
955         if ( rc != LDAP_SUCCESS ) {
956                 text = "searchDN massage error";
957                 goto error_return;
958         }
959
960         /*
961          * Rewrite the dn if needed
962          */
963         dc.rwmap = rwmap;
964         dc.conn = op->o_conn;
965         dc.rs = rs;
966         dc.ctx = "searchFilterAttrDN";
967
968         rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
969         if ( rc != LDAP_SUCCESS ) {
970                 text = "searchFilter/searchFilterAttrDN massage error";
971                 goto error_return;
972         }
973
974         f = str2filter_x( op, fstr.bv_val );
975
976         if ( f == NULL ) {
977                 text = "massaged filter parse error";
978                 goto error_return;
979         }
980
981         op->ors_filter = f;
982         op->ors_filterstr = fstr;
983
984         rc = rwm_map_attrnames( op, &rwmap->rwm_at, &rwmap->rwm_oc,
985                         op->ors_attrs, &an, RWM_MAP );
986         if ( rc != LDAP_SUCCESS ) {
987                 text = "attribute list mapping error";
988                 goto error_return;
989         }
990
991         op->ors_attrs = an;
992         /* store the mapped Attributes for later usage, in
993          * the case that other overlays change op->ors_attrs */
994         roc->ros.mapped_attrs = an;
995         roc->cb.sc_response = rwm_swap_attrs;
996
997         op->o_callback = &roc->cb;
998
999         return SLAP_CB_CONTINUE;
1000
1001 error_return:;
1002         if ( an != NULL ) {
1003                 ch_free( an );
1004         }
1005
1006         if ( f != NULL ) {
1007                 filter_free_x( op, f, 1 );
1008         }
1009
1010         if ( !BER_BVISNULL( &fstr ) ) {
1011                 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1012         }
1013
1014         rwm_op_rollback( op, rs, &roc->ros );
1015         op->oq_search = roc->ros.oq_search;
1016         op->o_tmpfree( roc, op->o_tmpmemctx );
1017
1018         op->o_bd->bd_info = (BackendInfo *)on->on_info;
1019         send_ldap_error( op, rs, rc, text );
1020
1021         return -1;
1022
1023 }
1024
1025 static int
1026 rwm_exop_passwd( Operation *op, SlapReply *rs )
1027 {
1028         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1029         int                     rc;
1030         rwm_op_cb *roc;
1031
1032         struct berval   id = BER_BVNULL,
1033                         pwold = BER_BVNULL,
1034                         pwnew = BER_BVNULL;
1035         BerElement *ber = NULL;
1036
1037         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
1038                 return LDAP_SUCCESS;
1039         }
1040
1041         if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
1042                 rs->sr_err = LDAP_OTHER;
1043                 return rs->sr_err;
1044         }
1045
1046         rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
1047                 &pwold, &pwnew, &rs->sr_text );
1048         if ( rs->sr_err != LDAP_SUCCESS ) {
1049                 return rs->sr_err;
1050         }
1051
1052         if ( !BER_BVISNULL( &id ) ) {
1053                 char idNul = id.bv_val[id.bv_len];
1054                 id.bv_val[id.bv_len] = '\0';
1055                 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
1056                                 &op->o_req_ndn, op->o_tmpmemctx );
1057                 id.bv_val[id.bv_len] = idNul;
1058                 if ( rs->sr_err != LDAP_SUCCESS ) {
1059                         rs->sr_text = "Invalid DN";
1060                         return rs->sr_err;
1061                 }
1062
1063         } else {
1064                 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
1065                 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
1066         }
1067
1068         roc = rwm_callback_get( op, rs );
1069
1070         rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1071         if ( rc != LDAP_SUCCESS ) {
1072                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1073                 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1074                 return -1;
1075         }
1076
1077         ber = ber_alloc_t( LBER_USE_DER );
1078         if ( !ber ) {
1079                 rs->sr_err = LDAP_OTHER;
1080                 rs->sr_text = "No memory";
1081                 return rs->sr_err;
1082         }
1083         ber_printf( ber, "{" );
1084         if ( !BER_BVISNULL( &id )) {
1085                 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, 
1086                         &op->o_req_dn );
1087         }
1088         if ( !BER_BVISNULL( &pwold )) {
1089                 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &pwold );
1090         }
1091         if ( !BER_BVISNULL( &pwnew )) {
1092                 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &pwnew );
1093         }
1094         ber_printf( ber, "N}" );
1095         ber_flatten( ber, &op->ore_reqdata );
1096         ber_free( ber, 1 );
1097
1098         op->o_callback = &roc->cb;
1099
1100         return SLAP_CB_CONTINUE;
1101 }
1102
1103 static struct exop {
1104         struct berval   oid;
1105         BI_op_extended  *extended;
1106 } exop_table[] = {
1107         { BER_BVC(LDAP_EXOP_MODIFY_PASSWD),     rwm_exop_passwd },
1108         { BER_BVNULL, NULL }
1109 };
1110
1111 static int
1112 rwm_extended( Operation *op, SlapReply *rs )
1113 {
1114         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1115         int                     rc;
1116         rwm_op_cb *roc;
1117
1118         int     i;
1119
1120         for ( i = 0; exop_table[i].extended != NULL; i++ ) {
1121                 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
1122                 {
1123                         rc = exop_table[i].extended( op, rs );
1124                         switch ( rc ) {
1125                         case LDAP_SUCCESS:
1126                                 break;
1127
1128                         case SLAP_CB_CONTINUE:
1129                         case SLAPD_ABANDON:
1130                                 return rc;
1131
1132                         default:
1133                                 send_ldap_result( op, rs );
1134                                 return rc;
1135                         }
1136                         break;
1137                 }
1138         }
1139
1140         roc = rwm_callback_get( op, rs );
1141
1142         rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1143         if ( rc != LDAP_SUCCESS ) {
1144                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1145                 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1146                 return -1;
1147         }
1148
1149         /* TODO: rewrite/map extended data ? ... */
1150         op->o_callback = &roc->cb;
1151
1152         return SLAP_CB_CONTINUE;
1153 }
1154
1155 static int
1156 rwm_matched( Operation *op, SlapReply *rs )
1157 {
1158         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1159         struct ldaprwmap        *rwmap = 
1160                         (struct ldaprwmap *)on->on_bi.bi_private;
1161
1162         struct berval           dn, mdn;
1163         dncookie                dc;
1164         int                     rc;
1165
1166         if ( rs->sr_matched == NULL ) {
1167                 return SLAP_CB_CONTINUE;
1168         }
1169
1170         dc.rwmap = rwmap;
1171         dc.conn = op->o_conn;
1172         dc.rs = rs;
1173         dc.ctx = "matchedDN";
1174         ber_str2bv( rs->sr_matched, 0, 0, &dn );
1175         mdn = dn;
1176         rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
1177         if ( rc != LDAP_SUCCESS ) {
1178                 rs->sr_err = rc;
1179                 rs->sr_text = "Rewrite error";
1180                 return 1;
1181         }
1182
1183         if ( mdn.bv_val != dn.bv_val ) {
1184                 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
1185                         ch_free( (void *)rs->sr_matched );
1186
1187                 } else {
1188                         rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
1189                 }
1190                 rs->sr_matched = mdn.bv_val;
1191         }
1192         
1193         return SLAP_CB_CONTINUE;
1194 }
1195
1196 static int
1197 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
1198 {
1199         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1200         struct ldaprwmap        *rwmap = 
1201                         (struct ldaprwmap *)on->on_bi.bi_private;
1202
1203         dncookie                dc;
1204         int                     rc;
1205         Attribute               **ap;
1206         int                     isupdate;
1207         int                     check_duplicate_attrs = 0;
1208
1209         /*
1210          * Rewrite the dn attrs, if needed
1211          */
1212         dc.rwmap = rwmap;
1213         dc.conn = op->o_conn;
1214         dc.rs = NULL; 
1215
1216         /* FIXME: the entries are in the remote mapping form;
1217          * so we need to select those attributes we are willing
1218          * to return, and remap them accordingly */
1219
1220         /* FIXME: in principle, one could map an attribute
1221          * on top of another, which already exists.
1222          * As such, in the end there might exist more than
1223          * one instance of an attribute.
1224          * We should at least check if this occurs, and issue
1225          * an error (because multiple instances of attrs in 
1226          * response are not valid), or merge the values (what
1227          * about duplicate values?) */
1228         isupdate = be_shadow_update( op );
1229         for ( ap = a_first; *ap; ) {
1230                 struct ldapmapping      *mapping = NULL;
1231                 int                     drop_missing;
1232                 int                     last = -1;
1233                 Attribute               *a;
1234
1235                 if ( ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS ) &&
1236                                 op->ors_attrs != NULL && 
1237                                 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
1238                                 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
1239                 {
1240                         goto cleanup_attr;
1241                 }
1242
1243                 drop_missing = rwm_mapping( &rwmap->rwm_at,
1244                                 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
1245                 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
1246                 {
1247                         goto cleanup_attr;
1248                 }
1249                 if ( mapping != NULL ) {
1250                         assert( mapping->m_dst_ad != NULL );
1251
1252                         /* try to normalize mapped Attributes if the original 
1253                          * AttributeType was not normalized */
1254                         if ( (!(*ap)->a_desc->ad_type->sat_equality || 
1255                                 !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
1256                                 mapping->m_dst_ad->ad_type->sat_equality &&
1257                                 mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
1258                         {
1259                                 if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS))
1260                                 {
1261                                         int i = 0;
1262
1263                                         last = (*ap)->a_numvals;
1264                                         if ( last )
1265                                         {
1266                                                 (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
1267
1268                                                 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
1269                                                         int             rc;
1270                                                         /*
1271                                                          * check that each value is valid per syntax
1272                                                          * and pretty if appropriate
1273                                                          */
1274                                                         rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
1275                                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1276                                                                 mapping->m_dst_ad->ad_type->sat_syntax,
1277                                                                 mapping->m_dst_ad->ad_type->sat_equality,
1278                                                                 &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
1279                                                                 NULL );
1280
1281                                                         if ( rc != LDAP_SUCCESS ) {
1282                                                                 BER_BVZERO( &(*ap)->a_nvals[i] );
1283                                                         }
1284                                                 }
1285                                                 BER_BVZERO( &(*ap)->a_nvals[i] );
1286                                         }
1287
1288                                 } else {
1289                                         assert( (*ap)->a_nvals == (*ap)->a_vals );
1290                                         (*ap)->a_nvals = NULL;
1291                                         ber_bvarray_dup_x( &(*ap)->a_nvals, (*ap)->a_vals, NULL );
1292                                 }
1293                         }
1294
1295                         /* rewrite the attribute description */
1296                         (*ap)->a_desc = mapping->m_dst_ad;
1297
1298                         /* will need to check for duplicate attrs */
1299                         check_duplicate_attrs++;
1300                 }
1301
1302                 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
1303                         if ( stripEntryDN ) {
1304                                 /* will be generated by frontend */
1305                                 goto cleanup_attr;
1306                         }
1307                         
1308                 } else if ( !isupdate
1309                         && !get_relax( op )
1310                         && (*ap)->a_desc->ad_type->sat_no_user_mod 
1311                         && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
1312                 {
1313                         goto next_attr;
1314                 }
1315
1316                 if ( last == -1 ) { /* not yet counted */ 
1317                         last = (*ap)->a_numvals;
1318                 }
1319
1320                 if ( last == 0 ) {
1321                         /* empty? leave it in place because of attrsonly and vlv */
1322                         goto next_attr;
1323                 }
1324                 last--;
1325
1326                 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
1327                                 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
1328                 {
1329                         struct berval   *bv;
1330                         
1331                         for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1332                                 struct berval   mapped;
1333
1334                                 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
1335                                 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
1336 remove_oc:;
1337                                         ch_free( bv[0].bv_val );
1338                                         BER_BVZERO( &bv[0] );
1339                                         if ( &(*ap)->a_vals[last] > &bv[0] ) {
1340                                                 bv[0] = (*ap)->a_vals[last];
1341                                                 BER_BVZERO( &(*ap)->a_vals[last] );
1342                                         }
1343                                         last--;
1344                                         bv--;
1345
1346                                 } else if ( mapped.bv_val != bv[0].bv_val
1347                                         && ber_bvstrcasecmp( &mapped, &bv[0] ) != 0 )
1348                                 {
1349                                         int     i;
1350
1351                                         for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[ i ] ); i++ ) {
1352                                                 if ( &(*ap)->a_vals[ i ] == bv ) {
1353                                                         continue;
1354                                                 }
1355
1356                                                 if ( ber_bvstrcasecmp( &mapped, &(*ap)->a_vals[ i ] ) == 0 ) {
1357                                                         break;
1358                                                 }
1359                                         }
1360
1361                                         if ( !BER_BVISNULL( &(*ap)->a_vals[ i ] ) ) {
1362                                                 goto remove_oc;
1363                                         }
1364
1365                                         /*
1366                                          * FIXME: after LBER_FREEing
1367                                          * the value is replaced by
1368                                          * ch_alloc'ed memory
1369                                          */
1370                                         ber_bvreplace( &bv[0], &mapped );
1371
1372                                         /* FIXME: will need to check
1373                                          * if the structuralObjectClass
1374                                          * changed */
1375                                 }
1376                         }
1377
1378                 /*
1379                  * It is necessary to try to rewrite attributes with
1380                  * dn syntax because they might be used in ACLs as
1381                  * members of groups; since ACLs are applied to the
1382                  * rewritten stuff, no dn-based subject clause could
1383                  * be used at the ldap backend side (see
1384                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
1385                  * The problem can be overcome by moving the dn-based
1386                  * ACLs to the target directory server, and letting
1387                  * everything pass thru the ldap backend. */
1388                 /* FIXME: handle distinguishedName-like syntaxes, like
1389                  * nameAndOptionalUID */
1390                 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
1391                                 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
1392                 {
1393                         dc.ctx = "searchAttrDN";
1394                         rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals, (*ap)->a_nvals );
1395                         if ( rc != LDAP_SUCCESS ) {
1396                                 goto cleanup_attr;
1397                         }
1398
1399                 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
1400                         dc.ctx = "searchAttrDN";
1401                         rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
1402                         if ( rc != LDAP_SUCCESS ) {
1403                                 goto cleanup_attr;
1404                         }
1405                 }
1406
1407
1408 next_attr:;
1409                 ap = &(*ap)->a_next;
1410                 continue;
1411
1412 cleanup_attr:;
1413                 a = *ap;
1414                 *ap = (*ap)->a_next;
1415
1416                 attr_free( a );
1417         }
1418
1419         /* only check if some mapping occurred */
1420         if ( check_duplicate_attrs ) {
1421                 for ( ap = a_first; *ap != NULL; ap = &(*ap)->a_next ) {
1422                         Attribute       **tap;
1423
1424                         for ( tap = &(*ap)->a_next; *tap != NULL; ) {
1425                                 if ( (*tap)->a_desc == (*ap)->a_desc ) {
1426                                         Entry           e = { 0 };
1427                                         Modification    mod = { 0 };
1428                                         const char      *text = NULL;
1429                                         char            textbuf[ SLAP_TEXT_BUFLEN ];
1430                                         Attribute       *next = (*tap)->a_next;
1431
1432                                         BER_BVSTR( &e.e_name, "" );
1433                                         BER_BVSTR( &e.e_nname, "" );
1434                                         e.e_attrs = *ap;
1435                                         mod.sm_op = LDAP_MOD_ADD;
1436                                         mod.sm_desc = (*ap)->a_desc;
1437                                         mod.sm_type = mod.sm_desc->ad_cname;
1438                                         mod.sm_numvals = (*tap)->a_numvals;
1439                                         mod.sm_values = (*tap)->a_vals;
1440                                         if ( (*tap)->a_nvals != (*tap)->a_vals ) {
1441                                                 mod.sm_nvalues = (*tap)->a_nvals;
1442                                         }
1443
1444                                         (void)modify_add_values( &e, &mod,
1445                                                 /* permissive */ 1,
1446                                                 &text, textbuf, sizeof( textbuf ) );
1447
1448                                         /* should not insert new attrs! */
1449                                         assert( e.e_attrs == *ap );
1450
1451                                         attr_free( *tap );
1452                                         *tap = next;
1453
1454                                 } else {
1455                                         tap = &(*tap)->a_next;
1456                                 }
1457                         }
1458                 }
1459         }
1460
1461         return 0;
1462 }
1463
1464 static int
1465 rwm_send_entry( Operation *op, SlapReply *rs )
1466 {
1467         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1468         struct ldaprwmap        *rwmap = 
1469                         (struct ldaprwmap *)on->on_bi.bi_private;
1470
1471         Entry                   *e = NULL;
1472         slap_mask_t             flags;
1473         struct berval           dn = BER_BVNULL,
1474                                 ndn = BER_BVNULL;
1475         dncookie                dc;
1476         int                     rc;
1477
1478         assert( rs->sr_entry != NULL );
1479
1480         /*
1481          * Rewrite the dn of the result, if needed
1482          */
1483         dc.rwmap = rwmap;
1484         dc.conn = op->o_conn;
1485         dc.rs = NULL; 
1486         dc.ctx = "searchEntryDN";
1487
1488         e = rs->sr_entry;
1489         flags = rs->sr_flags;
1490         if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1491                 /* FIXME: all we need to duplicate are:
1492                  * - dn
1493                  * - ndn
1494                  * - attributes that are requested
1495                  * - no values if attrsonly is set
1496                  */
1497
1498                 e = entry_dup( e );
1499                 if ( e == NULL ) {
1500                         rc = LDAP_NO_MEMORY;
1501                         goto fail;
1502                 }
1503
1504                 flags &= ~REP_ENTRY_MUSTRELEASE;
1505                 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1506         }
1507
1508         /*
1509          * Note: this may fail if the target host(s) schema differs
1510          * from the one known to the meta, and a DN with unknown
1511          * attributes is returned.
1512          */
1513         dn = e->e_name;
1514         ndn = e->e_nname;
1515         rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1516         if ( rc != LDAP_SUCCESS ) {
1517                 rc = 1;
1518                 goto fail;
1519         }
1520
1521         if ( e->e_name.bv_val != dn.bv_val ) {
1522                 ch_free( e->e_name.bv_val );
1523                 ch_free( e->e_nname.bv_val );
1524
1525                 e->e_name = dn;
1526                 e->e_nname = ndn;
1527         }
1528
1529         /* TODO: map entry attribute types, objectclasses 
1530          * and dn-valued attribute values */
1531
1532         /* FIXME: the entries are in the remote mapping form;
1533          * so we need to select those attributes we are willing
1534          * to return, and remap them accordingly */
1535         (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1536
1537         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1538                 /* ITS#6423: REP_ENTRY_MUSTRELEASE incompatible
1539                  * with REP_ENTRY_MODIFIABLE */
1540                 if ( rs->sr_entry == e ) {
1541                         rc = 1;
1542                         goto fail;
1543                 }
1544
1545                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1546                 be_entry_release_r( op, rs->sr_entry );
1547                 op->o_bd->bd_info = (BackendInfo *)on;
1548         }
1549
1550         rs->sr_entry = e;
1551         rs->sr_flags = flags;
1552
1553         return SLAP_CB_CONTINUE;
1554
1555 fail:;
1556         if ( e != NULL && e != rs->sr_entry ) {
1557                 if ( e->e_name.bv_val == dn.bv_val ) {
1558                         BER_BVZERO( &e->e_name );
1559                 }
1560
1561                 if ( e->e_nname.bv_val == ndn.bv_val ) {
1562                         BER_BVZERO( &e->e_nname );
1563                 }
1564
1565                 entry_free( e );
1566         }
1567
1568         if ( !BER_BVISNULL( &dn ) ) {
1569                 ch_free( dn.bv_val );
1570         }
1571
1572         if ( !BER_BVISNULL( &ndn ) ) {
1573                 ch_free( ndn.bv_val );
1574         }
1575
1576         return rc;
1577 }
1578
1579 static int
1580 rwm_operational( Operation *op, SlapReply *rs )
1581 {
1582         /* FIXME: the entries are in the remote mapping form;
1583          * so we need to select those attributes we are willing
1584          * to return, and remap them accordingly */
1585         if ( rs->sr_operational_attrs ) {
1586                 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1587         }
1588
1589         return SLAP_CB_CONTINUE;
1590 }
1591
1592 #if 0
1593 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1594  * rewritten for subsequent operations; fine for plain suffixmassage,
1595  * but destroys everything else */
1596 static int
1597 rwm_chk_referrals( Operation *op, SlapReply *rs )
1598 {
1599         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1600         int                     rc;
1601
1602         rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1603         if ( rc != LDAP_SUCCESS ) {
1604                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1605                 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1606                 return -1;
1607         }
1608
1609         return SLAP_CB_CONTINUE;
1610 }
1611 #endif
1612
1613 static int
1614 rwm_rw_config(
1615         BackendDB       *be,
1616         const char      *fname,
1617         int             lineno,
1618         int             argc,
1619         char            **argv )
1620 {
1621         slap_overinst           *on = (slap_overinst *) be->bd_info;
1622         struct ldaprwmap        *rwmap = 
1623                         (struct ldaprwmap *)on->on_bi.bi_private;
1624
1625         return rewrite_parse( rwmap->rwm_rw,
1626                                 fname, lineno, argc, argv );
1627
1628         return 0;
1629 }
1630
1631 static int
1632 rwm_suffixmassage_config(
1633         BackendDB       *be,
1634         const char      *fname,
1635         int             lineno,
1636         int             argc,
1637         char            **argv )
1638 {
1639         slap_overinst           *on = (slap_overinst *) be->bd_info;
1640         struct ldaprwmap        *rwmap = 
1641                         (struct ldaprwmap *)on->on_bi.bi_private;
1642
1643         struct berval           bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1644         int                     massaged;
1645         int                     rc;
1646                 
1647         /*
1648          * syntax:
1649          * 
1650          *      suffixmassage [<suffix>] <massaged suffix>
1651          *
1652          * the [<suffix>] field must be defined as a valid suffix
1653          * for the current database;
1654          * the <massaged suffix> shouldn't have already been
1655          * defined as a valid suffix for the current server
1656          */
1657         if ( argc == 2 ) {
1658                 if ( be->be_suffix == NULL ) {
1659                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1660                                        " \"suffixMassage [<suffix>]"
1661                                        " <massaged suffix>\" without "
1662                                        "<suffix> part requires database "
1663                                        "suffix be defined first.\n",
1664                                 fname, lineno, 0 );
1665                         return 1;
1666                 }
1667                 bvnc = be->be_suffix[ 0 ];
1668                 massaged = 1;
1669
1670         } else if ( argc == 3 ) {
1671                 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1672                 massaged = 2;
1673
1674         } else  {
1675                 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is"
1676                                " \"suffixMassage [<suffix>]"
1677                                " <massaged suffix>\"\n",
1678                         fname, lineno, 0 );
1679                 return 1;
1680         }
1681
1682         if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1683                 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
1684                         fname, lineno, bvnc.bv_val );
1685                 return 1;
1686         }
1687
1688         ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1689         if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1690                 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
1691                                 fname, lineno, brnc.bv_val );
1692                 free( nvnc.bv_val );
1693                 free( pvnc.bv_val );
1694                 return 1;
1695         }
1696
1697         /*
1698          * The suffix massaging is emulated 
1699          * by means of the rewrite capabilities
1700          */
1701         rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1702                         &pvnc, &nvnc, &prnc, &nrnc );
1703         free( nvnc.bv_val );
1704         free( pvnc.bv_val );
1705         free( nrnc.bv_val );
1706         free( prnc.bv_val );
1707
1708         return rc;
1709 }
1710
1711 static int
1712 rwm_m_config(
1713         BackendDB       *be,
1714         const char      *fname,
1715         int             lineno,
1716         int             argc,
1717         char            **argv )
1718 {
1719         slap_overinst           *on = (slap_overinst *) be->bd_info;
1720         struct ldaprwmap        *rwmap = 
1721                         (struct ldaprwmap *)on->on_bi.bi_private;
1722
1723         /* objectclass/attribute mapping */
1724         return rwm_map_config( &rwmap->rwm_oc,
1725                         &rwmap->rwm_at,
1726                         fname, lineno, argc, argv );
1727 }
1728
1729 static int
1730 rwm_response( Operation *op, SlapReply *rs )
1731 {
1732         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1733         struct ldaprwmap        *rwmap = 
1734                         (struct ldaprwmap *)on->on_bi.bi_private;
1735
1736         int             rc;
1737
1738         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1739                 return rwm_send_entry( op, rs );
1740         }
1741
1742         switch( op->o_tag ) {
1743         case LDAP_REQ_SEARCH:
1744         case LDAP_REQ_BIND:
1745         case LDAP_REQ_ADD:
1746         case LDAP_REQ_DELETE:
1747         case LDAP_REQ_MODRDN:
1748         case LDAP_REQ_MODIFY:
1749         case LDAP_REQ_COMPARE:
1750         case LDAP_REQ_EXTENDED:
1751                 if ( rs->sr_ref ) {
1752                         dncookie                dc;
1753
1754                         /*
1755                          * Rewrite the dn of the referrals, if needed
1756                          */
1757                         dc.rwmap = rwmap;
1758                         dc.conn = op->o_conn;
1759                         dc.rs = NULL; 
1760                         dc.ctx = "referralDN";
1761                         rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1762                         if ( rc != LDAP_SUCCESS ) {
1763                                 rc = 1;
1764                                 break;
1765                         }
1766                 }
1767                 rc = rwm_matched( op, rs );
1768                 break;
1769
1770         default:
1771                 rc = SLAP_CB_CONTINUE;
1772                 break;
1773         }
1774
1775         return rc;
1776 }
1777
1778 static int
1779 rwm_db_config(
1780         BackendDB       *be,
1781         const char      *fname,
1782         int             lineno,
1783         int             argc,
1784         char            **argv )
1785 {
1786         slap_overinst           *on = (slap_overinst *) be->bd_info;
1787         struct ldaprwmap        *rwmap = 
1788                         (struct ldaprwmap *)on->on_bi.bi_private;
1789
1790         int             rc = 0;
1791         char            *argv0 = NULL;
1792
1793         if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1794                 argv0 = argv[ 0 ];
1795                 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1796         }
1797
1798         if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1799                 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1800
1801         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1802                 rc = rwm_m_config( be, fname, lineno, argc, argv );
1803
1804         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1805                 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1806
1807         } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1808                 if ( argc != 2 ) {
1809                         Debug( LDAP_DEBUG_ANY,
1810                 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1811                                         fname, lineno, 0 );
1812                         return( 1 );
1813                 }
1814
1815                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1816                         rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F_MASK2);
1817
1818                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1819                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1820
1821                 /* TODO: not implemented yet */
1822                 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1823                         Debug( LDAP_DEBUG_ANY,
1824                 "%s: line %d: \"discover\" not supported yet "
1825                 "in \"t-f-support {no|yes|discover}\".\n",
1826                                         fname, lineno, 0 );
1827                         return( 1 );
1828 #if 0
1829                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1830 #endif
1831
1832                 } else {
1833                         Debug( LDAP_DEBUG_ANY,
1834         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1835                                 fname, lineno, argv[ 1 ] );
1836                         return 1;
1837                 }
1838
1839         } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) ==  0 ) {
1840                 if ( argc !=2 ) { 
1841                         Debug( LDAP_DEBUG_ANY,
1842                 "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
1843                                         fname, lineno, 0 );
1844                         return( 1 );
1845                 }
1846
1847                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1848                         rwmap->rwm_flags &= ~(RWM_F_NORMALIZE_MAPPED_ATTRS);
1849
1850                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1851                         rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1852                 }
1853
1854         } else {
1855                 rc = SLAP_CONF_UNKNOWN;
1856         }
1857
1858         if ( argv0 ) {
1859                 argv[ 0 ] = argv0;
1860         }
1861
1862         return rc;
1863 }
1864
1865 /*
1866  * dynamic configuration...
1867  */
1868
1869 enum {
1870         /* rewrite */
1871         RWM_CF_REWRITE = 1,
1872
1873         /* map */
1874         RWM_CF_MAP,
1875         RWM_CF_T_F_SUPPORT,
1876         RWM_CF_NORMALIZE_MAPPED,
1877         RWM_CF_DROP_UNREQUESTED,
1878
1879         RWM_CF_LAST
1880 };
1881
1882 static slap_verbmasks t_f_mode[] = {
1883         { BER_BVC( "true" ),            RWM_F_SUPPORT_T_F },
1884         { BER_BVC( "yes" ),             RWM_F_SUPPORT_T_F },
1885         { BER_BVC( "discover" ),        RWM_F_SUPPORT_T_F_DISCOVER },
1886         { BER_BVC( "false" ),           RWM_F_NONE },
1887         { BER_BVC( "no" ),              RWM_F_NONE },
1888         { BER_BVNULL,                   0 }
1889 };
1890
1891 static ConfigDriver rwm_cf_gen;
1892
1893 static ConfigTable rwmcfg[] = {
1894         { "rwm-rewrite", "rewrite",
1895                 2, 0, STRLENOF("rwm-rewrite"),
1896                 ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1897                 "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
1898                         "DESC 'Rewrites strings' "
1899                         "EQUALITY caseIgnoreMatch "
1900                         "SYNTAX OMsDirectoryString "
1901                         "X-ORDERED 'VALUES' )",
1902                 NULL, NULL },
1903
1904         { "rwm-suffixmassage", "[virtual]> <real",
1905                 2, 3, 0, ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1906                 NULL, NULL, NULL },
1907                 
1908         { "rwm-t-f-support", "true|false|discover",
1909                 2, 2, 0, ARG_MAGIC|RWM_CF_T_F_SUPPORT, rwm_cf_gen,
1910                 "( OLcfgOvAt:16.2 NAME 'olcRwmTFSupport' "
1911                         "DESC 'Absolute filters support' "
1912                         "SYNTAX OMsDirectoryString "
1913                         "SINGLE-VALUE )",
1914                 NULL, NULL },
1915
1916         { "rwm-map", "{objectClass|attribute}",
1917                 2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
1918                 "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
1919                         "DESC 'maps attributes/objectClasses' "
1920                         "EQUALITY caseIgnoreMatch "
1921                         "SYNTAX OMsDirectoryString "
1922                         "X-ORDERED 'VALUES' )",
1923                 NULL, NULL },
1924
1925         { "rwm-normalize-mapped-attrs", "true|false",
1926                 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_NORMALIZE_MAPPED, rwm_cf_gen,
1927                 "( OLcfgOvAt:16.4 NAME 'olcRwmNormalizeMapped' "
1928                         "DESC 'Normalize mapped attributes/objectClasses' "
1929                         "SYNTAX OMsBoolean "
1930                         "SINGLE-VALUE )",
1931                 NULL, NULL },
1932
1933         { "rwm-drop-unrequested-attrs", "true|false",
1934                 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_DROP_UNREQUESTED, rwm_cf_gen,
1935                 "( OLcfgOvAt:16.5 NAME 'olcRwmDropUnrequested' "
1936                         "DESC 'Drop unrequested attributes' "
1937                         "SYNTAX OMsBoolean "
1938                         "SINGLE-VALUE )",
1939                 NULL, NULL },
1940
1941         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1942 };
1943
1944 static ConfigOCs rwmocs[] = {
1945         { "( OLcfgOvOc:16.1 "
1946                 "NAME 'olcRwmConfig' "
1947                 "DESC 'Rewrite/remap configuration' "
1948                 "SUP olcOverlayConfig "
1949                 "MAY ( "
1950                         "olcRwmRewrite $ "
1951                         "olcRwmTFSupport $ "
1952                         "olcRwmMap $ "
1953                         "olcRwmNormalizeMapped "
1954                         ") )",
1955                 Cft_Overlay, rwmcfg, NULL, NULL },
1956         { NULL, 0, NULL }
1957 };
1958
1959 static void
1960 slap_bv_x_ordered_unparse( BerVarray in, BerVarray *out )
1961 {
1962         int             i;
1963         BerVarray       bva = NULL;
1964         char            ibuf[32], *ptr;
1965         struct berval   idx;
1966
1967         assert( in != NULL );
1968
1969         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ )
1970                 /* count'em */ ;
1971
1972         if ( i == 0 ) {
1973                 return;
1974         }
1975
1976         idx.bv_val = ibuf;
1977
1978         bva = ch_malloc( ( i + 1 ) * sizeof(struct berval) );
1979         BER_BVZERO( &bva[ 0 ] );
1980
1981         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1982                 idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
1983                 if ( idx.bv_len >= sizeof( ibuf ) ) {
1984                         ber_bvarray_free( bva );
1985                         return;
1986                 }
1987
1988                 bva[i].bv_len = idx.bv_len + in[i].bv_len;
1989                 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1990                 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
1991                 ptr = lutil_strcopy( ptr, in[i].bv_val );
1992                 *ptr = '\0';
1993                 BER_BVZERO( &bva[ i + 1 ] );
1994         }
1995
1996         *out = bva;
1997 }
1998
1999 static int
2000 rwm_bva_add(
2001         BerVarray               *bva,
2002         int                     idx,
2003         char                    **argv )
2004 {
2005         char            *line;
2006         struct berval   bv;
2007
2008         line = ldap_charray2str( argv, "\" \"" );
2009         if ( line != NULL ) {
2010                 int     len = strlen( argv[ 0 ] );
2011
2012                 ber_str2bv( line, 0, 0, &bv );
2013                 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
2014                         bv.bv_len - ( len + 1 ) );
2015                 bv.bv_val[ bv.bv_len - 1 ] = '"';
2016
2017                 if ( idx == -1 ) {
2018                         ber_bvarray_add( bva, &bv );
2019
2020                 } else {
2021                         (*bva)[ idx ] = bv;
2022                 }
2023
2024                 return 0;
2025         }
2026
2027         return -1;
2028 }
2029
2030 static int
2031 rwm_bva_rewrite_add(
2032         struct ldaprwmap        *rwmap,
2033         int                     idx,
2034         char                    **argv )
2035 {
2036         return rwm_bva_add( &rwmap->rwm_bva_rewrite, idx, argv );
2037 }
2038
2039 static int
2040 rwm_bva_map_add(
2041         struct ldaprwmap        *rwmap,
2042         int                     idx,
2043         char                    **argv )
2044 {
2045         return rwm_bva_add( &rwmap->rwm_bva_map, idx, argv );
2046 }
2047
2048 static int
2049 rwm_info_init( struct rewrite_info ** rwm_rw )
2050 {
2051         char                    *rargv[ 3 ];
2052
2053         *rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
2054         if ( *rwm_rw == NULL ) {
2055                 return -1;
2056         }
2057
2058         /* this rewriteContext by default must be null;
2059          * rules can be added if required */
2060         rargv[ 0 ] = "rewriteContext";
2061         rargv[ 1 ] = "searchFilter";
2062         rargv[ 2 ] = NULL;
2063         rewrite_parse( *rwm_rw, "<suffix massage>", 1, 2, rargv );
2064
2065         rargv[ 0 ] = "rewriteContext";
2066         rargv[ 1 ] = "default";
2067         rargv[ 2 ] = NULL;
2068         rewrite_parse( *rwm_rw, "<suffix massage>", 2, 2, rargv );
2069
2070         return 0;
2071 }
2072
2073 static int
2074 rwm_cf_gen( ConfigArgs *c )
2075 {
2076         slap_overinst           *on = (slap_overinst *)c->bi;
2077         struct ldaprwmap        *rwmap = 
2078                         (struct ldaprwmap *)on->on_bi.bi_private;
2079
2080         BackendDB               db;
2081         char                    *argv0;
2082         int                     idx0 = 0;
2083         int                     rc = 0;
2084
2085         db = *c->be;
2086         db.bd_info = c->bi;
2087
2088         if ( c->op == SLAP_CONFIG_EMIT ) {
2089                 struct berval   bv = BER_BVNULL;
2090
2091                 switch ( c->type ) {
2092                 case RWM_CF_REWRITE:
2093                         if ( rwmap->rwm_bva_rewrite == NULL ) {
2094                                 rc = 1;
2095
2096                         } else {
2097                                 slap_bv_x_ordered_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
2098                                 if ( !c->rvalue_vals ) {
2099                                         rc = 1;
2100                                 }
2101                         }
2102                         break;
2103
2104                 case RWM_CF_T_F_SUPPORT:
2105                         enum_to_verb( t_f_mode, (rwmap->rwm_flags & RWM_F_SUPPORT_T_F_MASK2), &bv );
2106                         if ( BER_BVISNULL( &bv ) ) {
2107                                 /* there's something wrong... */
2108                                 assert( 0 );
2109                                 rc = 1;
2110
2111                         } else {
2112                                 value_add_one( &c->rvalue_vals, &bv );
2113                         }
2114                         break;
2115
2116                 case RWM_CF_MAP:
2117                         if ( rwmap->rwm_bva_map == NULL ) {
2118                                 rc = 1;
2119
2120                         } else {
2121                                 slap_bv_x_ordered_unparse( rwmap->rwm_bva_map, &c->rvalue_vals );
2122                                 if ( !c->rvalue_vals ) {
2123                                         rc = 1;
2124                                 }
2125                         }
2126                         break;
2127
2128                 case RWM_CF_NORMALIZE_MAPPED:
2129                         c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
2130                         break;
2131
2132                 case RWM_CF_DROP_UNREQUESTED:
2133                         c->value_int = ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS );
2134                         break;
2135
2136                 default:
2137                         assert( 0 );
2138                         rc = 1;
2139                 }
2140
2141                 return rc;
2142
2143         } else if ( c->op == LDAP_MOD_DELETE ) {
2144                 switch ( c->type ) {
2145                 case RWM_CF_REWRITE:
2146                         if ( c->valx >= 0 ) {
2147                                 int i;
2148
2149                                 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2150                                         /* count'em */ ;
2151
2152                                 if ( c->valx >= i ) {
2153                                         rc = 1;
2154                                         break;
2155                                 }
2156
2157                                 ber_memfree( rwmap->rwm_bva_rewrite[ c->valx ].bv_val );
2158                                 for ( i = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i + 1 ] ); i++ )
2159                                 {
2160                                         rwmap->rwm_bva_rewrite[ i ] = rwmap->rwm_bva_rewrite[ i + 1 ];
2161                                 }
2162                                 BER_BVZERO( &rwmap->rwm_bva_rewrite[ i ] );
2163
2164                                 rewrite_info_delete( &rwmap->rwm_rw );
2165                                 assert( rwmap->rwm_rw == NULL );
2166
2167                                 rc = rwm_info_init( &rwmap->rwm_rw );
2168
2169                                 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2170                                 {
2171                                         ConfigArgs ca = { 0 };
2172
2173                                         ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2174                                         ca.argc = 0;
2175                                         config_fp_parse_line( &ca );
2176                                         
2177                                         if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2178                                                 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2179                                                         ca.argc, ca.argv );
2180
2181                                         } else {
2182                                                 rc = rwm_rw_config( &db, c->fname, c->lineno,
2183                                                         ca.argc, ca.argv );
2184                                         }
2185
2186                                         ch_free( ca.tline );
2187                                         ch_free( ca.argv );
2188
2189                                         assert( rc == 0 );
2190                                 }
2191
2192                         } else if ( rwmap->rwm_rw != NULL ) {
2193                                 rewrite_info_delete( &rwmap->rwm_rw );
2194                                 assert( rwmap->rwm_rw == NULL );
2195
2196                                 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2197                                 rwmap->rwm_bva_rewrite = NULL;
2198
2199                                 rc = rwm_info_init( &rwmap->rwm_rw );
2200                         }
2201                         break;
2202
2203                 case RWM_CF_T_F_SUPPORT:
2204                         rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2205                         break;
2206
2207                 case RWM_CF_MAP:
2208                         if ( c->valx >= 0 ) {
2209                                 struct ldapmap rwm_oc = rwmap->rwm_oc;
2210                                 struct ldapmap rwm_at = rwmap->rwm_at;
2211                                 char *argv[5];
2212                                 int cnt = 0;
2213
2214                                 if ( rwmap->rwm_bva_map ) {
2215                                         for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
2216                                                 /* count */ ;
2217                                 }
2218
2219                                 if ( c->valx >= cnt ) {
2220                                         rc = 1;
2221                                         break;
2222                                 }
2223
2224                                 memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
2225                                 memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
2226
2227                                 /* re-parse all mappings except the one
2228                                  * that needs to be eliminated */
2229                                 argv[0] = "map";
2230                                 for ( cnt = 0; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2231                                         ConfigArgs ca = { 0 };
2232
2233                                         if ( cnt == c->valx ) {
2234                                                 continue;
2235                                         }
2236
2237                                         ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2238                                         ca.argc = 0;
2239                                         config_fp_parse_line( &ca );
2240                                         
2241                                         argv[1] = ca.argv[0];
2242                                         argv[2] = ca.argv[1];
2243                                         argv[3] = ca.argv[2];
2244                                         argv[4] = ca.argv[3];
2245                         
2246                                         rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2247
2248                                         ch_free( ca.tline );
2249                                         ch_free( ca.argv );
2250
2251                                         /* in case of failure, restore
2252                                          * the existing mapping */
2253                                         if ( rc ) {
2254                                                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2255                                                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2256                                                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2257                                                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2258                                                 rwmap->rwm_oc = rwm_oc;
2259                                                 rwmap->rwm_at = rwm_at;
2260                                                 break;
2261                                         }
2262                                 }
2263
2264                                 /* in case of success, destroy the old mapping
2265                                  * and eliminate the deleted one */
2266                                 if ( rc == 0 ) {
2267                                         avl_free( rwm_oc.remap, rwm_mapping_dst_free );
2268                                         avl_free( rwm_oc.map, rwm_mapping_free );
2269                                         avl_free( rwm_at.remap, rwm_mapping_dst_free );
2270                                         avl_free( rwm_at.map, rwm_mapping_free );
2271
2272                                         ber_memfree( rwmap->rwm_bva_map[ c->valx ].bv_val );
2273                                         for ( cnt = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2274                                                 rwmap->rwm_bva_map[ cnt ] = rwmap->rwm_bva_map[ cnt + 1 ];
2275                                         }
2276                                 }
2277
2278                         } else {
2279                                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2280                                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2281                                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2282                                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2283
2284                                 rwmap->rwm_oc.remap = NULL;
2285                                 rwmap->rwm_oc.map = NULL;
2286                                 rwmap->rwm_at.remap = NULL;
2287                                 rwmap->rwm_at.map = NULL;
2288
2289                                 ber_bvarray_free( rwmap->rwm_bva_map );
2290                                 rwmap->rwm_bva_map = NULL;
2291                         }
2292                         break;
2293
2294                 case RWM_CF_NORMALIZE_MAPPED:
2295                         rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2296                         break;
2297
2298                 case RWM_CF_DROP_UNREQUESTED:
2299                         rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2300                         break;
2301
2302                 default:
2303                         return 1;
2304                 }
2305                 return rc;
2306         }
2307
2308         if ( strncasecmp( c->argv[ 0 ], "olcRwm", STRLENOF( "olcRwm" ) ) == 0 ) {
2309                 idx0 = 1;
2310         }
2311
2312         switch ( c->type ) {
2313         case RWM_CF_REWRITE:
2314                 if ( c->valx >= 0 ) {
2315                         struct rewrite_info *rwm_rw = rwmap->rwm_rw;
2316                         int i, last;
2317
2318                         for ( last = 0; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ last ] ); last++ )
2319                                 /* count'em */ ;
2320
2321                         if ( c->valx > last ) {
2322                                 c->valx = last;
2323                         }
2324
2325                         rwmap->rwm_rw = NULL;
2326                         rc = rwm_info_init( &rwmap->rwm_rw );
2327
2328                         for ( i = 0; i < c->valx; i++ ) {
2329                                 ConfigArgs ca = { 0 };
2330
2331                                 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2332                                 ca.argc = 0;
2333                                 config_fp_parse_line( &ca );
2334
2335                                 argv0 = ca.argv[ 0 ];
2336                                 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2337                                 
2338                                 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2339                                         rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2340                                                 ca.argc, ca.argv );
2341
2342                                 } else {
2343                                         rc = rwm_rw_config( &db, c->fname, c->lineno,
2344                                                 ca.argc, ca.argv );
2345                                 }
2346
2347                                 ca.argv[ 0 ] = argv0;
2348
2349                                 ch_free( ca.tline );
2350                                 ch_free( ca.argv );
2351
2352                                 assert( rc == 0 );
2353                         }
2354
2355                         argv0 = c->argv[ idx0 ];
2356                         if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2357                                 return 1;
2358                         }
2359                         c->argv[ idx0 ] += STRLENOF( "rwm-" );
2360                         if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2361                                 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2362                                         c->argc - idx0, &c->argv[ idx0 ] );
2363
2364                         } else {
2365                                 rc = rwm_rw_config( &db, c->fname, c->lineno,
2366                                         c->argc - idx0, &c->argv[ idx0 ] );
2367                         }
2368                         c->argv[ idx0 ] = argv0;
2369                         if ( rc != 0 ) {
2370                                 rewrite_info_delete( &rwmap->rwm_rw );
2371                                 assert( rwmap->rwm_rw == NULL );
2372
2373                                 rwmap->rwm_rw = rwm_rw;
2374                                 return 1;
2375                         }
2376
2377                         for ( i = c->valx; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2378                         {
2379                                 ConfigArgs ca = { 0 };
2380
2381                                 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2382                                 ca.argc = 0;
2383                                 config_fp_parse_line( &ca );
2384                                 
2385                                 argv0 = ca.argv[ 0 ];
2386                                 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2387                                 
2388                                 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2389                                         rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2390                                                 ca.argc, ca.argv );
2391
2392                                 } else {
2393                                         rc = rwm_rw_config( &db, c->fname, c->lineno,
2394                                                 ca.argc, ca.argv );
2395                                 }
2396
2397                                 ca.argv[ 0 ] = argv0;
2398
2399                                 ch_free( ca.tline );
2400                                 ch_free( ca.argv );
2401
2402                                 assert( rc == 0 );
2403                         }
2404
2405                         rwmap->rwm_bva_rewrite = ch_realloc( rwmap->rwm_bva_rewrite,
2406                                 ( last + 2 )*sizeof( struct berval ) );
2407                         BER_BVZERO( &rwmap->rwm_bva_rewrite[last+1] );
2408
2409                         for ( i = last - 1; i >= c->valx; i-- )
2410                         {
2411                                 rwmap->rwm_bva_rewrite[ i + 1 ] = rwmap->rwm_bva_rewrite[ i ];
2412                         }
2413
2414                         rwm_bva_rewrite_add( rwmap, c->valx, &c->argv[ idx0 ] );
2415
2416                         rewrite_info_delete( &rwm_rw );
2417                         assert( rwm_rw == NULL );
2418
2419                         break;
2420                 }
2421
2422                 argv0 = c->argv[ idx0 ];
2423                 if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2424                         return 1;
2425                 }
2426                 c->argv[ idx0 ] += STRLENOF( "rwm-" );
2427                 if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2428                         rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2429                                 c->argc - idx0, &c->argv[ idx0 ] );
2430
2431                 } else {
2432                         rc = rwm_rw_config( &db, c->fname, c->lineno,
2433                                 c->argc - idx0, &c->argv[ idx0 ] );
2434                 }
2435                 c->argv[ idx0 ] = argv0;
2436                 if ( rc ) {
2437                         return 1;
2438
2439                 } else {
2440                         rwm_bva_rewrite_add( rwmap, -1, &c->argv[ idx0 ] );
2441                 }
2442                 break;
2443
2444         case RWM_CF_T_F_SUPPORT:
2445                 rc = verb_to_mask( c->argv[ 1 ], t_f_mode );
2446                 if ( BER_BVISNULL( &t_f_mode[ rc ].word ) ) {
2447                         return 1;
2448                 }
2449
2450                 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2451                 rwmap->rwm_flags |= t_f_mode[ rc ].mask;
2452                 rc = 0;
2453                 break;
2454
2455         case RWM_CF_MAP:
2456                 if ( c->valx >= 0 ) {
2457                         struct ldapmap rwm_oc = rwmap->rwm_oc;
2458                         struct ldapmap rwm_at = rwmap->rwm_at;
2459                         char *argv[5];
2460                         int cnt = 0;
2461
2462                         if ( rwmap->rwm_bva_map ) {
2463                                 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
2464                                         /* count */ ;
2465                         }
2466
2467                         if ( c->valx >= cnt ) {
2468                                 c->valx = cnt;
2469                         }
2470
2471                         memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
2472                         memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
2473
2474                         /* re-parse all mappings, including the one
2475                          * that needs to be added */
2476                         argv[0] = "map";
2477                         for ( cnt = 0; cnt < c->valx; cnt++ ) {
2478                                 ConfigArgs ca = { 0 };
2479
2480                                 ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2481                                 ca.argc = 0;
2482                                 config_fp_parse_line( &ca );
2483
2484                                 argv[1] = ca.argv[0];
2485                                 argv[2] = ca.argv[1];
2486                                 argv[3] = ca.argv[2];
2487                                 argv[4] = ca.argv[3];
2488                         
2489                                 rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2490
2491                                 ch_free( ca.tline );
2492                                 ch_free( ca.argv );
2493
2494                                 /* in case of failure, restore
2495                                  * the existing mapping */
2496                                 if ( rc ) {
2497                                         goto rwmmap_fail;
2498                                 }
2499                         }
2500
2501                         argv0 = c->argv[0];
2502                         c->argv[0] = "map";
2503                         rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
2504                         c->argv[0] = argv0;
2505                         if ( rc ) {
2506                                 goto rwmmap_fail;
2507                         }
2508
2509                         if ( rwmap->rwm_bva_map ) {
2510                                 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2511                                         ConfigArgs ca = { 0 };
2512
2513                                         ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2514                                         ca.argc = 0;
2515                                         config_fp_parse_line( &ca );
2516                         
2517                                         argv[1] = ca.argv[0];
2518                                         argv[2] = ca.argv[1];
2519                                         argv[3] = ca.argv[2];
2520                                         argv[4] = ca.argv[3];
2521                         
2522                                         rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2523
2524                                         ch_free( ca.tline );
2525                                         ch_free( ca.argv );
2526
2527                                         /* in case of failure, restore
2528                                          * the existing mapping */
2529                                         if ( rc ) {
2530                                                 goto rwmmap_fail;
2531                                         }
2532                                 }
2533                         }
2534
2535                         /* in case of success, destroy the old mapping
2536                          * and add the new one */
2537                         if ( rc == 0 ) {
2538                                 BerVarray tmp;
2539                                 struct berval bv, *bvp = &bv;
2540
2541                                 if ( rwm_bva_add( &bvp, 0, &c->argv[ idx0 ] ) ) {
2542                                         rc = 1;
2543                                         goto rwmmap_fail;
2544                                 }
2545                                         
2546                                 tmp = ber_memrealloc( rwmap->rwm_bva_map,
2547                                         sizeof( struct berval )*( cnt + 2 ) );
2548                                 if ( tmp == NULL ) {
2549                                         ber_memfree( bv.bv_val );
2550                                         rc = 1;
2551                                         goto rwmmap_fail;
2552                                 }
2553                                 rwmap->rwm_bva_map = tmp;
2554                                 BER_BVZERO( &rwmap->rwm_bva_map[ cnt + 1 ] );
2555
2556                                 avl_free( rwm_oc.remap, rwm_mapping_dst_free );
2557                                 avl_free( rwm_oc.map, rwm_mapping_free );
2558                                 avl_free( rwm_at.remap, rwm_mapping_dst_free );
2559                                 avl_free( rwm_at.map, rwm_mapping_free );
2560
2561                                 for ( ; cnt-- > c->valx; ) {
2562                                         rwmap->rwm_bva_map[ cnt + 1 ] = rwmap->rwm_bva_map[ cnt ];
2563                                 }
2564                                 rwmap->rwm_bva_map[ c->valx ] = bv;
2565
2566                         } else {
2567 rwmmap_fail:;
2568                                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2569                                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2570                                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2571                                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2572                                 rwmap->rwm_oc = rwm_oc;
2573                                 rwmap->rwm_at = rwm_at;
2574                         }
2575
2576                         break;
2577                 }
2578
2579                 argv0 = c->argv[ 0 ];
2580                 c->argv[ 0 ] += STRLENOF( "rwm-" );
2581                 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
2582                 c->argv[ 0 ] = argv0;
2583                 if ( rc ) {
2584                         return 1;
2585
2586                 } else {
2587                         char            *line;
2588                         struct berval   bv;
2589
2590                         line = ldap_charray2str( &c->argv[ 1 ], " " );
2591                         if ( line != NULL ) {
2592                                 ber_str2bv( line, 0, 0, &bv );
2593                                 ber_bvarray_add( &rwmap->rwm_bva_map, &bv );
2594                         }
2595                 }
2596                 break;
2597
2598         case RWM_CF_NORMALIZE_MAPPED:
2599                 if ( c->value_int ) {
2600                         rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
2601                 } else {
2602                         rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2603                 }
2604                 break;
2605
2606         case RWM_CF_DROP_UNREQUESTED:
2607                 if ( c->value_int ) {
2608                         rwmap->rwm_flags |= RWM_F_DROP_UNREQUESTED_ATTRS;
2609                 } else {
2610                         rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2611                 }
2612                 break;
2613
2614         default:
2615                 assert( 0 );
2616                 return 1;
2617         }
2618
2619         return rc;
2620 }
2621
2622 static int
2623 rwm_db_init(
2624         BackendDB       *be,
2625         ConfigReply     *cr )
2626 {
2627         slap_overinst           *on = (slap_overinst *) be->bd_info;
2628         struct ldaprwmap        *rwmap;
2629         int                     rc = 0;
2630
2631         rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
2632
2633         /* default */
2634         rwmap->rwm_flags = RWM_F_DROP_UNREQUESTED_ATTRS;
2635
2636         rc = rwm_info_init( &rwmap->rwm_rw );
2637
2638         on->on_bi.bi_private = (void *)rwmap;
2639
2640         if ( rc ) {
2641                 (void)rwm_db_destroy( be, NULL );
2642         }
2643
2644         return rc;
2645 }
2646
2647 static int
2648 rwm_db_destroy(
2649         BackendDB       *be,
2650         ConfigReply     *cr )
2651 {
2652         slap_overinst   *on = (slap_overinst *) be->bd_info;
2653         int             rc = 0;
2654
2655         if ( on->on_bi.bi_private ) {
2656                 struct ldaprwmap        *rwmap = 
2657                         (struct ldaprwmap *)on->on_bi.bi_private;
2658
2659                 if ( rwmap->rwm_rw ) {
2660                         rewrite_info_delete( &rwmap->rwm_rw );
2661                         if ( rwmap->rwm_bva_rewrite )
2662                                 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2663                 }
2664
2665                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2666                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2667                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2668                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2669                 ber_bvarray_free( rwmap->rwm_bva_map );
2670
2671                 ch_free( rwmap );
2672         }
2673
2674         return rc;
2675 }
2676
2677 static slap_overinst rwm = { { NULL } };
2678
2679 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2680 static
2681 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2682 int
2683 rwm_initialize( void )
2684 {
2685         int             rc;
2686
2687         /* Make sure we don't exceed the bits reserved for userland */
2688         config_check_userland( RWM_CF_LAST );
2689
2690         memset( &rwm, 0, sizeof( slap_overinst ) );
2691
2692         rwm.on_bi.bi_type = "rwm";
2693         rwm.on_bi.bi_flags =
2694                 SLAPO_BFLAG_SINGLE |
2695                 0;
2696
2697         rwm.on_bi.bi_db_init = rwm_db_init;
2698         rwm.on_bi.bi_db_config = rwm_db_config;
2699         rwm.on_bi.bi_db_destroy = rwm_db_destroy;
2700
2701         rwm.on_bi.bi_op_bind = rwm_op_bind;
2702         rwm.on_bi.bi_op_search = rwm_op_search;
2703         rwm.on_bi.bi_op_compare = rwm_op_compare;
2704         rwm.on_bi.bi_op_modify = rwm_op_modify;
2705         rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
2706         rwm.on_bi.bi_op_add = rwm_op_add;
2707         rwm.on_bi.bi_op_delete = rwm_op_delete;
2708         rwm.on_bi.bi_op_unbind = rwm_op_unbind;
2709         rwm.on_bi.bi_extended = rwm_extended;
2710 #if 1 /* TODO */
2711         rwm.on_bi.bi_entry_release_rw = rwm_entry_release_rw;
2712         rwm.on_bi.bi_entry_get_rw = rwm_entry_get_rw;
2713 #endif
2714
2715         rwm.on_bi.bi_operational = rwm_operational;
2716         rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
2717
2718         rwm.on_bi.bi_connection_init = rwm_conn_init;
2719         rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
2720
2721         rwm.on_response = rwm_response;
2722
2723         rwm.on_bi.bi_cf_ocs = rwmocs;
2724
2725         rc = config_register_schema( rwmcfg, rwmocs );
2726         if ( rc ) {
2727                 return rc;
2728         }
2729
2730         return overlay_register( &rwm );
2731 }
2732
2733 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2734 int
2735 init_module( int argc, char *argv[] )
2736 {
2737         return rwm_initialize();
2738 }
2739 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2740
2741 #endif /* SLAPD_OVER_RWM */