Debian Codegraph @

servers/slapd/slapi/slapi_utils.c

raw
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 2002-2024 The OpenLDAP Foundation.
5 * Portions Copyright 1997,2002-2003 IBM Corporation.
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 IBM Corporation for use in
18 * IBM products and subsequently ported to OpenLDAP Software by
19 * Steve Omrani. Additional significant contributors include:
20 * Luke Howard
21 */
22
23 #include "portable.h"
24
25 #include <ac/string.h>
26 #include <ac/stdarg.h>
27 #include <ac/ctype.h>
28 #include <ac/unistd.h>
29 #include <lutil.h>
30
31 #include <slap.h>
32 #include <slapi.h>
33
34 #ifdef _WIN32
35 #include <winsock.h>
36 #else
37 #include <netdb.h>
38 #endif
39
40 #ifdef LDAP_SLAPI
41
42 /*
43 * server start time (should we use a struct timeval also in slapd?
44 */
45 static struct timeval base_time;
46 ldap_pvt_thread_mutex_t slapi_hn_mutex;
47 ldap_pvt_thread_mutex_t slapi_time_mutex;
48
49 struct slapi_mutex {
50 ldap_pvt_thread_mutex_t mutex;
51 };
52
53 struct slapi_condvar {
54 ldap_pvt_thread_cond_t cond;
55 ldap_pvt_thread_mutex_t mutex;
56 };
57
58 static int checkBVString(const struct berval *bv)
59 {
60 ber_len_t i;
61
62 for ( i = 0; i < bv->bv_len; i++ ) {
63 if ( bv->bv_val[i] == '\0' )
64 return 0;
65 }
66 if ( bv->bv_val[i] != '\0' )
67 return 0;
68
69 return 1;
70 }
71
72 /*
73 * This function converts an array of pointers to berval objects to
74 * an array of berval objects.
75 */
76
77 int
78 bvptr2obj(
79 struct berval **bvptr,
80 BerVarray *bvobj,
81 unsigned *num )
82 {
83 int rc = LDAP_SUCCESS;
84 int i;
85 BerVarray tmpberval;
86
87 if ( bvptr == NULL || *bvptr == NULL ) {
88 return LDAP_OTHER;
89 }
90
91 for ( i = 0; bvptr != NULL && bvptr[i] != NULL; i++ ) {
92 ; /* EMPTY */
93 }
94 if ( num )
95 *num = i;
96
97 tmpberval = (BerVarray)slapi_ch_malloc( (i + 1)*sizeof(struct berval));
98 if ( tmpberval == NULL ) {
99 return LDAP_NO_MEMORY;
100 }
101
102 for ( i = 0; bvptr[i] != NULL; i++ ) {
103 tmpberval[i].bv_val = bvptr[i]->bv_val;
104 tmpberval[i].bv_len = bvptr[i]->bv_len;
105 }
106 tmpberval[i].bv_val = NULL;
107 tmpberval[i].bv_len = 0;
108
109 if ( rc == LDAP_SUCCESS ) {
110 *bvobj = tmpberval;
111 }
112
113 return rc;
114 }
115
116 Slapi_Entry *
117 slapi_str2entry(
118 char *s,
119 int flags )
120 {
121 return str2entry( s );
122 }
123
124 char *
125 slapi_entry2str(
126 Slapi_Entry *e,
127 int *len )
128 {
129 char *ret = NULL;
130 char *s;
131
132 ldap_pvt_thread_mutex_lock( &entry2str_mutex );
133 s = entry2str( e, len );
134 if ( s != NULL )
135 ret = slapi_ch_strdup( s );
136 ldap_pvt_thread_mutex_unlock( &entry2str_mutex );
137
138 return ret;
139 }
140
141 char *
142 slapi_entry_get_dn( Slapi_Entry *e )
143 {
144 return e->e_name.bv_val;
145 }
146
147 int
148 slapi_x_entry_get_id( Slapi_Entry *e )
149 {
150 return e->e_id;
151 }
152
153 static int
154 slapi_int_dn_pretty( struct berval *in, struct berval *out )
155 {
156 Syntax *syntax = slap_schema.si_syn_distinguishedName;
157
158 assert( syntax != NULL );
159
160 return (syntax->ssyn_pretty)( syntax, in, out, NULL );
161 }
162
163 static int
164 slapi_int_dn_normalize( struct berval *in, struct berval *out )
165 {
166 MatchingRule *mr = slap_schema.si_mr_distinguishedNameMatch;
167 Syntax *syntax = slap_schema.si_syn_distinguishedName;
168
169 assert( mr != NULL );
170
171 return (mr->smr_normalize)( 0, syntax, mr, in, out, NULL );
172 }
173
174 void
175 slapi_entry_set_dn(
176 Slapi_Entry *e,
177 char *ldn )
178 {
179 struct berval dn = BER_BVNULL;
180
181 dn.bv_val = ldn;
182 dn.bv_len = strlen( ldn );
183
184 slapi_int_dn_pretty( &dn, &e->e_name );
185 slapi_int_dn_normalize( &dn, &e->e_nname );
186 }
187
188 Slapi_Entry *
189 slapi_entry_dup( Slapi_Entry *e )
190 {
191 return entry_dup( e );
192 }
193
194 int
195 slapi_entry_attr_delete(
196 Slapi_Entry *e,
197 char *type )
198 {
199 AttributeDescription *ad = NULL;
200 const char *text;
201
202 if ( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
203 return 1; /* LDAP_NO_SUCH_ATTRIBUTE */
204 }
205
206 if ( attr_delete( &e->e_attrs, ad ) == LDAP_SUCCESS ) {
207 return 0; /* attribute is deleted */
208 } else {
209 return -1; /* something went wrong */
210 }
211 }
212
213 Slapi_Entry *
214 slapi_entry_alloc( void )
215 {
216 return (Slapi_Entry *)entry_alloc();
217 }
218
219 void
220 slapi_entry_free( Slapi_Entry *e )
221 {
222 if ( e != NULL )
223 entry_free( e );
224 }
225
226 int
227 slapi_entry_attr_merge(
228 Slapi_Entry *e,
229 char *type,
230 struct berval **vals )
231 {
232 AttributeDescription *ad = NULL;
233 const char *text;
234 BerVarray bv;
235 int rc;
236
237 rc = slap_str2ad( type, &ad, &text );
238 if ( rc != LDAP_SUCCESS ) {
239 return -1;
240 }
241
242 rc = bvptr2obj( vals, &bv, NULL );
243 if ( rc != LDAP_SUCCESS ) {
244 return -1;
245 }
246
247 rc = attr_merge_normalize( e, ad, bv, NULL );
248 ch_free( bv );
249
250 return rc;
251 }
252
253 int
254 slapi_entry_attr_find(
255 Slapi_Entry *e,
256 char *type,
257 Slapi_Attr **attr )
258 {
259 AttributeDescription *ad = NULL;
260 const char *text;
261 int rc;
262
263 rc = slap_str2ad( type, &ad, &text );
264 if ( rc != LDAP_SUCCESS ) {
265 return -1;
266 }
267
268 *attr = attr_find( e->e_attrs, ad );
269 if ( *attr == NULL ) {
270 return -1;
271 }
272
273 return 0;
274 }
275
276 char *
277 slapi_entry_attr_get_charptr( const Slapi_Entry *e, const char *type )
278 {
279 AttributeDescription *ad = NULL;
280 const char *text;
281 int rc;
282 Attribute *attr;
283
284 rc = slap_str2ad( type, &ad, &text );
285 if ( rc != LDAP_SUCCESS ) {
286 return NULL;
287 }
288
289 attr = attr_find( e->e_attrs, ad );
290 if ( attr == NULL ) {
291 return NULL;
292 }
293
294 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
295 const char *p;
296
297 p = slapi_value_get_string( &attr->a_vals[0] );
298 if ( p != NULL ) {
299 return slapi_ch_strdup( p );
300 }
301 }
302
303 return NULL;
304 }
305
306 int
307 slapi_entry_attr_get_int( const Slapi_Entry *e, const char *type )
308 {
309 AttributeDescription *ad = NULL;
310 const char *text;
311 int rc;
312 Attribute *attr;
313
314 rc = slap_str2ad( type, &ad, &text );
315 if ( rc != LDAP_SUCCESS ) {
316 return 0;
317 }
318
319 attr = attr_find( e->e_attrs, ad );
320 if ( attr == NULL ) {
321 return 0;
322 }
323
324 return slapi_value_get_int( attr->a_vals );
325 }
326
327 long
328 slapi_entry_attr_get_long( const Slapi_Entry *e, const char *type )
329 {
330 AttributeDescription *ad = NULL;
331 const char *text;
332 int rc;
333 Attribute *attr;
334
335 rc = slap_str2ad( type, &ad, &text );
336 if ( rc != LDAP_SUCCESS ) {
337 return 0;
338 }
339
340 attr = attr_find( e->e_attrs, ad );
341 if ( attr == NULL ) {
342 return 0;
343 }
344
345 return slapi_value_get_long( attr->a_vals );
346 }
347
348 unsigned int
349 slapi_entry_attr_get_uint( const Slapi_Entry *e, const char *type )
350 {
351 AttributeDescription *ad = NULL;
352 const char *text;
353 int rc;
354 Attribute *attr;
355
356 rc = slap_str2ad( type, &ad, &text );
357 if ( rc != LDAP_SUCCESS ) {
358 return 0;
359 }
360
361 attr = attr_find( e->e_attrs, ad );
362 if ( attr == NULL ) {
363 return 0;
364 }
365
366 return slapi_value_get_uint( attr->a_vals );
367 }
368
369 unsigned long
370 slapi_entry_attr_get_ulong( const Slapi_Entry *e, const char *type )
371 {
372 AttributeDescription *ad = NULL;
373 const char *text;
374 int rc;
375 Attribute *attr;
376
377 rc = slap_str2ad( type, &ad, &text );
378 if ( rc != LDAP_SUCCESS ) {
379 return 0;
380 }
381
382 attr = attr_find( e->e_attrs, ad );
383 if ( attr == NULL ) {
384 return 0;
385 }
386
387 return slapi_value_get_ulong( attr->a_vals );
388 }
389
390 int
391 slapi_entry_attr_hasvalue( Slapi_Entry *e, const char *type, const char *value )
392 {
393 struct berval bv;
394 AttributeDescription *ad = NULL;
395 const char *text;
396 int rc;
397 Attribute *attr;
398
399 rc = slap_str2ad( type, &ad, &text );
400 if ( rc != LDAP_SUCCESS ) {
401 return 0;
402 }
403
404 attr = attr_find( e->e_attrs, ad );
405 if ( attr == NULL ) {
406 return 0;
407 }
408
409 bv.bv_val = (char *)value;
410 bv.bv_len = strlen( value );
411
412 return ( slapi_attr_value_find( attr, &bv ) != -1 );
413 }
414
415 void
416 slapi_entry_attr_set_charptr(Slapi_Entry* e, const char *type, const char *value)
417 {
418 AttributeDescription *ad = NULL;
419 const char *text;
420 int rc;
421 struct berval bv;
422
423 rc = slap_str2ad( type, &ad, &text );
424 if ( rc != LDAP_SUCCESS ) {
425 return;
426 }
427
428 attr_delete ( &e->e_attrs, ad );
429 if ( value != NULL ) {
430 bv.bv_val = (char *)value;
431 bv.bv_len = strlen(value);
432 attr_merge_normalize_one( e, ad, &bv, NULL );
433 }
434 }
435
436 void
437 slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
438 {
439 char buf[64];
440
441 snprintf( buf, sizeof( buf ), "%d", l );
442 slapi_entry_attr_set_charptr( e, type, buf );
443 }
444
445 void
446 slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
447 {
448 char buf[64];
449
450 snprintf( buf, sizeof( buf ), "%u", l );
451 slapi_entry_attr_set_charptr( e, type, buf );
452 }
453
454 void
455 slapi_entry_attr_set_long(Slapi_Entry* e, const char *type, long l)
456 {
457 char buf[64];
458
459 snprintf( buf, sizeof( buf ), "%ld", l );
460 slapi_entry_attr_set_charptr( e, type, buf );
461 }
462
463 void
464 slapi_entry_attr_set_ulong(Slapi_Entry* e, const char *type, unsigned long l)
465 {
466 char buf[64];
467
468 snprintf( buf, sizeof( buf ), "%lu", l );
469 slapi_entry_attr_set_charptr( e, type, buf );
470 }
471
472 int
473 slapi_is_rootdse( const char *dn )
474 {
475 return ( dn == NULL || dn[0] == '\0' );
476 }
477
478 int
479 slapi_entry_has_children( const Slapi_Entry *e )
480 {
481 Slapi_PBlock *pb;
482 Backend *be = select_backend( (struct berval *)&e->e_nname, 0 );
483 int rc, hasSubordinates = 0;
484
485 if ( be == NULL || be->be_has_subordinates == 0 ) {
486 return 0;
487 }
488
489 pb = slapi_pblock_new();
490 if ( pb == NULL ) {
491 return 0;
492 }
493 slapi_int_connection_init_pb( pb, LDAP_REQ_SEARCH );
494
495 rc = slapi_pblock_set( pb, SLAPI_TARGET_DN, slapi_entry_get_dn(
496 (Entry *) e ));
497 if ( rc == LDAP_SUCCESS ) {
498 pb->pb_op->o_bd = be;
499 rc = be->be_has_subordinates( pb->pb_op, (Entry *) e,
500 &hasSubordinates );
501 }
502
503 slapi_pblock_destroy( pb );
504
505 return ( rc == LDAP_SUCCESS && hasSubordinates == LDAP_COMPARE_TRUE );
506 }
507
508 /*
509 * Return approximate size of the entry rounded to the nearest
510 * 1K. Only the size of the attribute values are counted in the
511 * Sun implementation.
512 *
513 * http://docs.sun.com/source/816-6701-10/funcref.html#1017388
514 */
515 size_t slapi_entry_size(Slapi_Entry *e)
516 {
517 size_t size;
518 Attribute *a;
519 int i;
520
521 for ( size = 0, a = e->e_attrs; a != NULL; a = a->a_next ) {
522 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
523 size += a->a_vals[i].bv_len + 1;
524 }
525 }
526
527 size += 1023;
528 size -= (size % 1024);
529
530 return size;
531 }
532
533 /*
534 * Add values to entry.
535 *
536 * Returns:
537 * LDAP_SUCCESS Values added to entry
538 * LDAP_TYPE_OR_VALUE_EXISTS One or more values exist in entry already
539 * LDAP_CONSTRAINT_VIOLATION Any other error (odd, but it's the spec)
540 */
541 int
542 slapi_entry_add_values( Slapi_Entry *e, const char *type, struct berval **vals )
543 {
544 Modification mod;
545 const char *text;
546 int rc;
547 char textbuf[SLAP_TEXT_BUFLEN];
548
549 mod.sm_op = LDAP_MOD_ADD;
550 mod.sm_flags = 0;
551 mod.sm_desc = NULL;
552 mod.sm_type.bv_val = (char *)type;
553 mod.sm_type.bv_len = strlen( type );
554
555 rc = slap_str2ad( type, &mod.sm_desc, &text );
556 if ( rc != LDAP_SUCCESS ) {
557 return rc;
558 }
559
560 if ( vals == NULL ) {
561 /* Apparently vals can be NULL
562 * FIXME: sm_values = NULL ? */
563 mod.sm_values = (BerVarray)ch_malloc( sizeof(struct berval) );
564 mod.sm_values->bv_val = NULL;
565 mod.sm_numvals = 0;
566
567 } else {
568 rc = bvptr2obj( vals, &mod.sm_values, &mod.sm_numvals );
569 if ( rc != LDAP_SUCCESS ) {
570 return LDAP_CONSTRAINT_VIOLATION;
571 }
572 }
573 mod.sm_nvalues = NULL;
574
575 rc = modify_add_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
576
577 slapi_ch_free( (void **)&mod.sm_values );
578
579 return (rc == LDAP_SUCCESS) ? LDAP_SUCCESS : LDAP_CONSTRAINT_VIOLATION;
580 }
581
582 int
583 slapi_entry_add_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
584 {
585 return slapi_entry_add_values( e, type, vals );
586 }
587
588 int
589 slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
590 {
591 AttributeDescription *ad = NULL;
592 const char *text;
593 int rc;
594
595 rc = slap_str2ad( type, &ad, &text );
596 if ( rc != LDAP_SUCCESS ) {
597 return -1;
598 }
599
600 return attr_merge_normalize( e, ad, *vs, NULL );
601 }
602
603 int
604 slapi_entry_delete_values( Slapi_Entry *e, const char *type, struct berval **vals )
605 {
606 Modification mod;
607 const char *text;
608 int rc;
609 char textbuf[SLAP_TEXT_BUFLEN];
610
611 mod.sm_op = LDAP_MOD_DELETE;
612 mod.sm_flags = 0;
613 mod.sm_desc = NULL;
614 mod.sm_type.bv_val = (char *)type;
615 mod.sm_type.bv_len = strlen( type );
616
617 if ( vals == NULL ) {
618 /* If vals is NULL, this is a NOOP. */
619 return LDAP_SUCCESS;
620 }
621
622 rc = slap_str2ad( type, &mod.sm_desc, &text );
623 if ( rc != LDAP_SUCCESS ) {
624 return rc;
625 }
626
627 if ( vals[0] == NULL ) {
628 /* SLAPI doco says LDApb_opERATIONS_ERROR but LDAP_OTHER is better */
629 return attr_delete( &e->e_attrs, mod.sm_desc ) ? LDAP_OTHER : LDAP_SUCCESS;
630 }
631
632 rc = bvptr2obj( vals, &mod.sm_values, &mod.sm_numvals );
633 if ( rc != LDAP_SUCCESS ) {
634 return LDAP_CONSTRAINT_VIOLATION;
635 }
636 mod.sm_nvalues = NULL;
637
638 rc = modify_delete_values( e, &mod, 0, &text, textbuf, sizeof(textbuf) );
639
640 slapi_ch_free( (void **)&mod.sm_values );
641
642 return rc;
643 }
644
645 int
646 slapi_entry_delete_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
647 {
648 return slapi_entry_delete_values( e, type, vals );
649 }
650
651 int
652 slapi_entry_merge_values_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
653 {
654 return slapi_entry_attr_merge( e, (char *)type, vals );
655 }
656
657 int
658 slapi_entry_add_value(Slapi_Entry *e, const char *type, const Slapi_Value *value)
659 {
660 AttributeDescription *ad = NULL;
661 int rc;
662 const char *text;
663
664 rc = slap_str2ad( type, &ad, &text );
665 if ( rc != LDAP_SUCCESS ) {
666 return -1;
667 }
668
669 rc = attr_merge_normalize_one( e, ad, (Slapi_Value *)value, NULL );
670 if ( rc != LDAP_SUCCESS ) {
671 return -1;
672 }
673
674 return 0;
675 }
676
677 int
678 slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
679 {
680 Slapi_Value val;
681
682 val.bv_val = (char *)value;
683 val.bv_len = strlen( value );
684
685 return slapi_entry_add_value( e, type, &val );
686 }
687
688 int
689 slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
690 {
691 Slapi_Value *vals[2];
692 Slapi_Value val;
693
694 val.bv_val = (char *)value;
695 val.bv_len = strlen( value );
696 vals[0] = &val;
697 vals[1] = NULL;
698
699 return slapi_entry_delete_values_sv( e, type, vals );
700 }
701
702 int
703 slapi_entry_attr_merge_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
704 {
705 return slapi_entry_attr_merge( e, (char *)type, vals );
706 }
707
708 int
709 slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **attr )
710 {
711 if ( e == NULL ) {
712 return -1;
713 }
714
715 *attr = e->e_attrs;
716
717 return ( *attr != NULL ) ? 0 : -1;
718 }
719
720 int
721 slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **attr )
722 {
723 if ( e == NULL ) {
724 return -1;
725 }
726
727 if ( prevattr == NULL ) {
728 return -1;
729 }
730
731 *attr = prevattr->a_next;
732
733 return ( *attr != NULL ) ? 0 : -1;
734 }
735
736 int
737 slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
738 {
739 AttributeDescription *ad = NULL;
740 const char *text;
741 int rc;
742 BerVarray bv;
743
744 rc = slap_str2ad( type, &ad, &text );
745 if ( rc != LDAP_SUCCESS ) {
746 return 0;
747 }
748
749 attr_delete( &e->e_attrs, ad );
750
751 rc = bvptr2obj( vals, &bv, NULL );
752 if ( rc != LDAP_SUCCESS ) {
753 return -1;
754 }
755
756 rc = attr_merge_normalize( e, ad, bv, NULL );
757 slapi_ch_free( (void **)&bv );
758 if ( rc != LDAP_SUCCESS ) {
759 return -1;
760 }
761
762 return 0;
763 }
764
765 /*
766 * FIXME -- The caller must free the allocated memory.
767 * In Netscape they do not have to.
768 */
769 int
770 slapi_attr_get_values(
771 Slapi_Attr *attr,
772 struct berval ***vals )
773 {
774 int i, j;
775 struct berval **bv;
776
777 if ( attr == NULL ) {
778 return 1;
779 }
780
781 for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
782 ; /* EMPTY */
783 }
784
785 bv = (struct berval **)ch_malloc( (i + 1) * sizeof(struct berval *) );
786 for ( j = 0; j < i; j++ ) {
787 bv[j] = ber_dupbv( NULL, &attr->a_vals[j] );
788 }
789 bv[j] = NULL;
790
791 *vals = (struct berval **)bv;
792
793 return 0;
794 }
795
796 char *
797 slapi_dn_normalize( char *dn )
798 {
799 struct berval bdn;
800 struct berval pdn;
801
802 assert( dn != NULL );
803
804 bdn.bv_val = dn;
805 bdn.bv_len = strlen( dn );
806
807 if ( slapi_int_dn_pretty( &bdn, &pdn ) != LDAP_SUCCESS ) {
808 return NULL;
809 }
810
811 return pdn.bv_val;
812 }
813
814 char *
815 slapi_dn_normalize_case( char *dn )
816 {
817 struct berval bdn;
818 struct berval ndn;
819
820 assert( dn != NULL );
821
822 bdn.bv_val = dn;
823 bdn.bv_len = strlen( dn );
824
825 if ( slapi_int_dn_normalize( &bdn, &ndn ) != LDAP_SUCCESS ) {
826 return NULL;
827 }
828
829 return ndn.bv_val;
830 }
831
832 int
833 slapi_dn_issuffix(
834 char *dn,
835 char *suffix )
836 {
837 struct berval bdn, ndn;
838 struct berval bsuffix, nsuffix;
839 int rc;
840
841 assert( dn != NULL );
842 assert( suffix != NULL );
843
844 bdn.bv_val = dn;
845 bdn.bv_len = strlen( dn );
846
847 bsuffix.bv_val = suffix;
848 bsuffix.bv_len = strlen( suffix );
849
850 if ( dnNormalize( 0, NULL, NULL, &bdn, &ndn, NULL ) != LDAP_SUCCESS ) {
851 return 0;
852 }
853
854 if ( dnNormalize( 0, NULL, NULL, &bsuffix, &nsuffix, NULL )
855 != LDAP_SUCCESS )
856 {
857 slapi_ch_free( (void **)&ndn.bv_val );
858 return 0;
859 }
860
861 rc = dnIsSuffix( &ndn, &nsuffix );
862
863 slapi_ch_free( (void **)&ndn.bv_val );
864 slapi_ch_free( (void **)&nsuffix.bv_val );
865
866 return rc;
867 }
868
869 int
870 slapi_dn_isparent(
871 const char *parentdn,
872 const char *childdn )
873 {
874 struct berval assertedParentDN, normalizedAssertedParentDN;
875 struct berval childDN, normalizedChildDN;
876 struct berval normalizedParentDN;
877 int match;
878
879 assert( parentdn != NULL );
880 assert( childdn != NULL );
881
882 assertedParentDN.bv_val = (char *)parentdn;
883 assertedParentDN.bv_len = strlen( parentdn );
884
885 if ( dnNormalize( 0, NULL, NULL, &assertedParentDN,
886 &normalizedAssertedParentDN, NULL ) != LDAP_SUCCESS )
887 {
888 return 0;
889 }
890
891 childDN.bv_val = (char *)childdn;
892 childDN.bv_len = strlen( childdn );
893
894 if ( dnNormalize( 0, NULL, NULL, &childDN,
895 &normalizedChildDN, NULL ) != LDAP_SUCCESS )
896 {
897 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
898 return 0;
899 }
900
901 dnParent( &normalizedChildDN, &normalizedParentDN );
902
903 if ( dnMatch( &match, 0, slap_schema.si_syn_distinguishedName, NULL,
904 &normalizedParentDN, (void *)&normalizedAssertedParentDN ) != LDAP_SUCCESS )
905 {
906 match = -1;
907 }
908
909 slapi_ch_free( (void **)&normalizedAssertedParentDN.bv_val );
910 slapi_ch_free( (void **)&normalizedChildDN.bv_val );
911
912 return ( match == 0 );
913 }
914
915 /*
916 * Returns DN of the parent entry, or NULL if the DN is
917 * an empty string or NULL, or has no parent.
918 */
919 char *
920 slapi_dn_parent( const char *_dn )
921 {
922 struct berval dn, prettyDN;
923 struct berval parentDN;
924 char *ret;
925
926 if ( _dn == NULL ) {
927 return NULL;
928 }
929
930 dn.bv_val = (char *)_dn;
931 dn.bv_len = strlen( _dn );
932
933 if ( dn.bv_len == 0 ) {
934 return NULL;
935 }
936
937 if ( dnPretty( NULL, &dn, &prettyDN, NULL ) != LDAP_SUCCESS ) {
938 return NULL;
939 }
940
941 dnParent( &prettyDN, &parentDN ); /* in-place */
942
943 if ( parentDN.bv_len == 0 ) {
944 slapi_ch_free_string( &prettyDN.bv_val );
945 return NULL;
946 }
947
948 ret = slapi_ch_strdup( parentDN.bv_val );
949 slapi_ch_free_string( &prettyDN.bv_val );
950
951 return ret;
952 }
953
954 int slapi_dn_isbesuffix( Slapi_PBlock *pb, char *ldn )
955 {
956 struct berval ndn;
957 Backend *be;
958
959 if ( slapi_is_rootdse( ldn ) ) {
960 return 0;
961 }
962
963 /* according to spec should already be normalized */
964 ndn.bv_len = strlen( ldn );
965 ndn.bv_val = ldn;
966
967 be = select_backend( &pb->pb_op->o_req_ndn, 0 );
968 if ( be == NULL ) {
969 return 0;
970 }
971
972 return be_issuffix( be, &ndn );
973 }
974
975 /*
976 * Returns DN of the parent entry; or NULL if the DN is
977 * an empty string, if the DN has no parent, or if the
978 * DN is the suffix of the backend database
979 */
980 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *ldn )
981 {
982 Backend *be;
983 struct berval dn, prettyDN;
984 struct berval normalizedDN, parentDN;
985 char *parent = NULL;
986
987 if ( pb == NULL ) {
988 return NULL;
989 }
990
991 PBLOCK_ASSERT_OP( pb, 0 );
992
993 if ( slapi_is_rootdse( ldn ) ) {
994 return NULL;
995 }
996
997 dn.bv_val = (char *)ldn;
998 dn.bv_len = strlen( ldn );
999
1000 if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
1001 return NULL;
1002 }
1003
1004 be = select_backend( &pb->pb_op->o_req_ndn, 0 );
1005
1006 if ( be == NULL || be_issuffix( be, &normalizedDN ) == 0 ) {
1007 dnParent( &prettyDN, &parentDN );
1008
1009 if ( parentDN.bv_len != 0 )
1010 parent = slapi_ch_strdup( parentDN.bv_val );
1011 }
1012
1013 slapi_ch_free_string( &prettyDN.bv_val );
1014 slapi_ch_free_string( &normalizedDN.bv_val );
1015
1016 return parent;
1017 }
1018
1019 char *
1020 slapi_dn_ignore_case( char *dn )
1021 {
1022 return slapi_dn_normalize_case( dn );
1023 }
1024
1025 char *
1026 slapi_ch_malloc( unsigned long size )
1027 {
1028 return ch_malloc( size );
1029 }
1030
1031 void
1032 slapi_ch_free( void **ptr )
1033 {
1034 if ( ptr == NULL || *ptr == NULL )
1035 return;
1036 ch_free( *ptr );
1037 *ptr = NULL;
1038 }
1039
1040 void
1041 slapi_ch_free_string( char **ptr )
1042 {
1043 slapi_ch_free( (void **)ptr );
1044 }
1045
1046 void
1047 slapi_ch_array_free( char **arrayp )
1048 {
1049 char **p;
1050
1051 if ( arrayp != NULL ) {
1052 for ( p = arrayp; *p != NULL; p++ ) {
1053 slapi_ch_free( (void **)p );
1054 }
1055 slapi_ch_free( (void **)&arrayp );
1056 }
1057 }
1058
1059 struct berval *
1060 slapi_ch_bvdup(const struct berval *v)
1061 {
1062 return ber_dupbv(NULL, (struct berval *)v);
1063 }
1064
1065 struct berval **
1066 slapi_ch_bvecdup(const struct berval **v)
1067 {
1068 int i;
1069 struct berval **rv;
1070
1071 if ( v == NULL ) {
1072 return NULL;
1073 }
1074
1075 for ( i = 0; v[i] != NULL; i++ )
1076 ;
1077
1078 rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1079
1080 for ( i = 0; v[i] != NULL; i++ ) {
1081 rv[i] = slapi_ch_bvdup( v[i] );
1082 }
1083 rv[i] = NULL;
1084
1085 return rv;
1086 }
1087
1088 char *
1089 slapi_ch_calloc(
1090 unsigned long nelem,
1091 unsigned long size )
1092 {
1093 return ch_calloc( nelem, size );
1094 }
1095
1096 char *
1097 slapi_ch_realloc(
1098 char *block,
1099 unsigned long size )
1100 {
1101 return ch_realloc( block, size );
1102 }
1103
1104 char *
1105 slapi_ch_strdup( const char *s )
1106 {
1107 return ch_strdup( s );
1108 }
1109
1110 size_t
1111 slapi_ch_stlen( const char *s )
1112 {
1113 return strlen( s );
1114 }
1115
1116 int
1117 slapi_control_present(
1118 LDAPControl **controls,
1119 char *oid,
1120 struct berval **val,
1121 int *iscritical )
1122 {
1123 int i;
1124 int rc = 0;
1125
1126 if ( val ) {
1127 *val = NULL;
1128 }
1129
1130 if ( iscritical ) {
1131 *iscritical = 0;
1132 }
1133
1134 for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1135 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1136 continue;
1137 }
1138
1139 rc = 1;
1140 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1141 if ( val ) {
1142 *val = &controls[i]->ldctl_value;
1143 }
1144 }
1145
1146 if ( iscritical ) {
1147 *iscritical = controls[i]->ldctl_iscritical;
1148 }
1149
1150 break;
1151 }
1152
1153 return rc;
1154 }
1155
1156 static void
1157 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1158 unsigned long *slapi_mask)
1159 {
1160 *slapi_mask = SLAPI_OPERATION_NONE;
1161
1162 if ( slap_mask & SLAP_CTRL_ABANDON )
1163 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1164
1165 if ( slap_mask & SLAP_CTRL_ADD )
1166 *slapi_mask |= SLAPI_OPERATION_ADD;
1167
1168 if ( slap_mask & SLAP_CTRL_BIND )
1169 *slapi_mask |= SLAPI_OPERATION_BIND;
1170
1171 if ( slap_mask & SLAP_CTRL_COMPARE )
1172 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1173
1174 if ( slap_mask & SLAP_CTRL_DELETE )
1175 *slapi_mask |= SLAPI_OPERATION_DELETE;
1176
1177 if ( slap_mask & SLAP_CTRL_MODIFY )
1178 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1179
1180 if ( slap_mask & SLAP_CTRL_RENAME )
1181 *slapi_mask |= SLAPI_OPERATION_MODDN;
1182
1183 if ( slap_mask & SLAP_CTRL_SEARCH )
1184 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1185
1186 if ( slap_mask & SLAP_CTRL_UNBIND )
1187 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1188 }
1189
1190 static void
1191 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1192 slap_mask_t *slap_mask)
1193 {
1194 *slap_mask = 0;
1195
1196 if ( slapi_mask & SLAPI_OPERATION_BIND )
1197 *slap_mask |= SLAP_CTRL_BIND;
1198
1199 if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1200 *slap_mask |= SLAP_CTRL_UNBIND;
1201
1202 if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1203 *slap_mask |= SLAP_CTRL_SEARCH;
1204
1205 if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1206 *slap_mask |= SLAP_CTRL_MODIFY;
1207
1208 if ( slapi_mask & SLAPI_OPERATION_ADD )
1209 *slap_mask |= SLAP_CTRL_ADD;
1210
1211 if ( slapi_mask & SLAPI_OPERATION_DELETE )
1212 *slap_mask |= SLAP_CTRL_DELETE;
1213
1214 if ( slapi_mask & SLAPI_OPERATION_MODDN )
1215 *slap_mask |= SLAP_CTRL_RENAME;
1216
1217 if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1218 *slap_mask |= SLAP_CTRL_COMPARE;
1219
1220 if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1221 *slap_mask |= SLAP_CTRL_ABANDON;
1222
1223 *slap_mask |= SLAP_CTRL_GLOBAL;
1224 }
1225
1226 static int
1227 slapi_int_parse_control(
1228 Operation *op,
1229 SlapReply *rs,
1230 LDAPControl *ctrl )
1231 {
1232 /* Plugins must deal with controls themselves. */
1233
1234 return LDAP_SUCCESS;
1235 }
1236
1237 void
1238 slapi_register_supported_control(
1239 char *controloid,
1240 unsigned long controlops )
1241 {
1242 slap_mask_t controlmask;
1243
1244 slapiControlOp2SlapControlMask( controlops, &controlmask );
1245
1246 register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1247 }
1248
1249 int
1250 slapi_get_supported_controls(
1251 char ***ctrloidsp,
1252 unsigned long **ctrlopsp )
1253 {
1254 int i, rc;
1255
1256 rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1257 if ( rc != LDAP_SUCCESS ) {
1258 return rc;
1259 }
1260
1261 for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1262 /* In place, naughty. */
1263 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1264 }
1265
1266 return LDAP_SUCCESS;
1267 }
1268
1269 LDAPControl *
1270 slapi_dup_control( LDAPControl *ctrl )
1271 {
1272 LDAPControl *ret;
1273
1274 ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1275 ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1276 ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1277 ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1278
1279 return ret;
1280 }
1281
1282 void
1283 slapi_register_supported_saslmechanism( char *mechanism )
1284 {
1285 /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1286 slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1287 "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1288 }
1289
1290 char **
1291 slapi_get_supported_saslmechanisms( void )
1292 {
1293 /* FIXME -- can not get the saslmechanism without a connection. */
1294 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1295 "can not get the SASL mechanism list "
1296 "without a connection\n" );
1297 return NULL;
1298 }
1299
1300 char **
1301 slapi_get_supported_extended_ops( void )
1302 {
1303 int i, j, k;
1304 char **ppExtOpOID = NULL;
1305 int numExtOps = 0;
1306
1307 for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1308 ;
1309 }
1310
1311 for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1312 ;
1313 }
1314
1315 numExtOps = i + j;
1316 if ( numExtOps == 0 ) {
1317 return NULL;
1318 }
1319
1320 ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1321 for ( k = 0; k < i; k++ ) {
1322 struct berval *bv;
1323
1324 bv = get_supported_extop( k );
1325 assert( bv != NULL );
1326
1327 ppExtOpOID[ k ] = bv->bv_val;
1328 }
1329
1330 for ( ; k < j; k++ ) {
1331 struct berval *bv;
1332
1333 bv = slapi_int_get_supported_extop( k );
1334 assert( bv != NULL );
1335
1336 ppExtOpOID[ i + k ] = bv->bv_val;
1337 }
1338 ppExtOpOID[ i + k ] = NULL;
1339
1340 return ppExtOpOID;
1341 }
1342
1343 void
1344 slapi_send_ldap_result(
1345 Slapi_PBlock *pb,
1346 int err,
1347 char *matched,
1348 char *text,
1349 int nentries,
1350 struct berval **urls )
1351 {
1352 SlapReply *rs;
1353
1354 PBLOCK_ASSERT_OP( pb, 0 );
1355
1356 rs = pb->pb_rs;
1357
1358 rs->sr_err = err;
1359 rs->sr_matched = matched;
1360 rs->sr_text = text;
1361 rs->sr_ref = NULL;
1362
1363 if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1364 send_ldap_sasl( pb->pb_op, rs );
1365 } else if ( rs->sr_rspoid != NULL ) {
1366 send_ldap_extended( pb->pb_op, rs );
1367 } else {
1368 if ( pb->pb_op->o_tag == LDAP_REQ_SEARCH )
1369 rs->sr_nentries = nentries;
1370 if ( urls != NULL )
1371 bvptr2obj( urls, &rs->sr_ref, NULL );
1372
1373 send_ldap_result( pb->pb_op, rs );
1374
1375 if ( urls != NULL )
1376 slapi_ch_free( (void **)&rs->sr_ref );
1377 }
1378 }
1379
1380 int
1381 slapi_send_ldap_search_entry(
1382 Slapi_PBlock *pb,
1383 Slapi_Entry *e,
1384 LDAPControl **ectrls,
1385 char **attrs,
1386 int attrsonly )
1387 {
1388 SlapReply rs = { REP_SEARCH };
1389 int i = 0, j = 0;
1390 AttributeName *an = NULL;
1391 const char *text;
1392 int rc;
1393
1394 assert( pb->pb_op != NULL );
1395
1396 if ( attrs != NULL ) {
1397 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1398 ; /* empty */
1399 }
1400 }
1401
1402 if ( i ) {
1403 an = (AttributeName *) slapi_ch_calloc( i + 1, sizeof(AttributeName) );
1404 for ( i = 0; attrs[i] != NULL; i++ ) {
1405 an[j].an_name.bv_val = attrs[i];
1406 an[j].an_name.bv_len = strlen( attrs[i] );
1407 an[j].an_desc = NULL;
1408 if ( slap_bv2ad( &an[j].an_name, &an[j].an_desc, &text ) == LDAP_SUCCESS) {
1409 j++;
1410 }
1411 }
1412 an[j].an_name.bv_len = 0;
1413 an[j].an_name.bv_val = NULL;
1414 }
1415
1416 rs.sr_err = LDAP_SUCCESS;
1417 rs.sr_matched = NULL;
1418 rs.sr_text = NULL;
1419 rs.sr_ref = NULL;
1420 rs.sr_ctrls = ectrls;
1421 rs.sr_attrs = an;
1422 rs.sr_operational_attrs = NULL;
1423 rs.sr_entry = e;
1424 rs.sr_v2ref = NULL;
1425 rs.sr_flags = 0;
1426
1427 rc = send_search_entry( pb->pb_op, &rs );
1428
1429 slapi_ch_free( (void **)&an );
1430
1431 return rc;
1432 }
1433
1434 int
1435 slapi_send_ldap_search_reference(
1436 Slapi_PBlock *pb,
1437 Slapi_Entry *e,
1438 struct berval **references,
1439 LDAPControl **ectrls,
1440 struct berval **v2refs
1441 )
1442 {
1443 SlapReply rs = { REP_SEARCHREF };
1444 int rc;
1445
1446 rs.sr_err = LDAP_SUCCESS;
1447 rs.sr_matched = NULL;
1448 rs.sr_text = NULL;
1449
1450 rc = bvptr2obj( references, &rs.sr_ref, NULL );
1451 if ( rc != LDAP_SUCCESS ) {
1452 return rc;
1453 }
1454
1455 rs.sr_ctrls = ectrls;
1456 rs.sr_attrs = NULL;
1457 rs.sr_operational_attrs = NULL;
1458 rs.sr_entry = e;
1459
1460 if ( v2refs != NULL ) {
1461 rc = bvptr2obj( v2refs, &rs.sr_v2ref, NULL );
1462 if ( rc != LDAP_SUCCESS ) {
1463 slapi_ch_free( (void **)&rs.sr_ref );
1464 return rc;
1465 }
1466 } else {
1467 rs.sr_v2ref = NULL;
1468 }
1469
1470 rc = send_search_reference( pb->pb_op, &rs );
1471
1472 slapi_ch_free( (void **)&rs.sr_ref );
1473 slapi_ch_free( (void **)&rs.sr_v2ref );
1474
1475 return rc;
1476 }
1477
1478 Slapi_Filter *
1479 slapi_str2filter( char *str )
1480 {
1481 return str2filter( str );
1482 }
1483
1484 void
1485 slapi_filter_free(
1486 Slapi_Filter *f,
1487 int recurse )
1488 {
1489 filter_free( f );
1490 }
1491
1492 Slapi_Filter *
1493 slapi_filter_dup( Slapi_Filter *filter )
1494 {
1495 return filter_dup( filter, NULL );
1496 }
1497
1498 int
1499 slapi_filter_get_choice( Slapi_Filter *f )
1500 {
1501 int rc;
1502
1503 if ( f != NULL ) {
1504 rc = f->f_choice;
1505 } else {
1506 rc = 0;
1507 }
1508
1509 return rc;
1510 }
1511
1512 int
1513 slapi_filter_get_ava(
1514 Slapi_Filter *f,
1515 char **type,
1516 struct berval **bval )
1517 {
1518 int ftype;
1519 int rc = LDAP_SUCCESS;
1520
1521 assert( type != NULL );
1522 assert( bval != NULL );
1523
1524 *type = NULL;
1525 *bval = NULL;
1526
1527 ftype = f->f_choice;
1528 if ( ftype == LDAP_FILTER_EQUALITY
1529 || ftype == LDAP_FILTER_GE
1530 || ftype == LDAP_FILTER_LE
1531 || ftype == LDAP_FILTER_APPROX ) {
1532 /*
1533 * According to the SLAPI Reference Manual these are
1534 * not duplicated.
1535 */
1536 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1537 *bval = &f->f_un.f_un_ava->aa_value;
1538 } else { /* filter type not supported */
1539 rc = -1;
1540 }
1541
1542 return rc;
1543 }
1544
1545 Slapi_Filter *
1546 slapi_filter_list_first( Slapi_Filter *f )
1547 {
1548 int ftype;
1549
1550 if ( f == NULL ) {
1551 return NULL;
1552 }
1553
1554 ftype = f->f_choice;
1555 if ( ftype == LDAP_FILTER_AND
1556 || ftype == LDAP_FILTER_OR
1557 || ftype == LDAP_FILTER_NOT ) {
1558 return (Slapi_Filter *)f->f_list;
1559 } else {
1560 return NULL;
1561 }
1562 }
1563
1564 Slapi_Filter *
1565 slapi_filter_list_next(
1566 Slapi_Filter *f,
1567 Slapi_Filter *fprev )
1568 {
1569 int ftype;
1570
1571 if ( f == NULL ) {
1572 return NULL;
1573 }
1574
1575 ftype = f->f_choice;
1576 if ( ftype == LDAP_FILTER_AND
1577 || ftype == LDAP_FILTER_OR
1578 || ftype == LDAP_FILTER_NOT )
1579 {
1580 return fprev->f_next;
1581 }
1582
1583 return NULL;
1584 }
1585
1586 int
1587 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1588 {
1589 if ( f == NULL ) {
1590 return -1;
1591 }
1592
1593 switch ( f->f_choice ) {
1594 case LDAP_FILTER_GE:
1595 case LDAP_FILTER_LE:
1596 case LDAP_FILTER_EQUALITY:
1597 case LDAP_FILTER_APPROX:
1598 *type = f->f_av_desc->ad_cname.bv_val;
1599 break;
1600 case LDAP_FILTER_SUBSTRINGS:
1601 *type = f->f_sub_desc->ad_cname.bv_val;
1602 break;
1603 case LDAP_FILTER_PRESENT:
1604 *type = f->f_desc->ad_cname.bv_val;
1605 break;
1606 case LDAP_FILTER_EXT:
1607 *type = f->f_mr_desc->ad_cname.bv_val;
1608 break;
1609 default:
1610 /* Complex filters need not apply. */
1611 *type = NULL;
1612 return -1;
1613 }
1614
1615 return 0;
1616 }
1617
1618 int
1619 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
1620 {
1621 AttributeDescription **adp, *ad = NULL;
1622 const char *text;
1623 int rc;
1624
1625 if ( f == NULL ) {
1626 return -1;
1627 }
1628
1629 switch ( f->f_choice ) {
1630 case LDAP_FILTER_GE:
1631 case LDAP_FILTER_LE:
1632 case LDAP_FILTER_EQUALITY:
1633 case LDAP_FILTER_APPROX:
1634 adp = &f->f_av_desc;
1635 break;
1636 case LDAP_FILTER_SUBSTRINGS:
1637 adp = &f->f_sub_desc;
1638 break;
1639 case LDAP_FILTER_PRESENT:
1640 adp = &f->f_desc;
1641 break;
1642 case LDAP_FILTER_EXT:
1643 adp = &f->f_mr_desc;
1644 break;
1645 default:
1646 /* Complex filters need not apply. */
1647 return -1;
1648 }
1649
1650 rc = slap_str2ad( type, &ad, &text );
1651 if ( rc == LDAP_SUCCESS )
1652 *adp = ad;
1653
1654 return ( rc == LDAP_SUCCESS ) ? 0 : -1;
1655 }
1656
1657 int
1658 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1659 char ***any, char **final )
1660 {
1661 int i;
1662
1663 if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1664 return -1;
1665 }
1666
1667 /*
1668 * The caller shouldn't free but we can't return an
1669 * array of char *s from an array of bervals without
1670 * allocating memory, so we may as well be consistent.
1671 * XXX
1672 */
1673 *type = f->f_sub_desc->ad_cname.bv_val;
1674 *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1675 if ( f->f_sub_any != NULL ) {
1676 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1677 ;
1678 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1679 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1680 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1681 }
1682 (*any)[i] = NULL;
1683 } else {
1684 *any = NULL;
1685 }
1686 *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1687
1688 return 0;
1689 }
1690
1691 Slapi_Filter *
1692 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1693 {
1694 Slapi_Filter *f = NULL;
1695
1696 if ( ftype == LDAP_FILTER_AND ||
1697 ftype == LDAP_FILTER_OR ||
1698 ftype == LDAP_FILTER_NOT )
1699 {
1700 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1701 f->f_choice = ftype;
1702 f->f_list = f1;
1703 f->f_list->f_next = f2;
1704 f->f_next = NULL;
1705 }
1706
1707 return f;
1708 }
1709
1710 int
1711 slapi_x_filter_append( int ftype,
1712 Slapi_Filter **pContainingFilter, /* NULL on first call */
1713 Slapi_Filter **pNextFilter,
1714 Slapi_Filter *filterToAppend )
1715 {
1716 if ( ftype == LDAP_FILTER_AND ||
1717 ftype == LDAP_FILTER_OR ||
1718 ftype == LDAP_FILTER_NOT )
1719 {
1720 if ( *pContainingFilter == NULL ) {
1721 *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1722 (*pContainingFilter)->f_choice = ftype;
1723 (*pContainingFilter)->f_list = filterToAppend;
1724 (*pContainingFilter)->f_next = NULL;
1725 } else {
1726 if ( (*pContainingFilter)->f_choice != ftype ) {
1727 /* Sanity check */
1728 return -1;
1729 }
1730 (*pNextFilter)->f_next = filterToAppend;
1731 }
1732 *pNextFilter = filterToAppend;
1733
1734 return 0;
1735 }
1736 return -1;
1737 }
1738
1739 int
1740 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1741 int verify_access )
1742 {
1743 Operation *op;
1744 int rc;
1745
1746 if ( f == NULL ) {
1747 /* spec says return zero if no filter. */
1748 return 0;
1749 }
1750
1751 if ( verify_access ) {
1752 op = pb->pb_op;
1753 if ( op == NULL )
1754 return LDAP_PARAM_ERROR;
1755 } else {
1756 op = NULL;
1757 }
1758
1759 /*
1760 * According to acl.c it is safe to call test_filter() with
1761 * NULL arguments...
1762 */
1763 rc = test_filter( op, e, f );
1764 switch (rc) {
1765 case LDAP_COMPARE_TRUE:
1766 rc = 0;
1767 break;
1768 case LDAP_COMPARE_FALSE:
1769 break;
1770 case SLAPD_COMPARE_UNDEFINED:
1771 rc = LDAP_OTHER;
1772 break;
1773 case LDAP_PROTOCOL_ERROR:
1774 /* filter type unknown: spec says return -1 */
1775 rc = -1;
1776 break;
1777 }
1778
1779 return rc;
1780 }
1781
1782 int
1783 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1784 {
1785 return slapi_filter_test( NULL, e, f, 0 );
1786 }
1787
1788 int
1789 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1790 {
1791 switch ( f->f_choice ) {
1792 case LDAP_FILTER_AND:
1793 case LDAP_FILTER_NOT:
1794 case LDAP_FILTER_OR: {
1795 int rc;
1796
1797 /*
1798 * FIXME: altering f; should we use a temporary?
1799 */
1800 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1801 rc = slapi_filter_apply( f, fn, arg, error_code );
1802 if ( rc != 0 ) {
1803 return rc;
1804 }
1805 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1806 break;
1807 }
1808 }
1809 break;
1810 }
1811 case LDAP_FILTER_EQUALITY:
1812 case LDAP_FILTER_SUBSTRINGS:
1813 case LDAP_FILTER_GE:
1814 case LDAP_FILTER_LE:
1815 case LDAP_FILTER_PRESENT:
1816 case LDAP_FILTER_APPROX:
1817 case LDAP_FILTER_EXT:
1818 *error_code = fn( f, arg );
1819 break;
1820 default:
1821 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1822 }
1823
1824 if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1825 *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1826 return 0;
1827 }
1828
1829 return -1;
1830 }
1831
1832 int
1833 slapi_pw_find(
1834 struct berval **vals,
1835 struct berval *v )
1836 {
1837 int i;
1838
1839 if( ( vals == NULL ) || ( v == NULL ) )
1840 return 1;
1841
1842 for ( i = 0; vals[i] != NULL; i++ ) {
1843 if ( !lutil_passwd( vals[i], v, NULL, NULL ) )
1844 return 0;
1845 }
1846
1847 return 1;
1848 }
1849
1850 /* Get connected client IP address.
1851 *
1852 * The user must free the returned client IP after its use.
1853 * Compatible with IBM Tivoli call.
1854 *
1855 * Errors:
1856 * * LDAP_PARAM_ERROR - If the pb parameter is null.
1857 * * LDAP_OPERATIONS_ERROR - If the API encounters error processing the request.
1858 * * LDAP_NO_MEMORY - Failed to allocate required memory.
1859 */
1860 int
1861 slapi_get_client_ip(Slapi_PBlock *pb, char **clientIP)
1862 {
1863 char *s = NULL;
1864
1865 if(pb == NULL || pb->pb_conn == NULL) return(LDAP_PARAM_ERROR);
1866 if((s = (char *) slapi_ch_malloc(pb->pb_conn->c_peer_name.bv_len + 1)) == NULL) {
1867 return(LDAP_NO_MEMORY);
1868 }
1869
1870 memcpy(s, pb->pb_conn->c_peer_name.bv_val, pb->pb_conn->c_peer_name.bv_len);
1871
1872 s[pb->pb_conn->c_peer_name.bv_len] = 0;
1873
1874 *clientIP = s;
1875
1876 return(LDAP_SUCCESS);
1877 }
1878
1879 /* Free previously allocated client IP address. */
1880 void
1881 slapi_free_client_ip(char **clientIP)
1882 {
1883 slapi_ch_free((void **) clientIP);
1884 }
1885
1886 #define MAX_HOSTNAME 512
1887
1888 char *
1889 slapi_get_hostname( void )
1890 {
1891 char *hn = NULL;
1892 static int been_here = 0;
1893 static char *static_hn = NULL;
1894
1895 ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1896 if ( !been_here ) {
1897 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1898 if ( static_hn == NULL) {
1899 slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
1900 "Cannot allocate memory for hostname\n" );
1901 static_hn = NULL;
1902 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1903
1904 return hn;
1905
1906 } else {
1907 if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1908 slapi_log_error( SLAPI_LOG_FATAL,
1909 "SLAPI",
1910 "can't get hostname\n" );
1911 slapi_ch_free( (void **)&static_hn );
1912 static_hn = NULL;
1913 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1914
1915 return hn;
1916
1917 } else {
1918 been_here = 1;
1919 }
1920 }
1921 }
1922 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1923
1924 hn = ch_strdup( static_hn );
1925
1926 return hn;
1927 }
1928
1929 /*
1930 * FIXME: this should go in an appropriate header ...
1931 */
1932 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
1933
1934 int
1935 slapi_log_error(
1936 int severity,
1937 char *subsystem,
1938 char *fmt,
1939 ... )
1940 {
1941 int rc = LDAP_SUCCESS;
1942 va_list arglist;
1943
1944 va_start( arglist, fmt );
1945 rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
1946 va_end( arglist );
1947
1948 return rc;
1949 }
1950
1951
1952 unsigned long
1953 slapi_timer_current_time( void )
1954 {
1955 static int first_time = 1;
1956 #if !defined (_WIN32)
1957 struct timeval now;
1958 unsigned long ret;
1959
1960 ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1961 if (first_time) {
1962 first_time = 0;
1963 gettimeofday( &base_time, NULL );
1964 }
1965 gettimeofday( &now, NULL );
1966 ret = ( now.tv_sec - base_time.tv_sec ) * 1000000 +
1967 (now.tv_usec - base_time.tv_usec);
1968 ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1969
1970 return ret;
1971
1972 /*
1973 * Ain't it better?
1974 return (slap_get_time() - starttime) * 1000000;
1975 */
1976 #else /* _WIN32 */
1977 LARGE_INTEGER now;
1978 static LARGE_INTEGER base_time, performance_freq;
1979 static int performance_counter_present;
1980
1981 if ( first_time ) {
1982 first_time = 0;
1983 performance_counter_present = QueryPerformanceCounter( &base_time );
1984 QueryPerformanceFrequency( &performance_freq );
1985 }
1986
1987 if ( !performance_counter_present )
1988 return 0;
1989
1990 QueryPerformanceCounter( &now );
1991 return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1992 #endif /* _WIN32 */
1993 }
1994
1995 /*
1996 * FIXME ?
1997 */
1998 unsigned long
1999 slapi_timer_get_time( char *label )
2000 {
2001 unsigned long start = slapi_timer_current_time();
2002 printf("%10ld %10d usec %s\n", start, 0, label);
2003 return start;
2004 }
2005
2006 /*
2007 * FIXME ?
2008 */
2009 void
2010 slapi_timer_elapsed_time(
2011 char *label,
2012 unsigned long start )
2013 {
2014 unsigned long stop = slapi_timer_current_time();
2015 printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2016 }
2017
2018 void
2019 slapi_free_search_results_internal( Slapi_PBlock *pb )
2020 {
2021 Slapi_Entry **entries;
2022 int k = 0, nEnt = 0;
2023
2024 slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2025 slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2026 if ( nEnt == 0 || entries == NULL ) {
2027 return;
2028 }
2029
2030 for ( k = 0; k < nEnt; k++ ) {
2031 slapi_entry_free( entries[k] );
2032 entries[k] = NULL;
2033 }
2034
2035 slapi_ch_free( (void **)&entries );
2036 }
2037
2038 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2039 {
2040 if ( pb == NULL )
2041 return LDAP_PARAM_ERROR;
2042
2043 if ( pb->pb_conn == NULL )
2044 return LDAP_PARAM_ERROR;
2045
2046 #ifdef HAVE_TLS
2047 *isSSL = pb->pb_conn->c_is_tls;
2048 #else
2049 *isSSL = 0;
2050 #endif
2051
2052 return LDAP_SUCCESS;
2053 }
2054
2055 /*
2056 * DS 5.x compatibility API follow
2057 */
2058
2059 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2060 {
2061 AttributeType *at;
2062
2063 if ( attr == NULL )
2064 return LDAP_PARAM_ERROR;
2065
2066 at = attr->a_desc->ad_type;
2067
2068 *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2069
2070 if ( is_at_single_value( at ) )
2071 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2072 if ( is_at_operational( at ) )
2073 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2074 if ( is_at_obsolete( at ) )
2075 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2076 if ( is_at_collective( at ) )
2077 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2078 if ( is_at_no_user_mod( at ) )
2079 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2080
2081 return LDAP_SUCCESS;
2082 }
2083
2084 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2085 {
2086 unsigned long flags;
2087
2088 if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2089 return 0;
2090 return (flags & flag) ? 1 : 0;
2091 }
2092
2093 Slapi_Attr *slapi_attr_new( void )
2094 {
2095 Attribute *ad;
2096
2097 ad = (Attribute *)slapi_ch_calloc( 1, sizeof(*ad) );
2098
2099 return ad;
2100 }
2101
2102 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2103 {
2104 const char *text;
2105 AttributeDescription *ad = NULL;
2106
2107 if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2108 return NULL;
2109 }
2110
2111 a->a_desc = ad;
2112 a->a_vals = NULL;
2113 a->a_nvals = NULL;
2114 a->a_next = NULL;
2115 a->a_flags = 0;
2116
2117 return a;
2118 }
2119
2120 void slapi_attr_free( Slapi_Attr **a )
2121 {
2122 attr_free( *a );
2123 *a = NULL;
2124 }
2125
2126 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2127 {
2128 return attr_dup( (Slapi_Attr *)attr );
2129 }
2130
2131 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2132 {
2133 struct berval nval;
2134 struct berval *nvalp;
2135 int rc;
2136 AttributeDescription *desc = a->a_desc;
2137
2138 if ( desc->ad_type->sat_equality &&
2139 desc->ad_type->sat_equality->smr_normalize ) {
2140 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2141 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2142 desc->ad_type->sat_syntax,
2143 desc->ad_type->sat_equality,
2144 (Slapi_Value *)v, &nval, NULL );
2145 if ( rc != LDAP_SUCCESS ) {
2146 return rc;
2147 }
2148 nvalp = &nval;
2149 } else {
2150 nvalp = NULL;
2151 }
2152
2153 rc = attr_valadd( a, (Slapi_Value *)v, nvalp, 1 );
2154
2155 if ( nvalp != NULL ) {
2156 slapi_ch_free_string( &nval.bv_val );
2157 }
2158
2159 return rc;
2160 }
2161
2162 int slapi_attr_type2plugin( const char *type, void **pi )
2163 {
2164 *pi = NULL;
2165
2166 return LDAP_OTHER;
2167 }
2168
2169 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2170 {
2171 if ( attr == NULL ) {
2172 return LDAP_PARAM_ERROR;
2173 }
2174
2175 *type = attr->a_desc->ad_cname.bv_val;
2176
2177 return LDAP_SUCCESS;
2178 }
2179
2180 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2181 {
2182 if ( attr == NULL ) {
2183 return LDAP_PARAM_ERROR;
2184 }
2185 *oidp = attr->a_desc->ad_type->sat_oid;
2186
2187 return LDAP_SUCCESS;
2188 }
2189
2190 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2191 {
2192 MatchingRule *mr;
2193 int ret;
2194 int rc;
2195 const char *text;
2196
2197 mr = a->a_desc->ad_type->sat_equality;
2198 rc = value_match( &ret, a->a_desc, mr,
2199 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2200 (struct berval *)v1, (void *)v2, &text );
2201 if ( rc != LDAP_SUCCESS )
2202 return -1;
2203
2204 return ( ret == 0 ) ? 0 : -1;
2205 }
2206
2207 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2208 {
2209 int rc;
2210
2211 if ( a ->a_vals == NULL ) {
2212 return -1;
2213 }
2214 rc = attr_valfind( (Attribute *)a, SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, v,
2215 NULL, NULL );
2216 return rc == 0 ? 0 : -1;
2217 }
2218
2219 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2220 {
2221 AttributeDescription *a1 = NULL;
2222 AttributeDescription *a2 = NULL;
2223 const char *text;
2224 int ret;
2225
2226 if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2227 return -1;
2228 }
2229
2230 if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2231 return 1;
2232 }
2233
2234 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2235 ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2236 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2237
2238 switch ( opt ) {
2239 case SLAPI_TYPE_CMP_EXACT:
2240 ret = ad_cmp( a1, a2 );
2241 break;
2242 case SLAPI_TYPE_CMP_BASE:
2243 ret = ad_base_cmp( a1, a2 );
2244 break;
2245 case SLAPI_TYPE_CMP_SUBTYPE:
2246 ret = is_ad_subtype( a2, a2 );
2247 break;
2248 default:
2249 ret = -1;
2250 break;
2251 }
2252
2253 return ret;
2254 }
2255
2256 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2257 {
2258 return ( slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ) == 0 );
2259 }
2260
2261 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2262 {
2263 return slapi_valueset_first_value( &a->a_vals, v );
2264 }
2265
2266 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2267 {
2268 return slapi_valueset_next_value( &a->a_vals, hint, v );
2269 }
2270
2271 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2272 {
2273 *numValues = slapi_valueset_count( &a->a_vals );
2274
2275 return 0;
2276 }
2277
2278 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2279 {
2280 *vs = &((Slapi_Attr *)a)->a_vals;
2281
2282 return 0;
2283 }
2284
2285 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2286 {
2287 return slapi_attr_get_values( a, vals );
2288 }
2289
2290 char *slapi_attr_syntax_normalize( const char *s )
2291 {
2292 AttributeDescription *ad = NULL;
2293 const char *text;
2294
2295 if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2296 return NULL;
2297 }
2298
2299 return ad->ad_cname.bv_val;
2300 }
2301
2302 Slapi_Value *slapi_value_new( void )
2303 {
2304 struct berval *bv;
2305
2306 bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2307
2308 return bv;
2309 }
2310
2311 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2312 {
2313 return ber_dupbv( NULL, (struct berval *)bval );
2314 }
2315
2316 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2317 {
2318 return slapi_value_new_berval( v );
2319 }
2320
2321 Slapi_Value *slapi_value_new_string(const char *s)
2322 {
2323 struct berval bv;
2324
2325 bv.bv_val = (char *)s;
2326 bv.bv_len = strlen( s );
2327
2328 return slapi_value_new_berval( &bv );
2329 }
2330
2331 Slapi_Value *slapi_value_init(Slapi_Value *val)
2332 {
2333 val->bv_val = NULL;
2334 val->bv_len = 0;
2335
2336 return val;
2337 }
2338
2339 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2340 {
2341 return ber_dupbv( v, bval );
2342 }
2343
2344 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2345 {
2346 v->bv_val = slapi_ch_strdup( s );
2347 v->bv_len = strlen( s );
2348
2349 return v;
2350 }
2351
2352 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2353 {
2354 return slapi_value_new_value( v );
2355 }
2356
2357 void slapi_value_free(Slapi_Value **value)
2358 {
2359 if ( value == NULL ) {
2360 return;
2361 }
2362
2363 if ( (*value) != NULL ) {
2364 slapi_ch_free( (void **)&(*value)->bv_val );
2365 slapi_ch_free( (void **)value );
2366 }
2367 }
2368
2369 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2370 {
2371 return value;
2372 }
2373
2374 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2375 {
2376 if ( value == NULL ) {
2377 return NULL;
2378 }
2379 if ( value->bv_val != NULL ) {
2380 slapi_ch_free( (void **)&value->bv_val );
2381 }
2382 slapi_value_init_berval( value, (struct berval *)bval );
2383
2384 return value;
2385 }
2386
2387 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2388 {
2389 if ( value == NULL ) {
2390 return NULL;
2391 }
2392 return slapi_value_set_berval( value, vfrom );
2393 }
2394
2395 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2396 {
2397 if ( value == NULL ) {
2398 return NULL;
2399 }
2400 if ( value->bv_val != NULL ) {
2401 slapi_ch_free( (void **)&value->bv_val );
2402 }
2403 value->bv_val = slapi_ch_malloc( len );
2404 value->bv_len = len;
2405 AC_MEMCPY( value->bv_val, val, len );
2406
2407 return value;
2408 }
2409
2410 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2411 {
2412 if ( value == NULL ) {
2413 return -1;
2414 }
2415 slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2416 return 0;
2417 }
2418
2419 int slapi_value_set_int(Slapi_Value *value, int intVal)
2420 {
2421 char buf[64];
2422
2423 snprintf( buf, sizeof( buf ), "%d", intVal );
2424
2425 return slapi_value_set_string( value, buf );
2426 }
2427
2428 const char *slapi_value_get_string(const Slapi_Value *value)
2429 {
2430 if ( value == NULL ) return NULL;
2431 if ( value->bv_val == NULL ) return NULL;
2432 if ( !checkBVString( value ) ) return NULL;
2433
2434 return value->bv_val;
2435 }
2436
2437 int slapi_value_get_int(const Slapi_Value *value)
2438 {
2439 if ( value == NULL ) return 0;
2440 if ( value->bv_val == NULL ) return 0;
2441 if ( !checkBVString( value ) ) return 0;
2442
2443 return (int)strtol( value->bv_val, NULL, 10 );
2444 }
2445
2446 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2447 {
2448 if ( value == NULL ) return 0;
2449 if ( value->bv_val == NULL ) return 0;
2450 if ( !checkBVString( value ) ) return 0;
2451
2452 return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2453 }
2454
2455 long slapi_value_get_long(const Slapi_Value *value)
2456 {
2457 if ( value == NULL ) return 0;
2458 if ( value->bv_val == NULL ) return 0;
2459 if ( !checkBVString( value ) ) return 0;
2460
2461 return strtol( value->bv_val, NULL, 10 );
2462 }
2463
2464 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2465 {
2466 if ( value == NULL ) return 0;
2467 if ( value->bv_val == NULL ) return 0;
2468 if ( !checkBVString( value ) ) return 0;
2469
2470 return strtoul( value->bv_val, NULL, 10 );
2471 }
2472
2473 size_t slapi_value_get_length(const Slapi_Value *value)
2474 {
2475 if ( value == NULL )
2476 return 0;
2477
2478 return (size_t) value->bv_len;
2479 }
2480
2481 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2482 {
2483 return slapi_attr_value_cmp( a, v1, v2 );
2484 }
2485
2486 /* A ValueSet is a container for a BerVarray. */
2487 Slapi_ValueSet *slapi_valueset_new( void )
2488 {
2489 Slapi_ValueSet *vs;
2490
2491 vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2492 *vs = NULL;
2493
2494 return vs;
2495 }
2496
2497 void slapi_valueset_free(Slapi_ValueSet *vs)
2498 {
2499 if ( vs != NULL ) {
2500 BerVarray vp = *vs;
2501
2502 ber_bvarray_free( vp );
2503 vp = NULL;
2504
2505 slapi_ch_free( (void **)&vp );
2506 }
2507 }
2508
2509 void slapi_valueset_init(Slapi_ValueSet *vs)
2510 {
2511 if ( vs != NULL && *vs == NULL ) {
2512 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2513 (*vs)->bv_val = NULL;
2514 (*vs)->bv_len = 0;
2515 }
2516 }
2517
2518 void slapi_valueset_done(Slapi_ValueSet *vs)
2519 {
2520 BerVarray vp;
2521
2522 if ( vs == NULL )
2523 return;
2524
2525 for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2526 vp->bv_len = 0;
2527 slapi_ch_free( (void **)&vp->bv_val );
2528 }
2529 /* but don't free *vs or vs */
2530 }
2531
2532 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2533 {
2534 struct berval bv;
2535
2536 ber_dupbv( &bv, (Slapi_Value *)addval );
2537 ber_bvarray_add( vs, &bv );
2538 }
2539
2540 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2541 {
2542 return slapi_valueset_next_value( vs, 0, v );
2543 }
2544
2545 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2546 {
2547 int i;
2548 BerVarray vp;
2549
2550 if ( vs == NULL )
2551 return -1;
2552
2553 vp = *vs;
2554
2555 for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2556 if ( i == index ) {
2557 *v = &vp[i];
2558 return index + 1;
2559 }
2560 }
2561
2562 return -1;
2563 }
2564
2565 int slapi_valueset_count( const Slapi_ValueSet *vs )
2566 {
2567 int i;
2568 BerVarray vp;
2569
2570 if ( vs == NULL )
2571 return 0;
2572
2573 vp = *vs;
2574
2575 if ( vp == NULL )
2576 return 0;
2577
2578 for ( i = 0; vp[i].bv_val != NULL; i++ )
2579 ;
2580
2581 return i;
2582
2583 }
2584
2585 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2586 {
2587 BerVarray vp;
2588
2589 for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2590 slapi_valueset_add_value( vs1, vp );
2591 }
2592 }
2593
2594 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2595 struct berval *val, int access )
2596 {
2597 int rc;
2598 slap_access_t slap_access;
2599 AttributeDescription *ad = NULL;
2600 const char *text;
2601
2602 rc = slap_str2ad( attr, &ad, &text );
2603 if ( rc != LDAP_SUCCESS ) {
2604 return rc;
2605 }
2606
2607 /*
2608 * Whilst the SLAPI access types are arranged as a bitmask, the
2609 * documentation indicates that they are to be used separately.
2610 */
2611 switch ( access & SLAPI_ACL_ALL ) {
2612 case SLAPI_ACL_COMPARE:
2613 slap_access = ACL_COMPARE;
2614 break;
2615 case SLAPI_ACL_SEARCH:
2616 slap_access = ACL_SEARCH;
2617 break;
2618 case SLAPI_ACL_READ:
2619 slap_access = ACL_READ;
2620 break;
2621 case SLAPI_ACL_WRITE:
2622 slap_access = ACL_WRITE;
2623 break;
2624 case SLAPI_ACL_DELETE:
2625 slap_access = ACL_WDEL;
2626 break;
2627 case SLAPI_ACL_ADD:
2628 slap_access = ACL_WADD;
2629 break;
2630 case SLAPI_ACL_SELF: /* not documented */
2631 case SLAPI_ACL_PROXY: /* not documented */
2632 default:
2633 return LDAP_INSUFFICIENT_ACCESS;
2634 break;
2635 }
2636
2637 assert( pb->pb_op != NULL );
2638
2639 if ( access_allowed( pb->pb_op, e, ad, val, slap_access, NULL ) ) {
2640 return LDAP_SUCCESS;
2641 }
2642
2643 return LDAP_INSUFFICIENT_ACCESS;
2644 }
2645
2646 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2647 {
2648 int rc = LDAP_SUCCESS;
2649 Modifications *ml;
2650
2651 if ( pb == NULL || pb->pb_op == NULL )
2652 return LDAP_PARAM_ERROR;
2653
2654 ml = slapi_int_ldapmods2modifications( pb->pb_op, mods );
2655 if ( ml == NULL ) {
2656 return LDAP_OTHER;
2657 }
2658
2659 if ( rc == LDAP_SUCCESS ) {
2660 rc = acl_check_modlist( pb->pb_op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2661 }
2662
2663 slap_mods_free( ml, 1 );
2664
2665 return rc;
2666 }
2667
2668 /*
2669 * Synthesise an LDAPMod array from a Modifications list to pass
2670 * to SLAPI.
2671 */
2672 LDAPMod **slapi_int_modifications2ldapmods( Modifications *modlist )
2673 {
2674 Modifications *ml;
2675 LDAPMod **mods, *modp;
2676 int i, j;
2677
2678 for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2679 ;
2680
2681 mods = (LDAPMod **)slapi_ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2682
2683 for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2684 mods[i] = (LDAPMod *)slapi_ch_malloc( sizeof(LDAPMod) );
2685 modp = mods[i];
2686 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2687 if ( BER_BVISNULL( &ml->sml_type ) ) {
2688 /* may happen for internally generated mods */
2689 assert( ml->sml_desc != NULL );
2690 modp->mod_type = slapi_ch_strdup( ml->sml_desc->ad_cname.bv_val );
2691 } else {
2692 modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );
2693 }
2694
2695 if ( ml->sml_values != NULL ) {
2696 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
2697 ;
2698 modp->mod_bvalues = (struct berval **)slapi_ch_malloc( (j + 1) *
2699 sizeof(struct berval *) );
2700 for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
2701 modp->mod_bvalues[j] = (struct berval *)slapi_ch_malloc(
2702 sizeof(struct berval) );
2703 ber_dupbv( modp->mod_bvalues[j], &ml->sml_values[j] );
2704 }
2705 modp->mod_bvalues[j] = NULL;
2706 } else {
2707 modp->mod_bvalues = NULL;
2708 }
2709 i++;
2710 }
2711
2712 mods[i] = NULL;
2713
2714 return mods;
2715 }
2716
2717 /*
2718 * Convert a potentially modified array of LDAPMods back to a
2719 * Modification list. Unfortunately the values need to be
2720 * duplicated because slap_mods_check() will try to free them
2721 * before prettying (and we can't easily get out of calling
2722 * slap_mods_check() because we need normalized values).
2723 */
2724 Modifications *slapi_int_ldapmods2modifications ( Operation *op, LDAPMod **mods )
2725 {
2726 Modifications *modlist = NULL, **modtail;
2727 LDAPMod **modp;
2728 char textbuf[SLAP_TEXT_BUFLEN];
2729 const char *text;
2730
2731 if ( mods == NULL ) {
2732 return NULL;
2733 }
2734
2735 modtail = &modlist;
2736
2737 for ( modp = mods; *modp != NULL; modp++ ) {
2738 Modifications *mod;
2739 LDAPMod *lmod = *modp;
2740 int i;
2741 const char *text;
2742 AttributeDescription *ad = NULL;
2743
2744 if ( slap_str2ad( lmod->mod_type, &ad, &text ) != LDAP_SUCCESS ) {
2745 continue;
2746 }
2747
2748 mod = (Modifications *) slapi_ch_malloc( sizeof(Modifications) );
2749 mod->sml_op = lmod->mod_op & ~(LDAP_MOD_BVALUES);
2750 mod->sml_flags = 0;
2751 mod->sml_type = ad->ad_cname;
2752 mod->sml_desc = ad;
2753 mod->sml_next = NULL;
2754
2755 i = 0;
2756 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2757 if ( lmod->mod_bvalues != NULL ) {
2758 while ( lmod->mod_bvalues[i] != NULL )
2759 i++;
2760 }
2761 } else {
2762 if ( lmod->mod_values != NULL ) {
2763 while ( lmod->mod_values[i] != NULL )
2764 i++;
2765 }
2766 }
2767 mod->sml_numvals = i;
2768
2769 if ( i == 0 ) {
2770 mod->sml_values = NULL;
2771 } else {
2772 mod->sml_values = (BerVarray) slapi_ch_malloc( (i + 1) * sizeof(struct berval) );
2773
2774 /* NB: This implicitly trusts a plugin to return valid modifications. */
2775 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2776 for ( i = 0; lmod->mod_bvalues[i] != NULL; i++ ) {
2777 ber_dupbv( &mod->sml_values[i], lmod->mod_bvalues[i] );
2778 }
2779 } else {
2780 for ( i = 0; lmod->mod_values[i] != NULL; i++ ) {
2781 mod->sml_values[i].bv_val = slapi_ch_strdup( lmod->mod_values[i] );
2782 mod->sml_values[i].bv_len = strlen( lmod->mod_values[i] );
2783 }
2784 }
2785 mod->sml_values[i].bv_val = NULL;
2786 mod->sml_values[i].bv_len = 0;
2787 }
2788 mod->sml_nvalues = NULL;
2789
2790 *modtail = mod;
2791 modtail = &mod->sml_next;
2792 }
2793
2794 if ( slap_mods_check( op, modlist, &text, textbuf, sizeof( textbuf ), NULL ) != LDAP_SUCCESS ) {
2795 slap_mods_free( modlist, 1 );
2796 modlist = NULL;
2797 }
2798
2799 return modlist;
2800 }
2801
2802 /*
2803 * Sun ONE DS 5.x computed attribute support. Computed attributes
2804 * allow for dynamically generated operational attributes, a very
2805 * useful thing indeed.
2806 */
2807
2808 /*
2809 * For some reason Sun don't use the normal plugin mechanism
2810 * registration path to register an "evaluator" function (an
2811 * "evaluator" is responsible for adding computed attributes;
2812 * the nomenclature is somewhat confusing).
2813 *
2814 * As such slapi_compute_add_evaluator() registers the
2815 * function directly.
2816 */
2817 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2818 {
2819 Slapi_PBlock *pPlugin = NULL;
2820 int rc;
2821 int type = SLAPI_PLUGIN_OBJECT;
2822
2823 pPlugin = slapi_pblock_new();
2824 if ( pPlugin == NULL ) {
2825 rc = LDAP_NO_MEMORY;
2826 goto done;
2827 }
2828
2829 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2830 if ( rc != LDAP_SUCCESS ) {
2831 goto done;
2832 }
2833
2834 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2835 if ( rc != LDAP_SUCCESS ) {
2836 goto done;
2837 }
2838
2839 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2840 if ( rc != 0 ) {
2841 rc = LDAP_OTHER;
2842 goto done;
2843 }
2844
2845 done:
2846 if ( rc != LDAP_SUCCESS ) {
2847 if ( pPlugin != NULL ) {
2848 slapi_pblock_destroy( pPlugin );
2849 }
2850 return -1;
2851 }
2852
2853 return 0;
2854 }
2855
2856 /*
2857 * See notes above regarding slapi_compute_add_evaluator().
2858 */
2859 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2860 {
2861 Slapi_PBlock *pPlugin = NULL;
2862 int rc;
2863 int type = SLAPI_PLUGIN_OBJECT;
2864
2865 pPlugin = slapi_pblock_new();
2866 if ( pPlugin == NULL ) {
2867 rc = LDAP_NO_MEMORY;
2868 goto done;
2869 }
2870
2871 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2872 if ( rc != LDAP_SUCCESS ) {
2873 goto done;
2874 }
2875
2876 rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2877 if ( rc != LDAP_SUCCESS ) {
2878 goto done;
2879 }
2880
2881 rc = slapi_int_register_plugin( frontendDB, pPlugin );
2882 if ( rc != 0 ) {
2883 rc = LDAP_OTHER;
2884 goto done;
2885 }
2886
2887 done:
2888 if ( rc != LDAP_SUCCESS ) {
2889 if ( pPlugin != NULL ) {
2890 slapi_pblock_destroy( pPlugin );
2891 }
2892 return -1;
2893 }
2894
2895 return 0;
2896 }
2897
2898 /*
2899 * Call compute evaluators
2900 */
2901 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2902 {
2903 int rc = 0;
2904 slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2905
2906 rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2907 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2908 /* Nothing to do; front-end should ignore. */
2909 return 0;
2910 }
2911
2912 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2913 /*
2914 * -1: no attribute matched requested type
2915 * 0: one attribute matched
2916 * >0: error happened
2917 */
2918 rc = (*pGetPlugin)( c, type, e, outputfn );
2919 if ( rc > 0 ) {
2920 break;
2921 }
2922 }
2923
2924 slapi_ch_free( (void **)&tmpPlugin );
2925
2926 return rc;
2927 }
2928
2929 int
2930 compute_rewrite_search_filter( Slapi_PBlock *pb )
2931 {
2932 if ( pb == NULL || pb->pb_op == NULL )
2933 return LDAP_PARAM_ERROR;
2934
2935 return slapi_int_call_plugins( pb->pb_op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
2936 }
2937
2938 /*
2939 * New API to provide the plugin with access to the search
2940 * pblock. Have informed Sun DS team.
2941 */
2942 int
2943 slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
2944 {
2945 if ( c == NULL )
2946 return -1;
2947
2948 if ( c->cac_pb == NULL )
2949 return -1;
2950
2951 *pb = c->cac_pb;
2952
2953 return 0;
2954 }
2955
2956 Slapi_Mutex *slapi_new_mutex( void )
2957 {
2958 Slapi_Mutex *m;
2959
2960 m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
2961 if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
2962 slapi_ch_free( (void **)&m );
2963 return NULL;
2964 }
2965
2966 return m;
2967 }
2968
2969 void slapi_destroy_mutex( Slapi_Mutex *mutex )
2970 {
2971 if ( mutex != NULL ) {
2972 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
2973 slapi_ch_free( (void **)&mutex);
2974 }
2975 }
2976
2977 void slapi_lock_mutex( Slapi_Mutex *mutex )
2978 {
2979 ldap_pvt_thread_mutex_lock( &mutex->mutex );
2980 }
2981
2982 int slapi_unlock_mutex( Slapi_Mutex *mutex )
2983 {
2984 return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
2985 }
2986
2987 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
2988 {
2989 Slapi_CondVar *cv;
2990
2991 if ( mutex == NULL ) {
2992 return NULL;
2993 }
2994
2995 cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
2996 if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
2997 slapi_ch_free( (void **)&cv );
2998 return NULL;
2999 }
3000
3001 cv->mutex = mutex->mutex;
3002
3003 return cv;
3004 }
3005
3006 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3007 {
3008 if ( cvar != NULL ) {
3009 ldap_pvt_thread_cond_destroy( &cvar->cond );
3010 slapi_ch_free( (void **)&cvar );
3011 }
3012 }
3013
3014 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3015 {
3016 if ( cvar == NULL ) {
3017 return -1;
3018 }
3019
3020 return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3021 }
3022
3023 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3024 {
3025 if ( cvar == NULL ) {
3026 return -1;
3027 }
3028
3029 if ( notify_all ) {
3030 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3031 }
3032
3033 return ldap_pvt_thread_cond_signal( &cvar->cond );
3034 }
3035
3036 int slapi_int_access_allowed( Operation *op,
3037 Entry *entry,
3038 AttributeDescription *desc,
3039 struct berval *val,
3040 slap_access_t access,
3041 AccessControlState *state )
3042 {
3043 int rc, slap_access = 0;
3044 slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3045 Slapi_PBlock *pb;
3046
3047 pb = SLAPI_OPERATION_PBLOCK( op );
3048 if ( pb == NULL ) {
3049 /* internal operation */
3050 return 1;
3051 }
3052
3053 switch ( access ) {
3054 case ACL_COMPARE:
3055 slap_access |= SLAPI_ACL_COMPARE;
3056 break;
3057 case ACL_SEARCH:
3058 slap_access |= SLAPI_ACL_SEARCH;
3059 break;
3060 case ACL_READ:
3061 slap_access |= SLAPI_ACL_READ;
3062 break;
3063 case ACL_WRITE:
3064 slap_access |= SLAPI_ACL_WRITE;
3065 break;
3066 case ACL_WDEL:
3067 slap_access |= SLAPI_ACL_DELETE;
3068 break;
3069 case ACL_WADD:
3070 slap_access |= SLAPI_ACL_ADD;
3071 break;
3072 default:
3073 break;
3074 }
3075
3076 rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3077 if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3078 /* nothing to do; allowed access */
3079 return 1;
3080 }
3081
3082 rc = 1; /* default allow policy */
3083
3084 for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3085 /*
3086 * 0 access denied
3087 * 1 access granted
3088 */
3089 rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,
3090 val, slap_access, (void *)state );
3091 if ( rc == 0 ) {
3092 break;
3093 }
3094 }
3095
3096 slapi_ch_free( (void **)&tmpPlugin );
3097
3098 return rc;
3099 }
3100
3101 /*
3102 * There is no documentation for this.
3103 */
3104 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
3105 {
3106 LDAPRDN lrdn;
3107 LDAPAVA *ava;
3108 int rc;
3109 char *p;
3110
3111 *type = NULL;
3112
3113 bv->bv_len = 0;
3114 bv->bv_val = NULL;
3115
3116 rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
3117 if ( rc != LDAP_SUCCESS ) {
3118 return -1;
3119 }
3120
3121 if ( lrdn[1] != NULL ) {
3122 return -1; /* not single valued */
3123 }
3124
3125 ava = lrdn[0];
3126
3127 *type = slapi_ch_strdup( ava->la_attr.bv_val );
3128 ber_dupbv( bv, &ava->la_value );
3129
3130 ldap_rdnfree(lrdn);
3131
3132 return 0;
3133 }
3134
3135 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
3136 {
3137 struct berval new_dn, parent_dn, newrdn;
3138
3139 new_dn.bv_val = NULL;
3140
3141 parent_dn.bv_val = (char *)dn;
3142 parent_dn.bv_len = strlen( dn );
3143
3144 newrdn.bv_val = (char *)rdn;
3145 newrdn.bv_len = strlen( rdn );
3146
3147 build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
3148
3149 return new_dn.bv_val;
3150 }
3151
3152 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
3153 {
3154 Backend *be_orig;
3155 const char *text;
3156 char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
3157 size_t textlen = sizeof textbuf;
3158 int rc = LDAP_SUCCESS;
3159
3160 PBLOCK_ASSERT_OP( pb, 0 );
3161
3162 be_orig = pb->pb_op->o_bd;
3163
3164 pb->pb_op->o_bd = select_backend( &e->e_nname, 0 );
3165 if ( pb->pb_op->o_bd != NULL ) {
3166 rc = entry_schema_check( pb->pb_op, e, NULL, 0, 0, NULL,
3167 &text, textbuf, textlen );
3168 }
3169 pb->pb_op->o_bd = be_orig;
3170
3171 return ( rc == LDAP_SUCCESS ) ? 0 : 1;
3172 }
3173
3174 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
3175 {
3176 LDAPDN dn;
3177 int rc;
3178 int i = 0, match = 0;
3179
3180 rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3181 if ( rc != LDAP_SUCCESS ) {
3182 return 0;
3183 }
3184
3185 if ( dn[0] != NULL ) {
3186 LDAPRDN rdn = dn[0];
3187
3188 for ( i = 0; rdn[i] != NULL; i++ ) {
3189 LDAPAVA *ava = &rdn[0][i];
3190 Slapi_Attr *a = NULL;
3191
3192 if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
3193 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3194 match++;
3195 }
3196 }
3197
3198 ldap_dnfree( dn );
3199
3200 return ( i == match );
3201 }
3202
3203 int slapi_entry_add_rdn_values( Slapi_Entry *e )
3204 {
3205 LDAPDN dn;
3206 int i, rc;
3207
3208 rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3209 if ( rc != LDAP_SUCCESS ) {
3210 return rc;
3211 }
3212
3213 if ( dn[0] != NULL ) {
3214 LDAPRDN rdn = dn[0];
3215 struct berval *vals[2];
3216
3217 for ( i = 0; rdn[i] != NULL; i++ ) {
3218 LDAPAVA *ava = &rdn[0][i];
3219 Slapi_Attr *a = NULL;
3220
3221 if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
3222 slapi_attr_value_find( a, &ava->la_value ) == 0 )
3223 continue;
3224
3225 vals[0] = &ava->la_value;
3226 vals[1] = NULL;
3227
3228 slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
3229 }
3230 }
3231
3232 ldap_dnfree( dn );
3233
3234 return LDAP_SUCCESS;
3235 }
3236
3237 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
3238 {
3239 Attribute *attr;
3240
3241 attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
3242 if ( attr == NULL ) {
3243 return NULL;
3244 }
3245
3246 if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
3247 return slapi_value_get_string( &attr->a_vals[0] );
3248 }
3249
3250 return NULL;
3251 }
3252
3253 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
3254 {
3255 struct berval bv;
3256
3257 attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
3258
3259 bv.bv_val = uniqueid;
3260 bv.bv_len = strlen( uniqueid );
3261 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
3262 }
3263
3264 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
3265 {
3266 LDAP *ld;
3267 char *url;
3268 size_t size;
3269 int rc;
3270
3271 size = sizeof("ldap:///");
3272 if ( secure ) {
3273 size++;
3274 }
3275 size += strlen( ldaphost );
3276 if ( ldapport != 0 ) {
3277 size += 32;
3278 }
3279
3280 url = slapi_ch_malloc( size );
3281
3282 if ( ldapport != 0 ) {
3283 rc = snprintf( url, size, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
3284 } else {
3285 rc = snprintf( url, size, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
3286 }
3287
3288 if ( rc > 0 && (size_t) rc < size ) {
3289 rc = ldap_initialize( &ld, url );
3290 } else {
3291 ld = NULL;
3292 }
3293
3294 slapi_ch_free_string( &url );
3295
3296 return ( rc == LDAP_SUCCESS ) ? ld : NULL;
3297 }
3298
3299 void slapi_ldap_unbind( LDAP *ld )
3300 {
3301 ldap_unbind_ext_s( ld, NULL, NULL );
3302 }
3303
3304 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
3305 {
3306 if ( be == NULL )
3307 return LDAP_PARAM_ERROR;
3308
3309 *flags = SLAP_DBFLAGS(be);
3310
3311 return LDAP_SUCCESS;
3312 }
3313
3314 int
3315 slapi_int_count_controls( LDAPControl **ctrls )
3316 {
3317 size_t i;
3318
3319 if ( ctrls == NULL )
3320 return 0;
3321
3322 for ( i = 0; ctrls[i] != NULL; i++ )
3323 ;
3324
3325 return i;
3326 }
3327
3328 int
3329 slapi_op_abandoned( Slapi_PBlock *pb )
3330 {
3331 if ( pb->pb_op == NULL )
3332 return 0;
3333
3334 return ( pb->pb_op->o_abandon );
3335 }
3336
3337 char *
3338 slapi_op_type_to_string(unsigned long type)
3339 {
3340 char *str;
3341
3342 switch (type) {
3343 case SLAPI_OPERATION_BIND:
3344 str = "bind";
3345 break;
3346 case SLAPI_OPERATION_UNBIND:
3347 str = "unbind";
3348 break;
3349 case SLAPI_OPERATION_SEARCH:
3350 str = "search";
3351 break;
3352 case SLAPI_OPERATION_MODIFY:
3353 str = "modify";
3354 break;
3355 case SLAPI_OPERATION_ADD:
3356 str = "add";
3357 break;
3358 case SLAPI_OPERATION_DELETE:
3359 str = "delete";
3360 break;
3361 case SLAPI_OPERATION_MODDN:
3362 str = "modrdn";
3363 break;
3364 case SLAPI_OPERATION_COMPARE:
3365 str = "compare";
3366 break;
3367 case SLAPI_OPERATION_ABANDON:
3368 str = "abandon";
3369 break;
3370 case SLAPI_OPERATION_EXTENDED:
3371 str = "extended";
3372 break;
3373 default:
3374 str = "unknown operation type";
3375 break;
3376 }
3377 return str;
3378 }
3379
3380 unsigned long
3381 slapi_op_get_type(Slapi_Operation * op)
3382 {
3383 unsigned long type;
3384
3385 switch ( op->o_tag ) {
3386 case LDAP_REQ_BIND:
3387 type = SLAPI_OPERATION_BIND;
3388 break;
3389 case LDAP_REQ_UNBIND:
3390 type = SLAPI_OPERATION_UNBIND;
3391 break;
3392 case LDAP_REQ_SEARCH:
3393 type = SLAPI_OPERATION_SEARCH;
3394 break;
3395 case LDAP_REQ_MODIFY:
3396 type = SLAPI_OPERATION_MODIFY;
3397 break;
3398 case LDAP_REQ_ADD:
3399 type = SLAPI_OPERATION_ADD;
3400 break;
3401 case LDAP_REQ_DELETE:
3402 type = SLAPI_OPERATION_DELETE;
3403 break;
3404 case LDAP_REQ_MODRDN:
3405 type = SLAPI_OPERATION_MODDN;
3406 break;
3407 case LDAP_REQ_COMPARE:
3408 type = SLAPI_OPERATION_COMPARE;
3409 break;
3410 case LDAP_REQ_ABANDON:
3411 type = SLAPI_OPERATION_ABANDON;
3412 break;
3413 case LDAP_REQ_EXTENDED:
3414 type = SLAPI_OPERATION_EXTENDED;
3415 break;
3416 default:
3417 type = SLAPI_OPERATION_NONE;
3418 break;
3419 }
3420 return type;
3421 }
3422
3423 void slapi_be_set_readonly( Slapi_Backend *be, int readonly )
3424 {
3425 if ( be == NULL )
3426 return;
3427
3428 if ( readonly )
3429 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
3430 else
3431 be->be_restrictops &= ~(SLAP_RESTRICT_OP_WRITES);
3432 }
3433
3434 int slapi_be_get_readonly( Slapi_Backend *be )
3435 {
3436 if ( be == NULL )
3437 return 0;
3438
3439 return ( (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) == SLAP_RESTRICT_OP_WRITES );
3440 }
3441
3442 const char *slapi_x_be_get_updatedn( Slapi_Backend *be )
3443 {
3444 if ( be == NULL )
3445 return NULL;
3446
3447 return be->be_update_ndn.bv_val;
3448 }
3449
3450 Slapi_Backend *slapi_be_select( const Slapi_DN *sdn )
3451 {
3452 Slapi_Backend *be;
3453
3454 slapi_sdn_get_ndn( sdn );
3455
3456 be = select_backend( (struct berval *)&sdn->ndn, 0 );
3457
3458 return be;
3459 }
3460
3461 #if 0
3462 void
3463 slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag)
3464 {
3465 }
3466
3467 void
3468 slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag)
3469 {
3470 }
3471
3472 int
3473 slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag)
3474 {
3475 }
3476 #endif
3477
3478 #endif /* LDAP_SLAPI */
3479