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