]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/rwm.c
More schema tweaks
[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 = 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_MODIFIABLE | REP_ENTRY_MUSTBEFREED );
1047         }
1048
1049         /*
1050          * Note: this may fail if the target host(s) schema differs
1051          * from the one known to the meta, and a DN with unknown
1052          * attributes is returned.
1053          */
1054         dn = e->e_name;
1055         ndn = e->e_nname;
1056         rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1057         if ( rc != LDAP_SUCCESS ) {
1058                 rc = 1;
1059                 goto fail;
1060         }
1061
1062         if ( e->e_name.bv_val != dn.bv_val ) {
1063                 ch_free( e->e_name.bv_val );
1064                 ch_free( e->e_nname.bv_val );
1065
1066                 e->e_name = dn;
1067                 e->e_nname = ndn;
1068         }
1069
1070         /* TODO: map entry attribute types, objectclasses 
1071          * and dn-valued attribute values */
1072
1073         /* FIXME: the entries are in the remote mapping form;
1074          * so we need to select those attributes we are willing
1075          * to return, and remap them accordingly */
1076         (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1077
1078 #if 0
1079         if ( rs->sr_operational_attrs ) {
1080                 (void)rwm_attrs( op, rs, &rs->sr_operational_attrs, 0 );
1081         }
1082 #endif
1083
1084         rs->sr_entry = e;
1085         rs->sr_flags = flags;
1086
1087         return SLAP_CB_CONTINUE;
1088
1089 fail:;
1090         if ( e != NULL && e != rs->sr_entry ) {
1091                 if ( e->e_name.bv_val == dn.bv_val ) {
1092                         BER_BVZERO( &e->e_name );
1093                 }
1094
1095                 if ( e->e_nname.bv_val == ndn.bv_val ) {
1096                         BER_BVZERO( &e->e_nname );
1097                 }
1098
1099                 entry_free( e );
1100         }
1101
1102         if ( !BER_BVISNULL( &dn ) ) {
1103                 ch_free( dn.bv_val );
1104         }
1105
1106         if ( !BER_BVISNULL( &ndn ) ) {
1107                 ch_free( ndn.bv_val );
1108         }
1109
1110         return rc;
1111 }
1112
1113 static int
1114 rwm_operational( Operation *op, SlapReply *rs )
1115 {
1116         /* FIXME: the entries are in the remote mapping form;
1117          * so we need to select those attributes we are willing
1118          * to return, and remap them accordingly */
1119         if ( rs->sr_operational_attrs ) {
1120                 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1121         }
1122
1123         return SLAP_CB_CONTINUE;
1124 }
1125
1126 #if 0
1127 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1128  * rewritten for subsequent operations; fine for plain suffixmassage,
1129  * but destroys everything else */
1130 static int
1131 rwm_chk_referrals( Operation *op, SlapReply *rs )
1132 {
1133         slap_overinst           *on = (slap_overinst *) op->o_bd->bd_info;
1134         int                     rc;
1135
1136 #ifdef ENABLE_REWRITE
1137         rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1138 #else /* ! ENABLE_REWRITE */
1139         rc = 1;
1140         rc = rwm_op_dn_massage( op, rs, &rc );
1141 #endif /* ! ENABLE_REWRITE */
1142         if ( rc != LDAP_SUCCESS ) {
1143                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1144                 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1145                 return -1;
1146         }
1147
1148         return SLAP_CB_CONTINUE;
1149 }
1150 #endif
1151
1152 static int
1153 rwm_rw_config(
1154     BackendDB   *be,
1155     const char  *fname,
1156     int         lineno,
1157     int         argc,
1158     char        **argv
1159 )
1160 {
1161 #ifdef ENABLE_REWRITE
1162         slap_overinst           *on = (slap_overinst *) be->bd_info;
1163         struct ldaprwmap        *rwmap = 
1164                         (struct ldaprwmap *)on->on_bi.bi_private;
1165
1166         return rewrite_parse( rwmap->rwm_rw,
1167                                 fname, lineno, argc, argv );
1168
1169 #else /* !ENABLE_REWRITE */
1170         fprintf( stderr, "%s: line %d: rewrite capabilities "
1171                         "are not enabled\n", fname, lineno );
1172 #endif /* !ENABLE_REWRITE */
1173                 
1174         return 0;
1175 }
1176
1177 static int
1178 rwm_suffixmassage_config(
1179     BackendDB   *be,
1180     const char  *fname,
1181     int         lineno,
1182     int         argc,
1183     char        **argv
1184 )
1185 {
1186         slap_overinst           *on = (slap_overinst *) be->bd_info;
1187         struct ldaprwmap        *rwmap = 
1188                         (struct ldaprwmap *)on->on_bi.bi_private;
1189
1190         struct berval           bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1191         int                     massaged;
1192 #ifdef ENABLE_REWRITE
1193         int                     rc;
1194 #endif /* ENABLE_REWRITE */
1195                 
1196         /*
1197          * syntax:
1198          * 
1199          *      suffixmassage [<suffix>] <massaged suffix>
1200          *
1201          * the [<suffix>] field must be defined as a valid suffix
1202          * for the current database;
1203          * the <massaged suffix> shouldn't have already been
1204          * defined as a valid suffix for the current server
1205          */
1206         if ( argc == 2 ) {
1207                 if ( be->be_suffix == NULL ) {
1208                         fprintf( stderr, "%s: line %d: "
1209                                        " \"suffixMassage [<suffix>]"
1210                                        " <massaged suffix>\" without "
1211                                        "<suffix> part requires database "
1212                                        "suffix be defined first.\n",
1213                                 fname, lineno );
1214                         return 1;
1215                 }
1216                 bvnc = be->be_suffix[ 0 ];
1217                 massaged = 1;
1218
1219         } else if ( argc == 3 ) {
1220                 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1221                 massaged = 2;
1222
1223         } else  {
1224                 fprintf( stderr, "%s: line %d: syntax is"
1225                                " \"suffixMassage [<suffix>]"
1226                                " <massaged suffix>\"\n",
1227                         fname, lineno );
1228                 return 1;
1229         }
1230
1231         if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1232                 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1233                         fname, lineno, bvnc.bv_val );
1234                 return 1;
1235         }
1236
1237         ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1238         if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1239                 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
1240                                 fname, lineno, brnc.bv_val );
1241                 free( nvnc.bv_val );
1242                 free( pvnc.bv_val );
1243                 return 1;
1244         }
1245
1246 #ifdef ENABLE_REWRITE
1247         /*
1248          * The suffix massaging is emulated 
1249          * by means of the rewrite capabilities
1250          */
1251         rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1252                         &pvnc, &nvnc, &prnc, &nrnc );
1253         free( nvnc.bv_val );
1254         free( pvnc.bv_val );
1255         free( nrnc.bv_val );
1256         free( prnc.bv_val );
1257
1258         return( rc );
1259
1260 #else /* !ENABLE_REWRITE */
1261         ber_bvarray_add( &rwmap->rwm_suffix_massage, &pvnc );
1262         ber_bvarray_add( &rwmap->rwm_suffix_massage, &nvnc );
1263                 
1264         ber_bvarray_add( &rwmap->rwm_suffix_massage, &prnc );
1265         ber_bvarray_add( &rwmap->rwm_suffix_massage, &nrnc );
1266 #endif /* !ENABLE_REWRITE */
1267
1268         return 0;
1269 }
1270
1271 static int
1272 rwm_m_config(
1273     BackendDB   *be,
1274     const char  *fname,
1275     int         lineno,
1276     int         argc,
1277     char        **argv
1278 )
1279 {
1280         slap_overinst           *on = (slap_overinst *) be->bd_info;
1281         struct ldaprwmap        *rwmap = 
1282                         (struct ldaprwmap *)on->on_bi.bi_private;
1283
1284         /* objectclass/attribute mapping */
1285         return rwm_map_config( &rwmap->rwm_oc,
1286                         &rwmap->rwm_at,
1287                         fname, lineno, argc, argv );
1288 }
1289
1290 static int
1291 rwm_response( Operation *op, SlapReply *rs )
1292 {
1293         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1294         struct ldaprwmap        *rwmap = 
1295                         (struct ldaprwmap *)on->on_bi.bi_private;
1296
1297         int             rc;
1298
1299         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1300                 return rwm_send_entry( op, rs );
1301         }
1302
1303         switch( op->o_tag ) {
1304         case LDAP_REQ_SEARCH:
1305                 /* Note: the operation attrs are remapped */
1306                 if ( rs->sr_type == REP_RESULT
1307                                 && op->ors_attrs != NULL
1308                                 && op->ors_attrs != rs->sr_attrs )
1309                 {
1310                         ch_free( op->ors_attrs );
1311                         op->ors_attrs = rs->sr_attrs;
1312                 }
1313                 /* fall thru */
1314
1315         case LDAP_REQ_BIND:
1316         case LDAP_REQ_ADD:
1317         case LDAP_REQ_DELETE:
1318         case LDAP_REQ_MODRDN:
1319         case LDAP_REQ_MODIFY:
1320         case LDAP_REQ_COMPARE:
1321         case LDAP_REQ_EXTENDED:
1322                 if ( rs->sr_ref ) {
1323                         dncookie                dc;
1324
1325                         /*
1326                          * Rewrite the dn of the referrals, if needed
1327                          */
1328                         dc.rwmap = rwmap;
1329 #ifdef ENABLE_REWRITE
1330                         dc.conn = op->o_conn;
1331                         dc.rs = NULL; 
1332                         dc.ctx = "referralDN";
1333 #else /* ! ENABLE_REWRITE */
1334                         dc.tofrom = 0;
1335                         dc.normalized = 0;
1336 #endif /* ! ENABLE_REWRITE */
1337                         rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1338                         if ( rc != LDAP_SUCCESS ) {
1339                                 rc = 1;
1340                                 break;
1341                         }
1342                 }
1343                 rc = rwm_matched( op, rs );
1344                 break;
1345
1346         default:
1347                 rc = SLAP_CB_CONTINUE;
1348                 break;
1349         }
1350
1351         return rc;
1352 }
1353
1354 static int
1355 rwm_db_config(
1356     BackendDB   *be,
1357     const char  *fname,
1358     int         lineno,
1359     int         argc,
1360     char        **argv
1361 )
1362 {
1363         slap_overinst           *on = (slap_overinst *) be->bd_info;
1364         struct ldaprwmap        *rwmap = 
1365                         (struct ldaprwmap *)on->on_bi.bi_private;
1366
1367         int             rc = 0;
1368         char            *argv0 = NULL;
1369
1370         if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1371                 argv0 = argv[ 0 ];
1372                 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1373         }
1374
1375         if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1376                 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1377
1378         } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1379                 rc = rwm_m_config( be, fname, lineno, argc, argv );
1380
1381         } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1382                 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1383
1384         } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1385                 if ( argc != 2 ) {
1386                         fprintf( stderr,
1387                 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1388                                         fname, lineno );
1389                         return( 1 );
1390                 }
1391
1392                 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1393                         rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F|RWM_F_SUPPORT_T_F_DISCOVER);
1394
1395                 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1396                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1397
1398                 /* TODO: not implemented yet */
1399                 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1400                         fprintf( stderr,
1401                 "%s: line %d: \"discover\" not supported yet "
1402                 "in \"t-f-support {no|yes|discover}\".\n",
1403                                         fname, lineno );
1404                         return( 1 );
1405 #if 0
1406                         rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1407 #endif
1408
1409                 } else {
1410                         fprintf( stderr,
1411         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1412                                 fname, lineno, argv[ 1 ] );
1413                         return 1;
1414                 }
1415
1416         } else {
1417                 rc = SLAP_CONF_UNKNOWN;
1418         }
1419
1420         if ( argv0 ) {
1421                 argv[ 0 ] = argv0;
1422         }
1423
1424         return rc;
1425 }
1426
1427 static int
1428 rwm_db_init(
1429         BackendDB *be
1430 )
1431 {
1432         slap_overinst           *on = (slap_overinst *) be->bd_info;
1433         struct ldapmapping      *mapping = NULL;
1434         struct ldaprwmap        *rwmap;
1435 #ifdef ENABLE_REWRITE
1436         char                    *rargv[ 3 ];
1437 #endif /* ENABLE_REWRITE */
1438
1439         rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
1440
1441 #ifdef ENABLE_REWRITE
1442         rwmap->rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
1443         if ( rwmap->rwm_rw == NULL ) {
1444                 ch_free( rwmap );
1445                 return -1;
1446         }
1447
1448         /* this rewriteContext by default must be null;
1449          * rules can be added if required */
1450         rargv[ 0 ] = "rewriteContext";
1451         rargv[ 1 ] = "searchFilter";
1452         rargv[ 2 ] = NULL;
1453         rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 1, 2, rargv );
1454
1455         rargv[ 0 ] = "rewriteContext";
1456         rargv[ 1 ] = "default";
1457         rargv[ 2 ] = NULL;
1458         rewrite_parse( rwmap->rwm_rw, "<suffix massage>", 2, 2, rargv );
1459 #endif /* ENABLE_REWRITE */
1460
1461         if ( rwm_map_init( &rwmap->rwm_oc, &mapping ) != LDAP_SUCCESS ||
1462                         rwm_map_init( &rwmap->rwm_at, &mapping ) != LDAP_SUCCESS )
1463         {
1464                 return 1;
1465         }
1466
1467         on->on_bi.bi_private = (void *)rwmap;
1468
1469         return 0;
1470 }
1471
1472 static int
1473 rwm_db_destroy(
1474         BackendDB *be
1475 )
1476 {
1477         slap_overinst   *on = (slap_overinst *) be->bd_info;
1478         int             rc = 0;
1479
1480         if ( on->on_bi.bi_private ) {
1481                 struct ldaprwmap        *rwmap = 
1482                         (struct ldaprwmap *)on->on_bi.bi_private;
1483
1484 #ifdef ENABLE_REWRITE
1485                 if (rwmap->rwm_rw) {
1486                         rewrite_info_delete( &rwmap->rwm_rw );
1487                 }
1488 #else /* !ENABLE_REWRITE */
1489                 if ( rwmap->rwm_suffix_massage ) {
1490                         ber_bvarray_free( rwmap->rwm_suffix_massage );
1491                 }
1492 #endif /* !ENABLE_REWRITE */
1493
1494                 avl_free( rwmap->rwm_oc.remap, NULL );
1495                 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
1496                 avl_free( rwmap->rwm_at.remap, NULL );
1497                 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
1498
1499                 ch_free( rwmap );
1500         }
1501
1502         return rc;
1503 }
1504
1505 static slap_overinst rwm = { { NULL } };
1506
1507 int
1508 rwm_init(void)
1509 {
1510         memset( &rwm, 0, sizeof( slap_overinst ) );
1511
1512         rwm.on_bi.bi_type = "rwm";
1513
1514         rwm.on_bi.bi_db_init = rwm_db_init;
1515         rwm.on_bi.bi_db_config = rwm_db_config;
1516         rwm.on_bi.bi_db_destroy = rwm_db_destroy;
1517
1518         rwm.on_bi.bi_op_bind = rwm_op_bind;
1519         rwm.on_bi.bi_op_search = rwm_op_search;
1520         rwm.on_bi.bi_op_compare = rwm_op_compare;
1521         rwm.on_bi.bi_op_modify = rwm_op_modify;
1522         rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
1523         rwm.on_bi.bi_op_add = rwm_op_add;
1524         rwm.on_bi.bi_op_delete = rwm_op_delete;
1525         rwm.on_bi.bi_op_unbind = rwm_op_unbind;
1526         rwm.on_bi.bi_extended = rwm_extended;
1527
1528         rwm.on_bi.bi_operational = rwm_operational;
1529         rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
1530
1531 #ifdef ENABLE_REWRITE
1532         rwm.on_bi.bi_connection_init = rwm_conn_init;
1533         rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
1534 #endif /* ENABLE_REWRITE */
1535
1536         rwm.on_response = rwm_response;
1537
1538         return overlay_register( &rwm );
1539 }
1540
1541 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
1542 int
1543 init_module( int argc, char *argv[] )
1544 {
1545         return rwm_init();
1546 }
1547 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
1548
1549 #endif /* SLAPD_OVER_RWM */