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