]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_utils.c
47095742a29f34190e33a033e840f9aa0a7e381d
[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                 *type = slapi_ch_strdup( f->f_un.f_un_ava->aa_desc->ad_cname.bv_val );
1103                 if ( *type == NULL ) {
1104                         rc = LDAP_NO_MEMORY;
1105                         goto done;
1106                 }
1107
1108                 *bval = ber_dupbv( NULL, &f->f_un.f_un_ava->aa_value );
1109                 if ( *bval == NULL ) {
1110                         rc = LDAP_NO_MEMORY;
1111                         goto done;
1112                 }
1113         } else { /* filter type not supported */
1114                 rc = -1;
1115         }
1116
1117 done:
1118         if ( rc != LDAP_SUCCESS ) {
1119                 if ( *bval ) {
1120                         ch_free( *bval );
1121                         *bval = NULL;
1122                 }
1123
1124                 if ( *type ) {
1125                         ch_free( *type );
1126                         *type = NULL;
1127                 }
1128         }
1129
1130         return rc;
1131 #else /* !defined(LDAP_SLAPI) */
1132         return -1;
1133 #endif /* !defined(LDAP_SLAPI) */
1134 }
1135
1136 Slapi_Filter *
1137 slapi_filter_list_first( Slapi_Filter *f )
1138 {
1139 #if defined(LDAP_SLAPI)
1140         int             ftype;
1141
1142         if ( f == NULL ) {
1143                 return NULL;
1144         }
1145
1146         ftype = f->f_choice;
1147         if ( ftype == LDAP_FILTER_AND
1148                         || ftype == LDAP_FILTER_OR
1149                         || ftype == LDAP_FILTER_NOT ) {
1150                 return (Slapi_Filter *)f->f_and;
1151         } else {
1152                 return NULL;
1153         }
1154 #else /* !defined(LDAP_SLAPI) */
1155         return NULL;
1156 #endif /* !defined(LDAP_SLAPI) */
1157 }
1158
1159 Slapi_Filter *
1160 slapi_filter_list_next(
1161         Slapi_Filter    *f, 
1162         Slapi_Filter    *fprev )
1163 {
1164 #if defined(LDAP_SLAPI)
1165         int             ftype;
1166
1167         if ( f == NULL ) {
1168                 return NULL;
1169         }
1170
1171         ftype = f->f_choice;
1172         if ( ftype == LDAP_FILTER_AND
1173                         || ftype == LDAP_FILTER_OR
1174                         || ftype == LDAP_FILTER_NOT ) {
1175                 if ( f->f_and == fprev ) {
1176                         return f->f_and->f_next;
1177                 }
1178         }
1179
1180         return NULL;
1181 #else /* !defined(LDAP_SLAPI) */
1182         return NULL;
1183 #endif /* !defined(LDAP_SLAPI) */
1184 }
1185
1186 int 
1187 slapi_send_ldap_extended_response(
1188         Connection      *conn, 
1189         Operation       *op,
1190         int             errornum, 
1191         char            *respName,
1192         struct berval   *response )
1193 {
1194 #if defined(LDAP_SLAPI)
1195         send_ldap_extended( conn,op, errornum, NULL, NULL, NULL,
1196                         respName,response, NULL );
1197         return LDAP_SUCCESS;
1198 #else /* !defined(LDAP_SLAPI) */
1199         return -1;
1200 #endif /* !defined(LDAP_SLAPI) */
1201 }
1202
1203 int 
1204 slapi_pw_find(
1205         struct berval   **vals, 
1206         struct berval   *v ) 
1207 {
1208 #if defined(LDAP_SLAPI)
1209         /*
1210          * FIXME: what's the point?
1211          */
1212         return 1;
1213 #else /* !defined(LDAP_SLAPI) */
1214         return 1;
1215 #endif /* !defined(LDAP_SLAPI) */
1216 }
1217              
1218 char *
1219 slapi_get_hostname( void ) 
1220 {
1221 #if defined(LDAP_SLAPI)
1222         char            *hn = NULL;
1223
1224         /*
1225          * FIXME: I'd prefer a different check ...
1226          */
1227 #if defined _SPARC 
1228         hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1229         if ( hn == NULL) {
1230                 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1231                                 "can't malloc memory for hostname\n" );
1232                 hn = NULL;
1233                 
1234         } else if ( sysinfo( SI_HOSTNAME, hn, MAX_HOSTNAME ) < 0 ) {
1235                 slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1236                                 "can't get hostname\n" );
1237                 slapi_ch_free( (void **)&hn );
1238                 hn = NULL;
1239         }
1240 #else /* !_SPARC */
1241         static int      been_here = 0;   
1242         static char     *static_hn = NULL;
1243
1244         ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1245         if ( !been_here ) {
1246                 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1247                 if ( static_hn == NULL) {
1248                         slapi_log_error( SLAPI_LOG_FATAL, "SLAPI_SYSINFO",
1249                                         "can't malloc memory for hostname\n" );
1250                         static_hn = NULL;
1251                         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1252
1253                         return hn;
1254                         
1255                 } else { 
1256                         if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1257                                 slapi_log_error( SLAPI_LOG_FATAL,
1258                                                 "SLAPI_SYSINFO",
1259                                                 "can't get hostname\n" );
1260                                 slapi_ch_free( (void **)&static_hn );
1261                                 static_hn = NULL;
1262                                 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1263
1264                                 return hn;
1265
1266                         } else {
1267                                 been_here = 1;
1268                         }
1269                 }
1270         }
1271         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1272         
1273         hn = ch_strdup( static_hn );
1274 #endif /* !_SPARC */
1275
1276         return hn;
1277 #else /* !defined(LDAP_SLAPI) */
1278         return NULL;
1279 #endif /* !defined(LDAP_SLAPI) */
1280 }
1281
1282 /*
1283  * FIXME: this should go in an appropriate header ...
1284  */
1285 extern int vLogError( int level, char *subsystem, char *fmt, va_list arglist );
1286
1287 int 
1288 slapi_log_error(
1289         int             severity, 
1290         char            *subsystem, 
1291         char            *fmt, 
1292         ... ) 
1293 {
1294 #if defined(LDAP_SLAPI)
1295         int             rc = LDAP_SUCCESS;
1296         va_list         arglist;
1297
1298         va_start( arglist, fmt );
1299         rc = vLogError( severity, subsystem, fmt, arglist );
1300         va_end( arglist );
1301
1302         return rc;
1303 #else /* !defined(LDAP_SLAPI) */
1304         return -1;
1305 #endif /* !defined(LDAP_SLAPI) */
1306 }
1307
1308
1309 unsigned long
1310 slapi_timer_current_time( void ) 
1311 {
1312 #if defined(LDAP_SLAPI)
1313         static int      first_time = 1;
1314 #if !defined (_WIN32)
1315         struct timeval  now;
1316         unsigned long   ret;
1317
1318         ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1319         if (first_time) {
1320                 first_time = 0;
1321                 gettimeofday( &base_time, NULL );
1322         }
1323         gettimeofday( &now, NULL );
1324         ret = ( now.tv_sec  - base_time.tv_sec ) * 1000000 + 
1325                         (now.tv_usec - base_time.tv_usec);
1326         ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1327
1328         return ret;
1329
1330         /*
1331          * Ain't it better?
1332         return (slap_get_time() - starttime) * 1000000;
1333          */
1334 #else /* _WIN32 */
1335         LARGE_INTEGER now;
1336
1337         if ( first_time ) {
1338                 first_time = 0;
1339                 performance_counter_present = QueryPerformanceCounter( &base_time );
1340                 QueryPerformanceFrequency( &performance_freq );
1341         }
1342
1343         if ( !performance_counter_present )
1344              return 0;
1345
1346         QueryPerformanceCounter( &now );
1347         return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1348 #endif /* _WIN32 */
1349 #else /* !defined(LDAP_SLAPI) */
1350         return 0;
1351 #endif /* !defined(LDAP_SLAPI) */
1352 }
1353
1354 /*
1355  * FIXME ?
1356  */
1357 unsigned long
1358 slapi_timer_get_time( char *label ) 
1359 {
1360 #if defined(LDAP_SLAPI)
1361         unsigned long start = slapi_timer_current_time();
1362         printf("%10ld %10ld usec %s\n", start, 0, label);
1363         return start;
1364 #else /* !defined(LDAP_SLAPI) */
1365         return 0;
1366 #endif /* !defined(LDAP_SLAPI) */
1367 }
1368
1369 /*
1370  * FIXME ?
1371  */
1372 void
1373 slapi_timer_elapsed_time(
1374         char *label,
1375         unsigned long start ) 
1376 {
1377 #if defined(LDAP_SLAPI)
1378         unsigned long stop = slapi_timer_current_time();
1379         printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
1380 #endif /* defined(LDAP_SLAPI) */
1381 }
1382
1383 void
1384 slapi_free_search_results_internal( Slapi_PBlock *pb ) 
1385 {
1386 #if defined(LDAP_SLAPI)
1387         Slapi_Entry     **entries;
1388         int             k = 0, nEnt = 0;
1389
1390         slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
1391         slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
1392         if ( nEnt == 0 ) {
1393                 return;
1394         }
1395         
1396         if ( entries == NULL ) {
1397                 return;
1398         }
1399         
1400         for ( k = 0; k < nEnt; k++ ) {
1401                 slapi_entry_free( entries[k] );
1402         }
1403         
1404         slapi_ch_free( (void **)&entries );
1405 #endif /* defined(LDAP_SLAPI) */
1406 }
1407
1408 /*
1409  * Internal API to prime a Slapi_PBlock with a Backend.
1410  */
1411 int slapi_x_backend_set_pb( Slapi_PBlock *pb, Backend *be )
1412 {
1413 #if defined(LDAP_SLAPI)
1414         int rc;
1415         
1416         rc = slapi_pblock_set( pb, SLAPI_BACKEND, (void *)be );
1417         if ( rc != LDAP_SUCCESS )
1418                 return rc;
1419         
1420         if ( be != NULL ) {
1421                 rc = slapi_pblock_set( pb, SLAPI_BE_TYPE, (void *)be->bd_info->bi_type );
1422                 if ( rc != LDAP_SUCCESS )
1423                         return rc;
1424         }
1425
1426         return LDAP_SUCCESS;
1427 #else
1428         return -1;
1429 #endif /* defined(LDAP_SLAPI) */
1430 }
1431
1432 #if defined(LDAP_SLAPI)
1433 /*
1434  * If oldStyle is TRUE, then a value suitable for setting to
1435  * the deprecated SLAPI_CONN_AUTHTYPE value is returned 
1436  * (pointer to static storage).
1437  *
1438  * If oldStyle is FALSE, then a value suitable for setting to
1439  * the new SLAPI_CONN_AUTHMETHOD will be returned, which is
1440  * a pointer to allocated memory and will include the SASL
1441  * mechanism (if any).
1442  */
1443 static char *Authorization2AuthType( AuthorizationInformation *authz, int is_tls, int oldStyle )
1444 {
1445         size_t len;
1446         char *authType;
1447
1448         switch ( authz->sai_method ) {
1449         case LDAP_AUTH_SASL:
1450                 if ( oldStyle ) {
1451                         authType = SLAPD_AUTH_SASL;
1452                 } else {
1453                         len = sizeof(SLAPD_AUTH_SASL) + authz->sai_mech.bv_len;
1454                         authType = slapi_ch_malloc( len );
1455                         snprintf( authType, len, "%s%s", SLAPD_AUTH_SASL, authz->sai_mech.bv_val );
1456                 }
1457                 break;
1458         case LDAP_AUTH_SIMPLE:
1459                 authType = oldStyle ? SLAPD_AUTH_SIMPLE : slapi_ch_strdup( SLAPD_AUTH_SIMPLE );
1460                 break;
1461         case LDAP_AUTH_NONE:
1462                 authType = oldStyle ? SLAPD_AUTH_NONE : slapi_ch_strdup( SLAPD_AUTH_NONE );
1463                 break;
1464         default:
1465                 authType = NULL;
1466                 break;
1467         }
1468         if ( is_tls && authType == NULL ) {
1469                 authType = oldStyle ? SLAPD_AUTH_SSL : slapi_ch_strdup( SLAPD_AUTH_SSL );
1470         }
1471
1472         return authType;
1473 }
1474 #endif
1475
1476 /*
1477  * Internal API to prime a Slapi_PBlock with a Connection.
1478  */
1479 int slapi_x_connection_set_pb( Slapi_PBlock *pb, Connection *conn )
1480 {
1481 #if defined(LDAP_SLAPI)
1482         char *connAuthType;
1483         int rc;
1484
1485         rc = slapi_pblock_set( pb, SLAPI_CONNECTION, (void *)conn );
1486         if ( rc != LDAP_SUCCESS )
1487                 return rc;
1488
1489         if ( strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0 ) {
1490                 rc = slapi_pblock_set( pb, SLAPI_CONN_CLIENTIP, (void *)&conn->c_peer_name.bv_val[3] );
1491                 if ( rc != LDAP_SUCCESS )
1492                         return rc;
1493         } else if ( strncmp( conn->c_peer_name.bv_val, "PATH=", 5 ) == 0 ) {
1494                 rc = slapi_pblock_set( pb, SLAPI_X_CONN_CLIENTPATH, (void *)&conn->c_peer_name.bv_val[5] );
1495                 if ( rc != LDAP_SUCCESS )
1496                         return rc;
1497         }
1498
1499         if ( strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0 ) {
1500                 rc = slapi_pblock_set( pb, SLAPI_CONN_SERVERIP, (void *)&conn->c_sock_name.bv_val[3] );
1501                 if ( rc != LDAP_SUCCESS )
1502                         return rc;
1503         } else if ( strncmp( conn->c_sock_name.bv_val, "PATH=", 5 ) == 0 ) {
1504                 rc = slapi_pblock_set( pb, SLAPI_X_CONN_SERVERPATH, (void *)&conn->c_sock_name.bv_val[5] );
1505                 if ( rc != LDAP_SUCCESS )
1506                         return rc;
1507         }
1508
1509 #ifdef LDAP_CONNECTIONLESS
1510         rc = slapi_pblock_set( pb, SLAPI_X_CONN_IS_UDP, (void *)conn->c_is_udp );
1511         if ( rc != LDAP_SUCCESS )
1512                 return rc;
1513 #endif
1514
1515         rc = slapi_pblock_set( pb, SLAPI_CONN_ID, (void *)conn->c_connid );
1516         if ( rc != LDAP_SUCCESS )
1517                 return rc;
1518
1519         /* Returns pointer to static string */
1520         connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 1 );
1521         if ( connAuthType != NULL ) {
1522                 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, (void *)connAuthType);
1523                 if ( rc != LDAP_SUCCESS )
1524                         return rc;
1525         }
1526
1527         /* Returns pointer to allocated string */
1528         connAuthType = Authorization2AuthType( &conn->c_authz, conn->c_is_tls, 0 );
1529         if ( connAuthType != NULL ) {
1530                 rc = slapi_pblock_set(pb, SLAPI_CONN_AUTHMETHOD, (void *)connAuthType);
1531                 if ( rc != LDAP_SUCCESS )
1532                         return rc;
1533         }
1534
1535         if ( conn->c_authz.sai_dn.bv_val != NULL ) {
1536                 char *connDn = slapi_ch_strdup(conn->c_authz.sai_dn.bv_val);
1537                 rc = slapi_pblock_set(pb, SLAPI_CONN_DN, (void *)connDn);
1538                 if ( rc != LDAP_SUCCESS )
1539                         return rc;
1540         }
1541
1542         return rc;
1543 #else
1544         return -1;
1545 #endif /* defined(LDAP_SLAPI) */
1546 }
1547
1548 /*
1549  * Internal API to prime a Slapi_PBlock with an Operation.
1550  */
1551 int slapi_x_operation_set_pb( Slapi_PBlock *pb, Operation *op )
1552 {
1553 #if defined(LDAP_SLAPI)
1554         int isRoot = 0;
1555         int isUpdateDn = 0;
1556         int rc;
1557         Backend *be;
1558         char *opAuthType;
1559
1560         if ( slapi_pblock_get(pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
1561                 be = NULL;
1562         }
1563         if (be != NULL) {
1564                 isRoot = be_isroot( be, &op->o_ndn );
1565                 isUpdateDn = be_isupdate( be, &op->o_ndn );
1566         }
1567                 
1568         rc = slapi_pblock_set( pb, SLAPI_OPERATION, (void *)op );
1569         if ( rc != LDAP_SUCCESS )
1570                 return rc;
1571
1572         rc = slapi_pblock_set( pb, SLAPI_OPINITIATED_TIME, (void *)op->o_time );
1573         if ( rc != LDAP_SUCCESS )
1574                 return rc;
1575
1576         rc = slapi_pblock_set( pb, SLAPI_OPERATION_ID, (void *)op->o_opid );
1577         if ( rc != LDAP_SUCCESS )
1578                 return rc;
1579
1580         rc = slapi_pblock_set( pb, SLAPI_OPERATION_TYPE, (void *)op->o_tag );
1581         if ( rc != LDAP_SUCCESS )
1582                 return rc;
1583
1584         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, (void *)isRoot );
1585         if ( rc != LDAP_SUCCESS )
1586                 return rc;
1587
1588         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_ISUPDATEDN, (void *)isUpdateDn );
1589         if ( rc != LDAP_SUCCESS )
1590                 return rc;
1591
1592         rc = slapi_pblock_set( pb, SLAPI_REQCONTROLS, (void *)op->o_ctrls );
1593         if ( rc != LDAP_SUCCESS)
1594                 return rc;
1595
1596         rc = slapi_pblock_set( pb, SLAPI_REQUESTOR_DN, (void *)op->o_ndn.bv_val );
1597         if ( rc != LDAP_SUCCESS )
1598                 return rc;
1599
1600         rc = slapi_pblock_get( pb, SLAPI_CONN_AUTHMETHOD, (void *)&opAuthType );
1601         if ( rc == LDAP_SUCCESS && opAuthType != NULL ) {
1602                 /* Not quite sure what the point of this is. */
1603                 rc = slapi_pblock_set( pb, SLAPI_OPERATION_AUTHTYPE, (void *)opAuthType );
1604                 if ( rc != LDAP_SUCCESS )
1605                         return rc;
1606         }
1607
1608         return LDAP_SUCCESS;
1609 #else
1610         return -1;
1611 #endif
1612 }
1613
1614 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
1615 {
1616 #if defined( LDAP_SLAPI )
1617         Connection *conn;
1618
1619         slapi_pblock_get( pb, SLAPI_CONNECTION, &conn );
1620         *isSSL = conn->c_is_tls;
1621
1622         return LDAP_SUCCESS;
1623 #else
1624         return -1;
1625 #endif /* defined(LDAP_SLAPI) */
1626 }
1627
1628 /*
1629  * DS 5.x compatability API follow
1630  */
1631
1632 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
1633 {
1634 #if defined( LDAP_SLAPI )
1635         AttributeType *at;
1636
1637         if ( attr == NULL )
1638                 return LDAP_PARAM_ERROR;
1639
1640         at = attr->a_desc->ad_type;
1641
1642         *flags = SLAPI_ATTR_FLAG_STD_ATTR;
1643
1644         if ( is_at_single_value( at ) )
1645                 *flags |= SLAPI_ATTR_FLAG_SINGLE;
1646         if ( is_at_operational( at ) )
1647                 *flags |= SLAPI_ATTR_FLAG_OPATTR;
1648         if ( is_at_obsolete( at ) )
1649                 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
1650         if ( is_at_collective( at ) )
1651                 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
1652         if ( is_at_no_user_mod( at ) )
1653                 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
1654
1655         return LDAP_SUCCESS;
1656 #else
1657         return -1;
1658 #endif /* defined(LDAP_SLAPI) */
1659 }
1660
1661 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
1662 {
1663 #if defined( LDAP_SLAPI )
1664         unsigned long flags;
1665
1666         if ( slapi_attr_get_flags( attr, &flags ) != 0 )
1667                 return 0;
1668         return (flags & flag) ? 1 : 0;
1669 #else
1670         return 0;
1671 #endif /* defined(LDAP_SLAPI) */
1672 }
1673
1674 Slapi_Attr *slapi_attr_new( void )
1675 {
1676 #ifdef LDAP_SLAPI
1677         Attribute *ad;
1678
1679         ad = (Attribute  *)slapi_ch_calloc( 1, sizeof(*ad) );
1680
1681         return ad;
1682 #else
1683         return NULL;
1684 #endif
1685 }
1686
1687 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
1688 {
1689 #ifdef LDAP_SLAPI
1690         const char *text;
1691         AttributeDescription *ad = NULL;
1692
1693         if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
1694                 return NULL;
1695         }
1696
1697         a->a_desc = ad;
1698         a->a_vals = NULL;
1699         a->a_next = NULL;
1700         a->a_flags = 0;
1701
1702         return a;
1703 #else
1704         return NULL;
1705 #endif
1706 }
1707
1708 void slapi_attr_free( Slapi_Attr **a )
1709 {
1710 #ifdef LDAP_SLAPI
1711         attr_free( *a );
1712         *a = NULL;
1713 #endif
1714 }
1715
1716 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
1717 {
1718 #ifdef LDAP_SLAPI
1719         return attr_dup( (Slapi_Attr *)attr );
1720 #else
1721         return NULL;
1722 #endif
1723 }
1724
1725 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
1726 {
1727 #ifdef LDAP_SLAPI
1728         return value_add_one( &a->a_vals, (Slapi_Value *)v );
1729 #else
1730         return -1;
1731 #endif
1732 }
1733
1734 int slapi_attr_type2plugin( const char *type, void **pi )
1735 {
1736         *pi = NULL;
1737
1738         return LDAP_OTHER;
1739 }
1740
1741 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
1742 {
1743 #ifdef LDAP_SLAPI
1744         if ( attr == NULL ) {
1745                 return LDAP_PARAM_ERROR;
1746         }
1747
1748         *type = attr->a_desc->ad_cname.bv_val;
1749
1750         return LDAP_SUCCESS;
1751 #else
1752         return -1;
1753 #endif
1754 }
1755
1756 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
1757 {
1758 #ifdef LDAP_SLAPI
1759         if ( attr == NULL ) {
1760                 return LDAP_PARAM_ERROR;
1761         }
1762         *oidp = attr->a_desc->ad_type->sat_oid;
1763
1764         return LDAP_SUCCESS;
1765 #else
1766         return -1;
1767 #endif
1768 }
1769
1770 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
1771 {
1772 #ifdef LDAP_SLAPI
1773         MatchingRule *mr;
1774         int ret;
1775         int rc;
1776         const char *text;
1777
1778         mr = a->a_desc->ad_type->sat_equality;
1779         rc = value_match( &ret, a->a_desc, mr, SLAP_MR_ASSERTION_SYNTAX_MATCH,
1780                 (struct berval *)v1, (void *)v2, &text );
1781         if ( rc != LDAP_SUCCESS ) 
1782                 return -1;
1783
1784         return ( ret == 0 ) ? 0 : -1;
1785 #else
1786         return -1;
1787 #endif
1788 }
1789
1790 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
1791 {
1792 #ifdef LDAP_SLAPI
1793         MatchingRule *mr;
1794         struct berval *bv;
1795         int j;
1796         const char *text;
1797         int rc;
1798         int ret;
1799
1800         mr = a->a_desc->ad_type->sat_equality;
1801         for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
1802                 rc = value_match( &ret, a->a_desc, mr,
1803                         SLAP_MR_ASSERTION_SYNTAX_MATCH, bv, v, &text );
1804                 if ( rc != LDAP_SUCCESS ) {
1805                         return -1;
1806                 }
1807                 if ( ret == 0 ) {
1808                         return 0;
1809                 }
1810         }
1811 #endif
1812         return -1;
1813 }
1814
1815 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
1816 {
1817 #ifdef LDAP_SLAPI
1818         AttributeDescription *a1 = NULL;
1819         AttributeDescription *a2 = NULL;
1820         const char *text;
1821         int ret;
1822
1823         if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
1824                 return -1;
1825         }
1826
1827         if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
1828                 return 1;
1829         }
1830
1831 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
1832         ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
1833                 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
1834
1835         switch ( opt ) {
1836         case SLAPI_TYPE_CMP_EXACT:
1837                 ret = ad_cmp( a1, a2 );
1838                 break;
1839         case SLAPI_TYPE_CMP_BASE:
1840                 ret = ad_base_cmp( a1, a2 );
1841                 break;
1842         case SLAPI_TYPE_CMP_SUBTYPE:
1843                 ret = is_ad_subtype( a2, a2 );
1844                 break;
1845         default:
1846                 ret = -1;
1847                 break;
1848         }
1849
1850         return ret;
1851 #else
1852         return -1;
1853 #endif
1854 }
1855
1856 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
1857 {
1858 #ifdef LDAP_SLAPI
1859         return slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT );
1860 #else
1861         return -1;
1862 #endif
1863 }
1864
1865 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
1866 {
1867 #ifdef LDAP_SLAPI
1868         return slapi_valueset_first_value( &a->a_vals, v );
1869 #else
1870         return -1;
1871 #endif
1872 }
1873
1874 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
1875 {
1876 #ifdef LDAP_SLAPI
1877         return slapi_valueset_next_value( &a->a_vals, hint, v );
1878 #else
1879         return -1;
1880 #endif
1881 }
1882
1883 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
1884 {
1885 #ifdef LDAP_SLAPI
1886         *numValues = slapi_valueset_count( &a->a_vals );
1887
1888         return 0;
1889 #else
1890         return -1;
1891 #endif
1892 }
1893
1894 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
1895 {
1896 #ifdef LDAP_SLAPI
1897         *vs = &((Slapi_Attr *)a)->a_vals;
1898
1899         return 0;
1900 #else
1901         return -1;
1902 #endif
1903 }
1904
1905 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
1906 {
1907 #ifdef LDAP_SLAPI
1908         return slapi_attr_get_values( a, vals );
1909 #else
1910         return -1;
1911 #endif
1912 }
1913
1914 char *slapi_attr_syntax_normalize( const char *s )
1915 {
1916 #ifdef LDAP_SLAPI
1917         AttributeDescription *ad = NULL;
1918         const char *text;
1919
1920         if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
1921                 return NULL;
1922         }
1923
1924         return ad->ad_cname.bv_val;
1925 #else
1926         return -1;
1927 #endif
1928 }
1929
1930 Slapi_Value *slapi_value_new( void )
1931 {
1932 #ifdef LDAP_SLAPI
1933         struct berval *bv;
1934
1935         bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
1936
1937         return bv;
1938 #else
1939         return NULL;
1940 #endif
1941 }
1942
1943 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
1944 {
1945 #ifdef LDAP_SLAPI
1946         return ber_dupbv( NULL, (struct berval *)bval );
1947 #else
1948         return NULL;
1949 #endif
1950 }
1951
1952 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
1953 {
1954 #ifdef LDAP_SLAPI
1955         return slapi_value_new_berval( v );
1956 #else
1957         return NULL;
1958 #endif
1959 }
1960
1961 Slapi_Value *slapi_value_new_string(const char *s)
1962 {
1963 #ifdef LDAP_SLAPI
1964         struct berval bv;
1965
1966         bv.bv_val = (char *)s;
1967         bv.bv_len = strlen( s );
1968
1969         return slapi_value_new_berval( &bv );
1970 #else
1971         return NULL;
1972 #endif
1973 }
1974
1975 Slapi_Value *slapi_value_init(Slapi_Value *val)
1976 {
1977 #ifdef LDAP_SLAPI
1978         val->bv_val = NULL;
1979         val->bv_len = 0;
1980
1981         return val;
1982 #else
1983         return NULL;
1984 #endif
1985 }
1986
1987 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
1988 {
1989 #ifdef LDAP_SLAPI
1990         return ber_dupbv( v, bval );
1991 #else
1992         return NULL;
1993 #endif
1994 }
1995
1996 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
1997 {
1998 #ifdef LDAP_SLAPI
1999         v->bv_val = slapi_ch_strdup( (char *)s );
2000         v->bv_len = strlen( s );
2001
2002         return v;
2003 #else
2004         return NULL;
2005 #endif
2006 }
2007
2008 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2009 {
2010 #ifdef LDAP_SLAPI
2011         return slapi_value_new_value( v );
2012 #else
2013         return NULL;
2014 #endif
2015 }
2016
2017 void slapi_value_free(Slapi_Value **value)
2018 {
2019 #ifdef LDAP_SLAPI       
2020         if ( value == NULL ) {
2021                 return;
2022         }
2023
2024         if ( (*value) != NULL ) {
2025                 slapi_ch_free( (void **)&(*value)->bv_val );
2026                 slapi_ch_free( (void **)value );
2027         }
2028 #endif
2029 }
2030
2031 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2032 {
2033 #ifdef LDAP_SLAPI
2034         return value;
2035 #else
2036         return NULL;
2037 #endif
2038 }
2039
2040 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2041 {
2042 #ifdef LDAP_SLAPI
2043         if ( value == NULL ) {
2044                 return NULL;
2045         }
2046         if ( value->bv_val != NULL ) {
2047                 slapi_ch_free( (void **)&value->bv_val );
2048         }
2049         slapi_value_init_berval( value, (struct berval *)bval );
2050
2051         return value;
2052 #else
2053         return NULL;
2054 #endif
2055 }
2056
2057 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2058 {
2059 #ifdef LDAP_SLAPI
2060         if ( value == NULL ) {
2061                 return NULL;
2062         }
2063         return slapi_value_set_berval( value, vfrom );
2064 #else
2065         return NULL;
2066 #endif
2067 }
2068
2069 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2070 {
2071 #ifdef LDAP_SLAPI
2072         if ( value == NULL ) {
2073                 return NULL;
2074         }
2075         if ( value->bv_val != NULL ) {
2076                 slapi_ch_free( (void **)&value->bv_val );
2077         }
2078         value->bv_val = slapi_ch_malloc( len );
2079         value->bv_len = len;
2080         AC_MEMCPY( value->bv_val, val, len );
2081
2082         return value;
2083 #else
2084         return NULL;
2085 #endif
2086 }
2087
2088 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2089 {
2090 #ifdef LDAP_SLAPI
2091         if ( value == NULL ) {
2092                 return -1;
2093         }
2094         slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2095         return 0;
2096 #else
2097         return NULL;
2098 #endif
2099 }
2100
2101 int slapi_value_set_int(Slapi_Value *value, int intVal)
2102 {
2103 #ifdef LDAP_SLAPI
2104         char buf[64];
2105
2106         snprintf( buf, sizeof( buf ), "%d", intVal );
2107
2108         return slapi_value_set_string( value, buf );
2109 #else
2110         return -1;
2111 #endif
2112 }
2113
2114 const char *slapi_value_get_string(const Slapi_Value *value)
2115 {
2116 #ifdef LDAP_SLAPI
2117         if ( value == NULL ) {
2118                 return NULL;
2119         }
2120         return value->bv_val;
2121 #else
2122         return NULL;
2123 #endif
2124 }
2125
2126 #ifdef LDAP_SLAPI
2127 static int checkBVString(const struct berval *bv)
2128 {
2129         int i;
2130
2131         for ( i = 0; i < bv->bv_len; i++ ) {
2132                 if ( bv->bv_val[i] == '\0' )
2133                         return 0;
2134         }
2135         if ( bv->bv_val[i] != '\0' )
2136                 return 0;
2137
2138         return 1;
2139 }
2140 #endif
2141
2142 int slapi_value_get_int(const Slapi_Value *value)
2143 {
2144 #ifdef LDAP_SLAPI
2145         if ( value == NULL ) return 0;
2146         if ( value->bv_val == NULL ) return 0;
2147         if ( !checkBVString( value ) ) return 0;
2148
2149         return (int)strtol( value->bv_val, NULL, 10 );
2150 #else
2151         return NULL;
2152 #endif
2153 }
2154
2155 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2156 {
2157 #ifdef LDAP_SLAPI
2158         if ( value == NULL ) return 0;
2159         if ( value->bv_val == NULL ) return 0;
2160         if ( !checkBVString( value ) ) return 0;
2161
2162         return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2163 #else
2164         return NULL;
2165 #endif
2166 }
2167
2168 long slapi_value_get_long(const Slapi_Value *value)
2169 {
2170 #ifdef LDAP_SLAPI
2171         if ( value == NULL ) return 0;
2172         if ( value->bv_val == NULL ) return 0;
2173         if ( !checkBVString( value ) ) return 0;
2174
2175         return strtol( value->bv_val, NULL, 10 );
2176 #else
2177         return NULL;
2178 #endif
2179 }
2180
2181 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2182 {
2183 #ifdef LDAP_SLAPI
2184         if ( value == NULL ) return 0;
2185         if ( value->bv_val == NULL ) return 0;
2186         if ( !checkBVString( value ) ) return 0;
2187
2188         return strtoul( value->bv_val, NULL, 10 );
2189 #else
2190         return NULL;
2191 #endif
2192 }
2193
2194 size_t slapi_value_get_length(const Slapi_Value *value)
2195 {
2196 #ifdef LDAP_SLAPI
2197         if ( value == NULL )
2198                 return 0;
2199
2200         return (size_t) value->bv_len;
2201 #else
2202         return 0;
2203 #endif
2204 }
2205
2206 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2207 {
2208 #ifdef LDAP_SLAPI
2209         return slapi_attr_value_cmp( a, v1, v2 );
2210 #else
2211         return -1;
2212 #endif
2213 }
2214
2215 /* A ValueSet is a container for a BerVarray. */
2216 Slapi_ValueSet *slapi_valueset_new( void )
2217 {
2218 #ifdef LDAP_SLAPI
2219         Slapi_ValueSet *vs;
2220
2221         vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2222         *vs = NULL;
2223
2224         return vs;
2225 #else
2226         return NULL;
2227 #endif
2228 }
2229
2230 void slapi_valueset_free(Slapi_ValueSet *vs)
2231 {
2232 #ifdef LDAP_SLAPI
2233         if ( vs != NULL ) {
2234                 BerVarray vp = *vs;
2235
2236                 ber_bvarray_free( vp );
2237                 slapi_ch_free( (void **)&vp );
2238
2239                 *vs = NULL;
2240         }
2241 #endif
2242 }
2243
2244 void slapi_valueset_init(Slapi_ValueSet *vs)
2245 {
2246 #ifdef LDAP_SLAPI
2247         if ( vs != NULL && *vs == NULL ) {
2248                 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2249                 (*vs)->bv_val = NULL;
2250                 (*vs)->bv_len = 0;
2251         }
2252 #endif
2253 }
2254
2255 void slapi_valueset_done(Slapi_ValueSet *vs)
2256 {
2257 #ifdef LDAP_SLAPI
2258         BerVarray vp;
2259
2260         if ( vs == NULL )
2261                 return;
2262
2263         for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2264                 vp->bv_len = 0;
2265                 slapi_ch_free( (void **)&vp->bv_val );
2266         }
2267         /* but don't free *vs or vs */
2268 #endif
2269 }
2270
2271 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2272 {
2273 #ifdef LDAP_SLAPI
2274         struct berval bv;
2275
2276         ber_dupbv( &bv, (Slapi_Value *)addval );
2277         ber_bvarray_add( vs, &bv );
2278 #endif
2279 }
2280
2281 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2282 {
2283 #ifdef LDAP_SLAPI
2284         return slapi_valueset_next_value( vs, 0, v );
2285 #else
2286         return -1;
2287 #endif
2288 }
2289
2290 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2291 {
2292 #ifdef LDAP_SLAPI
2293         int i;
2294         BerVarray vp;
2295
2296         if ( vs == NULL )
2297                 return -1;
2298
2299         vp = *vs;
2300
2301         for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2302                 if ( i == index ) {
2303                         *v = &vp[i];
2304                         return index + 1;
2305                 }
2306         }
2307 #endif
2308
2309         return -1;
2310 }
2311
2312 int slapi_valueset_count( const Slapi_ValueSet *vs )
2313 {
2314 #ifdef LDAP_SLAPI
2315         int i;
2316         BerVarray vp;
2317
2318         if ( vs == NULL )
2319                 return 0;
2320
2321         vp = *vs;
2322
2323         for ( i = 0; vp[i].bv_val != NULL; i++ )
2324                 ;
2325
2326         return i;
2327 #else
2328         return 0;
2329 #endif
2330
2331 }
2332
2333 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2334 {
2335 #ifdef LDAP_SLAPI
2336         BerVarray vp;
2337
2338         for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2339                 slapi_valueset_add_value( vs1, vp );
2340         }
2341 #endif
2342 }
2343
2344 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2345         struct berval *val, int access )
2346 {
2347 #ifdef LDAPI_SLAPI
2348         Backend *be;
2349         Connection *conn;
2350         Operation *op;
2351         int ret;
2352         slap_access_t slap_access;
2353         AttributeDescription *ad = NULL;
2354         char *text;
2355
2356         ret = slap_str2ad( attr, &ad, &text );
2357         if ( ret != LDAP_SUCCESS ) {
2358                 return ret;
2359         }
2360
2361         switch ( access & SLAPI_ACL_ALL ) {
2362         case SLAPI_ACL_COMPARE:
2363                 slap_access = ACL_COMPARE;
2364                 break;
2365         case SLAPI_ACL_SEARCH:
2366                 slap_access = ACL_SEARCH;
2367                 break;
2368         case SLAPI_ACL_READ:
2369                 slap_access = ACL_READ;
2370                 break;
2371         case SLAPI_ACL_WRITE:
2372         case SLAPI_ACL_DELETE:
2373         case SLAPI_ACL_ADD:
2374         case SLAPI_ACL_SELF:
2375                 slap_access = ACL_WRITE;
2376                 break;
2377         default:
2378                 return LDAP_INSUFFICIENT_ACCESS;
2379                 break;
2380         }
2381
2382         if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2383                 return LDAP_PARAM_ERROR;
2384         }
2385
2386         if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2387                 return LDAP_PARAM_ERROR;
2388         }
2389
2390         if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2391                 return LDAP_PARAM_ERROR;
2392         }
2393
2394         ret = access_allowed( be, conn, op, e, desc, val, slap_access, NULL );
2395
2396         return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2397 #else
2398         return LDAP_UNWILLING_TO_PERFORM;
2399 #endif
2400 }
2401
2402 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2403 {
2404 #ifdef LDAP_SLAPI
2405         Backend *be;
2406         Connection *conn;
2407         Operation *op;
2408         int ret;
2409         Modifications *ml;
2410         Modifications *next;
2411
2412         if ( slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be ) != 0 ) {
2413                 return LDAP_PARAM_ERROR;
2414         }
2415
2416         if ( slapi_pblock_get( pb, SLAPI_CONNECTION, (void *)&conn ) != 0 ) {
2417                 return LDAP_PARAM_ERROR;
2418         }
2419
2420         if ( slapi_pblock_get( pb, SLAPI_OPERATION, (void *)&op ) != 0 ) {
2421                 return LDAP_PARAM_ERROR;
2422         }
2423
2424         ml = slapi_x_ldapmods2modifications( mods );
2425         if ( ml == NULL ) {
2426                 return LDAP_OTHER;
2427         }
2428
2429         ret = acl_check_modlist( be, conn, op, e, ml );
2430
2431         /* Careful when freeing the modlist because it has pointers into the mods array. */
2432         for ( ; ml != NULL; ml = next ) {
2433                 next = ml->sml_next;
2434
2435                 /* just free the containing array */
2436                 slapi_ch_free( (void **)&ml->sml_bvalues );
2437                 slapi_ch_free( (void **)&ml );
2438         }
2439
2440         return ret ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2441 #else
2442         return LDAP_UNWILLING_TO_PERFORM;
2443 #endif
2444 }
2445
2446 /*
2447  * Synthesise an LDAPMod array from a Modifications list to pass
2448  * to SLAPI. This synthesis is destructive and as such the 
2449  * Modifications list may not be used after calling this 
2450  * function.
2451  * 
2452  * This function must also be called before slap_mods_check().
2453  */
2454 LDAPMod **slapi_x_modifications2ldapmods(Modifications **pmodlist)
2455 {
2456 #ifdef LDAP_SLAPI
2457         Modifications *ml, *modlist;
2458         LDAPMod **mods, *modp;
2459         int i, j;
2460
2461         modlist = *pmodlist;
2462
2463         for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2464                 ;
2465
2466         mods = (LDAPMod **)ch_malloc( (i + 1) * sizeof(LDAPMod *) );
2467
2468         for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2469                 modp = mods[i];
2470                 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2471
2472                 /* Take ownership of original type. */
2473                 modp->mod_type = ml->sml_type.bv_val;
2474                 ml->sml_type.bv_val = NULL;
2475
2476                 if ( ml->sml_bvalues != NULL ) {
2477                         for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ )
2478                                 ;
2479                         modp->mod_bvalues = (struct berval **)ch_malloc( (j + 1) *
2480                                 sizeof(struct berval *) );
2481                         for( j = 0; ml->sml_bvalues[j].bv_val != NULL; j++ ) {
2482                                 /* Take ownership of original values. */
2483                                 modp->mod_bvalues[j] = (struct berval *)ch_malloc( sizeof(struct berval) );
2484                                 modp->mod_bvalues[j]->bv_len = ml->sml_bvalues[j].bv_len;
2485                                 modp->mod_bvalues[j]->bv_val = ml->sml_bvalues[j].bv_val;
2486                                 ml->sml_bvalues[j].bv_len = 0;
2487                                 ml->sml_bvalues[j].bv_val = NULL;
2488                         }
2489                         modp->mod_bvalues[j] = NULL;
2490                 } else {
2491                         modp->mod_bvalues = NULL;
2492                 }
2493                 i++;
2494         }
2495
2496         mods[i] = NULL;
2497
2498         slap_mods_free( modlist );
2499         *pmodlist = NULL;
2500
2501         return mods;
2502 #else
2503         return NULL;
2504 #endif
2505 }
2506
2507 /*
2508  * Convert a potentially modified array of LDAPMods back to a
2509  * Modification list. 
2510  * 
2511  * The returned Modification list contains pointers into the
2512  * LDAPMods array; the latter MUST be freed with
2513  * slapi_x_free_ldapmods() (see below).
2514  */
2515 Modifications *slapi_x_ldapmods2modifications (LDAPMod **mods)
2516 {
2517 #ifdef LDAP_SLAPI
2518         Modifications *modlist, **modtail;
2519         LDAPMod **modp;
2520
2521         modtail = &modlist;
2522
2523         for( modp = mods; *modp != NULL; modp++ ) {
2524                 Modifications *mod;
2525                 int i;
2526                 char **p;
2527                 struct berval **bvp;
2528
2529                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
2530                 mod->sml_op = (*modp)->mod_op & (~LDAP_MOD_BVALUES);
2531                 mod->sml_type.bv_val = (*modp)->mod_type;
2532                 mod->sml_type.bv_len = strlen( mod->sml_type.bv_val );
2533                 mod->sml_desc = NULL;
2534                 mod->sml_next = NULL;
2535
2536                 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2537                         for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ )
2538                                 ;
2539                 } else {
2540                         for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ )
2541                                 ;
2542                 }
2543
2544                 mod->sml_bvalues = (BerVarray) ch_malloc( (i + 1) * sizeof(struct berval) );
2545
2546                 /* NB: This implicitly trusts a plugin to return valid modifications. */
2547                 if ( (*modp)->mod_op & LDAP_MOD_BVALUES ) {
2548                         for( i = 0, bvp = (*modp)->mod_bvalues; *bvp != NULL; bvp++, i++ ) {
2549                                 mod->sml_bvalues[i].bv_val = (*bvp)->bv_val;
2550                                 mod->sml_bvalues[i].bv_len = (*bvp)->bv_len;
2551                         }
2552                 } else {
2553                         for( i = 0, p = (*modp)->mod_values; *p != NULL; p++, i++ ) {
2554                                 mod->sml_bvalues[i].bv_val = *p;
2555                                 mod->sml_bvalues[i].bv_len = strlen( *p );
2556                         }
2557                 }
2558                 mod->sml_bvalues[i].bv_val = NULL;
2559
2560                 *modtail = mod;
2561                 modtail = &mod->sml_next;
2562         }
2563         
2564         return modlist;
2565 #else
2566         return NULL;
2567 #endif 
2568 }
2569
2570 /*
2571  * This function only frees the parts of the mods array that
2572  * are not shared with the Modification list that was created
2573  * by slapi_x_ldapmods2modifications(). 
2574  *
2575  */
2576 void slapi_x_free_ldapmods (LDAPMod **mods)
2577 {
2578 #ifdef LDAP_SLAPI
2579         int i, j;
2580
2581         if (mods == NULL)
2582                 return;
2583
2584         for ( i = 0; mods[i] != NULL; i++ ) {
2585                 /*
2586                  * Don't free values themselves; they're owned by the
2587                  * Modification list. Do free the containing array.
2588                  */
2589                 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
2590                         for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ ) {
2591                                 ch_free( mods[i]->mod_bvalues[j] );
2592                         }
2593                         ch_free( mods[i]->mod_bvalues );
2594                 } else {
2595                         ch_free( mods[i]->mod_values );
2596                 }
2597                 /* Don't free type, for same reasons. */
2598                 ch_free( mods[i] );
2599         }
2600         ch_free( mods );
2601 #endif /* LDAP_SLAPI */
2602 }
2603
2604 /*
2605  * Sun ONE DS 5.x computed attribute support. Computed attributes
2606  * allow for dynamically generated operational attributes, a very
2607  * useful thing indeed.
2608  */
2609
2610 /*
2611  * Write the computed attribute to a BerElement. Complementary 
2612  * functions need to be defined for anything that replaces 
2613  * op->o_callback->sc_sendentry, if you wish to make computed
2614  * attributes available to it.
2615  */
2616 int slapi_x_compute_output_ber(computed_attr_context *c, Slapi_Attr *a, Slapi_Entry *e)
2617 {
2618 #ifdef LDAP_SLAPI
2619         Backend *be = NULL;
2620         Connection *conn = NULL;
2621         Operation *op = NULL;
2622         BerElement *ber;
2623         AttributeDescription *desc;
2624         int rc;
2625         int i;
2626
2627         if ( c == NULL ) {
2628                 return 1;
2629         }
2630
2631         if ( a == NULL ) {
2632                 return 1;
2633         }
2634
2635         if ( e == NULL ) {
2636                 return 1;
2637         }
2638
2639         rc = slapi_pblock_get( c->cac_pb, SLAPI_BACKEND, (void *)&be );
2640         if ( rc != 0 ) {
2641                 be = NULL; /* no backend for root DSE */
2642         }
2643
2644         rc = slapi_pblock_get( c->cac_pb, SLAPI_CONNECTION, (void *)&conn );
2645         if ( rc != 0 || conn == NULL ) {
2646                 return rc;
2647         }
2648
2649         rc = slapi_pblock_get( c->cac_pb, SLAPI_OPERATION, (void *)&op );
2650         if ( rc != 0 || op == NULL ) {
2651                 return rc;
2652         }
2653
2654         ber = (BerElement *)c->cac_private;
2655         desc = a->a_desc;
2656
2657         if ( c->cac_attrs == NULL ) {
2658                 /* All attrs request, skip operational attributes */
2659                 if ( is_at_operational( desc->ad_type ) ) {
2660                         return 0;
2661                 }
2662         } else {
2663                 /* Specific attrs requested */
2664                 if ( is_at_operational( desc->ad_type ) ) {
2665                         if ( !c->cac_opattrs && !ad_inlist( desc, c->cac_attrs ) ) {
2666                                 return 0;
2667                         }
2668                 } else {
2669                         if ( !c->cac_userattrs && !ad_inlist( desc, c->cac_attrs ) ) {
2670                                 return 0;
2671                         }
2672                 }
2673         }
2674
2675         if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_READ, &c->cac_acl_state) ) {
2676                 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
2677                         "acl: access to attribute %s not allowed\n",
2678                         desc->ad_cname.bv_val );
2679                 return 0;
2680         }
2681
2682         rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
2683         if (rc == -1 ) {
2684                 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2685                         "ber_printf failed\n");
2686                 return 1;
2687         }
2688
2689         if ( !c->cac_attrsonly ) {
2690                 for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
2691                         if ( !access_allowed( be, conn, op, e,
2692                                 desc, &a->a_vals[i], ACL_READ, &c->cac_acl_state)) {
2693                                 slapi_log_error( SLAPI_LOG_ACL, "SLAPI_COMPUTE",
2694                                         "slapi_x_compute_output_ber: conn %lu "
2695                                         "acl: access to %s, value %d not allowed\n",
2696                                         op->o_connid, desc->ad_cname.bv_val, i  );
2697                                 continue;
2698                         }
2699         
2700                         if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
2701                                 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2702                                         "ber_printf failed\n");
2703                                 return 1;
2704                         }
2705                 }
2706         }
2707
2708         if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
2709                 slapi_log_error( SLAPI_LOG_BER, "SLAPI_COMPUTE",
2710                         "ber_printf failed\n" );
2711                 return 1;
2712         }
2713
2714         return 0;
2715 #else
2716         return 1;
2717 #endif
2718 }
2719
2720 /*
2721  * For some reason Sun don't use the normal plugin mechanism
2722  * registration path to register an "evaluator" function (an
2723  * "evaluator" is responsible for adding computed attributes;
2724  * the nomenclature is somewhat confusing).
2725  *
2726  * As such slapi_compute_add_evaluator() registers the 
2727  * function directly.
2728  */
2729 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2730 {
2731 #ifdef LDAP_SLAPI
2732         Slapi_PBlock *pPlugin = NULL;
2733         int rc;
2734
2735         pPlugin = slapi_pblock_new();
2736         if ( pPlugin == NULL ) {
2737                 rc = LDAP_NO_MEMORY;
2738                 goto done;
2739         }
2740
2741         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2742         if ( rc != LDAP_SUCCESS ) {
2743                 goto done;
2744         }
2745
2746         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2747         if ( rc != LDAP_SUCCESS ) {
2748                 goto done;
2749         }
2750
2751         rc = insertPlugin( NULL, pPlugin );
2752         if ( rc != 0 ) {
2753                 rc = LDAP_OTHER;
2754                 goto done;
2755         }
2756
2757 done:
2758         if ( rc != LDAP_SUCCESS ) {
2759                 if ( pPlugin != NULL ) {
2760                         slapi_pblock_destroy( pPlugin );
2761                 }
2762                 return -1;
2763         }
2764
2765         return 0;
2766 #else
2767         return -1;
2768 #endif /* LDAP_SLAPI */
2769 }
2770
2771 /*
2772  * See notes above regarding slapi_compute_add_evaluator().
2773  */
2774 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2775 {
2776 #ifdef LDAP_SLAPI
2777         Slapi_PBlock *pPlugin = NULL;
2778         int rc;
2779
2780         pPlugin = slapi_pblock_new();
2781         if ( pPlugin == NULL ) {
2782                 rc = LDAP_NO_MEMORY;
2783                 goto done;
2784         }
2785
2786         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)SLAPI_PLUGIN_OBJECT );
2787         if ( rc != LDAP_SUCCESS ) {
2788                 goto done;
2789         }
2790
2791         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2792         if ( rc != LDAP_SUCCESS ) {
2793                 goto done;
2794         }
2795
2796         rc = insertPlugin( NULL, pPlugin );
2797         if ( rc != 0 ) {
2798                 rc = LDAP_OTHER;
2799                 goto done;
2800         }
2801
2802 done:
2803         if ( rc != LDAP_SUCCESS ) {
2804                 if ( pPlugin != NULL ) {
2805                         slapi_pblock_destroy( pPlugin );
2806                 }
2807                 return -1;
2808         }
2809
2810         return 0;
2811 #else
2812         return -1;
2813 #endif /* LDAP_SLAPI */
2814 }
2815
2816 /*
2817  * Call compute evaluators
2818  */
2819 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2820 {
2821 #ifdef LDAP_SLAPI
2822         int rc = 0;
2823         slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2824
2825         rc = getAllPluginFuncs( NULL, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2826         if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2827                 /* Nothing to do; front-end should ignore. */
2828                 return 0;
2829         }
2830
2831         for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2832                 /*
2833                  * -1: no attribute matched requested type
2834                  *  0: one attribute matched
2835                  * >0: error happened
2836                  */
2837                 rc = (*pGetPlugin)( c, type, e, outputfn );
2838                 if ( rc > 0 ) {
2839                         break;
2840                 }
2841         }
2842
2843         slapi_ch_free( (void **)&tmpPlugin );
2844
2845         return rc;
2846 #else
2847         return 1;
2848 #endif /* LDAP_SLAPI */
2849 }
2850
2851 int compute_rewrite_search_filter(Slapi_PBlock *pb)
2852 {
2853 #ifdef LDAP_SLAPI
2854         Backend *be;
2855         int rc;
2856
2857         rc = slapi_pblock_get( pb, SLAPI_BACKEND, (void *)&be );
2858         if ( rc != 0 ) {
2859                 return rc;
2860         }
2861
2862         return doPluginFNs( be, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
2863 #else
2864         return -1;
2865 #endif /* LDAP_SLAPI */
2866 }
2867
2868 /*
2869  * New API to provide the plugin with access to the search
2870  * pblock. Have informed Sun DS team.
2871  */
2872 int slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
2873 {
2874 #ifdef LDAP_SLAPI
2875         if ( c == NULL )
2876                 return -1;
2877
2878         if ( c->cac_pb == NULL )
2879                 return -1;
2880
2881         *pb = c->cac_pb;
2882
2883         return 0;
2884 #else
2885         return -1;
2886 #endif /* LDAP_SLAPI */
2887 }
2888