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