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