1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2005-2022 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
* This work was initially developed by Luke Howard for inclusion
* in OpenLDAP Software.
*/
#include "portable.h"
#include <ac/string.h>
#include <ac/stdarg.h>
#include <ac/ctype.h>
#include <ac/unistd.h>
#include <ldap_pvt.h>
#include <slap.h>
#include <slapi.h>
#ifdef LDAP_SLAPI
#define FLAG_DN 0x1
#define FLAG_NDN 0x2
void slapi_sdn_init( Slapi_DN *sdn )
{
sdn->flag = 0;
BER_BVZERO( &sdn->dn );
BER_BVZERO( &sdn->ndn );
}
Slapi_DN *slapi_sdn_new( void )
{
Slapi_DN *sdn;
sdn = (Slapi_DN *)slapi_ch_malloc( sizeof(*sdn ));
slapi_sdn_init( sdn );
return sdn;
}
void slapi_sdn_done( Slapi_DN *sdn )
{
if ( sdn == NULL )
return;
if ( sdn->flag & FLAG_DN ) {
slapi_ch_free_string( &sdn->dn.bv_val );
}
if ( sdn->flag & FLAG_NDN ) {
slapi_ch_free_string( &sdn->ndn.bv_val );
}
slapi_sdn_init( sdn );
}
void slapi_sdn_free( Slapi_DN **sdn )
{
slapi_sdn_done( *sdn );
slapi_ch_free( (void **)sdn );
}
const char *slapi_sdn_get_dn( const Slapi_DN *sdn )
{
if ( !BER_BVISNULL( &sdn->dn ) )
return sdn->dn.bv_val;
else
return sdn->ndn.bv_val;
}
const char *slapi_sdn_get_ndn( const Slapi_DN *sdn )
{
if ( BER_BVISNULL( &sdn->ndn ) ) {
dnNormalize( 0, NULL, NULL,
(struct berval *)&sdn->dn, (struct berval *)&sdn->ndn, NULL );
((Slapi_DN *)sdn)->flag |= FLAG_NDN;
}
return sdn->ndn.bv_val;
}
Slapi_DN *slapi_sdn_new_dn_byval( const char *dn )
{
Slapi_DN *sdn;
sdn = slapi_sdn_new();
return slapi_sdn_set_dn_byval( sdn, dn );
}
Slapi_DN *slapi_sdn_new_ndn_byval( const char *ndn )
{
Slapi_DN *sdn;
sdn = slapi_sdn_new();
return slapi_sdn_set_ndn_byval( sdn, ndn );
}
Slapi_DN *slapi_sdn_new_dn_byref( const char *dn )
{
Slapi_DN *sdn;
sdn = slapi_sdn_new();
return slapi_sdn_set_dn_byref( sdn, dn );
}
Slapi_DN *slapi_sdn_new_ndn_byref( const char *ndn )
{
Slapi_DN *sdn;
sdn = slapi_sdn_new();
return slapi_sdn_set_ndn_byref( sdn, ndn );
}
Slapi_DN *slapi_sdn_new_dn_passin( const char *dn )
{
Slapi_DN *sdn;
sdn = slapi_sdn_new();
return slapi_sdn_set_dn_passin( sdn, dn );
}
Slapi_DN *slapi_sdn_set_dn_byval( Slapi_DN *sdn, const char *dn )
{
if ( sdn == NULL ) {
return NULL;
}
slapi_sdn_done( sdn );
if ( dn != NULL ) {
sdn->dn.bv_val = slapi_ch_strdup( dn );
sdn->dn.bv_len = strlen( dn );
}
sdn->flag |= FLAG_DN;
return sdn;
}
Slapi_DN *slapi_sdn_set_dn_byref( Slapi_DN *sdn, const char *dn )
{
if ( sdn == NULL )
return NULL;
slapi_sdn_done( sdn );
if ( dn != NULL ) {
sdn->dn.bv_val = (char *)dn;
sdn->dn.bv_len = strlen( dn );
}
return sdn;
}
Slapi_DN *slapi_sdn_set_dn_passin( Slapi_DN *sdn, const char *dn )
{
if ( sdn == NULL )
return NULL;
slapi_sdn_set_dn_byref( sdn, dn );
sdn->flag |= FLAG_DN;
return sdn;
}
Slapi_DN *slapi_sdn_set_ndn_byval( Slapi_DN *sdn, const char *ndn )
{
if ( sdn == NULL ) {
return NULL;
}
slapi_sdn_done( sdn );
if ( ndn != NULL ) {
sdn->ndn.bv_val = slapi_ch_strdup( ndn );
sdn->ndn.bv_len = strlen( ndn );
}
sdn->flag |= FLAG_NDN;
return sdn;
}
Slapi_DN *slapi_sdn_set_ndn_byref( Slapi_DN *sdn, const char *ndn )
{
if ( sdn == NULL )
return NULL;
slapi_sdn_done( sdn );
if ( ndn != NULL ) {
sdn->ndn.bv_val = (char *)ndn;
sdn->ndn.bv_len = strlen( ndn );
}
return sdn;
}
Slapi_DN *slapi_sdn_set_ndn_passin( Slapi_DN *sdn, const char *ndn )
{
if ( sdn == NULL )
return NULL;
slapi_sdn_set_ndn_byref( sdn, ndn );
sdn->flag |= FLAG_NDN;
return sdn;
}
void slapi_sdn_get_parent( const Slapi_DN *sdn, Slapi_DN *sdn_parent )
{
struct berval parent_dn;
if ( !(sdn->flag & FLAG_DN) ) {
dnParent( (struct berval *)&sdn->ndn, &parent_dn );
slapi_sdn_set_ndn_byval( sdn_parent, parent_dn.bv_val );
} else {
dnParent( (struct berval *)&sdn->dn, &parent_dn );
slapi_sdn_set_dn_byval( sdn_parent, parent_dn.bv_val );
}
}
void slapi_sdn_get_backend_parent( const Slapi_DN *sdn,
Slapi_DN *sdn_parent,
const Slapi_Backend *backend )
{
slapi_sdn_get_ndn( sdn );
if ( backend == NULL ||
be_issuffix( (Slapi_Backend *)backend, (struct berval *)&sdn->ndn ) == 0 ) {
slapi_sdn_get_parent( sdn, sdn_parent );
}
}
Slapi_DN * slapi_sdn_dup( const Slapi_DN *sdn )
{
Slapi_DN *new_sdn;
new_sdn = slapi_sdn_new();
slapi_sdn_copy( sdn, new_sdn );
return new_sdn;
}
void slapi_sdn_copy( const Slapi_DN *from, Slapi_DN *to )
{
slapi_sdn_set_dn_byval( to, from->dn.bv_val );
}
int slapi_sdn_compare( const Slapi_DN *sdn1, const Slapi_DN *sdn2 )
{
int match = -1;
slapi_sdn_get_ndn( sdn1 );
slapi_sdn_get_ndn( sdn2 );
dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
(struct berval *)&sdn1->ndn, (void *)&sdn2->ndn );
return match;
}
int slapi_sdn_isempty( const Slapi_DN *sdn)
{
return ( BER_BVISEMPTY( &sdn->dn ) && BER_BVISEMPTY( &sdn->ndn ) );
}
int slapi_sdn_issuffix( const Slapi_DN *sdn, const Slapi_DN *suffix_sdn )
{
slapi_sdn_get_ndn( sdn );
slapi_sdn_get_ndn( suffix_sdn );
return dnIsSuffix( &sdn->ndn, &suffix_sdn->ndn );
}
int slapi_sdn_isparent( const Slapi_DN *parent, const Slapi_DN *child )
{
Slapi_DN child_parent;
slapi_sdn_get_ndn( child );
slapi_sdn_init( &child_parent );
dnParent( (struct berval *)&child->ndn, &child_parent.ndn );
return ( slapi_sdn_compare( parent, &child_parent ) == 0 );
}
int slapi_sdn_isgrandparent( const Slapi_DN *parent, const Slapi_DN *child )
{
Slapi_DN child_grandparent;
slapi_sdn_get_ndn( child );
slapi_sdn_init( &child_grandparent );
dnParent( (struct berval *)&child->ndn, &child_grandparent.ndn );
if ( child_grandparent.ndn.bv_len == 0 ) {
return 0;
}
dnParent( &child_grandparent.ndn, &child_grandparent.ndn );
return ( slapi_sdn_compare( parent, &child_grandparent ) == 0 );
}
int slapi_sdn_get_ndn_len( const Slapi_DN *sdn )
{
slapi_sdn_get_ndn( sdn );
return sdn->ndn.bv_len;
}
int slapi_sdn_scope_test( const Slapi_DN *dn, const Slapi_DN *base, int scope )
{
int rc;
switch ( scope ) {
case LDAP_SCOPE_BASE:
rc = ( slapi_sdn_compare( dn, base ) == 0 );
break;
case LDAP_SCOPE_ONELEVEL:
rc = slapi_sdn_isparent( base, dn );
break;
case LDAP_SCOPE_SUBTREE:
rc = slapi_sdn_issuffix( dn, base );
break;
default:
rc = 0;
break;
}
return rc;
}
void slapi_rdn_init( Slapi_RDN *rdn )
{
rdn->flag = 0;
BER_BVZERO( &rdn->bv );
rdn->rdn = NULL;
}
Slapi_RDN *slapi_rdn_new( void )
{
Slapi_RDN *rdn;
rdn = (Slapi_RDN *)slapi_ch_malloc( sizeof(*rdn ));
slapi_rdn_init( rdn );
return rdn;
}
Slapi_RDN *slapi_rdn_new_dn( const char *dn )
{
Slapi_RDN *rdn;
rdn = slapi_rdn_new();
slapi_rdn_init_dn( rdn, dn );
return rdn;
}
Slapi_RDN *slapi_rdn_new_sdn( const Slapi_DN *sdn )
{
return slapi_rdn_new_dn( slapi_sdn_get_dn( sdn ) );
}
Slapi_RDN *slapi_rdn_new_rdn( const Slapi_RDN *fromrdn )
{
return slapi_rdn_new_dn( fromrdn->bv.bv_val );
}
void slapi_rdn_init_dn( Slapi_RDN *rdn, const char *dn )
{
slapi_rdn_init( rdn );
slapi_rdn_set_dn( rdn, dn );
}
void slapi_rdn_init_sdn( Slapi_RDN *rdn, const Slapi_DN *sdn )
{
slapi_rdn_init( rdn );
slapi_rdn_set_sdn( rdn, sdn );
}
void slapi_rdn_init_rdn( Slapi_RDN *rdn, const Slapi_RDN *fromrdn )
{
slapi_rdn_init( rdn );
slapi_rdn_set_rdn( rdn, fromrdn );
}
void slapi_rdn_set_dn( Slapi_RDN *rdn, const char *dn )
{
struct berval bv;
slapi_rdn_done( rdn );
BER_BVZERO( &bv );
if ( dn != NULL ) {
bv.bv_val = (char *)dn;
bv.bv_len = strlen( dn );
}
dnExtractRdn( &bv, &rdn->bv, NULL );
rdn->flag |= FLAG_DN;
}
void slapi_rdn_set_sdn( Slapi_RDN *rdn, const Slapi_DN *sdn )
{
slapi_rdn_set_dn( rdn, slapi_sdn_get_dn( sdn ) );
}
void slapi_rdn_set_rdn( Slapi_RDN *rdn, const Slapi_RDN *fromrdn )
{
slapi_rdn_set_dn( rdn, fromrdn->bv.bv_val );
}
void slapi_rdn_free( Slapi_RDN **rdn )
{
slapi_rdn_done( *rdn );
slapi_ch_free( (void **)rdn );
}
void slapi_rdn_done( Slapi_RDN *rdn )
{
if ( rdn->rdn != NULL ) {
ldap_rdnfree( rdn->rdn );
rdn->rdn = NULL;
}
slapi_ch_free_string( &rdn->bv.bv_val );
slapi_rdn_init( rdn );
}
const char *slapi_rdn_get_rdn( const Slapi_RDN *rdn )
{
return rdn->bv.bv_val;
}
static int slapi_int_rdn_explode( Slapi_RDN *rdn )
{
char *next;
if ( rdn->rdn != NULL ) {
return LDAP_SUCCESS;
}
return ldap_bv2rdn( &rdn->bv, &rdn->rdn, &next, LDAP_DN_FORMAT_LDAP );
}
static int slapi_int_rdn_implode( Slapi_RDN *rdn )
{
struct berval bv;
int rc;
if ( rdn->rdn == NULL ) {
return LDAP_SUCCESS;
}
rc = ldap_rdn2bv( rdn->rdn, &bv, LDAP_DN_FORMAT_LDAPV3 | LDAP_DN_PRETTY );
if ( rc != LDAP_SUCCESS ) {
return rc;
}
slapi_ch_free_string( &rdn->bv.bv_val );
rdn->bv = bv;
return 0;
}
int slapi_rdn_get_num_components( Slapi_RDN *rdn )
{
int i;
if ( slapi_int_rdn_explode( rdn ) != LDAP_SUCCESS )
return 0;
for ( i = 0; rdn->rdn[i] != NULL; i++ )
;
return i;
}
int slapi_rdn_get_first( Slapi_RDN *rdn, char **type, char **value )
{
return slapi_rdn_get_next( rdn, 0, type, value );
}
int slapi_rdn_get_next( Slapi_RDN *rdn, int index, char **type, char **value )
{
slapi_int_rdn_explode( rdn );
if ( rdn->rdn == NULL || rdn->rdn[index] == NULL )
return -1;
*type = rdn->rdn[index]->la_attr.bv_val;
*value = rdn->rdn[index]->la_value.bv_val;
return index + 1;
}
int slapi_rdn_get_index( Slapi_RDN *rdn, const char *type, const char *value, size_t length )
{
int i, match;
struct berval bv;
AttributeDescription *ad = NULL;
const char *text;
slapi_int_rdn_explode( rdn );
if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
return -1;
}
bv.bv_val = (char *)value;
bv.bv_len = length;
for ( i = 0; rdn->rdn[i] != NULL; i++ ) {
if ( !slapi_attr_types_equivalent( ad->ad_cname.bv_val, type ))
continue;
if ( value_match( &match, ad, ad->ad_type->sat_equality, 0,
&rdn->rdn[i]->la_value, (void *)&bv, &text ) != LDAP_SUCCESS )
match = -1;
if ( match == 0 )
return i;
}
return -1;
}
int slapi_rdn_get_index_attr( Slapi_RDN *rdn, const char *type, char **value )
{
int i;
for ( i = 0; rdn->rdn[i] != NULL; i++ ) {
if ( slapi_attr_types_equivalent( rdn->rdn[i]->la_attr.bv_val, type ) ) {
*value = rdn->rdn[i]->la_value.bv_val;
return i;
}
}
return -1;
}
int slapi_rdn_contains( Slapi_RDN *rdn, const char *type, const char *value, size_t length )
{
return ( slapi_rdn_get_index( rdn, type, value, length ) != -1 );
}
int slapi_rdn_contains_attr( Slapi_RDN *rdn, const char *type, char **value )
{
return ( slapi_rdn_get_index_attr( rdn, type, value ) != -1 );
}
int slapi_rdn_compare( Slapi_RDN *rdn1, Slapi_RDN *rdn2 )
{
struct berval nrdn1 = BER_BVNULL;
struct berval nrdn2 = BER_BVNULL;
int match;
rdnNormalize( 0, NULL, NULL, (struct berval *)&rdn1->bv, &nrdn1, NULL );
rdnNormalize( 0, NULL, NULL, (struct berval *)&rdn2->bv, &nrdn2, NULL );
if ( rdnMatch( &match, 0, NULL, NULL, &nrdn1, (void *)&nrdn2 ) != LDAP_SUCCESS) {
match = -1;
}
return match;
}
int slapi_rdn_isempty( const Slapi_RDN *rdn )
{
return ( BER_BVISEMPTY( &rdn->bv ) );
}
int slapi_rdn_add( Slapi_RDN *rdn, const char *type, const char *value )
{
char *s;
size_t len;
len = strlen(type) + 1 + strlen( value );
if ( !BER_BVISEMPTY( &rdn->bv ) ) {
len += 1 + rdn->bv.bv_len;
}
s = slapi_ch_malloc( len + 1 );
if ( BER_BVISEMPTY( &rdn->bv ) ) {
snprintf( s, len + 1, "%s=%s", type, value );
} else {
snprintf( s, len + 1, "%s=%s+%s", type, value, rdn->bv.bv_val );
}
slapi_rdn_done( rdn );
rdn->bv.bv_len = len;
rdn->bv.bv_val = s;
return 1;
}
int slapi_rdn_remove_index( Slapi_RDN *rdn, int atindex )
{
int count, i;
count = slapi_rdn_get_num_components( rdn );
if ( atindex < 0 || atindex >= count )
return 0;
if ( rdn->rdn == NULL )
return 0;
slapi_ch_free_string( &rdn->rdn[atindex]->la_attr.bv_val );
slapi_ch_free_string( &rdn->rdn[atindex]->la_value.bv_val );
for ( i = atindex; i < count; i++ ) {
rdn->rdn[i] = rdn->rdn[i + 1];
}
if ( slapi_int_rdn_implode( rdn ) != LDAP_SUCCESS )
return 0;
return 1;
}
int slapi_rdn_remove( Slapi_RDN *rdn, const char *type, const char *value, size_t length )
{
int index = slapi_rdn_get_index( rdn, type, value, length );
return slapi_rdn_remove_index( rdn, index );
}
int slapi_rdn_remove_attr( Slapi_RDN *rdn, const char *type )
{
char *value;
int index = slapi_rdn_get_index_attr( rdn, type, &value );
return slapi_rdn_remove_index( rdn, index );
}
Slapi_DN *slapi_sdn_add_rdn( Slapi_DN *sdn, const Slapi_RDN *rdn )
{
struct berval bv;
build_new_dn( &bv, &sdn->dn, (struct berval *)&rdn->bv, NULL );
slapi_sdn_done( sdn );
sdn->dn = bv;
return sdn;
}
Slapi_DN *slapi_sdn_set_parent( Slapi_DN *sdn, const Slapi_DN *parentdn )
{
Slapi_RDN rdn;
slapi_rdn_init_sdn( &rdn, sdn );
slapi_sdn_set_dn_byref( sdn, slapi_sdn_get_dn( parentdn ) );
slapi_sdn_add_rdn( sdn, &rdn );
slapi_rdn_done( &rdn );
return sdn;
}
#endif /* LDAP_SLAPI */
|