]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwm.c
As an overlay, always return SLAP_CB_CONTINUE
[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-2011 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 )
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 );
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 );
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 );
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 );
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 );
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 );
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         int                     rc;
870         BackendDB               db;
871         Operation               op2;
872         SlapReply               rs = { REP_SEARCH };
873
874         rwm_op_state            ros = { 0 };
875         struct berval           mndn = BER_BVNULL;
876
877         if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
878                 return SLAP_CB_CONTINUE;
879         }
880
881         /* massage DN */
882         op2.o_tag = LDAP_REQ_SEARCH;
883         op2 = *op;
884         op2.o_req_dn = *ndn;
885         op2.o_req_ndn = *ndn;
886         rc = rwm_op_dn_massage( &op2, &rs, "searchDN", &ros );
887         if ( rc != LDAP_SUCCESS ) {
888                 return LDAP_OTHER;
889         }
890
891         mndn = BER_BVISNULL( &ros.r_ndn ) ? *ndn : ros.r_ndn;
892
893         /* map attribute & objectClass */
894         if ( at != NULL ) {
895         }
896
897         if ( oc != NULL ) {
898         }
899
900         /* fetch entry */
901         db = *op->o_bd;
902         op2.o_bd = &db;
903         op2.o_bd->bd_info = (BackendInfo *)on->on_info->oi_orig;
904         op2.ors_attrs = slap_anlist_all_attributes;
905         rc = op2.o_bd->bd_info->bi_entry_get_rw( &op2, &mndn, oc, at, rw, ep );
906         if ( rc == LDAP_SUCCESS && *ep != NULL ) {
907                 /* we assume be_entry_release() needs to be called */
908                 rs.sr_flags = REP_ENTRY_MUSTRELEASE;
909                 rs.sr_entry = *ep;
910
911                 /* duplicate & release */
912                 op2.o_bd->bd_info = (BackendInfo *)on;
913                 rc = rwm_send_entry( &op2, &rs );
914                 if ( rc == SLAP_CB_CONTINUE ) {
915                         *ep = rs.sr_entry;
916                         rc = LDAP_SUCCESS;
917                 } else {
918                         assert( rc != LDAP_SUCCESS && rs.sr_entry == *ep );
919                         *ep = NULL;
920                         op2.o_bd->bd_info = (BackendInfo *)on->on_info;
921                         be_entry_release_r( &op2, rs.sr_entry );
922                         op2.o_bd->bd_info = (BackendInfo *)on;
923                 }
924         }
925
926         if ( !BER_BVISNULL( &ros.r_ndn) && ros.r_ndn.bv_val != ndn->bv_val ) {
927                 op->o_tmpfree( ros.r_ndn.bv_val, op->o_tmpmemctx );
928         }
929
930         return rc;
931 }
932
933 static int
934 rwm_op_search( Operation *op, SlapReply *rs )
935 {
936         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
937         struct ldaprwmap        *rwmap = 
938                         (struct ldaprwmap *)on->on_bi.bi_private;
939
940         int                     rc;
941         dncookie                dc;
942
943         struct berval           fstr = BER_BVNULL;
944         Filter                  *f = NULL;
945
946         AttributeName           *an = NULL;
947
948         char                    *text = NULL;
949
950         rwm_op_cb               *roc = rwm_callback_get( op );
951
952         rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
953                 "searchFilter", op->ors_filterstr.bv_val );
954         if ( rc == LDAP_SUCCESS )
955                 rc = rwm_op_dn_massage( op, rs, "searchDN", &roc->ros );
956         if ( rc != LDAP_SUCCESS ) {
957                 text = "searchDN massage error";
958                 goto error_return;
959         }
960
961         /*
962          * Rewrite the dn if needed
963          */
964         dc.rwmap = rwmap;
965         dc.conn = op->o_conn;
966         dc.rs = rs;
967         dc.ctx = "searchFilterAttrDN";
968
969         rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
970         if ( rc != LDAP_SUCCESS ) {
971                 text = "searchFilter/searchFilterAttrDN massage error";
972                 goto error_return;
973         }
974
975         f = str2filter_x( op, fstr.bv_val );
976
977         if ( f == NULL ) {
978                 text = "massaged filter parse error";
979                 goto error_return;
980         }
981
982         op->ors_filter = f;
983         op->ors_filterstr = fstr;
984
985         rc = rwm_map_attrnames( op, &rwmap->rwm_at, &rwmap->rwm_oc,
986                         op->ors_attrs, &an, RWM_MAP );
987         if ( rc != LDAP_SUCCESS ) {
988                 text = "attribute list mapping error";
989                 goto error_return;
990         }
991
992         op->ors_attrs = an;
993         /* store the mapped Attributes for later usage, in
994          * the case that other overlays change op->ors_attrs */
995         roc->ros.mapped_attrs = an;
996         roc->cb.sc_response = rwm_swap_attrs;
997
998         op->o_callback = &roc->cb;
999
1000         return SLAP_CB_CONTINUE;
1001
1002 error_return:;
1003         if ( an != NULL ) {
1004                 ch_free( an );
1005         }
1006
1007         if ( f != NULL ) {
1008                 filter_free_x( op, f, 1 );
1009         }
1010
1011         if ( !BER_BVISNULL( &fstr ) ) {
1012                 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1013         }
1014
1015         rwm_op_rollback( op, rs, &roc->ros );
1016         op->oq_search = roc->ros.oq_search;
1017         op->o_tmpfree( roc, op->o_tmpmemctx );
1018
1019         op->o_bd->bd_info = (BackendInfo *)on->on_info;
1020         send_ldap_error( op, rs, rc, text );
1021
1022         return -1;
1023
1024 }
1025
1026 static int
1027 rwm_exop_passwd( Operation *op, SlapReply *rs )
1028 {
1029         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1030         int                     rc;
1031         rwm_op_cb *roc;
1032
1033         struct berval   id = BER_BVNULL,
1034                         pwold = BER_BVNULL,
1035                         pwnew = BER_BVNULL;
1036         BerElement *ber = NULL;
1037
1038         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
1039                 return LDAP_SUCCESS;
1040         }
1041
1042         if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
1043                 rs->sr_err = LDAP_OTHER;
1044                 return rs->sr_err;
1045         }
1046
1047         rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
1048                 &pwold, &pwnew, &rs->sr_text );
1049         if ( rs->sr_err != LDAP_SUCCESS ) {
1050                 return rs->sr_err;
1051         }
1052
1053         if ( !BER_BVISNULL( &id ) ) {
1054                 char idNul = id.bv_val[id.bv_len];
1055                 id.bv_val[id.bv_len] = '\0';
1056                 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
1057                                 &op->o_req_ndn, op->o_tmpmemctx );
1058                 id.bv_val[id.bv_len] = idNul;
1059                 if ( rs->sr_err != LDAP_SUCCESS ) {
1060                         rs->sr_text = "Invalid DN";
1061                         return rs->sr_err;
1062                 }
1063
1064         } else {
1065                 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
1066                 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
1067         }
1068
1069         roc = rwm_callback_get( op );
1070
1071         rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1072         if ( rc != LDAP_SUCCESS ) {
1073                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1074                 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1075                 return -1;
1076         }
1077
1078         ber = ber_alloc_t( LBER_USE_DER );
1079         if ( !ber ) {
1080                 rs->sr_err = LDAP_OTHER;
1081                 rs->sr_text = "No memory";
1082                 return rs->sr_err;
1083         }
1084         ber_printf( ber, "{" );
1085         if ( !BER_BVISNULL( &id )) {
1086                 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, 
1087                         &op->o_req_dn );
1088         }
1089         if ( !BER_BVISNULL( &pwold )) {
1090                 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &pwold );
1091         }
1092         if ( !BER_BVISNULL( &pwnew )) {
1093                 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &pwnew );
1094         }
1095         ber_printf( ber, "N}" );
1096         ber_flatten( ber, &op->ore_reqdata );
1097         ber_free( ber, 1 );
1098
1099         op->o_callback = &roc->cb;
1100
1101         return SLAP_CB_CONTINUE;
1102 }
1103
1104 static struct exop {
1105         struct berval   oid;
1106         BI_op_extended  *extended;
1107 } exop_table[] = {
1108         { BER_BVC(LDAP_EXOP_MODIFY_PASSWD),     rwm_exop_passwd },
1109         { BER_BVNULL, NULL }
1110 };
1111
1112 static int
1113 rwm_extended( Operation *op, SlapReply *rs )
1114 {
1115         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1116         int                     rc;
1117         rwm_op_cb *roc;
1118
1119         int     i;
1120
1121         for ( i = 0; exop_table[i].extended != NULL; i++ ) {
1122                 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
1123                 {
1124                         rc = exop_table[i].extended( op, rs );
1125                         switch ( rc ) {
1126                         case LDAP_SUCCESS:
1127                                 break;
1128
1129                         case SLAP_CB_CONTINUE:
1130                         case SLAPD_ABANDON:
1131                                 return rc;
1132
1133                         default:
1134                                 send_ldap_result( op, rs );
1135                                 return rc;
1136                         }
1137                         break;
1138                 }
1139         }
1140
1141         roc = rwm_callback_get( op );
1142
1143         rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1144         if ( rc != LDAP_SUCCESS ) {
1145                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1146                 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1147                 return -1;
1148         }
1149
1150         /* TODO: rewrite/map extended data ? ... */
1151         op->o_callback = &roc->cb;
1152
1153         return SLAP_CB_CONTINUE;
1154 }
1155
1156 static void
1157 rwm_matched( Operation *op, SlapReply *rs )
1158 {
1159         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1160         struct ldaprwmap        *rwmap = 
1161                         (struct ldaprwmap *)on->on_bi.bi_private;
1162
1163         struct berval           dn, mdn;
1164         dncookie                dc;
1165         int                     rc;
1166
1167         if ( rs->sr_matched == NULL ) {
1168                 return;
1169         }
1170
1171         dc.rwmap = rwmap;
1172         dc.conn = op->o_conn;
1173         dc.rs = rs;
1174         dc.ctx = "matchedDN";
1175         ber_str2bv( rs->sr_matched, 0, 0, &dn );
1176         mdn = dn;
1177         rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
1178         if ( rc != LDAP_SUCCESS ) {
1179                 rs->sr_err = rc;
1180                 rs->sr_text = "Rewrite error";
1181
1182         } else if ( mdn.bv_val != dn.bv_val ) {
1183                 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
1184                         ch_free( (void *)rs->sr_matched );
1185
1186                 } else {
1187                         rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
1188                 }
1189                 rs->sr_matched = mdn.bv_val;
1190         }
1191 }
1192
1193 static int
1194 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
1195 {
1196         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1197         struct ldaprwmap        *rwmap = 
1198                         (struct ldaprwmap *)on->on_bi.bi_private;
1199
1200         dncookie                dc;
1201         int                     rc;
1202         Attribute               **ap;
1203         int                     isupdate;
1204         int                     check_duplicate_attrs = 0;
1205
1206         /*
1207          * Rewrite the dn attrs, if needed
1208          */
1209         dc.rwmap = rwmap;
1210         dc.conn = op->o_conn;
1211         dc.rs = NULL; 
1212
1213         /* FIXME: the entries are in the remote mapping form;
1214          * so we need to select those attributes we are willing
1215          * to return, and remap them accordingly */
1216
1217         /* FIXME: in principle, one could map an attribute
1218          * on top of another, which already exists.
1219          * As such, in the end there might exist more than
1220          * one instance of an attribute.
1221          * We should at least check if this occurs, and issue
1222          * an error (because multiple instances of attrs in 
1223          * response are not valid), or merge the values (what
1224          * about duplicate values?) */
1225         isupdate = be_shadow_update( op );
1226         for ( ap = a_first; *ap; ) {
1227                 struct ldapmapping      *mapping = NULL;
1228                 int                     drop_missing;
1229                 int                     last = -1;
1230                 Attribute               *a;
1231
1232                 if ( ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS ) &&
1233                                 op->ors_attrs != NULL && 
1234                                 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
1235                                 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
1236                 {
1237                         goto cleanup_attr;
1238                 }
1239
1240                 drop_missing = rwm_mapping( &rwmap->rwm_at,
1241                                 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
1242                 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
1243                 {
1244                         goto cleanup_attr;
1245                 }
1246                 if ( mapping != NULL ) {
1247                         assert( mapping->m_dst_ad != NULL );
1248
1249                         /* try to normalize mapped Attributes if the original 
1250                          * AttributeType was not normalized */
1251                         if ( (!(*ap)->a_desc->ad_type->sat_equality || 
1252                                 !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
1253                                 mapping->m_dst_ad->ad_type->sat_equality &&
1254                                 mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
1255                         {
1256                                 if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS))
1257                                 {
1258                                         int i = 0;
1259
1260                                         last = (*ap)->a_numvals;
1261                                         if ( last )
1262                                         {
1263                                                 (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
1264
1265                                                 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
1266                                                         int             rc;
1267                                                         /*
1268                                                          * check that each value is valid per syntax
1269                                                          * and pretty if appropriate
1270                                                          */
1271                                                         rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
1272                                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1273                                                                 mapping->m_dst_ad->ad_type->sat_syntax,
1274                                                                 mapping->m_dst_ad->ad_type->sat_equality,
1275                                                                 &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
1276                                                                 NULL );
1277
1278                                                         if ( rc != LDAP_SUCCESS ) {
1279                                                                 BER_BVZERO( &(*ap)->a_nvals[i] );
1280                                                         }
1281                                                 }
1282                                                 BER_BVZERO( &(*ap)->a_nvals[i] );
1283                                         }
1284
1285                                 } else {
1286                                         assert( (*ap)->a_nvals == (*ap)->a_vals );
1287                                         (*ap)->a_nvals = NULL;
1288                                         ber_bvarray_dup_x( &(*ap)->a_nvals, (*ap)->a_vals, NULL );
1289                                 }
1290                         }
1291
1292                         /* rewrite the attribute description */
1293                         (*ap)->a_desc = mapping->m_dst_ad;
1294
1295                         /* will need to check for duplicate attrs */
1296                         check_duplicate_attrs++;
1297                 }
1298
1299                 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
1300                         if ( stripEntryDN ) {
1301                                 /* will be generated by frontend */
1302                                 goto cleanup_attr;
1303                         }
1304                         
1305                 } else if ( !isupdate
1306                         && !get_relax( op )
1307                         && (*ap)->a_desc->ad_type->sat_no_user_mod 
1308                         && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
1309                 {
1310                         goto next_attr;
1311                 }
1312
1313                 if ( last == -1 ) { /* not yet counted */ 
1314                         last = (*ap)->a_numvals;
1315                 }
1316
1317                 if ( last == 0 ) {
1318                         /* empty? leave it in place because of attrsonly and vlv */
1319                         goto next_attr;
1320                 }
1321                 last--;
1322
1323                 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
1324                                 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
1325                 {
1326                         struct berval   *bv;
1327                         
1328                         for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1329                                 struct berval   mapped;
1330
1331                                 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
1332                                 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
1333 remove_oc:;
1334                                         ch_free( bv[0].bv_val );
1335                                         BER_BVZERO( &bv[0] );
1336                                         if ( &(*ap)->a_vals[last] > &bv[0] ) {
1337                                                 bv[0] = (*ap)->a_vals[last];
1338                                                 BER_BVZERO( &(*ap)->a_vals[last] );
1339                                         }
1340                                         last--;
1341                                         bv--;
1342
1343                                 } else if ( mapped.bv_val != bv[0].bv_val
1344                                         && ber_bvstrcasecmp( &mapped, &bv[0] ) != 0 )
1345                                 {
1346                                         int     i;
1347
1348                                         for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[ i ] ); i++ ) {
1349                                                 if ( &(*ap)->a_vals[ i ] == bv ) {
1350                                                         continue;
1351                                                 }
1352
1353                                                 if ( ber_bvstrcasecmp( &mapped, &(*ap)->a_vals[ i ] ) == 0 ) {
1354                                                         break;
1355                                                 }
1356                                         }
1357
1358                                         if ( !BER_BVISNULL( &(*ap)->a_vals[ i ] ) ) {
1359                                                 goto remove_oc;
1360                                         }
1361
1362                                         /*
1363                                          * FIXME: after LBER_FREEing
1364                                          * the value is replaced by
1365                                          * ch_alloc'ed memory
1366                                          */
1367                                         ber_bvreplace( &bv[0], &mapped );
1368
1369                                         /* FIXME: will need to check
1370                                          * if the structuralObjectClass
1371                                          * changed */
1372                                 }
1373                         }
1374
1375                 /*
1376                  * It is necessary to try to rewrite attributes with
1377                  * dn syntax because they might be used in ACLs as
1378                  * members of groups; since ACLs are applied to the
1379                  * rewritten stuff, no dn-based subject clause could
1380                  * be used at the ldap backend side (see
1381                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
1382                  * The problem can be overcome by moving the dn-based
1383                  * ACLs to the target directory server, and letting
1384                  * everything pass thru the ldap backend. */
1385                 /* FIXME: handle distinguishedName-like syntaxes, like
1386                  * nameAndOptionalUID */
1387                 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
1388                                 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
1389                 {
1390                         dc.ctx = "searchAttrDN";
1391                         rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals, (*ap)->a_nvals );
1392                         if ( rc != LDAP_SUCCESS ) {
1393                                 goto cleanup_attr;
1394                         }
1395
1396                 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
1397                         dc.ctx = "searchAttrDN";
1398                         rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
1399                         if ( rc != LDAP_SUCCESS ) {
1400                                 goto cleanup_attr;
1401                         }
1402                 }
1403
1404
1405 next_attr:;
1406                 ap = &(*ap)->a_next;
1407                 continue;
1408
1409 cleanup_attr:;
1410                 a = *ap;
1411                 *ap = (*ap)->a_next;
1412
1413                 attr_free( a );
1414         }
1415
1416         /* only check if some mapping occurred */
1417         if ( check_duplicate_attrs ) {
1418                 for ( ap = a_first; *ap != NULL; ap = &(*ap)->a_next ) {
1419                         Attribute       **tap;
1420
1421                         for ( tap = &(*ap)->a_next; *tap != NULL; ) {
1422                                 if ( (*tap)->a_desc == (*ap)->a_desc ) {
1423                                         Entry           e = { 0 };
1424                                         Modification    mod = { 0 };
1425                                         const char      *text = NULL;
1426                                         char            textbuf[ SLAP_TEXT_BUFLEN ];
1427                                         Attribute       *next = (*tap)->a_next;
1428
1429                                         BER_BVSTR( &e.e_name, "" );
1430                                         BER_BVSTR( &e.e_nname, "" );
1431                                         e.e_attrs = *ap;
1432                                         mod.sm_op = LDAP_MOD_ADD;
1433                                         mod.sm_desc = (*ap)->a_desc;
1434                                         mod.sm_type = mod.sm_desc->ad_cname;
1435                                         mod.sm_numvals = (*tap)->a_numvals;
1436                                         mod.sm_values = (*tap)->a_vals;
1437                                         if ( (*tap)->a_nvals != (*tap)->a_vals ) {
1438                                                 mod.sm_nvalues = (*tap)->a_nvals;
1439                                         }
1440
1441                                         (void)modify_add_values( &e, &mod,
1442                                                 /* permissive */ 1,
1443                                                 &text, textbuf, sizeof( textbuf ) );
1444
1445                                         /* should not insert new attrs! */
1446                                         assert( e.e_attrs == *ap );
1447
1448                                         attr_free( *tap );
1449                                         *tap = next;
1450
1451                                 } else {
1452                                         tap = &(*tap)->a_next;
1453                                 }
1454                         }
1455                 }
1456         }
1457
1458         return 0;
1459 }
1460
1461 /* Should return SLAP_CB_CONTINUE or failure, never LDAP_SUCCESS. */
1462 static int
1463 rwm_send_entry( Operation *op, SlapReply *rs )
1464 {
1465         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1466         struct ldaprwmap        *rwmap = 
1467                         (struct ldaprwmap *)on->on_bi.bi_private;
1468
1469         Entry                   *e = NULL;
1470         struct berval           dn = BER_BVNULL,
1471                                 ndn = BER_BVNULL;
1472         dncookie                dc;
1473         int                     rc;
1474
1475         assert( rs->sr_entry != NULL );
1476
1477         /*
1478          * Rewrite the dn of the result, if needed
1479          */
1480         dc.rwmap = rwmap;
1481         dc.conn = op->o_conn;
1482         dc.rs = NULL; 
1483         dc.ctx = "searchEntryDN";
1484
1485         e = rs->sr_entry;
1486         if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1487                 /* FIXME: all we need to duplicate are:
1488                  * - dn
1489                  * - ndn
1490                  * - attributes that are requested
1491                  * - no values if attrsonly is set
1492                  */
1493                 e = entry_dup( e );
1494                 if ( e == NULL ) {
1495                         rc = LDAP_NO_MEMORY;
1496                         goto fail;
1497                 }
1498         } else if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1499                 /* ITS#6423: REP_ENTRY_MUSTRELEASE incompatible
1500                  * with REP_ENTRY_MODIFIABLE */
1501                 rc = 1;
1502                 goto fail;
1503         }
1504
1505         /*
1506          * Note: this may fail if the target host(s) schema differs
1507          * from the one known to the meta, and a DN with unknown
1508          * attributes is returned.
1509          */
1510         dn = e->e_name;
1511         ndn = e->e_nname;
1512         rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1513         if ( rc != LDAP_SUCCESS ) {
1514                 rc = 1;
1515                 goto fail;
1516         }
1517
1518         if ( e->e_name.bv_val != dn.bv_val ) {
1519                 ch_free( e->e_name.bv_val );
1520                 ch_free( e->e_nname.bv_val );
1521
1522                 e->e_name = dn;
1523                 e->e_nname = ndn;
1524         }
1525
1526         /* TODO: map entry attribute types, objectclasses 
1527          * and dn-valued attribute values */
1528
1529         /* FIXME: the entries are in the remote mapping form;
1530          * so we need to select those attributes we are willing
1531          * to return, and remap them accordingly */
1532         (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1533
1534         if ( e != rs->sr_entry ) {
1535                 /* Reimplementing rs_replace_entry(), I suppose to
1536                  * bypass our own dubious rwm_entry_release_rw() */
1537                 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1538                         rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1539                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
1540                         be_entry_release_r( op, rs->sr_entry );
1541                         op->o_bd->bd_info = (BackendInfo *)on;
1542                 } else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
1543                         entry_free( rs->sr_entry );
1544                 }
1545                 rs->sr_entry = e;
1546                 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
1547         }
1548
1549         return SLAP_CB_CONTINUE;
1550
1551 fail:;
1552         if ( e != NULL && e != rs->sr_entry ) {
1553                 if ( e->e_name.bv_val == dn.bv_val ) {
1554                         BER_BVZERO( &e->e_name );
1555                 }
1556
1557                 if ( e->e_nname.bv_val == ndn.bv_val ) {
1558                         BER_BVZERO( &e->e_nname );
1559                 }
1560
1561                 entry_free( e );
1562         }
1563
1564         if ( !BER_BVISNULL( &dn ) ) {
1565                 ch_free( dn.bv_val );
1566         }
1567
1568         if ( !BER_BVISNULL( &ndn ) ) {
1569                 ch_free( ndn.bv_val );
1570         }
1571
1572         return rc;
1573 }
1574
1575 static int
1576 rwm_operational( Operation *op, SlapReply *rs )
1577 {
1578         /* FIXME: the entries are in the remote mapping form;
1579          * so we need to select those attributes we are willing
1580          * to return, and remap them accordingly */
1581         if ( rs->sr_operational_attrs ) {
1582                 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1583         }
1584
1585         return SLAP_CB_CONTINUE;
1586 }
1587
1588 #if 0
1589 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1590  * rewritten for subsequent operations; fine for plain suffixmassage,
1591  * but destroys everything else */
1592 static int
1593 rwm_chk_referrals( Operation *op, SlapReply *rs )
1594 {
1595         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1596         int                     rc;
1597
1598         rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1599         if ( rc != LDAP_SUCCESS ) {
1600                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1601                 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1602                 return -1;
1603         }
1604
1605         return SLAP_CB_CONTINUE;
1606 }
1607 #endif
1608
1609 static int
1610 rwm_rw_config(
1611         BackendDB       *be,
1612         const char      *fname,
1613         int             lineno,
1614         int             argc,
1615         char            **argv )
1616 {
1617         slap_overinst           *on = (slap_overinst *) be->bd_info;
1618         struct ldaprwmap        *rwmap = 
1619                         (struct ldaprwmap *)on->on_bi.bi_private;
1620
1621         return rewrite_parse( rwmap->rwm_rw,
1622                                 fname, lineno, argc, argv );
1623
1624         return 0;
1625 }
1626
1627 static int
1628 rwm_suffixmassage_config(
1629         BackendDB       *be,
1630         const char      *fname,
1631         int             lineno,
1632         int             argc,
1633         char            **argv )
1634 {
1635         slap_overinst           *on = (slap_overinst *) be->bd_info;
1636         struct ldaprwmap        *rwmap = 
1637                         (struct ldaprwmap *)on->on_bi.bi_private;
1638
1639         struct berval           bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1640         int                     massaged;
1641         int                     rc;
1642                 
1643         /*
1644          * syntax:
1645          * 
1646          *      suffixmassage [<suffix>] <massaged suffix>
1647          *
1648          * the [<suffix>] field must be defined as a valid suffix
1649          * for the current database;
1650          * the <massaged suffix> shouldn't have already been
1651          * defined as a valid suffix for the current server
1652          */
1653         if ( argc == 2 ) {
1654                 if ( be->be_suffix == NULL ) {
1655                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1656                                        " \"suffixMassage [<suffix>]"
1657                                        " <massaged suffix>\" without "
1658                                        "<suffix> part requires database "
1659                                        "suffix be defined first.\n",
1660                                 fname, lineno, 0 );
1661                         return 1;
1662                 }
1663                 bvnc = be->be_suffix[ 0 ];
1664                 massaged = 1;
1665
1666         } else if ( argc == 3 ) {
1667                 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1668                 massaged = 2;
1669
1670         } else  {
1671                 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is"
1672                                " \"suffixMassage [<suffix>]"
1673                                " <massaged suffix>\"\n",
1674                         fname, lineno, 0 );
1675                 return 1;
1676         }
1677
1678         if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1679                 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
1680                         fname, lineno, bvnc.bv_val );
1681                 return 1;
1682         }
1683
1684         ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1685         if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1686                 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
1687                                 fname, lineno, brnc.bv_val );
1688                 free( nvnc.bv_val );
1689                 free( pvnc.bv_val );
1690                 return 1;
1691         }
1692
1693         /*
1694          * The suffix massaging is emulated 
1695          * by means of the rewrite capabilities
1696          */
1697         rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1698                         &pvnc, &nvnc, &prnc, &nrnc );
1699         free( nvnc.bv_val );
1700         free( pvnc.bv_val );
1701         free( nrnc.bv_val );
1702         free( prnc.bv_val );
1703
1704         return rc;
1705 }
1706
1707 static int
1708 rwm_m_config(
1709         BackendDB       *be,
1710         const char      *fname,
1711         int             lineno,
1712         int             argc,
1713         char            **argv )
1714 {
1715         slap_overinst           *on = (slap_overinst *) be->bd_info;
1716         struct ldaprwmap        *rwmap = 
1717                         (struct ldaprwmap *)on->on_bi.bi_private;
1718
1719         /* objectclass/attribute mapping */
1720         return rwm_map_config( &rwmap->rwm_oc,
1721                         &rwmap->rwm_at,
1722                         fname, lineno, argc, argv );
1723 }
1724
1725 static int
1726 rwm_response( Operation *op, SlapReply *rs )
1727 {
1728         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1729         struct ldaprwmap        *rwmap = 
1730                         (struct ldaprwmap *)on->on_bi.bi_private;
1731
1732         int             rc;
1733
1734         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1735                 return rwm_send_entry( op, rs );
1736         }
1737
1738         switch( op->o_tag ) {
1739         case LDAP_REQ_SEARCH:
1740         case LDAP_REQ_BIND:
1741         case LDAP_REQ_ADD:
1742         case LDAP_REQ_DELETE:
1743         case LDAP_REQ_MODRDN:
1744         case LDAP_REQ_MODIFY:
1745         case LDAP_REQ_COMPARE:
1746         case LDAP_REQ_EXTENDED:
1747                 if ( rs->sr_ref ) {
1748                         dncookie                dc;
1749
1750                         /*
1751                          * Rewrite the dn of the referrals, if needed
1752                          */
1753                         dc.rwmap = rwmap;
1754                         dc.conn = op->o_conn;
1755                         dc.rs = NULL; 
1756                         dc.ctx = "referralDN";
1757                         rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1758                         /* FIXME: impossible, so far */
1759                         if ( rc != LDAP_SUCCESS ) {
1760                                 rs->sr_err = rc;
1761                                 break;
1762                         }
1763                 }
1764
1765                 rwm_matched( op, rs );
1766                 break;
1767         }
1768
1769         return SLAP_CB_CONTINUE;
1770 }
1771
1772 static int
1773 rwm_db_config(
1774         BackendDB       *be,
1775         const char      *fname,
1776         int             lineno,
1777         int             argc,
1778         char            **argv )
1779 {
1780         slap_overinst           *on = (slap_overinst *) be->bd_info;
1781         struct ldaprwmap        *rwmap = 
1782                         (struct ldaprwmap *)on->on_bi.bi_private;
1783
1784         int             rc = 0;
1785         char            *argv0 = NULL;
1786
1787         if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1788                 argv0 = argv[ 0 ];
1789                 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1790         }
1791
1792         if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1793                 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1794
1795         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1796                 rc = rwm_m_config( be, fname, lineno, argc, argv );
1797
1798         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1799                 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1800
1801         } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1802                 if ( argc != 2 ) {
1803                         Debug( LDAP_DEBUG_ANY,
1804                 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1805                                         fname, lineno, 0 );
1806                         return( 1 );
1807                 }
1808
1809                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1810                         rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F_MASK2);
1811
1812                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1813                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1814
1815                 /* TODO: not implemented yet */
1816                 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1817                         Debug( LDAP_DEBUG_ANY,
1818                 "%s: line %d: \"discover\" not supported yet "
1819                 "in \"t-f-support {no|yes|discover}\".\n",
1820                                         fname, lineno, 0 );
1821                         return( 1 );
1822 #if 0
1823                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1824 #endif
1825
1826                 } else {
1827                         Debug( LDAP_DEBUG_ANY,
1828         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1829                                 fname, lineno, argv[ 1 ] );
1830                         return 1;
1831                 }
1832
1833         } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) ==  0 ) {
1834                 if ( argc !=2 ) { 
1835                         Debug( LDAP_DEBUG_ANY,
1836                 "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
1837                                         fname, lineno, 0 );
1838                         return( 1 );
1839                 }
1840
1841                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1842                         rwmap->rwm_flags &= ~(RWM_F_NORMALIZE_MAPPED_ATTRS);
1843
1844                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1845                         rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1846                 }
1847
1848         } else {
1849                 rc = SLAP_CONF_UNKNOWN;
1850         }
1851
1852         if ( argv0 ) {
1853                 argv[ 0 ] = argv0;
1854         }
1855
1856         return rc;
1857 }
1858
1859 /*
1860  * dynamic configuration...
1861  */
1862
1863 enum {
1864         /* rewrite */
1865         RWM_CF_REWRITE = 1,
1866
1867         /* map */
1868         RWM_CF_MAP,
1869         RWM_CF_T_F_SUPPORT,
1870         RWM_CF_NORMALIZE_MAPPED,
1871         RWM_CF_DROP_UNREQUESTED,
1872
1873         RWM_CF_LAST
1874 };
1875
1876 static slap_verbmasks t_f_mode[] = {
1877         { BER_BVC( "true" ),            RWM_F_SUPPORT_T_F },
1878         { BER_BVC( "yes" ),             RWM_F_SUPPORT_T_F },
1879         { BER_BVC( "discover" ),        RWM_F_SUPPORT_T_F_DISCOVER },
1880         { BER_BVC( "false" ),           RWM_F_NONE },
1881         { BER_BVC( "no" ),              RWM_F_NONE },
1882         { BER_BVNULL,                   0 }
1883 };
1884
1885 static ConfigDriver rwm_cf_gen;
1886
1887 static ConfigTable rwmcfg[] = {
1888         { "rwm-rewrite", "rewrite",
1889                 2, 0, STRLENOF("rwm-rewrite"),
1890                 ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1891                 "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
1892                         "DESC 'Rewrites strings' "
1893                         "EQUALITY caseIgnoreMatch "
1894                         "SYNTAX OMsDirectoryString "
1895                         "X-ORDERED 'VALUES' )",
1896                 NULL, NULL },
1897
1898         { "rwm-suffixmassage", "[virtual]> <real",
1899                 2, 3, 0, ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1900                 NULL, NULL, NULL },
1901                 
1902         { "rwm-t-f-support", "true|false|discover",
1903                 2, 2, 0, ARG_MAGIC|RWM_CF_T_F_SUPPORT, rwm_cf_gen,
1904                 "( OLcfgOvAt:16.2 NAME 'olcRwmTFSupport' "
1905                         "DESC 'Absolute filters support' "
1906                         "SYNTAX OMsDirectoryString "
1907                         "SINGLE-VALUE )",
1908                 NULL, NULL },
1909
1910         { "rwm-map", "{objectClass|attribute}",
1911                 2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
1912                 "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
1913                         "DESC 'maps attributes/objectClasses' "
1914                         "EQUALITY caseIgnoreMatch "
1915                         "SYNTAX OMsDirectoryString "
1916                         "X-ORDERED 'VALUES' )",
1917                 NULL, NULL },
1918
1919         { "rwm-normalize-mapped-attrs", "true|false",
1920                 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_NORMALIZE_MAPPED, rwm_cf_gen,
1921                 "( OLcfgOvAt:16.4 NAME 'olcRwmNormalizeMapped' "
1922                         "DESC 'Normalize mapped attributes/objectClasses' "
1923                         "SYNTAX OMsBoolean "
1924                         "SINGLE-VALUE )",
1925                 NULL, NULL },
1926
1927         { "rwm-drop-unrequested-attrs", "true|false",
1928                 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_DROP_UNREQUESTED, rwm_cf_gen,
1929                 "( OLcfgOvAt:16.5 NAME 'olcRwmDropUnrequested' "
1930                         "DESC 'Drop unrequested attributes' "
1931                         "SYNTAX OMsBoolean "
1932                         "SINGLE-VALUE )",
1933                 NULL, NULL },
1934
1935         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1936 };
1937
1938 static ConfigOCs rwmocs[] = {
1939         { "( OLcfgOvOc:16.1 "
1940                 "NAME 'olcRwmConfig' "
1941                 "DESC 'Rewrite/remap configuration' "
1942                 "SUP olcOverlayConfig "
1943                 "MAY ( "
1944                         "olcRwmRewrite $ "
1945                         "olcRwmTFSupport $ "
1946                         "olcRwmMap $ "
1947                         "olcRwmNormalizeMapped "
1948                         ") )",
1949                 Cft_Overlay, rwmcfg, NULL, NULL },
1950         { NULL, 0, NULL }
1951 };
1952
1953 static void
1954 slap_bv_x_ordered_unparse( BerVarray in, BerVarray *out )
1955 {
1956         int             i;
1957         BerVarray       bva = NULL;
1958         char            ibuf[32], *ptr;
1959         struct berval   idx;
1960
1961         assert( in != NULL );
1962
1963         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ )
1964                 /* count'em */ ;
1965
1966         if ( i == 0 ) {
1967                 return;
1968         }
1969
1970         idx.bv_val = ibuf;
1971
1972         bva = ch_malloc( ( i + 1 ) * sizeof(struct berval) );
1973         BER_BVZERO( &bva[ 0 ] );
1974
1975         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1976                 idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
1977                 if ( idx.bv_len >= sizeof( ibuf ) ) {
1978                         ber_bvarray_free( bva );
1979                         return;
1980                 }
1981
1982                 bva[i].bv_len = idx.bv_len + in[i].bv_len;
1983                 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1984                 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
1985                 ptr = lutil_strcopy( ptr, in[i].bv_val );
1986                 *ptr = '\0';
1987                 BER_BVZERO( &bva[ i + 1 ] );
1988         }
1989
1990         *out = bva;
1991 }
1992
1993 static int
1994 rwm_bva_add(
1995         BerVarray               *bva,
1996         int                     idx,
1997         char                    **argv )
1998 {
1999         char            *line;
2000         struct berval   bv;
2001
2002         line = ldap_charray2str( argv, "\" \"" );
2003         if ( line != NULL ) {
2004                 int     len = strlen( argv[ 0 ] );
2005
2006                 ber_str2bv( line, 0, 0, &bv );
2007                 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
2008                         bv.bv_len - ( len + 1 ) );
2009                 bv.bv_val[ bv.bv_len - 1 ] = '"';
2010
2011                 if ( idx == -1 ) {
2012                         ber_bvarray_add( bva, &bv );
2013
2014                 } else {
2015                         (*bva)[ idx ] = bv;
2016                 }
2017
2018                 return 0;
2019         }
2020
2021         return -1;
2022 }
2023
2024 static int
2025 rwm_bva_rewrite_add(
2026         struct ldaprwmap        *rwmap,
2027         int                     idx,
2028         char                    **argv )
2029 {
2030         return rwm_bva_add( &rwmap->rwm_bva_rewrite, idx, argv );
2031 }
2032
2033 #ifdef unused
2034 static int
2035 rwm_bva_map_add(
2036         struct ldaprwmap        *rwmap,
2037         int                     idx,
2038         char                    **argv )
2039 {
2040         return rwm_bva_add( &rwmap->rwm_bva_map, idx, argv );
2041 }
2042 #endif /* unused */
2043
2044 static int
2045 rwm_info_init( struct rewrite_info ** rwm_rw )
2046 {
2047         char                    *rargv[ 3 ];
2048
2049         *rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
2050         if ( *rwm_rw == NULL ) {
2051                 return -1;
2052         }
2053
2054         /* this rewriteContext by default must be null;
2055          * rules can be added if required */
2056         rargv[ 0 ] = "rewriteContext";
2057         rargv[ 1 ] = "searchFilter";
2058         rargv[ 2 ] = NULL;
2059         rewrite_parse( *rwm_rw, "<suffix massage>", 1, 2, rargv );
2060
2061         rargv[ 0 ] = "rewriteContext";
2062         rargv[ 1 ] = "default";
2063         rargv[ 2 ] = NULL;
2064         rewrite_parse( *rwm_rw, "<suffix massage>", 2, 2, rargv );
2065
2066         return 0;
2067 }
2068
2069 static int
2070 rwm_cf_gen( ConfigArgs *c )
2071 {
2072         slap_overinst           *on = (slap_overinst *)c->bi;
2073         struct ldaprwmap        *rwmap = 
2074                         (struct ldaprwmap *)on->on_bi.bi_private;
2075
2076         BackendDB               db;
2077         char                    *argv0;
2078         int                     idx0 = 0;
2079         int                     rc = 0;
2080
2081         db = *c->be;
2082         db.bd_info = c->bi;
2083
2084         if ( c->op == SLAP_CONFIG_EMIT ) {
2085                 struct berval   bv = BER_BVNULL;
2086
2087                 switch ( c->type ) {
2088                 case RWM_CF_REWRITE:
2089                         if ( rwmap->rwm_bva_rewrite == NULL ) {
2090                                 rc = 1;
2091
2092                         } else {
2093                                 slap_bv_x_ordered_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
2094                                 if ( !c->rvalue_vals ) {
2095                                         rc = 1;
2096                                 }
2097                         }
2098                         break;
2099
2100                 case RWM_CF_T_F_SUPPORT:
2101                         enum_to_verb( t_f_mode, (rwmap->rwm_flags & RWM_F_SUPPORT_T_F_MASK2), &bv );
2102                         if ( BER_BVISNULL( &bv ) ) {
2103                                 /* there's something wrong... */
2104                                 assert( 0 );
2105                                 rc = 1;
2106
2107                         } else {
2108                                 value_add_one( &c->rvalue_vals, &bv );
2109                         }
2110                         break;
2111
2112                 case RWM_CF_MAP:
2113                         if ( rwmap->rwm_bva_map == NULL ) {
2114                                 rc = 1;
2115
2116                         } else {
2117                                 slap_bv_x_ordered_unparse( rwmap->rwm_bva_map, &c->rvalue_vals );
2118                                 if ( !c->rvalue_vals ) {
2119                                         rc = 1;
2120                                 }
2121                         }
2122                         break;
2123
2124                 case RWM_CF_NORMALIZE_MAPPED:
2125                         c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
2126                         break;
2127
2128                 case RWM_CF_DROP_UNREQUESTED:
2129                         c->value_int = ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS );
2130                         break;
2131
2132                 default:
2133                         assert( 0 );
2134                         rc = 1;
2135                 }
2136
2137                 return rc;
2138
2139         } else if ( c->op == LDAP_MOD_DELETE ) {
2140                 switch ( c->type ) {
2141                 case RWM_CF_REWRITE:
2142                         if ( c->valx >= 0 ) {
2143                                 int i;
2144
2145                                 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2146                                         /* count'em */ ;
2147
2148                                 if ( c->valx >= i ) {
2149                                         rc = 1;
2150                                         break;
2151                                 }
2152
2153                                 ber_memfree( rwmap->rwm_bva_rewrite[ c->valx ].bv_val );
2154                                 for ( i = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i + 1 ] ); i++ )
2155                                 {
2156                                         rwmap->rwm_bva_rewrite[ i ] = rwmap->rwm_bva_rewrite[ i + 1 ];
2157                                 }
2158                                 BER_BVZERO( &rwmap->rwm_bva_rewrite[ i ] );
2159
2160                                 rewrite_info_delete( &rwmap->rwm_rw );
2161                                 assert( rwmap->rwm_rw == NULL );
2162
2163                                 rc = rwm_info_init( &rwmap->rwm_rw );
2164
2165                                 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2166                                 {
2167                                         ConfigArgs ca = { 0 };
2168
2169                                         ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2170                                         ca.argc = 0;
2171                                         config_fp_parse_line( &ca );
2172                                         
2173                                         if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2174                                                 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2175                                                         ca.argc, ca.argv );
2176
2177                                         } else {
2178                                                 rc = rwm_rw_config( &db, c->fname, c->lineno,
2179                                                         ca.argc, ca.argv );
2180                                         }
2181
2182                                         ch_free( ca.tline );
2183                                         ch_free( ca.argv );
2184
2185                                         assert( rc == 0 );
2186                                 }
2187
2188                         } else if ( rwmap->rwm_rw != NULL ) {
2189                                 rewrite_info_delete( &rwmap->rwm_rw );
2190                                 assert( rwmap->rwm_rw == NULL );
2191
2192                                 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2193                                 rwmap->rwm_bva_rewrite = NULL;
2194
2195                                 rc = rwm_info_init( &rwmap->rwm_rw );
2196                         }
2197                         break;
2198
2199                 case RWM_CF_T_F_SUPPORT:
2200                         rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2201                         break;
2202
2203                 case RWM_CF_MAP:
2204                         if ( c->valx >= 0 ) {
2205                                 struct ldapmap rwm_oc = rwmap->rwm_oc;
2206                                 struct ldapmap rwm_at = rwmap->rwm_at;
2207                                 char *argv[5];
2208                                 int cnt = 0;
2209
2210                                 if ( rwmap->rwm_bva_map ) {
2211                                         for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
2212                                                 /* count */ ;
2213                                 }
2214
2215                                 if ( c->valx >= cnt ) {
2216                                         rc = 1;
2217                                         break;
2218                                 }
2219
2220                                 memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
2221                                 memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
2222
2223                                 /* re-parse all mappings except the one
2224                                  * that needs to be eliminated */
2225                                 argv[0] = "map";
2226                                 for ( cnt = 0; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2227                                         ConfigArgs ca = { 0 };
2228
2229                                         if ( cnt == c->valx ) {
2230                                                 continue;
2231                                         }
2232
2233                                         ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2234                                         ca.argc = 0;
2235                                         config_fp_parse_line( &ca );
2236                                         
2237                                         argv[1] = ca.argv[0];
2238                                         argv[2] = ca.argv[1];
2239                                         argv[3] = ca.argv[2];
2240                                         argv[4] = ca.argv[3];
2241                         
2242                                         rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2243
2244                                         ch_free( ca.tline );
2245                                         ch_free( ca.argv );
2246
2247                                         /* in case of failure, restore
2248                                          * the existing mapping */
2249                                         if ( rc ) {
2250                                                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2251                                                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2252                                                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2253                                                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2254                                                 rwmap->rwm_oc = rwm_oc;
2255                                                 rwmap->rwm_at = rwm_at;
2256                                                 break;
2257                                         }
2258                                 }
2259
2260                                 /* in case of success, destroy the old mapping
2261                                  * and eliminate the deleted one */
2262                                 if ( rc == 0 ) {
2263                                         avl_free( rwm_oc.remap, rwm_mapping_dst_free );
2264                                         avl_free( rwm_oc.map, rwm_mapping_free );
2265                                         avl_free( rwm_at.remap, rwm_mapping_dst_free );
2266                                         avl_free( rwm_at.map, rwm_mapping_free );
2267
2268                                         ber_memfree( rwmap->rwm_bva_map[ c->valx ].bv_val );
2269                                         for ( cnt = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2270                                                 rwmap->rwm_bva_map[ cnt ] = rwmap->rwm_bva_map[ cnt + 1 ];
2271                                         }
2272                                 }
2273
2274                         } else {
2275                                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2276                                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2277                                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2278                                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2279
2280                                 rwmap->rwm_oc.remap = NULL;
2281                                 rwmap->rwm_oc.map = NULL;
2282                                 rwmap->rwm_at.remap = NULL;
2283                                 rwmap->rwm_at.map = NULL;
2284
2285                                 ber_bvarray_free( rwmap->rwm_bva_map );
2286                                 rwmap->rwm_bva_map = NULL;
2287                         }
2288                         break;
2289
2290                 case RWM_CF_NORMALIZE_MAPPED:
2291                         rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2292                         break;
2293
2294                 case RWM_CF_DROP_UNREQUESTED:
2295                         rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2296                         break;
2297
2298                 default:
2299                         return 1;
2300                 }
2301                 return rc;
2302         }
2303
2304         if ( strncasecmp( c->argv[ 0 ], "olcRwm", STRLENOF( "olcRwm" ) ) == 0 ) {
2305                 idx0 = 1;
2306         }
2307
2308         switch ( c->type ) {
2309         case RWM_CF_REWRITE:
2310                 if ( c->valx >= 0 ) {
2311                         struct rewrite_info *rwm_rw = rwmap->rwm_rw;
2312                         int i, last;
2313
2314                         for ( last = 0; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ last ] ); last++ )
2315                                 /* count'em */ ;
2316
2317                         if ( c->valx > last ) {
2318                                 c->valx = last;
2319                         }
2320
2321                         rwmap->rwm_rw = NULL;
2322                         rc = rwm_info_init( &rwmap->rwm_rw );
2323
2324                         for ( i = 0; i < c->valx; i++ ) {
2325                                 ConfigArgs ca = { 0 };
2326
2327                                 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2328                                 ca.argc = 0;
2329                                 config_fp_parse_line( &ca );
2330
2331                                 argv0 = ca.argv[ 0 ];
2332                                 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2333                                 
2334                                 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2335                                         rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2336                                                 ca.argc, ca.argv );
2337
2338                                 } else {
2339                                         rc = rwm_rw_config( &db, c->fname, c->lineno,
2340                                                 ca.argc, ca.argv );
2341                                 }
2342
2343                                 ca.argv[ 0 ] = argv0;
2344
2345                                 ch_free( ca.tline );
2346                                 ch_free( ca.argv );
2347
2348                                 assert( rc == 0 );
2349                         }
2350
2351                         argv0 = c->argv[ idx0 ];
2352                         if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2353                                 return 1;
2354                         }
2355                         c->argv[ idx0 ] += STRLENOF( "rwm-" );
2356                         if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2357                                 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2358                                         c->argc - idx0, &c->argv[ idx0 ] );
2359
2360                         } else {
2361                                 rc = rwm_rw_config( &db, c->fname, c->lineno,
2362                                         c->argc - idx0, &c->argv[ idx0 ] );
2363                         }
2364                         c->argv[ idx0 ] = argv0;
2365                         if ( rc != 0 ) {
2366                                 rewrite_info_delete( &rwmap->rwm_rw );
2367                                 assert( rwmap->rwm_rw == NULL );
2368
2369                                 rwmap->rwm_rw = rwm_rw;
2370                                 return 1;
2371                         }
2372
2373                         for ( i = c->valx; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2374                         {
2375                                 ConfigArgs ca = { 0 };
2376
2377                                 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2378                                 ca.argc = 0;
2379                                 config_fp_parse_line( &ca );
2380                                 
2381                                 argv0 = ca.argv[ 0 ];
2382                                 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2383                                 
2384                                 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2385                                         rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2386                                                 ca.argc, ca.argv );
2387
2388                                 } else {
2389                                         rc = rwm_rw_config( &db, c->fname, c->lineno,
2390                                                 ca.argc, ca.argv );
2391                                 }
2392
2393                                 ca.argv[ 0 ] = argv0;
2394
2395                                 ch_free( ca.tline );
2396                                 ch_free( ca.argv );
2397
2398                                 assert( rc == 0 );
2399                         }
2400
2401                         rwmap->rwm_bva_rewrite = ch_realloc( rwmap->rwm_bva_rewrite,
2402                                 ( last + 2 )*sizeof( struct berval ) );
2403                         BER_BVZERO( &rwmap->rwm_bva_rewrite[last+1] );
2404
2405                         for ( i = last - 1; i >= c->valx; i-- )
2406                         {
2407                                 rwmap->rwm_bva_rewrite[ i + 1 ] = rwmap->rwm_bva_rewrite[ i ];
2408                         }
2409
2410                         rwm_bva_rewrite_add( rwmap, c->valx, &c->argv[ idx0 ] );
2411
2412                         rewrite_info_delete( &rwm_rw );
2413                         assert( rwm_rw == NULL );
2414
2415                         break;
2416                 }
2417
2418                 argv0 = c->argv[ idx0 ];
2419                 if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2420                         return 1;
2421                 }
2422                 c->argv[ idx0 ] += STRLENOF( "rwm-" );
2423                 if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2424                         rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2425                                 c->argc - idx0, &c->argv[ idx0 ] );
2426
2427                 } else {
2428                         rc = rwm_rw_config( &db, c->fname, c->lineno,
2429                                 c->argc - idx0, &c->argv[ idx0 ] );
2430                 }
2431                 c->argv[ idx0 ] = argv0;
2432                 if ( rc ) {
2433                         return 1;
2434
2435                 } else {
2436                         rwm_bva_rewrite_add( rwmap, -1, &c->argv[ idx0 ] );
2437                 }
2438                 break;
2439
2440         case RWM_CF_T_F_SUPPORT:
2441                 rc = verb_to_mask( c->argv[ 1 ], t_f_mode );
2442                 if ( BER_BVISNULL( &t_f_mode[ rc ].word ) ) {
2443                         return 1;
2444                 }
2445
2446                 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2447                 rwmap->rwm_flags |= t_f_mode[ rc ].mask;
2448                 rc = 0;
2449                 break;
2450
2451         case RWM_CF_MAP:
2452                 if ( c->valx >= 0 ) {
2453                         struct ldapmap rwm_oc = rwmap->rwm_oc;
2454                         struct ldapmap rwm_at = rwmap->rwm_at;
2455                         char *argv[5];
2456                         int cnt = 0;
2457
2458                         if ( rwmap->rwm_bva_map ) {
2459                                 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
2460                                         /* count */ ;
2461                         }
2462
2463                         if ( c->valx >= cnt ) {
2464                                 c->valx = cnt;
2465                         }
2466
2467                         memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
2468                         memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
2469
2470                         /* re-parse all mappings, including the one
2471                          * that needs to be added */
2472                         argv[0] = "map";
2473                         for ( cnt = 0; cnt < c->valx; cnt++ ) {
2474                                 ConfigArgs ca = { 0 };
2475
2476                                 ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2477                                 ca.argc = 0;
2478                                 config_fp_parse_line( &ca );
2479
2480                                 argv[1] = ca.argv[0];
2481                                 argv[2] = ca.argv[1];
2482                                 argv[3] = ca.argv[2];
2483                                 argv[4] = ca.argv[3];
2484                         
2485                                 rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2486
2487                                 ch_free( ca.tline );
2488                                 ch_free( ca.argv );
2489
2490                                 /* in case of failure, restore
2491                                  * the existing mapping */
2492                                 if ( rc ) {
2493                                         goto rwmmap_fail;
2494                                 }
2495                         }
2496
2497                         argv0 = c->argv[0];
2498                         c->argv[0] = "map";
2499                         rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
2500                         c->argv[0] = argv0;
2501                         if ( rc ) {
2502                                 goto rwmmap_fail;
2503                         }
2504
2505                         if ( rwmap->rwm_bva_map ) {
2506                                 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2507                                         ConfigArgs ca = { 0 };
2508
2509                                         ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2510                                         ca.argc = 0;
2511                                         config_fp_parse_line( &ca );
2512                         
2513                                         argv[1] = ca.argv[0];
2514                                         argv[2] = ca.argv[1];
2515                                         argv[3] = ca.argv[2];
2516                                         argv[4] = ca.argv[3];
2517                         
2518                                         rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2519
2520                                         ch_free( ca.tline );
2521                                         ch_free( ca.argv );
2522
2523                                         /* in case of failure, restore
2524                                          * the existing mapping */
2525                                         if ( rc ) {
2526                                                 goto rwmmap_fail;
2527                                         }
2528                                 }
2529                         }
2530
2531                         /* in case of success, destroy the old mapping
2532                          * and add the new one */
2533                         if ( rc == 0 ) {
2534                                 BerVarray tmp;
2535                                 struct berval bv, *bvp = &bv;
2536
2537                                 if ( rwm_bva_add( &bvp, 0, &c->argv[ idx0 ] ) ) {
2538                                         rc = 1;
2539                                         goto rwmmap_fail;
2540                                 }
2541                                         
2542                                 tmp = ber_memrealloc( rwmap->rwm_bva_map,
2543                                         sizeof( struct berval )*( cnt + 2 ) );
2544                                 if ( tmp == NULL ) {
2545                                         ber_memfree( bv.bv_val );
2546                                         rc = 1;
2547                                         goto rwmmap_fail;
2548                                 }
2549                                 rwmap->rwm_bva_map = tmp;
2550                                 BER_BVZERO( &rwmap->rwm_bva_map[ cnt + 1 ] );
2551
2552                                 avl_free( rwm_oc.remap, rwm_mapping_dst_free );
2553                                 avl_free( rwm_oc.map, rwm_mapping_free );
2554                                 avl_free( rwm_at.remap, rwm_mapping_dst_free );
2555                                 avl_free( rwm_at.map, rwm_mapping_free );
2556
2557                                 for ( ; cnt-- > c->valx; ) {
2558                                         rwmap->rwm_bva_map[ cnt + 1 ] = rwmap->rwm_bva_map[ cnt ];
2559                                 }
2560                                 rwmap->rwm_bva_map[ c->valx ] = bv;
2561
2562                         } else {
2563 rwmmap_fail:;
2564                                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2565                                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2566                                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2567                                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2568                                 rwmap->rwm_oc = rwm_oc;
2569                                 rwmap->rwm_at = rwm_at;
2570                         }
2571
2572                         break;
2573                 }
2574
2575                 argv0 = c->argv[ 0 ];
2576                 c->argv[ 0 ] += STRLENOF( "rwm-" );
2577                 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
2578                 c->argv[ 0 ] = argv0;
2579                 if ( rc ) {
2580                         return 1;
2581
2582                 } else {
2583                         char            *line;
2584                         struct berval   bv;
2585
2586                         line = ldap_charray2str( &c->argv[ 1 ], " " );
2587                         if ( line != NULL ) {
2588                                 ber_str2bv( line, 0, 0, &bv );
2589                                 ber_bvarray_add( &rwmap->rwm_bva_map, &bv );
2590                         }
2591                 }
2592                 break;
2593
2594         case RWM_CF_NORMALIZE_MAPPED:
2595                 if ( c->value_int ) {
2596                         rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
2597                 } else {
2598                         rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2599                 }
2600                 break;
2601
2602         case RWM_CF_DROP_UNREQUESTED:
2603                 if ( c->value_int ) {
2604                         rwmap->rwm_flags |= RWM_F_DROP_UNREQUESTED_ATTRS;
2605                 } else {
2606                         rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2607                 }
2608                 break;
2609
2610         default:
2611                 assert( 0 );
2612                 return 1;
2613         }
2614
2615         return rc;
2616 }
2617
2618 static int
2619 rwm_db_init(
2620         BackendDB       *be,
2621         ConfigReply     *cr )
2622 {
2623         slap_overinst           *on = (slap_overinst *) be->bd_info;
2624         struct ldaprwmap        *rwmap;
2625         int                     rc = 0;
2626
2627         rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
2628
2629         /* default */
2630         rwmap->rwm_flags = RWM_F_DROP_UNREQUESTED_ATTRS;
2631
2632         rc = rwm_info_init( &rwmap->rwm_rw );
2633
2634         on->on_bi.bi_private = (void *)rwmap;
2635
2636         if ( rc ) {
2637                 (void)rwm_db_destroy( be, NULL );
2638         }
2639
2640         return rc;
2641 }
2642
2643 static int
2644 rwm_db_destroy(
2645         BackendDB       *be,
2646         ConfigReply     *cr )
2647 {
2648         slap_overinst   *on = (slap_overinst *) be->bd_info;
2649         int             rc = 0;
2650
2651         if ( on->on_bi.bi_private ) {
2652                 struct ldaprwmap        *rwmap = 
2653                         (struct ldaprwmap *)on->on_bi.bi_private;
2654
2655                 if ( rwmap->rwm_rw ) {
2656                         rewrite_info_delete( &rwmap->rwm_rw );
2657                         if ( rwmap->rwm_bva_rewrite )
2658                                 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2659                 }
2660
2661                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2662                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2663                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2664                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2665                 ber_bvarray_free( rwmap->rwm_bva_map );
2666
2667                 ch_free( rwmap );
2668         }
2669
2670         return rc;
2671 }
2672
2673 static slap_overinst rwm = { { NULL } };
2674
2675 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2676 static
2677 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2678 int
2679 rwm_initialize( void )
2680 {
2681         int             rc;
2682
2683         /* Make sure we don't exceed the bits reserved for userland */
2684         config_check_userland( RWM_CF_LAST );
2685
2686         memset( &rwm, 0, sizeof( slap_overinst ) );
2687
2688         rwm.on_bi.bi_type = "rwm";
2689         rwm.on_bi.bi_flags =
2690                 SLAPO_BFLAG_SINGLE |
2691                 0;
2692
2693         rwm.on_bi.bi_db_init = rwm_db_init;
2694         rwm.on_bi.bi_db_config = rwm_db_config;
2695         rwm.on_bi.bi_db_destroy = rwm_db_destroy;
2696
2697         rwm.on_bi.bi_op_bind = rwm_op_bind;
2698         rwm.on_bi.bi_op_search = rwm_op_search;
2699         rwm.on_bi.bi_op_compare = rwm_op_compare;
2700         rwm.on_bi.bi_op_modify = rwm_op_modify;
2701         rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
2702         rwm.on_bi.bi_op_add = rwm_op_add;
2703         rwm.on_bi.bi_op_delete = rwm_op_delete;
2704         rwm.on_bi.bi_op_unbind = rwm_op_unbind;
2705         rwm.on_bi.bi_extended = rwm_extended;
2706 #if 1 /* TODO */
2707         rwm.on_bi.bi_entry_release_rw = rwm_entry_release_rw;
2708         rwm.on_bi.bi_entry_get_rw = rwm_entry_get_rw;
2709 #endif
2710
2711         rwm.on_bi.bi_operational = rwm_operational;
2712         rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
2713
2714         rwm.on_bi.bi_connection_init = rwm_conn_init;
2715         rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
2716
2717         rwm.on_response = rwm_response;
2718
2719         rwm.on_bi.bi_cf_ocs = rwmocs;
2720
2721         rc = config_register_schema( rwmcfg, rwmocs );
2722         if ( rc ) {
2723                 return rc;
2724         }
2725
2726         return overlay_register( &rwm );
2727 }
2728
2729 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2730 int
2731 init_module( int argc, char *argv[] )
2732 {
2733         return rwm_initialize();
2734 }
2735 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2736
2737 #endif /* SLAPD_OVER_RWM */