]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwm.c
more for the allop overlay
[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-2005 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 "rwm.h"
28
29 static int
30 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie )
31 {
32         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
33         struct ldaprwmap        *rwmap = 
34                         (struct ldaprwmap *)on->on_bi.bi_private;
35
36         struct berval           dn = BER_BVNULL,
37                                 ndn = BER_BVNULL;
38         int                     rc = 0;
39         dncookie                dc;
40
41         /*
42          * Rewrite the dn if needed
43          */
44         dc.rwmap = rwmap;
45 #ifdef ENABLE_REWRITE
46         dc.conn = op->o_conn;
47         dc.rs = rs;
48         dc.ctx = (char *)cookie;
49 #else /* ! ENABLE_REWRITE */
50         dc.tofrom = ((int *)cookie)[0];
51         dc.normalized = 0;
52 #endif /* ! ENABLE_REWRITE */
53
54         /* NOTE: in those cases where only the ndn is available,
55          * and the caller sets op->o_req_dn = op->o_req_ndn,
56          * only rewrite the op->o_req_ndn and use it as 
57          * op->o_req_dn as well */
58         ndn = op->o_req_ndn;
59         if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
60                 dn = op->o_req_dn;
61                 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
62         } else {
63                 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
64         }
65
66         if ( rc != LDAP_SUCCESS ) {
67                 return rc;
68         }
69
70         if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
71                         || ndn.bv_val == op->o_req_ndn.bv_val )
72         {
73                 return LDAP_SUCCESS;
74         }
75
76         if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
77                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
78                 op->o_req_dn = dn;
79         } else {
80                 op->o_req_dn = ndn;
81         }
82         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
83         op->o_req_ndn = ndn;
84
85         return LDAP_SUCCESS;
86 }
87
88 static int
89 rwm_op_add( Operation *op, SlapReply *rs )
90 {
91         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
92         struct ldaprwmap        *rwmap = 
93                         (struct ldaprwmap *)on->on_bi.bi_private;
94
95         int                     rc,
96                                 i;
97         Attribute               **ap = NULL;
98         char                    *olddn = op->o_req_dn.bv_val;
99         int                     isupdate;
100
101 #ifdef ENABLE_REWRITE
102         rc = rwm_op_dn_massage( op, rs, "addDN" );
103 #else /* ! ENABLE_REWRITE */
104         rc = 1;
105         rc = rwm_op_dn_massage( op, rs, &rc );
106 #endif /* ! ENABLE_REWRITE */
107         if ( rc != LDAP_SUCCESS ) {
108                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
109                 send_ldap_error( op, rs, rc, "addDN massage error" );
110                 return -1;
111         }
112
113         if ( olddn != op->o_req_dn.bv_val ) {
114                 ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
115                 ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
116         }
117
118         /* Count number of attributes in entry */ 
119         isupdate = be_shadow_update( op );
120         for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
121                 Attribute       *a;
122
123                 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
124                                 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
125                 {
126                         int             j, last;
127
128                         for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[ last ] ); last++ )
129                                         /* count values */ ;
130                         last--;
131                         for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
132                                 struct ldapmapping      *mapping = NULL;
133
134                                 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
135                                                 &mapping, RWM_MAP );
136                                 if ( mapping == NULL ) {
137                                         if ( rwmap->rwm_at.drop_missing ) {
138                                                 /* FIXME: we allow to remove objectClasses as well;
139                                                  * if the resulting entry is inconsistent, that's
140                                                  * the relayed database's business...
141                                                  */
142                                                 ch_free( (*ap)->a_vals[ j ].bv_val );
143                                                 if ( last > j ) {
144                                                         (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
145                                                 }
146                                                 BER_BVZERO( &(*ap)->a_vals[ last ] );
147                                                 last--;
148                                                 j--;
149                                         }
150
151                                 } else {
152                                         ch_free( (*ap)->a_vals[ j ].bv_val );
153                                         ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
154                                 }
155                         }
156
157                 } else if ( !isupdate && !get_manageDIT( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
158                 {
159                         goto next_attr;
160
161                 } else {
162                         struct ldapmapping      *mapping = NULL;
163
164                         ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
165                                         &mapping, RWM_MAP );
166                         if ( mapping == NULL ) {
167                                 if ( rwmap->rwm_at.drop_missing ) {
168                                         goto cleanup_attr;
169                                 }
170                         }
171
172                         if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
173                                         || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
174                         {
175                                 /*
176                                  * FIXME: rewrite could fail; in this case
177                                  * the operation should give up, right?
178                                  */
179 #ifdef ENABLE_REWRITE
180                                 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
181                                                 (*ap)->a_vals,
182                                                 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
183 #else /* ! ENABLE_REWRITE */
184                                 rc = 1;
185                                 rc = rwm_dnattr_rewrite( op, rs, &rc, (*ap)->a_vals,
186                                                 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
187 #endif /* ! ENABLE_REWRITE */
188                                 if ( rc ) {
189                                         goto cleanup_attr;
190                                 }
191
192                         } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
193 #ifdef ENABLE_REWRITE
194                                 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
195                                                 (*ap)->a_vals,
196                                                 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
197 #else /* ! ENABLE_REWRITE */
198                                 rc = 1;
199                                 rc = rwm_referral_rewrite( op, rs, &rc, (*ap)->a_vals,
200                                                 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
201 #endif /* ! ENABLE_REWRITE */
202                                 if ( rc != LDAP_SUCCESS ) {
203                                         goto cleanup_attr;
204                                 }
205                         }
206                 
207                         if ( mapping != NULL ) {
208                                 assert( mapping->m_dst_ad != NULL );
209                                 (*ap)->a_desc = mapping->m_dst_ad;
210                         }
211                 }
212
213 next_attr:;
214                 ap = &(*ap)->a_next;
215                 continue;
216
217 cleanup_attr:;
218                 /* FIXME: leaking attribute/values? */
219                 a = *ap;
220
221                 *ap = (*ap)->a_next;
222                 attr_free( a );
223         }
224
225         /* TODO: map attribute types, values of DN-valued attributes ... */
226         return SLAP_CB_CONTINUE;
227 }
228
229 #ifdef ENABLE_REWRITE
230 static int
231 rwm_conn_init( BackendDB *be, Connection *conn )
232 {
233         slap_overinst           *on = (slap_overinst *) be->bd_info;
234         struct ldaprwmap        *rwmap = 
235                         (struct ldaprwmap *)on->on_bi.bi_private;
236
237         ( void )rewrite_session_init( rwmap->rwm_rw, conn );
238
239         return SLAP_CB_CONTINUE;
240 }
241
242 static int
243 rwm_conn_destroy( BackendDB *be, Connection *conn )
244 {
245         slap_overinst           *on = (slap_overinst *) be->bd_info;
246         struct ldaprwmap        *rwmap = 
247                         (struct ldaprwmap *)on->on_bi.bi_private;
248
249         ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
250
251         return SLAP_CB_CONTINUE;
252 }
253 #endif /* ENABLE_REWRITE */
254
255 static int
256 rwm_op_bind( Operation *op, SlapReply *rs )
257 {
258         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
259         int                     rc;
260
261 #ifdef ENABLE_REWRITE
262         rc = rwm_op_dn_massage( op, rs, "bindDN" );
263 #else /* ! ENABLE_REWRITE */
264         rc = 1;
265         rc = rwm_op_dn_massage( op, rs, &rc );
266 #endif /* ! ENABLE_REWRITE */
267         if ( rc != LDAP_SUCCESS ) {
268                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
269                 send_ldap_error( op, rs, rc, "bindDN massage error" );
270                 return -1;
271         }
272
273         return SLAP_CB_CONTINUE;
274 }
275
276 static int
277 rwm_op_unbind( Operation *op, SlapReply *rs )
278 {
279         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
280         struct ldaprwmap        *rwmap = 
281                         (struct ldaprwmap *)on->on_bi.bi_private;
282
283 #ifdef ENABLE_REWRITE
284         rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
285 #endif /* ENABLE_REWRITE */
286
287         return SLAP_CB_CONTINUE;
288 }
289
290 static int
291 rwm_op_compare( Operation *op, SlapReply *rs )
292 {
293         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
294         struct ldaprwmap        *rwmap = 
295                         (struct ldaprwmap *)on->on_bi.bi_private;
296
297         int                     rc;
298         struct berval           mapped_at = BER_BVNULL,
299                                 mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
300
301 #ifdef ENABLE_REWRITE
302         rc = rwm_op_dn_massage( op, rs, "compareDN" );
303 #else /* ! ENABLE_REWRITE */
304         rc = 1;
305         rc = rwm_op_dn_massage( op, rs, &rc );
306 #endif /* ! ENABLE_REWRITE */
307         if ( rc != LDAP_SUCCESS ) {
308                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
309                 send_ldap_error( op, rs, rc, "compareDN massage error" );
310                 return -1;
311         }
312
313         /* if the attribute is an objectClass, try to remap its value */
314         if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
315                         || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
316         {
317                 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
318                                 &mapped_vals[0], RWM_MAP );
319                 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
320                 {
321                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
322                         send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
323                         return -1;
324
325                 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
326                         ber_bvreplace_x( &op->orc_ava->aa_value, &mapped_vals[0], op->o_tmpmemctx );
327                 }
328                 mapped_at = op->orc_ava->aa_desc->ad_cname;
329
330         } else {
331                 struct ldapmapping      *mapping = NULL;
332                 AttributeDescription    *ad = op->orc_ava->aa_desc;
333
334                 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
335                                 &mapping, RWM_MAP );
336                 if ( mapping == NULL ) {
337                         if ( rwmap->rwm_at.drop_missing ) {
338                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
339                                 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
340                                 return -1;
341                         }
342
343                 } else {
344                         assert( mapping->m_dst_ad != NULL );
345                         ad = mapping->m_dst_ad;
346                 }
347
348                 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
349                                 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
350                 {
351                         struct berval   *mapped_valsp[2];
352                         
353                         mapped_valsp[0] = &mapped_vals[0];
354                         mapped_valsp[1] = &mapped_vals[1];
355
356                         mapped_vals[0] = op->orc_ava->aa_value;
357
358 #ifdef ENABLE_REWRITE
359                         rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
360 #else /* ! ENABLE_REWRITE */
361                         rc = 1;
362                         rc = rwm_dnattr_rewrite( op, rs, &rc, NULL, mapped_valsp );
363 #endif /* ! ENABLE_REWRITE */
364
365                         if ( rc != LDAP_SUCCESS ) {
366                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
367                                 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
368                                 return -1;
369                         }
370
371                         if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
372                                 /* NOTE: if we get here, rwm_dnattr_rewrite()
373                                  * already freed the old value, so now 
374                                  * it's invalid */
375                                 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
376                                                 op->o_tmpmemctx );
377                                 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
378                         }
379                 }
380                 op->orc_ava->aa_desc = ad;
381         }
382
383         return SLAP_CB_CONTINUE;
384 }
385
386 static int
387 rwm_op_delete( Operation *op, SlapReply *rs )
388 {
389         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
390         int                     rc;
391
392 #ifdef ENABLE_REWRITE
393         rc = rwm_op_dn_massage( op, rs, "deleteDN" );
394 #else /* ! ENABLE_REWRITE */
395         rc = 1;
396         rc = rwm_op_dn_massage( op, rs, &rc );
397 #endif /* ! ENABLE_REWRITE */
398         if ( rc != LDAP_SUCCESS ) {
399                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
400                 send_ldap_error( op, rs, rc, "deleteDN massage error" );
401                 return -1;
402         }
403
404         return SLAP_CB_CONTINUE;
405 }
406
407 static int
408 rwm_op_modify( Operation *op, SlapReply *rs )
409 {
410         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
411         struct ldaprwmap        *rwmap = 
412                         (struct ldaprwmap *)on->on_bi.bi_private;
413
414         int                     isupdate;
415         Modifications           **mlp;
416         int                     rc;
417
418 #ifdef ENABLE_REWRITE
419         rc = rwm_op_dn_massage( op, rs, "modifyDN" );
420 #else /* ! ENABLE_REWRITE */
421         rc = 1;
422         rc = rwm_op_dn_massage( op, rs, &rc );
423 #endif /* ! ENABLE_REWRITE */
424         if ( rc != LDAP_SUCCESS ) {
425                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
426                 send_ldap_error( op, rs, rc, "modifyDN massage error" );
427                 return -1;
428         }
429
430         isupdate = be_shadow_update( op );
431         for ( mlp = &op->oq_modify.rs_modlist; *mlp; ) {
432                 int                     is_oc = 0;
433                 Modifications           *ml;
434                 struct ldapmapping      *mapping = NULL;
435
436                 if ( (*mlp)->sml_desc == slap_schema.si_ad_objectClass 
437                                 || (*mlp)->sml_desc == slap_schema.si_ad_structuralObjectClass )
438                 {
439                         is_oc = 1;
440
441                 } else if ( !isupdate && !get_manageDIT( op ) && (*mlp)->sml_desc->ad_type->sat_no_user_mod  )
442                 {
443                         goto next_mod;
444
445                 } else {
446                         int                     drop_missing;
447
448                         drop_missing = rwm_mapping( &rwmap->rwm_at,
449                                         &(*mlp)->sml_desc->ad_cname,
450                                         &mapping, RWM_MAP );
451                         if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
452                         {
453                                 goto cleanup_mod;
454                         }
455                 }
456
457                 if ( (*mlp)->sml_values != NULL ) {
458                         if ( is_oc ) {
459                                 int     last, j;
460
461                                 for ( last = 0; !BER_BVISNULL( &(*mlp)->sml_values[ last ] ); last++ )
462                                         /* count values */ ;
463                                 last--;
464
465                                 for ( j = 0; !BER_BVISNULL( &(*mlp)->sml_values[ j ] ); j++ ) {
466                                         struct ldapmapping      *oc_mapping = NULL;
467                 
468                                         ( void )rwm_mapping( &rwmap->rwm_oc, &(*mlp)->sml_values[ j ],
469                                                         &oc_mapping, RWM_MAP );
470                                         if ( oc_mapping == NULL ) {
471                                                 if ( rwmap->rwm_at.drop_missing ) {
472                                                         /* FIXME: we allow to remove objectClasses as well;
473                                                          * if the resulting entry is inconsistent, that's
474                                                          * the relayed database's business...
475                                                          */
476                                                         ch_free( (*mlp)->sml_values[ j ].bv_val );
477                                                         if ( last > j ) {
478                                                                 (*mlp)->sml_values[ j ] = (*mlp)->sml_values[ last ];
479                                                         }
480                                                         BER_BVZERO( &(*mlp)->sml_values[ last ] );
481                                                         last--;
482                                                         j--;
483                                                 }
484         
485                                         } else {
486                                                 ch_free( (*mlp)->sml_values[ j ].bv_val );
487                                                 ber_dupbv( &(*mlp)->sml_values[ j ], &oc_mapping->m_dst );
488                                         }
489                                 }
490
491                         } else {
492                                 if ( (*mlp)->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
493                                                 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
494                                 {
495 #ifdef ENABLE_REWRITE
496                                         rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
497                                                         (*mlp)->sml_values,
498                                                         (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
499 #else /* ! ENABLE_REWRITE */
500                                         rc = 1;
501                                         rc = rwm_dnattr_rewrite( op, rs, &rc, 
502                                                         (*mlp)->sml_values,
503                                                         (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
504 #endif /* ! ENABLE_REWRITE */
505
506                                 } else if ( (*mlp)->sml_desc == slap_schema.si_ad_ref ) {
507 #ifdef ENABLE_REWRITE
508                                         rc = rwm_referral_rewrite( op, rs,
509                                                         "referralAttrDN",
510                                                         (*mlp)->sml_values,
511                                                         (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
512 #else /* ! ENABLE_REWRITE */
513                                         rc = 1;
514                                         rc = rwm_referral_rewrite( op, rs, &rc,
515                                                         (*mlp)->sml_values,
516                                                         (*mlp)->sml_nvalues ? &(*mlp)->sml_nvalues : NULL );
517 #endif /* ! ENABLE_REWRITE */
518                                         if ( rc != LDAP_SUCCESS ) {
519                                                 goto cleanup_mod;
520                                         }
521                                 }
522
523                                 if ( rc != LDAP_SUCCESS ) {
524                                         goto cleanup_mod;
525                                 }
526                         }
527                 }
528
529 next_mod:;
530                 if ( mapping != NULL ) {
531                         /* use new attribute description */
532                         assert( mapping->m_dst_ad != NULL );
533                         (*mlp)->sml_desc = mapping->m_dst_ad;
534                 }
535
536                 mlp = &(*mlp)->sml_next;
537                 continue;
538
539 cleanup_mod:;
540                 ml = *mlp;
541                 *mlp = (*mlp)->sml_next;
542                 slap_mod_free( &ml->sml_mod, 0 );
543                 free( ml );
544         }
545
546         return SLAP_CB_CONTINUE;
547 }
548
549 static int
550 rwm_op_modrdn( Operation *op, SlapReply *rs )
551 {
552         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
553         struct ldaprwmap        *rwmap = 
554                         (struct ldaprwmap *)on->on_bi.bi_private;
555         
556         int                     rc;
557
558         if ( op->orr_newSup ) {
559                 dncookie        dc;
560                 struct berval   nnewSup = BER_BVNULL;
561                 struct berval   newSup = BER_BVNULL;
562
563                 /*
564                  * Rewrite the new superior, if defined and required
565                  */
566                 dc.rwmap = rwmap;
567 #ifdef ENABLE_REWRITE
568                 dc.conn = op->o_conn;
569                 dc.rs = rs;
570                 dc.ctx = "newSuperiorDN";
571 #else /* ! ENABLE_REWRITE */
572                 dc.tofrom = 0;
573                 dc.normalized = 0;
574 #endif /* ! ENABLE_REWRITE */
575                 newSup = *op->orr_newSup;
576                 nnewSup = *op->orr_nnewSup;
577                 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
578                 if ( rc != LDAP_SUCCESS ) {
579                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
580                         send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
581                         return -1;
582                 }
583
584                 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
585                         op->o_tmpfree( op->orr_newSup->bv_val, op->o_tmpmemctx );
586                         op->o_tmpfree( op->orr_nnewSup->bv_val, op->o_tmpmemctx );
587                         *op->orr_newSup = newSup;
588                         *op->orr_nnewSup = nnewSup;
589                 }
590         }
591
592         /*
593          * Rewrite the dn, if needed
594          */
595 #ifdef ENABLE_REWRITE
596         rc = rwm_op_dn_massage( op, rs, "renameDN" );
597 #else /* ! ENABLE_REWRITE */
598         rc = 1;
599         rc = rwm_op_dn_massage( op, rs, &rc );
600 #endif /* ! ENABLE_REWRITE */
601         if ( rc != LDAP_SUCCESS ) {
602                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
603                 send_ldap_error( op, rs, rc, "renameDN massage error" );
604                 return -1;
605         }
606
607         /* TODO: rewrite newRDN, attribute types, 
608          * values of DN-valued attributes ... */
609         return SLAP_CB_CONTINUE;
610 }
611
612 static slap_callback    rwm_cb;
613
614 static void
615 rwm_keyfree(
616         void            *key,
617         void            *data )
618 {
619         ber_memfree_x( data, NULL );
620 }
621
622 static slap_callback *
623 rwm_callback_get( Operation *op )
624 {
625         void            *data = NULL;
626
627         if ( op->o_threadctx == NULL ) {
628                 return &rwm_cb;
629         }
630
631         ldap_pvt_thread_pool_getkey( op->o_threadctx,
632                         rwm_keyfree, &data, NULL );
633         if ( data == NULL ) {
634                 data = ch_calloc( sizeof( slap_callback ), 1 );
635                 ldap_pvt_thread_pool_setkey( op->o_threadctx,
636                                 rwm_keyfree, data, rwm_keyfree );
637         }
638
639         return (slap_callback *)data;
640 }
641
642 static int
643 rwm_swap_attrs( Operation *op, SlapReply *rs )
644 {
645         slap_callback   *cb = op->o_callback;
646         AttributeName   *an = (AttributeName *)cb->sc_private;
647
648         rs->sr_attrs = an;
649         
650         return SLAP_CB_CONTINUE;
651 }
652
653 static int
654 rwm_op_search( Operation *op, SlapReply *rs )
655 {
656         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
657         struct ldaprwmap        *rwmap = 
658                         (struct ldaprwmap *)on->on_bi.bi_private;
659
660         int                     rc;
661         dncookie                dc;
662
663         struct berval           fstr = BER_BVNULL;
664         Filter                  *f = NULL;
665
666         slap_callback           *cb = NULL;
667         AttributeName           *an = NULL;
668
669         char                    *text = NULL;
670
671 #ifdef ENABLE_REWRITE
672         rc = rwm_op_dn_massage( op, rs, "searchDN" );
673 #else /* ! ENABLE_REWRITE */
674         rc = 1;
675         rc = rwm_op_dn_massage( op, rs, &rc );
676 #endif /* ! ENABLE_REWRITE */
677         if ( rc != LDAP_SUCCESS ) {
678                 text = "searchDN massage error";
679                 goto error_return;
680         }
681
682         /*
683          * Rewrite the dn if needed
684          */
685         dc.rwmap = rwmap;
686 #ifdef ENABLE_REWRITE
687         dc.conn = op->o_conn;
688         dc.rs = rs;
689         dc.ctx = "searchFilterAttrDN";
690 #else /* ! ENABLE_REWRITE */
691         dc.tofrom = 0;
692         dc.normalized = 0;
693 #endif /* ! ENABLE_REWRITE */
694
695         rc = rwm_filter_map_rewrite( &dc, op->ors_filter, &fstr );
696         if ( rc != LDAP_SUCCESS ) {
697                 text = "searchFilter/searchFilterAttrDN massage error";
698                 goto error_return;
699         }
700
701         f = str2filter_x( op, fstr.bv_val );
702
703         if ( f == NULL ) {
704                 text = "massaged filter parse error";
705                 goto error_return;
706         }
707
708         if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
709                 ch_free( op->ors_filterstr.bv_val );
710         }
711
712         if( op->ors_filter ) {
713                 filter_free_x( op, op->ors_filter );
714         }
715
716         op->ors_filter = f;
717         op->ors_filterstr = fstr;
718
719         rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
720                         op->ors_attrs, &an, RWM_MAP );
721         if ( rc != LDAP_SUCCESS ) {
722                 text = "attribute list mapping error";
723                 goto error_return;
724         }
725
726         cb = rwm_callback_get( op );
727
728         cb->sc_response = rwm_swap_attrs;
729         cb->sc_cleanup = NULL;
730         cb->sc_private = (void *)op->ors_attrs;
731         cb->sc_next = op->o_callback;
732
733         op->o_callback = cb;
734         op->ors_attrs = an;
735
736         return SLAP_CB_CONTINUE;
737
738 error_return:;
739         if ( an != NULL ) {
740                 ch_free( an );
741         }
742
743         if ( f != NULL ) {
744                 filter_free_x( op, f );
745         }
746
747         if ( !BER_BVISNULL( &fstr ) ) {
748                 ch_free( fstr.bv_val );
749         }
750
751         op->o_bd->bd_info = (BackendInfo *)on->on_info;
752         send_ldap_error( op, rs, rc, text );
753
754         return -1;
755
756 }
757
758 static int
759 rwm_extended( Operation *op, SlapReply *rs )
760 {
761         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
762         int                     rc;
763
764 #ifdef ENABLE_REWRITE
765         rc = rwm_op_dn_massage( op, rs, "extendedDN" );
766 #else /* ! ENABLE_REWRITE */
767         rc = 1;
768         rc = rwm_op_dn_massage( op, rs, &rc );
769 #endif /* ! ENABLE_REWRITE */
770         if ( rc != LDAP_SUCCESS ) {
771                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
772                 send_ldap_error( op, rs, rc, "extendedDN massage error" );
773                 return -1;
774         }
775
776         /* TODO: rewrite/map extended data ? ... */
777         return SLAP_CB_CONTINUE;
778 }
779
780 static int
781 rwm_matched( Operation *op, SlapReply *rs )
782 {
783         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
784         struct ldaprwmap        *rwmap = 
785                         (struct ldaprwmap *)on->on_bi.bi_private;
786
787         struct berval           dn, mdn;
788         dncookie                dc;
789         int                     rc;
790
791         if ( rs->sr_matched == NULL ) {
792                 return SLAP_CB_CONTINUE;
793         }
794
795         dc.rwmap = rwmap;
796 #ifdef ENABLE_REWRITE
797         dc.conn = op->o_conn;
798         dc.rs = rs;
799         dc.ctx = "matchedDN";
800 #else /* ! ENABLE_REWRITE */
801         dc.tofrom = 0;
802         dc.normalized = 0;
803 #endif /* ! ENABLE_REWRITE */
804         ber_str2bv( rs->sr_matched, 0, 0, &dn );
805         mdn = dn;
806         rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
807         if ( rc != LDAP_SUCCESS ) {
808                 rs->sr_err = rc;
809                 rs->sr_text = "Rewrite error";
810                 return 1;
811         }
812
813         if ( mdn.bv_val != dn.bv_val ) {
814                 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
815                         ch_free( (void *)rs->sr_matched );
816
817                 } else {
818                         rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
819                 }
820                 rs->sr_matched = mdn.bv_val;
821         }
822         
823         return SLAP_CB_CONTINUE;
824 }
825
826 static int
827 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
828 {
829         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
830         struct ldaprwmap        *rwmap = 
831                         (struct ldaprwmap *)on->on_bi.bi_private;
832
833         dncookie                dc;
834         int                     rc;
835         Attribute               **ap;
836         int                     isupdate;
837
838         /*
839          * Rewrite the dn attrs, if needed
840          */
841         dc.rwmap = rwmap;
842 #ifdef ENABLE_REWRITE
843         dc.conn = op->o_conn;
844         dc.rs = NULL; 
845 #else /* ! ENABLE_REWRITE */
846         dc.tofrom = 0;
847         dc.normalized = 0;
848 #endif /* ! ENABLE_REWRITE */
849
850         /* FIXME: the entries are in the remote mapping form;
851          * so we need to select those attributes we are willing
852          * to return, and remap them accordingly */
853
854         /* FIXME: in principle, one could map an attribute
855          * on top of another, which already exists.
856          * As such, in the end there might exist more than
857          * one instance of an attribute.
858          * We should at least check if this occurs, and issue
859          * an error (because multiple instances of attrs in 
860          * response are not valid), or merge the values (what
861          * about duplicate values?) */
862         isupdate = be_shadow_update( op );
863         for ( ap = a_first; *ap; ) {
864                 struct ldapmapping      *mapping = NULL;
865                 int                     drop_missing;
866                 int                     last;
867                 Attribute               *a;
868
869                 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
870                 {
871                         /* go on */ ;
872                         
873                 } else {
874                         if ( op->ors_attrs != NULL && 
875                                         !SLAP_USERATTRS( rs->sr_attr_flags ) &&
876                                         !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
877                         {
878                                 goto cleanup_attr;
879                         }
880
881                         drop_missing = rwm_mapping( &rwmap->rwm_at,
882                                         &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
883                         if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
884                         {
885                                 goto cleanup_attr;
886                         }
887
888                         if ( mapping != NULL ) {
889                                 (*ap)->a_desc = mapping->m_dst_ad;
890                         }
891                 }
892
893                 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
894                         if ( stripEntryDN ) {
895                                 /* will be generated by frontend */
896                                 goto cleanup_attr;
897                         }
898                         
899                 } else if ( !isupdate
900                         && !get_manageDIT( op )
901                         && (*ap)->a_desc->ad_type->sat_no_user_mod 
902                         && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
903                 {
904                         goto next_attr;
905                 }
906
907                 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
908                         /* just count */ ;
909
910                 if ( last == 0 ) {
911                         /* empty? leave it in place because of attrsonly and vlv */
912                         goto next_attr;
913                 }
914                 last--;
915
916                 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
917                                 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
918                 {
919                         struct berval   *bv;
920                         
921                         for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
922                                 struct berval   mapped;
923
924                                 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
925                                 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
926                                         ch_free( bv[0].bv_val );
927                                         BER_BVZERO( &bv[0] );
928                                         if ( &(*ap)->a_vals[last] > &bv[0] ) {
929                                                 bv[0] = (*ap)->a_vals[last];
930                                                 BER_BVZERO( &(*ap)->a_vals[last] );
931                                         }
932                                         last--;
933                                         bv--;
934
935                                 } else if ( mapped.bv_val != bv[0].bv_val ) {
936                                         /*
937                                          * FIXME: after LBER_FREEing
938                                          * the value is replaced by
939                                          * ch_alloc'ed memory
940                                          */
941                                         ber_bvreplace( &bv[0], &mapped );
942                                 }
943                         }
944
945                 /*
946                  * It is necessary to try to rewrite attributes with
947                  * dn syntax because they might be used in ACLs as
948                  * members of groups; since ACLs are applied to the
949                  * rewritten stuff, no dn-based subject clause could
950                  * be used at the ldap backend side (see
951                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
952                  * The problem can be overcome by moving the dn-based
953                  * ACLs to the target directory server, and letting
954                  * everything pass thru the ldap backend. */
955                 /* FIXME: handle distinguishedName-like syntaxes, like
956                  * nameAndOptionalUID */
957                 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
958                                 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
959                 {
960 #ifdef ENABLE_REWRITE
961                         dc.ctx = "searchAttrDN";
962 #endif /* ENABLE_REWRITE */
963                         rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
964                         if ( rc != LDAP_SUCCESS ) {
965                                 goto cleanup_attr;
966                         }
967
968                 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
969 #ifdef ENABLE_REWRITE
970                         dc.ctx = "searchAttrDN";
971 #endif /* ENABLE_REWRITE */
972                         rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
973                         if ( rc != LDAP_SUCCESS ) {
974                                 goto cleanup_attr;
975                         }
976                 }
977
978                 if ( mapping != NULL ) {
979                         /* rewrite the attribute description */
980                         assert( mapping->m_dst_ad != NULL );
981                         (*ap)->a_desc = mapping->m_dst_ad;
982                 }
983
984 next_attr:;
985                 ap = &(*ap)->a_next;
986                 continue;
987
988 cleanup_attr:;
989                 a = *ap;
990                 *ap = (*ap)->a_next;
991
992                 attr_free( a );
993         }
994
995         return 0;
996 }
997
998 static int
999 rwm_send_entry( Operation *op, SlapReply *rs )
1000 {
1001         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1002         struct ldaprwmap        *rwmap = 
1003                         (struct ldaprwmap *)on->on_bi.bi_private;
1004
1005         Entry                   *e = NULL;
1006         slap_mask_t             flags;
1007         struct berval           dn = BER_BVNULL,
1008                                 ndn = BER_BVNULL;
1009         dncookie                dc;
1010         int                     rc;
1011
1012         assert( rs->sr_entry != NULL );
1013
1014         /*
1015          * Rewrite the dn of the result, if needed
1016          */
1017         dc.rwmap = rwmap;
1018 #ifdef ENABLE_REWRITE
1019         dc.conn = op->o_conn;
1020         dc.rs = NULL; 
1021         dc.ctx = "searchEntryDN";
1022 #else /* ! ENABLE_REWRITE */
1023         dc.tofrom = 0;
1024         dc.normalized = 0;
1025 #endif /* ! ENABLE_REWRITE */
1026
1027         e = rs->sr_entry;
1028         flags = rs->sr_flags;
1029         if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1030                 /* FIXME: all we need to duplicate are:
1031                  * - dn
1032                  * - ndn
1033                  * - attributes that are requested
1034                  * - no values if attrsonly is set
1035                  */
1036
1037                 e = entry_dup( e );
1038                 if ( e == NULL ) {
1039                         rc = LDAP_NO_MEMORY;
1040                         goto fail;
1041                 }
1042
1043                 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1044         }
1045
1046         /*
1047          * Note: this may fail if the target host(s) schema differs
1048          * from the one known to the meta, and a DN with unknown
1049          * attributes is returned.
1050          */
1051         dn = e->e_name;
1052         ndn = e->e_nname;
1053         rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1054         if ( rc != LDAP_SUCCESS ) {
1055                 rc = 1;
1056                 goto fail;
1057         }
1058
1059         if ( e->e_name.bv_val != dn.bv_val ) {
1060                 ch_free( e->e_name.bv_val );
1061                 ch_free( e->e_nname.bv_val );
1062
1063                 e->e_name = dn;
1064                 e->e_nname = ndn;
1065         }
1066
1067         /* TODO: map entry attribute types, objectclasses 
1068          * and dn-valued attribute values */
1069
1070         /* FIXME: the entries are in the remote mapping form;
1071          * so we need to select those attributes we are willing
1072          * to return, and remap them accordingly */
1073         (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1074
1075 #if 0
1076         if ( rs->sr_operational_attrs ) {
1077                 (void)rwm_attrs( op, rs, &rs->sr_operational_attrs, 0 );
1078         }
1079 #endif
1080
1081         rs->sr_entry = e;
1082         rs->sr_flags = flags;
1083
1084         return SLAP_CB_CONTINUE;
1085
1086 fail:;
1087         if ( e != NULL && e != rs->sr_entry ) {
1088                 if ( e->e_name.bv_val == dn.bv_val ) {
1089                         BER_BVZERO( &e->e_name );
1090                 }
1091
1092                 if ( e->e_nname.bv_val == ndn.bv_val ) {
1093                         BER_BVZERO( &e->e_nname );
1094                 }
1095
1096                 entry_free( e );
1097         }
1098
1099         if ( !BER_BVISNULL( &dn ) ) {
1100                 ch_free( dn.bv_val );
1101         }
1102
1103         if ( !BER_BVISNULL( &ndn ) ) {
1104                 ch_free( ndn.bv_val );
1105         }
1106
1107         return rc;
1108 }
1109
1110 static int
1111 rwm_operational( Operation *op, SlapReply *rs )
1112 {
1113         /* FIXME: the entries are in the remote mapping form;
1114          * so we need to select those attributes we are willing
1115          * to return, and remap them accordingly */
1116         if ( rs->sr_operational_attrs ) {
1117                 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1118         }
1119
1120         return SLAP_CB_CONTINUE;
1121 }
1122
1123 #if 0
1124 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1125  * rewritten for subsequent operations; fine for plain suffixmassage,
1126  * but destroys everything else */
1127 static int
1128 rwm_chk_referrals( Operation *op, SlapReply *rs )
1129 {
1130         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1131         int                     rc;
1132
1133 #ifdef ENABLE_REWRITE
1134         rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1135 #else /* ! ENABLE_REWRITE */
1136         rc = 1;
1137         rc = rwm_op_dn_massage( op, rs, &rc );
1138 #endif /* ! ENABLE_REWRITE */
1139         if ( rc != LDAP_SUCCESS ) {
1140                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1141                 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1142                 return -1;
1143         }
1144
1145         return SLAP_CB_CONTINUE;
1146 }
1147 #endif
1148
1149 static int
1150 rwm_rw_config(
1151     BackendDB   *be,
1152     const char  *fname,
1153     int         lineno,
1154     int         argc,
1155     char        **argv
1156 )
1157 {
1158 #ifdef ENABLE_REWRITE
1159         slap_overinst           *on = (slap_overinst *) be->bd_info;
1160         struct ldaprwmap        *rwmap = 
1161                         (struct ldaprwmap *)on->on_bi.bi_private;
1162
1163         return rewrite_parse( rwmap->rwm_rw,
1164                                 fname, lineno, argc, argv );
1165
1166 #else /* !ENABLE_REWRITE */
1167         fprintf( stderr, "%s: line %d: rewrite capabilities "
1168                         "are not enabled\n", fname, lineno );
1169 #endif /* !ENABLE_REWRITE */
1170                 
1171         return 0;
1172 }
1173
1174 static int
1175 rwm_suffixmassage_config(
1176     BackendDB   *be,
1177     const char  *fname,
1178     int         lineno,
1179     int         argc,
1180     char        **argv
1181 )
1182 {
1183         slap_overinst           *on = (slap_overinst *) be->bd_info;
1184         struct ldaprwmap        *rwmap = 
1185                         (struct ldaprwmap *)on->on_bi.bi_private;
1186
1187         struct berval           bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1188         int                     massaged;
1189 #ifdef ENABLE_REWRITE
1190         int                     rc;
1191 #endif /* ENABLE_REWRITE */
1192                 
1193         /*
1194          * syntax:
1195          * 
1196          *      suffixmassage [<suffix>] <massaged suffix>
1197          *
1198          * the [<suffix>] field must be defined as a valid suffix
1199          * for the current database;
1200          * the <massaged suffix> shouldn't have already been
1201          * defined as a valid suffix for the current server
1202          */
1203         if ( argc == 2 ) {
1204                 if ( be->be_suffix == NULL ) {
1205                         fprintf( stderr, "%s: line %d: "
1206                                        " \"suffixMassage [<suffix>]"
1207                                        " <massaged suffix>\" without "
1208                                        "<suffix> part requires database "
1209                                        "suffix be defined first.\n",
1210                                 fname, lineno );
1211                         return 1;
1212                 }
1213                 bvnc = be->be_suffix[ 0 ];
1214                 massaged = 1;
1215
1216         } else if ( argc == 3 ) {
1217                 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1218                 massaged = 2;
1219
1220         } else  {
1221                 fprintf( stderr, "%s: line %d: syntax is"
1222                                " \"suffixMassage [<suffix>]"
1223                                " <massaged suffix>\"\n",
1224                         fname, lineno );
1225                 return 1;
1226         }
1227
1228         if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1229                 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1230                         fname, lineno, bvnc.bv_val );
1231                 return 1;
1232         }
1233
1234         ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1235         if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1236                 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1237                                 fname, lineno, brnc.bv_val );
1238                 free( nvnc.bv_val );
1239                 free( pvnc.bv_val );
1240                 return 1;
1241         }
1242
1243 #ifdef ENABLE_REWRITE
1244         /*
1245          * The suffix massaging is emulated 
1246          * by means of the rewrite capabilities
1247          */
1248         rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1249                         &pvnc, &nvnc, &prnc, &nrnc );
1250         free( nvnc.bv_val );
1251         free( pvnc.bv_val );
1252         free( nrnc.bv_val );
1253         free( prnc.bv_val );
1254
1255         return( rc );
1256
1257 #else /* !ENABLE_REWRITE */
1258         ber_bvarray_add( &rwmap->rwm_suffix_massage, &pvnc );
1259         ber_bvarray_add( &rwmap->rwm_suffix_massage, &nvnc );
1260                 
1261         ber_bvarray_add( &rwmap->rwm_suffix_massage, &prnc );
1262         ber_bvarray_add( &rwmap->rwm_suffix_massage, &nrnc );
1263 #endif /* !ENABLE_REWRITE */
1264
1265         return 0;
1266 }
1267
1268 static int
1269 rwm_m_config(
1270     BackendDB   *be,
1271     const char  *fname,
1272     int         lineno,
1273     int         argc,
1274     char        **argv
1275 )
1276 {
1277         slap_overinst           *on = (slap_overinst *) be->bd_info;
1278         struct ldaprwmap        *rwmap = 
1279                         (struct ldaprwmap *)on->on_bi.bi_private;
1280
1281         /* objectclass/attribute mapping */
1282         return rwm_map_config( &rwmap->rwm_oc,
1283                         &rwmap->rwm_at,
1284                         fname, lineno, argc, argv );
1285 }
1286
1287 static int
1288 rwm_response( Operation *op, SlapReply *rs )
1289 {
1290         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1291         struct ldaprwmap        *rwmap = 
1292                         (struct ldaprwmap *)on->on_bi.bi_private;
1293
1294         int             rc;
1295
1296         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1297                 return rwm_send_entry( op, rs );
1298         }
1299
1300         switch( op->o_tag ) {
1301         case LDAP_REQ_SEARCH:
1302                 /* Note: the operation attrs are remapped */
1303                 if ( rs->sr_type == REP_RESULT
1304                                 && op->ors_attrs != NULL
1305                                 && op->ors_attrs != rs->sr_attrs )
1306                 {
1307                         ch_free( op->ors_attrs );
1308                         op->ors_attrs = rs->sr_attrs;
1309                 }
1310                 /* fall thru */
1311
1312         case LDAP_REQ_BIND:
1313         case LDAP_REQ_ADD:
1314         case LDAP_REQ_DELETE:
1315         case LDAP_REQ_MODRDN:
1316         case LDAP_REQ_MODIFY:
1317         case LDAP_REQ_COMPARE:
1318         case LDAP_REQ_EXTENDED:
1319                 if ( rs->sr_ref ) {
1320                         dncookie                dc;
1321
1322                         /*
1323                          * Rewrite the dn of the referrals, if needed
1324                          */
1325                         dc.rwmap = rwmap;
1326 #ifdef ENABLE_REWRITE
1327                         dc.conn = op->o_conn;
1328                         dc.rs = NULL; 
1329                         dc.ctx = "referralDN";
1330 #else /* ! ENABLE_REWRITE */
1331                         dc.tofrom = 0;
1332                         dc.normalized = 0;
1333 #endif /* ! ENABLE_REWRITE */
1334                         rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1335                         if ( rc != LDAP_SUCCESS ) {
1336                                 rc = 1;
1337                                 break;
1338                         }
1339                 }
1340                 rc = rwm_matched( op, rs );
1341                 break;
1342
1343         default:
1344                 rc = SLAP_CB_CONTINUE;
1345                 break;
1346         }
1347
1348         return rc;
1349 }
1350
1351 static int
1352 rwm_db_config(
1353     BackendDB   *be,
1354     const char  *fname,
1355     int         lineno,
1356     int         argc,
1357     char        **argv
1358 )
1359 {
1360         slap_overinst           *on = (slap_overinst *) be->bd_info;
1361         struct ldaprwmap        *rwmap = 
1362                         (struct ldaprwmap *)on->on_bi.bi_private;
1363
1364         int             rc = 0;
1365         char            *argv0 = NULL;
1366
1367         if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1368                 argv0 = argv[ 0 ];
1369                 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1370         }
1371
1372         if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1373                 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1374
1375         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1376                 rc = rwm_m_config( be, fname, lineno, argc, argv );
1377
1378         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1379                 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1380
1381         } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1382                 if ( argc != 2 ) {
1383                         fprintf( stderr,
1384                 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1385                                         fname, lineno );
1386                         return( 1 );
1387                 }
1388
1389                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1390                         rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F|RWM_F_SUPPORT_T_F_DISCOVER);
1391
1392                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1393                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1394
1395 #if 0
1396                 /* TODO: not implemented yet */
1397                 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1398                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1399 #endif
1400
1401                 } else {
1402                         fprintf( stderr,
1403         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1404                                 fname, lineno, argv[ 1 ] );
1405                         return 1;
1406                 }
1407
1408         } else {
1409                 rc = SLAP_CONF_UNKNOWN;
1410         }
1411
1412         if ( argv0 ) {
1413                 argv[ 0 ] = argv0;
1414         }
1415
1416         return rc;
1417 }
1418
1419 static int
1420 rwm_db_init(
1421         BackendDB *be
1422 )
1423 {
1424         slap_overinst           *on = (slap_overinst *) be->bd_info;
1425         struct ldapmapping      *mapping = NULL;
1426         struct ldaprwmap        *rwmap;
1427 #ifdef ENABLE_REWRITE
1428         char                    *rargv[ 3 ];
1429 #endif /* ENABLE_REWRITE */
1430
1431         rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
1432
1433 #ifdef ENABLE_REWRITE
1434         rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1435         if ( rwmap->rwm_rw == NULL ) {
1436                 ch_free( rwmap );
1437                 return -1;
1438         }
1439
1440         /* this rewriteContext by default must be null;
1441          * rules can be added if required */
1442         rargv[ 0 ] = "rewriteContext";
1443         rargv[ 1 ] = "searchFilter";
1444         rargv[ 2 ] = NULL;
1445         rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1446
1447         rargv[ 0 ] = "rewriteContext";
1448         rargv[ 1 ] = "default";
1449         rargv[ 2 ] = NULL;
1450         rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
1451 #endif /* ENABLE_REWRITE */
1452
1453         if ( rwm_map_init( &rwmap->rwm_oc, &mapping ) != LDAP_SUCCESS ||
1454                         rwm_map_init( &rwmap->rwm_at, &mapping ) != LDAP_SUCCESS )
1455         {
1456                 return 1;
1457         }
1458
1459         on->on_bi.bi_private = (void *)rwmap;
1460
1461         return 0;
1462 }
1463
1464 static int
1465 rwm_db_destroy(
1466         BackendDB *be
1467 )
1468 {
1469         slap_overinst   *on = (slap_overinst *) be->bd_info;
1470         int             rc = 0;
1471
1472         if ( on->on_bi.bi_private ) {
1473                 struct ldaprwmap        *rwmap = 
1474                         (struct ldaprwmap *)on->on_bi.bi_private;
1475
1476 #ifdef ENABLE_REWRITE
1477                 if (rwmap->rwm_rw) {
1478                         rewrite_info_delete( &rwmap->rwm_rw );
1479                 }
1480 #else /* !ENABLE_REWRITE */
1481                 if ( rwmap->rwm_suffix_massage ) {
1482                         ber_bvarray_free( rwmap->rwm_suffix_massage );
1483                 }
1484 #endif /* !ENABLE_REWRITE */
1485
1486                 avl_free( rwmap->rwm_oc.remap, NULL );
1487                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1488                 avl_free( rwmap->rwm_at.remap, NULL );
1489                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1490
1491                 ch_free( rwmap );
1492         }
1493
1494         return rc;
1495 }
1496
1497 static slap_overinst rwm = { { NULL } };
1498
1499 int
1500 rwm_init(void)
1501 {
1502         memset( &rwm, 0, sizeof( slap_overinst ) );
1503
1504         rwm.on_bi.bi_type = "rwm";
1505
1506         rwm.on_bi.bi_db_init = rwm_db_init;
1507         rwm.on_bi.bi_db_config = rwm_db_config;
1508         rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1509
1510         rwm.on_bi.bi_op_bind = rwm_op_bind;
1511         rwm.on_bi.bi_op_search = rwm_op_search;
1512         rwm.on_bi.bi_op_compare = rwm_op_compare;
1513         rwm.on_bi.bi_op_modify = rwm_op_modify;
1514         rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
1515         rwm.on_bi.bi_op_add = rwm_op_add;
1516         rwm.on_bi.bi_op_delete = rwm_op_delete;
1517         rwm.on_bi.bi_op_unbind = rwm_op_unbind;
1518         rwm.on_bi.bi_extended = rwm_extended;
1519
1520         rwm.on_bi.bi_operational = rwm_operational;
1521         rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
1522
1523 #ifdef ENABLE_REWRITE
1524         rwm.on_bi.bi_connection_init = rwm_conn_init;
1525         rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
1526 #endif /* ENABLE_REWRITE */
1527
1528         rwm.on_response = rwm_response;
1529
1530         return overlay_register( &rwm );
1531 }
1532
1533 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1534 int
1535 init_module( int argc, char *argv[] )
1536 {
1537         return rwm_init();
1538 }
1539 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1540
1541 #endif /* SLAPD_OVER_RWM */