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