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