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