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