| 1 |
/* map.c - ldap backend mapping routines */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 1998-2024 The OpenLDAP Foundation. |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted only as authorized by the OpenLDAP |
| 10 |
* Public License. |
| 11 |
* |
| 12 |
* A copy of this license is available in the file LICENSE in the |
| 13 |
* top-level directory of the distribution or, alternatively, at |
| 14 |
* <http://www.OpenLDAP.org/license.html>. |
| 15 |
*/ |
| 16 |
/* ACKNOWLEDGEMENTS: |
| 17 |
* This work was initially developed by the Howard Chu for inclusion |
| 18 |
* in OpenLDAP Software and subsequently enhanced by Pierangelo |
| 19 |
* Masarati. |
| 20 |
*/ |
| 21 |
/* This is an altered version */ |
| 22 |
/* |
| 23 |
* Copyright 1999, Howard Chu, All rights reserved. <[email protected]> |
| 24 |
* |
| 25 |
* Permission is granted to anyone to use this software for any purpose |
| 26 |
* on any computer system, and to alter it and redistribute it, subject |
| 27 |
* to the following restrictions: |
| 28 |
* |
| 29 |
* 1. The author is not responsible for the consequences of use of this |
| 30 |
* software, no matter how awful, even if they arise from flaws in it. |
| 31 |
* |
| 32 |
* 2. The origin of this software must not be misrepresented, either by |
| 33 |
* explicit claim or by omission. Since few users ever read sources, |
| 34 |
* credits should appear in the documentation. |
| 35 |
* |
| 36 |
* 3. Altered versions must be plainly marked as such, and must not be |
| 37 |
* misrepresented as being the original software. Since few users |
| 38 |
* ever read sources, credits should appear in the documentation. |
| 39 |
* |
| 40 |
* 4. This notice may not be removed or altered. |
| 41 |
* |
| 42 |
* |
| 43 |
* |
| 44 |
* Copyright 2000, Pierangelo Masarati, All rights reserved. <[email protected]> |
| 45 |
* |
| 46 |
* This software is being modified by Pierangelo Masarati. |
| 47 |
* The previously reported conditions apply to the modified code as well. |
| 48 |
* Changes in the original code are highlighted where required. |
| 49 |
* Credits for the original code go to the author, Howard Chu. |
| 50 |
*/ |
| 51 |
|
| 52 |
#include "portable.h" |
| 53 |
|
| 54 |
#include <stdio.h> |
| 55 |
|
| 56 |
#include <ac/string.h> |
| 57 |
#include <ac/socket.h> |
| 58 |
|
| 59 |
#include "slap.h" |
| 60 |
#include "lutil.h" |
| 61 |
#include "../back-ldap/back-ldap.h" |
| 62 |
#include "back-meta.h" |
| 63 |
|
| 64 |
int |
| 65 |
mapping_cmp ( const void *c1, const void *c2 ) |
| 66 |
{ |
| 67 |
struct ldapmapping *map1 = (struct ldapmapping *)c1; |
| 68 |
struct ldapmapping *map2 = (struct ldapmapping *)c2; |
| 69 |
int rc = map1->src.bv_len - map2->src.bv_len; |
| 70 |
if (rc) return rc; |
| 71 |
return ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) ); |
| 72 |
} |
| 73 |
|
| 74 |
int |
| 75 |
mapping_dup ( void *c1, void *c2 ) |
| 76 |
{ |
| 77 |
struct ldapmapping *map1 = (struct ldapmapping *)c1; |
| 78 |
struct ldapmapping *map2 = (struct ldapmapping *)c2; |
| 79 |
|
| 80 |
return ( ( strcasecmp( map1->src.bv_val, map2->src.bv_val ) == 0 ) ? -1 : 0 ); |
| 81 |
} |
| 82 |
|
| 83 |
void |
| 84 |
ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m ) |
| 85 |
{ |
| 86 |
struct ldapmapping *mapping; |
| 87 |
|
| 88 |
assert( m != NULL ); |
| 89 |
|
| 90 |
*m = NULL; |
| 91 |
|
| 92 |
mapping = (struct ldapmapping *)ch_calloc( 2, |
| 93 |
sizeof( struct ldapmapping ) ); |
| 94 |
if ( mapping == NULL ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
ber_str2bv( "objectclass", STRLENOF("objectclass"), 1, &mapping[0].src); |
| 99 |
ber_dupbv( &mapping[0].dst, &mapping[0].src ); |
| 100 |
mapping[1].src = mapping[0].src; |
| 101 |
mapping[1].dst = mapping[0].dst; |
| 102 |
|
| 103 |
ldap_avl_insert( &lm->map, (caddr_t)&mapping[0], |
| 104 |
mapping_cmp, mapping_dup ); |
| 105 |
ldap_avl_insert( &lm->remap, (caddr_t)&mapping[1], |
| 106 |
mapping_cmp, mapping_dup ); |
| 107 |
*m = mapping; |
| 108 |
} |
| 109 |
|
| 110 |
int |
| 111 |
ldap_back_mapping ( struct ldapmap *map, struct berval *s, struct ldapmapping **m, |
| 112 |
int remap ) |
| 113 |
{ |
| 114 |
Avlnode *tree; |
| 115 |
struct ldapmapping fmapping; |
| 116 |
|
| 117 |
assert( m != NULL ); |
| 118 |
|
| 119 |
/* let special attrnames slip through (ITS#5760) */ |
| 120 |
if ( bvmatch( s, slap_bv_no_attrs ) |
| 121 |
|| bvmatch( s, slap_bv_all_user_attrs ) |
| 122 |
|| bvmatch( s, slap_bv_all_operational_attrs ) ) |
| 123 |
{ |
| 124 |
*m = NULL; |
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
if ( remap == BACKLDAP_REMAP ) { |
| 129 |
tree = map->remap; |
| 130 |
|
| 131 |
} else { |
| 132 |
tree = map->map; |
| 133 |
} |
| 134 |
|
| 135 |
fmapping.src = *s; |
| 136 |
*m = (struct ldapmapping *)ldap_avl_find( tree, (caddr_t)&fmapping, mapping_cmp ); |
| 137 |
if ( *m == NULL ) { |
| 138 |
return map->drop_missing; |
| 139 |
} |
| 140 |
|
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
void |
| 145 |
ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv, |
| 146 |
int remap ) |
| 147 |
{ |
| 148 |
struct ldapmapping *mapping; |
| 149 |
int drop_missing; |
| 150 |
|
| 151 |
/* map->map may be NULL when mapping is configured, |
| 152 |
* but map->remap can't */ |
| 153 |
if ( map->remap == NULL ) { |
| 154 |
*bv = *s; |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
BER_BVZERO( bv ); |
| 159 |
drop_missing = ldap_back_mapping( map, s, &mapping, remap ); |
| 160 |
if ( mapping != NULL ) { |
| 161 |
if ( !BER_BVISNULL( &mapping->dst ) ) { |
| 162 |
*bv = mapping->dst; |
| 163 |
} |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
if ( !drop_missing ) { |
| 168 |
*bv = *s; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
int |
| 173 |
ldap_back_map_attrs( |
| 174 |
Operation *op, |
| 175 |
struct ldapmap *at_map, |
| 176 |
AttributeName *an, |
| 177 |
int remap, |
| 178 |
char ***mapped_attrs ) |
| 179 |
{ |
| 180 |
int i, x, j; |
| 181 |
char **na; |
| 182 |
struct berval mapped; |
| 183 |
|
| 184 |
if ( an == NULL && op->o_bd->be_extra_anlist == NULL ) { |
| 185 |
*mapped_attrs = NULL; |
| 186 |
return LDAP_SUCCESS; |
| 187 |
} |
| 188 |
|
| 189 |
i = 0; |
| 190 |
if ( an != NULL ) { |
| 191 |
for ( ; !BER_BVISNULL( &an[i].an_name ); i++ ) |
| 192 |
/* */ ; |
| 193 |
} |
| 194 |
|
| 195 |
x = 0; |
| 196 |
if ( op->o_bd->be_extra_anlist != NULL ) { |
| 197 |
for ( ; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ ) |
| 198 |
/* */ ; |
| 199 |
} |
| 200 |
|
| 201 |
assert( i > 0 || x > 0 ); |
| 202 |
|
| 203 |
na = (char **)ber_memcalloc_x( i + x + 1, sizeof(char *), op->o_tmpmemctx ); |
| 204 |
if ( na == NULL ) { |
| 205 |
*mapped_attrs = NULL; |
| 206 |
return LDAP_NO_MEMORY; |
| 207 |
} |
| 208 |
|
| 209 |
j = 0; |
| 210 |
if ( i > 0 ) { |
| 211 |
for ( i = 0; !BER_BVISNULL( &an[i].an_name ); i++ ) { |
| 212 |
ldap_back_map( at_map, &an[i].an_name, &mapped, remap ); |
| 213 |
if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) { |
| 214 |
na[j++] = mapped.bv_val; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if ( x > 0 ) { |
| 220 |
for ( x = 0; !BER_BVISNULL( &op->o_bd->be_extra_anlist[x].an_name ); x++ ) { |
| 221 |
if ( op->o_bd->be_extra_anlist[x].an_desc && |
| 222 |
ad_inlist( op->o_bd->be_extra_anlist[x].an_desc, an ) ) |
| 223 |
{ |
| 224 |
continue; |
| 225 |
} |
| 226 |
|
| 227 |
ldap_back_map( at_map, &op->o_bd->be_extra_anlist[x].an_name, &mapped, remap ); |
| 228 |
if ( !BER_BVISNULL( &mapped ) && !BER_BVISEMPTY( &mapped ) ) { |
| 229 |
na[j++] = mapped.bv_val; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
if ( j == 0 && ( i > 0 || x > 0 ) ) { |
| 235 |
na[j++] = LDAP_NO_ATTRS; |
| 236 |
} |
| 237 |
na[j] = NULL; |
| 238 |
|
| 239 |
*mapped_attrs = na; |
| 240 |
|
| 241 |
return LDAP_SUCCESS; |
| 242 |
} |
| 243 |
|
| 244 |
static int |
| 245 |
map_attr_value( |
| 246 |
dncookie *dc, |
| 247 |
AttributeDescription *ad, |
| 248 |
struct berval *mapped_attr, |
| 249 |
struct berval *value, |
| 250 |
struct berval *mapped_value, |
| 251 |
int remap, |
| 252 |
void *memctx ) |
| 253 |
{ |
| 254 |
struct berval vtmp; |
| 255 |
int freeval = 0; |
| 256 |
|
| 257 |
ldap_back_map( &dc->target->mt_rwmap.rwm_at, &ad->ad_cname, mapped_attr, remap ); |
| 258 |
if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) { |
| 259 |
#if 0 |
| 260 |
/* |
| 261 |
* FIXME: are we sure we need to search oc_map if at_map fails? |
| 262 |
*/ |
| 263 |
ldap_back_map( &dc->target->mt_rwmap.rwm_oc, &ad->ad_cname, mapped_attr, remap ); |
| 264 |
if ( BER_BVISNULL( mapped_attr ) || BER_BVISEMPTY( mapped_attr ) ) { |
| 265 |
*mapped_attr = ad->ad_cname; |
| 266 |
} |
| 267 |
#endif |
| 268 |
if ( dc->target->mt_rwmap.rwm_at.drop_missing ) { |
| 269 |
return -1; |
| 270 |
} |
| 271 |
|
| 272 |
*mapped_attr = ad->ad_cname; |
| 273 |
} |
| 274 |
|
| 275 |
if ( value == NULL ) { |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) |
| 280 |
{ |
| 281 |
dncookie fdc = *dc; |
| 282 |
|
| 283 |
fdc.ctx = "searchFilterAttrDN"; |
| 284 |
|
| 285 |
switch ( ldap_back_dn_massage( &fdc, value, &vtmp ) ) { |
| 286 |
case LDAP_SUCCESS: |
| 287 |
if ( vtmp.bv_val != value->bv_val ) { |
| 288 |
freeval = 1; |
| 289 |
} |
| 290 |
break; |
| 291 |
|
| 292 |
case LDAP_UNWILLING_TO_PERFORM: |
| 293 |
return -1; |
| 294 |
|
| 295 |
case LDAP_OTHER: |
| 296 |
return -1; |
| 297 |
} |
| 298 |
|
| 299 |
} else if ( ad->ad_type->sat_equality && |
| 300 |
ad->ad_type->sat_equality->smr_usage & SLAP_MR_MUTATION_NORMALIZER ) |
| 301 |
{ |
| 302 |
if ( ad->ad_type->sat_equality->smr_normalize( |
| 303 |
(SLAP_MR_DENORMALIZE|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX), |
| 304 |
NULL, NULL, value, &vtmp, memctx ) ) |
| 305 |
{ |
| 306 |
return -1; |
| 307 |
} |
| 308 |
freeval = 2; |
| 309 |
|
| 310 |
} else if ( ad == slap_schema.si_ad_objectClass || ad == slap_schema.si_ad_structuralObjectClass ) { |
| 311 |
ldap_back_map( &dc->target->mt_rwmap.rwm_oc, value, &vtmp, remap ); |
| 312 |
if ( BER_BVISNULL( &vtmp ) || BER_BVISEMPTY( &vtmp ) ) { |
| 313 |
vtmp = *value; |
| 314 |
} |
| 315 |
|
| 316 |
} else { |
| 317 |
vtmp = *value; |
| 318 |
} |
| 319 |
|
| 320 |
filter_escape_value_x( &vtmp, mapped_value, memctx ); |
| 321 |
|
| 322 |
switch ( freeval ) { |
| 323 |
case 1: |
| 324 |
ber_memfree( vtmp.bv_val ); |
| 325 |
break; |
| 326 |
case 2: |
| 327 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
static int |
| 335 |
ldap_back_int_filter_map_rewrite( |
| 336 |
dncookie *dc, |
| 337 |
Filter *f, |
| 338 |
struct berval *fstr, |
| 339 |
int remap, |
| 340 |
void *memctx ) |
| 341 |
{ |
| 342 |
int i; |
| 343 |
Filter *p; |
| 344 |
struct berval atmp, |
| 345 |
vtmp, |
| 346 |
*tmp; |
| 347 |
static struct berval |
| 348 |
/* better than nothing... */ |
| 349 |
ber_bvfalse = BER_BVC( "(!(objectClass=*))" ), |
| 350 |
ber_bvtf_false = BER_BVC( "(|)" ), |
| 351 |
/* better than nothing... */ |
| 352 |
ber_bvtrue = BER_BVC( "(objectClass=*)" ), |
| 353 |
ber_bvtf_true = BER_BVC( "(&)" ), |
| 354 |
#if 0 |
| 355 |
/* no longer needed; preserved for completeness */ |
| 356 |
ber_bvundefined = BER_BVC( "(?=undefined)" ), |
| 357 |
#endif |
| 358 |
ber_bverror = BER_BVC( "(?=error)" ), |
| 359 |
ber_bvunknown = BER_BVC( "(?=unknown)" ), |
| 360 |
ber_bvnone = BER_BVC( "(?=none)" ); |
| 361 |
ber_len_t len; |
| 362 |
|
| 363 |
assert( fstr != NULL ); |
| 364 |
BER_BVZERO( fstr ); |
| 365 |
|
| 366 |
if ( f == NULL ) { |
| 367 |
ber_dupbv_x( fstr, &ber_bvnone, memctx ); |
| 368 |
return LDAP_OTHER; |
| 369 |
} |
| 370 |
|
| 371 |
switch ( ( f->f_choice & SLAPD_FILTER_MASK ) ) { |
| 372 |
case LDAP_FILTER_EQUALITY: |
| 373 |
if ( map_attr_value( dc, f->f_av_desc, &atmp, |
| 374 |
&f->f_av_value, &vtmp, remap, memctx ) ) |
| 375 |
{ |
| 376 |
goto computed; |
| 377 |
} |
| 378 |
|
| 379 |
fstr->bv_len = atmp.bv_len + vtmp.bv_len |
| 380 |
+ ( sizeof("(=)") - 1 ); |
| 381 |
fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx ); |
| 382 |
if ( !fstr->bv_val ) { |
| 383 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 384 |
return LDAP_NO_MEMORY; |
| 385 |
} |
| 386 |
|
| 387 |
snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)", |
| 388 |
atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" ); |
| 389 |
|
| 390 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 391 |
break; |
| 392 |
|
| 393 |
case LDAP_FILTER_GE: |
| 394 |
if ( map_attr_value( dc, f->f_av_desc, &atmp, |
| 395 |
&f->f_av_value, &vtmp, remap, memctx ) ) |
| 396 |
{ |
| 397 |
goto computed; |
| 398 |
} |
| 399 |
|
| 400 |
fstr->bv_len = atmp.bv_len + vtmp.bv_len |
| 401 |
+ ( sizeof("(>=)") - 1 ); |
| 402 |
fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx ); |
| 403 |
if ( !fstr->bv_val ) { |
| 404 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 405 |
return LDAP_NO_MEMORY; |
| 406 |
} |
| 407 |
|
| 408 |
snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)", |
| 409 |
atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" ); |
| 410 |
|
| 411 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 412 |
break; |
| 413 |
|
| 414 |
case LDAP_FILTER_LE: |
| 415 |
if ( map_attr_value( dc, f->f_av_desc, &atmp, |
| 416 |
&f->f_av_value, &vtmp, remap, memctx ) ) |
| 417 |
{ |
| 418 |
goto computed; |
| 419 |
} |
| 420 |
|
| 421 |
fstr->bv_len = atmp.bv_len + vtmp.bv_len |
| 422 |
+ ( sizeof("(<=)") - 1 ); |
| 423 |
fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx ); |
| 424 |
if ( !fstr->bv_val ) { |
| 425 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 426 |
return LDAP_NO_MEMORY; |
| 427 |
} |
| 428 |
|
| 429 |
snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)", |
| 430 |
atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" ); |
| 431 |
|
| 432 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 433 |
break; |
| 434 |
|
| 435 |
case LDAP_FILTER_APPROX: |
| 436 |
if ( map_attr_value( dc, f->f_av_desc, &atmp, |
| 437 |
&f->f_av_value, &vtmp, remap, memctx ) ) |
| 438 |
{ |
| 439 |
goto computed; |
| 440 |
} |
| 441 |
|
| 442 |
fstr->bv_len = atmp.bv_len + vtmp.bv_len |
| 443 |
+ ( sizeof("(~=)") - 1 ); |
| 444 |
fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx ); |
| 445 |
if ( !fstr->bv_val ) { |
| 446 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 447 |
return LDAP_NO_MEMORY; |
| 448 |
} |
| 449 |
|
| 450 |
snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)", |
| 451 |
atmp.bv_val, vtmp.bv_len ? vtmp.bv_val : "" ); |
| 452 |
|
| 453 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 454 |
break; |
| 455 |
|
| 456 |
case LDAP_FILTER_SUBSTRINGS: |
| 457 |
if ( map_attr_value( dc, f->f_sub_desc, &atmp, |
| 458 |
NULL, NULL, remap, memctx ) ) |
| 459 |
{ |
| 460 |
goto computed; |
| 461 |
} |
| 462 |
|
| 463 |
/* cannot be a DN ... */ |
| 464 |
|
| 465 |
fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) ); |
| 466 |
fstr->bv_val = ber_memalloc_x( fstr->bv_len + 128, memctx ); /* FIXME: why 128 ? */ |
| 467 |
if ( !fstr->bv_val ) { |
| 468 |
return LDAP_NO_MEMORY; |
| 469 |
} |
| 470 |
|
| 471 |
snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)", |
| 472 |
atmp.bv_val ); |
| 473 |
|
| 474 |
if ( !BER_BVISNULL( &f->f_sub_initial ) ) { |
| 475 |
char *tmp; |
| 476 |
|
| 477 |
len = fstr->bv_len; |
| 478 |
|
| 479 |
filter_escape_value_x( &f->f_sub_initial, &vtmp, memctx ); |
| 480 |
|
| 481 |
fstr->bv_len += vtmp.bv_len; |
| 482 |
tmp = ber_memrealloc_x( fstr->bv_val, fstr->bv_len + 1, memctx ); |
| 483 |
if ( !tmp ) { |
| 484 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 485 |
return LDAP_NO_MEMORY; |
| 486 |
} |
| 487 |
fstr->bv_val = tmp; |
| 488 |
|
| 489 |
snprintf( &fstr->bv_val[len - 2], vtmp.bv_len + 3, |
| 490 |
/* "(attr=" */ "%s*)", |
| 491 |
vtmp.bv_len ? vtmp.bv_val : "" ); |
| 492 |
|
| 493 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 494 |
} |
| 495 |
|
| 496 |
if ( f->f_sub_any != NULL ) { |
| 497 |
for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) { |
| 498 |
char *tmp; |
| 499 |
|
| 500 |
len = fstr->bv_len; |
| 501 |
filter_escape_value_x( &f->f_sub_any[i], &vtmp, memctx ); |
| 502 |
|
| 503 |
fstr->bv_len += vtmp.bv_len + 1; |
| 504 |
tmp = ber_memrealloc_x( fstr->bv_val, fstr->bv_len + 1, memctx ); |
| 505 |
if ( !tmp ) { |
| 506 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 507 |
return LDAP_NO_MEMORY; |
| 508 |
} |
| 509 |
fstr->bv_val = tmp; |
| 510 |
|
| 511 |
snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3, |
| 512 |
/* "(attr=[init]*[any*]" */ "%s*)", |
| 513 |
vtmp.bv_len ? vtmp.bv_val : "" ); |
| 514 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
if ( !BER_BVISNULL( &f->f_sub_final ) ) { |
| 519 |
char *tmp; |
| 520 |
|
| 521 |
len = fstr->bv_len; |
| 522 |
|
| 523 |
filter_escape_value_x( &f->f_sub_final, &vtmp, memctx ); |
| 524 |
|
| 525 |
fstr->bv_len += vtmp.bv_len; |
| 526 |
tmp = ber_memrealloc_x( fstr->bv_val, fstr->bv_len + 1, memctx ); |
| 527 |
if ( !tmp ) { |
| 528 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 529 |
return LDAP_NO_MEMORY; |
| 530 |
} |
| 531 |
fstr->bv_val = tmp; |
| 532 |
|
| 533 |
snprintf( &fstr->bv_val[len - 1], vtmp.bv_len + 3, |
| 534 |
/* "(attr=[init*][any*]" */ "%s)", |
| 535 |
vtmp.bv_len ? vtmp.bv_val : "" ); |
| 536 |
|
| 537 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 538 |
} |
| 539 |
|
| 540 |
break; |
| 541 |
|
| 542 |
case LDAP_FILTER_PRESENT: |
| 543 |
if ( map_attr_value( dc, f->f_desc, &atmp, |
| 544 |
NULL, NULL, remap, memctx ) ) |
| 545 |
{ |
| 546 |
goto computed; |
| 547 |
} |
| 548 |
|
| 549 |
fstr->bv_len = atmp.bv_len + ( STRLENOF( "(=*)" ) ); |
| 550 |
fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx ); |
| 551 |
if ( !fstr->bv_val ) { |
| 552 |
return LDAP_NO_MEMORY; |
| 553 |
} |
| 554 |
|
| 555 |
snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=*)", |
| 556 |
atmp.bv_val ); |
| 557 |
break; |
| 558 |
|
| 559 |
case LDAP_FILTER_AND: |
| 560 |
case LDAP_FILTER_OR: |
| 561 |
case LDAP_FILTER_NOT: |
| 562 |
fstr->bv_len = STRLENOF( "(%)" ); |
| 563 |
fstr->bv_val = ber_memalloc_x( fstr->bv_len + 128, memctx ); /* FIXME: why 128? */ |
| 564 |
|
| 565 |
snprintf( fstr->bv_val, fstr->bv_len + 1, "(%c)", |
| 566 |
f->f_choice == LDAP_FILTER_AND ? '&' : |
| 567 |
f->f_choice == LDAP_FILTER_OR ? '|' : '!' ); |
| 568 |
|
| 569 |
for ( p = f->f_list; p != NULL; p = p->f_next ) { |
| 570 |
int rc; |
| 571 |
|
| 572 |
len = fstr->bv_len; |
| 573 |
|
| 574 |
rc = ldap_back_int_filter_map_rewrite( dc, p, &vtmp, remap, memctx ); |
| 575 |
if ( rc != LDAP_SUCCESS ) { |
| 576 |
return rc; |
| 577 |
} |
| 578 |
|
| 579 |
fstr->bv_len += vtmp.bv_len; |
| 580 |
fstr->bv_val = ber_memrealloc_x( fstr->bv_val, fstr->bv_len + 1, memctx ); |
| 581 |
if ( !fstr->bv_val ) { |
| 582 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 583 |
return LDAP_NO_MEMORY; |
| 584 |
} |
| 585 |
|
| 586 |
snprintf( &fstr->bv_val[len-1], vtmp.bv_len + 2, |
| 587 |
/*"("*/ "%s)", vtmp.bv_len ? vtmp.bv_val : "" ); |
| 588 |
|
| 589 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 590 |
} |
| 591 |
|
| 592 |
break; |
| 593 |
|
| 594 |
case LDAP_FILTER_EXT: |
| 595 |
if ( f->f_mr_desc ) { |
| 596 |
if ( map_attr_value( dc, f->f_mr_desc, &atmp, |
| 597 |
&f->f_mr_value, &vtmp, remap, memctx ) ) |
| 598 |
{ |
| 599 |
goto computed; |
| 600 |
} |
| 601 |
|
| 602 |
} else { |
| 603 |
BER_BVSTR( &atmp, "" ); |
| 604 |
filter_escape_value_x( &f->f_mr_value, &vtmp, memctx ); |
| 605 |
} |
| 606 |
|
| 607 |
/* FIXME: cleanup (less ?: operators...) */ |
| 608 |
fstr->bv_len = atmp.bv_len + |
| 609 |
( f->f_mr_dnattrs ? STRLENOF( ":dn" ) : 0 ) + |
| 610 |
( !BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_len + 1 : 0 ) + |
| 611 |
vtmp.bv_len + ( STRLENOF( "(:=)" ) ); |
| 612 |
fstr->bv_val = ber_memalloc_x( fstr->bv_len + 1, memctx ); |
| 613 |
if ( !fstr->bv_val ) { |
| 614 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 615 |
return LDAP_NO_MEMORY; |
| 616 |
} |
| 617 |
|
| 618 |
snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s%s%s%s:=%s)", |
| 619 |
atmp.bv_val, |
| 620 |
f->f_mr_dnattrs ? ":dn" : "", |
| 621 |
!BER_BVISEMPTY( &f->f_mr_rule_text ) ? ":" : "", |
| 622 |
!BER_BVISEMPTY( &f->f_mr_rule_text ) ? f->f_mr_rule_text.bv_val : "", |
| 623 |
vtmp.bv_len ? vtmp.bv_val : "" ); |
| 624 |
ber_memfree_x( vtmp.bv_val, memctx ); |
| 625 |
break; |
| 626 |
|
| 627 |
case SLAPD_FILTER_COMPUTED: |
| 628 |
switch ( f->f_result ) { |
| 629 |
/* FIXME: treat UNDEFINED as FALSE */ |
| 630 |
case SLAPD_COMPARE_UNDEFINED: |
| 631 |
computed:; |
| 632 |
if ( META_BACK_TGT_NOUNDEFFILTER( dc->target ) ) { |
| 633 |
return LDAP_COMPARE_FALSE; |
| 634 |
} |
| 635 |
/* fallthru */ |
| 636 |
|
| 637 |
case LDAP_COMPARE_FALSE: |
| 638 |
if ( META_BACK_TGT_T_F( dc->target ) ) { |
| 639 |
tmp = &ber_bvtf_false; |
| 640 |
break; |
| 641 |
} |
| 642 |
tmp = &ber_bvfalse; |
| 643 |
break; |
| 644 |
|
| 645 |
case LDAP_COMPARE_TRUE: |
| 646 |
if ( META_BACK_TGT_T_F( dc->target ) ) { |
| 647 |
tmp = &ber_bvtf_true; |
| 648 |
break; |
| 649 |
} |
| 650 |
|
| 651 |
tmp = &ber_bvtrue; |
| 652 |
break; |
| 653 |
|
| 654 |
default: |
| 655 |
tmp = &ber_bverror; |
| 656 |
break; |
| 657 |
} |
| 658 |
|
| 659 |
ber_dupbv_x( fstr, tmp, memctx ); |
| 660 |
break; |
| 661 |
|
| 662 |
default: |
| 663 |
ber_dupbv_x( fstr, &ber_bvunknown, memctx ); |
| 664 |
break; |
| 665 |
} |
| 666 |
|
| 667 |
return 0; |
| 668 |
} |
| 669 |
|
| 670 |
int |
| 671 |
ldap_back_filter_map_rewrite( |
| 672 |
dncookie *dc, |
| 673 |
Filter *f, |
| 674 |
struct berval *fstr, |
| 675 |
int remap, |
| 676 |
void *memctx ) |
| 677 |
{ |
| 678 |
int rc; |
| 679 |
dncookie fdc; |
| 680 |
struct berval ftmp; |
| 681 |
static char *dmy = ""; |
| 682 |
|
| 683 |
rc = ldap_back_int_filter_map_rewrite( dc, f, fstr, remap, memctx ); |
| 684 |
|
| 685 |
if ( rc != LDAP_SUCCESS ) { |
| 686 |
return rc; |
| 687 |
} |
| 688 |
|
| 689 |
fdc = *dc; |
| 690 |
ftmp = *fstr; |
| 691 |
|
| 692 |
fdc.ctx = "searchFilter"; |
| 693 |
|
| 694 |
switch ( rewrite_session( fdc.target->mt_rwmap.rwm_rw, fdc.ctx, |
| 695 |
( !BER_BVISEMPTY( &ftmp ) ? ftmp.bv_val : dmy ), |
| 696 |
fdc.conn, &fstr->bv_val ) ) |
| 697 |
{ |
| 698 |
case REWRITE_REGEXEC_OK: |
| 699 |
if ( !BER_BVISNULL( fstr ) ) { |
| 700 |
fstr->bv_len = strlen( fstr->bv_val ); |
| 701 |
|
| 702 |
} else { |
| 703 |
*fstr = ftmp; |
| 704 |
} |
| 705 |
Debug( LDAP_DEBUG_ARGS, |
| 706 |
"[rw] %s: \"%s\" -> \"%s\"\n", |
| 707 |
fdc.ctx, BER_BVISNULL( &ftmp ) ? "" : ftmp.bv_val, |
| 708 |
BER_BVISNULL( fstr ) ? "" : fstr->bv_val ); |
| 709 |
rc = LDAP_SUCCESS; |
| 710 |
break; |
| 711 |
|
| 712 |
case REWRITE_REGEXEC_UNWILLING: |
| 713 |
if ( fdc.rs ) { |
| 714 |
fdc.rs->sr_err = LDAP_UNWILLING_TO_PERFORM; |
| 715 |
fdc.rs->sr_text = "Operation not allowed"; |
| 716 |
} |
| 717 |
rc = LDAP_UNWILLING_TO_PERFORM; |
| 718 |
break; |
| 719 |
|
| 720 |
case REWRITE_REGEXEC_ERR: |
| 721 |
if ( fdc.rs ) { |
| 722 |
fdc.rs->sr_err = LDAP_OTHER; |
| 723 |
fdc.rs->sr_text = "Rewrite error"; |
| 724 |
} |
| 725 |
rc = LDAP_OTHER; |
| 726 |
break; |
| 727 |
} |
| 728 |
|
| 729 |
if ( fstr->bv_val == dmy ) { |
| 730 |
BER_BVZERO( fstr ); |
| 731 |
|
| 732 |
} else if ( fstr->bv_val != ftmp.bv_val ) { |
| 733 |
/* NOTE: need to realloc mapped filter on slab |
| 734 |
* and free the original one, until librewrite |
| 735 |
* becomes slab-aware |
| 736 |
*/ |
| 737 |
ber_dupbv_x( &ftmp, fstr, memctx ); |
| 738 |
ch_free( fstr->bv_val ); |
| 739 |
*fstr = ftmp; |
| 740 |
} |
| 741 |
|
| 742 |
return rc; |
| 743 |
} |
| 744 |
|
| 745 |
int |
| 746 |
ldap_back_referral_result_rewrite( |
| 747 |
dncookie *dc, |
| 748 |
BerVarray a_vals, |
| 749 |
void *memctx |
| 750 |
) |
| 751 |
{ |
| 752 |
int i, last; |
| 753 |
|
| 754 |
assert( dc != NULL ); |
| 755 |
assert( a_vals != NULL ); |
| 756 |
|
| 757 |
for ( last = 0; !BER_BVISNULL( &a_vals[ last ] ); last++ ) |
| 758 |
; |
| 759 |
last--; |
| 760 |
|
| 761 |
for ( i = 0; !BER_BVISNULL( &a_vals[ i ] ); i++ ) { |
| 762 |
struct berval dn, |
| 763 |
olddn = BER_BVNULL; |
| 764 |
int rc; |
| 765 |
LDAPURLDesc *ludp; |
| 766 |
|
| 767 |
rc = ldap_url_parse( a_vals[ i ].bv_val, &ludp ); |
| 768 |
if ( rc != LDAP_URL_SUCCESS ) { |
| 769 |
/* leave attr untouched if massage failed */ |
| 770 |
continue; |
| 771 |
} |
| 772 |
|
| 773 |
/* FIXME: URLs like "ldap:///dc=suffix" if passed |
| 774 |
* thru ldap_url_parse() and ldap_url_desc2str() |
| 775 |
* get rewritten as "ldap:///dc=suffix??base"; |
| 776 |
* we don't want this to occur... */ |
| 777 |
if ( ludp->lud_scope == LDAP_SCOPE_BASE ) { |
| 778 |
ludp->lud_scope = LDAP_SCOPE_DEFAULT; |
| 779 |
} |
| 780 |
|
| 781 |
ber_str2bv( ludp->lud_dn, 0, 0, &olddn ); |
| 782 |
|
| 783 |
rc = ldap_back_dn_massage( dc, &olddn, &dn ); |
| 784 |
switch ( rc ) { |
| 785 |
case LDAP_UNWILLING_TO_PERFORM: |
| 786 |
/* |
| 787 |
* FIXME: need to check if it may be considered |
| 788 |
* legal to trim values when adding/modifying; |
| 789 |
* it should be when searching (e.g. ACLs). |
| 790 |
*/ |
| 791 |
ber_memfree( a_vals[ i ].bv_val ); |
| 792 |
if ( last > i ) { |
| 793 |
a_vals[ i ] = a_vals[ last ]; |
| 794 |
} |
| 795 |
BER_BVZERO( &a_vals[ last ] ); |
| 796 |
last--; |
| 797 |
i--; |
| 798 |
break; |
| 799 |
|
| 800 |
default: |
| 801 |
/* leave attr untouched if massage failed */ |
| 802 |
if ( !BER_BVISNULL( &dn ) && olddn.bv_val != dn.bv_val ) |
| 803 |
{ |
| 804 |
char *newurl; |
| 805 |
|
| 806 |
ludp->lud_dn = dn.bv_val; |
| 807 |
newurl = ldap_url_desc2str( ludp ); |
| 808 |
free( dn.bv_val ); |
| 809 |
if ( newurl == NULL ) { |
| 810 |
/* FIXME: leave attr untouched |
| 811 |
* even if ldap_url_desc2str failed... |
| 812 |
*/ |
| 813 |
break; |
| 814 |
} |
| 815 |
|
| 816 |
ber_memfree_x( a_vals[ i ].bv_val, memctx ); |
| 817 |
ber_str2bv_x( newurl, 0, 1, &a_vals[ i ], memctx ); |
| 818 |
ber_memfree( newurl ); |
| 819 |
ludp->lud_dn = olddn.bv_val; |
| 820 |
} |
| 821 |
break; |
| 822 |
} |
| 823 |
|
| 824 |
ldap_free_urldesc( ludp ); |
| 825 |
} |
| 826 |
|
| 827 |
return 0; |
| 828 |
} |
| 829 |
|
| 830 |
/* |
| 831 |
* I don't like this much, but we need two different |
| 832 |
* functions because different heap managers may be |
| 833 |
* in use in back-ldap/meta to reduce the amount of |
| 834 |
* calls to malloc routines, and some of the free() |
| 835 |
* routines may be macros with args |
| 836 |
*/ |
| 837 |
int |
| 838 |
ldap_dnattr_rewrite( |
| 839 |
dncookie *dc, |
| 840 |
BerVarray a_vals |
| 841 |
) |
| 842 |
{ |
| 843 |
struct berval bv; |
| 844 |
int i, last; |
| 845 |
|
| 846 |
assert( a_vals != NULL ); |
| 847 |
|
| 848 |
for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ ) |
| 849 |
; |
| 850 |
last--; |
| 851 |
|
| 852 |
for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) { |
| 853 |
switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) { |
| 854 |
case LDAP_UNWILLING_TO_PERFORM: |
| 855 |
/* |
| 856 |
* FIXME: need to check if it may be considered |
| 857 |
* legal to trim values when adding/modifying; |
| 858 |
* it should be when searching (e.g. ACLs). |
| 859 |
*/ |
| 860 |
ch_free( a_vals[i].bv_val ); |
| 861 |
if ( last > i ) { |
| 862 |
a_vals[i] = a_vals[last]; |
| 863 |
} |
| 864 |
BER_BVZERO( &a_vals[last] ); |
| 865 |
last--; |
| 866 |
break; |
| 867 |
|
| 868 |
default: |
| 869 |
/* leave attr untouched if massage failed */ |
| 870 |
if ( !BER_BVISNULL( &bv ) && bv.bv_val != a_vals[i].bv_val ) { |
| 871 |
ch_free( a_vals[i].bv_val ); |
| 872 |
a_vals[i] = bv; |
| 873 |
} |
| 874 |
break; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
return 0; |
| 879 |
} |
| 880 |
|
| 881 |
int |
| 882 |
ldap_dnattr_result_rewrite( |
| 883 |
dncookie *dc, |
| 884 |
BerVarray a_vals |
| 885 |
) |
| 886 |
{ |
| 887 |
struct berval bv; |
| 888 |
int i, last; |
| 889 |
|
| 890 |
assert( a_vals != NULL ); |
| 891 |
|
| 892 |
for ( last = 0; !BER_BVISNULL( &a_vals[last] ); last++ ) |
| 893 |
; |
| 894 |
last--; |
| 895 |
|
| 896 |
for ( i = 0; !BER_BVISNULL( &a_vals[i] ); i++ ) { |
| 897 |
switch ( ldap_back_dn_massage( dc, &a_vals[i], &bv ) ) { |
| 898 |
case LDAP_UNWILLING_TO_PERFORM: |
| 899 |
/* |
| 900 |
* FIXME: need to check if it may be considered |
| 901 |
* legal to trim values when adding/modifying; |
| 902 |
* it should be when searching (e.g. ACLs). |
| 903 |
*/ |
| 904 |
ber_memfree( a_vals[i].bv_val ); |
| 905 |
if ( last > i ) { |
| 906 |
a_vals[i] = a_vals[last]; |
| 907 |
} |
| 908 |
BER_BVZERO( &a_vals[last] ); |
| 909 |
last--; |
| 910 |
break; |
| 911 |
|
| 912 |
default: |
| 913 |
/* leave attr untouched if massage failed */ |
| 914 |
if ( !BER_BVISNULL( &bv ) && a_vals[i].bv_val != bv.bv_val ) { |
| 915 |
ber_memfree( a_vals[i].bv_val ); |
| 916 |
a_vals[i] = bv; |
| 917 |
} |
| 918 |
break; |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
return 0; |
| 923 |
} |
| 924 |
|