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