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