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