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