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