]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_utils.c
b3d08ca68d432f52c348b23ccfb6f9288ad31b5e
[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         Operation *op,
1206         SlapReply *rs,
1207         LDAPControl *ctrl )
1208 {
1209         /* Plugins must deal with controls themselves. */
1210
1211         return LDAP_SUCCESS;
1212 }
1213 #endif /* LDAP_SLAPI */
1214
1215 void 
1216 slapi_register_supported_control(
1217         char            *controloid, 
1218         unsigned long   controlops )
1219 {
1220 #ifdef LDAP_SLAPI
1221         slap_mask_t controlmask;
1222
1223         slapiControlOp2SlapControlMask( controlops, &controlmask );
1224
1225         register_supported_control( controloid, controlmask, NULL, parseSlapiControl );
1226 #endif /* LDAP_SLAPI */
1227 }
1228
1229 int 
1230 slapi_get_supported_controls(
1231         char            ***ctrloidsp, 
1232         unsigned long   **ctrlopsp ) 
1233 {
1234 #ifdef LDAP_SLAPI
1235         int i, rc;
1236
1237         rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1238         if ( rc != LDAP_SUCCESS ) {
1239                 return rc;
1240         }
1241
1242         for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1243                 /* In place, naughty. */
1244                 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1245         }
1246
1247         return LDAP_SUCCESS;
1248 #else /* LDAP_SLAPI */
1249         return 1;
1250 #endif /* LDAP_SLAPI */
1251 }
1252
1253 LDAPControl *
1254 slapi_dup_control( LDAPControl *ctrl )
1255 {
1256 #ifdef LDAP_SLAPI
1257         LDAPControl *ret;
1258
1259         ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1260         ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1261         ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1262         ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1263
1264         return ret;
1265 #else
1266         return NULL;
1267 #endif /* LDAP_SLAPI */
1268 }
1269
1270 void 
1271 slapi_register_supported_saslmechanism( char *mechanism )
1272 {
1273 #ifdef LDAP_SLAPI
1274         /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1275         slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1276                         "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1277 #endif /* LDAP_SLAPI */
1278 }
1279
1280 char **
1281 slapi_get_supported_saslmechanisms( void )
1282 {
1283 #ifdef LDAP_SLAPI
1284         /* FIXME -- can not get the saslmechanism without a connection. */
1285         slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SASL",
1286                         "can not get the SASL mechanism list "
1287                         "without a connection\n" );
1288         return NULL;
1289 #else /* LDAP_SLAPI */
1290         return NULL;
1291 #endif /* LDAP_SLAPI */
1292 }
1293
1294 char **
1295 slapi_get_supported_extended_ops( void )
1296 {
1297 #ifdef LDAP_SLAPI
1298         int             i, j, k;
1299         char            **ppExtOpOID = NULL;
1300         int             numExtOps = 0;
1301
1302         for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1303                 ;
1304         }
1305         
1306         for ( j = 0; ns_get_supported_extop( j ) != NULL; j++ ) {
1307                 ;
1308         }
1309
1310         numExtOps = i + j;
1311         if ( numExtOps == 0 ) {
1312                 return NULL;
1313         }
1314
1315         ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1316         for ( k = 0; k < i; k++ ) {
1317                 struct berval   *bv;
1318
1319                 bv = get_supported_extop( k );
1320                 assert( bv != NULL );
1321
1322                 ppExtOpOID[ k ] = bv->bv_val;
1323         }
1324         
1325         for ( ; k < j; k++ ) {
1326                 struct berval   *bv;
1327
1328                 bv = ns_get_supported_extop( k );
1329                 assert( bv != NULL );
1330
1331                 ppExtOpOID[ i + k ] = bv->bv_val;
1332         }
1333         ppExtOpOID[ i + k ] = NULL;
1334
1335         return ppExtOpOID;
1336 #else /* LDAP_SLAPI */
1337         return NULL;
1338 #endif /* LDAP_SLAPI */
1339 }
1340
1341 void 
1342 slapi_send_ldap_result(
1343         Slapi_PBlock    *pb, 
1344         int             err, 
1345         char            *matched, 
1346         char            *text, 
1347         int             nentries, 
1348         struct berval   **urls ) 
1349 {
1350 #ifdef LDAP_SLAPI
1351         Operation       *op;
1352         struct berval   *s;
1353         char            *extOID = NULL;
1354         struct berval   *extValue = NULL;
1355         int             rc;
1356         SlapReply       rs;
1357
1358         slapi_pblock_get( pb, SLAPI_OPERATION, &op );
1359
1360         rs.sr_err = err;
1361         rs.sr_matched = matched;
1362         rs.sr_text = text;
1363         rs.sr_ref = NULL;
1364         rs.sr_ctrls = NULL;
1365
1366         if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1367                 rs.sr_type = REP_SASL;
1368                 slapi_pblock_get( pb, SLAPI_BIND_RET_SASLCREDS, (void *) &rs.sr_sasldata );
1369
1370                 send_ldap_sasl( op, &rs );
1371                 return;
1372         }
1373
1374         slapi_pblock_get( pb, SLAPI_EXT_OP_RET_OID, &extOID );
1375         if ( extOID != NULL ) {
1376                 rs.sr_type = REP_EXTENDED;
1377                 rs.sr_rspoid = extOID;
1378                 slapi_pblock_get( pb, SLAPI_EXT_OP_RET_VALUE, &rs.sr_rspdata );
1379                 send_ldap_response( op, &rs );
1380                 return;
1381         }
1382
1383         rs.sr_type = REP_RESULT;
1384         slapi_pblock_get( pb, SLAPI_RESCONTROLS, &rs.sr_ctrls );
1385
1386         send_ldap_result( op, &rs );
1387 #endif /* LDAP_SLAPI */
1388 }
1389
1390 int 
1391 slapi_send_ldap_search_entry(
1392         Slapi_PBlock    *pb, 
1393         Slapi_Entry     *e, 
1394         LDAPControl     **ectrls, 
1395         char            **attrs, 
1396         int             attrsonly )
1397 {
1398 #ifdef LDAP_SLAPI
1399         Backend         *be;
1400         Connection      *pConn;
1401         Operation       *pOp;
1402         int             rc;
1403         SlapReply       rs;
1404         int             i;
1405         AttributeName   *an = NULL;
1406         const char      *text;
1407
1408         for ( i = 0; attrs[ i ] != NULL; i++ ) {
1409                 ; /* empty */
1410         }
1411
1412         if ( i > 0 ) {
1413                 an = (AttributeName *) ch_malloc( (i+1) * sizeof(AttributeName) );
1414                 for ( i = 0; attrs[i] != NULL; i++ ) {
1415                         an[i].an_name.bv_val = ch_strdup( attrs[i] );
1416                         an[i].an_name.bv_len = strlen( attrs[i] );
1417                         an[i].an_desc = NULL;
1418                         if( slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text ) != LDAP_SUCCESS)
1419                                 return -1;
1420                 }
1421                 an[i].an_name.bv_len = 0;
1422                 an[i].an_name.bv_val = NULL;
1423         }
1424
1425         rs.sr_type = REP_SEARCH;
1426         rs.sr_err = LDAP_SUCCESS;
1427         rs.sr_matched = NULL;
1428         rs.sr_text = NULL;
1429         rs.sr_ref = NULL;
1430         rs.sr_ctrls = ectrls;
1431         rs.sr_attrs = an;
1432         rs.sr_entry = e;
1433         rs.sr_v2ref = NULL;
1434
1435         if ( rc = slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&pOp) != 0 ) {
1436                 rc = LDAP_OTHER;
1437         } else {
1438                 rc = send_search_entry( pOp, &rs );
1439         }
1440
1441         return rc;
1442
1443 #else /* LDAP_SLAPI */
1444         return -1;
1445 #endif /* LDAP_SLAPI */
1446 }
1447
1448
1449 Slapi_Filter *
1450 slapi_str2filter( char *str ) 
1451 {
1452 #ifdef LDAP_SLAPI
1453         return str2filter( str );
1454 #else /* LDAP_SLAPI */
1455         return NULL;
1456 #endif /* LDAP_SLAPI */
1457 }
1458
1459 void 
1460 slapi_filter_free(
1461         Slapi_Filter    *f, 
1462         int             recurse ) 
1463 {
1464 #ifdef LDAP_SLAPI
1465         filter_free( f );
1466 #endif /* LDAP_SLAPI */
1467 }
1468
1469 Slapi_Filter *
1470 slapi_filter_dup( Slapi_Filter *filter )
1471 {
1472 #ifdef LDAP_SLAPI
1473         Filter *f;
1474
1475         f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1476         f->f_next = NULL;
1477         f->f_choice = filter->f_choice;
1478
1479         switch ( f->f_choice ) {
1480         case LDAP_FILTER_AND:
1481         case LDAP_FILTER_NOT:
1482         case LDAP_FILTER_OR: {
1483                 Filter *pFilter, **ppF;
1484
1485                 for ( pFilter = filter->f_list, ppF = &f->f_list;
1486                       pFilter != NULL;
1487                       pFilter = pFilter->f_next, ppF = &f->f_next )
1488                 {
1489                         *ppF = slapi_filter_dup( pFilter );
1490                         if ( *ppF == NULL )
1491                                 break;
1492                 }
1493                 break;
1494         }
1495         case LDAP_FILTER_PRESENT:
1496                 f->f_desc = filter->f_desc;
1497                 break;
1498         case LDAP_FILTER_EQUALITY:
1499         case LDAP_FILTER_GE:
1500         case LDAP_FILTER_LE:
1501         case LDAP_FILTER_APPROX:
1502                 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1503                 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1504                 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1505                 break;
1506         case LDAP_FILTER_EXT:
1507                 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1508                 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1509                 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1510                 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1511                 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1512                 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1513         case LDAP_FILTER_SUBSTRINGS: {
1514                 int i;
1515
1516                 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1517                 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1518                 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1519                         ;
1520                 f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1521                 for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1522                         ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1523                 }
1524                 f->f_sub_any[i].bv_val = NULL;
1525                 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1526                 break;
1527         }
1528         case SLAPD_FILTER_COMPUTED:
1529                 f->f_result = filter->f_result;
1530                 break;
1531         default:
1532                 slapi_ch_free( (void **)&f );
1533                 f = NULL;
1534                 break;
1535         }
1536
1537         return f;
1538 #else
1539         return NULL;
1540 #endif /* LDAP_SLAPI */
1541 }
1542
1543 int 
1544 slapi_filter_get_choice( Slapi_Filter *f )
1545 {
1546 #ifdef LDAP_SLAPI
1547         int             rc;
1548
1549         if ( f != NULL ) {
1550                 rc = f->f_choice;
1551         } else {
1552                 rc = 0;
1553         }
1554
1555         return rc;
1556 #else /* LDAP_SLAPI */
1557         return -1;              /* invalid filter type */
1558 #endif /* LDAP_SLAPI */
1559 }
1560
1561 int 
1562 slapi_filter_get_ava(
1563         Slapi_Filter    *f, 
1564         char            **type, 
1565         struct berval   **bval )
1566 {
1567 #ifdef LDAP_SLAPI
1568         int             ftype;
1569         int             rc = LDAP_SUCCESS;
1570
1571         assert( type != NULL );
1572         assert( bval != NULL );
1573
1574         *type = NULL;
1575         *bval = NULL;
1576
1577         ftype = f->f_choice;
1578         if ( ftype == LDAP_FILTER_EQUALITY 
1579                         || ftype ==  LDAP_FILTER_GE 
1580                         || ftype == LDAP_FILTER_LE 
1581                         || ftype == LDAP_FILTER_APPROX ) {
1582                 /*
1583                  * According to the SLAPI Reference Manual these are
1584                  * not duplicated.
1585                  */
1586                 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1587                 *bval = &f->f_un.f_un_ava->aa_value;
1588         } else { /* filter type not supported */
1589                 rc = -1;
1590         }
1591
1592         return rc;
1593 #else /* LDAP_SLAPI */
1594         return -1;
1595 #endif /* LDAP_SLAPI */
1596 }
1597
1598 Slapi_Filter *
1599 slapi_filter_list_first( Slapi_Filter *f )
1600 {
1601 #ifdef LDAP_SLAPI
1602         int             ftype;
1603
1604         if ( f == NULL ) {
1605                 return NULL;
1606         }
1607
1608         ftype = f->f_choice;
1609         if ( ftype == LDAP_FILTER_AND
1610                         || ftype == LDAP_FILTER_OR
1611                         || ftype == LDAP_FILTER_NOT ) {
1612                 return (Slapi_Filter *)f->f_and;
1613         } else {
1614                 return NULL;
1615         }
1616 #else /* LDAP_SLAPI */
1617         return NULL;
1618 #endif /* LDAP_SLAPI */
1619 }
1620
1621 Slapi_Filter *
1622 slapi_filter_list_next(
1623         Slapi_Filter    *f, 
1624         Slapi_Filter    *fprev )
1625 {
1626 #ifdef LDAP_SLAPI
1627         int             ftype;
1628
1629         if ( f == NULL ) {
1630                 return NULL;
1631         }
1632
1633         ftype = f->f_choice;
1634         if ( ftype == LDAP_FILTER_AND
1635                         || ftype == LDAP_FILTER_OR
1636                         || ftype == LDAP_FILTER_NOT )
1637         {
1638                 return fprev->f_next;
1639         }
1640
1641         return NULL;
1642 #else /* LDAP_SLAPI */
1643         return NULL;
1644 #endif /* LDAP_SLAPI */
1645 }
1646
1647 int
1648 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1649 {
1650 #ifdef LDAP_SLAPI
1651         if ( f == NULL ) {
1652                 return -1;
1653         }
1654
1655         switch ( f->f_choice ) {
1656         case LDAP_FILTER_GE:
1657         case LDAP_FILTER_LE:
1658         case LDAP_FILTER_EQUALITY:
1659         case LDAP_FILTER_APPROX:
1660                 *type = f->f_av_desc->ad_cname.bv_val;
1661                 break;
1662         case LDAP_FILTER_SUBSTRINGS:
1663                 *type = f->f_sub_desc->ad_cname.bv_val;
1664                 break;
1665         case LDAP_FILTER_PRESENT:
1666                 *type = f->f_desc->ad_cname.bv_val;
1667                 break;
1668         case LDAP_FILTER_EXT:
1669                 *type = f->f_mr_desc->ad_cname.bv_val;
1670                 break;
1671         default:
1672                 /* Complex filters need not apply. */
1673                 *type = NULL;
1674                 return -1;
1675         }
1676
1677         return 0;
1678 #else
1679         return -1;
1680 #endif /* LDAP_SLAPI */
1681 }
1682
1683 int
1684 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1685         char ***any, char **final )
1686 {
1687 #ifdef LDAP_SLAPI
1688         int i;
1689
1690         if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1691                 return -1;
1692         }
1693
1694         /*
1695          * The caller shouldn't free but we can't return an
1696          * array of char *s from an array of bervals without
1697          * allocating memory, so we may as well be consistent.
1698          * XXX
1699          */
1700         *type = f->f_sub_desc->ad_cname.bv_val;
1701         *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1702         for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1703                 ;
1704         *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1705         for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1706                 (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1707         }
1708         (*any)[i] = NULL;
1709         *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1710
1711         return 0;
1712 #else
1713         return -1;
1714 #endif /* LDAP_SLAPI */
1715 }
1716
1717 Slapi_Filter *
1718 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2)
1719 {
1720 #ifdef LDAP_SLAPI
1721         Slapi_Filter *f = NULL;
1722
1723         if ( ftype == LDAP_FILTER_AND ||
1724              ftype == LDAP_FILTER_OR ||
1725              ftype == LDAP_FILTER_NOT )
1726         {
1727                 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1728                 f->f_choice = ftype;
1729                 f->f_list = f1;
1730                 f->f_next = f2;
1731         }
1732
1733         return f;
1734 #else
1735         return NULL;
1736 #endif /* LDAP_SLAPI */
1737 }
1738
1739 int
1740 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1741         int verify_access )
1742 {
1743 #ifdef LDAP_SLAPI
1744         Backend *be = NULL;
1745         Connection *conn;
1746         Operation *op;
1747         int rc;
1748
1749         if ( f == NULL ) {
1750                 /* spec says return zero if no filter. */
1751                 return 0;
1752         }
1753
1754         if ( verify_access ) {
1755                 (void) slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be);
1756                 rc = slapi_pblock_get(pb, SLAPI_CONNECTION, (void *)&conn);
1757                 if ( rc != 0 ) {
1758                         return LDAP_PARAM_ERROR;
1759                 }
1760                 rc = slapi_pblock_get(pb, SLAPI_OPERATION, (void *)&op);
1761                 if ( rc != 0 ) {
1762                         return LDAP_PARAM_ERROR;
1763                 }
1764         } else {
1765                 conn = NULL;
1766                 op = NULL;
1767         }
1768         /*
1769          * According to acl.c it is safe to call test_filter() with
1770          * NULL arguments...
1771          */
1772         rc = test_filter( op, e, f );
1773         switch (rc) {
1774         case LDAP_COMPARE_TRUE:
1775                 rc = 0;
1776                 break;
1777         case LDAP_COMPARE_FALSE:
1778                 break;
1779         case SLAPD_COMPARE_UNDEFINED:
1780                 rc = LDAP_OTHER;
1781                 break;
1782         case LDAP_PROTOCOL_ERROR:
1783                 /* filter type unknown: spec says return -1 */
1784                 rc = -1;
1785                 break;
1786         }
1787
1788         return rc;
1789 #else
1790         return -1;
1791 #endif /* LDAP_SLAPI */
1792 }
1793
1794 int
1795 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1796 {
1797 #ifdef LDAP_SLAPI
1798         return slapi_filter_test( NULL, e, f, 0 );
1799 #else
1800         return -1;
1801 #endif
1802 }
1803
1804 int
1805 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1806 {
1807 #ifdef LDAP_SLAPI
1808         switch ( f->f_choice ) {
1809         case LDAP_FILTER_AND:
1810         case LDAP_FILTER_NOT:
1811         case LDAP_FILTER_OR: {
1812                 int rc;
1813
1814                 /*
1815                  * FIXME: altering f; should we use a temporary?
1816                  */
1817                 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1818                         rc = slapi_filter_apply( f, fn, arg, error_code );
1819                         if ( rc != 0 ) {
1820                                 return rc;
1821                         }
1822                         if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1823                                 break;
1824                         }
1825                 }
1826                 break;
1827         }
1828         case LDAP_FILTER_EQUALITY:
1829         case LDAP_FILTER_SUBSTRINGS:
1830         case LDAP_FILTER_GE:
1831         case LDAP_FILTER_LE:
1832         case LDAP_FILTER_PRESENT:
1833         case LDAP_FILTER_APPROX:
1834         case LDAP_FILTER_EXT:
1835                 *error_code = fn( f, arg );
1836                 break;
1837         default:
1838                 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1839         }
1840
1841         if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1842              *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1843                 return 0;
1844         }
1845
1846         return -1;
1847 #else
1848         *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1849         return -1;
1850 #endif /* LDAP_SLAPI */
1851 }
1852
1853 int 
1854 slapi_send_ldap_extended_response(
1855         Connection      *conn, 
1856         Operation       *op,
1857         int             errornum, 
1858         char            *respName,
1859         struct berval   *response )
1860 {
1861 #ifdef LDAP_SLAPI
1862         SlapReply       rs;
1863
1864         rs.sr_type = REP_EXTENDED;
1865         rs.sr_err = errornum;
1866         rs.sr_matched = NULL;
1867         rs.sr_text = NULL;
1868         rs.sr_ref = NULL;
1869         rs.sr_ctrls = NULL;
1870         rs.sr_rspoid = respName;
1871         rs.sr_rspdata = response;
1872
1873         send_ldap_extended( op, &rs );
1874
1875         return LDAP_SUCCESS;
1876 #else /* LDAP_SLAPI */
1877         return -1;
1878 #endif /* LDAP_SLAPI */
1879 }
1880
1881 int 
1882 slapi_pw_find(
1883         struct berval   **vals, 
1884         struct berval   *v ) 
1885 {
1886 #ifdef LDAP_SLAPI
1887         /*
1888          * FIXME: what's the point?
1889          */
1890         return 1;
1891 #else /* LDAP_SLAPI */
1892         return 1;
1893 #endif /* LDAP_SLAPI */
1894 }
1895
1896 #define MAX_HOSTNAME 512
1897
1898 char *
1899 slapi_get_hostname( void ) 
1900 {
1901 #ifdef LDAP_SLAPI
1902         char            *hn = NULL;
1903
1904         /*
1905          * FIXME: I'd prefer a different check ...
1906          */
1907 #if defined _SPARC 
1908         hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1909         if ( hn == NULL) {
1910                 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1911                                 "can't malloc memory for hostname\n" );
1912                 hn = NULL;
1913                 
1914         } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1915                 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1916                                 "can't get hostname\n" );
1917                 slapi_ch_free( (void **)&hn );
1918                 hn = NULL;
1919         }
1920 #else /* !_SPARC */
1921         static int      been_here = 0;   
1922         static char     *static_hn = NULL;
1923
1924         ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1925         if ( !been_here ) {
1926                 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1927                 if ( static_hn == NULL) {
1928                         slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1929                                         "can't malloc memory for hostname\n" );
1930                         static_hn = NULL;
1931                         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1932
1933                         return hn;
1934                         
1935                 } else { 
1936                         if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1937                                 slapi_log_error( SLAPI_LOG_FATAL,
1938                                                 "SLAPI_SYSINFO",
1939                                                 "can't get hostname\n" );
1940                                 slapi_ch_free( (void **)&static_hn );
1941                                 static_hn = NULL;
1942                                 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1943
1944                                 return hn;
1945
1946                         } else {
1947                                 been_here = 1;
1948                         }
1949                 }
1950         }
1951         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1952         
1953         hn = ch_strdup( static_hn );
1954 #endif /* !_SPARC */
1955
1956         return hn;
1957 #else /* LDAP_SLAPI */
1958         return NULL;
1959 #endif /* LDAP_SLAPI */
1960 }
1961
1962 /*
1963  * FIXME: this should go in an appropriate header ...
1964  */
1965 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1966
1967 int 
1968 slapi_log_error(
1969         int             severity, 
1970         char            *subsystem, 
1971         char            *fmt, 
1972         ... ) 
1973 {
1974 #ifdef LDAP_SLAPI
1975         int             rc = LDAP_SUCCESS;
1976         va_list         arglist;
1977
1978         va_start( arglist, fmt );
1979         rc = vLogError( severity, subsystem, fmt, arglist );
1980         va_end( arglist );
1981
1982         return rc;
1983 #else /* LDAP_SLAPI */
1984         return -1;
1985 #endif /* LDAP_SLAPI */
1986 }
1987
1988
1989 unsigned long
1990 slapi_timer_current_time( void ) 
1991 {
1992 #ifdef LDAP_SLAPI
1993         static int      first_time = 1;
1994 #if !defined (_WIN32)
1995         struct timeval  now;
1996         unsigned long   ret;
1997
1998         ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1999         if (first_time) {
2000                 first_time = 0;
2001                 gettimeofday( &base_time, NULL );
2002         }
2003         gettimeofday( &now, NULL );
2004         ret = ( now.tv_sec  - base_time.tv_sec ) * 1000000 + 
2005                         (now.tv_usec - base_time.tv_usec);
2006         ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
2007
2008         return ret;
2009
2010         /*
2011          * Ain't it better?
2012         return (slap_get_time() - starttime) * 1000000;
2013          */
2014 #else /* _WIN32 */
2015         LARGE_INTEGER now;
2016
2017         if ( first_time ) {
2018                 first_time = 0;
2019                 performance_counter_present = QueryPerformanceCounter( &base_time );
2020                 QueryPerformanceFrequency( &performance_freq );
2021         }
2022
2023         if ( !performance_counter_present )
2024              return 0;
2025
2026         QueryPerformanceCounter( &now );
2027         return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
2028 #endif /* _WIN32 */
2029 #else /* LDAP_SLAPI */
2030         return 0;
2031 #endif /* LDAP_SLAPI */
2032 }
2033
2034 /*
2035  * FIXME ?
2036  */
2037 unsigned long
2038 slapi_timer_get_time( char *label ) 
2039 {
2040 #ifdef LDAP_SLAPI
2041         unsigned long start = slapi_timer_current_time();
2042         printf("%10ld %10ld usec %s\n", start, 0, label);
2043         return start;
2044 #else /* LDAP_SLAPI */
2045         return 0;
2046 #endif /* LDAP_SLAPI */
2047 }
2048
2049 /*
2050  * FIXME ?
2051  */
2052 void
2053 slapi_timer_elapsed_time(
2054         char *label,
2055         unsigned long start ) 
2056 {
2057 #ifdef LDAP_SLAPI
2058         unsigned long stop = slapi_timer_current_time();
2059         printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2060 #endif /* LDAP_SLAPI */
2061 }
2062
2063 void
2064 slapi_free_search_results_internal( Slapi_PBlock *pb ) 
2065 {
2066 #ifdef LDAP_SLAPI
2067         Slapi_Entry     **entries;
2068         int             k = 0, nEnt = 0;
2069
2070         slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2071         slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2072         if ( nEnt == 0 ) {
2073                 return;
2074         }
2075         
2076         if ( entries == NULL ) {
2077                 return;
2078         }
2079         
2080         for ( k = 0; k < nEnt; k++ ) {
2081                 slapi_entry_free( entries[k] );
2082         }
2083         
2084         slapi_ch_free( (void **)&entries );
2085 #endif /* LDAP_SLAPI */
2086 }
2087
2088 /*
2089  * Internal API to prime a Slapi_PBlock with a Backend.
2090  */
2091 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
2092 {
2093 #ifdef LDAP_SLAPI
2094         int rc;
2095         
2096         rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
2097         if ( rc != LDAP_SUCCESS )
2098                 return rc;
2099         
2100         if ( be != NULL ) {
2101                 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
2102                 if ( rc != LDAP_SUCCESS )
2103                         return rc;
2104         }
2105
2106         return LDAP_SUCCESS;
2107 #else
2108         return -1;
2109 #endif /* LDAP_SLAPI */
2110 }
2111
2112 #ifdef LDAP_SLAPI
2113 /*
2114  * If oldStyle is TRUE, then a value suitable for setting to
2115  * the deprecated SLAPI_CONN_AUTHTYPE value is returned 
2116  * (pointer to static storage).
2117  *
2118  * If oldStyle is FALSE, then a value suitable for setting to
2119  * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
2120  * a pointer to allocated memory and will include the SASL
2121  * mechanism (if any).
2122  */
2123 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
2124 {
2125         size_t len;
2126         char *authType;
2127
2128         switch ( authz->sai_method ) {
2129         case LDAP_AUTH_SASL:
2130                 if ( oldStyle ) {
2131                         authType = SLAPD_AUTH_SASL;
2132                 } else {
2133                         len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
2134                         authType = slapi_ch_malloc( len );
2135                         snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
2136                 }
2137                 break;
2138         case LDAP_AUTH_SIMPLE:
2139                 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
2140                 break;
2141         case LDAP_AUTH_NONE:
2142                 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
2143                 break;
2144         default:
2145                 authType = NULL;
2146                 break;
2147         }
2148         if ( is_tls && authType == NULL ) {
2149                 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
2150         }
2151
2152         return authType;
2153 }
2154 #endif
2155
2156 /*
2157  * Internal API to prime a Slapi_PBlock with a Connection.
2158  */
2159 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
2160 {
2161 #ifdef LDAP_SLAPI
2162         char *connAuthType;
2163         int rc;
2164
2165         rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
2166         if ( rc != LDAP_SUCCESS )
2167                 return rc;
2168
2169         if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
2170                 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
2171                 if ( rc != LDAP_SUCCESS )
2172                         return rc;
2173         } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
2174                 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
2175                 if ( rc != LDAP_SUCCESS )
2176                         return rc;
2177         }
2178
2179         if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
2180                 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
2181                 if ( rc != LDAP_SUCCESS )
2182                         return rc;
2183         } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
2184                 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
2185                 if ( rc != LDAP_SUCCESS )
2186                         return rc;
2187         }
2188
2189 #ifdef LDAP_CONNECTIONLESS
2190         rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
2191         if ( rc != LDAP_SUCCESS )
2192                 return rc;
2193 #endif
2194
2195         rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
2196         if ( rc != LDAP_SUCCESS )
2197                 return rc;
2198
2199         /* Returns pointer to static string */
2200         connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
2201         if ( connAuthType != NULL ) {
2202                 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
2203                 if ( rc != LDAP_SUCCESS )
2204                         return rc;
2205         }
2206
2207         /* Returns pointer to allocated string */
2208         connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
2209         if ( connAuthType != NULL ) {
2210                 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
2211                 if ( rc != LDAP_SUCCESS )
2212                         return rc;
2213         }
2214
2215         if ( conn->c_authz.sai_dn.bv_val != NULL ) {
2216                 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
2217                 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
2218                 if ( rc != LDAP_SUCCESS )
2219                         return rc;
2220         }
2221
2222         return rc;
2223 #else
2224         return -1;
2225 #endif /* LDAP_SLAPI */
2226 }
2227
2228 /*
2229  * Internal API to prime a Slapi_PBlock with an Operation.
2230  */
2231 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
2232 {
2233 #ifdef LDAP_SLAPI
2234         int isRoot = 0;
2235         int isUpdateDn = 0;
2236         int rc;
2237         Backend *be;
2238         char *opAuthType;
2239
2240         if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2241                 be = NULL;
2242         }
2243         if (be != NULL) {
2244                 isRoot = be_isroot( be, &op->o_ndn );
2245                 isUpdateDn = be_isupdate( be, &op->o_ndn );
2246         }
2247                 
2248         rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
2249         if ( rc != LDAP_SUCCESS )
2250                 return rc;
2251
2252         rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
2253         if ( rc != LDAP_SUCCESS )
2254                 return rc;
2255
2256         rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
2257         if ( rc != LDAP_SUCCESS )
2258                 return rc;
2259
2260         rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
2261         if ( rc != LDAP_SUCCESS )
2262                 return rc;
2263
2264         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
2265         if ( rc != LDAP_SUCCESS )
2266                 return rc;
2267
2268         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
2269         if ( rc != LDAP_SUCCESS )
2270                 return rc;
2271
2272         rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
2273         if ( rc != LDAP_SUCCESS)
2274                 return rc;
2275
2276         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
2277         if ( rc != LDAP_SUCCESS )
2278                 return rc;
2279
2280         rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
2281         if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
2282                 /* Not quite sure what the point of this is. */
2283                 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
2284                 if ( rc != LDAP_SUCCESS )
2285                         return rc;
2286         }
2287
2288         return LDAP_SUCCESS;
2289 #else
2290         return -1;
2291 #endif
2292 }
2293
2294 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2295 {
2296 #ifdef LDAP_SLAPI
2297         Connection *conn;
2298
2299         slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
2300         *isSSL = conn->c_is_tls;
2301
2302         return LDAP_SUCCESS;
2303 #else
2304         return -1;
2305 #endif /* LDAP_SLAPI */
2306 }
2307
2308 /*
2309  * DS 5.x compatability API follow
2310  */
2311
2312 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2313 {
2314 #ifdef LDAP_SLAPI
2315         AttributeType *at;
2316
2317         if ( attr == NULL )
2318                 return LDAP_PARAM_ERROR;
2319
2320         at = attr->a_desc->ad_type;
2321
2322         *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2323
2324         if ( is_at_single_value( at ) )
2325                 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2326         if ( is_at_operational( at ) )
2327                 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2328         if ( is_at_obsolete( at ) )
2329                 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2330         if ( is_at_collective( at ) )
2331                 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2332         if ( is_at_no_user_mod( at ) )
2333                 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2334
2335         return LDAP_SUCCESS;
2336 #else
2337         return -1;
2338 #endif /* LDAP_SLAPI */
2339 }
2340
2341 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2342 {
2343 #ifdef LDAP_SLAPI
2344         unsigned long flags;
2345
2346         if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2347                 return 0;
2348         return (flags & flag) ? 1 : 0;
2349 #else
2350         return 0;
2351 #endif /* LDAP_SLAPI */
2352 }
2353
2354 Slapi_Attr *slapi_attr_new( void )
2355 {
2356 #ifdef LDAP_SLAPI
2357         Attribute *ad;
2358
2359         ad = (Attribute  *)slapi_ch_calloc( 1, sizeof(*ad) );
2360
2361         return ad;
2362 #else
2363         return NULL;
2364 #endif
2365 }
2366
2367 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2368 {
2369 #ifdef LDAP_SLAPI
2370         const char *text;
2371         AttributeDescription *ad = NULL;
2372
2373         if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2374                 return NULL;
2375         }
2376
2377         a->a_desc = ad;
2378         a->a_vals = NULL;
2379 #ifdef SLAP_NVALUES
2380         a->a_nvals = NULL;
2381 #endif
2382         a->a_next = NULL;
2383         a->a_flags = 0;
2384
2385         return a;
2386 #else
2387         return NULL;
2388 #endif
2389 }
2390
2391 void slapi_attr_free( Slapi_Attr **a )
2392 {
2393 #ifdef LDAP_SLAPI
2394         attr_free( *a );
2395         *a = NULL;
2396 #endif
2397 }
2398
2399 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2400 {
2401 #ifdef LDAP_SLAPI
2402         return attr_dup( (Slapi_Attr *)attr );
2403 #else
2404         return NULL;
2405 #endif
2406 }
2407
2408 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2409 {
2410 #ifdef LDAP_SLAPI
2411 #ifdef SLAP_NVALUES
2412         /*
2413          * FIXME: here we may lose alignment between a_vals/a_nvals
2414          */
2415 #endif
2416         return value_add_one( &a->a_vals, (Slapi_Value *)v );
2417 #else
2418         return -1;
2419 #endif
2420 }
2421
2422 int slapi_attr_type2plugin( const char *type, void **pi )
2423 {
2424         *pi = NULL;
2425
2426         return LDAP_OTHER;
2427 }
2428
2429 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2430 {
2431 #ifdef LDAP_SLAPI
2432         if ( attr == NULL ) {
2433                 return LDAP_PARAM_ERROR;
2434         }
2435
2436         *type = attr->a_desc->ad_cname.bv_val;
2437
2438         return LDAP_SUCCESS;
2439 #else
2440         return -1;
2441 #endif
2442 }
2443
2444 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2445 {
2446 #ifdef LDAP_SLAPI
2447         if ( attr == NULL ) {
2448                 return LDAP_PARAM_ERROR;
2449         }
2450         *oidp = attr->a_desc->ad_type->sat_oid;
2451
2452         return LDAP_SUCCESS;
2453 #else
2454         return -1;
2455 #endif
2456 }
2457
2458 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2459 {
2460 #ifdef LDAP_SLAPI
2461         MatchingRule *mr;
2462         int ret;
2463         int rc;
2464         const char *text;
2465
2466         mr = a->a_desc->ad_type->sat_equality;
2467 #ifdef SLAP_NVALUES
2468         rc = value_match( &ret, a->a_desc, mr,
2469                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2470                 (struct berval *)v1, (void *)v2, &text );
2471 #else
2472         rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
2473                 (struct berval *)v1, (void *)v2, &text );
2474 #endif
2475         if ( rc != LDAP_SUCCESS ) 
2476                 return -1;
2477
2478         return ( ret == 0 ) ? 0 : -1;
2479 #else
2480         return -1;
2481 #endif
2482 }
2483
2484 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2485 {
2486 #ifdef LDAP_SLAPI
2487         MatchingRule *mr;
2488         struct berval *bv;
2489         int j;
2490         const char *text;
2491         int rc;
2492         int ret;
2493
2494         mr = a->a_desc->ad_type->sat_equality;
2495         for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2496 #ifdef SLAP_NVALUES
2497                 rc = value_match( &ret, a->a_desc, mr,
2498                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2499 #else
2500                 rc = value_match( &ret, a->a_desc, mr,
2501                         SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
2502 #endif
2503                 if ( rc != LDAP_SUCCESS ) {
2504                         return -1;
2505                 }
2506                 if ( ret == 0 ) {
2507                         return 0;
2508                 }
2509         }
2510 #endif
2511         return -1;
2512 }
2513
2514 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2515 {
2516 #ifdef LDAP_SLAPI
2517         AttributeDescription *a1 = NULL;
2518         AttributeDescription *a2 = NULL;
2519         const char *text;
2520         int ret;
2521
2522         if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2523                 return -1;
2524         }
2525
2526         if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2527                 return 1;
2528         }
2529
2530 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2531         ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2532                 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2533
2534         switch ( opt ) {
2535         case SLAPI_TYPE_CMP_EXACT:
2536                 ret = ad_cmp( a1, a2 );
2537                 break;
2538         case SLAPI_TYPE_CMP_BASE:
2539                 ret = ad_base_cmp( a1, a2 );
2540                 break;
2541         case SLAPI_TYPE_CMP_SUBTYPE:
2542                 ret = is_ad_subtype( a2, a2 );
2543                 break;
2544         default:
2545                 ret = -1;
2546                 break;
2547         }
2548
2549         return ret;
2550 #else
2551         return -1;
2552 #endif
2553 }
2554
2555 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2556 {
2557 #ifdef LDAP_SLAPI
2558         return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2559 #else
2560         return -1;
2561 #endif
2562 }
2563
2564 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2565 {
2566 #ifdef LDAP_SLAPI
2567         return slapi_valueset_first_value( &a->a_vals, v );
2568 #else
2569         return -1;
2570 #endif
2571 }
2572
2573 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2574 {
2575 #ifdef LDAP_SLAPI
2576         return slapi_valueset_next_value( &a->a_vals, hint, v );
2577 #else
2578         return -1;
2579 #endif
2580 }
2581
2582 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2583 {
2584 #ifdef LDAP_SLAPI
2585         *numValues = slapi_valueset_count( &a->a_vals );
2586
2587         return 0;
2588 #else
2589         return -1;
2590 #endif
2591 }
2592
2593 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2594 {
2595 #ifdef LDAP_SLAPI
2596         *vs = &((Slapi_Attr *)a)->a_vals;
2597
2598         return 0;
2599 #else
2600         return -1;
2601 #endif
2602 }
2603
2604 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2605 {
2606 #ifdef LDAP_SLAPI
2607         return slapi_attr_get_values( a, vals );
2608 #else
2609         return -1;
2610 #endif
2611 }
2612
2613 char *slapi_attr_syntax_normalize( const char *s )
2614 {
2615 #ifdef LDAP_SLAPI
2616         AttributeDescription *ad = NULL;
2617         const char *text;
2618
2619         if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2620                 return NULL;
2621         }
2622
2623         return ad->ad_cname.bv_val;
2624 #else
2625         return -1;
2626 #endif
2627 }
2628
2629 Slapi_Value *slapi_value_new( void )
2630 {
2631 #ifdef LDAP_SLAPI
2632         struct berval *bv;
2633
2634         bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2635
2636         return bv;
2637 #else
2638         return NULL;
2639 #endif
2640 }
2641
2642 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2643 {
2644 #ifdef LDAP_SLAPI
2645         return ber_dupbv( NULL, (struct berval *)bval );
2646 #else
2647         return NULL;
2648 #endif
2649 }
2650
2651 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2652 {
2653 #ifdef LDAP_SLAPI
2654         return slapi_value_new_berval( v );
2655 #else
2656         return NULL;
2657 #endif
2658 }
2659
2660 Slapi_Value *slapi_value_new_string(const char *s)
2661 {
2662 #ifdef LDAP_SLAPI
2663         struct berval bv;
2664
2665         bv.bv_val = (char *)s;
2666         bv.bv_len = strlen( s );
2667
2668         return slapi_value_new_berval( &bv );
2669 #else
2670         return NULL;
2671 #endif
2672 }
2673
2674 Slapi_Value *slapi_value_init(Slapi_Value *val)
2675 {
2676 #ifdef LDAP_SLAPI
2677         val->bv_val = NULL;
2678         val->bv_len = 0;
2679
2680         return val;
2681 #else
2682         return NULL;
2683 #endif
2684 }
2685
2686 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2687 {
2688 #ifdef LDAP_SLAPI
2689         return ber_dupbv( v, bval );
2690 #else
2691         return NULL;
2692 #endif
2693 }
2694
2695 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2696 {
2697 #ifdef LDAP_SLAPI
2698         v->bv_val = slapi_ch_strdup( (char *)s );
2699         v->bv_len = strlen( s );
2700
2701         return v;
2702 #else
2703         return NULL;
2704 #endif
2705 }
2706
2707 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2708 {
2709 #ifdef LDAP_SLAPI
2710         return slapi_value_new_value( v );
2711 #else
2712         return NULL;
2713 #endif
2714 }
2715
2716 void slapi_value_free(Slapi_Value **value)
2717 {
2718 #ifdef LDAP_SLAPI       
2719         if ( value == NULL ) {
2720                 return;
2721         }
2722
2723         if ( (*value) != NULL ) {
2724                 slapi_ch_free( (void **)&(*value)->bv_val );
2725                 slapi_ch_free( (void **)value );
2726         }
2727 #endif
2728 }
2729
2730 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2731 {
2732 #ifdef LDAP_SLAPI
2733         return value;
2734 #else
2735         return NULL;
2736 #endif
2737 }
2738
2739 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
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         slapi_value_init_berval( value, (struct berval *)bval );
2749
2750         return value;
2751 #else
2752         return NULL;
2753 #endif
2754 }
2755
2756 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2757 {
2758 #ifdef LDAP_SLAPI
2759         if ( value == NULL ) {
2760                 return NULL;
2761         }
2762         return slapi_value_set_berval( value, vfrom );
2763 #else
2764         return NULL;
2765 #endif
2766 }
2767
2768 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2769 {
2770 #ifdef LDAP_SLAPI
2771         if ( value == NULL ) {
2772                 return NULL;
2773         }
2774         if ( value->bv_val != NULL ) {
2775                 slapi_ch_free( (void **)&value->bv_val );
2776         }
2777         value->bv_val = slapi_ch_malloc( len );
2778         value->bv_len = len;
2779         AC_MEMCPY( value->bv_val, val, len );
2780
2781         return value;
2782 #else
2783         return NULL;
2784 #endif
2785 }
2786
2787 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2788 {
2789 #ifdef LDAP_SLAPI
2790         if ( value == NULL ) {
2791                 return -1;
2792         }
2793         slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2794         return 0;
2795 #else
2796         return NULL;
2797 #endif
2798 }
2799
2800 int slapi_value_set_int(Slapi_Value *value, int intVal)
2801 {
2802 #ifdef LDAP_SLAPI
2803         char buf[64];
2804
2805         snprintf( buf, sizeof( buf ), "%d", intVal );
2806
2807         return slapi_value_set_string( value, buf );
2808 #else
2809         return -1;
2810 #endif
2811 }
2812
2813 const char *slapi_value_get_string(const Slapi_Value *value)
2814 {
2815 #ifdef LDAP_SLAPI
2816         if ( value == NULL ) {
2817                 return NULL;
2818         }
2819         return value->bv_val;
2820 #else
2821         return NULL;
2822 #endif
2823 }
2824
2825 #ifdef LDAP_SLAPI
2826 static int checkBVString(const struct berval *bv)
2827 {
2828         int i;
2829
2830         for ( i = 0; i < bv->bv_len; i++ ) {
2831                 if ( bv->bv_val[i] == '\0' )
2832                         return 0;
2833         }
2834         if ( bv->bv_val[i] != '\0' )
2835                 return 0;
2836
2837         return 1;
2838 }
2839 #endif
2840
2841 int slapi_value_get_int(const Slapi_Value *value)
2842 {
2843 #ifdef LDAP_SLAPI
2844         if ( value == NULL ) return 0;
2845         if ( value->bv_val == NULL ) return 0;
2846         if ( !checkBVString( value ) ) return 0;
2847
2848         return (int)strtol( value->bv_val, NULL, 10 );
2849 #else
2850         return NULL;
2851 #endif
2852 }
2853
2854 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2855 {
2856 #ifdef LDAP_SLAPI
2857         if ( value == NULL ) return 0;
2858         if ( value->bv_val == NULL ) return 0;
2859         if ( !checkBVString( value ) ) return 0;
2860
2861         return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2862 #else
2863         return NULL;
2864 #endif
2865 }
2866
2867 long slapi_value_get_long(const Slapi_Value *value)
2868 {
2869 #ifdef LDAP_SLAPI
2870         if ( value == NULL ) return 0;
2871         if ( value->bv_val == NULL ) return 0;
2872         if ( !checkBVString( value ) ) return 0;
2873
2874         return strtol( value->bv_val, NULL, 10 );
2875 #else
2876         return NULL;
2877 #endif
2878 }
2879
2880 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2881 {
2882 #ifdef LDAP_SLAPI
2883         if ( value == NULL ) return 0;
2884         if ( value->bv_val == NULL ) return 0;
2885         if ( !checkBVString( value ) ) return 0;
2886
2887         return strtoul( value->bv_val, NULL, 10 );
2888 #else
2889         return NULL;
2890 #endif
2891 }
2892
2893 size_t slapi_value_get_length(const Slapi_Value *value)
2894 {
2895 #ifdef LDAP_SLAPI
2896         if ( value == NULL )
2897                 return 0;
2898
2899         return (size_t) value->bv_len;
2900 #else
2901         return 0;
2902 #endif
2903 }
2904
2905 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2906 {
2907 #ifdef LDAP_SLAPI
2908         return slapi_attr_value_cmp( a, v1, v2 );
2909 #else
2910         return -1;
2911 #endif
2912 }
2913
2914 /* A ValueSet is a container for a BerVarray. */
2915 Slapi_ValueSet *slapi_valueset_new( void )
2916 {
2917 #ifdef LDAP_SLAPI
2918         Slapi_ValueSet *vs;
2919
2920         vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2921         *vs = NULL;
2922
2923         return vs;
2924 #else
2925         return NULL;
2926 #endif
2927 }
2928
2929 void slapi_valueset_free(Slapi_ValueSet *vs)
2930 {
2931 #ifdef LDAP_SLAPI
2932         if ( vs != NULL ) {
2933                 BerVarray vp = *vs;
2934
2935                 ber_bvarray_free( vp );
2936                 slapi_ch_free( (void **)&vp );
2937
2938                 *vs = NULL;
2939         }
2940 #endif
2941 }
2942
2943 void slapi_valueset_init(Slapi_ValueSet *vs)
2944 {
2945 #ifdef LDAP_SLAPI
2946         if ( vs != NULL && *vs == NULL ) {
2947                 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2948                 (*vs)->bv_val = NULL;
2949                 (*vs)->bv_len = 0;
2950         }
2951 #endif
2952 }
2953
2954 void slapi_valueset_done(Slapi_ValueSet *vs)
2955 {
2956 #ifdef LDAP_SLAPI
2957         BerVarray vp;
2958
2959         if ( vs == NULL )
2960                 return;
2961
2962         for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2963                 vp->bv_len = 0;
2964                 slapi_ch_free( (void **)&vp->bv_val );
2965         }
2966         /* but don't free *vs or vs */
2967 #endif
2968 }
2969
2970 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2971 {
2972 #ifdef LDAP_SLAPI
2973         struct berval bv;
2974
2975         ber_dupbv( &bv, (Slapi_Value *)addval );
2976         ber_bvarray_add( vs, &bv );
2977 #endif
2978 }
2979
2980 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2981 {
2982 #ifdef LDAP_SLAPI
2983         return slapi_valueset_next_value( vs, 0, v );
2984 #else
2985         return -1;
2986 #endif
2987 }
2988
2989 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2990 {
2991 #ifdef LDAP_SLAPI
2992         int i;
2993         BerVarray vp;
2994
2995         if ( vs == NULL )
2996                 return -1;
2997
2998         vp = *vs;
2999
3000         for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3001                 if ( i == index ) {
3002                         *v = &vp[i];
3003                         return index + 1;
3004                 }
3005         }
3006 #endif
3007
3008         return -1;
3009 }
3010
3011 int slapi_valueset_count( const Slapi_ValueSet *vs )
3012 {
3013 #ifdef LDAP_SLAPI
3014         int i;
3015         BerVarray vp;
3016
3017         if ( vs == NULL )
3018                 return 0;
3019
3020         vp = *vs;
3021
3022         for ( i = 0; vp[i].bv_val != NULL; i++ )
3023                 ;
3024
3025         return i;
3026 #else
3027         return 0;
3028 #endif
3029
3030 }
3031
3032 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3033 {
3034 #ifdef LDAP_SLAPI
3035         BerVarray vp;
3036
3037         for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3038                 slapi_valueset_add_value( vs1, vp );
3039         }
3040 #endif
3041 }
3042
3043 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3044         struct berval *val, int access )
3045 {
3046 #ifdef LDAPI_SLAPI
3047         Backend *be;
3048         Connection *conn;
3049         Operation *op;
3050         int ret;
3051         slap_access_t slap_access;
3052         AttributeDescription *ad = NULL;
3053         char *text;
3054
3055         ret = slap_str2ad( attr, &ad, &text );
3056         if ( ret != LDAP_SUCCESS ) {
3057                 return ret;
3058         }
3059
3060         switch ( access & SLAPI_ACL_ALL ) {
3061         case SLAPI_ACL_COMPARE:
3062                 slap_access = ACL_COMPARE;
3063                 break;
3064         case SLAPI_ACL_SEARCH:
3065                 slap_access = ACL_SEARCH;
3066                 break;
3067         case SLAPI_ACL_READ:
3068                 slap_access = ACL_READ;
3069                 break;
3070         case SLAPI_ACL_WRITE:
3071         case SLAPI_ACL_DELETE:
3072         case SLAPI_ACL_ADD:
3073         case SLAPI_ACL_SELF:
3074                 slap_access = ACL_WRITE;
3075                 break;
3076         default:
3077                 return LDAP_INSUFFICIENT_ACCESS;
3078                 break;
3079         }
3080
3081         if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3082                 return LDAP_PARAM_ERROR;
3083         }
3084
3085         if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3086                 return LDAP_PARAM_ERROR;
3087         }
3088
3089         if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3090                 return LDAP_PARAM_ERROR;
3091         }
3092
3093         ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
3094
3095         return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3096 #else
3097         return LDAP_UNWILLING_TO_PERFORM;
3098 #endif
3099 }
3100
3101 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3102 {
3103 #ifdef LDAP_SLAPI
3104         Operation *op;
3105         int ret;
3106         Modifications *ml;
3107         Modifications *next;
3108
3109         if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3110                 return LDAP_PARAM_ERROR;
3111         }
3112
3113         ml = slapi_x_ldapmods2modifications( mods );
3114         if ( ml == NULL ) {
3115                 return LDAP_OTHER;
3116         }
3117
3118         ret = acl_check_modlist( op, e, ml );
3119
3120         /* Careful when freeing the modlist because it has pointers into the mods array. */
3121         for ( ; ml != NULL; ml = next ) {
3122                 next = ml->sml_next;
3123
3124                 /* just free the containing array */
3125                 slapi_ch_free( (void **)&ml->sml_bvalues );
3126                 slapi_ch_free( (void **)&ml );
3127         }
3128
3129         return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3130 #else
3131         return LDAP_UNWILLING_TO_PERFORM;
3132 #endif
3133 }
3134
3135 /*
3136  * Synthesise an LDAPMod array from a Modifications list to pass
3137  * to SLAPI. This synthesis is destructive and as such the 
3138  * Modifications list may not be used after calling this 
3139  * function.
3140  * 
3141  * This function must also be called before slap_mods_check().
3142  */
3143 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
3144 {
3145 #ifdef LDAP_SLAPI
3146         Modifications *ml, *modlist;
3147         LDAPMod **mods, *modp;
3148         int i, j;
3149
3150         modlist = *pmodlist;
3151
3152         for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3153                 ;
3154
3155         mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3156
3157         for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3158                 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3159                 modp = mods[i];
3160                 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3161
3162                 /* Take ownership of original type. */
3163                 modp->mod_type = ml->sml_type.bv_val;
3164                 ml->sml_type.bv_val = NULL;
3165
3166                 if ( ml->sml_bvalues != NULL ) {
3167                         for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
3168                                 ;
3169                         modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3170                                 sizeof(struct berval *) );
3171                         for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
3172                                 /* Take ownership of original values. */
3173                                 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3174                                 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
3175                                 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
3176                                 ml->sml_bvalues[j].bv_len = 0;
3177                                 ml->sml_bvalues[j].bv_val = NULL;
3178                         }
3179                         modp->mod_bvalues[j] = NULL;
3180                 } else {
3181                         modp->mod_bvalues = NULL;
3182                 }
3183                 i++;
3184         }
3185
3186         mods[i] = NULL;
3187
3188         slap_mods_free( modlist );
3189         *pmodlist = NULL;
3190
3191         return mods;
3192 #else
3193         return NULL;
3194 #endif
3195 }
3196
3197 /*
3198  * Convert a potentially modified array of LDAPMods back to a
3199  * Modification list. 
3200  * 
3201  * The returned Modification list contains pointers into the
3202  * LDAPMods array; the latter MUST be freed with
3203  * slapi_x_free_ldapmods() (see below).
3204  */
3205 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
3206 {
3207 #ifdef LDAP_SLAPI
3208         Modifications *modlist, **modtail;
3209         LDAPMod **modp;
3210
3211         modtail = &modlist;
3212
3213         for( modp = mods; *modp != NULL; modp++ ) {
3214                 Modifications *mod;
3215                 int i;
3216                 char **p;
3217                 struct berval **bvp;
3218
3219                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3220                 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3221                 mod->sml_type.bv_val = (*modp)->mod_type;
3222                 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3223                 mod->sml_desc = NULL;
3224                 mod->sml_next = NULL;
3225
3226                 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3227                         for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3228                                 ;
3229                 } else {
3230                         for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3231                                 ;
3232                 }
3233
3234                 if ( i == 0 ) {
3235                          mod->sml_bvalues = NULL;
3236                 } else {
3237                         mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3238
3239                         /* NB: This implicitly trusts a plugin to return valid modifications. */
3240                         if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3241                                 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3242                                         mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
3243                                         mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
3244                                 }
3245                         } else {
3246                                 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3247                                         mod->sml_bvalues[i].bv_val = *p;
3248                                         mod->sml_bvalues[i].bv_len = strlen( *p );
3249                                 }
3250                         }
3251                         mod->sml_bvalues[i].bv_val = NULL;
3252                 }
3253 #ifdef SLAP_NVALUES
3254                 mod->sml_nvalues = NULL;
3255 #endif
3256
3257                 *modtail = mod;
3258                 modtail = &mod->sml_next;
3259         }
3260         
3261         return modlist;
3262 #else
3263         return NULL;
3264 #endif 
3265 }
3266
3267 /*
3268  * This function only frees the parts of the mods array that
3269  * are not shared with the Modification list that was created
3270  * by slapi_x_ldapmods2modifications(). 
3271  *
3272  */
3273 void slapi_x_free_ldapmods (LDAPMod **mods)
3274 {
3275 #ifdef LDAP_SLAPI
3276         int i, j;
3277
3278         if (mods == NULL)
3279                 return;
3280
3281         for ( i = 0; mods[i] != NULL; i++ ) {
3282                 /*
3283                  * Don't free values themselves; they're owned by the
3284                  * Modification list. Do free the containing array.
3285                  */
3286                 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3287                         for ( j = 0; mods[i]->mod_bvalues != NULL && mods[i]->mod_bvalues[j] != NULL; j++ ) {
3288                                 ch_free( mods[i]->mod_bvalues[j] );
3289                         }
3290                         ch_free( mods[i]->mod_bvalues );
3291                 } else {
3292                         ch_free( mods[i]->mod_values );
3293                 }
3294                 /* Don't free type, for same reasons. */
3295                 ch_free( mods[i] );
3296         }
3297         ch_free( mods );
3298 #endif /* LDAP_SLAPI */
3299 }
3300
3301 /*
3302  * Sun ONE DS 5.x computed attribute support. Computed attributes
3303  * allow for dynamically generated operational attributes, a very
3304  * useful thing indeed.
3305  */
3306
3307 /*
3308  * Write the computed attribute to a BerElement. Complementary 
3309  * functions need to be defined for anything that replaces 
3310  * op->o_callback->sc_sendentry, if you wish to make computed
3311  * attributes available to it.
3312  */
3313 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3314 {
3315 #ifdef LDAP_SLAPI
3316         Operation *op = NULL;
3317         BerElement *ber;
3318         AttributeDescription *desc = NULL;
3319         int rc;
3320         int i;
3321
3322         if ( c == NULL ) {
3323                 return 1;
3324         }
3325
3326         if ( a == NULL ) {
3327                 return 1;
3328         }
3329
3330         if ( e == NULL ) {
3331                 return 1;
3332         }
3333
3334         rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3335         if ( rc != 0 || op == NULL ) {
3336                 return rc;
3337         }
3338
3339         ber = (BerElement *)c->cac_private;
3340         desc = a->a_desc;
3341
3342         if ( c->cac_attrs == NULL ) {
3343                 /* All attrs request, skip operational attributes */
3344                 if ( is_at_operational( desc->ad_type ) ) {
3345                         return 0;
3346                 }
3347         } else {
3348                 /* Specific attrs requested */
3349                 if ( is_at_operational( desc->ad_type ) ) {
3350                         if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3351                                 return 0;
3352                         }
3353                 } else {
3354                         if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3355                                 return 0;
3356                         }
3357                 }
3358         }
3359
3360         if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3361                 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3362                         "acl: access to attribute %s not allowed\n",
3363                         desc->ad_cname.bv_val );
3364                 return 0;
3365         }
3366
3367         rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3368         if (rc == -1 ) {
3369                 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3370                         "ber_printf failed\n");
3371                 return 1;
3372         }
3373
3374         if ( !c->cac_attrsonly ) {
3375                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3376                         if ( !access_allowed( op, e,
3377                                 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3378                                 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
3379                                         "slapi_x_compute_output_ber: conn %lu "
3380                                         "acl: access to %s, value %d not allowed\n",
3381                                         op->o_connid, desc->ad_cname.bv_val, i  );
3382                                 continue;
3383                         }
3384         
3385                         if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3386                                 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3387                                         "ber_printf failed\n");
3388                                 return 1;
3389                         }
3390                 }
3391         }
3392
3393         if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3394                 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
3395                         "ber_printf failed\n" );
3396                 return 1;
3397         }
3398
3399         return 0;
3400 #else
3401         return 1;
3402 #endif
3403 }
3404
3405 /*
3406  * For some reason Sun don't use the normal plugin mechanism
3407  * registration path to register an "evaluator" function (an
3408  * "evaluator" is responsible for adding computed attributes;
3409  * the nomenclature is somewhat confusing).
3410  *
3411  * As such slapi_compute_add_evaluator() registers the 
3412  * function directly.
3413  */
3414 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3415 {
3416 #ifdef LDAP_SLAPI
3417         Slapi_PBlock *pPlugin = NULL;
3418         int rc;
3419
3420         pPlugin = slapi_pblock_new();
3421         if ( pPlugin == NULL ) {
3422                 rc = LDAP_NO_MEMORY;
3423                 goto done;
3424         }
3425
3426         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3427         if ( rc != LDAP_SUCCESS ) {
3428                 goto done;
3429         }
3430
3431         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3432         if ( rc != LDAP_SUCCESS ) {
3433                 goto done;
3434         }
3435
3436         rc = insertPlugin( NULL, pPlugin );
3437         if ( rc != 0 ) {
3438                 rc = LDAP_OTHER;
3439                 goto done;
3440         }
3441
3442 done:
3443         if ( rc != LDAP_SUCCESS ) {
3444                 if ( pPlugin != NULL ) {
3445                         slapi_pblock_destroy( pPlugin );
3446                 }
3447                 return -1;
3448         }
3449
3450         return 0;
3451 #else
3452         return -1;
3453 #endif /* LDAP_SLAPI */
3454 }
3455
3456 /*
3457  * See notes above regarding slapi_compute_add_evaluator().
3458  */
3459 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3460 {
3461 #ifdef LDAP_SLAPI
3462         Slapi_PBlock *pPlugin = NULL;
3463         int rc;
3464
3465         pPlugin = slapi_pblock_new();
3466         if ( pPlugin == NULL ) {
3467                 rc = LDAP_NO_MEMORY;
3468                 goto done;
3469         }
3470
3471         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3472         if ( rc != LDAP_SUCCESS ) {
3473                 goto done;
3474         }
3475
3476         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3477         if ( rc != LDAP_SUCCESS ) {
3478                 goto done;
3479         }
3480
3481         rc = insertPlugin( NULL, pPlugin );
3482         if ( rc != 0 ) {
3483                 rc = LDAP_OTHER;
3484                 goto done;
3485         }
3486
3487 done:
3488         if ( rc != LDAP_SUCCESS ) {
3489                 if ( pPlugin != NULL ) {
3490                         slapi_pblock_destroy( pPlugin );
3491                 }
3492                 return -1;
3493         }
3494
3495         return 0;
3496 #else
3497         return -1;
3498 #endif /* LDAP_SLAPI */
3499 }
3500
3501 /*
3502  * Call compute evaluators
3503  */
3504 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3505 {
3506 #ifdef LDAP_SLAPI
3507         int rc = 0;
3508         slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3509
3510         rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3511         if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3512                 /* Nothing to do; front-end should ignore. */
3513                 return 0;
3514         }
3515
3516         for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3517                 /*
3518                  * -1: no attribute matched requested type
3519                  *  0: one attribute matched
3520                  * >0: error happened
3521                  */
3522                 rc = (*pGetPlugin)( c, type, e, outputfn );
3523                 if ( rc > 0 ) {
3524                         break;
3525                 }
3526         }
3527
3528         slapi_ch_free( (void **)&tmpPlugin );
3529
3530         return rc;
3531 #else
3532         return 1;
3533 #endif /* LDAP_SLAPI */
3534 }
3535
3536 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3537 {
3538 #ifdef LDAP_SLAPI
3539         Backend *be;
3540         int rc;
3541
3542         rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3543         if ( rc != 0 ) {
3544                 return rc;
3545         }
3546
3547         return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3548 #else
3549         return -1;
3550 #endif /* LDAP_SLAPI */
3551 }
3552
3553 /*
3554  * New API to provide the plugin with access to the search
3555  * pblock. Have informed Sun DS team.
3556  */
3557 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3558 {
3559 #ifdef LDAP_SLAPI
3560         if ( c == NULL )
3561                 return -1;
3562
3563         if ( c->cac_pb == NULL )
3564                 return -1;
3565
3566         *pb = c->cac_pb;
3567
3568         return 0;
3569 #else
3570         return -1;
3571 #endif /* LDAP_SLAPI */
3572 }
3573
3574 Slapi_Mutex *slapi_new_mutex( void )
3575 {
3576 #ifdef LDAP_SLAPI
3577         Slapi_Mutex *m;
3578
3579         m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3580         if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3581                 slapi_ch_free( (void **)&m );
3582                 return NULL;
3583         }
3584
3585         return m;
3586 #else
3587         return NULL;
3588 #endif
3589 }
3590
3591 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3592 {
3593 #ifdef LDAP_SLAPI
3594         if ( mutex != NULL ) {
3595                 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3596                 slapi_ch_free( (void **)&mutex);
3597         }
3598 #endif
3599 }
3600
3601 void slapi_lock_mutex( Slapi_Mutex *mutex )
3602 {
3603 #ifdef LDAP_SLAPI
3604         ldap_pvt_thread_mutex_lock( &mutex->mutex );
3605 #endif
3606 }
3607
3608 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3609 {
3610 #ifdef LDAP_SLAPI
3611         return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3612 #else
3613         return -1;
3614 #endif
3615 }
3616
3617 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3618 {
3619 #ifdef LDAP_SLAPI
3620         Slapi_CondVar *cv;
3621
3622         if ( mutex == NULL ) {
3623                 return NULL;
3624         }
3625
3626         cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3627         if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3628                 slapi_ch_free( (void **)&cv );
3629                 return NULL;
3630         }
3631
3632         /* XXX struct copy */
3633         cv->mutex = mutex->mutex;
3634
3635         return cv;
3636 #else   
3637         return NULL;
3638 #endif
3639 }
3640
3641 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3642 {
3643 #ifdef LDAP_SLAPI
3644         if ( cvar != NULL ) {
3645                 ldap_pvt_thread_cond_destroy( &cvar->cond );
3646                 slapi_ch_free( (void **)&cvar );
3647         }
3648 #endif
3649 }
3650
3651 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3652 {
3653 #ifdef LDAP_SLAPI
3654         if ( cvar == NULL ) {
3655                 return -1;
3656         }
3657
3658         return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3659 #else
3660         return -1;
3661 #endif
3662 }
3663
3664 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3665 {
3666 #ifdef LDAP_SLAPI
3667         if ( cvar == NULL ) {
3668                 return -1;
3669         }
3670
3671         if ( notify_all ) {
3672                 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3673         }
3674
3675         return ldap_pvt_thread_cond_signal( &cvar->cond );
3676 #else
3677         return -1;
3678 #endif
3679 }
3680