]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_utils.c
Add ability to cache negative results and specify negative TTL on templates
[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-2006 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 *)slapi_ch_calloc( 1, sizeof(Slapi_Entry) );
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, 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, 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, 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         struct berval *bv;
1045
1046         bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1047         bv->bv_len = v->bv_len;
1048         bv->bv_val = slapi_ch_malloc( bv->bv_len );
1049         AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1050
1051         return bv;
1052 }
1053
1054 struct berval **
1055 slapi_ch_bvecdup(const struct berval **v)
1056 {
1057         int i;
1058         struct berval **rv;
1059
1060         if ( v == NULL ) {
1061                 return NULL;
1062         }
1063
1064         for ( i = 0; v[i] != NULL; i++ )
1065                 ;
1066
1067         rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1068
1069         for ( i = 0; v[i] != NULL; i++ ) {
1070                 rv[i] = slapi_ch_bvdup( v[i] );
1071         }
1072         rv[i] = NULL;
1073
1074         return rv;
1075 }
1076
1077 char *
1078 slapi_ch_calloc(
1079         unsigned long nelem, 
1080         unsigned long size ) 
1081 {
1082         return ch_calloc( nelem, size );
1083 }
1084
1085 char *
1086 slapi_ch_realloc(
1087         char *block, 
1088         unsigned long size ) 
1089 {
1090         return ch_realloc( block, size );
1091 }
1092
1093 char *
1094 slapi_ch_strdup( const char *s ) 
1095 {
1096         return ch_strdup( s );
1097 }
1098
1099 size_t
1100 slapi_ch_stlen( const char *s ) 
1101 {
1102         return strlen( s );
1103 }
1104
1105 int 
1106 slapi_control_present(
1107         LDAPControl     **controls, 
1108         char            *oid, 
1109         struct berval   **val, 
1110         int             *iscritical ) 
1111 {
1112         int             i;
1113         int             rc = 0;
1114
1115         if ( val ) {
1116                 *val = NULL;
1117         }
1118         
1119         if ( iscritical ) {
1120                 *iscritical = 0;
1121         }
1122         
1123         for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1124                 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1125                         continue;
1126                 }
1127
1128                 rc = 1;
1129                 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1130                         if ( val ) {
1131                                 *val = &controls[i]->ldctl_value;
1132                         }
1133                 }
1134
1135                 if ( iscritical ) {
1136                         *iscritical = controls[i]->ldctl_iscritical;
1137                 }
1138
1139                 break;
1140         }
1141
1142         return rc;
1143 }
1144
1145 static void
1146 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1147         unsigned long *slapi_mask)
1148 {
1149         *slapi_mask = SLAPI_OPERATION_NONE;
1150
1151         if ( slap_mask & SLAP_CTRL_ABANDON ) 
1152                 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1153
1154         if ( slap_mask & SLAP_CTRL_ADD )
1155                 *slapi_mask |= SLAPI_OPERATION_ADD;
1156
1157         if ( slap_mask & SLAP_CTRL_BIND )
1158                 *slapi_mask |= SLAPI_OPERATION_BIND;
1159
1160         if ( slap_mask & SLAP_CTRL_COMPARE )
1161                 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1162
1163         if ( slap_mask & SLAP_CTRL_DELETE )
1164                 *slapi_mask |= SLAPI_OPERATION_DELETE;
1165
1166         if ( slap_mask & SLAP_CTRL_MODIFY )
1167                 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1168
1169         if ( slap_mask & SLAP_CTRL_RENAME )
1170                 *slapi_mask |= SLAPI_OPERATION_MODDN;
1171
1172         if ( slap_mask & SLAP_CTRL_SEARCH )
1173                 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1174
1175         if ( slap_mask & SLAP_CTRL_UNBIND )
1176                 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1177 }
1178
1179 static void
1180 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1181         slap_mask_t *slap_mask)
1182 {
1183         *slap_mask = 0;
1184
1185         if ( slapi_mask & SLAPI_OPERATION_BIND )
1186                 *slap_mask |= SLAP_CTRL_BIND;
1187
1188         if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1189                 *slap_mask |= SLAP_CTRL_UNBIND;
1190
1191         if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1192                 *slap_mask |= SLAP_CTRL_SEARCH;
1193
1194         if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1195                 *slap_mask |= SLAP_CTRL_MODIFY;
1196
1197         if ( slapi_mask & SLAPI_OPERATION_ADD )
1198                 *slap_mask |= SLAP_CTRL_ADD;
1199
1200         if ( slapi_mask & SLAPI_OPERATION_DELETE )
1201                 *slap_mask |= SLAP_CTRL_DELETE;
1202
1203         if ( slapi_mask & SLAPI_OPERATION_MODDN )
1204                 *slap_mask |= SLAP_CTRL_RENAME;
1205
1206         if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1207                 *slap_mask |= SLAP_CTRL_COMPARE;
1208
1209         if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1210                 *slap_mask |= SLAP_CTRL_ABANDON;
1211
1212         *slap_mask |= SLAP_CTRL_GLOBAL;
1213 }
1214
1215 static int
1216 slapi_int_parse_control(
1217         Operation *op,
1218         SlapReply *rs,
1219         LDAPControl *ctrl )
1220 {
1221         /* Plugins must deal with controls themselves. */
1222
1223         return LDAP_SUCCESS;
1224 }
1225
1226 void 
1227 slapi_register_supported_control(
1228         char            *controloid, 
1229         unsigned long   controlops )
1230 {
1231         slap_mask_t controlmask;
1232
1233         slapiControlOp2SlapControlMask( controlops, &controlmask );
1234
1235         register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1236 }
1237
1238 int 
1239 slapi_get_supported_controls(
1240         char            ***ctrloidsp, 
1241         unsigned long   **ctrlopsp ) 
1242 {
1243         int i, rc;
1244
1245         rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1246         if ( rc != LDAP_SUCCESS ) {
1247                 return rc;
1248         }
1249
1250         for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1251                 /* In place, naughty. */
1252                 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1253         }
1254
1255         return LDAP_SUCCESS;
1256 }
1257
1258 LDAPControl *
1259 slapi_dup_control( LDAPControl *ctrl )
1260 {
1261         LDAPControl *ret;
1262
1263         ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1264         ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1265         ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1266         ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1267
1268         return ret;
1269 }
1270
1271 void 
1272 slapi_register_supported_saslmechanism( char *mechanism )
1273 {
1274         /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1275         slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1276                         "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1277 }
1278
1279 char **
1280 slapi_get_supported_saslmechanisms( void )
1281 {
1282         /* FIXME -- can not get the saslmechanism without a connection. */
1283         slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1284                         "can not get the SASL mechanism list "
1285                         "without a connection\n" );
1286         return NULL;
1287 }
1288
1289 char **
1290 slapi_get_supported_extended_ops( void )
1291 {
1292         int             i, j, k;
1293         char            **ppExtOpOID = NULL;
1294         int             numExtOps = 0;
1295
1296         for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1297                 ;
1298         }
1299         
1300         for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1301                 ;
1302         }
1303
1304         numExtOps = i + j;
1305         if ( numExtOps == 0 ) {
1306                 return NULL;
1307         }
1308
1309         ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1310         for ( k = 0; k < i; k++ ) {
1311                 struct berval   *bv;
1312
1313                 bv = get_supported_extop( k );
1314                 assert( bv != NULL );
1315
1316                 ppExtOpOID[ k ] = bv->bv_val;
1317         }
1318         
1319         for ( ; k < j; k++ ) {
1320                 struct berval   *bv;
1321
1322                 bv = slapi_int_get_supported_extop( k );
1323                 assert( bv != NULL );
1324
1325                 ppExtOpOID[ i + k ] = bv->bv_val;
1326         }
1327         ppExtOpOID[ i + k ] = NULL;
1328
1329         return ppExtOpOID;
1330 }
1331
1332 void 
1333 slapi_send_ldap_result(
1334         Slapi_PBlock    *pb, 
1335         int             err, 
1336         char            *matched, 
1337         char            *text, 
1338         int             nentries, 
1339         struct berval   **urls ) 
1340 {
1341         SlapReply       *rs;
1342
1343         PBLOCK_ASSERT_OP( pb, 0 );
1344
1345         rs = pb->pb_rs;
1346
1347         rs->sr_err = err;
1348         rs->sr_matched = matched;
1349         rs->sr_text = text;
1350         rs->sr_ref = NULL;
1351
1352         if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1353                 send_ldap_sasl( pb->pb_op, rs );
1354         } else if ( rs->sr_rspoid != NULL ) {
1355                 send_ldap_extended( pb->pb_op, rs );
1356         } else {
1357                 if ( pb->pb_op->o_tag == LDAP_REQ_SEARCH )
1358                         rs->sr_nentries = nentries;
1359
1360                 send_ldap_result( pb->pb_op, rs );
1361         }
1362 }
1363
1364 int 
1365 slapi_send_ldap_search_entry(
1366         Slapi_PBlock    *pb, 
1367         Slapi_Entry     *e, 
1368         LDAPControl     **ectrls, 
1369         char            **attrs, 
1370         int             attrsonly )
1371 {
1372         SlapReply               rs = { REP_SEARCH };
1373         int                     i = 0;
1374         AttributeName           *an = NULL;
1375         const char              *text;
1376         int                     rc;
1377
1378         assert( pb->pb_op != NULL );
1379
1380         if ( attrs != NULL ) {
1381                 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1382                         ; /* empty */
1383                 }
1384         }
1385
1386         if ( i ) {
1387                 an = (AttributeName *) slapi_ch_malloc( (i+1) * sizeof(AttributeName) );
1388                 for ( i = 0; attrs[i] != NULL; i++ ) {
1389                         an[i].an_name.bv_val = attrs[i];
1390                         an[i].an_name.bv_len = strlen( attrs[i] );
1391                         an[i].an_desc = NULL;
1392                         rs.sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1393                         if ( rs.sr_err != LDAP_SUCCESS) {
1394                                 slapi_ch_free( (void **)&an );
1395                                 return -1;
1396                         }
1397                 }
1398                 an[i].an_name.bv_len = 0;
1399                 an[i].an_name.bv_val = NULL;
1400         }
1401
1402         rs.sr_err = LDAP_SUCCESS;
1403         rs.sr_matched = NULL;
1404         rs.sr_text = NULL;
1405         rs.sr_ref = NULL;
1406         rs.sr_ctrls = ectrls;
1407         rs.sr_attrs = an;
1408         rs.sr_operational_attrs = NULL;
1409         rs.sr_entry = e;
1410         rs.sr_v2ref = NULL;
1411         rs.sr_flags = 0;
1412
1413         rc = send_search_entry( pb->pb_op, &rs );
1414
1415         slapi_ch_free( (void **)&an );
1416
1417         return rc;
1418 }
1419
1420 int 
1421 slapi_send_ldap_search_reference(
1422         Slapi_PBlock    *pb,
1423         Slapi_Entry     *e,
1424         struct berval   **references,
1425         LDAPControl     **ectrls, 
1426         struct berval   **v2refs
1427         )
1428 {
1429         SlapReply       rs = { REP_SEARCHREF };
1430         int             rc;
1431
1432         rs.sr_err = LDAP_SUCCESS;
1433         rs.sr_matched = NULL;
1434         rs.sr_text = NULL;
1435
1436         rc = bvptr2obj( references, &rs.sr_ref );
1437         if ( rc != LDAP_SUCCESS ) {
1438                 return rc;
1439         }
1440
1441         rs.sr_ctrls = ectrls;
1442         rs.sr_attrs = NULL;
1443         rs.sr_operational_attrs = NULL;
1444         rs.sr_entry = e;
1445
1446         if ( v2refs != NULL ) {
1447                 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1448                 if ( rc != LDAP_SUCCESS ) {
1449                         slapi_ch_free( (void **)&rs.sr_ref );
1450                         return rc;
1451                 }
1452         } else {
1453                 rs.sr_v2ref = NULL;
1454         }
1455
1456         rc = send_search_reference( pb->pb_op, &rs );
1457
1458         slapi_ch_free( (void **)&rs.sr_ref );
1459         slapi_ch_free( (void **)&rs.sr_v2ref );
1460
1461         return rc;
1462 }
1463
1464 Slapi_Filter *
1465 slapi_str2filter( char *str ) 
1466 {
1467         return str2filter( str );
1468 }
1469
1470 void 
1471 slapi_filter_free(
1472         Slapi_Filter    *f, 
1473         int             recurse ) 
1474 {
1475         filter_free( f );
1476 }
1477
1478 Slapi_Filter *
1479 slapi_filter_dup( Slapi_Filter *filter )
1480 {
1481         Filter *f;
1482
1483         f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1484         f->f_next = NULL;
1485         f->f_choice = filter->f_choice;
1486
1487         switch ( f->f_choice ) {
1488         case LDAP_FILTER_AND:
1489         case LDAP_FILTER_NOT:
1490         case LDAP_FILTER_OR: {
1491                 Filter *pFilter, **ppF;
1492
1493                 for ( pFilter = filter->f_list, ppF = &f->f_list;
1494                       pFilter != NULL;
1495                       pFilter = pFilter->f_next, ppF = &f->f_next )
1496                 {
1497                         *ppF = slapi_filter_dup( pFilter );
1498                         if ( *ppF == NULL )
1499                                 break;
1500                 }
1501                 break;
1502         }
1503         case LDAP_FILTER_PRESENT:
1504                 f->f_desc = filter->f_desc;
1505                 break;
1506         case LDAP_FILTER_EQUALITY:
1507         case LDAP_FILTER_GE:
1508         case LDAP_FILTER_LE:
1509         case LDAP_FILTER_APPROX:
1510                 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1511                 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1512                 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1513                 break;
1514         case LDAP_FILTER_EXT:
1515                 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1516                 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1517                 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1518                 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1519                 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1520                 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1521                 break;
1522         case LDAP_FILTER_SUBSTRINGS: {
1523                 int i;
1524
1525                 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1526                 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1527                 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1528                 if ( filter->f_sub_any != NULL ) {
1529                         for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1530                                 ;
1531                         f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1532                         for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1533                                 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1534                         }
1535                         f->f_sub_any[i].bv_val = NULL;
1536                 } else {
1537                         f->f_sub_any = NULL;
1538                 }
1539                 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1540                 break;
1541         }
1542         case SLAPD_FILTER_COMPUTED:
1543                 f->f_result = filter->f_result;
1544                 break;
1545         default:
1546                 slapi_ch_free( (void **)&f );
1547                 f = NULL;
1548                 break;
1549         }
1550
1551         return f;
1552 }
1553
1554 int 
1555 slapi_filter_get_choice( Slapi_Filter *f )
1556 {
1557         int             rc;
1558
1559         if ( f != NULL ) {
1560                 rc = f->f_choice;
1561         } else {
1562                 rc = 0;
1563         }
1564
1565         return rc;
1566 }
1567
1568 int 
1569 slapi_filter_get_ava(
1570         Slapi_Filter    *f, 
1571         char            **type, 
1572         struct berval   **bval )
1573 {
1574         int             ftype;
1575         int             rc = LDAP_SUCCESS;
1576
1577         assert( type != NULL );
1578         assert( bval != NULL );
1579
1580         *type = NULL;
1581         *bval = NULL;
1582
1583         ftype = f->f_choice;
1584         if ( ftype == LDAP_FILTER_EQUALITY 
1585                         || ftype ==  LDAP_FILTER_GE 
1586                         || ftype == LDAP_FILTER_LE 
1587                         || ftype == LDAP_FILTER_APPROX ) {
1588                 /*
1589                  * According to the SLAPI Reference Manual these are
1590                  * not duplicated.
1591                  */
1592                 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1593                 *bval = &f->f_un.f_un_ava->aa_value;
1594         } else { /* filter type not supported */
1595                 rc = -1;
1596         }
1597
1598         return rc;
1599 }
1600
1601 Slapi_Filter *
1602 slapi_filter_list_first( Slapi_Filter *f )
1603 {
1604         int             ftype;
1605
1606         if ( f == NULL ) {
1607                 return NULL;
1608         }
1609
1610         ftype = f->f_choice;
1611         if ( ftype == LDAP_FILTER_AND
1612                         || ftype == LDAP_FILTER_OR
1613                         || ftype == LDAP_FILTER_NOT ) {
1614                 return (Slapi_Filter *)f->f_list;
1615         } else {
1616                 return NULL;
1617         }
1618 }
1619
1620 Slapi_Filter *
1621 slapi_filter_list_next(
1622         Slapi_Filter    *f, 
1623         Slapi_Filter    *fprev )
1624 {
1625         int             ftype;
1626
1627         if ( f == NULL ) {
1628                 return NULL;
1629         }
1630
1631         ftype = f->f_choice;
1632         if ( ftype == LDAP_FILTER_AND
1633                         || ftype == LDAP_FILTER_OR
1634                         || ftype == LDAP_FILTER_NOT )
1635         {
1636                 return fprev->f_next;
1637         }
1638
1639         return NULL;
1640 }
1641
1642 int
1643 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1644 {
1645         if ( f == NULL ) {
1646                 return -1;
1647         }
1648
1649         switch ( f->f_choice ) {
1650         case LDAP_FILTER_GE:
1651         case LDAP_FILTER_LE:
1652         case LDAP_FILTER_EQUALITY:
1653         case LDAP_FILTER_APPROX:
1654                 *type = f->f_av_desc->ad_cname.bv_val;
1655                 break;
1656         case LDAP_FILTER_SUBSTRINGS:
1657                 *type = f->f_sub_desc->ad_cname.bv_val;
1658                 break;
1659         case LDAP_FILTER_PRESENT:
1660                 *type = f->f_desc->ad_cname.bv_val;
1661                 break;
1662         case LDAP_FILTER_EXT:
1663                 *type = f->f_mr_desc->ad_cname.bv_val;
1664                 break;
1665         default:
1666                 /* Complex filters need not apply. */
1667                 *type = NULL;
1668                 return -1;
1669         }
1670
1671         return 0;
1672 }
1673
1674 int
1675 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
1676 {
1677         AttributeDescription **adp, *ad = NULL;
1678         const char *text;
1679         int rc;
1680
1681         if ( f == NULL ) {
1682                 return -1;
1683         }
1684
1685         switch ( f->f_choice ) {
1686         case LDAP_FILTER_GE:
1687         case LDAP_FILTER_LE:
1688         case LDAP_FILTER_EQUALITY:
1689         case LDAP_FILTER_APPROX:
1690                 adp = &f->f_av_desc;
1691                 break;
1692         case LDAP_FILTER_SUBSTRINGS:
1693                 adp = &f->f_sub_desc;
1694                 break;
1695         case LDAP_FILTER_PRESENT:
1696                 adp = &f->f_desc;
1697                 break;
1698         case LDAP_FILTER_EXT:
1699                 adp = &f->f_mr_desc;
1700                 break;
1701         default:
1702                 /* Complex filters need not apply. */
1703                 return -1;
1704         }
1705
1706         rc = slap_str2ad( type, &ad, &text );
1707         if ( rc == LDAP_SUCCESS )
1708                 *adp = ad;
1709
1710         return ( rc == LDAP_SUCCESS ) ? 0 : -1;
1711 }
1712
1713 int
1714 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1715         char ***any, char **final )
1716 {
1717         int i;
1718
1719         if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1720                 return -1;
1721         }
1722
1723         /*
1724          * The caller shouldn't free but we can't return an
1725          * array of char *s from an array of bervals without
1726          * allocating memory, so we may as well be consistent.
1727          * XXX
1728          */
1729         *type = f->f_sub_desc->ad_cname.bv_val;
1730         *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1731         if ( f->f_sub_any != NULL ) {
1732                 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1733                         ;
1734                 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1735                 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1736                         (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1737                 }
1738                 (*any)[i] = NULL;
1739         } else {
1740                 *any = NULL;
1741         }
1742         *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1743
1744         return 0;
1745 }
1746
1747 Slapi_Filter *
1748 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1749 {
1750         Slapi_Filter *f = NULL;
1751
1752         if ( ftype == LDAP_FILTER_AND ||
1753              ftype == LDAP_FILTER_OR ||
1754              ftype == LDAP_FILTER_NOT )
1755         {
1756                 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1757                 f->f_choice = ftype;
1758                 f->f_list = f1;
1759                 f->f_list->f_next = f2;
1760                 f->f_next = NULL;
1761         }
1762
1763         return f;
1764 }
1765
1766 int
1767 slapi_x_filter_append( int ftype,
1768         Slapi_Filter **pContainingFilter, /* NULL on first call */
1769         Slapi_Filter **pNextFilter,
1770         Slapi_Filter *filterToAppend )
1771 {
1772         if ( ftype == LDAP_FILTER_AND ||
1773              ftype == LDAP_FILTER_OR ||
1774              ftype == LDAP_FILTER_NOT )
1775         {
1776                 if ( *pContainingFilter == NULL ) {
1777                         *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1778                         (*pContainingFilter)->f_choice = ftype;
1779                         (*pContainingFilter)->f_list = filterToAppend;
1780                         (*pContainingFilter)->f_next = NULL;
1781                 } else {
1782                         if ( (*pContainingFilter)->f_choice != ftype ) {
1783                                 /* Sanity check */
1784                                 return -1;
1785                         }
1786                         (*pNextFilter)->f_next = filterToAppend;
1787                 }
1788                 *pNextFilter = filterToAppend;
1789
1790                 return 0;
1791         }
1792         return -1;
1793 }
1794
1795 int
1796 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1797         int verify_access )
1798 {
1799         Operation *op;
1800         int rc;
1801
1802         if ( f == NULL ) {
1803                 /* spec says return zero if no filter. */
1804                 return 0;
1805         }
1806
1807         if ( verify_access ) {
1808                 op = pb->pb_op;
1809                 if ( op == NULL )
1810                         return LDAP_PARAM_ERROR;
1811         } else {
1812                 op = NULL;
1813         }
1814
1815         /*
1816          * According to acl.c it is safe to call test_filter() with
1817          * NULL arguments...
1818          */
1819         rc = test_filter( op, e, f );
1820         switch (rc) {
1821         case LDAP_COMPARE_TRUE:
1822                 rc = 0;
1823                 break;
1824         case LDAP_COMPARE_FALSE:
1825                 break;
1826         case SLAPD_COMPARE_UNDEFINED:
1827                 rc = LDAP_OTHER;
1828                 break;
1829         case LDAP_PROTOCOL_ERROR:
1830                 /* filter type unknown: spec says return -1 */
1831                 rc = -1;
1832                 break;
1833         }
1834
1835         return rc;
1836 }
1837
1838 int
1839 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1840 {
1841         return slapi_filter_test( NULL, e, f, 0 );
1842 }
1843
1844 int
1845 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1846 {
1847         switch ( f->f_choice ) {
1848         case LDAP_FILTER_AND:
1849         case LDAP_FILTER_NOT:
1850         case LDAP_FILTER_OR: {
1851                 int rc;
1852
1853                 /*
1854                  * FIXME: altering f; should we use a temporary?
1855                  */
1856                 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1857                         rc = slapi_filter_apply( f, fn, arg, error_code );
1858                         if ( rc != 0 ) {
1859                                 return rc;
1860                         }
1861                         if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1862                                 break;
1863                         }
1864                 }
1865                 break;
1866         }
1867         case LDAP_FILTER_EQUALITY:
1868         case LDAP_FILTER_SUBSTRINGS:
1869         case LDAP_FILTER_GE:
1870         case LDAP_FILTER_LE:
1871         case LDAP_FILTER_PRESENT:
1872         case LDAP_FILTER_APPROX:
1873         case LDAP_FILTER_EXT:
1874                 *error_code = fn( f, arg );
1875                 break;
1876         default:
1877                 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1878         }
1879
1880         if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1881              *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1882                 return 0;
1883         }
1884
1885         return -1;
1886 }
1887
1888 int 
1889 slapi_pw_find(
1890         struct berval   **vals, 
1891         struct berval   *v ) 
1892 {
1893         /*
1894          * FIXME: what's the point?
1895          */
1896         return 1;
1897 }
1898
1899 #define MAX_HOSTNAME 512
1900
1901 char *
1902 slapi_get_hostname( void ) 
1903 {
1904         char            *hn = NULL;
1905         static int      been_here = 0;   
1906         static char     *static_hn = NULL;
1907
1908         ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1909         if ( !been_here ) {
1910                 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1911                 if ( static_hn == NULL) {
1912                         slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
1913                                         "Cannot allocate memory for hostname\n" );
1914                         static_hn = NULL;
1915                         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1916
1917                         return hn;
1918                         
1919                 } else { 
1920                         if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1921                                 slapi_log_error( SLAPI_LOG_FATAL,
1922                                                 "SLAPI",
1923                                                 "can't get hostname\n" );
1924                                 slapi_ch_free( (void **)&static_hn );
1925                                 static_hn = NULL;
1926                                 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1927
1928                                 return hn;
1929
1930                         } else {
1931                                 been_here = 1;
1932                         }
1933                 }
1934         }
1935         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1936         
1937         hn = ch_strdup( static_hn );
1938
1939         return hn;
1940 }
1941
1942 /*
1943  * FIXME: this should go in an appropriate header ...
1944  */
1945 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
1946
1947 int 
1948 slapi_log_error(
1949         int             severity, 
1950         char            *subsystem, 
1951         char            *fmt, 
1952         ... ) 
1953 {
1954         int             rc = LDAP_SUCCESS;
1955         va_list         arglist;
1956
1957         va_start( arglist, fmt );
1958         rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
1959         va_end( arglist );
1960
1961         return rc;
1962 }
1963
1964
1965 unsigned long
1966 slapi_timer_current_time( void ) 
1967 {
1968         static int      first_time = 1;
1969 #if !defined (_WIN32)
1970         struct timeval  now;
1971         unsigned long   ret;
1972
1973         ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1974         if (first_time) {
1975                 first_time = 0;
1976                 gettimeofday( &base_time, NULL );
1977         }
1978         gettimeofday( &now, NULL );
1979         ret = ( now.tv_sec  - base_time.tv_sec ) * 1000000 + 
1980                         (now.tv_usec - base_time.tv_usec);
1981         ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1982
1983         return ret;
1984
1985         /*
1986          * Ain't it better?
1987         return (slap_get_time() - starttime) * 1000000;
1988          */
1989 #else /* _WIN32 */
1990         LARGE_INTEGER now;
1991
1992         if ( first_time ) {
1993                 first_time = 0;
1994                 performance_counter_present = QueryPerformanceCounter( &base_time );
1995                 QueryPerformanceFrequency( &performance_freq );
1996         }
1997
1998         if ( !performance_counter_present )
1999              return 0;
2000
2001         QueryPerformanceCounter( &now );
2002         return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2003 #endif /* _WIN32 */
2004 }
2005
2006 /*
2007  * FIXME ?
2008  */
2009 unsigned long
2010 slapi_timer_get_time( char *label ) 
2011 {
2012         unsigned long start = slapi_timer_current_time();
2013         printf("%10ld %10d usec %s\n", start, 0, label);
2014         return start;
2015 }
2016
2017 /*
2018  * FIXME ?
2019  */
2020 void
2021 slapi_timer_elapsed_time(
2022         char *label,
2023         unsigned long start ) 
2024 {
2025         unsigned long stop = slapi_timer_current_time();
2026         printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2027 }
2028
2029 void
2030 slapi_free_search_results_internal( Slapi_PBlock *pb ) 
2031 {
2032         Slapi_Entry     **entries;
2033         int             k = 0, nEnt = 0;
2034
2035         slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2036         slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2037         if ( nEnt == 0 || entries == NULL ) {
2038                 return;
2039         }
2040
2041         for ( k = 0; k < nEnt; k++ ) {
2042                 slapi_entry_free( entries[k] );
2043                 entries[k] = NULL;
2044         }
2045         
2046         slapi_ch_free( (void **)&entries );
2047 }
2048
2049 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2050 {
2051         if ( pb == NULL )
2052                 return LDAP_PARAM_ERROR;
2053
2054         if ( pb->pb_conn == NULL )
2055                 return LDAP_PARAM_ERROR;
2056
2057 #ifdef HAVE_TLS
2058         *isSSL = pb->pb_conn->c_is_tls;
2059 #else
2060         *isSSL = 0;
2061 #endif
2062
2063         return LDAP_SUCCESS;
2064 }
2065
2066 /*
2067  * DS 5.x compatability API follow
2068  */
2069
2070 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2071 {
2072         AttributeType *at;
2073
2074         if ( attr == NULL )
2075                 return LDAP_PARAM_ERROR;
2076
2077         at = attr->a_desc->ad_type;
2078
2079         *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2080
2081         if ( is_at_single_value( at ) )
2082                 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2083         if ( is_at_operational( at ) )
2084                 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2085         if ( is_at_obsolete( at ) )
2086                 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2087         if ( is_at_collective( at ) )
2088                 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2089         if ( is_at_no_user_mod( at ) )
2090                 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2091
2092         return LDAP_SUCCESS;
2093 }
2094
2095 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2096 {
2097         unsigned long flags;
2098
2099         if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2100                 return 0;
2101         return (flags & flag) ? 1 : 0;
2102 }
2103
2104 Slapi_Attr *slapi_attr_new( void )
2105 {
2106         Attribute *ad;
2107
2108         ad = (Attribute  *)slapi_ch_calloc( 1, sizeof(*ad) );
2109
2110         return ad;
2111 }
2112
2113 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2114 {
2115         const char *text;
2116         AttributeDescription *ad = NULL;
2117
2118         if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2119                 return NULL;
2120         }
2121
2122         a->a_desc = ad;
2123         a->a_vals = NULL;
2124         a->a_nvals = NULL;
2125         a->a_next = NULL;
2126         a->a_flags = 0;
2127
2128         return a;
2129 }
2130
2131 void slapi_attr_free( Slapi_Attr **a )
2132 {
2133         attr_free( *a );
2134         *a = NULL;
2135 }
2136
2137 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2138 {
2139         return attr_dup( (Slapi_Attr *)attr );
2140 }
2141
2142 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2143 {
2144         struct berval nval;
2145         struct berval *nvalp;
2146         int rc;
2147         AttributeDescription *desc = a->a_desc;
2148
2149         if ( desc->ad_type->sat_equality &&
2150              desc->ad_type->sat_equality->smr_normalize ) {
2151                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2152                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2153                         desc->ad_type->sat_syntax,
2154                         desc->ad_type->sat_equality,
2155                         (Slapi_Value *)v, &nval, NULL );
2156                 if ( rc != LDAP_SUCCESS ) {
2157                         return rc;
2158                 }
2159                 nvalp = &nval;
2160         } else {
2161                 nvalp = NULL;
2162         }
2163
2164         rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2165         if ( rc == 0 && nvalp != NULL ) {
2166                 rc = value_add_one( &a->a_nvals, nvalp );
2167         } else {
2168                 a->a_nvals = a->a_vals;
2169         }
2170
2171         if ( nvalp != NULL ) {
2172                 slapi_ch_free_string( &nval.bv_val );
2173         }
2174
2175         return rc;
2176 }
2177
2178 int slapi_attr_type2plugin( const char *type, void **pi )
2179 {
2180         *pi = NULL;
2181
2182         return LDAP_OTHER;
2183 }
2184
2185 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2186 {
2187         if ( attr == NULL ) {
2188                 return LDAP_PARAM_ERROR;
2189         }
2190
2191         *type = attr->a_desc->ad_cname.bv_val;
2192
2193         return LDAP_SUCCESS;
2194 }
2195
2196 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2197 {
2198         if ( attr == NULL ) {
2199                 return LDAP_PARAM_ERROR;
2200         }
2201         *oidp = attr->a_desc->ad_type->sat_oid;
2202
2203         return LDAP_SUCCESS;
2204 }
2205
2206 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2207 {
2208         MatchingRule *mr;
2209         int ret;
2210         int rc;
2211         const char *text;
2212
2213         mr = a->a_desc->ad_type->sat_equality;
2214         rc = value_match( &ret, a->a_desc, mr,
2215                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2216                 (struct berval *)v1, (void *)v2, &text );
2217         if ( rc != LDAP_SUCCESS ) 
2218                 return -1;
2219
2220         return ( ret == 0 ) ? 0 : -1;
2221 }
2222
2223 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2224 {
2225         MatchingRule *mr;
2226         struct berval *bv;
2227         int j;
2228         const char *text;
2229         int rc;
2230         int ret;
2231
2232         if ( a ->a_vals == NULL ) {
2233                 return -1;
2234         }
2235         mr = a->a_desc->ad_type->sat_equality;
2236         for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2237                 rc = value_match( &ret, a->a_desc, mr,
2238                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2239                 if ( rc != LDAP_SUCCESS ) {
2240                         return -1;
2241                 }
2242                 if ( ret == 0 ) {
2243                         return 0;
2244                 }
2245         }
2246         return -1;
2247 }
2248
2249 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2250 {
2251         AttributeDescription *a1 = NULL;
2252         AttributeDescription *a2 = NULL;
2253         const char *text;
2254         int ret;
2255
2256         if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2257                 return -1;
2258         }
2259
2260         if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2261                 return 1;
2262         }
2263
2264 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2265         ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2266                 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2267
2268         switch ( opt ) {
2269         case SLAPI_TYPE_CMP_EXACT:
2270                 ret = ad_cmp( a1, a2 );
2271                 break;
2272         case SLAPI_TYPE_CMP_BASE:
2273                 ret = ad_base_cmp( a1, a2 );
2274                 break;
2275         case SLAPI_TYPE_CMP_SUBTYPE:
2276                 ret = is_ad_subtype( a2, a2 );
2277                 break;
2278         default:
2279                 ret = -1;
2280                 break;
2281         }
2282
2283         return ret;
2284 }
2285
2286 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2287 {
2288         return ( slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ) == 0 );
2289 }
2290
2291 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2292 {
2293         return slapi_valueset_first_value( &a->a_vals, v );
2294 }
2295
2296 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2297 {
2298         return slapi_valueset_next_value( &a->a_vals, hint, v );
2299 }
2300
2301 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2302 {
2303         *numValues = slapi_valueset_count( &a->a_vals );
2304
2305         return 0;
2306 }
2307
2308 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2309 {
2310         *vs = &((Slapi_Attr *)a)->a_vals;
2311
2312         return 0;
2313 }
2314
2315 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2316 {
2317         return slapi_attr_get_values( a, vals );
2318 }
2319
2320 char *slapi_attr_syntax_normalize( const char *s )
2321 {
2322         AttributeDescription *ad = NULL;
2323         const char *text;
2324
2325         if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2326                 return NULL;
2327         }
2328
2329         return ad->ad_cname.bv_val;
2330 }
2331
2332 Slapi_Value *slapi_value_new( void )
2333 {
2334         struct berval *bv;
2335
2336         bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2337
2338         return bv;
2339 }
2340
2341 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2342 {
2343         return ber_dupbv( NULL, (struct berval *)bval );
2344 }
2345
2346 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2347 {
2348         return slapi_value_new_berval( v );
2349 }
2350
2351 Slapi_Value *slapi_value_new_string(const char *s)
2352 {
2353         struct berval bv;
2354
2355         bv.bv_val = (char *)s;
2356         bv.bv_len = strlen( s );
2357
2358         return slapi_value_new_berval( &bv );
2359 }
2360
2361 Slapi_Value *slapi_value_init(Slapi_Value *val)
2362 {
2363         val->bv_val = NULL;
2364         val->bv_len = 0;
2365
2366         return val;
2367 }
2368
2369 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2370 {
2371         return ber_dupbv( v, bval );
2372 }
2373
2374 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2375 {
2376         v->bv_val = slapi_ch_strdup( s );
2377         v->bv_len = strlen( s );
2378
2379         return v;
2380 }
2381
2382 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2383 {
2384         return slapi_value_new_value( v );
2385 }
2386
2387 void slapi_value_free(Slapi_Value **value)
2388 {
2389         if ( value == NULL ) {
2390                 return;
2391         }
2392
2393         if ( (*value) != NULL ) {
2394                 slapi_ch_free( (void **)&(*value)->bv_val );
2395                 slapi_ch_free( (void **)value );
2396         }
2397 }
2398
2399 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2400 {
2401         return value;
2402 }
2403
2404 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2405 {
2406         if ( value == NULL ) {
2407                 return NULL;
2408         }
2409         if ( value->bv_val != NULL ) {
2410                 slapi_ch_free( (void **)&value->bv_val );
2411         }
2412         slapi_value_init_berval( value, (struct berval *)bval );
2413
2414         return value;
2415 }
2416
2417 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2418 {
2419         if ( value == NULL ) {
2420                 return NULL;
2421         }
2422         return slapi_value_set_berval( value, vfrom );
2423 }
2424
2425 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2426 {
2427         if ( value == NULL ) {
2428                 return NULL;
2429         }
2430         if ( value->bv_val != NULL ) {
2431                 slapi_ch_free( (void **)&value->bv_val );
2432         }
2433         value->bv_val = slapi_ch_malloc( len );
2434         value->bv_len = len;
2435         AC_MEMCPY( value->bv_val, val, len );
2436
2437         return value;
2438 }
2439
2440 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2441 {
2442         if ( value == NULL ) {
2443                 return -1;
2444         }
2445         slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2446         return 0;
2447 }
2448
2449 int slapi_value_set_int(Slapi_Value *value, int intVal)
2450 {
2451         char buf[64];
2452
2453         snprintf( buf, sizeof( buf ), "%d", intVal );
2454
2455         return slapi_value_set_string( value, buf );
2456 }
2457
2458 const char *slapi_value_get_string(const Slapi_Value *value)
2459 {
2460         if ( value == NULL ) return NULL;
2461         if ( value->bv_val == NULL ) return NULL;
2462         if ( !checkBVString( value ) ) return NULL;
2463
2464         return value->bv_val;
2465 }
2466
2467 int slapi_value_get_int(const Slapi_Value *value)
2468 {
2469         if ( value == NULL ) return 0;
2470         if ( value->bv_val == NULL ) return 0;
2471         if ( !checkBVString( value ) ) return 0;
2472
2473         return (int)strtol( value->bv_val, NULL, 10 );
2474 }
2475
2476 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2477 {
2478         if ( value == NULL ) return 0;
2479         if ( value->bv_val == NULL ) return 0;
2480         if ( !checkBVString( value ) ) return 0;
2481
2482         return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2483 }
2484
2485 long slapi_value_get_long(const Slapi_Value *value)
2486 {
2487         if ( value == NULL ) return 0;
2488         if ( value->bv_val == NULL ) return 0;
2489         if ( !checkBVString( value ) ) return 0;
2490
2491         return strtol( value->bv_val, NULL, 10 );
2492 }
2493
2494 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2495 {
2496         if ( value == NULL ) return 0;
2497         if ( value->bv_val == NULL ) return 0;
2498         if ( !checkBVString( value ) ) return 0;
2499
2500         return strtoul( value->bv_val, NULL, 10 );
2501 }
2502
2503 size_t slapi_value_get_length(const Slapi_Value *value)
2504 {
2505         if ( value == NULL )
2506                 return 0;
2507
2508         return (size_t) value->bv_len;
2509 }
2510
2511 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2512 {
2513         return slapi_attr_value_cmp( a, v1, v2 );
2514 }
2515
2516 /* A ValueSet is a container for a BerVarray. */
2517 Slapi_ValueSet *slapi_valueset_new( void )
2518 {
2519         Slapi_ValueSet *vs;
2520
2521         vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2522         *vs = NULL;
2523
2524         return vs;
2525 }
2526
2527 void slapi_valueset_free(Slapi_ValueSet *vs)
2528 {
2529         if ( vs != NULL ) {
2530                 BerVarray vp = *vs;
2531
2532                 ber_bvarray_free( vp );
2533                 slapi_ch_free( (void **)&vp );
2534
2535                 *vs = NULL;
2536         }
2537 }
2538
2539 void slapi_valueset_init(Slapi_ValueSet *vs)
2540 {
2541         if ( vs != NULL && *vs == NULL ) {
2542                 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2543                 (*vs)->bv_val = NULL;
2544                 (*vs)->bv_len = 0;
2545         }
2546 }
2547
2548 void slapi_valueset_done(Slapi_ValueSet *vs)
2549 {
2550         BerVarray vp;
2551
2552         if ( vs == NULL )
2553                 return;
2554
2555         for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2556                 vp->bv_len = 0;
2557                 slapi_ch_free( (void **)&vp->bv_val );
2558         }
2559         /* but don't free *vs or vs */
2560 }
2561
2562 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2563 {
2564         struct berval bv;
2565
2566         ber_dupbv( &bv, (Slapi_Value *)addval );
2567         ber_bvarray_add( vs, &bv );
2568 }
2569
2570 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2571 {
2572         return slapi_valueset_next_value( vs, 0, v );
2573 }
2574
2575 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2576 {
2577         int i;
2578         BerVarray vp;
2579
2580         if ( vs == NULL )
2581                 return -1;
2582
2583         vp = *vs;
2584
2585         for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2586                 if ( i == index ) {
2587                         *v = &vp[i];
2588                         return index + 1;
2589                 }
2590         }
2591
2592         return -1;
2593 }
2594
2595 int slapi_valueset_count( const Slapi_ValueSet *vs )
2596 {
2597         int i;
2598         BerVarray vp;
2599
2600         if ( vs == NULL )
2601                 return 0;
2602
2603         vp = *vs;
2604
2605         for ( i = 0; vp[i].bv_val != NULL; i++ )
2606                 ;
2607
2608         return i;
2609
2610 }
2611
2612 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2613 {
2614         BerVarray vp;
2615
2616         for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2617                 slapi_valueset_add_value( vs1, vp );
2618         }
2619 }
2620
2621 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2622         struct berval *val, int access )
2623 {
2624         int rc;
2625         slap_access_t slap_access;
2626         AttributeDescription *ad = NULL;
2627         const char *text;
2628
2629         rc = slap_str2ad( attr, &ad, &text );
2630         if ( rc != LDAP_SUCCESS ) {
2631                 return rc;
2632         }
2633
2634         /*
2635          * Whilst the SLAPI access types are arranged as a bitmask, the
2636          * documentation indicates that they are to be used separately.
2637          */
2638         switch ( access & SLAPI_ACL_ALL ) {
2639         case SLAPI_ACL_COMPARE:
2640                 slap_access = ACL_COMPARE;
2641                 break;
2642         case SLAPI_ACL_SEARCH:
2643                 slap_access = ACL_SEARCH;
2644                 break;
2645         case SLAPI_ACL_READ:
2646                 slap_access = ACL_READ;
2647                 break;
2648         case SLAPI_ACL_WRITE:
2649                 slap_access = ACL_WRITE;
2650                 break;
2651         case SLAPI_ACL_DELETE:
2652                 slap_access = ACL_WDEL;
2653                 break;
2654         case SLAPI_ACL_ADD:
2655                 slap_access = ACL_WADD;
2656                 break;
2657         case SLAPI_ACL_SELF:  /* not documented */
2658         case SLAPI_ACL_PROXY: /* not documented */
2659         default:
2660                 return LDAP_INSUFFICIENT_ACCESS;
2661                 break;
2662         }
2663
2664         assert( pb->pb_op != NULL );
2665
2666         if ( access_allowed( pb->pb_op, e, ad, val, slap_access, NULL ) ) {
2667                 return LDAP_SUCCESS;
2668         }
2669
2670         return LDAP_INSUFFICIENT_ACCESS;
2671 }
2672
2673 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2674 {
2675         int rc = LDAP_SUCCESS;
2676         Modifications *ml;
2677
2678         if ( pb == NULL || pb->pb_op == NULL )
2679                 return LDAP_PARAM_ERROR;
2680
2681         ml = slapi_int_ldapmods2modifications( mods );
2682         if ( ml == NULL ) {
2683                 return LDAP_OTHER;
2684         }
2685
2686         if ( rc == LDAP_SUCCESS ) {
2687                 rc = acl_check_modlist( pb->pb_op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2688         }
2689
2690         slap_mods_free( ml, 1 );
2691
2692         return rc;
2693 }
2694
2695 /*
2696  * Synthesise an LDAPMod array from a Modifications list to pass
2697  * to SLAPI.
2698  */
2699 LDAPMod **slapi_int_modifications2ldapmods( Modifications *modlist )
2700 {
2701         Modifications *ml;
2702         LDAPMod **mods, *modp;
2703         int i, j;
2704
2705         for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2706                 ;
2707
2708         mods = (LDAPMod **)slapi_ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2709
2710         for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2711                 mods[i] = (LDAPMod *)slapi_ch_malloc( sizeof(LDAPMod) );
2712                 modp = mods[i];
2713                 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2714                 if ( BER_BVISNULL( &ml->sml_type ) ) {
2715                         /* may happen for internally generated mods */
2716                         assert( ml->sml_desc != NULL );
2717                         modp->mod_type = slapi_ch_strdup( ml->sml_desc->ad_cname.bv_val );
2718                 } else {
2719                         modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );
2720                 }
2721
2722                 if ( ml->sml_values != NULL ) {
2723                         for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
2724                                 ;
2725                         modp->mod_bvalues = (struct berval **)slapi_ch_malloc( (j + 1) *
2726                                 sizeof(struct berval *) );
2727                         for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
2728                                 modp->mod_bvalues[j] = (struct berval *)slapi_ch_malloc(
2729                                                 sizeof(struct berval) );
2730                                 ber_dupbv( modp->mod_bvalues[j], &ml->sml_values[j] );
2731                         }
2732                         modp->mod_bvalues[j] = NULL;
2733                 } else {
2734                         modp->mod_bvalues = NULL;
2735                 }
2736                 i++;
2737         }
2738
2739         mods[i] = NULL;
2740
2741         return mods;
2742 }
2743
2744 /*
2745  * Convert a potentially modified array of LDAPMods back to a
2746  * Modification list. Unfortunately the values need to be
2747  * duplicated because slap_mods_check() will try to free them
2748  * before prettying (and we can't easily get out of calling
2749  * slap_mods_check() because we need normalized values).
2750  */
2751 Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods )
2752 {
2753         Modifications *modlist = NULL, **modtail;
2754         LDAPMod **modp;
2755         char textbuf[SLAP_TEXT_BUFLEN];
2756         const char *text;
2757
2758         if ( mods == NULL ) {
2759                 return NULL;
2760         }
2761
2762         modtail = &modlist;
2763
2764         for ( modp = mods; *modp != NULL; modp++ ) {
2765                 Modifications *mod;
2766                 LDAPMod *lmod = *modp;
2767                 int i;
2768                 const char *text;
2769                 AttributeDescription *ad = NULL;
2770
2771                 if ( slap_str2ad( lmod->mod_type, &ad, &text ) != LDAP_SUCCESS ) {
2772                         continue;
2773                 }
2774
2775                 mod = (Modifications *) slapi_ch_malloc( sizeof(Modifications) );
2776                 mod->sml_op = lmod->mod_op & ~(LDAP_MOD_BVALUES);
2777                 mod->sml_flags = 0;
2778                 mod->sml_type = ad->ad_cname;
2779                 mod->sml_desc = ad;
2780                 mod->sml_next = NULL;
2781
2782                 i = 0;
2783                 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2784                         if ( lmod->mod_bvalues != NULL ) {
2785                                 while ( lmod->mod_bvalues[i] != NULL )
2786                                         i++;
2787                         }
2788                 } else {
2789                         if ( lmod->mod_values != NULL ) {
2790                                 while ( lmod->mod_values[i] != NULL )
2791                                         i++;
2792                         }
2793                 }
2794
2795                 if ( i == 0 ) {
2796                         mod->sml_values = NULL;
2797                 } else {
2798                         mod->sml_values = (BerVarray) slapi_ch_malloc( (i + 1) * sizeof(struct berval) );
2799
2800                         /* NB: This implicitly trusts a plugin to return valid modifications. */
2801                         if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2802                                 for ( i = 0; lmod->mod_bvalues[i] != NULL; i++ ) {
2803                                         ber_dupbv( &mod->sml_values[i], lmod->mod_bvalues[i] );
2804                                 }
2805                         } else {
2806                                 for ( i = 0; lmod->mod_values[i] != NULL; i++ ) {
2807                                         mod->sml_values[i].bv_val = slapi_ch_strdup( lmod->mod_values[i] );
2808                                         mod->sml_values[i].bv_len = strlen( lmod->mod_values[i] );
2809                                 }
2810                         }
2811                         mod->sml_values[i].bv_val = NULL;
2812                         mod->sml_values[i].bv_len = 0;
2813                 }
2814                 mod->sml_nvalues = NULL;
2815
2816                 *modtail = mod;
2817                 modtail = &mod->sml_next;
2818         }
2819
2820         if ( slap_mods_check( modlist, &text, textbuf, sizeof( textbuf ), NULL ) != LDAP_SUCCESS ) {
2821                 slap_mods_free( modlist, 1 );
2822                 modlist = NULL;
2823         }
2824
2825         return modlist;
2826 }
2827
2828 /*
2829  * Sun ONE DS 5.x computed attribute support. Computed attributes
2830  * allow for dynamically generated operational attributes, a very
2831  * useful thing indeed.
2832  */
2833
2834 /*
2835  * For some reason Sun don't use the normal plugin mechanism
2836  * registration path to register an "evaluator" function (an
2837  * "evaluator" is responsible for adding computed attributes;
2838  * the nomenclature is somewhat confusing).
2839  *
2840  * As such slapi_compute_add_evaluator() registers the 
2841  * function directly.
2842  */
2843 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2844 {
2845         Slapi_PBlock *pPlugin = NULL;
2846         int rc;
2847         int type = SLAPI_PLUGIN_OBJECT;
2848
2849         pPlugin = slapi_pblock_new();
2850         if ( pPlugin == NULL ) {
2851                 rc = LDAP_NO_MEMORY;
2852                 goto done;
2853         }
2854
2855         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2856         if ( rc != LDAP_SUCCESS ) {
2857                 goto done;
2858         }
2859
2860         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2861         if ( rc != LDAP_SUCCESS ) {
2862                 goto done;
2863         }
2864
2865         rc = slapi_int_register_plugin( frontendDB, pPlugin );
2866         if ( rc != 0 ) {
2867                 rc = LDAP_OTHER;
2868                 goto done;
2869         }
2870
2871 done:
2872         if ( rc != LDAP_SUCCESS ) {
2873                 if ( pPlugin != NULL ) {
2874                         slapi_pblock_destroy( pPlugin );
2875                 }
2876                 return -1;
2877         }
2878
2879         return 0;
2880 }
2881
2882 /*
2883  * See notes above regarding slapi_compute_add_evaluator().
2884  */
2885 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2886 {
2887         Slapi_PBlock *pPlugin = NULL;
2888         int rc;
2889         int type = SLAPI_PLUGIN_OBJECT;
2890
2891         pPlugin = slapi_pblock_new();
2892         if ( pPlugin == NULL ) {
2893                 rc = LDAP_NO_MEMORY;
2894                 goto done;
2895         }
2896
2897         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2898         if ( rc != LDAP_SUCCESS ) {
2899                 goto done;
2900         }
2901
2902         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2903         if ( rc != LDAP_SUCCESS ) {
2904                 goto done;
2905         }
2906
2907         rc = slapi_int_register_plugin( frontendDB, pPlugin );
2908         if ( rc != 0 ) {
2909                 rc = LDAP_OTHER;
2910                 goto done;
2911         }
2912
2913 done:
2914         if ( rc != LDAP_SUCCESS ) {
2915                 if ( pPlugin != NULL ) {
2916                         slapi_pblock_destroy( pPlugin );
2917                 }
2918                 return -1;
2919         }
2920
2921         return 0;
2922 }
2923
2924 /*
2925  * Call compute evaluators
2926  */
2927 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2928 {
2929         int rc = 0;
2930         slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2931
2932         rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2933         if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2934                 /* Nothing to do; front-end should ignore. */
2935                 return 0;
2936         }
2937
2938         for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2939                 /*
2940                  * -1: no attribute matched requested type
2941                  *  0: one attribute matched
2942                  * >0: error happened
2943                  */
2944                 rc = (*pGetPlugin)( c, type, e, outputfn );
2945                 if ( rc > 0 ) {
2946                         break;
2947                 }
2948         }
2949
2950         slapi_ch_free( (void **)&tmpPlugin );
2951
2952         return rc;
2953 }
2954
2955 int
2956 compute_rewrite_search_filter( Slapi_PBlock *pb )
2957 {
2958         if ( pb == NULL || pb->pb_op == NULL )
2959                 return LDAP_PARAM_ERROR;
2960
2961         return slapi_int_call_plugins( pb->pb_op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
2962 }
2963
2964 /*
2965  * New API to provide the plugin with access to the search
2966  * pblock. Have informed Sun DS team.
2967  */
2968 int
2969 slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
2970 {
2971         if ( c == NULL )
2972                 return -1;
2973
2974         if ( c->cac_pb == NULL )
2975                 return -1;
2976
2977         *pb = c->cac_pb;
2978
2979         return 0;
2980 }
2981
2982 Slapi_Mutex *slapi_new_mutex( void )
2983 {
2984         Slapi_Mutex *m;
2985
2986         m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
2987         if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
2988                 slapi_ch_free( (void **)&m );
2989                 return NULL;
2990         }
2991
2992         return m;
2993 }
2994
2995 void slapi_destroy_mutex( Slapi_Mutex *mutex )
2996 {
2997         if ( mutex != NULL ) {
2998                 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
2999                 slapi_ch_free( (void **)&mutex);
3000         }
3001 }
3002
3003 void slapi_lock_mutex( Slapi_Mutex *mutex )
3004 {
3005         ldap_pvt_thread_mutex_lock( &mutex->mutex );
3006 }
3007
3008 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3009 {
3010         return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3011 }
3012
3013 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3014 {
3015         Slapi_CondVar *cv;
3016
3017         if ( mutex == NULL ) {
3018                 return NULL;
3019         }
3020
3021         cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3022         if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3023                 slapi_ch_free( (void **)&cv );
3024                 return NULL;
3025         }
3026
3027         cv->mutex = mutex->mutex;
3028
3029         return cv;
3030 }
3031
3032 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3033 {
3034         if ( cvar != NULL ) {
3035                 ldap_pvt_thread_cond_destroy( &cvar->cond );
3036                 slapi_ch_free( (void **)&cvar );
3037         }
3038 }
3039
3040 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3041 {
3042         if ( cvar == NULL ) {
3043                 return -1;
3044         }
3045
3046         return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3047 }
3048
3049 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3050 {
3051         if ( cvar == NULL ) {
3052                 return -1;
3053         }
3054
3055         if ( notify_all ) {
3056                 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3057         }
3058
3059         return ldap_pvt_thread_cond_signal( &cvar->cond );
3060 }
3061
3062 int slapi_int_access_allowed( Operation *op,
3063         Entry *entry,
3064         AttributeDescription *desc,
3065         struct berval *val,
3066         slap_access_t access,
3067         AccessControlState *state )
3068 {
3069         int rc, slap_access = 0;
3070         slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3071         Slapi_PBlock *pb;
3072
3073         pb = SLAPI_OPERATION_PBLOCK( op );
3074         if ( pb == NULL ) {
3075                 /* internal operation */
3076                 return 1;
3077         }
3078
3079         switch ( access ) {
3080         case ACL_COMPARE:
3081                 slap_access |= SLAPI_ACL_COMPARE;
3082                 break;
3083         case ACL_SEARCH:
3084                 slap_access |= SLAPI_ACL_SEARCH;
3085                 break;
3086         case ACL_READ:
3087                 slap_access |= SLAPI_ACL_READ;
3088                 break;
3089         case ACL_WRITE:
3090                 slap_access |= SLAPI_ACL_WRITE;
3091                 break;
3092         case ACL_WDEL:
3093                 slap_access |= SLAPI_ACL_DELETE;
3094                 break;
3095         case ACL_WADD:
3096                 slap_access |= SLAPI_ACL_ADD;
3097                 break;
3098         default:
3099                 break;
3100         }
3101
3102         rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3103         if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3104                 /* nothing to do; allowed access */
3105                 return 1;
3106         }
3107
3108         rc = 1; /* default allow policy */
3109
3110         for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3111                 /*
3112                  * 0    access denied
3113                  * 1    access granted
3114                  */
3115                 rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,
3116                                     val, slap_access, (void *)state );
3117                 if ( rc == 0 ) {
3118                         break;
3119                 }
3120         }
3121
3122         slapi_ch_free( (void **)&tmpPlugin );
3123
3124         return rc;
3125 }
3126
3127 /*
3128  * There is no documentation for this.
3129  */
3130 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
3131 {
3132         LDAPRDN lrdn;
3133         LDAPAVA *ava;
3134         int rc;
3135         char *p;
3136
3137         *type = NULL;
3138
3139         bv->bv_len = 0;
3140         bv->bv_val = NULL;
3141
3142         rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
3143         if ( rc != LDAP_SUCCESS ) {
3144                 return -1;
3145         }
3146
3147         if ( lrdn[1] != NULL ) {
3148                 return -1; /* not single valued */
3149         }
3150
3151         ava = lrdn[0];
3152
3153         *type = slapi_ch_strdup( ava->la_attr.bv_val );
3154         ber_dupbv( bv, &ava->la_value );
3155
3156         ldap_rdnfree(lrdn);
3157
3158         return 0;
3159 }
3160
3161 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
3162 {
3163         struct berval new_dn, parent_dn, newrdn;
3164
3165         new_dn.bv_val = NULL;
3166
3167         parent_dn.bv_val = (char *)dn;
3168         parent_dn.bv_len = strlen( dn );
3169
3170         newrdn.bv_val = (char *)rdn;
3171         newrdn.bv_len = strlen( rdn );
3172
3173         build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
3174
3175         return new_dn.bv_val;
3176 }
3177
3178 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
3179 {
3180         Backend *be_orig;
3181         const char *text;
3182         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
3183         size_t textlen = sizeof textbuf;
3184         int rc = LDAP_SUCCESS;
3185
3186         PBLOCK_ASSERT_OP( pb, 0 );
3187
3188         be_orig = pb->pb_op->o_bd;
3189
3190         pb->pb_op->o_bd = select_backend( &e->e_nname, 0, 0 );
3191         if ( pb->pb_op->o_bd != NULL ) {
3192                 rc = entry_schema_check( pb->pb_op, e, NULL, 0,
3193                         &text, textbuf, textlen );
3194         }
3195         pb->pb_op->o_bd = be_orig;
3196
3197         return ( rc == LDAP_SUCCESS ) ? 0 : 1;
3198 }
3199
3200 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
3201 {
3202         LDAPDN dn;
3203         int rc;
3204         int i = 0, match = 0;
3205
3206         rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3207         if ( rc != LDAP_SUCCESS ) {
3208                 return 0;
3209         }
3210
3211         if ( dn[0] != NULL ) {
3212                 LDAPRDN rdn = dn[0];
3213
3214                 for ( i = 0; rdn[i] != NULL; i++ ) {
3215                         LDAPAVA *ava = &rdn[0][i];
3216                         Slapi_Attr *a = NULL;
3217
3218                         if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
3219                              slapi_attr_value_find( a, &ava->la_value ) == 0 )
3220                                 match++;
3221                 }
3222         }
3223
3224         ldap_dnfree( dn );
3225
3226         return ( i == match );
3227 }
3228
3229 int slapi_entry_add_rdn_values( Slapi_Entry *e )
3230 {
3231         LDAPDN dn;
3232         int i, rc;
3233
3234         rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3235         if ( rc != LDAP_SUCCESS ) {
3236                 return rc;
3237         }
3238
3239         if ( dn[0] != NULL ) {
3240                 LDAPRDN rdn = dn[0];
3241                 struct berval *vals[2];
3242
3243                 for ( i = 0; rdn[i] != NULL; i++ ) {
3244                         LDAPAVA *ava = &rdn[0][i];
3245                         Slapi_Attr *a = NULL;
3246
3247                         if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
3248                              slapi_attr_value_find( a, &ava->la_value ) == 0 )
3249                                 continue;
3250
3251                         vals[0] = &ava->la_value;
3252                         vals[1] = NULL;
3253
3254                         slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
3255                 }
3256         }
3257
3258         ldap_dnfree( dn );
3259
3260         return LDAP_SUCCESS;
3261 }
3262
3263 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
3264 {
3265         Attribute *attr;
3266
3267         attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
3268         if ( attr == NULL ) {
3269                 return NULL;
3270         }
3271
3272         if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
3273                 return slapi_value_get_string( &attr->a_vals[0] );
3274         }
3275
3276         return NULL;
3277 }
3278
3279 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
3280 {
3281         struct berval bv;
3282
3283         attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
3284
3285         bv.bv_val = uniqueid;
3286         bv.bv_len = strlen( uniqueid );
3287         attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
3288 }
3289
3290 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
3291 {
3292         LDAP *ld;
3293         char *url;
3294         size_t size;
3295         int rc;
3296
3297         size = sizeof("ldap:///");
3298         if ( secure )
3299                 size++;
3300         size += strlen( ldaphost );
3301         if ( ldapport != 0 )
3302                 size += 32;
3303
3304         url = slapi_ch_malloc( size );
3305
3306         if ( ldapport != 0 ) {
3307                 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
3308         } else {
3309                 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
3310         }
3311
3312         rc = ldap_initialize( &ld, url );
3313
3314         slapi_ch_free_string( &url );
3315
3316         return ( rc == LDAP_SUCCESS ) ? ld : NULL;
3317 }
3318
3319 void slapi_ldap_unbind( LDAP *ld )
3320 {
3321         ldap_unbind_ext_s( ld, NULL, NULL );
3322 }
3323
3324 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
3325 {
3326         if ( be == NULL )
3327                 return LDAP_PARAM_ERROR;
3328
3329         *flags = SLAP_DBFLAGS(be);
3330
3331         return LDAP_SUCCESS;
3332 }
3333
3334 int
3335 slapi_int_count_controls( LDAPControl **ctrls )
3336 {
3337         size_t i;
3338
3339         if ( ctrls == NULL )
3340                 return 0;
3341
3342         for ( i = 0; ctrls[i] != NULL; i++ )
3343                 ;
3344
3345         return i;
3346 }
3347
3348 int
3349 slapi_op_abandoned( Slapi_PBlock *pb )
3350 {
3351         if ( pb->pb_op == NULL )
3352                 return 0;
3353
3354         return ( pb->pb_op->o_abandon );
3355 }
3356
3357 char *
3358 slapi_op_type_to_string(unsigned long type)
3359 {
3360         char *str;
3361
3362         switch (type) {
3363         case SLAPI_OPERATION_BIND:
3364                 str = "bind";
3365                 break;
3366         case SLAPI_OPERATION_UNBIND:
3367                 str = "unbind";
3368                 break;
3369         case SLAPI_OPERATION_SEARCH:
3370                 str = "search";
3371                 break;
3372         case SLAPI_OPERATION_MODIFY:
3373                 str = "modify";
3374                 break;
3375         case SLAPI_OPERATION_ADD:
3376                 str = "add";
3377                 break;
3378         case SLAPI_OPERATION_DELETE:
3379                 str = "delete";
3380                 break;
3381         case SLAPI_OPERATION_MODDN:
3382                 str = "modrdn";
3383                 break;
3384         case SLAPI_OPERATION_COMPARE:
3385                 str = "compare";
3386                 break;
3387         case SLAPI_OPERATION_ABANDON:
3388                 str = "abandon";
3389                 break;
3390         case SLAPI_OPERATION_EXTENDED:
3391                 str = "extended";
3392                 break;
3393         default:
3394                 str = "unknown operation type";
3395                 break;
3396         }
3397         return str;
3398 }
3399
3400 unsigned long
3401 slapi_op_get_type(Slapi_Operation * op)
3402 {
3403         unsigned long type;
3404
3405         switch ( op->o_tag ) {
3406         case LDAP_REQ_BIND:
3407                 type = SLAPI_OPERATION_BIND;
3408                 break;
3409         case LDAP_REQ_UNBIND:
3410                 type = SLAPI_OPERATION_UNBIND;
3411                 break;
3412         case LDAP_REQ_SEARCH:
3413                 type = SLAPI_OPERATION_SEARCH;
3414                 break;
3415         case LDAP_REQ_MODIFY:
3416                 type = SLAPI_OPERATION_MODIFY;
3417                 break;
3418         case LDAP_REQ_ADD:
3419                 type = SLAPI_OPERATION_ADD;
3420                 break;
3421         case LDAP_REQ_DELETE:
3422                 type = SLAPI_OPERATION_DELETE;
3423                 break;
3424         case LDAP_REQ_MODRDN:
3425                 type = SLAPI_OPERATION_MODDN;
3426                 break;
3427         case LDAP_REQ_COMPARE:
3428                 type = SLAPI_OPERATION_COMPARE;
3429                 break;
3430         case LDAP_REQ_ABANDON:
3431                 type = SLAPI_OPERATION_ABANDON;
3432                 break;
3433         case LDAP_REQ_EXTENDED:
3434                 type = SLAPI_OPERATION_EXTENDED;
3435                 break;
3436         default:
3437                 type = SLAPI_OPERATION_NONE;
3438                 break;
3439         }
3440         return type;
3441 }
3442
3443 void slapi_be_set_readonly( Slapi_Backend *be, int readonly )
3444 {
3445         if ( be == NULL )
3446                 return;
3447
3448         if ( readonly )
3449                 be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
3450         else
3451                 be->be_restrictops &= ~(SLAP_RESTRICT_OP_WRITES);
3452 }
3453
3454 int slapi_be_get_readonly( Slapi_Backend *be )
3455 {
3456         if ( be == NULL )
3457                 return 0;
3458
3459         return ( (be->be_restrictops & SLAP_RESTRICT_OP_WRITES) == SLAP_RESTRICT_OP_WRITES );
3460 }
3461
3462 const char *slapi_x_be_get_updatedn( Slapi_Backend *be )
3463 {
3464         if ( be == NULL )
3465                 return NULL;
3466
3467         return be->be_update_ndn.bv_val;
3468 }
3469
3470 Slapi_Backend *slapi_be_select( const Slapi_DN *sdn )
3471 {
3472         Slapi_Backend *be;
3473
3474         slapi_sdn_get_ndn( sdn );
3475
3476         be = select_backend( (struct berval *)&sdn->ndn, 0, 0 );
3477
3478         return be;
3479 }
3480
3481 #if 0
3482 void
3483 slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag)
3484 {
3485 }
3486
3487 void
3488 slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag)
3489 {
3490 }
3491
3492 int
3493 slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag)
3494 {
3495 }
3496 #endif
3497
3498 #endif /* LDAP_SLAPI */
3499