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