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