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