]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwm.c
Happy new year!
[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-2006 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 = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
673                 "searchFilter", op->ors_filterstr.bv_val );
674         if ( rc == LDAP_SUCCESS )
675                 rc = rwm_op_dn_massage( op, rs, "searchDN" );
676 #else /* ! ENABLE_REWRITE */
677         rc = 1;
678         rc = rwm_op_dn_massage( op, rs, &rc );
679 #endif /* ! ENABLE_REWRITE */
680         if ( rc != LDAP_SUCCESS ) {
681                 text = "searchDN massage error";
682                 goto error_return;
683         }
684
685         /*
686          * Rewrite the dn if needed
687          */
688         dc.rwmap = rwmap;
689 #ifdef ENABLE_REWRITE
690         dc.conn = op->o_conn;
691         dc.rs = rs;
692         dc.ctx = "searchFilterAttrDN";
693 #else /* ! ENABLE_REWRITE */
694         dc.tofrom = 0;
695         dc.normalized = 0;
696 #endif /* ! ENABLE_REWRITE */
697
698         rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
699         if ( rc != LDAP_SUCCESS ) {
700                 text = "searchFilter/searchFilterAttrDN massage error";
701                 goto error_return;
702         }
703
704         f = str2filter_x( op, fstr.bv_val );
705
706         if ( f == NULL ) {
707                 text = "massaged filter parse error";
708                 goto error_return;
709         }
710
711         if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
712                 ch_free( op->ors_filterstr.bv_val );
713         }
714
715         if( op->ors_filter ) {
716                 filter_free_x( op, op->ors_filter );
717         }
718
719         op->ors_filter = f;
720         op->ors_filterstr = fstr;
721
722         rc = rwm_map_attrnames( &rwmap->rwm_at, &rwmap->rwm_oc,
723                         op->ors_attrs, &an, RWM_MAP );
724         if ( rc != LDAP_SUCCESS ) {
725                 text = "attribute list mapping error";
726                 goto error_return;
727         }
728
729         cb = rwm_callback_get( op );
730
731         cb->sc_response = rwm_swap_attrs;
732         cb->sc_cleanup = NULL;
733         cb->sc_private = (void *)op->ors_attrs;
734         cb->sc_next = op->o_callback;
735
736         op->o_callback = cb;
737         op->ors_attrs = an;
738
739         return SLAP_CB_CONTINUE;
740
741 error_return:;
742         if ( an != NULL ) {
743                 ch_free( an );
744         }
745
746         if ( f != NULL ) {
747                 filter_free_x( op, f );
748         }
749
750         if ( !BER_BVISNULL( &fstr ) ) {
751                 ch_free( fstr.bv_val );
752         }
753
754         op->o_bd->bd_info = (BackendInfo *)on->on_info;
755         send_ldap_error( op, rs, rc, text );
756
757         return -1;
758
759 }
760
761 static int
762 rwm_extended( Operation *op, SlapReply *rs )
763 {
764         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
765         int                     rc;
766
767 #ifdef ENABLE_REWRITE
768         rc = rwm_op_dn_massage( op, rs, "extendedDN" );
769 #else /* ! ENABLE_REWRITE */
770         rc = 1;
771         rc = rwm_op_dn_massage( op, rs, &rc );
772 #endif /* ! ENABLE_REWRITE */
773         if ( rc != LDAP_SUCCESS ) {
774                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
775                 send_ldap_error( op, rs, rc, "extendedDN massage error" );
776                 return -1;
777         }
778
779         /* TODO: rewrite/map extended data ? ... */
780         return SLAP_CB_CONTINUE;
781 }
782
783 static int
784 rwm_matched( Operation *op, SlapReply *rs )
785 {
786         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
787         struct ldaprwmap        *rwmap = 
788                         (struct ldaprwmap *)on->on_bi.bi_private;
789
790         struct berval           dn, mdn;
791         dncookie                dc;
792         int                     rc;
793
794         if ( rs->sr_matched == NULL ) {
795                 return SLAP_CB_CONTINUE;
796         }
797
798         dc.rwmap = rwmap;
799 #ifdef ENABLE_REWRITE
800         dc.conn = op->o_conn;
801         dc.rs = rs;
802         dc.ctx = "matchedDN";
803 #else /* ! ENABLE_REWRITE */
804         dc.tofrom = 0;
805         dc.normalized = 0;
806 #endif /* ! ENABLE_REWRITE */
807         ber_str2bv( rs->sr_matched, 0, 0, &dn );
808         mdn = dn;
809         rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
810         if ( rc != LDAP_SUCCESS ) {
811                 rs->sr_err = rc;
812                 rs->sr_text = "Rewrite error";
813                 return 1;
814         }
815
816         if ( mdn.bv_val != dn.bv_val ) {
817                 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
818                         ch_free( (void *)rs->sr_matched );
819
820                 } else {
821                         rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
822                 }
823                 rs->sr_matched = mdn.bv_val;
824         }
825         
826         return SLAP_CB_CONTINUE;
827 }
828
829 static int
830 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
831 {
832         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
833         struct ldaprwmap        *rwmap = 
834                         (struct ldaprwmap *)on->on_bi.bi_private;
835
836         dncookie                dc;
837         int                     rc;
838         Attribute               **ap;
839         int                     isupdate;
840
841         /*
842          * Rewrite the dn attrs, if needed
843          */
844         dc.rwmap = rwmap;
845 #ifdef ENABLE_REWRITE
846         dc.conn = op->o_conn;
847         dc.rs = NULL; 
848 #else /* ! ENABLE_REWRITE */
849         dc.tofrom = 0;
850         dc.normalized = 0;
851 #endif /* ! ENABLE_REWRITE */
852
853         /* FIXME: the entries are in the remote mapping form;
854          * so we need to select those attributes we are willing
855          * to return, and remap them accordingly */
856
857         /* FIXME: in principle, one could map an attribute
858          * on top of another, which already exists.
859          * As such, in the end there might exist more than
860          * one instance of an attribute.
861          * We should at least check if this occurs, and issue
862          * an error (because multiple instances of attrs in 
863          * response are not valid), or merge the values (what
864          * about duplicate values?) */
865         isupdate = be_shadow_update( op );
866         for ( ap = a_first; *ap; ) {
867                 struct ldapmapping      *mapping = NULL;
868                 int                     drop_missing;
869                 int                     last;
870                 Attribute               *a;
871
872                 if ( SLAP_OPATTRS( rs->sr_attr_flags ) && is_at_operational( (*ap)->a_desc->ad_type ) )
873                 {
874                         /* go on */ ;
875                         
876                 } else {
877                         if ( op->ors_attrs != NULL && 
878                                         !SLAP_USERATTRS( rs->sr_attr_flags ) &&
879                                         !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
880                         {
881                                 goto cleanup_attr;
882                         }
883
884                         drop_missing = rwm_mapping( &rwmap->rwm_at,
885                                         &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
886                         if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
887                         {
888                                 goto cleanup_attr;
889                         }
890
891                         if ( mapping != NULL ) {
892                                 (*ap)->a_desc = mapping->m_dst_ad;
893                         }
894                 }
895
896                 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
897                         if ( stripEntryDN ) {
898                                 /* will be generated by frontend */
899                                 goto cleanup_attr;
900                         }
901                         
902                 } else if ( !isupdate
903                         && !get_manageDIT( op )
904                         && (*ap)->a_desc->ad_type->sat_no_user_mod 
905                         && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
906                 {
907                         goto next_attr;
908                 }
909
910                 for ( last = 0; !BER_BVISNULL( &(*ap)->a_vals[last] ); last++ )
911                         /* just count */ ;
912
913                 if ( last == 0 ) {
914                         /* empty? leave it in place because of attrsonly and vlv */
915                         goto next_attr;
916                 }
917                 last--;
918
919                 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
920                                 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
921                 {
922                         struct berval   *bv;
923                         
924                         for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
925                                 struct berval   mapped;
926
927                                 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
928                                 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
929                                         ch_free( bv[0].bv_val );
930                                         BER_BVZERO( &bv[0] );
931                                         if ( &(*ap)->a_vals[last] > &bv[0] ) {
932                                                 bv[0] = (*ap)->a_vals[last];
933                                                 BER_BVZERO( &(*ap)->a_vals[last] );
934                                         }
935                                         last--;
936                                         bv--;
937
938                                 } else if ( mapped.bv_val != bv[0].bv_val ) {
939                                         /*
940                                          * FIXME: after LBER_FREEing
941                                          * the value is replaced by
942                                          * ch_alloc'ed memory
943                                          */
944                                         ber_bvreplace( &bv[0], &mapped );
945                                 }
946                         }
947
948                 /*
949                  * It is necessary to try to rewrite attributes with
950                  * dn syntax because they might be used in ACLs as
951                  * members of groups; since ACLs are applied to the
952                  * rewritten stuff, no dn-based subject clause could
953                  * be used at the ldap backend side (see
954                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
955                  * The problem can be overcome by moving the dn-based
956                  * ACLs to the target directory server, and letting
957                  * everything pass thru the ldap backend. */
958                 /* FIXME: handle distinguishedName-like syntaxes, like
959                  * nameAndOptionalUID */
960                 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
961                                 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
962                 {
963 #ifdef ENABLE_REWRITE
964                         dc.ctx = "searchAttrDN";
965 #endif /* ENABLE_REWRITE */
966                         rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals );
967                         if ( rc != LDAP_SUCCESS ) {
968                                 goto cleanup_attr;
969                         }
970
971                 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
972 #ifdef ENABLE_REWRITE
973                         dc.ctx = "searchAttrDN";
974 #endif /* ENABLE_REWRITE */
975                         rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
976                         if ( rc != LDAP_SUCCESS ) {
977                                 goto cleanup_attr;
978                         }
979                 }
980
981                 if ( mapping != NULL ) {
982                         /* rewrite the attribute description */
983                         assert( mapping->m_dst_ad != NULL );
984                         (*ap)->a_desc = mapping->m_dst_ad;
985                 }
986
987 next_attr:;
988                 ap = &(*ap)->a_next;
989                 continue;
990
991 cleanup_attr:;
992                 a = *ap;
993                 *ap = (*ap)->a_next;
994
995                 attr_free( a );
996         }
997
998         return 0;
999 }
1000
1001 static int
1002 rwm_send_entry( Operation *op, SlapReply *rs )
1003 {
1004         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1005         struct ldaprwmap        *rwmap = 
1006                         (struct ldaprwmap *)on->on_bi.bi_private;
1007
1008         Entry                   *e = NULL;
1009         slap_mask_t             flags;
1010         struct berval           dn = BER_BVNULL,
1011                                 ndn = BER_BVNULL;
1012         dncookie                dc;
1013         int                     rc;
1014
1015         assert( rs->sr_entry != NULL );
1016
1017         /*
1018          * Rewrite the dn of the result, if needed
1019          */
1020         dc.rwmap = rwmap;
1021 #ifdef ENABLE_REWRITE
1022         dc.conn = op->o_conn;
1023         dc.rs = NULL; 
1024         dc.ctx = "searchEntryDN";
1025 #else /* ! ENABLE_REWRITE */
1026         dc.tofrom = 0;
1027         dc.normalized = 0;
1028 #endif /* ! ENABLE_REWRITE */
1029
1030         e = rs->sr_entry;
1031         flags = rs->sr_flags;
1032         if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1033                 /* FIXME: all we need to duplicate are:
1034                  * - dn
1035                  * - ndn
1036                  * - attributes that are requested
1037                  * - no values if attrsonly is set
1038                  */
1039
1040                 e = entry_dup( e );
1041                 if ( e == NULL ) {
1042                         rc = LDAP_NO_MEMORY;
1043                         goto fail;
1044                 }
1045
1046                 flags &= ~REP_ENTRY_MUSTRELEASE;
1047                 flags |= ( REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1048         }
1049
1050         /*
1051          * Note: this may fail if the target host(s) schema differs
1052          * from the one known to the meta, and a DN with unknown
1053          * attributes is returned.
1054          */
1055         dn = e->e_name;
1056         ndn = e->e_nname;
1057         rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1058         if ( rc != LDAP_SUCCESS ) {
1059                 rc = 1;
1060                 goto fail;
1061         }
1062
1063         if ( e->e_name.bv_val != dn.bv_val ) {
1064                 ch_free( e->e_name.bv_val );
1065                 ch_free( e->e_nname.bv_val );
1066
1067                 e->e_name = dn;
1068                 e->e_nname = ndn;
1069         }
1070
1071         /* TODO: map entry attribute types, objectclasses 
1072          * and dn-valued attribute values */
1073
1074         /* FIXME: the entries are in the remote mapping form;
1075          * so we need to select those attributes we are willing
1076          * to return, and remap them accordingly */
1077         (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1078
1079         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1080                 be_entry_release_rw( op, rs->sr_entry, 0 );
1081         }
1082
1083         rs->sr_entry = e;
1084         rs->sr_flags = flags;
1085
1086         return SLAP_CB_CONTINUE;
1087
1088 fail:;
1089         if ( e != NULL && e != rs->sr_entry ) {
1090                 if ( e->e_name.bv_val == dn.bv_val ) {
1091                         BER_BVZERO( &e->e_name );
1092                 }
1093
1094                 if ( e->e_nname.bv_val == ndn.bv_val ) {
1095                         BER_BVZERO( &e->e_nname );
1096                 }
1097
1098                 entry_free( e );
1099         }
1100
1101         if ( !BER_BVISNULL( &dn ) ) {
1102                 ch_free( dn.bv_val );
1103         }
1104
1105         if ( !BER_BVISNULL( &ndn ) ) {
1106                 ch_free( ndn.bv_val );
1107         }
1108
1109         return rc;
1110 }
1111
1112 static int
1113 rwm_operational( Operation *op, SlapReply *rs )
1114 {
1115         /* FIXME: the entries are in the remote mapping form;
1116          * so we need to select those attributes we are willing
1117          * to return, and remap them accordingly */
1118         if ( rs->sr_operational_attrs ) {
1119                 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1120         }
1121
1122         return SLAP_CB_CONTINUE;
1123 }
1124
1125 #if 0
1126 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1127  * rewritten for subsequent operations; fine for plain suffixmassage,
1128  * but destroys everything else */
1129 static int
1130 rwm_chk_referrals( Operation *op, SlapReply *rs )
1131 {
1132         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1133         int                     rc;
1134
1135 #ifdef ENABLE_REWRITE
1136         rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1137 #else /* ! ENABLE_REWRITE */
1138         rc = 1;
1139         rc = rwm_op_dn_massage( op, rs, &rc );
1140 #endif /* ! ENABLE_REWRITE */
1141         if ( rc != LDAP_SUCCESS ) {
1142                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1143                 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1144                 return -1;
1145         }
1146
1147         return SLAP_CB_CONTINUE;
1148 }
1149 #endif
1150
1151 static int
1152 rwm_rw_config(
1153     BackendDB   *be,
1154     const char  *fname,
1155     int         lineno,
1156     int         argc,
1157     char        **argv
1158 )
1159 {
1160 #ifdef ENABLE_REWRITE
1161         slap_overinst           *on = (slap_overinst *) be->bd_info;
1162         struct ldaprwmap        *rwmap = 
1163                         (struct ldaprwmap *)on->on_bi.bi_private;
1164
1165         return rewrite_parse( rwmap->rwm_rw,
1166                                 fname, lineno, argc, argv );
1167
1168 #else /* !ENABLE_REWRITE */
1169         fprintf( stderr, "%s: line %d: rewrite capabilities "
1170                         "are not enabled\n", fname, lineno );
1171 #endif /* !ENABLE_REWRITE */
1172                 
1173         return 0;
1174 }
1175
1176 static int
1177 rwm_suffixmassage_config(
1178     BackendDB   *be,
1179     const char  *fname,
1180     int         lineno,
1181     int         argc,
1182     char        **argv
1183 )
1184 {
1185         slap_overinst           *on = (slap_overinst *) be->bd_info;
1186         struct ldaprwmap        *rwmap = 
1187                         (struct ldaprwmap *)on->on_bi.bi_private;
1188
1189         struct berval           bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1190         int                     massaged;
1191 #ifdef ENABLE_REWRITE
1192         int                     rc;
1193 #endif /* ENABLE_REWRITE */
1194                 
1195         /*
1196          * syntax:
1197          * 
1198          *      suffixmassage [<suffix>] <massaged suffix>
1199          *
1200          * the [<suffix>] field must be defined as a valid suffix
1201          * for the current database;
1202          * the <massaged suffix> shouldn't have already been
1203          * defined as a valid suffix for the current server
1204          */
1205         if ( argc == 2 ) {
1206                 if ( be->be_suffix == NULL ) {
1207                         fprintf( stderr, "%s: line %d: "
1208                                        " \"suffixMassage [<suffix>]"
1209                                        " <massaged suffix>\" without "
1210                                        "<suffix> part requires database "
1211                                        "suffix be defined first.\n",
1212                                 fname, lineno );
1213                         return 1;
1214                 }
1215                 bvnc = be->be_suffix[ 0 ];
1216                 massaged = 1;
1217
1218         } else if ( argc == 3 ) {
1219                 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1220                 massaged = 2;
1221
1222         } else  {
1223                 fprintf( stderr, "%s: line %d: syntax is"
1224                                " \"suffixMassage [<suffix>]"
1225                                " <massaged suffix>\"\n",
1226                         fname, lineno );
1227                 return 1;
1228         }
1229
1230         if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1231                 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1232                         fname, lineno, bvnc.bv_val );
1233                 return 1;
1234         }
1235
1236         ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1237         if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1238                 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1239                                 fname, lineno, brnc.bv_val );
1240                 free( nvnc.bv_val );
1241                 free( pvnc.bv_val );
1242                 return 1;
1243         }
1244
1245 #ifdef ENABLE_REWRITE
1246         /*
1247          * The suffix massaging is emulated 
1248          * by means of the rewrite capabilities
1249          */
1250         rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1251                         &pvnc, &nvnc, &prnc, &nrnc );
1252         free( nvnc.bv_val );
1253         free( pvnc.bv_val );
1254         free( nrnc.bv_val );
1255         free( prnc.bv_val );
1256
1257         return( rc );
1258
1259 #else /* !ENABLE_REWRITE */
1260         ber_bvarray_add( &rwmap->rwm_suffix_massage, &pvnc );
1261         ber_bvarray_add( &rwmap->rwm_suffix_massage, &nvnc );
1262                 
1263         ber_bvarray_add( &rwmap->rwm_suffix_massage, &prnc );
1264         ber_bvarray_add( &rwmap->rwm_suffix_massage, &nrnc );
1265 #endif /* !ENABLE_REWRITE */
1266
1267         return 0;
1268 }
1269
1270 static int
1271 rwm_m_config(
1272     BackendDB   *be,
1273     const char  *fname,
1274     int         lineno,
1275     int         argc,
1276     char        **argv
1277 )
1278 {
1279         slap_overinst           *on = (slap_overinst *) be->bd_info;
1280         struct ldaprwmap        *rwmap = 
1281                         (struct ldaprwmap *)on->on_bi.bi_private;
1282
1283         /* objectclass/attribute mapping */
1284         return rwm_map_config( &rwmap->rwm_oc,
1285                         &rwmap->rwm_at,
1286                         fname, lineno, argc, argv );
1287 }
1288
1289 static int
1290 rwm_response( Operation *op, SlapReply *rs )
1291 {
1292         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1293         struct ldaprwmap        *rwmap = 
1294                         (struct ldaprwmap *)on->on_bi.bi_private;
1295
1296         int             rc;
1297
1298         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1299                 return rwm_send_entry( op, rs );
1300         }
1301
1302         switch( op->o_tag ) {
1303         case LDAP_REQ_SEARCH:
1304                 /* Note: the operation attrs are remapped */
1305                 if ( rs->sr_type == REP_RESULT
1306                                 && op->ors_attrs != NULL
1307                                 && op->ors_attrs != rs->sr_attrs )
1308                 {
1309                         ch_free( op->ors_attrs );
1310                         op->ors_attrs = rs->sr_attrs;
1311                 }
1312                 /* fall thru */
1313
1314         case LDAP_REQ_BIND:
1315         case LDAP_REQ_ADD:
1316         case LDAP_REQ_DELETE:
1317         case LDAP_REQ_MODRDN:
1318         case LDAP_REQ_MODIFY:
1319         case LDAP_REQ_COMPARE:
1320         case LDAP_REQ_EXTENDED:
1321                 if ( rs->sr_ref ) {
1322                         dncookie                dc;
1323
1324                         /*
1325                          * Rewrite the dn of the referrals, if needed
1326                          */
1327                         dc.rwmap = rwmap;
1328 #ifdef ENABLE_REWRITE
1329                         dc.conn = op->o_conn;
1330                         dc.rs = NULL; 
1331                         dc.ctx = "referralDN";
1332 #else /* ! ENABLE_REWRITE */
1333                         dc.tofrom = 0;
1334                         dc.normalized = 0;
1335 #endif /* ! ENABLE_REWRITE */
1336                         rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1337                         if ( rc != LDAP_SUCCESS ) {
1338                                 rc = 1;
1339                                 break;
1340                         }
1341                 }
1342                 rc = rwm_matched( op, rs );
1343                 break;
1344
1345         default:
1346                 rc = SLAP_CB_CONTINUE;
1347                 break;
1348         }
1349
1350         return rc;
1351 }
1352
1353 static int
1354 rwm_db_config(
1355     BackendDB   *be,
1356     const char  *fname,
1357     int         lineno,
1358     int         argc,
1359     char        **argv
1360 )
1361 {
1362         slap_overinst           *on = (slap_overinst *) be->bd_info;
1363         struct ldaprwmap        *rwmap = 
1364                         (struct ldaprwmap *)on->on_bi.bi_private;
1365
1366         int             rc = 0;
1367         char            *argv0 = NULL;
1368
1369         if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1370                 argv0 = argv[ 0 ];
1371                 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1372         }
1373
1374         if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1375                 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1376
1377         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1378                 rc = rwm_m_config( be, fname, lineno, argc, argv );
1379
1380         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1381                 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1382
1383         } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1384                 if ( argc != 2 ) {
1385                         fprintf( stderr,
1386                 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1387                                         fname, lineno );
1388                         return( 1 );
1389                 }
1390
1391                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1392                         rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F|RWM_F_SUPPORT_T_F_DISCOVER);
1393
1394                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1395                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1396
1397                 /* TODO: not implemented yet */
1398                 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1399                         fprintf( stderr,
1400                 "%s: line %d: \"discover\" not supported yet "
1401                 "in \"t-f-support {no|yes|discover}\".\n",
1402                                         fname, lineno );
1403                         return( 1 );
1404 #if 0
1405                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1406 #endif
1407
1408                 } else {
1409                         fprintf( stderr,
1410         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1411                                 fname, lineno, argv[ 1 ] );
1412                         return 1;
1413                 }
1414
1415         } else {
1416                 rc = SLAP_CONF_UNKNOWN;
1417         }
1418
1419         if ( argv0 ) {
1420                 argv[ 0 ] = argv0;
1421         }
1422
1423         return rc;
1424 }
1425
1426 static int
1427 rwm_db_init(
1428         BackendDB *be
1429 )
1430 {
1431         slap_overinst           *on = (slap_overinst *) be->bd_info;
1432         struct ldapmapping      *mapping = NULL;
1433         struct ldaprwmap        *rwmap;
1434 #ifdef ENABLE_REWRITE
1435         char                    *rargv[ 3 ];
1436 #endif /* ENABLE_REWRITE */
1437
1438         rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
1439
1440 #ifdef ENABLE_REWRITE
1441         rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1442         if ( rwmap->rwm_rw == NULL ) {
1443                 ch_free( rwmap );
1444                 return -1;
1445         }
1446
1447         /* this rewriteContext by default must be null;
1448          * rules can be added if required */
1449         rargv[ 0 ] = "rewriteContext";
1450         rargv[ 1 ] = "searchFilter";
1451         rargv[ 2 ] = NULL;
1452         rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1453
1454         rargv[ 0 ] = "rewriteContext";
1455         rargv[ 1 ] = "default";
1456         rargv[ 2 ] = NULL;
1457         rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
1458 #endif /* ENABLE_REWRITE */
1459
1460         if ( rwm_map_init( &rwmap->rwm_oc, &mapping ) != LDAP_SUCCESS ||
1461                         rwm_map_init( &rwmap->rwm_at, &mapping ) != LDAP_SUCCESS )
1462         {
1463                 return 1;
1464         }
1465
1466         on->on_bi.bi_private = (void *)rwmap;
1467
1468         return 0;
1469 }
1470
1471 static int
1472 rwm_db_destroy(
1473         BackendDB *be
1474 )
1475 {
1476         slap_overinst   *on = (slap_overinst *) be->bd_info;
1477         int             rc = 0;
1478
1479         if ( on->on_bi.bi_private ) {
1480                 struct ldaprwmap        *rwmap = 
1481                         (struct ldaprwmap *)on->on_bi.bi_private;
1482
1483 #ifdef ENABLE_REWRITE
1484                 if ( rwmap->rwm_rw ) {
1485                         rewrite_info_delete( &rwmap->rwm_rw );
1486                 }
1487 #else /* !ENABLE_REWRITE */
1488                 if ( rwmap->rwm_suffix_massage ) {
1489                         ber_bvarray_free( rwmap->rwm_suffix_massage );
1490                 }
1491 #endif /* !ENABLE_REWRITE */
1492
1493                 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
1494                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1495                 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
1496                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1497
1498                 ch_free( rwmap );
1499         }
1500
1501         return rc;
1502 }
1503
1504 static slap_overinst rwm = { { NULL } };
1505
1506 int
1507 rwm_initialize(void)
1508 {
1509         memset( &rwm, 0, sizeof( slap_overinst ) );
1510
1511         rwm.on_bi.bi_type = "rwm";
1512
1513         rwm.on_bi.bi_db_init = rwm_db_init;
1514         rwm.on_bi.bi_db_config = rwm_db_config;
1515         rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1516
1517         rwm.on_bi.bi_op_bind = rwm_op_bind;
1518         rwm.on_bi.bi_op_search = rwm_op_search;
1519         rwm.on_bi.bi_op_compare = rwm_op_compare;
1520         rwm.on_bi.bi_op_modify = rwm_op_modify;
1521         rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
1522         rwm.on_bi.bi_op_add = rwm_op_add;
1523         rwm.on_bi.bi_op_delete = rwm_op_delete;
1524         rwm.on_bi.bi_op_unbind = rwm_op_unbind;
1525         rwm.on_bi.bi_extended = rwm_extended;
1526
1527         rwm.on_bi.bi_operational = rwm_operational;
1528         rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
1529
1530 #ifdef ENABLE_REWRITE
1531         rwm.on_bi.bi_connection_init = rwm_conn_init;
1532         rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
1533 #endif /* ENABLE_REWRITE */
1534
1535         rwm.on_response = rwm_response;
1536
1537         return overlay_register( &rwm );
1538 }
1539
1540 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1541 int
1542 init_module( int argc, char *argv[] )
1543 {
1544         return rwm_initialize();
1545 }
1546 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1547
1548 #endif /* SLAPD_OVER_RWM */