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