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