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