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