]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_utils.c
Fix "FIXME" in slapi_attr_add_value() - store normalized value as well as
[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         struct berval nval;
2702         struct berval *nvalp;
2703         int rc;
2704         AttributeDescription *desc = a->a_desc;
2705
2706         if ( desc->ad_type->sat_equality &&
2707              desc->ad_type->sat_equality->smr_normalize ) {
2708                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2709                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2710                         desc->ad_type->sat_syntax,
2711                         desc->ad_type->sat_equality,
2712                         (Slapi_Value *)v, &nval, NULL );
2713                 if ( rc != LDAP_SUCCESS ) {
2714                         return rc;
2715                 }
2716                 nvalp = &nval;
2717         } else {
2718                 nvalp = NULL;
2719         }
2720
2721         rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2722         if ( rc == 0 && nvalp != NULL ) {
2723                 rc = value_add_one( &a->a_nvals, nvalp );
2724         } else {
2725                 a->a_nvals = a->a_vals;
2726         }
2727
2728         if ( nvalp != NULL ) {
2729                 slapi_ch_free_string( &nval.bv_val );
2730         }
2731
2732         return rc;
2733 #else
2734         return -1;
2735 #endif
2736 }
2737
2738 int slapi_attr_type2plugin( const char *type, void **pi )
2739 {
2740         *pi = NULL;
2741
2742         return LDAP_OTHER;
2743 }
2744
2745 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2746 {
2747 #ifdef LDAP_SLAPI
2748         if ( attr == NULL ) {
2749                 return LDAP_PARAM_ERROR;
2750         }
2751
2752         *type = attr->a_desc->ad_cname.bv_val;
2753
2754         return LDAP_SUCCESS;
2755 #else
2756         return -1;
2757 #endif
2758 }
2759
2760 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2761 {
2762 #ifdef LDAP_SLAPI
2763         if ( attr == NULL ) {
2764                 return LDAP_PARAM_ERROR;
2765         }
2766         *oidp = attr->a_desc->ad_type->sat_oid;
2767
2768         return LDAP_SUCCESS;
2769 #else
2770         return -1;
2771 #endif
2772 }
2773
2774 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2775 {
2776 #ifdef LDAP_SLAPI
2777         MatchingRule *mr;
2778         int ret;
2779         int rc;
2780         const char *text;
2781
2782         mr = a->a_desc->ad_type->sat_equality;
2783         rc = value_match( &ret, a->a_desc, mr,
2784                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2785                 (struct berval *)v1, (void *)v2, &text );
2786         if ( rc != LDAP_SUCCESS ) 
2787                 return -1;
2788
2789         return ( ret == 0 ) ? 0 : -1;
2790 #else
2791         return -1;
2792 #endif
2793 }
2794
2795 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2796 {
2797 #ifdef LDAP_SLAPI
2798         MatchingRule *mr;
2799         struct berval *bv;
2800         int j;
2801         const char *text;
2802         int rc;
2803         int ret;
2804
2805         if ( a ->a_vals == NULL ) {
2806                 return -1;
2807         }
2808         mr = a->a_desc->ad_type->sat_equality;
2809         for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2810                 rc = value_match( &ret, a->a_desc, mr,
2811                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2812                 if ( rc != LDAP_SUCCESS ) {
2813                         return -1;
2814                 }
2815                 if ( ret == 0 ) {
2816                         return 0;
2817                 }
2818         }
2819 #endif /* LDAP_SLAPI */
2820         return -1;
2821 }
2822
2823 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2824 {
2825 #ifdef LDAP_SLAPI
2826         AttributeDescription *a1 = NULL;
2827         AttributeDescription *a2 = NULL;
2828         const char *text;
2829         int ret;
2830
2831         if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2832                 return -1;
2833         }
2834
2835         if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2836                 return 1;
2837         }
2838
2839 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2840         ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2841                 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2842
2843         switch ( opt ) {
2844         case SLAPI_TYPE_CMP_EXACT:
2845                 ret = ad_cmp( a1, a2 );
2846                 break;
2847         case SLAPI_TYPE_CMP_BASE:
2848                 ret = ad_base_cmp( a1, a2 );
2849                 break;
2850         case SLAPI_TYPE_CMP_SUBTYPE:
2851                 ret = is_ad_subtype( a2, a2 );
2852                 break;
2853         default:
2854                 ret = -1;
2855                 break;
2856         }
2857
2858         return ret;
2859 #else
2860         return -1;
2861 #endif
2862 }
2863
2864 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2865 {
2866 #ifdef LDAP_SLAPI
2867         return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
2868 #else
2869         return -1;
2870 #endif
2871 }
2872
2873 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2874 {
2875 #ifdef LDAP_SLAPI
2876         return slapi_valueset_first_value( &a->a_vals, v );
2877 #else
2878         return -1;
2879 #endif
2880 }
2881
2882 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2883 {
2884 #ifdef LDAP_SLAPI
2885         return slapi_valueset_next_value( &a->a_vals, hint, v );
2886 #else
2887         return -1;
2888 #endif
2889 }
2890
2891 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2892 {
2893 #ifdef LDAP_SLAPI
2894         *numValues = slapi_valueset_count( &a->a_vals );
2895
2896         return 0;
2897 #else
2898         return -1;
2899 #endif
2900 }
2901
2902 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2903 {
2904 #ifdef LDAP_SLAPI
2905         *vs = &((Slapi_Attr *)a)->a_vals;
2906
2907         return 0;
2908 #else
2909         return -1;
2910 #endif
2911 }
2912
2913 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2914 {
2915 #ifdef LDAP_SLAPI
2916         return slapi_attr_get_values( a, vals );
2917 #else
2918         return -1;
2919 #endif
2920 }
2921
2922 char *slapi_attr_syntax_normalize( const char *s )
2923 {
2924 #ifdef LDAP_SLAPI
2925         AttributeDescription *ad = NULL;
2926         const char *text;
2927
2928         if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2929                 return NULL;
2930         }
2931
2932         return ad->ad_cname.bv_val;
2933 #else
2934         return -1;
2935 #endif
2936 }
2937
2938 Slapi_Value *slapi_value_new( void )
2939 {
2940 #ifdef LDAP_SLAPI
2941         struct berval *bv;
2942
2943         bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2944
2945         return bv;
2946 #else
2947         return NULL;
2948 #endif
2949 }
2950
2951 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2952 {
2953 #ifdef LDAP_SLAPI
2954         return ber_dupbv( NULL, (struct berval *)bval );
2955 #else
2956         return NULL;
2957 #endif
2958 }
2959
2960 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2961 {
2962 #ifdef LDAP_SLAPI
2963         return slapi_value_new_berval( v );
2964 #else
2965         return NULL;
2966 #endif
2967 }
2968
2969 Slapi_Value *slapi_value_new_string(const char *s)
2970 {
2971 #ifdef LDAP_SLAPI
2972         struct berval bv;
2973
2974         bv.bv_val = (char *)s;
2975         bv.bv_len = strlen( s );
2976
2977         return slapi_value_new_berval( &bv );
2978 #else
2979         return NULL;
2980 #endif
2981 }
2982
2983 Slapi_Value *slapi_value_init(Slapi_Value *val)
2984 {
2985 #ifdef LDAP_SLAPI
2986         val->bv_val = NULL;
2987         val->bv_len = 0;
2988
2989         return val;
2990 #else
2991         return NULL;
2992 #endif
2993 }
2994
2995 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2996 {
2997 #ifdef LDAP_SLAPI
2998         return ber_dupbv( v, bval );
2999 #else
3000         return NULL;
3001 #endif
3002 }
3003
3004 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
3005 {
3006 #ifdef LDAP_SLAPI
3007         v->bv_val = slapi_ch_strdup( (char *)s );
3008         v->bv_len = strlen( s );
3009
3010         return v;
3011 #else
3012         return NULL;
3013 #endif
3014 }
3015
3016 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
3017 {
3018 #ifdef LDAP_SLAPI
3019         return slapi_value_new_value( v );
3020 #else
3021         return NULL;
3022 #endif
3023 }
3024
3025 void slapi_value_free(Slapi_Value **value)
3026 {
3027 #ifdef LDAP_SLAPI       
3028         if ( value == NULL ) {
3029                 return;
3030         }
3031
3032         if ( (*value) != NULL ) {
3033                 slapi_ch_free( (void **)&(*value)->bv_val );
3034                 slapi_ch_free( (void **)value );
3035         }
3036 #endif
3037 }
3038
3039 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
3040 {
3041 #ifdef LDAP_SLAPI
3042         return value;
3043 #else
3044         return NULL;
3045 #endif
3046 }
3047
3048 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
3049 {
3050 #ifdef LDAP_SLAPI
3051         if ( value == NULL ) {
3052                 return NULL;
3053         }
3054         if ( value->bv_val != NULL ) {
3055                 slapi_ch_free( (void **)&value->bv_val );
3056         }
3057         slapi_value_init_berval( value, (struct berval *)bval );
3058
3059         return value;
3060 #else
3061         return NULL;
3062 #endif
3063 }
3064
3065 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
3066 {
3067 #ifdef LDAP_SLAPI
3068         if ( value == NULL ) {
3069                 return NULL;
3070         }
3071         return slapi_value_set_berval( value, vfrom );
3072 #else
3073         return NULL;
3074 #endif
3075 }
3076
3077 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
3078 {
3079 #ifdef LDAP_SLAPI
3080         if ( value == NULL ) {
3081                 return NULL;
3082         }
3083         if ( value->bv_val != NULL ) {
3084                 slapi_ch_free( (void **)&value->bv_val );
3085         }
3086         value->bv_val = slapi_ch_malloc( len );
3087         value->bv_len = len;
3088         AC_MEMCPY( value->bv_val, val, len );
3089
3090         return value;
3091 #else
3092         return NULL;
3093 #endif
3094 }
3095
3096 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
3097 {
3098 #ifdef LDAP_SLAPI
3099         if ( value == NULL ) {
3100                 return -1;
3101         }
3102         slapi_value_set( value, (void *)strVal, strlen( strVal ) );
3103         return 0;
3104 #else
3105         return NULL;
3106 #endif
3107 }
3108
3109 int slapi_value_set_int(Slapi_Value *value, int intVal)
3110 {
3111 #ifdef LDAP_SLAPI
3112         char buf[64];
3113
3114         snprintf( buf, sizeof( buf ), "%d", intVal );
3115
3116         return slapi_value_set_string( value, buf );
3117 #else
3118         return -1;
3119 #endif
3120 }
3121
3122 const char *slapi_value_get_string(const Slapi_Value *value)
3123 {
3124 #ifdef LDAP_SLAPI
3125         if ( value == NULL ) return NULL;
3126         if ( value->bv_val == NULL ) return NULL;
3127         if ( !checkBVString( value ) ) return NULL;
3128
3129         return value->bv_val;
3130 #else
3131         return NULL;
3132 #endif
3133 }
3134
3135 int slapi_value_get_int(const Slapi_Value *value)
3136 {
3137 #ifdef LDAP_SLAPI
3138         if ( value == NULL ) return 0;
3139         if ( value->bv_val == NULL ) return 0;
3140         if ( !checkBVString( value ) ) return 0;
3141
3142         return (int)strtol( value->bv_val, NULL, 10 );
3143 #else
3144         return NULL;
3145 #endif
3146 }
3147
3148 unsigned int slapi_value_get_uint(const Slapi_Value *value)
3149 {
3150 #ifdef LDAP_SLAPI
3151         if ( value == NULL ) return 0;
3152         if ( value->bv_val == NULL ) return 0;
3153         if ( !checkBVString( value ) ) return 0;
3154
3155         return (unsigned int)strtoul( value->bv_val, NULL, 10 );
3156 #else
3157         return NULL;
3158 #endif
3159 }
3160
3161 long slapi_value_get_long(const Slapi_Value *value)
3162 {
3163 #ifdef LDAP_SLAPI
3164         if ( value == NULL ) return 0;
3165         if ( value->bv_val == NULL ) return 0;
3166         if ( !checkBVString( value ) ) return 0;
3167
3168         return strtol( value->bv_val, NULL, 10 );
3169 #else
3170         return NULL;
3171 #endif
3172 }
3173
3174 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
3175 {
3176 #ifdef LDAP_SLAPI
3177         if ( value == NULL ) return 0;
3178         if ( value->bv_val == NULL ) return 0;
3179         if ( !checkBVString( value ) ) return 0;
3180
3181         return strtoul( value->bv_val, NULL, 10 );
3182 #else
3183         return NULL;
3184 #endif
3185 }
3186
3187 size_t slapi_value_get_length(const Slapi_Value *value)
3188 {
3189 #ifdef LDAP_SLAPI
3190         if ( value == NULL )
3191                 return 0;
3192
3193         return (size_t) value->bv_len;
3194 #else
3195         return 0;
3196 #endif
3197 }
3198
3199 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
3200 {
3201 #ifdef LDAP_SLAPI
3202         return slapi_attr_value_cmp( a, v1, v2 );
3203 #else
3204         return -1;
3205 #endif
3206 }
3207
3208 /* A ValueSet is a container for a BerVarray. */
3209 Slapi_ValueSet *slapi_valueset_new( void )
3210 {
3211 #ifdef LDAP_SLAPI
3212         Slapi_ValueSet *vs;
3213
3214         vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
3215         *vs = NULL;
3216
3217         return vs;
3218 #else
3219         return NULL;
3220 #endif
3221 }
3222
3223 void slapi_valueset_free(Slapi_ValueSet *vs)
3224 {
3225 #ifdef LDAP_SLAPI
3226         if ( vs != NULL ) {
3227                 BerVarray vp = *vs;
3228
3229                 ber_bvarray_free( vp );
3230                 slapi_ch_free( (void **)&vp );
3231
3232                 *vs = NULL;
3233         }
3234 #endif
3235 }
3236
3237 void slapi_valueset_init(Slapi_ValueSet *vs)
3238 {
3239 #ifdef LDAP_SLAPI
3240         if ( vs != NULL && *vs == NULL ) {
3241                 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
3242                 (*vs)->bv_val = NULL;
3243                 (*vs)->bv_len = 0;
3244         }
3245 #endif
3246 }
3247
3248 void slapi_valueset_done(Slapi_ValueSet *vs)
3249 {
3250 #ifdef LDAP_SLAPI
3251         BerVarray vp;
3252
3253         if ( vs == NULL )
3254                 return;
3255
3256         for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
3257                 vp->bv_len = 0;
3258                 slapi_ch_free( (void **)&vp->bv_val );
3259         }
3260         /* but don't free *vs or vs */
3261 #endif
3262 }
3263
3264 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
3265 {
3266 #ifdef LDAP_SLAPI
3267         struct berval bv;
3268
3269         ber_dupbv( &bv, (Slapi_Value *)addval );
3270         ber_bvarray_add( vs, &bv );
3271 #endif
3272 }
3273
3274 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
3275 {
3276 #ifdef LDAP_SLAPI
3277         return slapi_valueset_next_value( vs, 0, v );
3278 #else
3279         return -1;
3280 #endif
3281 }
3282
3283 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
3284 {
3285 #ifdef LDAP_SLAPI
3286         int i;
3287         BerVarray vp;
3288
3289         if ( vs == NULL )
3290                 return -1;
3291
3292         vp = *vs;
3293
3294         for ( i = 0; vp[i].bv_val != NULL; i++ ) {
3295                 if ( i == index ) {
3296                         *v = &vp[i];
3297                         return index + 1;
3298                 }
3299         }
3300 #endif
3301
3302         return -1;
3303 }
3304
3305 int slapi_valueset_count( const Slapi_ValueSet *vs )
3306 {
3307 #ifdef LDAP_SLAPI
3308         int i;
3309         BerVarray vp;
3310
3311         if ( vs == NULL )
3312                 return 0;
3313
3314         vp = *vs;
3315
3316         for ( i = 0; vp[i].bv_val != NULL; i++ )
3317                 ;
3318
3319         return i;
3320 #else
3321         return 0;
3322 #endif
3323
3324 }
3325
3326 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
3327 {
3328 #ifdef LDAP_SLAPI
3329         BerVarray vp;
3330
3331         for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
3332                 slapi_valueset_add_value( vs1, vp );
3333         }
3334 #endif
3335 }
3336
3337 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
3338         struct berval *val, int access )
3339 {
3340 #ifdef LDAP_SLAPI
3341         Backend *be;
3342         Connection *conn;
3343         Operation *op;
3344         int ret;
3345         slap_access_t slap_access;
3346         AttributeDescription *ad = NULL;
3347         const char *text;
3348
3349         ret = slap_str2ad( attr, &ad, &text );
3350         if ( ret != LDAP_SUCCESS ) {
3351                 return ret;
3352         }
3353
3354         switch ( access & SLAPI_ACL_ALL ) {
3355         case SLAPI_ACL_COMPARE:
3356                 slap_access = ACL_COMPARE;
3357                 break;
3358         case SLAPI_ACL_SEARCH:
3359                 slap_access = ACL_SEARCH;
3360                 break;
3361         case SLAPI_ACL_READ:
3362                 slap_access = ACL_READ;
3363                 break;
3364         case SLAPI_ACL_WRITE:
3365         case SLAPI_ACL_DELETE:
3366         case SLAPI_ACL_ADD:
3367         case SLAPI_ACL_SELF:
3368                 slap_access = ACL_WRITE;
3369                 break;
3370         default:
3371                 return LDAP_INSUFFICIENT_ACCESS;
3372                 break;
3373         }
3374
3375         if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
3376                 return LDAP_PARAM_ERROR;
3377         }
3378
3379         if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
3380                 return LDAP_PARAM_ERROR;
3381         }
3382
3383         if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3384                 return LDAP_PARAM_ERROR;
3385         }
3386
3387         ret = access_allowed( op, e, ad, val, slap_access, NULL );
3388
3389         return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3390 #else
3391         return LDAP_UNWILLING_TO_PERFORM;
3392 #endif
3393 }
3394
3395 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
3396 {
3397 #ifdef LDAP_SLAPI
3398         Operation *op;
3399         int rc = LDAP_SUCCESS;
3400         Modifications *ml, *mp;
3401
3402         if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
3403                 return LDAP_PARAM_ERROR;
3404         }
3405
3406         ml = slapi_int_ldapmods2modifications( mods );
3407         if ( ml == NULL ) {
3408                 return LDAP_OTHER;
3409         }
3410
3411         for ( mp = ml; mp != NULL; mp = mp->sml_next ) {
3412                 rc = slap_bv2ad( &mp->sml_type, &mp->sml_desc, (const char **)errbuf );
3413                 if ( rc != LDAP_SUCCESS ) {
3414                         break;
3415                 }
3416         }
3417
3418         if ( rc == LDAP_SUCCESS ) {
3419                 rc = acl_check_modlist( op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
3420         }
3421
3422         /* Careful when freeing the modlist because it has pointers into the mods array. */
3423         for ( ; ml != NULL; ml = mp ) {
3424                 mp = ml->sml_next;
3425
3426                 /* just free the containing array */
3427                 slapi_ch_free( (void **)&ml->sml_values );
3428                 slapi_ch_free( (void **)&ml );
3429         }
3430
3431         return rc;
3432 #else
3433         return LDAP_UNWILLING_TO_PERFORM;
3434 #endif
3435 }
3436
3437 /*
3438  * Synthesise an LDAPMod array from a Modifications list to pass
3439  * to SLAPI. This synthesis is destructive and as such the 
3440  * Modifications list may not be used after calling this 
3441  * function.
3442  * 
3443  * This function must also be called before slap_mods_check().
3444  */
3445 LDAPMod **slapi_int_modifications2ldapmods(Modifications **pmodlist)
3446 {
3447 #ifdef LDAP_SLAPI
3448         Modifications *ml, *modlist;
3449         LDAPMod **mods, *modp;
3450         int i, j;
3451
3452         modlist = *pmodlist;
3453
3454         for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
3455                 ;
3456
3457         mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
3458
3459         for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
3460                 mods[i] = (LDAPMod *)ch_malloc( sizeof(LDAPMod) );
3461                 modp = mods[i];
3462                 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
3463
3464                 /* Take ownership of original type. */
3465                 modp->mod_type = ml->sml_type.bv_val;
3466                 ml->sml_type.bv_val = NULL;
3467
3468                 if ( ml->sml_values != NULL ) {
3469                         for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
3470                                 ;
3471                         modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
3472                                 sizeof(struct berval *) );
3473                         for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
3474                                 /* Take ownership of original values. */
3475                                 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
3476                                 modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
3477                                 modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
3478                                 ml->sml_values[j].bv_len = 0;
3479                                 ml->sml_values[j].bv_val = NULL;
3480                         }
3481                         modp->mod_bvalues[j] = NULL;
3482                 } else {
3483                         modp->mod_bvalues = NULL;
3484                 }
3485                 i++;
3486         }
3487
3488         mods[i] = NULL;
3489
3490         slap_mods_free( modlist );
3491         *pmodlist = NULL;
3492
3493         return mods;
3494 #else
3495         return NULL;
3496 #endif
3497 }
3498
3499 /*
3500  * Convert a potentially modified array of LDAPMods back to a
3501  * Modification list. 
3502  * 
3503  * The returned Modification list contains pointers into the
3504  * LDAPMods array; the latter MUST be freed with
3505  * slapi_int_free_ldapmods() (see below).
3506  */
3507 Modifications *slapi_int_ldapmods2modifications (LDAPMod **mods)
3508 {
3509 #ifdef LDAP_SLAPI
3510         Modifications *modlist = NULL, **modtail;
3511         LDAPMod **modp;
3512
3513         modtail = &modlist;
3514
3515         for( modp = mods; *modp != NULL; modp++ ) {
3516                 Modifications *mod;
3517                 int i;
3518                 char **p;
3519                 struct berval **bvp;
3520
3521                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
3522                 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
3523                 mod->sml_type.bv_val = (*modp)->mod_type;
3524                 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
3525                 mod->sml_desc = NULL;
3526                 mod->sml_next = NULL;
3527
3528                 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3529                         for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ )
3530                                 ;
3531                 } else {
3532                         for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ )
3533                                 ;
3534                 }
3535
3536                 if ( i == 0 ) {
3537                         mod->sml_values = NULL;
3538                 } else {
3539                         mod->sml_values = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
3540
3541                         /* NB: This implicitly trusts a plugin to return valid modifications. */
3542                         if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
3543                                 for( i = 0, bvp = (*modp)->mod_bvalues; bvp != NULL && *bvp != NULL; bvp++, i++ ) {
3544                                         mod->sml_values[i].bv_val = (*bvp)->bv_val;
3545                                         mod->sml_values[i].bv_len = (*bvp)->bv_len;
3546                                 }
3547                         } else {
3548                                 for( i = 0, p = (*modp)->mod_values; p != NULL && *p != NULL; p++, i++ ) {
3549                                         mod->sml_values[i].bv_val = *p;
3550                                         mod->sml_values[i].bv_len = strlen( *p );
3551                                 }
3552                         }
3553                         mod->sml_values[i].bv_val = NULL;
3554                         mod->sml_values[i].bv_len = 0;
3555                 }
3556                 mod->sml_nvalues = NULL;
3557
3558                 *modtail = mod;
3559                 modtail = &mod->sml_next;
3560         }
3561         
3562         return modlist;
3563 #else
3564         return NULL;
3565 #endif 
3566 }
3567
3568 /*
3569  * This function only frees the parts of the mods array that
3570  * are not shared with the Modification list that was created
3571  * by slapi_int_ldapmods2modifications(). 
3572  *
3573  */
3574 void slapi_int_free_ldapmods (LDAPMod **mods)
3575 {
3576 #ifdef LDAP_SLAPI
3577         int i, j;
3578
3579         if (mods == NULL)
3580                 return;
3581
3582         for ( i = 0; mods[i] != NULL; i++ ) {
3583                 /*
3584                  * Don't free values themselves; they're owned by the
3585                  * Modification list. Do free the containing array.
3586                  */
3587                 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
3588                         for ( j = 0; mods[i]->mod_values != NULL && mods[i]->mod_values[j] != NULL; j++ ) {
3589                                 ch_free( mods[i]->mod_values[j] );
3590                         }
3591                         ch_free( mods[i]->mod_values );
3592                 } else {
3593                         ch_free( mods[i]->mod_values );
3594                 }
3595                 /* Don't free type, for same reasons. */
3596                 ch_free( mods[i] );
3597         }
3598         ch_free( mods );
3599 #endif /* LDAP_SLAPI */
3600 }
3601
3602 /*
3603  * Sun ONE DS 5.x computed attribute support. Computed attributes
3604  * allow for dynamically generated operational attributes, a very
3605  * useful thing indeed.
3606  */
3607
3608 /*
3609  * Write the computed attribute to a BerElement. Complementary 
3610  * functions need to be defined for anything that replaces 
3611  * op->o_callback->sc_sendentry, if you wish to make computed
3612  * attributes available to it.
3613  */
3614 int slapi_int_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
3615 {
3616 #ifdef LDAP_SLAPI
3617         Operation *op = NULL;
3618         BerElement *ber;
3619         AttributeDescription *desc = NULL;
3620         int rc;
3621         int i;
3622
3623         if ( c == NULL ) {
3624                 return 1;
3625         }
3626
3627         if ( a == NULL ) {
3628                 return 1;
3629         }
3630
3631         if ( e == NULL ) {
3632                 return 1;
3633         }
3634
3635         rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
3636         if ( rc != 0 || op == NULL ) {
3637                 return rc;
3638         }
3639
3640         ber = (BerElement *)c->cac_private;
3641         desc = a->a_desc;
3642
3643         if ( c->cac_attrs == NULL ) {
3644                 /* All attrs request, skip operational attributes */
3645                 if ( is_at_operational( desc->ad_type ) ) {
3646                         return 0;
3647                 }
3648         } else {
3649                 /* Specific attrs requested */
3650                 if ( is_at_operational( desc->ad_type ) ) {
3651                         if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3652                                 return 0;
3653                         }
3654                 } else {
3655                         if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
3656                                 return 0;
3657                         }
3658                 }
3659         }
3660
3661         if ( !access_allowed( op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
3662                 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3663                         "acl: access to attribute %s not allowed\n",
3664                         desc->ad_cname.bv_val );
3665                 return 0;
3666         }
3667
3668         rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
3669         if (rc == -1 ) {
3670                 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3671                         "ber_printf failed\n");
3672                 return 1;
3673         }
3674
3675         if ( !c->cac_attrsonly && a->a_vals != NULL ) {
3676                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
3677                         if ( !access_allowed( op, e,
3678                                 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
3679                                 slapi_log_error( SLAPI_LOG_ACL, "slapi_int_compute_output_ber",
3680                                         "conn %lu "
3681                                         "acl: access to %s, value %d not allowed\n",
3682                                         op->o_connid, desc->ad_cname.bv_val, i  );
3683                                 continue;
3684                         }
3685         
3686                         if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
3687                                 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3688                                         "ber_printf failed\n");
3689                                 return 1;
3690                         }
3691                 }
3692         }
3693
3694         if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
3695                 slapi_log_error( SLAPI_LOG_BER, "slapi_int_compute_output_ber",
3696                         "ber_printf failed\n" );
3697                 return 1;
3698         }
3699
3700         return 0;
3701 #else
3702         return 1;
3703 #endif
3704 }
3705
3706 /*
3707  * For some reason Sun don't use the normal plugin mechanism
3708  * registration path to register an "evaluator" function (an
3709  * "evaluator" is responsible for adding computed attributes;
3710  * the nomenclature is somewhat confusing).
3711  *
3712  * As such slapi_compute_add_evaluator() registers the 
3713  * function directly.
3714  */
3715 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
3716 {
3717 #ifdef LDAP_SLAPI
3718         Slapi_PBlock *pPlugin = NULL;
3719         int rc;
3720
3721         pPlugin = slapi_pblock_new();
3722         if ( pPlugin == NULL ) {
3723                 rc = LDAP_NO_MEMORY;
3724                 goto done;
3725         }
3726
3727         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3728         if ( rc != LDAP_SUCCESS ) {
3729                 goto done;
3730         }
3731
3732         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
3733         if ( rc != LDAP_SUCCESS ) {
3734                 goto done;
3735         }
3736
3737         rc = slapi_int_register_plugin( NULL, pPlugin );
3738         if ( rc != 0 ) {
3739                 rc = LDAP_OTHER;
3740                 goto done;
3741         }
3742
3743 done:
3744         if ( rc != LDAP_SUCCESS ) {
3745                 if ( pPlugin != NULL ) {
3746                         slapi_pblock_destroy( pPlugin );
3747                 }
3748                 return -1;
3749         }
3750
3751         return 0;
3752 #else
3753         return -1;
3754 #endif /* LDAP_SLAPI */
3755 }
3756
3757 /*
3758  * See notes above regarding slapi_compute_add_evaluator().
3759  */
3760 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
3761 {
3762 #ifdef LDAP_SLAPI
3763         Slapi_PBlock *pPlugin = NULL;
3764         int rc;
3765
3766         pPlugin = slapi_pblock_new();
3767         if ( pPlugin == NULL ) {
3768                 rc = LDAP_NO_MEMORY;
3769                 goto done;
3770         }
3771
3772         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
3773         if ( rc != LDAP_SUCCESS ) {
3774                 goto done;
3775         }
3776
3777         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
3778         if ( rc != LDAP_SUCCESS ) {
3779                 goto done;
3780         }
3781
3782         rc = slapi_int_register_plugin( NULL, pPlugin );
3783         if ( rc != 0 ) {
3784                 rc = LDAP_OTHER;
3785                 goto done;
3786         }
3787
3788 done:
3789         if ( rc != LDAP_SUCCESS ) {
3790                 if ( pPlugin != NULL ) {
3791                         slapi_pblock_destroy( pPlugin );
3792                 }
3793                 return -1;
3794         }
3795
3796         return 0;
3797 #else
3798         return -1;
3799 #endif /* LDAP_SLAPI */
3800 }
3801
3802 /*
3803  * Call compute evaluators
3804  */
3805 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
3806 {
3807 #ifdef LDAP_SLAPI
3808         int rc = 0;
3809         slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
3810
3811         rc = slapi_int_get_plugins( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
3812         if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3813                 /* Nothing to do; front-end should ignore. */
3814                 return 0;
3815         }
3816
3817         for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3818                 /*
3819                  * -1: no attribute matched requested type
3820                  *  0: one attribute matched
3821                  * >0: error happened
3822                  */
3823                 rc = (*pGetPlugin)( c, type, e, outputfn );
3824                 if ( rc > 0 ) {
3825                         break;
3826                 }
3827         }
3828
3829         slapi_ch_free( (void **)&tmpPlugin );
3830
3831         return rc;
3832 #else
3833         return 1;
3834 #endif /* LDAP_SLAPI */
3835 }
3836
3837 int compute_rewrite_search_filter(Slapi_PBlock *pb)
3838 {
3839 #ifdef LDAP_SLAPI
3840         Backend *be;
3841         int rc;
3842
3843         rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
3844         if ( rc != 0 ) {
3845                 return rc;
3846         }
3847
3848         return slapi_int_call_plugins( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3849 #else
3850         return -1;
3851 #endif /* LDAP_SLAPI */
3852 }
3853
3854 /*
3855  * New API to provide the plugin with access to the search
3856  * pblock. Have informed Sun DS team.
3857  */
3858 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3859 {
3860 #ifdef LDAP_SLAPI
3861         if ( c == NULL )
3862                 return -1;
3863
3864         if ( c->cac_pb == NULL )
3865                 return -1;
3866
3867         *pb = c->cac_pb;
3868
3869         return 0;
3870 #else
3871         return -1;
3872 #endif /* LDAP_SLAPI */
3873 }
3874
3875 Slapi_Mutex *slapi_new_mutex( void )
3876 {
3877 #ifdef LDAP_SLAPI
3878         Slapi_Mutex *m;
3879
3880         m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3881         if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3882                 slapi_ch_free( (void **)&m );
3883                 return NULL;
3884         }
3885
3886         return m;
3887 #else
3888         return NULL;
3889 #endif
3890 }
3891
3892 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3893 {
3894 #ifdef LDAP_SLAPI
3895         if ( mutex != NULL ) {
3896                 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3897                 slapi_ch_free( (void **)&mutex);
3898         }
3899 #endif
3900 }
3901
3902 void slapi_lock_mutex( Slapi_Mutex *mutex )
3903 {
3904 #ifdef LDAP_SLAPI
3905         ldap_pvt_thread_mutex_lock( &mutex->mutex );
3906 #endif
3907 }
3908
3909 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3910 {
3911 #ifdef LDAP_SLAPI
3912         return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3913 #else
3914         return -1;
3915 #endif
3916 }
3917
3918 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3919 {
3920 #ifdef LDAP_SLAPI
3921         Slapi_CondVar *cv;
3922
3923         if ( mutex == NULL ) {
3924                 return NULL;
3925         }
3926
3927         cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3928         if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3929                 slapi_ch_free( (void **)&cv );
3930                 return NULL;
3931         }
3932
3933         /* XXX struct copy */
3934         cv->mutex = mutex->mutex;
3935
3936         return cv;
3937 #else   
3938         return NULL;
3939 #endif
3940 }
3941
3942 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3943 {
3944 #ifdef LDAP_SLAPI
3945         if ( cvar != NULL ) {
3946                 ldap_pvt_thread_cond_destroy( &cvar->cond );
3947                 slapi_ch_free( (void **)&cvar );
3948         }
3949 #endif
3950 }
3951
3952 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3953 {
3954 #ifdef LDAP_SLAPI
3955         if ( cvar == NULL ) {
3956                 return -1;
3957         }
3958
3959         return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3960 #else
3961         return -1;
3962 #endif
3963 }
3964
3965 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3966 {
3967 #ifdef LDAP_SLAPI
3968         if ( cvar == NULL ) {
3969                 return -1;
3970         }
3971
3972         if ( notify_all ) {
3973                 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3974         }
3975
3976         return ldap_pvt_thread_cond_signal( &cvar->cond );
3977 #else
3978         return -1;
3979 #endif
3980 }
3981
3982 int slapi_int_access_allowed( Operation *op,
3983         Entry *entry,
3984         AttributeDescription *desc,
3985         struct berval *val,
3986         slap_access_t access,
3987         AccessControlState *state )
3988 {
3989 #ifdef LDAP_SLAPI
3990         int rc, slap_access = 0;
3991         slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3992
3993         if ( op->o_pb == NULL ) {
3994                 /* internal operation */
3995                 return 1;
3996         }
3997
3998         switch ( access ) {
3999         case ACL_WRITE:
4000                 slap_access |= SLAPI_ACL_ADD | SLAPI_ACL_DELETE | SLAPI_ACL_WRITE;
4001                 break;
4002         case ACL_READ:
4003                 slap_access |= SLAPI_ACL_READ;
4004                 break;
4005         case ACL_SEARCH:
4006                 slap_access |= SLAPI_ACL_SEARCH;
4007                 break;
4008         case ACL_COMPARE:
4009                 slap_access = ACL_COMPARE;
4010                 break;
4011         default:
4012                 break;
4013         }
4014
4015         rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
4016         if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
4017                 /* nothing to do; allowed access */
4018                 return 1;
4019         }
4020
4021         slapi_int_pblock_set_operation( op->o_pb, op );
4022
4023         rc = 1; /* default allow policy */
4024
4025         for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
4026                 /*
4027                  * 0    access denied
4028                  * 1    access granted
4029                  */
4030                 rc = (*pGetPlugin)( op->o_pb, entry, desc->ad_cname.bv_val,
4031                                         val, slap_access, (void *)state );
4032                 if ( rc == 0 ) {
4033                         break;
4034                 }
4035         }
4036
4037         slapi_ch_free( (void **)&tmpPlugin );
4038
4039         return rc;
4040 #else
4041         return 1;
4042 #endif /* LDAP_SLAPI */
4043 }
4044
4045 /*
4046  * There is no documentation for this.
4047  */
4048 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
4049 {
4050 #ifdef LDAP_SLAPI
4051         LDAPRDN lrdn;
4052         LDAPAVA *ava;
4053         int rc;
4054         char *p;
4055
4056         *type = NULL;
4057
4058         bv->bv_len = 0;
4059         bv->bv_val = NULL;
4060
4061         rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
4062         if ( rc != LDAP_SUCCESS ) {
4063                 return -1;
4064         }
4065
4066         if ( lrdn[1] != NULL ) {
4067                 return -1; /* not single valued */
4068         }
4069
4070         ava = lrdn[0];
4071
4072         *type = slapi_ch_strdup( ava->la_attr.bv_val );
4073         ber_dupbv( bv, &ava->la_value );
4074
4075         ldap_rdnfree(lrdn);
4076
4077         return 0;
4078 #else
4079         return -1;
4080 #endif /* LDAP_SLAPI */
4081 }
4082
4083 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
4084 {
4085 #ifdef LDAP_SLAPI
4086         struct berval new_dn, parent_dn, newrdn;
4087
4088         new_dn.bv_val = NULL;
4089
4090         parent_dn.bv_val = (char *)dn;
4091         parent_dn.bv_len = strlen( dn );
4092
4093         newrdn.bv_val = (char *)rdn;
4094         newrdn.bv_len = strlen( rdn );
4095
4096         build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
4097
4098         return new_dn.bv_val;
4099 #else
4100         return NULL;
4101 #endif /* LDAP_SLAPI */
4102 }
4103
4104 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
4105 {
4106 #ifdef LDAP_SLAPI
4107         Backend *be;
4108         const char *text;
4109         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
4110         size_t textlen = sizeof textbuf;
4111         int rc;
4112
4113         if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void **)&be ) != 0 )
4114                 return -1;
4115
4116         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
4117
4118         return ( rc == LDAP_SUCCESS ) ? 0 : 1;
4119 #else
4120         return -1;
4121 #endif /* LDAP_SLAPI */
4122 }
4123
4124 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
4125 {
4126 #ifdef LDAP_SLAPI
4127         LDAPDN dn;
4128         int rc;
4129         int i = 0, match = 0;
4130
4131         rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4132         if ( rc != LDAP_SUCCESS ) {
4133                 return 0;
4134         }
4135
4136         if ( dn[0] != NULL ) {
4137                 LDAPRDN rdn = dn[0];
4138
4139                 for ( i = 0; rdn[i] != NULL; i++ ) {
4140                         LDAPAVA *ava = &rdn[0][i];
4141                         Slapi_Attr *a = NULL;
4142
4143                         if ( slapi_entry_attr_find( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
4144                              slapi_attr_value_find( a, &ava->la_value ) == 0 )
4145                                 match++;
4146                 }
4147         }
4148
4149         ldap_dnfree( dn );
4150
4151         return ( i == match );
4152 #else
4153         return 0;
4154 #endif /* LDAP_SLAPI */
4155 }
4156
4157 int slapi_entry_add_rdn_values( Slapi_Entry *e )
4158 {
4159 #ifdef LDAP_SLAPI
4160         LDAPDN dn;
4161         int i, rc;
4162
4163         rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
4164         if ( rc != LDAP_SUCCESS ) {
4165                 return rc;
4166         }
4167
4168         if ( dn[0] != NULL ) {
4169                 LDAPRDN rdn = dn[0];
4170                 struct berval *vals[2];
4171
4172                 for ( i = 0; rdn[i] != NULL; i++ ) {
4173                         LDAPAVA *ava = &rdn[0][i];
4174                         Slapi_Attr *a = NULL;
4175
4176                         if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
4177                              slapi_attr_value_find( a, &ava->la_value ) == 0 )
4178                                 continue;
4179
4180                         vals[0] = &ava->la_value;
4181                         vals[1] = NULL;
4182
4183                         slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
4184                 }
4185         }
4186
4187         ldap_dnfree( dn );
4188
4189         return LDAP_SUCCESS;
4190 #else
4191         return LDAP_OTHER;
4192 #endif /* LDAP_SLAPI */
4193 }
4194
4195 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
4196 {
4197 #ifdef LDAP_SLAPI
4198         Attribute *attr;
4199         const char *uniqueid;
4200
4201         attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
4202         if ( attr == NULL ) {
4203                 return NULL;
4204         }
4205
4206         if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
4207                 return slapi_value_get_string( &attr->a_vals[0] );
4208         }
4209 #endif /* LDAP_SLAPI */
4210
4211         return NULL;
4212 }
4213
4214 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
4215 {
4216 #ifdef LDAP_SLAPI
4217         struct berval bv;
4218
4219         attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
4220
4221         bv.bv_val = uniqueid;
4222         bv.bv_len = strlen( uniqueid );
4223         attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
4224 #endif /* LDAP_SLAPI */
4225 }
4226
4227 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
4228 {
4229 #ifdef LDAP_SLAPI
4230         LDAP *ld;
4231         char *url;
4232         size_t size;
4233         int rc;
4234
4235         size = sizeof("ldap:///");
4236         if ( secure )
4237                 size++;
4238         size += strlen( ldaphost );
4239         if ( ldapport != 0 )
4240                 size += 32;
4241
4242         url = slapi_ch_malloc( size );
4243
4244         if ( ldapport != 0 ) {
4245                 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
4246         } else {
4247                 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
4248         }
4249
4250         rc = ldap_initialize( &ld, url );
4251
4252         slapi_ch_free_string( &url );
4253
4254         return ( rc == LDAP_SUCCESS ) ? ld : NULL;
4255 #else
4256         return NULL;
4257 #endif /* LDAP_SLAPI */
4258 }
4259
4260 void slapi_ldap_unbind( LDAP *ld )
4261 {
4262 #ifdef LDAP_SLAPI
4263         ldap_unbind( ld );
4264 #endif /* LDAP_SLAPI */
4265 }
4266