]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwm.c
Reworked recent backend API changes, now using a separate struct,
[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-2007 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 static int
42 rwm_db_destroy( BackendDB *be, ConfigReply *cr );
43
44 typedef struct rwm_op_cb {
45         slap_callback cb;
46         rwm_op_state ros;
47 } rwm_op_cb;
48
49 static int
50 rwm_op_cleanup( Operation *op, SlapReply *rs )
51 {
52         slap_callback   *cb = op->o_callback;
53         rwm_op_state *ros = cb->sc_private;
54
55         if ( rs->sr_type == REP_RESULT || rs->sr_type == REP_EXTENDED ||
56                 op->o_abandon || rs->sr_err == SLAPD_ABANDON ) {
57
58                 op->o_req_dn = ros->ro_dn;
59                 op->o_req_ndn = ros->ro_ndn;
60
61                 if ( !BER_BVISNULL( &ros->r_dn )
62                         && ros->r_dn.bv_val != ros->r_ndn.bv_val )
63                 {
64                         ch_free( ros->r_dn.bv_val );
65                         BER_BVZERO( &ros->r_dn );
66                 }
67
68                 if ( !BER_BVISNULL( &ros->r_ndn ) ) {
69                         ch_free( ros->r_ndn.bv_val );
70                         BER_BVZERO( &ros->r_ndn );
71                 }
72
73                 switch( ros->r_tag ) {
74                 case LDAP_REQ_COMPARE:
75                         if ( op->orc_ava->aa_value.bv_val != ros->orc_ava->aa_value.bv_val )
76                                 op->o_tmpfree( op->orc_ava->aa_value.bv_val, op->o_tmpmemctx );
77                         op->orc_ava = ros->orc_ava;
78                         break;
79                 case LDAP_REQ_MODIFY:
80                         slap_mods_free( op->orm_modlist, 1 );
81                         op->orm_modlist = ros->orm_modlist;
82                         break;
83                 case LDAP_REQ_MODRDN:
84                         if ( op->orr_newSup != ros->orr_newSup ) {
85                                 ch_free( op->orr_newSup->bv_val );
86                                 ch_free( op->orr_nnewSup->bv_val );
87                                 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
88                                 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
89                                 op->orr_newSup = ros->orr_newSup;
90                                 op->orr_nnewSup = ros->orr_nnewSup;
91                         }
92                         break;
93                 case LDAP_REQ_SEARCH:
94                         ch_free( ros->mapped_attrs );
95                         filter_free_x( op, op->ors_filter );
96                         ch_free( op->ors_filterstr.bv_val );
97                         op->ors_attrs = ros->ors_attrs;
98                         op->ors_filter = ros->ors_filter;
99                         op->ors_filterstr = ros->ors_filterstr;
100                         break;
101                 case LDAP_REQ_EXTENDED:
102                         if ( op->ore_reqdata != ros->ore_reqdata ) {
103                                 ber_bvfree( op->ore_reqdata );
104                                 op->ore_reqdata = ros->ore_reqdata;
105                         }
106                         break;
107                 default:        break;
108                 }
109                 op->o_callback = op->o_callback->sc_next;
110                 op->o_tmpfree( cb, op->o_tmpmemctx );
111         }
112
113         return SLAP_CB_CONTINUE;
114 }
115
116 static rwm_op_cb *
117 rwm_callback_get( Operation *op, SlapReply *rs )
118 {
119         rwm_op_cb       *roc = NULL;
120
121         roc = op->o_tmpalloc( sizeof( struct rwm_op_cb ), op->o_tmpmemctx );
122         roc->cb.sc_cleanup = rwm_op_cleanup;
123         roc->cb.sc_response = NULL;
124         roc->cb.sc_next = op->o_callback;
125         roc->cb.sc_private = &roc->ros;
126         roc->ros.r_tag = op->o_tag;
127         roc->ros.ro_dn = op->o_req_dn;
128         roc->ros.ro_ndn = op->o_req_ndn;
129         roc->ros.o_request = op->o_request;
130         BER_BVZERO( &roc->ros.r_dn );
131         BER_BVZERO( &roc->ros.r_ndn );
132
133         return roc;
134 }
135
136
137 static int
138 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie,
139         rwm_op_state *ros )
140 {
141         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
142         struct ldaprwmap        *rwmap = 
143                         (struct ldaprwmap *)on->on_bi.bi_private;
144
145         struct berval           dn = BER_BVNULL,
146                                 ndn = BER_BVNULL;
147         int                     rc = 0;
148         dncookie                dc;
149
150         /*
151          * Rewrite the dn if needed
152          */
153         dc.rwmap = rwmap;
154         dc.conn = op->o_conn;
155         dc.rs = rs;
156         dc.ctx = (char *)cookie;
157
158         /* NOTE: in those cases where only the ndn is available,
159          * and the caller sets op->o_req_dn = op->o_req_ndn,
160          * only rewrite the op->o_req_ndn and use it as 
161          * op->o_req_dn as well */
162         ndn = op->o_req_ndn;
163         if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
164                 dn = op->o_req_dn;
165                 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
166         } else {
167                 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
168         }
169
170         if ( rc != LDAP_SUCCESS ) {
171                 return rc;
172         }
173
174         if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
175                         || ndn.bv_val == op->o_req_ndn.bv_val )
176         {
177                 return LDAP_SUCCESS;
178         }
179
180         if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
181                 op->o_req_dn = dn;
182                 ros->r_dn = dn;
183         } else {
184                 op->o_req_dn = ndn;
185         }
186         op->o_req_ndn = ndn;
187         ros->r_ndn = ndn;
188
189         return LDAP_SUCCESS;
190 }
191
192 static int
193 rwm_op_add( Operation *op, SlapReply *rs )
194 {
195         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
196         struct ldaprwmap        *rwmap = 
197                         (struct ldaprwmap *)on->on_bi.bi_private;
198
199         int                     rc,
200                                 i;
201         Attribute               **ap = NULL;
202         char                    *olddn = op->o_req_dn.bv_val;
203         int                     isupdate;
204
205         rwm_op_cb               *roc = rwm_callback_get( op, rs );
206
207         rc = rwm_op_dn_massage( op, rs, "addDN", &roc->ros );
208         if ( rc != LDAP_SUCCESS ) {
209                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
210                 send_ldap_error( op, rs, rc, "addDN massage error" );
211                 return -1;
212         }
213
214         if ( olddn != op->o_req_dn.bv_val ) {
215                 ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
216                 ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
217         }
218
219         /* Count number of attributes in entry */ 
220         isupdate = be_shadow_update( op );
221         for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
222                 Attribute       *a;
223
224                 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
225                                 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
226                 {
227                         int             j, last;
228
229                         for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[ last ] ); last++ )
230                                         /* count values */ ;
231                         last--;
232                         for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
233                                 struct ldapmapping      *mapping = NULL;
234
235                                 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
236                                                 &mapping, RWM_MAP );
237                                 if ( mapping == NULL ) {
238                                         if ( rwmap->rwm_at.drop_missing ) {
239                                                 /* FIXME: we allow to remove objectClasses as well;
240                                                  * if the resulting entry is inconsistent, that's
241                                                  * the relayed database's business...
242                                                  */
243                                                 ch_free( (*ap)->a_vals[ j ].bv_val );
244                                                 if ( last > j ) {
245                                                         (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
246                                                 }
247                                                 BER_BVZERO( &(*ap)->a_vals[ last ] );
248                                                 last--;
249                                                 j--;
250                                         }
251
252                                 } else {
253                                         ch_free( (*ap)->a_vals[ j ].bv_val );
254                                         ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
255                                 }
256                         }
257
258                 } else if ( !isupdate && !get_relax( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
259                 {
260                         goto next_attr;
261
262                 } else {
263                         struct ldapmapping      *mapping = NULL;
264
265                         ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
266                                         &mapping, RWM_MAP );
267                         if ( mapping == NULL ) {
268                                 if ( rwmap->rwm_at.drop_missing ) {
269                                         goto cleanup_attr;
270                                 }
271                         }
272
273                         if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
274                                         || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
275                         {
276                                 /*
277                                  * FIXME: rewrite could fail; in this case
278                                  * the operation should give up, right?
279                                  */
280                                 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
281                                                 (*ap)->a_vals,
282                                                 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
283                                 if ( rc ) {
284                                         goto cleanup_attr;
285                                 }
286
287                         } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
288                                 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
289                                                 (*ap)->a_vals,
290                                                 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
291                                 if ( rc != LDAP_SUCCESS ) {
292                                         goto cleanup_attr;
293                                 }
294                         }
295                 
296                         if ( mapping != NULL ) {
297                                 assert( mapping->m_dst_ad != NULL );
298                                 (*ap)->a_desc = mapping->m_dst_ad;
299                         }
300                 }
301
302 next_attr:;
303                 ap = &(*ap)->a_next;
304                 continue;
305
306 cleanup_attr:;
307                 /* FIXME: leaking attribute/values? */
308                 a = *ap;
309
310                 *ap = (*ap)->a_next;
311                 attr_free( a );
312         }
313
314         op->o_callback = &roc->cb;
315
316         return SLAP_CB_CONTINUE;
317 }
318
319 static int
320 rwm_conn_init( BackendDB *be, Connection *conn )
321 {
322         slap_overinst           *on = (slap_overinst *) be->bd_info;
323         struct ldaprwmap        *rwmap = 
324                         (struct ldaprwmap *)on->on_bi.bi_private;
325
326         ( void )rewrite_session_init( rwmap->rwm_rw, conn );
327
328         return SLAP_CB_CONTINUE;
329 }
330
331 static int
332 rwm_conn_destroy( BackendDB *be, Connection *conn )
333 {
334         slap_overinst           *on = (slap_overinst *) be->bd_info;
335         struct ldaprwmap        *rwmap = 
336                         (struct ldaprwmap *)on->on_bi.bi_private;
337
338         ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
339
340         return SLAP_CB_CONTINUE;
341 }
342
343 static int
344 rwm_op_bind( Operation *op, SlapReply *rs )
345 {
346         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
347         int                     rc;
348
349         rwm_op_cb               *roc = rwm_callback_get( op, rs );
350
351         rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
352         if ( rc != LDAP_SUCCESS ) {
353                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
354                 send_ldap_error( op, rs, rc, "bindDN massage error" );
355                 return -1;
356         }
357
358         op->o_callback = &roc->cb;
359
360         return SLAP_CB_CONTINUE;
361 }
362
363 static int
364 rwm_op_unbind( Operation *op, SlapReply *rs )
365 {
366         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
367         struct ldaprwmap        *rwmap = 
368                         (struct ldaprwmap *)on->on_bi.bi_private;
369
370         rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
371
372         return SLAP_CB_CONTINUE;
373 }
374
375 static int
376 rwm_op_compare( Operation *op, SlapReply *rs )
377 {
378         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
379         struct ldaprwmap        *rwmap = 
380                         (struct ldaprwmap *)on->on_bi.bi_private;
381
382         int                     rc;
383         struct berval           mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
384
385         rwm_op_cb               *roc = rwm_callback_get( op, rs );
386
387         rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
388         if ( rc != LDAP_SUCCESS ) {
389                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
390                 send_ldap_error( op, rs, rc, "compareDN massage error" );
391                 return -1;
392         }
393
394         /* if the attribute is an objectClass, try to remap its value */
395         if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
396                         || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
397         {
398                 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
399                                 &mapped_vals[0], RWM_MAP );
400                 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
401                 {
402                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
403                         send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
404                         return -1;
405
406                 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
407                         ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
408                                 op->o_tmpmemctx );
409                 }
410
411         } else {
412                 struct ldapmapping      *mapping = NULL;
413                 AttributeDescription    *ad = op->orc_ava->aa_desc;
414
415                 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
416                                 &mapping, RWM_MAP );
417                 if ( mapping == NULL ) {
418                         if ( rwmap->rwm_at.drop_missing ) {
419                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
420                                 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
421                                 return -1;
422                         }
423
424                 } else {
425                         assert( mapping->m_dst_ad != NULL );
426                         ad = mapping->m_dst_ad;
427                 }
428
429                 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
430                                 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
431                 {
432                         struct berval   *mapped_valsp[2];
433                         
434                         mapped_valsp[0] = &mapped_vals[0];
435                         mapped_valsp[1] = &mapped_vals[1];
436
437                         mapped_vals[0] = op->orc_ava->aa_value;
438
439                         rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
440
441                         if ( rc != LDAP_SUCCESS ) {
442                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
443                                 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
444                                 return -1;
445                         }
446
447                         if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
448                                 /* NOTE: if we get here, rwm_dnattr_rewrite()
449                                  * already freed the old value, so now 
450                                  * it's invalid */
451                                 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
452                                         op->o_tmpmemctx );
453                                 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
454                         }
455                 }
456                 op->orc_ava->aa_desc = ad;
457         }
458
459         op->o_callback = &roc->cb;
460
461         return SLAP_CB_CONTINUE;
462 }
463
464 static int
465 rwm_op_delete( Operation *op, SlapReply *rs )
466 {
467         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
468         int                     rc;
469
470         rwm_op_cb               *roc = rwm_callback_get( op, rs );
471
472         rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
473         if ( rc != LDAP_SUCCESS ) {
474                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
475                 send_ldap_error( op, rs, rc, "deleteDN massage error" );
476                 return -1;
477         }
478
479         op->o_callback = &roc->cb;
480
481         return SLAP_CB_CONTINUE;
482 }
483
484 static int
485 rwm_op_modify( Operation *op, SlapReply *rs )
486 {
487         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
488         struct ldaprwmap        *rwmap = 
489                         (struct ldaprwmap *)on->on_bi.bi_private;
490
491         int                     isupdate;
492         Modifications           **mlp;
493         int                     rc;
494
495         rwm_op_cb               *roc = rwm_callback_get( op, rs );
496
497         rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
498         if ( rc != LDAP_SUCCESS ) {
499                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
500                 send_ldap_error( op, rs, rc, "modifyDN massage error" );
501                 return -1;
502         }
503
504         isupdate = be_shadow_update( op );
505         for ( mlp = &op->oq_modify.rs_modlist; *mlp; ) {
506                 int                     is_oc = 0;
507                 Modifications           *ml;
508                 struct ldapmapping      *mapping = NULL;
509
510                 /* duplicate the modlist */
511                 ml = ch_malloc( sizeof( Modifications ));
512                 *ml = **mlp;
513                 *mlp = ml;
514
515                 if ( ml->sml_desc == slap_schema.si_ad_objectClass 
516                                 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
517                 {
518                         is_oc = 1;
519
520                 } else if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod  )
521                 {
522                         goto next_mod;
523
524                 } else {
525                         int                     drop_missing;
526
527                         drop_missing = rwm_mapping( &rwmap->rwm_at,
528                                         &ml->sml_desc->ad_cname,
529                                         &mapping, RWM_MAP );
530                         if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
531                         {
532                                 goto cleanup_mod;
533                         }
534                 }
535
536                 if ( ml->sml_values != NULL ) {
537                         int i, num;
538                         struct berval *bva;
539
540                         for ( num = 0; !BER_BVISNULL( &ml->sml_values[ num ] ); num++ )
541                                 /* count values */ ;
542
543                         bva = ch_malloc( (num+1) * sizeof( struct berval ));
544                         for (i=0; i<num; i++)
545                                 ber_dupbv( &bva[i], &ml->sml_values[i] );
546                         BER_BVZERO( &bva[i] );
547                         ml->sml_values = bva;
548
549                         if ( ml->sml_nvalues ) {
550                                 bva = ch_malloc( (num+1) * sizeof( struct berval ));
551                                 for (i=0; i<num; i++)
552                                         ber_dupbv( &bva[i], &ml->sml_nvalues[i] );
553                                 BER_BVZERO( &bva[i] );
554                                 ml->sml_nvalues = bva;
555                         }
556
557                         if ( is_oc ) {
558                                 int     last, j;
559
560                                 last = num-1;
561
562                                 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
563                                         struct ldapmapping      *oc_mapping = NULL;
564                 
565                                         ( void )rwm_mapping( &rwmap->rwm_oc, &ml->sml_values[ j ],
566                                                         &oc_mapping, RWM_MAP );
567                                         if ( oc_mapping == NULL ) {
568                                                 if ( rwmap->rwm_at.drop_missing ) {
569                                                         /* FIXME: we allow to remove objectClasses as well;
570                                                          * if the resulting entry is inconsistent, that's
571                                                          * the relayed database's business...
572                                                          */
573                                                         if ( last > j ) {
574                                                                 ch_free( ml->sml_values[ j ].bv_val );
575                                                                 ml->sml_values[ j ] = ml->sml_values[ last ];
576                                                         }
577                                                         BER_BVZERO( &ml->sml_values[ last ] );
578                                                         last--;
579                                                         j--;
580                                                 }
581         
582                                         } else {
583                                                 ch_free( ml->sml_values[ j ].bv_val );
584                                                 ber_dupbv( &ml->sml_values[ j ], &oc_mapping->m_dst );
585                                         }
586                                 }
587
588                         } else {
589                                 if ( ml->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
590                                                 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
591                                 {
592                                         rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
593                                                         ml->sml_values,
594                                                         ml->sml_nvalues ? &ml->sml_nvalues : NULL );
595
596                                 } else if ( ml->sml_desc == slap_schema.si_ad_ref ) {
597                                         rc = rwm_referral_rewrite( op, rs,
598                                                         "referralAttrDN",
599                                                         ml->sml_values,
600                                                         ml->sml_nvalues ? &ml->sml_nvalues : NULL );
601                                         if ( rc != LDAP_SUCCESS ) {
602                                                 goto cleanup_mod;
603                                         }
604                                 }
605
606                                 if ( rc != LDAP_SUCCESS ) {
607                                         goto cleanup_mod;
608                                 }
609                         }
610                 }
611
612 next_mod:;
613                 if ( mapping != NULL ) {
614                         /* use new attribute description */
615                         assert( mapping->m_dst_ad != NULL );
616                         ml->sml_desc = mapping->m_dst_ad;
617                 }
618
619                 mlp = &ml->sml_next;
620                 continue;
621
622 cleanup_mod:;
623                 ml = *mlp;
624                 *mlp = (*mlp)->sml_next;
625                 slap_mod_free( &ml->sml_mod, 0 );
626                 free( ml );
627         }
628
629         op->o_callback = &roc->cb;
630
631         return SLAP_CB_CONTINUE;
632 }
633
634 static int
635 rwm_op_modrdn( Operation *op, SlapReply *rs )
636 {
637         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
638         struct ldaprwmap        *rwmap = 
639                         (struct ldaprwmap *)on->on_bi.bi_private;
640         
641         int                     rc;
642
643         rwm_op_cb               *roc = rwm_callback_get( op, rs );
644
645         if ( op->orr_newSup ) {
646                 dncookie        dc;
647                 struct berval   nnewSup = BER_BVNULL;
648                 struct berval   newSup = BER_BVNULL;
649
650                 /*
651                  * Rewrite the new superior, if defined and required
652                  */
653                 dc.rwmap = rwmap;
654                 dc.conn = op->o_conn;
655                 dc.rs = rs;
656                 dc.ctx = "newSuperiorDN";
657                 newSup = *op->orr_newSup;
658                 nnewSup = *op->orr_nnewSup;
659                 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
660                 if ( rc != LDAP_SUCCESS ) {
661                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
662                         send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
663                         return -1;
664                 }
665
666                 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
667                         op->orr_newSup = op->o_tmpalloc( sizeof( struct berval ),
668                                 op->o_tmpmemctx );
669                         op->orr_nnewSup = op->o_tmpalloc( sizeof( struct berval ),
670                                 op->o_tmpmemctx );
671                         *op->orr_newSup = newSup;
672                         *op->orr_nnewSup = nnewSup;
673                 }
674         }
675
676         /*
677          * Rewrite the dn, if needed
678          */
679         rc = rwm_op_dn_massage( op, rs, "renameDN", &roc->ros );
680         if ( rc != LDAP_SUCCESS ) {
681                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
682                 send_ldap_error( op, rs, rc, "renameDN massage error" );
683                 if ( op->orr_newSup != roc->ros.orr_newSup ) {
684                         ch_free( op->orr_newSup->bv_val );
685                         ch_free( op->orr_nnewSup->bv_val );
686                         op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
687                         op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
688                         op->orr_newSup = roc->ros.orr_newSup;
689                         op->orr_nnewSup = roc->ros.orr_nnewSup;
690                 }
691                 return -1;
692         }
693
694         /* TODO: rewrite newRDN, attribute types, 
695          * values of DN-valued attributes ... */
696
697         op->o_callback = &roc->cb;
698
699         return SLAP_CB_CONTINUE;
700 }
701
702
703 static int
704 rwm_swap_attrs( Operation *op, SlapReply *rs )
705 {
706         slap_callback   *cb = op->o_callback;
707         rwm_op_state *ros = cb->sc_private;
708
709         rs->sr_attrs = ros->ors_attrs;
710
711         /* other overlays might have touched op->ors_attrs, 
712          * so we restore the original version here, otherwise
713          * attribute-mapping might fail */
714         op->ors_attrs = ros->mapped_attrs; 
715         
716         return SLAP_CB_CONTINUE;
717 }
718
719 static int
720 rwm_op_search( Operation *op, SlapReply *rs )
721 {
722         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
723         struct ldaprwmap        *rwmap = 
724                         (struct ldaprwmap *)on->on_bi.bi_private;
725
726         int                     rc;
727         dncookie                dc;
728
729         struct berval           fstr = BER_BVNULL;
730         Filter                  *f = NULL;
731
732         AttributeName           *an = NULL;
733
734         char                    *text = NULL;
735
736         rwm_op_cb               *roc = rwm_callback_get( op, rs );
737
738         rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
739                 "searchFilter", op->ors_filterstr.bv_val );
740         if ( rc == LDAP_SUCCESS )
741                 rc = rwm_op_dn_massage( op, rs, "searchDN", &roc->ros );
742         if ( rc != LDAP_SUCCESS ) {
743                 text = "searchDN massage error";
744                 goto error_return;
745         }
746
747         /*
748          * Rewrite the dn if needed
749          */
750         dc.rwmap = rwmap;
751         dc.conn = op->o_conn;
752         dc.rs = rs;
753         dc.ctx = "searchFilterAttrDN";
754
755         rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
756         if ( rc != LDAP_SUCCESS ) {
757                 text = "searchFilter/searchFilterAttrDN massage error";
758                 goto error_return;
759         }
760
761         f = str2filter_x( op, fstr.bv_val );
762
763         if ( f == NULL ) {
764                 text = "massaged filter parse error";
765                 goto error_return;
766         }
767
768         op->ors_filter = f;
769         op->ors_filterstr = fstr;
770
771         rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
772                         op->ors_attrs, &an, RWM_MAP );
773         if ( rc != LDAP_SUCCESS ) {
774                 text = "attribute list mapping error";
775                 goto error_return;
776         }
777
778         op->ors_attrs = an;
779         /* store the mapped Attributes for later usage, in
780          * the case that other overlays change op->ors_attrs */
781         roc->ros.mapped_attrs = an;
782         roc->cb.sc_response = rwm_swap_attrs;
783
784         op->o_callback = &roc->cb;
785
786         return SLAP_CB_CONTINUE;
787
788 error_return:;
789         if ( an != NULL ) {
790                 ch_free( an );
791         }
792
793         if ( f != NULL ) {
794                 filter_free_x( op, f );
795         }
796
797         if ( !BER_BVISNULL( &fstr ) ) {
798                 ch_free( fstr.bv_val );
799         }
800
801         op->oq_search = roc->ros.oq_search;
802
803         op->o_bd->bd_info = (BackendInfo *)on->on_info;
804         send_ldap_error( op, rs, rc, text );
805
806         return -1;
807
808 }
809
810 static int
811 rwm_exop_passwd( Operation *op, SlapReply *rs )
812 {
813         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
814         int                     rc;
815         rwm_op_cb *roc;
816
817         struct berval   id = BER_BVNULL,
818                         pwold = BER_BVNULL,
819                         pwnew = BER_BVNULL;
820         BerElement *ber = NULL;
821
822         if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
823                 return LDAP_SUCCESS;
824         }
825
826         if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
827                 rs->sr_err = LDAP_OTHER;
828                 return rs->sr_err;
829         }
830
831         rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
832                 &pwold, &pwnew, &rs->sr_text );
833         if ( rs->sr_err != LDAP_SUCCESS ) {
834                 return rs->sr_err;
835         }
836
837         if ( !BER_BVISNULL( &id ) ) {
838                 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
839                                 &op->o_req_ndn, op->o_tmpmemctx );
840                 if ( rs->sr_err != LDAP_SUCCESS ) {
841                         rs->sr_text = "Invalid DN";
842                         return rs->sr_err;
843                 }
844
845         } else {
846                 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
847                 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
848         }
849
850         roc = rwm_callback_get( op, rs );
851
852         rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
853         if ( rc != LDAP_SUCCESS ) {
854                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
855                 send_ldap_error( op, rs, rc, "extendedDN massage error" );
856                 return -1;
857         }
858
859         ber = ber_alloc_t( LBER_USE_DER );
860         if ( !ber ) {
861                 rs->sr_err = LDAP_OTHER;
862                 rs->sr_text = "No memory";
863                 return rs->sr_err;
864         }
865         ber_printf( ber, "{" );
866         if ( !BER_BVISNULL( &id )) {
867                 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID, 
868                         &op->o_req_dn );
869         }
870         if ( !BER_BVISNULL( &pwold )) {
871                 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &pwold );
872         }
873         if ( !BER_BVISNULL( &pwnew )) {
874                 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &pwnew );
875         }
876         ber_printf( ber, "N}" );
877         ber_flatten( ber, &op->ore_reqdata );
878         ber_free( ber, 1 );
879
880         op->o_callback = &roc->cb;
881
882         return SLAP_CB_CONTINUE;
883 }
884
885 static struct exop {
886         struct berval   oid;
887         BI_op_extended  *extended;
888 } exop_table[] = {
889         { BER_BVC(LDAP_EXOP_MODIFY_PASSWD),     rwm_exop_passwd },
890         { BER_BVNULL, NULL }
891 };
892
893 static int
894 rwm_extended( Operation *op, SlapReply *rs )
895 {
896         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
897         int                     rc;
898         rwm_op_cb *roc;
899
900         int     i;
901
902         for ( i = 0; exop_table[i].extended != NULL; i++ ) {
903                 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
904                 {
905                         rc = exop_table[i].extended( op, rs );
906                         switch ( rc ) {
907                         case LDAP_SUCCESS:
908                                 break;
909
910                         case SLAP_CB_CONTINUE:
911                         case SLAPD_ABANDON:
912                                 return rc;
913
914                         default:
915                                 send_ldap_result( op, rs );
916                                 return rc;
917                         }
918                         break;
919                 }
920         }
921
922         roc = rwm_callback_get( op, rs );
923
924         rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
925         if ( rc != LDAP_SUCCESS ) {
926                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
927                 send_ldap_error( op, rs, rc, "extendedDN massage error" );
928                 return -1;
929         }
930
931         /* TODO: rewrite/map extended data ? ... */
932         op->o_callback = &roc->cb;
933
934         return SLAP_CB_CONTINUE;
935 }
936
937 static int
938 rwm_matched( Operation *op, SlapReply *rs )
939 {
940         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
941         struct ldaprwmap        *rwmap = 
942                         (struct ldaprwmap *)on->on_bi.bi_private;
943
944         struct berval           dn, mdn;
945         dncookie                dc;
946         int                     rc;
947
948         if ( rs->sr_matched == NULL ) {
949                 return SLAP_CB_CONTINUE;
950         }
951
952         dc.rwmap = rwmap;
953         dc.conn = op->o_conn;
954         dc.rs = rs;
955         dc.ctx = "matchedDN";
956         ber_str2bv( rs->sr_matched, 0, 0, &dn );
957         mdn = dn;
958         rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
959         if ( rc != LDAP_SUCCESS ) {
960                 rs->sr_err = rc;
961                 rs->sr_text = "Rewrite error";
962                 return 1;
963         }
964
965         if ( mdn.bv_val != dn.bv_val ) {
966                 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
967                         ch_free( (void *)rs->sr_matched );
968
969                 } else {
970                         rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
971                 }
972                 rs->sr_matched = mdn.bv_val;
973         }
974         
975         return SLAP_CB_CONTINUE;
976 }
977
978 static int
979 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
980 {
981         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
982         struct ldaprwmap        *rwmap = 
983                         (struct ldaprwmap *)on->on_bi.bi_private;
984
985         dncookie                dc;
986         int                     rc;
987         Attribute               **ap;
988         int                     isupdate;
989
990         /*
991          * Rewrite the dn attrs, if needed
992          */
993         dc.rwmap = rwmap;
994         dc.conn = op->o_conn;
995         dc.rs = NULL; 
996
997         /* FIXME: the entries are in the remote mapping form;
998          * so we need to select those attributes we are willing
999          * to return, and remap them accordingly */
1000
1001         /* FIXME: in principle, one could map an attribute
1002          * on top of another, which already exists.
1003          * As such, in the end there might exist more than
1004          * one instance of an attribute.
1005          * We should at least check if this occurs, and issue
1006          * an error (because multiple instances of attrs in 
1007          * response are not valid), or merge the values (what
1008          * about duplicate values?) */
1009         isupdate = be_shadow_update( op );
1010         for ( ap = a_first; *ap; ) {
1011                 struct ldapmapping      *mapping = NULL;
1012                 int                     drop_missing;
1013                 int                     last=-1;
1014                 Attribute               *a;
1015
1016                 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
1017                 {
1018                         /* go on */ ;
1019                         
1020                 } else {
1021                         if ( op->ors_attrs != NULL && 
1022                                         !SLAP_USERATTRS( rs->sr_attr_flags ) &&
1023                                         !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
1024                         {
1025                                 goto cleanup_attr;
1026                         }
1027
1028                         drop_missing = rwm_mapping( &rwmap->rwm_at,
1029                                         &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
1030                         if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
1031                         {
1032                                 goto cleanup_attr;
1033                         }
1034                         if ( mapping != NULL ) {
1035                                 assert( mapping->m_dst_ad != NULL );
1036
1037                                 /* try to normalize mapped Attributes if the original 
1038                                  * AttributeType was not normalized */
1039                                 if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS) && 
1040                                         (!(*ap)->a_desc->ad_type->sat_equality || 
1041                                         !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
1042                                         mapping->m_dst_ad->ad_type->sat_equality &&
1043                                         mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
1044                                 {
1045                                         int i=0;
1046                                         for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
1047                                                 /* just count */ ;
1048
1049                                         if ( last )
1050                                         {
1051                                                 (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
1052
1053                                                 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
1054                                                         int             rc;
1055                                                         /*
1056                                                          * check that each value is valid per syntax
1057                                                          * and pretty if appropriate
1058                                                          */
1059                                                         rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
1060                                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1061                                                                 mapping->m_dst_ad->ad_type->sat_syntax,
1062                                                                 mapping->m_dst_ad->ad_type->sat_equality,
1063                                                                 &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
1064                                                                 NULL );
1065
1066                                                         if ( rc != LDAP_SUCCESS ) {
1067                                                                 BER_BVZERO( &(*ap)->a_nvals[i] );
1068                                                         }
1069                                                 }
1070                                                 BER_BVZERO( &(*ap)->a_nvals[i] );
1071                                         }
1072                                 }
1073                                 /* rewrite the attribute description */
1074                                 (*ap)->a_desc = mapping->m_dst_ad;
1075                         }
1076                 }
1077
1078                 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
1079                         if ( stripEntryDN ) {
1080                                 /* will be generated by frontend */
1081                                 goto cleanup_attr;
1082                         }
1083                         
1084                 } else if ( !isupdate
1085                         && !get_relax( op )
1086                         && (*ap)->a_desc->ad_type->sat_no_user_mod 
1087                         && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
1088                 {
1089                         goto next_attr;
1090                 }
1091
1092                 if ( last == -1 ) { /* not yet counted */ 
1093                         for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
1094                                 /* just count */ ;
1095                 }
1096
1097                 if ( last == 0 ) {
1098                         /* empty? leave it in place because of attrsonly and vlv */
1099                         goto next_attr;
1100                 }
1101                 last--;
1102
1103                 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
1104                                 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
1105                 {
1106                         struct berval   *bv;
1107                         
1108                         for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1109                                 struct berval   mapped;
1110
1111                                 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
1112                                 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
1113                                         ch_free( bv[0].bv_val );
1114                                         BER_BVZERO( &bv[0] );
1115                                         if ( &(*ap)->a_vals[last] > &bv[0] ) {
1116                                                 bv[0] = (*ap)->a_vals[last];
1117                                                 BER_BVZERO( &(*ap)->a_vals[last] );
1118                                         }
1119                                         last--;
1120                                         bv--;
1121
1122                                 } else if ( mapped.bv_val != bv[0].bv_val ) {
1123                                         /*
1124                                          * FIXME: after LBER_FREEing
1125                                          * the value is replaced by
1126                                          * ch_alloc'ed memory
1127                                          */
1128                                         ber_bvreplace( &bv[0], &mapped );
1129                                 }
1130                         }
1131
1132                 /*
1133                  * It is necessary to try to rewrite attributes with
1134                  * dn syntax because they might be used in ACLs as
1135                  * members of groups; since ACLs are applied to the
1136                  * rewritten stuff, no dn-based subject clause could
1137                  * be used at the ldap backend side (see
1138                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
1139                  * The problem can be overcome by moving the dn-based
1140                  * ACLs to the target directory server, and letting
1141                  * everything pass thru the ldap backend. */
1142                 /* FIXME: handle distinguishedName-like syntaxes, like
1143                  * nameAndOptionalUID */
1144                 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
1145                                 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
1146                 {
1147                         dc.ctx = "searchAttrDN";
1148                         rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
1149                         if ( rc != LDAP_SUCCESS ) {
1150                                 goto cleanup_attr;
1151                         }
1152
1153                 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
1154                         dc.ctx = "searchAttrDN";
1155                         rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
1156                         if ( rc != LDAP_SUCCESS ) {
1157                                 goto cleanup_attr;
1158                         }
1159                 }
1160
1161
1162 next_attr:;
1163                 ap = &(*ap)->a_next;
1164                 continue;
1165
1166 cleanup_attr:;
1167                 a = *ap;
1168                 *ap = (*ap)->a_next;
1169
1170                 attr_free( a );
1171         }
1172
1173         return 0;
1174 }
1175
1176 static int
1177 rwm_send_entry( Operation *op, SlapReply *rs )
1178 {
1179         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1180         struct ldaprwmap        *rwmap = 
1181                         (struct ldaprwmap *)on->on_bi.bi_private;
1182
1183         Entry                   *e = NULL;
1184         slap_mask_t             flags;
1185         struct berval           dn = BER_BVNULL,
1186                                 ndn = BER_BVNULL;
1187         dncookie                dc;
1188         int                     rc;
1189
1190         assert( rs->sr_entry != NULL );
1191
1192         /*
1193          * Rewrite the dn of the result, if needed
1194          */
1195         dc.rwmap = rwmap;
1196         dc.conn = op->o_conn;
1197         dc.rs = NULL; 
1198         dc.ctx = "searchEntryDN";
1199
1200         e = rs->sr_entry;
1201         flags = rs->sr_flags;
1202         if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1203                 /* FIXME: all we need to duplicate are:
1204                  * - dn
1205                  * - ndn
1206                  * - attributes that are requested
1207                  * - no values if attrsonly is set
1208                  */
1209
1210                 e = entry_dup( e );
1211                 if ( e == NULL ) {
1212                         rc = LDAP_NO_MEMORY;
1213                         goto fail;
1214                 }
1215
1216                 flags &= ~REP_ENTRY_MUSTRELEASE;
1217                 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1218         }
1219
1220         /*
1221          * Note: this may fail if the target host(s) schema differs
1222          * from the one known to the meta, and a DN with unknown
1223          * attributes is returned.
1224          */
1225         dn = e->e_name;
1226         ndn = e->e_nname;
1227         rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1228         if ( rc != LDAP_SUCCESS ) {
1229                 rc = 1;
1230                 goto fail;
1231         }
1232
1233         if ( e->e_name.bv_val != dn.bv_val ) {
1234                 ch_free( e->e_name.bv_val );
1235                 ch_free( e->e_nname.bv_val );
1236
1237                 e->e_name = dn;
1238                 e->e_nname = ndn;
1239         }
1240
1241         /* TODO: map entry attribute types, objectclasses 
1242          * and dn-valued attribute values */
1243
1244         /* FIXME: the entries are in the remote mapping form;
1245          * so we need to select those attributes we are willing
1246          * to return, and remap them accordingly */
1247         (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1248
1249         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1250                 be_entry_release_rw( op, rs->sr_entry, 0 );
1251         }
1252
1253         rs->sr_entry = e;
1254         rs->sr_flags = flags;
1255
1256         return SLAP_CB_CONTINUE;
1257
1258 fail:;
1259         if ( e != NULL && e != rs->sr_entry ) {
1260                 if ( e->e_name.bv_val == dn.bv_val ) {
1261                         BER_BVZERO( &e->e_name );
1262                 }
1263
1264                 if ( e->e_nname.bv_val == ndn.bv_val ) {
1265                         BER_BVZERO( &e->e_nname );
1266                 }
1267
1268                 entry_free( e );
1269         }
1270
1271         if ( !BER_BVISNULL( &dn ) ) {
1272                 ch_free( dn.bv_val );
1273         }
1274
1275         if ( !BER_BVISNULL( &ndn ) ) {
1276                 ch_free( ndn.bv_val );
1277         }
1278
1279         return rc;
1280 }
1281
1282 static int
1283 rwm_operational( Operation *op, SlapReply *rs )
1284 {
1285         /* FIXME: the entries are in the remote mapping form;
1286          * so we need to select those attributes we are willing
1287          * to return, and remap them accordingly */
1288         if ( rs->sr_operational_attrs ) {
1289                 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1290         }
1291
1292         return SLAP_CB_CONTINUE;
1293 }
1294
1295 #if 0
1296 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1297  * rewritten for subsequent operations; fine for plain suffixmassage,
1298  * but destroys everything else */
1299 static int
1300 rwm_chk_referrals( Operation *op, SlapReply *rs )
1301 {
1302         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1303         int                     rc;
1304
1305         rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1306         if ( rc != LDAP_SUCCESS ) {
1307                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1308                 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1309                 return -1;
1310         }
1311
1312         return SLAP_CB_CONTINUE;
1313 }
1314 #endif
1315
1316 static int
1317 rwm_rw_config(
1318         BackendDB       *be,
1319         const char      *fname,
1320         int             lineno,
1321         int             argc,
1322         char            **argv )
1323 {
1324         slap_overinst           *on = (slap_overinst *) be->bd_info;
1325         struct ldaprwmap        *rwmap = 
1326                         (struct ldaprwmap *)on->on_bi.bi_private;
1327
1328         return rewrite_parse( rwmap->rwm_rw,
1329                                 fname, lineno, argc, argv );
1330
1331         return 0;
1332 }
1333
1334 static int
1335 rwm_suffixmassage_config(
1336         BackendDB       *be,
1337         const char      *fname,
1338         int             lineno,
1339         int             argc,
1340         char            **argv )
1341 {
1342         slap_overinst           *on = (slap_overinst *) be->bd_info;
1343         struct ldaprwmap        *rwmap = 
1344                         (struct ldaprwmap *)on->on_bi.bi_private;
1345
1346         struct berval           bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1347         int                     massaged;
1348         int                     rc;
1349                 
1350         /*
1351          * syntax:
1352          * 
1353          *      suffixmassage [<suffix>] <massaged suffix>
1354          *
1355          * the [<suffix>] field must be defined as a valid suffix
1356          * for the current database;
1357          * the <massaged suffix> shouldn't have already been
1358          * defined as a valid suffix for the current server
1359          */
1360         if ( argc == 2 ) {
1361                 if ( be->be_suffix == NULL ) {
1362                         fprintf( stderr, "%s: line %d: "
1363                                        " \"suffixMassage [<suffix>]"
1364                                        " <massaged suffix>\" without "
1365                                        "<suffix> part requires database "
1366                                        "suffix be defined first.\n",
1367                                 fname, lineno );
1368                         return 1;
1369                 }
1370                 bvnc = be->be_suffix[ 0 ];
1371                 massaged = 1;
1372
1373         } else if ( argc == 3 ) {
1374                 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1375                 massaged = 2;
1376
1377         } else  {
1378                 fprintf( stderr, "%s: line %d: syntax is"
1379                                " \"suffixMassage [<suffix>]"
1380                                " <massaged suffix>\"\n",
1381                         fname, lineno );
1382                 return 1;
1383         }
1384
1385         if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1386                 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1387                         fname, lineno, bvnc.bv_val );
1388                 return 1;
1389         }
1390
1391         ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1392         if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1393                 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1394                                 fname, lineno, brnc.bv_val );
1395                 free( nvnc.bv_val );
1396                 free( pvnc.bv_val );
1397                 return 1;
1398         }
1399
1400         /*
1401          * The suffix massaging is emulated 
1402          * by means of the rewrite capabilities
1403          */
1404         rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1405                         &pvnc, &nvnc, &prnc, &nrnc );
1406         free( nvnc.bv_val );
1407         free( pvnc.bv_val );
1408         free( nrnc.bv_val );
1409         free( prnc.bv_val );
1410
1411         return rc;
1412 }
1413
1414 static int
1415 rwm_m_config(
1416         BackendDB       *be,
1417         const char      *fname,
1418         int             lineno,
1419         int             argc,
1420         char            **argv )
1421 {
1422         slap_overinst           *on = (slap_overinst *) be->bd_info;
1423         struct ldaprwmap        *rwmap = 
1424                         (struct ldaprwmap *)on->on_bi.bi_private;
1425
1426         /* objectclass/attribute mapping */
1427         return rwm_map_config( &rwmap->rwm_oc,
1428                         &rwmap->rwm_at,
1429                         fname, lineno, argc, argv );
1430 }
1431
1432 static int
1433 rwm_response( 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         int             rc;
1440
1441         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1442                 return rwm_send_entry( op, rs );
1443         }
1444
1445         switch( op->o_tag ) {
1446         case LDAP_REQ_SEARCH:
1447         case LDAP_REQ_BIND:
1448         case LDAP_REQ_ADD:
1449         case LDAP_REQ_DELETE:
1450         case LDAP_REQ_MODRDN:
1451         case LDAP_REQ_MODIFY:
1452         case LDAP_REQ_COMPARE:
1453         case LDAP_REQ_EXTENDED:
1454                 if ( rs->sr_ref ) {
1455                         dncookie                dc;
1456
1457                         /*
1458                          * Rewrite the dn of the referrals, if needed
1459                          */
1460                         dc.rwmap = rwmap;
1461                         dc.conn = op->o_conn;
1462                         dc.rs = NULL; 
1463                         dc.ctx = "referralDN";
1464                         rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1465                         if ( rc != LDAP_SUCCESS ) {
1466                                 rc = 1;
1467                                 break;
1468                         }
1469                 }
1470                 rc = rwm_matched( op, rs );
1471                 break;
1472
1473         default:
1474                 rc = SLAP_CB_CONTINUE;
1475                 break;
1476         }
1477
1478         return rc;
1479 }
1480
1481 static int
1482 rwm_db_config(
1483         BackendDB       *be,
1484         const char      *fname,
1485         int             lineno,
1486         int             argc,
1487         char            **argv )
1488 {
1489         slap_overinst           *on = (slap_overinst *) be->bd_info;
1490         struct ldaprwmap        *rwmap = 
1491                         (struct ldaprwmap *)on->on_bi.bi_private;
1492
1493         int             rc = 0;
1494         char            *argv0 = NULL;
1495
1496         if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1497                 argv0 = argv[ 0 ];
1498                 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1499         }
1500
1501         if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1502                 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1503
1504         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1505                 rc = rwm_m_config( be, fname, lineno, argc, argv );
1506
1507         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1508                 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1509
1510         } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1511                 if ( argc != 2 ) {
1512                         fprintf( stderr,
1513                 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1514                                         fname, lineno );
1515                         return( 1 );
1516                 }
1517
1518                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1519                         rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F_MASK2);
1520
1521                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1522                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1523
1524                 /* TODO: not implemented yet */
1525                 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1526                         fprintf( stderr,
1527                 "%s: line %d: \"discover\" not supported yet "
1528                 "in \"t-f-support {no|yes|discover}\".\n",
1529                                         fname, lineno );
1530                         return( 1 );
1531 #if 0
1532                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1533 #endif
1534
1535                 } else {
1536                         fprintf( stderr,
1537         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1538                                 fname, lineno, argv[ 1 ] );
1539                         return 1;
1540                 }
1541         } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) ==  0 ) {
1542                 if ( argc !=2 ) { 
1543                         fprintf( stderr,
1544                 "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
1545                                         fname, lineno );
1546                         return( 1 );
1547                 }
1548
1549                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1550                         rwmap->rwm_flags &= ~(RWM_F_NORMALIZE_MAPPED_ATTRS);
1551
1552                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1553                         rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1554                 }
1555
1556         } else {
1557                 rc = SLAP_CONF_UNKNOWN;
1558         }
1559
1560         if ( argv0 ) {
1561                 argv[ 0 ] = argv0;
1562         }
1563
1564         return rc;
1565 }
1566
1567 /*
1568  * dynamic configuration...
1569  */
1570
1571 enum {
1572         /* rewrite */
1573         RWM_CF_REWRITE = 1,
1574         RWM_CF_SUFFIXMASSAGE,
1575         RWM_CF_T_F_SUPPORT,
1576
1577         /* map */
1578         RWM_CF_MAP,
1579         RWM_CF_NORMALIZE_MAPPED,
1580
1581         RWM_CF_LAST
1582 };
1583
1584 static slap_verbmasks t_f_mode[] = {
1585         { BER_BVC( "yes" ),             RWM_F_SUPPORT_T_F },
1586         { BER_BVC( "discover" ),        RWM_F_SUPPORT_T_F_DISCOVER },
1587         { BER_BVC( "no" ),              RWM_F_NONE },
1588         { BER_BVNULL,                   0 }
1589 };
1590
1591 static ConfigDriver rwm_cf_gen;
1592
1593 static ConfigTable rwmcfg[] = {
1594         { "rwm-rewrite", "rewrite",
1595                 2, 0, STRLENOF("rwm-rewrite"),
1596                 ARG_MAGIC|ARG_QUOTE|RWM_CF_REWRITE, rwm_cf_gen,
1597                 "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
1598                         "DESC 'Rewrites strings' "
1599                         "EQUALITY caseIgnoreMatch "
1600                         "SYNTAX OMsDirectoryString "
1601                         "X-ORDERED 'VALUES' )",
1602                 NULL, NULL },
1603
1604         { "rwm-suffixmassage", "[virtual]> <real",
1605                 2, 3, 0, ARG_MAGIC|RWM_CF_SUFFIXMASSAGE, rwm_cf_gen,
1606                 NULL, NULL, NULL },
1607                 
1608         { "rwm-t-f-support", "true|false|discover",
1609                 2, 2, 0, ARG_MAGIC|RWM_CF_T_F_SUPPORT, rwm_cf_gen,
1610                 "( OLcfgOvAt:16.2 NAME 'olcRwmTFSupport' "
1611                         "DESC 'Absolute filters support' "
1612                         "SYNTAX OMsDirectoryString "
1613                         "SINGLE-VALUE )",
1614                 NULL, NULL },
1615
1616         { "rwm-map", "{objectClass|attribute}",
1617                 2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
1618                 "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
1619                         "DESC 'maps attributes/objectClasses' "
1620                         "SYNTAX OMsDirectoryString "
1621                         "X-ORDERED 'VALUES' )",
1622                 NULL, NULL },
1623
1624         { "rwm-normalize-mapped-attrs", "true|false",
1625                 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_NORMALIZE_MAPPED, rwm_cf_gen,
1626                 "( OLcfgOvAt:16.4 NAME 'olcRwmNormalizeMapped' "
1627                         "DESC 'Normalize mapped attributes/objectClasses' "
1628                         "SYNTAX OMsBoolean "
1629                         "SINGLE-VALUE )",
1630                 NULL, NULL },
1631
1632         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1633 };
1634
1635 static ConfigOCs rwmocs[] = {
1636         { "( OLcfgOvOc:16.1 "
1637                 "NAME 'olcRwmConfig' "
1638                 "DESC 'Rewrite/remap configuration' "
1639                 "SUP olcOverlayConfig "
1640                 "MAY ( "
1641                         "olcRwmRewrite $ "
1642                         "olcRwmTFSupport $ "
1643                         "olcRwmMap $ "
1644                         "olcRwmNormalizeMapped "
1645                         ") )",
1646                 Cft_Overlay, rwmcfg, NULL, NULL },
1647         { NULL, 0, NULL }
1648 };
1649
1650 static void
1651 slap_rewrite_unparse( BerVarray in, BerVarray *out )
1652 {
1653         int             i;
1654         BerVarray       bva = NULL;
1655         char            ibuf[32], *ptr;
1656         struct berval   idx;
1657
1658         assert( in != NULL );
1659
1660         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ )
1661                 /* count'em */ ;
1662
1663         if ( i == 0 ) {
1664                 return;
1665         }
1666
1667         idx.bv_val = ibuf;
1668
1669         bva = ch_malloc( ( i + 1 ) * sizeof(struct berval) );
1670         BER_BVZERO( &bva[ 0 ] );
1671
1672         for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1673                 idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
1674                 if ( idx.bv_len >= sizeof( ibuf ) ) {
1675                         ber_bvarray_free( bva );
1676                         return;
1677                 }
1678
1679                 bva[i].bv_len = idx.bv_len + in[i].bv_len;
1680                 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
1681                 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
1682                 ptr = lutil_strcopy( ptr, in[i].bv_val );
1683                 *ptr = '\0';
1684                 BER_BVZERO( &bva[ i + 1 ] );
1685         }
1686
1687         *out = bva;
1688 }
1689
1690 static int
1691 rwm_cf_gen( ConfigArgs *c )
1692 {
1693         slap_overinst           *on = (slap_overinst *)c->bi;
1694         struct ldaprwmap        *rwmap = 
1695                         (struct ldaprwmap *)on->on_bi.bi_private;
1696
1697         BackendDB               db;
1698         char                    *argv0;
1699         int                     rc = 0;
1700
1701         db = *c->be;
1702         db.bd_info = c->bi;
1703
1704         if ( c->op == SLAP_CONFIG_EMIT ) {
1705                 struct berval   bv = BER_BVNULL;
1706
1707                 switch ( c->type ) {
1708                 case RWM_CF_REWRITE:
1709                         slap_rewrite_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
1710                         if ( !c->rvalue_vals ) rc = 1;
1711                         break;
1712
1713                 case RWM_CF_T_F_SUPPORT:
1714                         enum_to_verb( t_f_mode, (rwmap->rwm_flags & RWM_F_SUPPORT_T_F_MASK2), &bv );
1715                         if ( BER_BVISNULL( &bv ) ) {
1716                                 /* there's something wrong... */
1717                                 assert( 0 );
1718                                 rc = 1;
1719
1720                         } else {
1721                                 value_add_one( &c->rvalue_vals, &bv );
1722                         }
1723                         break;
1724
1725                 case RWM_CF_MAP:
1726                         if ( rwmap->rwm_bva_map == NULL ) {
1727                                 rc = 1;
1728
1729                         } else {
1730                                 value_add( &c->rvalue_vals, rwmap->rwm_bva_map );
1731                         }
1732                         break;
1733
1734                 case RWM_CF_NORMALIZE_MAPPED:
1735                         c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
1736                         break;
1737
1738                 default:
1739                         assert( 0 );
1740                         rc = 1;
1741                 }
1742
1743                 return rc;
1744
1745         } else if ( c->op == LDAP_MOD_DELETE ) {
1746                 switch ( c->type ) {
1747                 case RWM_CF_REWRITE:
1748                         if ( c->valx >= 0 ) {
1749                                 /* single modification is not allowed */
1750                                 rc = 1;
1751
1752                         } else {
1753                                 rewrite_info_delete( &rwmap->rwm_rw );
1754
1755                                 ber_bvarray_free( rwmap->rwm_bva_rewrite );
1756                                 rwmap->rwm_bva_rewrite = NULL;
1757                         }
1758                         break;
1759
1760                 case RWM_CF_T_F_SUPPORT:
1761                         rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
1762                         break;
1763
1764                 case RWM_CF_MAP:
1765                         if ( c->valx >= 0 ) {
1766                                 /* single modification is not allowed */
1767                                 rc = 1;
1768
1769                         } else {
1770                                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
1771                                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1772                                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
1773                                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1774
1775                                 rwmap->rwm_oc.remap = NULL;
1776                                 rwmap->rwm_oc.map = NULL;
1777                                 rwmap->rwm_at.remap = NULL;
1778                                 rwmap->rwm_at.map = NULL;
1779
1780                                 ber_bvarray_free( rwmap->rwm_bva_map );
1781                                 rwmap->rwm_bva_map = NULL;
1782                         }
1783                         break;
1784
1785                 case RWM_CF_NORMALIZE_MAPPED:
1786                         rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
1787                         break;
1788
1789                 default:
1790                         return 1;
1791                 }
1792                 return rc;
1793         }
1794
1795         switch ( c->type ) {
1796         case RWM_CF_REWRITE:
1797                 argv0 = c->argv[ 0 ];
1798                 c->argv[ 0 ] += STRLENOF( "rwm-" );
1799                 rc = rwm_rw_config( &db, c->fname, c->lineno, c->argc, c->argv );
1800                 c->argv[ 0 ] = argv0;
1801                 if ( rc ) {
1802                         return 1;
1803
1804                 } else {
1805                         char            *line;
1806                         struct berval   bv;
1807
1808                         line = ldap_charray2str( c->argv, "\" \"" );
1809                         if ( line != NULL ) {
1810                                 int     len = strlen( c->argv[ 0 ] );
1811
1812                                 ber_str2bv( line, 0, 0, &bv );
1813                                 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
1814                                         bv.bv_len - ( len + 1 ) );
1815                                 bv.bv_val[ bv.bv_len - 1 ] = '"';
1816                                 ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
1817                         }
1818                 }
1819                 break;
1820
1821         case RWM_CF_SUFFIXMASSAGE:
1822                 argv0 = c->argv[ 0 ];
1823                 c->argv[ 0 ] += STRLENOF( "rwm-" );
1824                 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno, c->argc, c->argv );
1825                 c->argv[ 0 ] = argv0;
1826                 if ( rc ) {
1827                         return 1;
1828
1829                 } else {
1830                         char            *line;
1831                         struct berval   bv;
1832
1833                         /* FIXME: not optimal; in fact, this keeps track
1834                          * of the fact that a set of rules was added
1835                          * using the rwm-suffixmassage shortcut, but the
1836                          * rules are not clarified */
1837
1838                         line = ldap_charray2str( c->argv, "\" \"" );
1839                         if ( line != NULL ) {
1840                                 int     len = strlen( c->argv[ 0 ] );
1841
1842                                 ber_str2bv( line, 0, 0, &bv );
1843                                 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
1844                                         bv.bv_len - ( len + 1 ) );
1845                                 bv.bv_val[ bv.bv_len - 1 ] = '"';
1846                                 ber_bvarray_add( &rwmap->rwm_bva_rewrite, &bv );
1847                         }
1848                 }
1849                 break;
1850
1851         case RWM_CF_T_F_SUPPORT:
1852                 rc = verb_to_mask( c->argv[ 1 ], t_f_mode );
1853                 if ( BER_BVISNULL( &t_f_mode[ rc ].word ) ) {
1854                         return 1;
1855                 }
1856
1857                 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
1858                 rwmap->rwm_flags |= t_f_mode[ rc ].mask;
1859                 rc = 0;
1860                 break;
1861
1862         case RWM_CF_MAP:
1863                 argv0 = c->argv[ 0 ];
1864                 c->argv[ 0 ] += STRLENOF( "rwm-" );
1865                 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
1866                 c->argv[ 0 ] = argv0;
1867                 if ( rc ) {
1868                         return 1;
1869
1870                 } else {
1871                         char            *line;
1872                         struct berval   bv;
1873
1874                         line = ldap_charray2str( &c->argv[ 1 ], " " );
1875                         if ( line != NULL ) {
1876                                 ber_str2bv( line, 0, 0, &bv );
1877                                 ber_bvarray_add( &rwmap->rwm_bva_map, &bv );
1878                         }
1879                 }
1880                 break;
1881
1882         case RWM_CF_NORMALIZE_MAPPED:
1883                 if ( c->value_int ) {
1884                         rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1885                 } else {
1886                         rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
1887                 }
1888                 break;
1889
1890         default:
1891                 assert( 0 );
1892                 return 1;
1893         }
1894
1895         return rc;
1896 }
1897
1898
1899
1900
1901
1902
1903
1904 static int
1905 rwm_db_init(
1906         BackendDB       *be,
1907         ConfigReply     *cr )
1908 {
1909         slap_overinst           *on = (slap_overinst *) be->bd_info;
1910         struct ldaprwmap        *rwmap;
1911         char                    *rargv[ 3 ];
1912         int                     rc = 0;
1913
1914         rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
1915
1916         rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1917         if ( rwmap->rwm_rw == NULL ) {
1918                 rc = -1;
1919                 goto error_return;
1920         }
1921
1922         /* this rewriteContext by default must be null;
1923          * rules can be added if required */
1924         rargv[ 0 ] = "rewriteContext";
1925         rargv[ 1 ] = "searchFilter";
1926         rargv[ 2 ] = NULL;
1927         rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1928
1929         rargv[ 0 ] = "rewriteContext";
1930         rargv[ 1 ] = "default";
1931         rargv[ 2 ] = NULL;
1932         rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
1933
1934 error_return:;
1935         on->on_bi.bi_private = (void *)rwmap;
1936
1937         if ( rc ) {
1938                 (void)rwm_db_destroy( be, NULL );
1939         }
1940
1941         return rc;
1942 }
1943
1944 static int
1945 rwm_db_destroy(
1946         BackendDB       *be,
1947         ConfigReply     *cr )
1948 {
1949         slap_overinst   *on = (slap_overinst *) be->bd_info;
1950         int             rc = 0;
1951
1952         if ( on->on_bi.bi_private ) {
1953                 struct ldaprwmap        *rwmap = 
1954                         (struct ldaprwmap *)on->on_bi.bi_private;
1955
1956                 if ( rwmap->rwm_rw ) {
1957                         rewrite_info_delete( &rwmap->rwm_rw );
1958                         ber_bvarray_free( rwmap->rwm_bva_rewrite );
1959                 }
1960
1961                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
1962                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1963                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
1964                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1965                 ber_bvarray_free( rwmap->rwm_bva_map );
1966
1967                 ch_free( rwmap );
1968         }
1969
1970         return rc;
1971 }
1972
1973 static slap_overinst rwm = { { NULL } };
1974
1975 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1976 static
1977 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1978 int
1979 rwm_initialize( void )
1980 {
1981         int             rc;
1982
1983         /* Make sure we don't exceed the bits reserved for userland */
1984         config_check_userland( RWM_CF_LAST );
1985
1986         memset( &rwm, 0, sizeof( slap_overinst ) );
1987
1988         rwm.on_bi.bi_type = "rwm";
1989         rwm.on_bi.bi_flags =
1990                 SLAPO_BFLAG_SINGLE |
1991                 0;
1992
1993         rwm.on_bi.bi_db_init = rwm_db_init;
1994         rwm.on_bi.bi_db_config = rwm_db_config;
1995         rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1996
1997         rwm.on_bi.bi_op_bind = rwm_op_bind;
1998         rwm.on_bi.bi_op_search = rwm_op_search;
1999         rwm.on_bi.bi_op_compare = rwm_op_compare;
2000         rwm.on_bi.bi_op_modify = rwm_op_modify;
2001         rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
2002         rwm.on_bi.bi_op_add = rwm_op_add;
2003         rwm.on_bi.bi_op_delete = rwm_op_delete;
2004         rwm.on_bi.bi_op_unbind = rwm_op_unbind;
2005         rwm.on_bi.bi_extended = rwm_extended;
2006
2007         rwm.on_bi.bi_operational = rwm_operational;
2008         rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
2009
2010         rwm.on_bi.bi_connection_init = rwm_conn_init;
2011         rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
2012
2013         rwm.on_response = rwm_response;
2014
2015         rwm.on_bi.bi_cf_ocs = rwmocs;
2016
2017         rc = config_register_schema( rwmcfg, rwmocs );
2018         if ( rc ) {
2019                 return rc;
2020         }
2021
2022         return overlay_register( &rwm );
2023 }
2024
2025 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2026 int
2027 init_module( int argc, char *argv[] )
2028 {
2029         return rwm_initialize();
2030 }
2031 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2032
2033 #endif /* SLAPD_OVER_RWM */