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