rwm.c revision 1.1.1.4.10.1 1 /* $NetBSD: rwm.c,v 1.1.1.4.10.1 2017/04/21 16:52:31 bouyer Exp $ */
2
3 /* rwm.c - rewrite/remap operations */
4 /* $OpenLDAP$ */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2003-2016 The OpenLDAP Foundation.
8 * Portions Copyright 2003 Pierangelo Masarati.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
13 * Public License.
14 *
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
18 */
19
20 #include <sys/cdefs.h>
21 __RCSID("$NetBSD: rwm.c,v 1.1.1.4.10.1 2017/04/21 16:52:31 bouyer Exp $");
22
23 #include "portable.h"
24
25 #ifdef SLAPD_OVER_RWM
26
27 #include <stdio.h>
28
29 #include <ac/string.h>
30
31 #include "slap.h"
32 #include "config.h"
33 #include "lutil.h"
34 #include "rwm.h"
35
36 typedef struct rwm_op_state {
37 ber_tag_t r_tag;
38 struct berval ro_dn;
39 struct berval ro_ndn;
40 struct berval r_dn;
41 struct berval r_ndn;
42 struct berval rx_dn;
43 struct berval rx_ndn;
44 AttributeName *mapped_attrs;
45 OpRequest o_request;
46 } rwm_op_state;
47
48 typedef struct rwm_op_cb {
49 slap_callback cb;
50 rwm_op_state ros;
51 } rwm_op_cb;
52
53 static int
54 rwm_db_destroy( BackendDB *be, ConfigReply *cr );
55
56 static int
57 rwm_send_entry( Operation *op, SlapReply *rs );
58
59 static void
60 rwm_op_rollback( Operation *op, SlapReply *rs, rwm_op_state *ros )
61 {
62 /* in case of successful extended operation cleanup
63 * gets called *after* (ITS#6632); this hack counts
64 * on others to cleanup our o_req_dn/o_req_ndn,
65 * while we cleanup theirs. */
66 if ( ros->r_tag == LDAP_REQ_EXTENDED && rs->sr_err == LDAP_SUCCESS ) {
67 if ( !BER_BVISNULL( &ros->rx_dn ) ) {
68 ch_free( ros->rx_dn.bv_val );
69 }
70 if ( !BER_BVISNULL( &ros->rx_ndn ) ) {
71 ch_free( ros->rx_ndn.bv_val );
72 }
73
74 } else {
75 if ( !BER_BVISNULL( &ros->ro_dn ) ) {
76 op->o_req_dn = ros->ro_dn;
77 }
78 if ( !BER_BVISNULL( &ros->ro_ndn ) ) {
79 op->o_req_ndn = ros->ro_ndn;
80 }
81
82 if ( !BER_BVISNULL( &ros->r_dn )
83 && ros->r_dn.bv_val != ros->ro_dn.bv_val )
84 {
85 assert( ros->r_dn.bv_val != ros->r_ndn.bv_val );
86 ch_free( ros->r_dn.bv_val );
87 }
88
89 if ( !BER_BVISNULL( &ros->r_ndn )
90 && ros->r_ndn.bv_val != ros->ro_ndn.bv_val )
91 {
92 ch_free( ros->r_ndn.bv_val );
93 }
94 }
95
96 BER_BVZERO( &ros->r_dn );
97 BER_BVZERO( &ros->r_ndn );
98 BER_BVZERO( &ros->ro_dn );
99 BER_BVZERO( &ros->ro_ndn );
100 BER_BVZERO( &ros->rx_dn );
101 BER_BVZERO( &ros->rx_ndn );
102
103 switch( ros->r_tag ) {
104 case LDAP_REQ_COMPARE:
105 if ( op->orc_ava->aa_value.bv_val != ros->orc_ava->aa_value.bv_val )
106 op->o_tmpfree( op->orc_ava->aa_value.bv_val, op->o_tmpmemctx );
107 op->orc_ava = ros->orc_ava;
108 break;
109 case LDAP_REQ_MODIFY:
110 slap_mods_free( op->orm_modlist, 1 );
111 op->orm_modlist = ros->orm_modlist;
112 break;
113 case LDAP_REQ_MODRDN:
114 if ( op->orr_newSup != ros->orr_newSup ) {
115 if ( op->orr_newSup ) {
116 ch_free( op->orr_newSup->bv_val );
117 ch_free( op->orr_nnewSup->bv_val );
118 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
119 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
120 }
121 op->orr_newSup = ros->orr_newSup;
122 op->orr_nnewSup = ros->orr_nnewSup;
123 }
124 if ( op->orr_newrdn.bv_val != ros->orr_newrdn.bv_val ) {
125 ch_free( op->orr_newrdn.bv_val );
126 ch_free( op->orr_nnewrdn.bv_val );
127 op->orr_newrdn = ros->orr_newrdn;
128 op->orr_nnewrdn = ros->orr_nnewrdn;
129 }
130 break;
131 case LDAP_REQ_SEARCH:
132 op->o_tmpfree( ros->mapped_attrs, op->o_tmpmemctx );
133 filter_free_x( op, op->ors_filter, 1 );
134 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
135 op->ors_attrs = ros->ors_attrs;
136 op->ors_filter = ros->ors_filter;
137 op->ors_filterstr = ros->ors_filterstr;
138 break;
139 case LDAP_REQ_EXTENDED:
140 if ( op->ore_reqdata != ros->ore_reqdata ) {
141 ber_bvfree( op->ore_reqdata );
142 op->ore_reqdata = ros->ore_reqdata;
143 }
144 break;
145 case LDAP_REQ_BIND:
146 if ( rs->sr_err == LDAP_SUCCESS ) {
147 #if 0
148 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
149 /* too late, c_mutex released */
150 Debug( LDAP_DEBUG_ANY, "*** DN: \"%s\" => \"%s\"\n",
151 op->o_conn->c_ndn.bv_val,
152 op->o_req_ndn.bv_val );
153 ber_bvreplace( &op->o_conn->c_ndn,
154 &op->o_req_ndn );
155 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
156 #endif
157 }
158 break;
159 default: break;
160 }
161 }
162
163 static int
164 rwm_op_cleanup( Operation *op, SlapReply *rs )
165 {
166 slap_callback *cb = op->o_callback;
167 rwm_op_state *ros = cb->sc_private;
168
169 if ( rs->sr_type == REP_RESULT || rs->sr_type == REP_EXTENDED ||
170 op->o_abandon || rs->sr_err == SLAPD_ABANDON )
171 {
172 rwm_op_rollback( op, rs, ros );
173
174 op->o_callback = op->o_callback->sc_next;
175 op->o_tmpfree( cb, op->o_tmpmemctx );
176 }
177
178 return SLAP_CB_CONTINUE;
179 }
180
181 static rwm_op_cb *
182 rwm_callback_get( Operation *op )
183 {
184 rwm_op_cb *roc;
185
186 roc = op->o_tmpcalloc( 1, sizeof( struct rwm_op_cb ), op->o_tmpmemctx );
187 roc->cb.sc_cleanup = rwm_op_cleanup;
188 roc->cb.sc_response = NULL;
189 roc->cb.sc_next = op->o_callback;
190 roc->cb.sc_private = &roc->ros;
191 roc->ros.r_tag = op->o_tag;
192 roc->ros.ro_dn = op->o_req_dn;
193 roc->ros.ro_ndn = op->o_req_ndn;
194 BER_BVZERO( &roc->ros.r_dn );
195 BER_BVZERO( &roc->ros.r_ndn );
196 BER_BVZERO( &roc->ros.rx_dn );
197 BER_BVZERO( &roc->ros.rx_ndn );
198 roc->ros.mapped_attrs = NULL;
199 roc->ros.o_request = op->o_request;
200
201 return roc;
202 }
203
204
205 static int
206 rwm_op_dn_massage( Operation *op, SlapReply *rs, void *cookie,
207 rwm_op_state *ros )
208 {
209 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
210 struct ldaprwmap *rwmap =
211 (struct ldaprwmap *)on->on_bi.bi_private;
212
213 struct berval dn = BER_BVNULL,
214 ndn = BER_BVNULL;
215 int rc = 0;
216 dncookie dc;
217
218 /*
219 * Rewrite the dn if needed
220 */
221 dc.rwmap = rwmap;
222 dc.conn = op->o_conn;
223 dc.rs = rs;
224 dc.ctx = (char *)cookie;
225
226 /* NOTE: in those cases where only the ndn is available,
227 * and the caller sets op->o_req_dn = op->o_req_ndn,
228 * only rewrite the op->o_req_ndn and use it as
229 * op->o_req_dn as well */
230 ndn = op->o_req_ndn;
231 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
232 dn = op->o_req_dn;
233 rc = rwm_dn_massage_pretty_normalize( &dc, &op->o_req_dn, &dn, &ndn );
234 } else {
235 rc = rwm_dn_massage_normalize( &dc, &op->o_req_ndn, &ndn );
236 }
237
238 if ( rc != LDAP_SUCCESS ) {
239 return rc;
240 }
241
242 if ( ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val && dn.bv_val == op->o_req_dn.bv_val )
243 || ndn.bv_val == op->o_req_ndn.bv_val )
244 {
245 return LDAP_SUCCESS;
246 }
247
248 if ( op->o_req_dn.bv_val != op->o_req_ndn.bv_val ) {
249 op->o_req_dn = dn;
250 assert( BER_BVISNULL( &ros->r_dn ) );
251 ros->r_dn = dn;
252 } else {
253 op->o_req_dn = ndn;
254 }
255 op->o_req_ndn = ndn;
256 assert( BER_BVISNULL( &ros->r_ndn ) );
257 ros->r_ndn = ndn;
258
259 if ( ros->r_tag == LDAP_REQ_EXTENDED ) {
260 ros->rx_dn = ros->r_dn;
261 ros->rx_ndn = ros->r_ndn;
262 }
263
264 return LDAP_SUCCESS;
265 }
266
267 static int
268 rwm_op_add( Operation *op, SlapReply *rs )
269 {
270 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
271 struct ldaprwmap *rwmap =
272 (struct ldaprwmap *)on->on_bi.bi_private;
273
274 int rc,
275 i;
276 Attribute **ap = NULL;
277 char *olddn = op->o_req_dn.bv_val;
278 int isupdate;
279
280 rwm_op_cb *roc = rwm_callback_get( op );
281
282 rc = rwm_op_dn_massage( op, rs, "addDN", &roc->ros );
283 if ( rc != LDAP_SUCCESS ) {
284 op->o_bd->bd_info = (BackendInfo *)on->on_info;
285 send_ldap_error( op, rs, rc, "addDN massage error" );
286 return -1;
287 }
288
289 if ( olddn != op->o_req_dn.bv_val ) {
290 ber_bvreplace( &op->ora_e->e_name, &op->o_req_dn );
291 ber_bvreplace( &op->ora_e->e_nname, &op->o_req_ndn );
292 }
293
294 /* Count number of attributes in entry */
295 isupdate = be_shadow_update( op );
296 for ( i = 0, ap = &op->oq_add.rs_e->e_attrs; *ap; ) {
297 Attribute *a;
298
299 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass ||
300 (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
301 {
302 int j, last;
303
304 last = (*ap)->a_numvals - 1;
305 for ( j = 0; !BER_BVISNULL( &(*ap)->a_vals[ j ] ); j++ ) {
306 struct ldapmapping *mapping = NULL;
307
308 ( void )rwm_mapping( &rwmap->rwm_oc, &(*ap)->a_vals[ j ],
309 &mapping, RWM_MAP );
310 if ( mapping == NULL ) {
311 if ( rwmap->rwm_at.drop_missing ) {
312 /* FIXME: we allow to remove objectClasses as well;
313 * if the resulting entry is inconsistent, that's
314 * the relayed database's business...
315 */
316 ch_free( (*ap)->a_vals[ j ].bv_val );
317 if ( last > j ) {
318 (*ap)->a_vals[ j ] = (*ap)->a_vals[ last ];
319 }
320 BER_BVZERO( &(*ap)->a_vals[ last ] );
321 (*ap)->a_numvals--;
322 last--;
323 j--;
324 }
325
326 } else {
327 ch_free( (*ap)->a_vals[ j ].bv_val );
328 ber_dupbv( &(*ap)->a_vals[ j ], &mapping->m_dst );
329 }
330 }
331
332 } else if ( !isupdate && !get_relax( op ) && (*ap)->a_desc->ad_type->sat_no_user_mod )
333 {
334 goto next_attr;
335
336 } else {
337 struct ldapmapping *mapping = NULL;
338
339 ( void )rwm_mapping( &rwmap->rwm_at, &(*ap)->a_desc->ad_cname,
340 &mapping, RWM_MAP );
341 if ( mapping == NULL ) {
342 if ( rwmap->rwm_at.drop_missing ) {
343 goto cleanup_attr;
344 }
345 }
346
347 if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
348 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
349 {
350 /*
351 * FIXME: rewrite could fail; in this case
352 * the operation should give up, right?
353 */
354 rc = rwm_dnattr_rewrite( op, rs, "addAttrDN",
355 (*ap)->a_vals,
356 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
357 if ( rc ) {
358 goto cleanup_attr;
359 }
360
361 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
362 rc = rwm_referral_rewrite( op, rs, "referralAttrDN",
363 (*ap)->a_vals,
364 (*ap)->a_nvals ? &(*ap)->a_nvals : NULL );
365 if ( rc != LDAP_SUCCESS ) {
366 goto cleanup_attr;
367 }
368 }
369
370 if ( mapping != NULL ) {
371 assert( mapping->m_dst_ad != NULL );
372 (*ap)->a_desc = mapping->m_dst_ad;
373 }
374 }
375
376 next_attr:;
377 ap = &(*ap)->a_next;
378 continue;
379
380 cleanup_attr:;
381 /* FIXME: leaking attribute/values? */
382 a = *ap;
383
384 *ap = (*ap)->a_next;
385 attr_free( a );
386 }
387
388 op->o_callback = &roc->cb;
389
390 return SLAP_CB_CONTINUE;
391 }
392
393 static int
394 rwm_conn_init( BackendDB *be, Connection *conn )
395 {
396 slap_overinst *on = (slap_overinst *) be->bd_info;
397 struct ldaprwmap *rwmap =
398 (struct ldaprwmap *)on->on_bi.bi_private;
399
400 ( void )rewrite_session_init( rwmap->rwm_rw, conn );
401
402 return SLAP_CB_CONTINUE;
403 }
404
405 static int
406 rwm_conn_destroy( BackendDB *be, Connection *conn )
407 {
408 slap_overinst *on = (slap_overinst *) be->bd_info;
409 struct ldaprwmap *rwmap =
410 (struct ldaprwmap *)on->on_bi.bi_private;
411
412 ( void )rewrite_session_delete( rwmap->rwm_rw, conn );
413
414 return SLAP_CB_CONTINUE;
415 }
416
417 static int
418 rwm_op_bind( Operation *op, SlapReply *rs )
419 {
420 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
421 int rc;
422
423 rwm_op_cb *roc = rwm_callback_get( op );
424
425 rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
426 if ( rc != LDAP_SUCCESS ) {
427 op->o_bd->bd_info = (BackendInfo *)on->on_info;
428 send_ldap_error( op, rs, rc, "bindDN massage error" );
429 return -1;
430 }
431
432 overlay_callback_after_backover( op, &roc->cb, 1 );
433
434 return SLAP_CB_CONTINUE;
435 }
436
437 static int
438 rwm_op_unbind( Operation *op, SlapReply *rs )
439 {
440 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
441 struct ldaprwmap *rwmap =
442 (struct ldaprwmap *)on->on_bi.bi_private;
443
444 rewrite_session_delete( rwmap->rwm_rw, op->o_conn );
445
446 return SLAP_CB_CONTINUE;
447 }
448
449 static int
450 rwm_op_compare( Operation *op, SlapReply *rs )
451 {
452 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
453 struct ldaprwmap *rwmap =
454 (struct ldaprwmap *)on->on_bi.bi_private;
455
456 int rc;
457 struct berval mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
458
459 rwm_op_cb *roc = rwm_callback_get( op );
460
461 rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
462 if ( rc != LDAP_SUCCESS ) {
463 op->o_bd->bd_info = (BackendInfo *)on->on_info;
464 send_ldap_error( op, rs, rc, "compareDN massage error" );
465 return -1;
466 }
467
468 /* if the attribute is an objectClass, try to remap its value */
469 if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
470 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass )
471 {
472 rwm_map( &rwmap->rwm_oc, &op->orc_ava->aa_value,
473 &mapped_vals[0], RWM_MAP );
474 if ( BER_BVISNULL( &mapped_vals[0] ) || BER_BVISEMPTY( &mapped_vals[0] ) )
475 {
476 op->o_bd->bd_info = (BackendInfo *)on->on_info;
477 send_ldap_error( op, rs, LDAP_OTHER, "compare objectClass map error" );
478 return -1;
479
480 } else if ( mapped_vals[0].bv_val != op->orc_ava->aa_value.bv_val ) {
481 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
482 op->o_tmpmemctx );
483 }
484
485 } else {
486 struct ldapmapping *mapping = NULL;
487 AttributeDescription *ad = op->orc_ava->aa_desc;
488
489 ( void )rwm_mapping( &rwmap->rwm_at, &op->orc_ava->aa_desc->ad_cname,
490 &mapping, RWM_MAP );
491 if ( mapping == NULL ) {
492 if ( rwmap->rwm_at.drop_missing ) {
493 op->o_bd->bd_info = (BackendInfo *)on->on_info;
494 send_ldap_error( op, rs, LDAP_OTHER, "compare attributeType map error" );
495 return -1;
496 }
497
498 } else {
499 assert( mapping->m_dst_ad != NULL );
500 ad = mapping->m_dst_ad;
501 }
502
503 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
504 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
505 {
506 struct berval *mapped_valsp[2];
507
508 mapped_valsp[0] = &mapped_vals[0];
509 mapped_valsp[1] = &mapped_vals[1];
510
511 mapped_vals[0] = op->orc_ava->aa_value;
512
513 rc = rwm_dnattr_rewrite( op, rs, "compareAttrDN", NULL, mapped_valsp );
514
515 if ( rc != LDAP_SUCCESS ) {
516 op->o_bd->bd_info = (BackendInfo *)on->on_info;
517 send_ldap_error( op, rs, rc, "compareAttrDN massage error" );
518 return -1;
519 }
520
521 if ( mapped_vals[ 0 ].bv_val != op->orc_ava->aa_value.bv_val ) {
522 /* NOTE: if we get here, rwm_dnattr_rewrite()
523 * already freed the old value, so now
524 * it's invalid */
525 ber_dupbv_x( &op->orc_ava->aa_value, &mapped_vals[0],
526 op->o_tmpmemctx );
527 ber_memfree_x( mapped_vals[ 0 ].bv_val, NULL );
528 }
529 }
530 op->orc_ava->aa_desc = ad;
531 }
532
533 op->o_callback = &roc->cb;
534
535 return SLAP_CB_CONTINUE;
536 }
537
538 static int
539 rwm_op_delete( Operation *op, SlapReply *rs )
540 {
541 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
542 int rc;
543
544 rwm_op_cb *roc = rwm_callback_get( op );
545
546 rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
547 if ( rc != LDAP_SUCCESS ) {
548 op->o_bd->bd_info = (BackendInfo *)on->on_info;
549 send_ldap_error( op, rs, rc, "deleteDN massage error" );
550 return -1;
551 }
552
553 op->o_callback = &roc->cb;
554
555 return SLAP_CB_CONTINUE;
556 }
557
558 static int
559 rwm_op_modify( Operation *op, SlapReply *rs )
560 {
561 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
562 struct ldaprwmap *rwmap =
563 (struct ldaprwmap *)on->on_bi.bi_private;
564
565 int isupdate;
566 Modifications **mlp;
567 int rc;
568
569 rwm_op_cb *roc = rwm_callback_get( op );
570
571 rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
572 if ( rc != LDAP_SUCCESS ) {
573 op->o_bd->bd_info = (BackendInfo *)on->on_info;
574 send_ldap_error( op, rs, rc, "modifyDN massage error" );
575 return -1;
576 }
577
578 isupdate = be_shadow_update( op );
579 for ( mlp = &op->orm_modlist; *mlp; ) {
580 int is_oc = 0;
581 Modifications *ml = *mlp;
582 struct ldapmapping *mapping = NULL;
583
584 /* ml points to a temporary mod until needs duplication */
585 if ( ml->sml_desc == slap_schema.si_ad_objectClass
586 || ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
587 {
588 is_oc = 1;
589
590 } else if ( !isupdate && !get_relax( op ) && ml->sml_desc->ad_type->sat_no_user_mod )
591 {
592 ml = ch_malloc( sizeof( Modifications ) );
593 *ml = **mlp;
594 if ( (*mlp)->sml_values ) {
595 ber_bvarray_dup_x( &ml->sml_values, (*mlp)->sml_values, NULL );
596 if ( (*mlp)->sml_nvalues ) {
597 ber_bvarray_dup_x( &ml->sml_nvalues, (*mlp)->sml_nvalues, NULL );
598 }
599 }
600 *mlp = ml;
601 goto next_mod;
602
603 } else {
604 int drop_missing;
605
606 drop_missing = rwm_mapping( &rwmap->rwm_at,
607 &ml->sml_desc->ad_cname,
608 &mapping, RWM_MAP );
609 if ( drop_missing || ( mapping != NULL && BER_BVISNULL( &mapping->m_dst ) ) )
610 {
611 goto skip_mod;
612 }
613 }
614
615 /* duplicate the modlist */
616 ml = ch_malloc( sizeof( Modifications ));
617 *ml = **mlp;
618 *mlp = ml;
619
620 if ( ml->sml_values != NULL ) {
621 int i, num;
622 struct berval *bva;
623
624 for ( num = 0; !BER_BVISNULL( &ml->sml_values[ num ] ); num++ )
625 /* count values */ ;
626
627 bva = ch_malloc( (num+1) * sizeof( struct berval ));
628 for (i=0; i<num; i++)
629 ber_dupbv( &bva[i], &ml->sml_values[i] );
630 BER_BVZERO( &bva[i] );
631 ml->sml_values = bva;
632
633 if ( ml->sml_nvalues ) {
634 bva = ch_malloc( (num+1) * sizeof( struct berval ));
635 for (i=0; i<num; i++)
636 ber_dupbv( &bva[i], &ml->sml_nvalues[i] );
637 BER_BVZERO( &bva[i] );
638 ml->sml_nvalues = bva;
639 }
640
641 if ( is_oc ) {
642 int last, j;
643
644 last = num-1;
645
646 for ( j = 0; !BER_BVISNULL( &ml->sml_values[ j ] ); j++ ) {
647 struct ldapmapping *oc_mapping = NULL;
648
649 ( void )rwm_mapping( &rwmap->rwm_oc, &ml->sml_values[ j ],
650 &oc_mapping, RWM_MAP );
651 if ( oc_mapping == NULL ) {
652 if ( rwmap->rwm_at.drop_missing ) {
653 /* FIXME: we allow to remove objectClasses as well;
654 * if the resulting entry is inconsistent, that's
655 * the relayed database's business...
656 */
657 if ( last > j ) {
658 ch_free( ml->sml_values[ j ].bv_val );
659 ml->sml_values[ j ] = ml->sml_values[ last ];
660 }
661 BER_BVZERO( &ml->sml_values[ last ] );
662 last--;
663 j--;
664 }
665
666 } else {
667 ch_free( ml->sml_values[ j ].bv_val );
668 ber_dupbv( &ml->sml_values[ j ], &oc_mapping->m_dst );
669 }
670 }
671
672 } else {
673 if ( ml->sml_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
674 || ( mapping != NULL && mapping->m_dst_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
675 {
676 rc = rwm_dnattr_rewrite( op, rs, "modifyAttrDN",
677 ml->sml_values,
678 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
679
680 } else if ( ml->sml_desc == slap_schema.si_ad_ref ) {
681 rc = rwm_referral_rewrite( op, rs,
682 "referralAttrDN",
683 ml->sml_values,
684 ml->sml_nvalues ? &ml->sml_nvalues : NULL );
685 if ( rc != LDAP_SUCCESS ) {
686 goto cleanup_mod;
687 }
688 }
689
690 if ( rc != LDAP_SUCCESS ) {
691 goto cleanup_mod;
692 }
693 }
694 }
695
696 next_mod:;
697 if ( mapping != NULL ) {
698 /* use new attribute description */
699 assert( mapping->m_dst_ad != NULL );
700 ml->sml_desc = mapping->m_dst_ad;
701 }
702
703 mlp = &ml->sml_next;
704 continue;
705
706 skip_mod:;
707 *mlp = (*mlp)->sml_next;
708 continue;
709
710 cleanup_mod:;
711 ml = *mlp;
712 *mlp = (*mlp)->sml_next;
713 slap_mod_free( &ml->sml_mod, 0 );
714 free( ml );
715 }
716
717 op->o_callback = &roc->cb;
718
719 return SLAP_CB_CONTINUE;
720 }
721
722 static int
723 rwm_op_modrdn( Operation *op, SlapReply *rs )
724 {
725 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
726 struct ldaprwmap *rwmap =
727 (struct ldaprwmap *)on->on_bi.bi_private;
728
729 int rc;
730 dncookie dc;
731
732 rwm_op_cb *roc = rwm_callback_get( op );
733
734 if ( op->orr_newSup ) {
735 struct berval nnewSup = BER_BVNULL;
736 struct berval newSup = BER_BVNULL;
737
738 /*
739 * Rewrite the new superior, if defined and required
740 */
741 dc.rwmap = rwmap;
742 dc.conn = op->o_conn;
743 dc.rs = rs;
744 dc.ctx = "newSuperiorDN";
745 newSup = *op->orr_newSup;
746 nnewSup = *op->orr_nnewSup;
747 rc = rwm_dn_massage_pretty_normalize( &dc, op->orr_newSup, &newSup, &nnewSup );
748 if ( rc != LDAP_SUCCESS ) {
749 op->o_bd->bd_info = (BackendInfo *)on->on_info;
750 send_ldap_error( op, rs, rc, "newSuperiorDN massage error" );
751 return -1;
752 }
753
754 if ( op->orr_newSup->bv_val != newSup.bv_val ) {
755 op->orr_newSup = op->o_tmpalloc( sizeof( struct berval ),
756 op->o_tmpmemctx );
757 op->orr_nnewSup = op->o_tmpalloc( sizeof( struct berval ),
758 op->o_tmpmemctx );
759 *op->orr_newSup = newSup;
760 *op->orr_nnewSup = nnewSup;
761 }
762 }
763
764 /*
765 * Rewrite the newRDN, if needed
766 */
767 {
768 struct berval newrdn = BER_BVNULL;
769 struct berval nnewrdn = BER_BVNULL;
770
771 dc.rwmap = rwmap;
772 dc.conn = op->o_conn;
773 dc.rs = rs;
774 dc.ctx = "newRDN";
775 newrdn = op->orr_newrdn;
776 nnewrdn = op->orr_nnewrdn;
777 rc = rwm_dn_massage_pretty_normalize( &dc, &op->orr_newrdn, &newrdn, &nnewrdn );
778 if ( rc != LDAP_SUCCESS ) {
779 op->o_bd->bd_info = (BackendInfo *)on->on_info;
780 send_ldap_error( op, rs, rc, "newRDN massage error" );
781 goto err;
782 }
783
784 if ( op->orr_newrdn.bv_val != newrdn.bv_val ) {
785 op->orr_newrdn = newrdn;
786 op->orr_nnewrdn = nnewrdn;
787 }
788 }
789
790 /*
791 * Rewrite the dn, if needed
792 */
793 rc = rwm_op_dn_massage( op, rs, "renameDN", &roc->ros );
794 if ( rc != LDAP_SUCCESS ) {
795 op->o_bd->bd_info = (BackendInfo *)on->on_info;
796 send_ldap_error( op, rs, rc, "renameDN massage error" );
797 goto err;
798 }
799
800 op->o_callback = &roc->cb;
801
802 rc = SLAP_CB_CONTINUE;
803
804 if ( 0 ) {
805 err:;
806 if ( op->orr_newSup != roc->ros.orr_newSup ) {
807 ch_free( op->orr_newSup->bv_val );
808 ch_free( op->orr_nnewSup->bv_val );
809 op->o_tmpfree( op->orr_newSup, op->o_tmpmemctx );
810 op->o_tmpfree( op->orr_nnewSup, op->o_tmpmemctx );
811 op->orr_newSup = roc->ros.orr_newSup;
812 op->orr_nnewSup = roc->ros.orr_nnewSup;
813 }
814
815 if ( op->orr_newrdn.bv_val != roc->ros.orr_newrdn.bv_val ) {
816 ch_free( op->orr_newrdn.bv_val );
817 ch_free( op->orr_nnewrdn.bv_val );
818 op->orr_newrdn = roc->ros.orr_newrdn;
819 op->orr_nnewrdn = roc->ros.orr_nnewrdn;
820 }
821 }
822
823 return rc;
824 }
825
826
827 static int
828 rwm_swap_attrs( Operation *op, SlapReply *rs )
829 {
830 slap_callback *cb = op->o_callback;
831 rwm_op_state *ros = cb->sc_private;
832
833 rs->sr_attrs = ros->ors_attrs;
834
835 /* other overlays might have touched op->ors_attrs,
836 * so we restore the original version here, otherwise
837 * attribute-mapping might fail */
838 op->ors_attrs = ros->mapped_attrs;
839
840 return SLAP_CB_CONTINUE;
841 }
842
843 /*
844 * NOTE: this implementation of get/release entry is probably far from
845 * optimal. The rationale consists in intercepting the request directed
846 * to the underlying database, in order to rewrite/remap the request,
847 * perform it using the modified data, duplicate the resulting entry
848 * and finally free it when release is called.
849 * This implies that subsequent overlays are not called, as the request
850 * is directly shunted to the underlying database.
851 */
852 static int
853 rwm_entry_release_rw( Operation *op, Entry *e, int rw )
854 {
855 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
856
857 /* can't be ours */
858 if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
859 return SLAP_CB_CONTINUE;
860 }
861
862 /* just free entry if (probably) ours */
863 if ( e->e_private == NULL && BER_BVISNULL( &e->e_bv ) ) {
864 entry_free( e );
865 return LDAP_SUCCESS;
866 }
867
868 return SLAP_CB_CONTINUE;
869 }
870
871 static int
872 rwm_entry_get_rw( Operation *op, struct berval *ndn,
873 ObjectClass *oc, AttributeDescription *at, int rw, Entry **ep )
874 {
875 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
876 int rc;
877 BackendDB db;
878 Operation op2;
879 SlapReply rs = { REP_SEARCH };
880
881 rwm_op_state ros = { 0 };
882 struct berval mndn = BER_BVNULL;
883
884 if ( ((BackendInfo *)on->on_info->oi_orig)->bi_entry_get_rw == NULL ) {
885 return SLAP_CB_CONTINUE;
886 }
887
888 /* massage DN */
889 op2.o_tag = LDAP_REQ_SEARCH;
890 op2 = *op;
891 op2.o_req_dn = *ndn;
892 op2.o_req_ndn = *ndn;
893 rc = rwm_op_dn_massage( &op2, &rs, "searchDN", &ros );
894 if ( rc != LDAP_SUCCESS ) {
895 return LDAP_OTHER;
896 }
897
898 mndn = BER_BVISNULL( &ros.r_ndn ) ? *ndn : ros.r_ndn;
899
900 /* map attribute & objectClass */
901 if ( at != NULL ) {
902 }
903
904 if ( oc != NULL ) {
905 }
906
907 /* fetch entry */
908 db = *op->o_bd;
909 op2.o_bd = &db;
910 op2.o_bd->bd_info = (BackendInfo *)on->on_info->oi_orig;
911 op2.ors_attrs = slap_anlist_all_attributes;
912 rc = op2.o_bd->bd_info->bi_entry_get_rw( &op2, &mndn, oc, at, rw, ep );
913 if ( rc == LDAP_SUCCESS && *ep != NULL ) {
914 /* we assume be_entry_release() needs to be called */
915 rs.sr_flags = REP_ENTRY_MUSTRELEASE;
916 rs.sr_entry = *ep;
917
918 /* duplicate & release */
919 op2.o_bd->bd_info = (BackendInfo *)on;
920 rc = rwm_send_entry( &op2, &rs );
921 RS_ASSERT( rs.sr_flags & REP_ENTRY_MUSTFLUSH );
922 if ( rc == SLAP_CB_CONTINUE ) {
923 *ep = rs.sr_entry;
924 rc = LDAP_SUCCESS;
925 } else {
926 assert( rc != LDAP_SUCCESS && rs.sr_entry == *ep );
927 *ep = NULL;
928 op2.o_bd->bd_info = (BackendInfo *)on->on_info;
929 be_entry_release_r( &op2, rs.sr_entry );
930 op2.o_bd->bd_info = (BackendInfo *)on;
931 }
932 }
933
934 if ( !BER_BVISNULL( &ros.r_ndn) && ros.r_ndn.bv_val != ndn->bv_val ) {
935 op->o_tmpfree( ros.r_ndn.bv_val, op->o_tmpmemctx );
936 }
937
938 return rc;
939 }
940
941 static int
942 rwm_op_search( Operation *op, SlapReply *rs )
943 {
944 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
945 struct ldaprwmap *rwmap =
946 (struct ldaprwmap *)on->on_bi.bi_private;
947
948 int rc;
949 dncookie dc;
950
951 struct berval fstr = BER_BVNULL;
952 Filter *f = NULL;
953
954 AttributeName *an = NULL;
955
956 char *text = NULL;
957
958 rwm_op_cb *roc = rwm_callback_get( op );
959
960 rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
961 "searchFilter", op->ors_filterstr.bv_val );
962 if ( rc == LDAP_SUCCESS )
963 rc = rwm_op_dn_massage( op, rs, "searchDN", &roc->ros );
964 if ( rc != LDAP_SUCCESS ) {
965 text = "searchDN massage error";
966 goto error_return;
967 }
968
969 /*
970 * Rewrite the dn if needed
971 */
972 dc.rwmap = rwmap;
973 dc.conn = op->o_conn;
974 dc.rs = rs;
975 dc.ctx = "searchFilterAttrDN";
976
977 rc = rwm_filter_map_rewrite( op, &dc, op->ors_filter, &fstr );
978 if ( rc != LDAP_SUCCESS ) {
979 text = "searchFilter/searchFilterAttrDN massage error";
980 goto error_return;
981 }
982
983 f = str2filter_x( op, fstr.bv_val );
984
985 if ( f == NULL ) {
986 text = "massaged filter parse error";
987 goto error_return;
988 }
989
990 op->ors_filter = f;
991 op->ors_filterstr = fstr;
992
993 rc = rwm_map_attrnames( op, &rwmap->rwm_at, &rwmap->rwm_oc,
994 op->ors_attrs, &an, RWM_MAP );
995 if ( rc != LDAP_SUCCESS ) {
996 text = "attribute list mapping error";
997 goto error_return;
998 }
999
1000 op->ors_attrs = an;
1001 /* store the mapped Attributes for later usage, in
1002 * the case that other overlays change op->ors_attrs */
1003 roc->ros.mapped_attrs = an;
1004 roc->cb.sc_response = rwm_swap_attrs;
1005
1006 op->o_callback = &roc->cb;
1007
1008 return SLAP_CB_CONTINUE;
1009
1010 error_return:;
1011 if ( an != NULL ) {
1012 ch_free( an );
1013 }
1014
1015 if ( f != NULL ) {
1016 filter_free_x( op, f, 1 );
1017 }
1018
1019 if ( !BER_BVISNULL( &fstr ) ) {
1020 op->o_tmpfree( fstr.bv_val, op->o_tmpmemctx );
1021 }
1022
1023 rwm_op_rollback( op, rs, &roc->ros );
1024 op->oq_search = roc->ros.oq_search;
1025 op->o_tmpfree( roc, op->o_tmpmemctx );
1026
1027 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1028 send_ldap_error( op, rs, rc, text );
1029
1030 return -1;
1031
1032 }
1033
1034 static int
1035 rwm_exop_passwd( Operation *op, SlapReply *rs )
1036 {
1037 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1038 int rc;
1039 rwm_op_cb *roc;
1040
1041 struct berval id = BER_BVNULL,
1042 pwold = BER_BVNULL,
1043 pwnew = BER_BVNULL;
1044 BerElement *ber = NULL;
1045
1046 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
1047 return LDAP_SUCCESS;
1048 }
1049
1050 if ( !SLAP_ISGLOBALOVERLAY( op->o_bd ) ) {
1051 rs->sr_err = LDAP_OTHER;
1052 return rs->sr_err;
1053 }
1054
1055 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
1056 &pwold, &pwnew, &rs->sr_text );
1057 if ( rs->sr_err != LDAP_SUCCESS ) {
1058 return rs->sr_err;
1059 }
1060
1061 if ( !BER_BVISNULL( &id ) ) {
1062 char idNul = id.bv_val[id.bv_len];
1063 id.bv_val[id.bv_len] = '\0';
1064 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
1065 &op->o_req_ndn, op->o_tmpmemctx );
1066 id.bv_val[id.bv_len] = idNul;
1067 if ( rs->sr_err != LDAP_SUCCESS ) {
1068 rs->sr_text = "Invalid DN";
1069 return rs->sr_err;
1070 }
1071
1072 } else {
1073 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
1074 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
1075 }
1076
1077 roc = rwm_callback_get( op );
1078
1079 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1080 if ( rc != LDAP_SUCCESS ) {
1081 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1082 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1083 return -1;
1084 }
1085
1086 ber = ber_alloc_t( LBER_USE_DER );
1087 if ( !ber ) {
1088 rs->sr_err = LDAP_OTHER;
1089 rs->sr_text = "No memory";
1090 return rs->sr_err;
1091 }
1092 ber_printf( ber, "{" );
1093 if ( !BER_BVISNULL( &id )) {
1094 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_ID,
1095 &op->o_req_dn );
1096 }
1097 if ( !BER_BVISNULL( &pwold )) {
1098 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_OLD, &pwold );
1099 }
1100 if ( !BER_BVISNULL( &pwnew )) {
1101 ber_printf( ber, "tO", LDAP_TAG_EXOP_MODIFY_PASSWD_NEW, &pwnew );
1102 }
1103 ber_printf( ber, "N}" );
1104 ber_flatten( ber, &op->ore_reqdata );
1105 ber_free( ber, 1 );
1106
1107 op->o_callback = &roc->cb;
1108
1109 return SLAP_CB_CONTINUE;
1110 }
1111
1112 static struct exop {
1113 struct berval oid;
1114 BI_op_extended *extended;
1115 } exop_table[] = {
1116 { BER_BVC(LDAP_EXOP_MODIFY_PASSWD), rwm_exop_passwd },
1117 { BER_BVNULL, NULL }
1118 };
1119
1120 static int
1121 rwm_extended( Operation *op, SlapReply *rs )
1122 {
1123 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1124 int rc;
1125 rwm_op_cb *roc;
1126
1127 int i;
1128
1129 for ( i = 0; exop_table[i].extended != NULL; i++ ) {
1130 if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
1131 {
1132 rc = exop_table[i].extended( op, rs );
1133 switch ( rc ) {
1134 case LDAP_SUCCESS:
1135 break;
1136
1137 case SLAP_CB_CONTINUE:
1138 case SLAPD_ABANDON:
1139 return rc;
1140
1141 default:
1142 send_ldap_result( op, rs );
1143 return rc;
1144 }
1145 break;
1146 }
1147 }
1148
1149 roc = rwm_callback_get( op );
1150
1151 rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
1152 if ( rc != LDAP_SUCCESS ) {
1153 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1154 send_ldap_error( op, rs, rc, "extendedDN massage error" );
1155 return -1;
1156 }
1157
1158 /* TODO: rewrite/map extended data ? ... */
1159 op->o_callback = &roc->cb;
1160
1161 return SLAP_CB_CONTINUE;
1162 }
1163
1164 static void
1165 rwm_matched( Operation *op, SlapReply *rs )
1166 {
1167 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1168 struct ldaprwmap *rwmap =
1169 (struct ldaprwmap *)on->on_bi.bi_private;
1170
1171 struct berval dn, mdn;
1172 dncookie dc;
1173 int rc;
1174
1175 if ( rs->sr_matched == NULL ) {
1176 return;
1177 }
1178
1179 dc.rwmap = rwmap;
1180 dc.conn = op->o_conn;
1181 dc.rs = rs;
1182 dc.ctx = "matchedDN";
1183 ber_str2bv( rs->sr_matched, 0, 0, &dn );
1184 mdn = dn;
1185 rc = rwm_dn_massage_pretty( &dc, &dn, &mdn );
1186 if ( rc != LDAP_SUCCESS ) {
1187 rs->sr_err = rc;
1188 rs->sr_text = "Rewrite error";
1189
1190 } else if ( mdn.bv_val != dn.bv_val ) {
1191 if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
1192 ch_free( (void *)rs->sr_matched );
1193
1194 } else {
1195 rs->sr_flags |= REP_MATCHED_MUSTBEFREED;
1196 }
1197 rs->sr_matched = mdn.bv_val;
1198 }
1199 }
1200
1201 static int
1202 rwm_attrs( Operation *op, SlapReply *rs, Attribute** a_first, int stripEntryDN )
1203 {
1204 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1205 struct ldaprwmap *rwmap =
1206 (struct ldaprwmap *)on->on_bi.bi_private;
1207
1208 dncookie dc;
1209 int rc;
1210 Attribute **ap;
1211 int isupdate;
1212 int check_duplicate_attrs = 0;
1213
1214 /*
1215 * Rewrite the dn attrs, if needed
1216 */
1217 dc.rwmap = rwmap;
1218 dc.conn = op->o_conn;
1219 dc.rs = NULL;
1220
1221 /* FIXME: the entries are in the remote mapping form;
1222 * so we need to select those attributes we are willing
1223 * to return, and remap them accordingly */
1224
1225 /* FIXME: in principle, one could map an attribute
1226 * on top of another, which already exists.
1227 * As such, in the end there might exist more than
1228 * one instance of an attribute.
1229 * We should at least check if this occurs, and issue
1230 * an error (because multiple instances of attrs in
1231 * response are not valid), or merge the values (what
1232 * about duplicate values?) */
1233 isupdate = be_shadow_update( op );
1234 for ( ap = a_first; *ap; ) {
1235 struct ldapmapping *mapping = NULL;
1236 int drop_missing;
1237 int last = -1;
1238 Attribute *a;
1239
1240 if ( ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS ) &&
1241 op->ors_attrs != NULL &&
1242 !SLAP_USERATTRS( rs->sr_attr_flags ) &&
1243 !ad_inlist( (*ap)->a_desc, op->ors_attrs ) )
1244 {
1245 goto cleanup_attr;
1246 }
1247
1248 drop_missing = rwm_mapping( &rwmap->rwm_at,
1249 &(*ap)->a_desc->ad_cname, &mapping, RWM_REMAP );
1250 if ( drop_missing || ( mapping != NULL && BER_BVISEMPTY( &mapping->m_dst ) ) )
1251 {
1252 goto cleanup_attr;
1253 }
1254 if ( mapping != NULL ) {
1255 assert( mapping->m_dst_ad != NULL );
1256
1257 /* try to normalize mapped Attributes if the original
1258 * AttributeType was not normalized */
1259 if ( (!(*ap)->a_desc->ad_type->sat_equality ||
1260 !(*ap)->a_desc->ad_type->sat_equality->smr_normalize) &&
1261 mapping->m_dst_ad->ad_type->sat_equality &&
1262 mapping->m_dst_ad->ad_type->sat_equality->smr_normalize )
1263 {
1264 if ((rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS))
1265 {
1266 int i = 0;
1267
1268 last = (*ap)->a_numvals;
1269 if ( last )
1270 {
1271 (*ap)->a_nvals = ch_malloc( (last+1) * sizeof(struct berval) );
1272
1273 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[i]); i++ ) {
1274 int rc;
1275 /*
1276 * check that each value is valid per syntax
1277 * and pretty if appropriate
1278 */
1279 rc = mapping->m_dst_ad->ad_type->sat_equality->smr_normalize(
1280 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1281 mapping->m_dst_ad->ad_type->sat_syntax,
1282 mapping->m_dst_ad->ad_type->sat_equality,
1283 &(*ap)->a_vals[i], &(*ap)->a_nvals[i],
1284 NULL );
1285
1286 if ( rc != LDAP_SUCCESS ) {
1287 /* FIXME: this is wrong, putting a non-normalized value
1288 * into nvals. But when a proxy sends us bogus data,
1289 * we still need to give it to the client, even if it
1290 * violates the syntax. I.e., we don't want to silently
1291 * drop things and trigger an apparent data loss.
1292 */
1293 ber_dupbv( &(*ap)->a_nvals[i], &(*ap)->a_vals[i] );
1294 }
1295 }
1296 BER_BVZERO( &(*ap)->a_nvals[i] );
1297 }
1298
1299 } else {
1300 assert( (*ap)->a_nvals == (*ap)->a_vals );
1301 (*ap)->a_nvals = NULL;
1302 ber_bvarray_dup_x( &(*ap)->a_nvals, (*ap)->a_vals, NULL );
1303 }
1304 }
1305
1306 /* rewrite the attribute description */
1307 (*ap)->a_desc = mapping->m_dst_ad;
1308
1309 /* will need to check for duplicate attrs */
1310 check_duplicate_attrs++;
1311 }
1312
1313 if ( (*ap)->a_desc == slap_schema.si_ad_entryDN ) {
1314 if ( stripEntryDN ) {
1315 /* will be generated by frontend */
1316 goto cleanup_attr;
1317 }
1318
1319 } else if ( !isupdate
1320 && !get_relax( op )
1321 && (*ap)->a_desc->ad_type->sat_no_user_mod
1322 && (*ap)->a_desc->ad_type != slap_schema.si_at_undefined )
1323 {
1324 goto next_attr;
1325 }
1326
1327 if ( last == -1 ) { /* not yet counted */
1328 last = (*ap)->a_numvals;
1329 }
1330
1331 if ( last == 0 ) {
1332 /* empty? leave it in place because of attrsonly and vlv */
1333 goto next_attr;
1334 }
1335 last--;
1336
1337 if ( (*ap)->a_desc == slap_schema.si_ad_objectClass
1338 || (*ap)->a_desc == slap_schema.si_ad_structuralObjectClass )
1339 {
1340 struct berval *bv;
1341
1342 for ( bv = (*ap)->a_vals; !BER_BVISNULL( bv ); bv++ ) {
1343 struct berval mapped;
1344
1345 rwm_map( &rwmap->rwm_oc, &bv[0], &mapped, RWM_REMAP );
1346 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
1347 remove_oc:;
1348 ch_free( bv[0].bv_val );
1349 BER_BVZERO( &bv[0] );
1350 if ( &(*ap)->a_vals[last] > &bv[0] ) {
1351 bv[0] = (*ap)->a_vals[last];
1352 BER_BVZERO( &(*ap)->a_vals[last] );
1353 }
1354 last--;
1355 bv--;
1356
1357 } else if ( mapped.bv_val != bv[0].bv_val
1358 && ber_bvstrcasecmp( &mapped, &bv[0] ) != 0 )
1359 {
1360 int i;
1361
1362 for ( i = 0; !BER_BVISNULL( &(*ap)->a_vals[ i ] ); i++ ) {
1363 if ( &(*ap)->a_vals[ i ] == bv ) {
1364 continue;
1365 }
1366
1367 if ( ber_bvstrcasecmp( &mapped, &(*ap)->a_vals[ i ] ) == 0 ) {
1368 break;
1369 }
1370 }
1371
1372 if ( !BER_BVISNULL( &(*ap)->a_vals[ i ] ) ) {
1373 goto remove_oc;
1374 }
1375
1376 /*
1377 * FIXME: after LBER_FREEing
1378 * the value is replaced by
1379 * ch_alloc'ed memory
1380 */
1381 ber_bvreplace( &bv[0], &mapped );
1382
1383 /* FIXME: will need to check
1384 * if the structuralObjectClass
1385 * changed */
1386 }
1387 }
1388
1389 /*
1390 * It is necessary to try to rewrite attributes with
1391 * dn syntax because they might be used in ACLs as
1392 * members of groups; since ACLs are applied to the
1393 * rewritten stuff, no dn-based subject clause could
1394 * be used at the ldap backend side (see
1395 * http://www.OpenLDAP.org/faq/data/cache/452.html)
1396 * The problem can be overcome by moving the dn-based
1397 * ACLs to the target directory server, and letting
1398 * everything pass thru the ldap backend. */
1399 /* FIXME: handle distinguishedName-like syntaxes, like
1400 * nameAndOptionalUID */
1401 } else if ( (*ap)->a_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName
1402 || ( mapping != NULL && mapping->m_src_ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) )
1403 {
1404 dc.ctx = "searchAttrDN";
1405 rc = rwm_dnattr_result_rewrite( &dc, (*ap)->a_vals, (*ap)->a_nvals );
1406 if ( rc != LDAP_SUCCESS ) {
1407 goto cleanup_attr;
1408 }
1409
1410 } else if ( (*ap)->a_desc == slap_schema.si_ad_ref ) {
1411 dc.ctx = "searchAttrDN";
1412 rc = rwm_referral_result_rewrite( &dc, (*ap)->a_vals );
1413 if ( rc != LDAP_SUCCESS ) {
1414 goto cleanup_attr;
1415 }
1416 }
1417
1418
1419 next_attr:;
1420 ap = &(*ap)->a_next;
1421 continue;
1422
1423 cleanup_attr:;
1424 a = *ap;
1425 *ap = (*ap)->a_next;
1426
1427 attr_free( a );
1428 }
1429
1430 /* only check if some mapping occurred */
1431 if ( check_duplicate_attrs ) {
1432 for ( ap = a_first; *ap != NULL; ap = &(*ap)->a_next ) {
1433 Attribute **tap;
1434
1435 for ( tap = &(*ap)->a_next; *tap != NULL; ) {
1436 if ( (*tap)->a_desc == (*ap)->a_desc ) {
1437 Entry e = { 0 };
1438 Modification mod = { 0 };
1439 const char *text = NULL;
1440 char textbuf[ SLAP_TEXT_BUFLEN ];
1441 Attribute *next = (*tap)->a_next;
1442
1443 BER_BVSTR( &e.e_name, "" );
1444 BER_BVSTR( &e.e_nname, "" );
1445 e.e_attrs = *ap;
1446 mod.sm_op = LDAP_MOD_ADD;
1447 mod.sm_desc = (*ap)->a_desc;
1448 mod.sm_type = mod.sm_desc->ad_cname;
1449 mod.sm_numvals = (*tap)->a_numvals;
1450 mod.sm_values = (*tap)->a_vals;
1451 if ( (*tap)->a_nvals != (*tap)->a_vals ) {
1452 mod.sm_nvalues = (*tap)->a_nvals;
1453 }
1454
1455 (void)modify_add_values( &e, &mod,
1456 /* permissive */ 1,
1457 &text, textbuf, sizeof( textbuf ) );
1458
1459 /* should not insert new attrs! */
1460 assert( e.e_attrs == *ap );
1461
1462 attr_free( *tap );
1463 *tap = next;
1464
1465 } else {
1466 tap = &(*tap)->a_next;
1467 }
1468 }
1469 }
1470 }
1471
1472 return 0;
1473 }
1474
1475 /* Should return SLAP_CB_CONTINUE or failure, never LDAP_SUCCESS. */
1476 static int
1477 rwm_send_entry( Operation *op, SlapReply *rs )
1478 {
1479 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1480 struct ldaprwmap *rwmap =
1481 (struct ldaprwmap *)on->on_bi.bi_private;
1482
1483 Entry *e = NULL;
1484 struct berval dn = BER_BVNULL,
1485 ndn = BER_BVNULL;
1486 dncookie dc;
1487 int rc;
1488
1489 assert( rs->sr_entry != NULL );
1490
1491 /*
1492 * Rewrite the dn of the result, if needed
1493 */
1494 dc.rwmap = rwmap;
1495 dc.conn = op->o_conn;
1496 dc.rs = NULL;
1497 dc.ctx = "searchEntryDN";
1498
1499 e = rs->sr_entry;
1500 if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
1501 /* FIXME: all we need to duplicate are:
1502 * - dn
1503 * - ndn
1504 * - attributes that are requested
1505 * - no values if attrsonly is set
1506 */
1507 e = entry_dup( e );
1508 if ( e == NULL ) {
1509 rc = LDAP_NO_MEMORY;
1510 goto fail;
1511 }
1512 } else if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1513 /* ITS#6423: REP_ENTRY_MUSTRELEASE incompatible
1514 * with REP_ENTRY_MODIFIABLE */
1515 RS_ASSERT( 0 );
1516 rc = 1;
1517 goto fail;
1518 }
1519
1520 /*
1521 * Note: this may fail if the target host(s) schema differs
1522 * from the one known to the meta, and a DN with unknown
1523 * attributes is returned.
1524 */
1525 dn = e->e_name;
1526 ndn = e->e_nname;
1527 rc = rwm_dn_massage_pretty_normalize( &dc, &e->e_name, &dn, &ndn );
1528 if ( rc != LDAP_SUCCESS ) {
1529 rc = 1;
1530 goto fail;
1531 }
1532
1533 if ( e->e_name.bv_val != dn.bv_val ) {
1534 ch_free( e->e_name.bv_val );
1535 ch_free( e->e_nname.bv_val );
1536
1537 e->e_name = dn;
1538 e->e_nname = ndn;
1539 }
1540
1541 /* TODO: map entry attribute types, objectclasses
1542 * and dn-valued attribute values */
1543
1544 /* FIXME: the entries are in the remote mapping form;
1545 * so we need to select those attributes we are willing
1546 * to return, and remap them accordingly */
1547 (void)rwm_attrs( op, rs, &e->e_attrs, 1 );
1548
1549 if ( e != rs->sr_entry ) {
1550 /* Reimplementing rs_replace_entry(), I suppose to
1551 * bypass our own dubious rwm_entry_release_rw() */
1552 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1553 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1554 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1555 be_entry_release_r( op, rs->sr_entry );
1556 op->o_bd->bd_info = (BackendInfo *)on;
1557 } else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
1558 entry_free( rs->sr_entry );
1559 }
1560 rs->sr_entry = e;
1561 rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
1562 }
1563
1564 return SLAP_CB_CONTINUE;
1565
1566 fail:;
1567 if ( e != NULL && e != rs->sr_entry ) {
1568 if ( e->e_name.bv_val == dn.bv_val ) {
1569 BER_BVZERO( &e->e_name );
1570 }
1571
1572 if ( e->e_nname.bv_val == ndn.bv_val ) {
1573 BER_BVZERO( &e->e_nname );
1574 }
1575
1576 entry_free( e );
1577 }
1578
1579 if ( !BER_BVISNULL( &dn ) ) {
1580 ch_free( dn.bv_val );
1581 }
1582
1583 if ( !BER_BVISNULL( &ndn ) ) {
1584 ch_free( ndn.bv_val );
1585 }
1586
1587 return rc;
1588 }
1589
1590 static int
1591 rwm_operational( Operation *op, SlapReply *rs )
1592 {
1593 /* FIXME: the entries are in the remote mapping form;
1594 * so we need to select those attributes we are willing
1595 * to return, and remap them accordingly */
1596 if ( rs->sr_operational_attrs ) {
1597 rwm_attrs( op, rs, &rs->sr_operational_attrs, 1 );
1598 }
1599
1600 return SLAP_CB_CONTINUE;
1601 }
1602
1603 #if 0
1604 /* don't use this; it cannot be reverted, and leaves op->o_req_dn
1605 * rewritten for subsequent operations; fine for plain suffixmassage,
1606 * but destroys everything else */
1607 static int
1608 rwm_chk_referrals( Operation *op, SlapReply *rs )
1609 {
1610 slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
1611 int rc;
1612
1613 rc = rwm_op_dn_massage( op, rs, "referralCheckDN" );
1614 if ( rc != LDAP_SUCCESS ) {
1615 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1616 send_ldap_error( op, rs, rc, "referralCheckDN massage error" );
1617 return -1;
1618 }
1619
1620 return SLAP_CB_CONTINUE;
1621 }
1622 #endif
1623
1624 static int
1625 rwm_rw_config(
1626 BackendDB *be,
1627 const char *fname,
1628 int lineno,
1629 int argc,
1630 char **argv )
1631 {
1632 slap_overinst *on = (slap_overinst *) be->bd_info;
1633 struct ldaprwmap *rwmap =
1634 (struct ldaprwmap *)on->on_bi.bi_private;
1635
1636 return rewrite_parse( rwmap->rwm_rw,
1637 fname, lineno, argc, argv );
1638
1639 return 0;
1640 }
1641
1642 static int
1643 rwm_suffixmassage_config(
1644 BackendDB *be,
1645 const char *fname,
1646 int lineno,
1647 int argc,
1648 char **argv )
1649 {
1650 slap_overinst *on = (slap_overinst *) be->bd_info;
1651 struct ldaprwmap *rwmap =
1652 (struct ldaprwmap *)on->on_bi.bi_private;
1653
1654 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
1655 int massaged;
1656 int rc;
1657
1658 /*
1659 * syntax:
1660 *
1661 * suffixmassage [<suffix>] <massaged suffix>
1662 *
1663 * the [<suffix>] field must be defined as a valid suffix
1664 * for the current database;
1665 * the <massaged suffix> shouldn't have already been
1666 * defined as a valid suffix for the current server
1667 */
1668 if ( argc == 2 ) {
1669 if ( be->be_suffix == NULL ) {
1670 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1671 " \"suffixMassage [<suffix>]"
1672 " <massaged suffix>\" without "
1673 "<suffix> part requires database "
1674 "suffix be defined first.\n",
1675 fname, lineno, 0 );
1676 return 1;
1677 }
1678 bvnc = be->be_suffix[ 0 ];
1679 massaged = 1;
1680
1681 } else if ( argc == 3 ) {
1682 ber_str2bv( argv[ 1 ], 0, 0, &bvnc );
1683 massaged = 2;
1684
1685 } else {
1686 Debug( LDAP_DEBUG_ANY, "%s: line %d: syntax is"
1687 " \"suffixMassage [<suffix>]"
1688 " <massaged suffix>\"\n",
1689 fname, lineno, 0 );
1690 return 1;
1691 }
1692
1693 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
1694 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
1695 fname, lineno, bvnc.bv_val );
1696 return 1;
1697 }
1698
1699 ber_str2bv( argv[ massaged ], 0, 0, &brnc );
1700 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
1701 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix DN %s is invalid\n",
1702 fname, lineno, brnc.bv_val );
1703 free( nvnc.bv_val );
1704 free( pvnc.bv_val );
1705 return 1;
1706 }
1707
1708 /*
1709 * The suffix massaging is emulated
1710 * by means of the rewrite capabilities
1711 */
1712 rc = rwm_suffix_massage_config( rwmap->rwm_rw,
1713 &pvnc, &nvnc, &prnc, &nrnc );
1714 free( nvnc.bv_val );
1715 free( pvnc.bv_val );
1716 free( nrnc.bv_val );
1717 free( prnc.bv_val );
1718
1719 return rc;
1720 }
1721
1722 static int
1723 rwm_m_config(
1724 BackendDB *be,
1725 const char *fname,
1726 int lineno,
1727 int argc,
1728 char **argv )
1729 {
1730 slap_overinst *on = (slap_overinst *) be->bd_info;
1731 struct ldaprwmap *rwmap =
1732 (struct ldaprwmap *)on->on_bi.bi_private;
1733
1734 /* objectclass/attribute mapping */
1735 return rwm_map_config( &rwmap->rwm_oc,
1736 &rwmap->rwm_at,
1737 fname, lineno, argc, argv );
1738 }
1739
1740 static int
1741 rwm_response( Operation *op, SlapReply *rs )
1742 {
1743 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1744 struct ldaprwmap *rwmap =
1745 (struct ldaprwmap *)on->on_bi.bi_private;
1746
1747 int rc;
1748
1749 if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
1750 return rwm_send_entry( op, rs );
1751 }
1752
1753 switch( op->o_tag ) {
1754 case LDAP_REQ_SEARCH:
1755 case LDAP_REQ_BIND:
1756 case LDAP_REQ_ADD:
1757 case LDAP_REQ_DELETE:
1758 case LDAP_REQ_MODRDN:
1759 case LDAP_REQ_MODIFY:
1760 case LDAP_REQ_COMPARE:
1761 case LDAP_REQ_EXTENDED:
1762 if ( rs->sr_ref ) {
1763 dncookie dc;
1764
1765 /*
1766 * Rewrite the dn of the referrals, if needed
1767 */
1768 dc.rwmap = rwmap;
1769 dc.conn = op->o_conn;
1770 dc.rs = NULL;
1771 dc.ctx = "referralDN";
1772 rc = rwm_referral_result_rewrite( &dc, rs->sr_ref );
1773 /* FIXME: impossible, so far */
1774 if ( rc != LDAP_SUCCESS ) {
1775 rs->sr_err = rc;
1776 break;
1777 }
1778 }
1779
1780 rwm_matched( op, rs );
1781 break;
1782 }
1783
1784 return SLAP_CB_CONTINUE;
1785 }
1786
1787 static int
1788 rwm_db_config(
1789 BackendDB *be,
1790 const char *fname,
1791 int lineno,
1792 int argc,
1793 char **argv )
1794 {
1795 slap_overinst *on = (slap_overinst *) be->bd_info;
1796 struct ldaprwmap *rwmap =
1797 (struct ldaprwmap *)on->on_bi.bi_private;
1798
1799 int rc = 0;
1800 char *argv0 = NULL;
1801
1802 if ( strncasecmp( argv[ 0 ], "rwm-", STRLENOF( "rwm-" ) ) == 0 ) {
1803 argv0 = argv[ 0 ];
1804 argv[ 0 ] = &argv0[ STRLENOF( "rwm-" ) ];
1805 }
1806
1807 if ( strncasecmp( argv[0], "rewrite", STRLENOF("rewrite") ) == 0 ) {
1808 rc = rwm_rw_config( be, fname, lineno, argc, argv );
1809
1810 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
1811 rc = rwm_m_config( be, fname, lineno, argc, argv );
1812
1813 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
1814 rc = rwm_suffixmassage_config( be, fname, lineno, argc, argv );
1815
1816 } else if ( strcasecmp( argv[0], "t-f-support" ) == 0 ) {
1817 if ( argc != 2 ) {
1818 Debug( LDAP_DEBUG_ANY,
1819 "%s: line %d: \"t-f-support {no|yes|discover}\" needs 1 argument.\n",
1820 fname, lineno, 0 );
1821 return( 1 );
1822 }
1823
1824 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1825 rwmap->rwm_flags &= ~(RWM_F_SUPPORT_T_F_MASK2);
1826
1827 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1828 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F;
1829
1830 /* TODO: not implemented yet */
1831 } else if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
1832 Debug( LDAP_DEBUG_ANY,
1833 "%s: line %d: \"discover\" not supported yet "
1834 "in \"t-f-support {no|yes|discover}\".\n",
1835 fname, lineno, 0 );
1836 return( 1 );
1837 #if 0
1838 rwmap->rwm_flags |= RWM_F_SUPPORT_T_F_DISCOVER;
1839 #endif
1840
1841 } else {
1842 Debug( LDAP_DEBUG_ANY,
1843 "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
1844 fname, lineno, argv[ 1 ] );
1845 return 1;
1846 }
1847
1848 } else if ( strcasecmp( argv[0], "normalize-mapped-attrs" ) == 0 ) {
1849 if ( argc !=2 ) {
1850 Debug( LDAP_DEBUG_ANY,
1851 "%s: line %d: \"normalize-mapped-attrs {no|yes}\" needs 1 argument.\n",
1852 fname, lineno, 0 );
1853 return( 1 );
1854 }
1855
1856 if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
1857 rwmap->rwm_flags &= ~(RWM_F_NORMALIZE_MAPPED_ATTRS);
1858
1859 } else if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
1860 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
1861 }
1862
1863 } else {
1864 rc = SLAP_CONF_UNKNOWN;
1865 }
1866
1867 if ( argv0 ) {
1868 argv[ 0 ] = argv0;
1869 }
1870
1871 return rc;
1872 }
1873
1874 /*
1875 * dynamic configuration...
1876 */
1877
1878 enum {
1879 /* rewrite */
1880 RWM_CF_REWRITE = 1,
1881
1882 /* map */
1883 RWM_CF_MAP,
1884 RWM_CF_T_F_SUPPORT,
1885 RWM_CF_NORMALIZE_MAPPED,
1886 RWM_CF_DROP_UNREQUESTED,
1887
1888 RWM_CF_LAST
1889 };
1890
1891 static slap_verbmasks t_f_mode[] = {
1892 { BER_BVC( "true" ), RWM_F_SUPPORT_T_F },
1893 { BER_BVC( "yes" ), RWM_F_SUPPORT_T_F },
1894 { BER_BVC( "discover" ), RWM_F_SUPPORT_T_F_DISCOVER },
1895 { BER_BVC( "false" ), RWM_F_NONE },
1896 { BER_BVC( "no" ), RWM_F_NONE },
1897 { BER_BVNULL, 0 }
1898 };
1899
1900 static ConfigDriver rwm_cf_gen;
1901
1902 static ConfigTable rwmcfg[] = {
1903 { "rwm-rewrite", "rewrite",
1904 2, 0, STRLENOF("rwm-rewrite"),
1905 ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1906 "( OLcfgOvAt:16.1 NAME 'olcRwmRewrite' "
1907 "DESC 'Rewrites strings' "
1908 "EQUALITY caseIgnoreMatch "
1909 "SYNTAX OMsDirectoryString "
1910 "X-ORDERED 'VALUES' )",
1911 NULL, NULL },
1912
1913 { "rwm-suffixmassage", "[virtual]> <real",
1914 2, 3, 0, ARG_MAGIC|RWM_CF_REWRITE, rwm_cf_gen,
1915 NULL, NULL, NULL },
1916
1917 { "rwm-t-f-support", "true|false|discover",
1918 2, 2, 0, ARG_MAGIC|RWM_CF_T_F_SUPPORT, rwm_cf_gen,
1919 "( OLcfgOvAt:16.2 NAME 'olcRwmTFSupport' "
1920 "DESC 'Absolute filters support' "
1921 "SYNTAX OMsDirectoryString "
1922 "SINGLE-VALUE )",
1923 NULL, NULL },
1924
1925 { "rwm-map", "{objectClass|attribute}",
1926 2, 4, 0, ARG_MAGIC|RWM_CF_MAP, rwm_cf_gen,
1927 "( OLcfgOvAt:16.3 NAME 'olcRwmMap' "
1928 "DESC 'maps attributes/objectClasses' "
1929 "EQUALITY caseIgnoreMatch "
1930 "SYNTAX OMsDirectoryString "
1931 "X-ORDERED 'VALUES' )",
1932 NULL, NULL },
1933
1934 { "rwm-normalize-mapped-attrs", "true|false",
1935 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_NORMALIZE_MAPPED, rwm_cf_gen,
1936 "( OLcfgOvAt:16.4 NAME 'olcRwmNormalizeMapped' "
1937 "DESC 'Normalize mapped attributes/objectClasses' "
1938 "SYNTAX OMsBoolean "
1939 "SINGLE-VALUE )",
1940 NULL, NULL },
1941
1942 { "rwm-drop-unrequested-attrs", "true|false",
1943 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|RWM_CF_DROP_UNREQUESTED, rwm_cf_gen,
1944 "( OLcfgOvAt:16.5 NAME 'olcRwmDropUnrequested' "
1945 "DESC 'Drop unrequested attributes' "
1946 "SYNTAX OMsBoolean "
1947 "SINGLE-VALUE )",
1948 NULL, NULL },
1949
1950 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
1951 };
1952
1953 static ConfigOCs rwmocs[] = {
1954 { "( OLcfgOvOc:16.1 "
1955 "NAME 'olcRwmConfig' "
1956 "DESC 'Rewrite/remap configuration' "
1957 "SUP olcOverlayConfig "
1958 "MAY ( "
1959 "olcRwmRewrite $ "
1960 "olcRwmTFSupport $ "
1961 "olcRwmMap $ "
1962 "olcRwmNormalizeMapped $ "
1963 "olcRwmDropUnrequested"
1964 ") )",
1965 Cft_Overlay, rwmcfg, NULL, NULL },
1966 { NULL, 0, NULL }
1967 };
1968
1969 static void
1970 slap_bv_x_ordered_unparse( BerVarray in, BerVarray *out )
1971 {
1972 int i;
1973 BerVarray bva = NULL;
1974 char ibuf[32], *ptr;
1975 struct berval idx;
1976
1977 assert( in != NULL );
1978
1979 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ )
1980 /* count'em */ ;
1981
1982 if ( i == 0 ) {
1983 return;
1984 }
1985
1986 idx.bv_val = ibuf;
1987
1988 bva = ch_malloc( ( i + 1 ) * sizeof(struct berval) );
1989 BER_BVZERO( &bva[ 0 ] );
1990
1991 for ( i = 0; !BER_BVISNULL( &in[i] ); i++ ) {
1992 idx.bv_len = snprintf( idx.bv_val, sizeof( ibuf ), "{%d}", i );
1993 if ( idx.bv_len >= sizeof( ibuf ) ) {
1994 ber_bvarray_free( bva );
1995 return;
1996 }
1997
1998 bva[i].bv_len = idx.bv_len + in[i].bv_len;
1999 bva[i].bv_val = ch_malloc( bva[i].bv_len + 1 );
2000 ptr = lutil_strcopy( bva[i].bv_val, ibuf );
2001 ptr = lutil_strcopy( ptr, in[i].bv_val );
2002 *ptr = '\0';
2003 BER_BVZERO( &bva[ i + 1 ] );
2004 }
2005
2006 *out = bva;
2007 }
2008
2009 static int
2010 rwm_bva_add(
2011 BerVarray *bva,
2012 int idx,
2013 char **argv )
2014 {
2015 char *line;
2016 struct berval bv;
2017
2018 line = ldap_charray2str( argv, "\" \"" );
2019 if ( line != NULL ) {
2020 int len = strlen( argv[ 0 ] );
2021
2022 ber_str2bv( line, 0, 0, &bv );
2023 AC_MEMCPY( &bv.bv_val[ len ], &bv.bv_val[ len + 1 ],
2024 bv.bv_len - ( len + 1 ) );
2025 bv.bv_val[ bv.bv_len - 1 ] = '"';
2026
2027 if ( idx == -1 ) {
2028 ber_bvarray_add( bva, &bv );
2029
2030 } else {
2031 (*bva)[ idx ] = bv;
2032 }
2033
2034 return 0;
2035 }
2036
2037 return -1;
2038 }
2039
2040 static int
2041 rwm_bva_rewrite_add(
2042 struct ldaprwmap *rwmap,
2043 int idx,
2044 char **argv )
2045 {
2046 return rwm_bva_add( &rwmap->rwm_bva_rewrite, idx, argv );
2047 }
2048
2049 #ifdef unused
2050 static int
2051 rwm_bva_map_add(
2052 struct ldaprwmap *rwmap,
2053 int idx,
2054 char **argv )
2055 {
2056 return rwm_bva_add( &rwmap->rwm_bva_map, idx, argv );
2057 }
2058 #endif /* unused */
2059
2060 static int
2061 rwm_info_init( struct rewrite_info ** rwm_rw )
2062 {
2063 char *rargv[ 3 ];
2064
2065 *rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
2066 if ( *rwm_rw == NULL ) {
2067 return -1;
2068 }
2069
2070 /* this rewriteContext by default must be null;
2071 * rules can be added if required */
2072 rargv[ 0 ] = "rewriteContext";
2073 rargv[ 1 ] = "searchFilter";
2074 rargv[ 2 ] = NULL;
2075 rewrite_parse( *rwm_rw, "<suffix massage>", 1, 2, rargv );
2076
2077 rargv[ 0 ] = "rewriteContext";
2078 rargv[ 1 ] = "default";
2079 rargv[ 2 ] = NULL;
2080 rewrite_parse( *rwm_rw, "<suffix massage>", 2, 2, rargv );
2081
2082 return 0;
2083 }
2084
2085 static int
2086 rwm_cf_gen( ConfigArgs *c )
2087 {
2088 slap_overinst *on = (slap_overinst *)c->bi;
2089 struct ldaprwmap *rwmap =
2090 (struct ldaprwmap *)on->on_bi.bi_private;
2091
2092 BackendDB db;
2093 char *argv0;
2094 int idx0 = 0;
2095 int rc = 0;
2096
2097 db = *c->be;
2098 db.bd_info = c->bi;
2099
2100 if ( c->op == SLAP_CONFIG_EMIT ) {
2101 struct berval bv = BER_BVNULL;
2102
2103 switch ( c->type ) {
2104 case RWM_CF_REWRITE:
2105 if ( rwmap->rwm_bva_rewrite == NULL ) {
2106 rc = 1;
2107
2108 } else {
2109 slap_bv_x_ordered_unparse( rwmap->rwm_bva_rewrite, &c->rvalue_vals );
2110 if ( !c->rvalue_vals ) {
2111 rc = 1;
2112 }
2113 }
2114 break;
2115
2116 case RWM_CF_T_F_SUPPORT:
2117 enum_to_verb( t_f_mode, (rwmap->rwm_flags & RWM_F_SUPPORT_T_F_MASK2), &bv );
2118 if ( BER_BVISNULL( &bv ) ) {
2119 /* there's something wrong... */
2120 assert( 0 );
2121 rc = 1;
2122
2123 } else {
2124 value_add_one( &c->rvalue_vals, &bv );
2125 }
2126 break;
2127
2128 case RWM_CF_MAP:
2129 if ( rwmap->rwm_bva_map == NULL ) {
2130 rc = 1;
2131
2132 } else {
2133 slap_bv_x_ordered_unparse( rwmap->rwm_bva_map, &c->rvalue_vals );
2134 if ( !c->rvalue_vals ) {
2135 rc = 1;
2136 }
2137 }
2138 break;
2139
2140 case RWM_CF_NORMALIZE_MAPPED:
2141 c->value_int = ( rwmap->rwm_flags & RWM_F_NORMALIZE_MAPPED_ATTRS );
2142 break;
2143
2144 case RWM_CF_DROP_UNREQUESTED:
2145 c->value_int = ( rwmap->rwm_flags & RWM_F_DROP_UNREQUESTED_ATTRS );
2146 break;
2147
2148 default:
2149 assert( 0 );
2150 rc = 1;
2151 }
2152
2153 return rc;
2154
2155 } else if ( c->op == LDAP_MOD_DELETE ) {
2156 switch ( c->type ) {
2157 case RWM_CF_REWRITE:
2158 if ( c->valx >= 0 ) {
2159 int i;
2160
2161 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2162 /* count'em */ ;
2163
2164 if ( c->valx >= i ) {
2165 rc = 1;
2166 break;
2167 }
2168
2169 ber_memfree( rwmap->rwm_bva_rewrite[ c->valx ].bv_val );
2170 for ( i = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i + 1 ] ); i++ )
2171 {
2172 rwmap->rwm_bva_rewrite[ i ] = rwmap->rwm_bva_rewrite[ i + 1 ];
2173 }
2174 BER_BVZERO( &rwmap->rwm_bva_rewrite[ i ] );
2175
2176 rewrite_info_delete( &rwmap->rwm_rw );
2177 assert( rwmap->rwm_rw == NULL );
2178
2179 rc = rwm_info_init( &rwmap->rwm_rw );
2180
2181 for ( i = 0; !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2182 {
2183 ConfigArgs ca = { 0 };
2184
2185 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2186 ca.argc = 0;
2187 init_config_argv( &ca );
2188 config_parse_ldif( &ca );
2189
2190 argv0 = ca.argv[ 0 ];
2191 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2192
2193 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2194 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2195 ca.argc, ca.argv );
2196
2197 } else {
2198 rc = rwm_rw_config( &db, c->fname, c->lineno,
2199 ca.argc, ca.argv );
2200 }
2201
2202 ca.argv[ 0 ] = argv0;
2203
2204 ch_free( ca.tline );
2205 ch_free( ca.argv );
2206
2207 assert( rc == 0 );
2208 }
2209
2210 } else if ( rwmap->rwm_rw != NULL ) {
2211 rewrite_info_delete( &rwmap->rwm_rw );
2212 assert( rwmap->rwm_rw == NULL );
2213
2214 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2215 rwmap->rwm_bva_rewrite = NULL;
2216
2217 rc = rwm_info_init( &rwmap->rwm_rw );
2218 }
2219 break;
2220
2221 case RWM_CF_T_F_SUPPORT:
2222 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2223 break;
2224
2225 case RWM_CF_MAP:
2226 if ( c->valx >= 0 ) {
2227 struct ldapmap rwm_oc = rwmap->rwm_oc;
2228 struct ldapmap rwm_at = rwmap->rwm_at;
2229 char *argv[5];
2230 int cnt = 0;
2231
2232 if ( rwmap->rwm_bva_map ) {
2233 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
2234 /* count */ ;
2235 }
2236
2237 if ( c->valx >= cnt ) {
2238 rc = 1;
2239 break;
2240 }
2241
2242 memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
2243 memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
2244
2245 /* re-parse all mappings except the one
2246 * that needs to be eliminated */
2247 argv[0] = "map";
2248 for ( cnt = 0; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2249 ConfigArgs ca = { 0 };
2250
2251 if ( cnt == c->valx ) {
2252 continue;
2253 }
2254
2255 ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2256 ca.argc = 0;
2257 init_config_argv( &ca );
2258 config_parse_ldif( &ca );
2259
2260 argv[1] = ca.argv[0];
2261 argv[2] = ca.argv[1];
2262 argv[3] = ca.argv[2];
2263 argv[4] = ca.argv[3];
2264
2265 rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2266
2267 ch_free( ca.tline );
2268 ch_free( ca.argv );
2269
2270 /* in case of failure, restore
2271 * the existing mapping */
2272 if ( rc ) {
2273 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2274 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2275 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2276 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2277 rwmap->rwm_oc = rwm_oc;
2278 rwmap->rwm_at = rwm_at;
2279 break;
2280 }
2281 }
2282
2283 /* in case of success, destroy the old mapping
2284 * and eliminate the deleted one */
2285 if ( rc == 0 ) {
2286 avl_free( rwm_oc.remap, rwm_mapping_dst_free );
2287 avl_free( rwm_oc.map, rwm_mapping_free );
2288 avl_free( rwm_at.remap, rwm_mapping_dst_free );
2289 avl_free( rwm_at.map, rwm_mapping_free );
2290
2291 ber_memfree( rwmap->rwm_bva_map[ c->valx ].bv_val );
2292 for ( cnt = c->valx; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2293 rwmap->rwm_bva_map[ cnt ] = rwmap->rwm_bva_map[ cnt + 1 ];
2294 }
2295 }
2296
2297 } else {
2298 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2299 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2300 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2301 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2302
2303 rwmap->rwm_oc.remap = NULL;
2304 rwmap->rwm_oc.map = NULL;
2305 rwmap->rwm_at.remap = NULL;
2306 rwmap->rwm_at.map = NULL;
2307
2308 ber_bvarray_free( rwmap->rwm_bva_map );
2309 rwmap->rwm_bva_map = NULL;
2310 }
2311 break;
2312
2313 case RWM_CF_NORMALIZE_MAPPED:
2314 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2315 break;
2316
2317 case RWM_CF_DROP_UNREQUESTED:
2318 rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2319 break;
2320
2321 default:
2322 return 1;
2323 }
2324 return rc;
2325 }
2326
2327 if ( strncasecmp( c->argv[ 0 ], "olcRwm", STRLENOF( "olcRwm" ) ) == 0 ) {
2328 idx0 = 1;
2329 }
2330
2331 switch ( c->type ) {
2332 case RWM_CF_REWRITE:
2333 if ( c->valx >= 0 ) {
2334 struct rewrite_info *rwm_rw = rwmap->rwm_rw;
2335 int i, last;
2336
2337 for ( last = 0; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ last ] ); last++ )
2338 /* count'em */ ;
2339
2340 if ( c->valx > last ) {
2341 c->valx = last;
2342 }
2343
2344 rwmap->rwm_rw = NULL;
2345 rc = rwm_info_init( &rwmap->rwm_rw );
2346
2347 for ( i = 0; i < c->valx; i++ ) {
2348 ConfigArgs ca = { 0 };
2349
2350 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2351 ca.argc = 0;
2352 init_config_argv( &ca );
2353 config_parse_ldif( &ca );
2354
2355 argv0 = ca.argv[ 0 ];
2356 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2357
2358 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2359 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2360 ca.argc, ca.argv );
2361
2362 } else {
2363 rc = rwm_rw_config( &db, c->fname, c->lineno,
2364 ca.argc, ca.argv );
2365 }
2366
2367 ca.argv[ 0 ] = argv0;
2368
2369 ch_free( ca.tline );
2370 ch_free( ca.argv );
2371
2372 assert( rc == 0 );
2373 }
2374
2375 argv0 = c->argv[ idx0 ];
2376 if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2377 return 1;
2378 }
2379 c->argv[ idx0 ] += STRLENOF( "rwm-" );
2380 if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2381 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2382 c->argc - idx0, &c->argv[ idx0 ] );
2383
2384 } else {
2385 rc = rwm_rw_config( &db, c->fname, c->lineno,
2386 c->argc - idx0, &c->argv[ idx0 ] );
2387 }
2388 c->argv[ idx0 ] = argv0;
2389 if ( rc != 0 ) {
2390 rewrite_info_delete( &rwmap->rwm_rw );
2391 assert( rwmap->rwm_rw == NULL );
2392
2393 rwmap->rwm_rw = rwm_rw;
2394 return 1;
2395 }
2396
2397 for ( i = c->valx; rwmap->rwm_bva_rewrite && !BER_BVISNULL( &rwmap->rwm_bva_rewrite[ i ] ); i++ )
2398 {
2399 ConfigArgs ca = { 0 };
2400
2401 ca.line = rwmap->rwm_bva_rewrite[ i ].bv_val;
2402 ca.argc = 0;
2403 init_config_argv( &ca );
2404 config_parse_ldif( &ca );
2405
2406 argv0 = ca.argv[ 0 ];
2407 ca.argv[ 0 ] += STRLENOF( "rwm-" );
2408
2409 if ( strcasecmp( ca.argv[ 0 ], "suffixmassage" ) == 0 ) {
2410 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2411 ca.argc, ca.argv );
2412
2413 } else {
2414 rc = rwm_rw_config( &db, c->fname, c->lineno,
2415 ca.argc, ca.argv );
2416 }
2417
2418 ca.argv[ 0 ] = argv0;
2419
2420 ch_free( ca.tline );
2421 ch_free( ca.argv );
2422
2423 assert( rc == 0 );
2424 }
2425
2426 rwmap->rwm_bva_rewrite = ch_realloc( rwmap->rwm_bva_rewrite,
2427 ( last + 2 )*sizeof( struct berval ) );
2428 BER_BVZERO( &rwmap->rwm_bva_rewrite[last+1] );
2429
2430 for ( i = last - 1; i >= c->valx; i-- )
2431 {
2432 rwmap->rwm_bva_rewrite[ i + 1 ] = rwmap->rwm_bva_rewrite[ i ];
2433 }
2434
2435 rwm_bva_rewrite_add( rwmap, c->valx, &c->argv[ idx0 ] );
2436
2437 rewrite_info_delete( &rwm_rw );
2438 assert( rwm_rw == NULL );
2439
2440 break;
2441 }
2442
2443 argv0 = c->argv[ idx0 ];
2444 if ( strncasecmp( argv0, "rwm-", STRLENOF( "rwm-" ) ) != 0 ) {
2445 return 1;
2446 }
2447 c->argv[ idx0 ] += STRLENOF( "rwm-" );
2448 if ( strcasecmp( c->argv[ idx0 ], "suffixmassage" ) == 0 ) {
2449 rc = rwm_suffixmassage_config( &db, c->fname, c->lineno,
2450 c->argc - idx0, &c->argv[ idx0 ] );
2451
2452 } else {
2453 rc = rwm_rw_config( &db, c->fname, c->lineno,
2454 c->argc - idx0, &c->argv[ idx0 ] );
2455 }
2456 c->argv[ idx0 ] = argv0;
2457 if ( rc ) {
2458 return 1;
2459
2460 } else {
2461 rwm_bva_rewrite_add( rwmap, -1, &c->argv[ idx0 ] );
2462 }
2463 break;
2464
2465 case RWM_CF_T_F_SUPPORT:
2466 rc = verb_to_mask( c->argv[ 1 ], t_f_mode );
2467 if ( BER_BVISNULL( &t_f_mode[ rc ].word ) ) {
2468 return 1;
2469 }
2470
2471 rwmap->rwm_flags &= ~RWM_F_SUPPORT_T_F_MASK2;
2472 rwmap->rwm_flags |= t_f_mode[ rc ].mask;
2473 rc = 0;
2474 break;
2475
2476 case RWM_CF_MAP:
2477 if ( c->valx >= 0 ) {
2478 struct ldapmap rwm_oc = rwmap->rwm_oc;
2479 struct ldapmap rwm_at = rwmap->rwm_at;
2480 char *argv[5];
2481 int cnt = 0;
2482
2483 if ( rwmap->rwm_bva_map ) {
2484 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ )
2485 /* count */ ;
2486 }
2487
2488 if ( c->valx >= cnt ) {
2489 c->valx = cnt;
2490 }
2491
2492 memset( &rwmap->rwm_oc, 0, sizeof( rwmap->rwm_oc ) );
2493 memset( &rwmap->rwm_at, 0, sizeof( rwmap->rwm_at ) );
2494
2495 /* re-parse all mappings, including the one
2496 * that needs to be added */
2497 argv[0] = "map";
2498 for ( cnt = 0; cnt < c->valx; cnt++ ) {
2499 ConfigArgs ca = { 0 };
2500
2501 ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2502 ca.argc = 0;
2503 init_config_argv( &ca );
2504 config_parse_ldif( &ca );
2505
2506 argv[1] = ca.argv[0];
2507 argv[2] = ca.argv[1];
2508 argv[3] = ca.argv[2];
2509 argv[4] = ca.argv[3];
2510
2511 rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2512
2513 ch_free( ca.tline );
2514 ch_free( ca.argv );
2515
2516 /* in case of failure, restore
2517 * the existing mapping */
2518 if ( rc ) {
2519 goto rwmmap_fail;
2520 }
2521 }
2522
2523 argv0 = c->argv[0];
2524 c->argv[0] = "map";
2525 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
2526 c->argv[0] = argv0;
2527 if ( rc ) {
2528 goto rwmmap_fail;
2529 }
2530
2531 if ( rwmap->rwm_bva_map ) {
2532 for ( ; !BER_BVISNULL( &rwmap->rwm_bva_map[ cnt ] ); cnt++ ) {
2533 ConfigArgs ca = { 0 };
2534
2535 ca.line = rwmap->rwm_bva_map[ cnt ].bv_val;
2536 ca.argc = 0;
2537 init_config_argv( &ca );
2538 config_parse_ldif( &ca );
2539
2540 argv[1] = ca.argv[0];
2541 argv[2] = ca.argv[1];
2542 argv[3] = ca.argv[2];
2543 argv[4] = ca.argv[3];
2544
2545 rc = rwm_m_config( &db, c->fname, c->lineno, ca.argc + 1, argv );
2546
2547 ch_free( ca.tline );
2548 ch_free( ca.argv );
2549
2550 /* in case of failure, restore
2551 * the existing mapping */
2552 if ( rc ) {
2553 goto rwmmap_fail;
2554 }
2555 }
2556 }
2557
2558 /* in case of success, destroy the old mapping
2559 * and add the new one */
2560 if ( rc == 0 ) {
2561 BerVarray tmp;
2562 struct berval bv, *bvp = &bv;
2563
2564 if ( rwm_bva_add( &bvp, 0, &c->argv[ idx0 ] ) ) {
2565 rc = 1;
2566 goto rwmmap_fail;
2567 }
2568
2569 tmp = ber_memrealloc( rwmap->rwm_bva_map,
2570 sizeof( struct berval )*( cnt + 2 ) );
2571 if ( tmp == NULL ) {
2572 ber_memfree( bv.bv_val );
2573 rc = 1;
2574 goto rwmmap_fail;
2575 }
2576 rwmap->rwm_bva_map = tmp;
2577 BER_BVZERO( &rwmap->rwm_bva_map[ cnt + 1 ] );
2578
2579 avl_free( rwm_oc.remap, rwm_mapping_dst_free );
2580 avl_free( rwm_oc.map, rwm_mapping_free );
2581 avl_free( rwm_at.remap, rwm_mapping_dst_free );
2582 avl_free( rwm_at.map, rwm_mapping_free );
2583
2584 for ( ; cnt-- > c->valx; ) {
2585 rwmap->rwm_bva_map[ cnt + 1 ] = rwmap->rwm_bva_map[ cnt ];
2586 }
2587 rwmap->rwm_bva_map[ c->valx ] = bv;
2588
2589 } else {
2590 rwmmap_fail:;
2591 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2592 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2593 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2594 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2595 rwmap->rwm_oc = rwm_oc;
2596 rwmap->rwm_at = rwm_at;
2597 }
2598
2599 break;
2600 }
2601
2602 argv0 = c->argv[ 0 ];
2603 c->argv[ 0 ] += STRLENOF( "rwm-" );
2604 rc = rwm_m_config( &db, c->fname, c->lineno, c->argc, c->argv );
2605 c->argv[ 0 ] = argv0;
2606 if ( rc ) {
2607 return 1;
2608
2609 } else {
2610 char *line;
2611 struct berval bv;
2612
2613 line = ldap_charray2str( &c->argv[ 1 ], " " );
2614 if ( line != NULL ) {
2615 ber_str2bv( line, 0, 0, &bv );
2616 ber_bvarray_add( &rwmap->rwm_bva_map, &bv );
2617 }
2618 }
2619 break;
2620
2621 case RWM_CF_NORMALIZE_MAPPED:
2622 if ( c->value_int ) {
2623 rwmap->rwm_flags |= RWM_F_NORMALIZE_MAPPED_ATTRS;
2624 } else {
2625 rwmap->rwm_flags &= ~RWM_F_NORMALIZE_MAPPED_ATTRS;
2626 }
2627 break;
2628
2629 case RWM_CF_DROP_UNREQUESTED:
2630 if ( c->value_int ) {
2631 rwmap->rwm_flags |= RWM_F_DROP_UNREQUESTED_ATTRS;
2632 } else {
2633 rwmap->rwm_flags &= ~RWM_F_DROP_UNREQUESTED_ATTRS;
2634 }
2635 break;
2636
2637 default:
2638 assert( 0 );
2639 return 1;
2640 }
2641
2642 return rc;
2643 }
2644
2645 static int
2646 rwm_db_init(
2647 BackendDB *be,
2648 ConfigReply *cr )
2649 {
2650 slap_overinst *on = (slap_overinst *) be->bd_info;
2651 struct ldaprwmap *rwmap;
2652 int rc = 0;
2653
2654 rwmap = (struct ldaprwmap *)ch_calloc( 1, sizeof( struct ldaprwmap ) );
2655
2656 /* default */
2657 rwmap->rwm_flags = RWM_F_DROP_UNREQUESTED_ATTRS;
2658
2659 rc = rwm_info_init( &rwmap->rwm_rw );
2660
2661 on->on_bi.bi_private = (void *)rwmap;
2662
2663 if ( rc ) {
2664 (void)rwm_db_destroy( be, NULL );
2665 }
2666
2667 return rc;
2668 }
2669
2670 static int
2671 rwm_db_destroy(
2672 BackendDB *be,
2673 ConfigReply *cr )
2674 {
2675 slap_overinst *on = (slap_overinst *) be->bd_info;
2676 int rc = 0;
2677
2678 if ( on->on_bi.bi_private ) {
2679 struct ldaprwmap *rwmap =
2680 (struct ldaprwmap *)on->on_bi.bi_private;
2681
2682 if ( rwmap->rwm_rw ) {
2683 rewrite_info_delete( &rwmap->rwm_rw );
2684 if ( rwmap->rwm_bva_rewrite )
2685 ber_bvarray_free( rwmap->rwm_bva_rewrite );
2686 }
2687
2688 avl_free( rwmap->rwm_oc.remap, rwm_mapping_dst_free );
2689 avl_free( rwmap->rwm_oc.map, rwm_mapping_free );
2690 avl_free( rwmap->rwm_at.remap, rwm_mapping_dst_free );
2691 avl_free( rwmap->rwm_at.map, rwm_mapping_free );
2692 ber_bvarray_free( rwmap->rwm_bva_map );
2693
2694 ch_free( rwmap );
2695 }
2696
2697 return rc;
2698 }
2699
2700 static slap_overinst rwm = { { NULL } };
2701
2702 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2703 static
2704 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2705 int
2706 rwm_initialize( void )
2707 {
2708 int rc;
2709
2710 /* Make sure we don't exceed the bits reserved for userland */
2711 config_check_userland( RWM_CF_LAST );
2712
2713 memset( &rwm, 0, sizeof( slap_overinst ) );
2714
2715 rwm.on_bi.bi_type = "rwm";
2716 rwm.on_bi.bi_flags =
2717 SLAPO_BFLAG_SINGLE |
2718 0;
2719
2720 rwm.on_bi.bi_db_init = rwm_db_init;
2721 rwm.on_bi.bi_db_config = rwm_db_config;
2722 rwm.on_bi.bi_db_destroy = rwm_db_destroy;
2723
2724 rwm.on_bi.bi_op_bind = rwm_op_bind;
2725 rwm.on_bi.bi_op_search = rwm_op_search;
2726 rwm.on_bi.bi_op_compare = rwm_op_compare;
2727 rwm.on_bi.bi_op_modify = rwm_op_modify;
2728 rwm.on_bi.bi_op_modrdn = rwm_op_modrdn;
2729 rwm.on_bi.bi_op_add = rwm_op_add;
2730 rwm.on_bi.bi_op_delete = rwm_op_delete;
2731 rwm.on_bi.bi_op_unbind = rwm_op_unbind;
2732 rwm.on_bi.bi_extended = rwm_extended;
2733 #if 1 /* TODO */
2734 rwm.on_bi.bi_entry_release_rw = rwm_entry_release_rw;
2735 rwm.on_bi.bi_entry_get_rw = rwm_entry_get_rw;
2736 #endif
2737
2738 rwm.on_bi.bi_operational = rwm_operational;
2739 rwm.on_bi.bi_chk_referrals = 0 /* rwm_chk_referrals */ ;
2740
2741 rwm.on_bi.bi_connection_init = rwm_conn_init;
2742 rwm.on_bi.bi_connection_destroy = rwm_conn_destroy;
2743
2744 rwm.on_response = rwm_response;
2745
2746 rwm.on_bi.bi_cf_ocs = rwmocs;
2747
2748 rc = config_register_schema( rwmcfg, rwmocs );
2749 if ( rc ) {
2750 return rc;
2751 }
2752
2753 return overlay_register( &rwm );
2754 }
2755
2756 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
2757 int
2758 init_module( int argc, char *argv[] )
2759 {
2760 return rwm_initialize();
2761 }
2762 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
2763
2764 #endif /* SLAPD_OVER_RWM */
2765