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