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