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