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