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