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