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