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