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