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