]> git.sur5r.net Git - openldap/blob - servers/slapd/slapi/slapi_utils.c
Call slap_mods2entry() in do_add(), so that global overlays (including
[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 /*
940  * Returns DN of the parent entry; or NULL if the DN is
941  * an empty string, if the DN has no parent, or if the
942  * DN is the suffix of the backend database
943  */
944 char *slapi_dn_beparent( Slapi_PBlock *pb, const char *_dn )
945 {
946         Backend         *be;
947         struct berval   dn, prettyDN;
948         struct berval   normalizedDN, parentDN;
949
950         if ( pb == NULL || pb->pb_op == NULL )
951                 return NULL;
952
953         be = pb->pb_op->o_bd;
954
955         dn.bv_val = (char *)_dn;
956         dn.bv_len = strlen( _dn );
957
958         if ( dnPrettyNormal( NULL, &dn, &prettyDN, &normalizedDN, NULL ) != LDAP_SUCCESS ) {
959                 return NULL;
960         }
961
962         if ( be != NULL && be_issuffix( be, &normalizedDN ) ) {
963                 slapi_ch_free( (void **)&prettyDN.bv_val );
964                 slapi_ch_free( (void **)&normalizedDN.bv_val );
965                 return NULL;
966         }
967
968         dnParent( &prettyDN, &parentDN );
969
970         slapi_ch_free( (void **)&prettyDN.bv_val );
971         slapi_ch_free( (void **)&normalizedDN.bv_val );
972
973         if ( parentDN.bv_len == 0 ) {
974                 return NULL;
975         }
976
977         return slapi_ch_strdup( parentDN.bv_val );
978 }
979
980 char *
981 slapi_dn_ignore_case( char *dn )
982 {       
983         return slapi_dn_normalize_case( dn );
984 }
985
986 char *
987 slapi_ch_malloc( unsigned long size ) 
988 {
989         return ch_malloc( size );       
990 }
991
992 void 
993 slapi_ch_free( void **ptr ) 
994 {
995         if ( ptr == NULL || *ptr == NULL )
996                 return;
997         ch_free( *ptr );
998         *ptr = NULL;
999 }
1000
1001 void 
1002 slapi_ch_free_string( char **ptr ) 
1003 {
1004         slapi_ch_free( (void **)ptr );
1005 }
1006
1007 void
1008 slapi_ch_array_free( char **arrayp )
1009 {
1010         char **p;
1011
1012         if ( arrayp != NULL ) {
1013                 for ( p = arrayp; *p != NULL; p++ ) {
1014                         slapi_ch_free( (void **)p );
1015                 }
1016                 slapi_ch_free( (void **)&arrayp );
1017         }
1018 }
1019
1020 struct berval *
1021 slapi_ch_bvdup(const struct berval *v)
1022 {
1023         struct berval *bv;
1024
1025         bv = (struct berval *) slapi_ch_malloc( sizeof(struct berval) );
1026         bv->bv_len = v->bv_len;
1027         bv->bv_val = slapi_ch_malloc( bv->bv_len );
1028         AC_MEMCPY( bv->bv_val, v->bv_val, bv->bv_len );
1029
1030         return bv;
1031 }
1032
1033 struct berval **
1034 slapi_ch_bvecdup(const struct berval **v)
1035 {
1036         int i;
1037         struct berval **rv;
1038
1039         if ( v == NULL ) {
1040                 return NULL;
1041         }
1042
1043         for ( i = 0; v[i] != NULL; i++ )
1044                 ;
1045
1046         rv = (struct berval **) slapi_ch_malloc( (i + 1) * sizeof(struct berval *) );
1047
1048         for ( i = 0; v[i] != NULL; i++ ) {
1049                 rv[i] = slapi_ch_bvdup( v[i] );
1050         }
1051         rv[i] = NULL;
1052
1053         return rv;
1054 }
1055
1056 char *
1057 slapi_ch_calloc(
1058         unsigned long nelem, 
1059         unsigned long size ) 
1060 {
1061         return ch_calloc( nelem, size );
1062 }
1063
1064 char *
1065 slapi_ch_realloc(
1066         char *block, 
1067         unsigned long size ) 
1068 {
1069         return ch_realloc( block, size );
1070 }
1071
1072 char *
1073 slapi_ch_strdup( char *s ) 
1074 {
1075         return ch_strdup( (const char *)s );
1076 }
1077
1078 size_t
1079 slapi_ch_stlen( char *s ) 
1080 {
1081         return strlen( (const char *)s );
1082 }
1083
1084 int 
1085 slapi_control_present(
1086         LDAPControl     **controls, 
1087         char            *oid, 
1088         struct berval   **val, 
1089         int             *iscritical ) 
1090 {
1091         int             i;
1092         int             rc = 0;
1093
1094         if ( val ) {
1095                 *val = NULL;
1096         }
1097         
1098         if ( iscritical ) {
1099                 *iscritical = 0;
1100         }
1101         
1102         for ( i = 0; controls != NULL && controls[i] != NULL; i++ ) {
1103                 if ( strcmp( controls[i]->ldctl_oid, oid ) != 0 ) {
1104                         continue;
1105                 }
1106
1107                 rc = 1;
1108                 if ( controls[i]->ldctl_value.bv_len != 0 ) {
1109                         if ( val ) {
1110                                 *val = &controls[i]->ldctl_value;
1111                         }
1112                 }
1113
1114                 if ( iscritical ) {
1115                         *iscritical = controls[i]->ldctl_iscritical;
1116                 }
1117
1118                 break;
1119         }
1120
1121         return rc;
1122 }
1123
1124 static void
1125 slapControlMask2SlapiControlOp(slap_mask_t slap_mask,
1126         unsigned long *slapi_mask)
1127 {
1128         *slapi_mask = SLAPI_OPERATION_NONE;
1129
1130         if ( slap_mask & SLAP_CTRL_ABANDON ) 
1131                 *slapi_mask |= SLAPI_OPERATION_ABANDON;
1132
1133         if ( slap_mask & SLAP_CTRL_ADD )
1134                 *slapi_mask |= SLAPI_OPERATION_ADD;
1135
1136         if ( slap_mask & SLAP_CTRL_BIND )
1137                 *slapi_mask |= SLAPI_OPERATION_BIND;
1138
1139         if ( slap_mask & SLAP_CTRL_COMPARE )
1140                 *slapi_mask |= SLAPI_OPERATION_COMPARE;
1141
1142         if ( slap_mask & SLAP_CTRL_DELETE )
1143                 *slapi_mask |= SLAPI_OPERATION_DELETE;
1144
1145         if ( slap_mask & SLAP_CTRL_MODIFY )
1146                 *slapi_mask |= SLAPI_OPERATION_MODIFY;
1147
1148         if ( slap_mask & SLAP_CTRL_RENAME )
1149                 *slapi_mask |= SLAPI_OPERATION_MODDN;
1150
1151         if ( slap_mask & SLAP_CTRL_SEARCH )
1152                 *slapi_mask |= SLAPI_OPERATION_SEARCH;
1153
1154         if ( slap_mask & SLAP_CTRL_UNBIND )
1155                 *slapi_mask |= SLAPI_OPERATION_UNBIND;
1156 }
1157
1158 static void
1159 slapiControlOp2SlapControlMask(unsigned long slapi_mask,
1160         slap_mask_t *slap_mask)
1161 {
1162         *slap_mask = 0;
1163
1164         if ( slapi_mask & SLAPI_OPERATION_BIND )
1165                 *slap_mask |= SLAP_CTRL_BIND;
1166
1167         if ( slapi_mask & SLAPI_OPERATION_UNBIND )
1168                 *slap_mask |= SLAP_CTRL_UNBIND;
1169
1170         if ( slapi_mask & SLAPI_OPERATION_SEARCH )
1171                 *slap_mask |= SLAP_CTRL_SEARCH;
1172
1173         if ( slapi_mask & SLAPI_OPERATION_MODIFY )
1174                 *slap_mask |= SLAP_CTRL_MODIFY;
1175
1176         if ( slapi_mask & SLAPI_OPERATION_ADD )
1177                 *slap_mask |= SLAP_CTRL_ADD;
1178
1179         if ( slapi_mask & SLAPI_OPERATION_DELETE )
1180                 *slap_mask |= SLAP_CTRL_DELETE;
1181
1182         if ( slapi_mask & SLAPI_OPERATION_MODDN )
1183                 *slap_mask |= SLAP_CTRL_RENAME;
1184
1185         if ( slapi_mask & SLAPI_OPERATION_COMPARE )
1186                 *slap_mask |= SLAP_CTRL_COMPARE;
1187
1188         if ( slapi_mask & SLAPI_OPERATION_ABANDON )
1189                 *slap_mask |= SLAP_CTRL_ABANDON;
1190
1191         *slap_mask |= SLAP_CTRL_GLOBAL;
1192 }
1193
1194 static int
1195 slapi_int_parse_control(
1196         Operation *op,
1197         SlapReply *rs,
1198         LDAPControl *ctrl )
1199 {
1200         /* Plugins must deal with controls themselves. */
1201
1202         return LDAP_SUCCESS;
1203 }
1204
1205 void 
1206 slapi_register_supported_control(
1207         char            *controloid, 
1208         unsigned long   controlops )
1209 {
1210         slap_mask_t controlmask;
1211
1212         slapiControlOp2SlapControlMask( controlops, &controlmask );
1213
1214         register_supported_control( controloid, controlmask, NULL, slapi_int_parse_control, NULL );
1215 }
1216
1217 int 
1218 slapi_get_supported_controls(
1219         char            ***ctrloidsp, 
1220         unsigned long   **ctrlopsp ) 
1221 {
1222         int i, rc;
1223
1224         rc = get_supported_controls( ctrloidsp, (slap_mask_t **)ctrlopsp );
1225         if ( rc != LDAP_SUCCESS ) {
1226                 return rc;
1227         }
1228
1229         for ( i = 0; (*ctrloidsp)[i] != NULL; i++ ) {
1230                 /* In place, naughty. */
1231                 slapControlMask2SlapiControlOp( (*ctrlopsp)[i], &((*ctrlopsp)[i]) );
1232         }
1233
1234         return LDAP_SUCCESS;
1235 }
1236
1237 LDAPControl *
1238 slapi_dup_control( LDAPControl *ctrl )
1239 {
1240         LDAPControl *ret;
1241
1242         ret = (LDAPControl *)slapi_ch_malloc( sizeof(*ret) );
1243         ret->ldctl_oid = slapi_ch_strdup( ctrl->ldctl_oid );
1244         ber_dupbv( &ret->ldctl_value, &ctrl->ldctl_value );
1245         ret->ldctl_iscritical = ctrl->ldctl_iscritical;
1246
1247         return ret;
1248 }
1249
1250 void 
1251 slapi_register_supported_saslmechanism( char *mechanism )
1252 {
1253         /* FIXME -- can not add saslmechanism to OpenLDAP dynamically */
1254         slapi_log_error( SLAPI_LOG_FATAL, "slapi_register_supported_saslmechanism",
1255                         "OpenLDAP does not support dynamic registration of SASL mechanisms\n" );
1256 }
1257
1258 char **
1259 slapi_get_supported_saslmechanisms( void )
1260 {
1261         /* FIXME -- can not get the saslmechanism without a connection. */
1262         slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_supported_saslmechanisms",
1263                         "can not get the SASL mechanism list "
1264                         "without a connection\n" );
1265         return NULL;
1266 }
1267
1268 char **
1269 slapi_get_supported_extended_ops( void )
1270 {
1271         int             i, j, k;
1272         char            **ppExtOpOID = NULL;
1273         int             numExtOps = 0;
1274
1275         for ( i = 0; get_supported_extop( i ) != NULL; i++ ) {
1276                 ;
1277         }
1278         
1279         for ( j = 0; slapi_int_get_supported_extop( j ) != NULL; j++ ) {
1280                 ;
1281         }
1282
1283         numExtOps = i + j;
1284         if ( numExtOps == 0 ) {
1285                 return NULL;
1286         }
1287
1288         ppExtOpOID = (char **)slapi_ch_malloc( (numExtOps + 1) * sizeof(char *) );
1289         for ( k = 0; k < i; k++ ) {
1290                 struct berval   *bv;
1291
1292                 bv = get_supported_extop( k );
1293                 assert( bv != NULL );
1294
1295                 ppExtOpOID[ k ] = bv->bv_val;
1296         }
1297         
1298         for ( ; k < j; k++ ) {
1299                 struct berval   *bv;
1300
1301                 bv = slapi_int_get_supported_extop( k );
1302                 assert( bv != NULL );
1303
1304                 ppExtOpOID[ i + k ] = bv->bv_val;
1305         }
1306         ppExtOpOID[ i + k ] = NULL;
1307
1308         return ppExtOpOID;
1309 }
1310
1311 void 
1312 slapi_send_ldap_result(
1313         Slapi_PBlock    *pb, 
1314         int             err, 
1315         char            *matched, 
1316         char            *text, 
1317         int             nentries, 
1318         struct berval   **urls ) 
1319 {
1320         SlapReply       *rs;
1321
1322         PBLOCK_ASSERT_OP( pb, 0 );
1323
1324         rs = pb->pb_rs;
1325
1326         rs->sr_err = err;
1327         rs->sr_matched = matched;
1328         rs->sr_text = text;
1329         rs->sr_ref = NULL;
1330
1331         if ( err == LDAP_SASL_BIND_IN_PROGRESS ) {
1332                 send_ldap_sasl( pb->pb_op, rs );
1333         } else if ( rs->sr_rspoid != NULL ) {
1334                 send_ldap_extended( pb->pb_op, rs );
1335         } else {
1336                 if ( pb->pb_op->o_tag == LDAP_REQ_SEARCH )
1337                         rs->sr_nentries = nentries;
1338
1339                 send_ldap_result( pb->pb_op, rs );
1340         }
1341 }
1342
1343 int 
1344 slapi_send_ldap_search_entry(
1345         Slapi_PBlock    *pb, 
1346         Slapi_Entry     *e, 
1347         LDAPControl     **ectrls, 
1348         char            **attrs, 
1349         int             attrsonly )
1350 {
1351         SlapReply               rs = { REP_SEARCH };
1352         int                     i = 0;
1353         AttributeName           *an = NULL;
1354         const char              *text;
1355         int                     rc;
1356
1357         assert( pb->pb_op != NULL );
1358
1359         if ( attrs != NULL ) {
1360                 for ( i = 0; attrs[ i ] != NULL; i++ ) {
1361                         ; /* empty */
1362                 }
1363         }
1364
1365         if ( i ) {
1366                 an = (AttributeName *) slapi_ch_malloc( (i+1) * sizeof(AttributeName) );
1367                 for ( i = 0; attrs[i] != NULL; i++ ) {
1368                         an[i].an_name.bv_val = attrs[i];
1369                         an[i].an_name.bv_len = strlen( attrs[i] );
1370                         an[i].an_desc = NULL;
1371                         rs.sr_err = slap_bv2ad( &an[i].an_name, &an[i].an_desc, &text );
1372                         if ( rs.sr_err != LDAP_SUCCESS) {
1373                                 slapi_ch_free( (void **)&an );
1374                                 return -1;
1375                         }
1376                 }
1377                 an[i].an_name.bv_len = 0;
1378                 an[i].an_name.bv_val = NULL;
1379         }
1380
1381         rs.sr_err = LDAP_SUCCESS;
1382         rs.sr_matched = NULL;
1383         rs.sr_text = NULL;
1384         rs.sr_ref = NULL;
1385         rs.sr_ctrls = ectrls;
1386         rs.sr_attrs = an;
1387         rs.sr_operational_attrs = NULL;
1388         rs.sr_entry = e;
1389         rs.sr_v2ref = NULL;
1390         rs.sr_flags = 0;
1391
1392         rc = send_search_entry( pb->pb_op, &rs );
1393
1394         slapi_ch_free( (void **)&an );
1395
1396         return rc;
1397 }
1398
1399 int 
1400 slapi_send_ldap_search_reference(
1401         Slapi_PBlock    *pb,
1402         Slapi_Entry     *e,
1403         struct berval   **references,
1404         LDAPControl     **ectrls, 
1405         struct berval   **v2refs
1406         )
1407 {
1408         SlapReply       rs = { REP_SEARCHREF };
1409         int             rc;
1410
1411         rs.sr_err = LDAP_SUCCESS;
1412         rs.sr_matched = NULL;
1413         rs.sr_text = NULL;
1414
1415         rc = bvptr2obj( references, &rs.sr_ref );
1416         if ( rc != LDAP_SUCCESS ) {
1417                 return rc;
1418         }
1419
1420         rs.sr_ctrls = ectrls;
1421         rs.sr_attrs = NULL;
1422         rs.sr_operational_attrs = NULL;
1423         rs.sr_entry = e;
1424
1425         if ( v2refs != NULL ) {
1426                 rc = bvptr2obj( v2refs, &rs.sr_v2ref );
1427                 if ( rc != LDAP_SUCCESS ) {
1428                         slapi_ch_free( (void **)&rs.sr_ref );
1429                         return rc;
1430                 }
1431         } else {
1432                 rs.sr_v2ref = NULL;
1433         }
1434
1435         rc = send_search_reference( pb->pb_op, &rs );
1436
1437         slapi_ch_free( (void **)&rs.sr_ref );
1438         slapi_ch_free( (void **)&rs.sr_v2ref );
1439
1440         return rc;
1441 }
1442
1443 Slapi_Filter *
1444 slapi_str2filter( char *str ) 
1445 {
1446         return str2filter( str );
1447 }
1448
1449 void 
1450 slapi_filter_free(
1451         Slapi_Filter    *f, 
1452         int             recurse ) 
1453 {
1454         filter_free( f );
1455 }
1456
1457 Slapi_Filter *
1458 slapi_filter_dup( Slapi_Filter *filter )
1459 {
1460         Filter *f;
1461
1462         f = (Filter *) slapi_ch_malloc( sizeof(Filter) );
1463         f->f_next = NULL;
1464         f->f_choice = filter->f_choice;
1465
1466         switch ( f->f_choice ) {
1467         case LDAP_FILTER_AND:
1468         case LDAP_FILTER_NOT:
1469         case LDAP_FILTER_OR: {
1470                 Filter *pFilter, **ppF;
1471
1472                 for ( pFilter = filter->f_list, ppF = &f->f_list;
1473                       pFilter != NULL;
1474                       pFilter = pFilter->f_next, ppF = &f->f_next )
1475                 {
1476                         *ppF = slapi_filter_dup( pFilter );
1477                         if ( *ppF == NULL )
1478                                 break;
1479                 }
1480                 break;
1481         }
1482         case LDAP_FILTER_PRESENT:
1483                 f->f_desc = filter->f_desc;
1484                 break;
1485         case LDAP_FILTER_EQUALITY:
1486         case LDAP_FILTER_GE:
1487         case LDAP_FILTER_LE:
1488         case LDAP_FILTER_APPROX:
1489                 f->f_ava = (AttributeAssertion *)slapi_ch_malloc( sizeof(AttributeAssertion) );
1490                 f->f_ava->aa_desc = filter->f_ava->aa_desc;
1491                 ber_dupbv( &f->f_ava->aa_value, &filter->f_ava->aa_value );
1492                 break;
1493         case LDAP_FILTER_EXT:
1494                 f->f_mra = (MatchingRuleAssertion *)slapi_ch_malloc( sizeof(MatchingRuleAssertion) );
1495                 f->f_mra->ma_rule = filter->f_mra->ma_rule;
1496                 f->f_mra->ma_rule_text = filter->f_mra->ma_rule_text; /* struct copy */
1497                 f->f_mra->ma_desc = filter->f_mra->ma_desc;
1498                 f->f_mra->ma_dnattrs = filter->f_mra->ma_dnattrs;
1499                 ber_dupbv( &f->f_mra->ma_value, &filter->f_mra->ma_value );
1500                 break;
1501         case LDAP_FILTER_SUBSTRINGS: {
1502                 int i;
1503
1504                 f->f_sub = (SubstringsAssertion *)slapi_ch_malloc( sizeof(SubstringsAssertion) );
1505                 f->f_sub->sa_desc = filter->f_sub->sa_desc;
1506                 ber_dupbv( &f->f_sub_initial, &filter->f_sub_initial );
1507                 if ( filter->f_sub_any != NULL ) {
1508                         for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ )
1509                                 ;
1510                         f->f_sub_any = (BerVarray)slapi_ch_malloc( (i + 1) * (sizeof(struct berval)) );
1511                         for ( i = 0; filter->f_sub_any[i].bv_val != NULL; i++ ) {
1512                                 ber_dupbv( &f->f_sub_any[i], &filter->f_sub_any[i] );
1513                         }
1514                         f->f_sub_any[i].bv_val = NULL;
1515                 } else {
1516                         f->f_sub_any = NULL;
1517                 }
1518                 ber_dupbv( &f->f_sub_final, &filter->f_sub_final );
1519                 break;
1520         }
1521         case SLAPD_FILTER_COMPUTED:
1522                 f->f_result = filter->f_result;
1523                 break;
1524         default:
1525                 slapi_ch_free( (void **)&f );
1526                 f = NULL;
1527                 break;
1528         }
1529
1530         return f;
1531 }
1532
1533 int 
1534 slapi_filter_get_choice( Slapi_Filter *f )
1535 {
1536         int             rc;
1537
1538         if ( f != NULL ) {
1539                 rc = f->f_choice;
1540         } else {
1541                 rc = 0;
1542         }
1543
1544         return rc;
1545 }
1546
1547 int 
1548 slapi_filter_get_ava(
1549         Slapi_Filter    *f, 
1550         char            **type, 
1551         struct berval   **bval )
1552 {
1553         int             ftype;
1554         int             rc = LDAP_SUCCESS;
1555
1556         assert( type != NULL );
1557         assert( bval != NULL );
1558
1559         *type = NULL;
1560         *bval = NULL;
1561
1562         ftype = f->f_choice;
1563         if ( ftype == LDAP_FILTER_EQUALITY 
1564                         || ftype ==  LDAP_FILTER_GE 
1565                         || ftype == LDAP_FILTER_LE 
1566                         || ftype == LDAP_FILTER_APPROX ) {
1567                 /*
1568                  * According to the SLAPI Reference Manual these are
1569                  * not duplicated.
1570                  */
1571                 *type = f->f_un.f_un_ava->aa_desc->ad_cname.bv_val;
1572                 *bval = &f->f_un.f_un_ava->aa_value;
1573         } else { /* filter type not supported */
1574                 rc = -1;
1575         }
1576
1577         return rc;
1578 }
1579
1580 Slapi_Filter *
1581 slapi_filter_list_first( Slapi_Filter *f )
1582 {
1583         int             ftype;
1584
1585         if ( f == NULL ) {
1586                 return NULL;
1587         }
1588
1589         ftype = f->f_choice;
1590         if ( ftype == LDAP_FILTER_AND
1591                         || ftype == LDAP_FILTER_OR
1592                         || ftype == LDAP_FILTER_NOT ) {
1593                 return (Slapi_Filter *)f->f_list;
1594         } else {
1595                 return NULL;
1596         }
1597 }
1598
1599 Slapi_Filter *
1600 slapi_filter_list_next(
1601         Slapi_Filter    *f, 
1602         Slapi_Filter    *fprev )
1603 {
1604         int             ftype;
1605
1606         if ( f == NULL ) {
1607                 return NULL;
1608         }
1609
1610         ftype = f->f_choice;
1611         if ( ftype == LDAP_FILTER_AND
1612                         || ftype == LDAP_FILTER_OR
1613                         || ftype == LDAP_FILTER_NOT )
1614         {
1615                 return fprev->f_next;
1616         }
1617
1618         return NULL;
1619 }
1620
1621 int
1622 slapi_filter_get_attribute_type( Slapi_Filter *f, char **type )
1623 {
1624         if ( f == NULL ) {
1625                 return -1;
1626         }
1627
1628         switch ( f->f_choice ) {
1629         case LDAP_FILTER_GE:
1630         case LDAP_FILTER_LE:
1631         case LDAP_FILTER_EQUALITY:
1632         case LDAP_FILTER_APPROX:
1633                 *type = f->f_av_desc->ad_cname.bv_val;
1634                 break;
1635         case LDAP_FILTER_SUBSTRINGS:
1636                 *type = f->f_sub_desc->ad_cname.bv_val;
1637                 break;
1638         case LDAP_FILTER_PRESENT:
1639                 *type = f->f_desc->ad_cname.bv_val;
1640                 break;
1641         case LDAP_FILTER_EXT:
1642                 *type = f->f_mr_desc->ad_cname.bv_val;
1643                 break;
1644         default:
1645                 /* Complex filters need not apply. */
1646                 *type = NULL;
1647                 return -1;
1648         }
1649
1650         return 0;
1651 }
1652
1653 int
1654 slapi_x_filter_set_attribute_type( Slapi_Filter *f, const char *type )
1655 {
1656         AttributeDescription **adp, *ad = NULL;
1657         const char *text;
1658         int rc;
1659
1660         if ( f == NULL ) {
1661                 return -1;
1662         }
1663
1664         switch ( f->f_choice ) {
1665         case LDAP_FILTER_GE:
1666         case LDAP_FILTER_LE:
1667         case LDAP_FILTER_EQUALITY:
1668         case LDAP_FILTER_APPROX:
1669                 adp = &f->f_av_desc;
1670                 break;
1671         case LDAP_FILTER_SUBSTRINGS:
1672                 adp = &f->f_sub_desc;
1673                 break;
1674         case LDAP_FILTER_PRESENT:
1675                 adp = &f->f_desc;
1676                 break;
1677         case LDAP_FILTER_EXT:
1678                 adp = &f->f_mr_desc;
1679                 break;
1680         default:
1681                 /* Complex filters need not apply. */
1682                 return -1;
1683         }
1684
1685         rc = slap_str2ad( type, &ad, &text );
1686         if ( rc == LDAP_SUCCESS )
1687                 *adp = ad;
1688
1689         return ( rc == LDAP_SUCCESS ) ? 0 : -1;
1690 }
1691
1692 int
1693 slapi_filter_get_subfilt( Slapi_Filter *f, char **type, char **initial,
1694         char ***any, char **final )
1695 {
1696         int i;
1697
1698         if ( f->f_choice != LDAP_FILTER_SUBSTRINGS ) {
1699                 return -1;
1700         }
1701
1702         /*
1703          * The caller shouldn't free but we can't return an
1704          * array of char *s from an array of bervals without
1705          * allocating memory, so we may as well be consistent.
1706          * XXX
1707          */
1708         *type = f->f_sub_desc->ad_cname.bv_val;
1709         *initial = f->f_sub_initial.bv_val ? slapi_ch_strdup(f->f_sub_initial.bv_val) : NULL;
1710         if ( f->f_sub_any != NULL ) {
1711                 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ )
1712                         ;
1713                 *any = (char **)slapi_ch_malloc( (i + 1) * sizeof(char *) );
1714                 for ( i = 0; f->f_sub_any[i].bv_val != NULL; i++ ) {
1715                         (*any)[i] = slapi_ch_strdup(f->f_sub_any[i].bv_val);
1716                 }
1717                 (*any)[i] = NULL;
1718         } else {
1719                 *any = NULL;
1720         }
1721         *final = f->f_sub_final.bv_val ? slapi_ch_strdup(f->f_sub_final.bv_val) : NULL;
1722
1723         return 0;
1724 }
1725
1726 Slapi_Filter *
1727 slapi_filter_join( int ftype, Slapi_Filter *f1, Slapi_Filter *f2 )
1728 {
1729         Slapi_Filter *f = NULL;
1730
1731         if ( ftype == LDAP_FILTER_AND ||
1732              ftype == LDAP_FILTER_OR ||
1733              ftype == LDAP_FILTER_NOT )
1734         {
1735                 f = (Slapi_Filter *)slapi_ch_malloc( sizeof(*f) );
1736                 f->f_choice = ftype;
1737                 f->f_list = f1;
1738                 f->f_list->f_next = f2;
1739                 f->f_next = NULL;
1740         }
1741
1742         return f;
1743 }
1744
1745 int
1746 slapi_x_filter_append( int ftype,
1747         Slapi_Filter **pContainingFilter, /* NULL on first call */
1748         Slapi_Filter **pNextFilter,
1749         Slapi_Filter *filterToAppend )
1750 {
1751         if ( ftype == LDAP_FILTER_AND ||
1752              ftype == LDAP_FILTER_OR ||
1753              ftype == LDAP_FILTER_NOT )
1754         {
1755                 if ( *pContainingFilter == NULL ) {
1756                         *pContainingFilter = (Slapi_Filter *)slapi_ch_malloc( sizeof(Slapi_Filter) );
1757                         (*pContainingFilter)->f_choice = ftype;
1758                         (*pContainingFilter)->f_list = filterToAppend;
1759                         (*pContainingFilter)->f_next = NULL;
1760                 } else {
1761                         if ( (*pContainingFilter)->f_choice != ftype ) {
1762                                 /* Sanity check */
1763                                 return -1;
1764                         }
1765                         (*pNextFilter)->f_next = filterToAppend;
1766                 }
1767                 *pNextFilter = filterToAppend;
1768
1769                 return 0;
1770         }
1771         return -1;
1772 }
1773
1774 int
1775 slapi_filter_test( Slapi_PBlock *pb, Slapi_Entry *e, Slapi_Filter *f,
1776         int verify_access )
1777 {
1778         Operation *op;
1779         int rc;
1780
1781         if ( f == NULL ) {
1782                 /* spec says return zero if no filter. */
1783                 return 0;
1784         }
1785
1786         if ( verify_access ) {
1787                 op = pb->pb_op;
1788                 if ( op == NULL )
1789                         return LDAP_PARAM_ERROR;
1790         } else {
1791                 op = NULL;
1792         }
1793
1794         /*
1795          * According to acl.c it is safe to call test_filter() with
1796          * NULL arguments...
1797          */
1798         rc = test_filter( op, e, f );
1799         switch (rc) {
1800         case LDAP_COMPARE_TRUE:
1801                 rc = 0;
1802                 break;
1803         case LDAP_COMPARE_FALSE:
1804                 break;
1805         case SLAPD_COMPARE_UNDEFINED:
1806                 rc = LDAP_OTHER;
1807                 break;
1808         case LDAP_PROTOCOL_ERROR:
1809                 /* filter type unknown: spec says return -1 */
1810                 rc = -1;
1811                 break;
1812         }
1813
1814         return rc;
1815 }
1816
1817 int
1818 slapi_filter_test_simple( Slapi_Entry *e, Slapi_Filter *f)
1819 {
1820         return slapi_filter_test( NULL, e, f, 0 );
1821 }
1822
1823 int
1824 slapi_filter_apply( Slapi_Filter *f, FILTER_APPLY_FN fn, void *arg, int *error_code )
1825 {
1826         switch ( f->f_choice ) {
1827         case LDAP_FILTER_AND:
1828         case LDAP_FILTER_NOT:
1829         case LDAP_FILTER_OR: {
1830                 int rc;
1831
1832                 /*
1833                  * FIXME: altering f; should we use a temporary?
1834                  */
1835                 for ( f = f->f_list; f != NULL; f = f->f_next ) {
1836                         rc = slapi_filter_apply( f, fn, arg, error_code );
1837                         if ( rc != 0 ) {
1838                                 return rc;
1839                         }
1840                         if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ) {
1841                                 break;
1842                         }
1843                 }
1844                 break;
1845         }
1846         case LDAP_FILTER_EQUALITY:
1847         case LDAP_FILTER_SUBSTRINGS:
1848         case LDAP_FILTER_GE:
1849         case LDAP_FILTER_LE:
1850         case LDAP_FILTER_PRESENT:
1851         case LDAP_FILTER_APPROX:
1852         case LDAP_FILTER_EXT:
1853                 *error_code = fn( f, arg );
1854                 break;
1855         default:
1856                 *error_code = SLAPI_FILTER_UNKNOWN_FILTER_TYPE;
1857         }
1858
1859         if ( *error_code == SLAPI_FILTER_SCAN_NOMORE ||
1860              *error_code == SLAPI_FILTER_SCAN_CONTINUE ) {
1861                 return 0;
1862         }
1863
1864         return -1;
1865 }
1866
1867 int 
1868 slapi_pw_find(
1869         struct berval   **vals, 
1870         struct berval   *v ) 
1871 {
1872         /*
1873          * FIXME: what's the point?
1874          */
1875         return 1;
1876 }
1877
1878 #define MAX_HOSTNAME 512
1879
1880 char *
1881 slapi_get_hostname( void ) 
1882 {
1883         char            *hn = NULL;
1884         static int      been_here = 0;   
1885         static char     *static_hn = NULL;
1886
1887         ldap_pvt_thread_mutex_lock( &slapi_hn_mutex );
1888         if ( !been_here ) {
1889                 static_hn = (char *)slapi_ch_malloc( MAX_HOSTNAME );
1890                 if ( static_hn == NULL) {
1891                         slapi_log_error( SLAPI_LOG_FATAL, "slapi_get_hostname",
1892                                         "Cannot allocate memory for hostname\n" );
1893                         static_hn = NULL;
1894                         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1895
1896                         return hn;
1897                         
1898                 } else { 
1899                         if ( gethostname( static_hn, MAX_HOSTNAME ) != 0 ) {
1900                                 slapi_log_error( SLAPI_LOG_FATAL,
1901                                                 "SLAPI",
1902                                                 "can't get hostname\n" );
1903                                 slapi_ch_free( (void **)&static_hn );
1904                                 static_hn = NULL;
1905                                 ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1906
1907                                 return hn;
1908
1909                         } else {
1910                                 been_here = 1;
1911                         }
1912                 }
1913         }
1914         ldap_pvt_thread_mutex_unlock( &slapi_hn_mutex );
1915         
1916         hn = ch_strdup( static_hn );
1917
1918         return hn;
1919 }
1920
1921 /*
1922  * FIXME: this should go in an appropriate header ...
1923  */
1924 extern int slapi_int_log_error( int level, char *subsystem, char *fmt, va_list arglist );
1925
1926 int 
1927 slapi_log_error(
1928         int             severity, 
1929         char            *subsystem, 
1930         char            *fmt, 
1931         ... ) 
1932 {
1933         int             rc = LDAP_SUCCESS;
1934         va_list         arglist;
1935
1936         va_start( arglist, fmt );
1937         rc = slapi_int_log_error( severity, subsystem, fmt, arglist );
1938         va_end( arglist );
1939
1940         return rc;
1941 }
1942
1943
1944 unsigned long
1945 slapi_timer_current_time( void ) 
1946 {
1947         static int      first_time = 1;
1948 #if !defined (_WIN32)
1949         struct timeval  now;
1950         unsigned long   ret;
1951
1952         ldap_pvt_thread_mutex_lock( &slapi_time_mutex );
1953         if (first_time) {
1954                 first_time = 0;
1955                 gettimeofday( &base_time, NULL );
1956         }
1957         gettimeofday( &now, NULL );
1958         ret = ( now.tv_sec  - base_time.tv_sec ) * 1000000 + 
1959                         (now.tv_usec - base_time.tv_usec);
1960         ldap_pvt_thread_mutex_unlock( &slapi_time_mutex );
1961
1962         return ret;
1963
1964         /*
1965          * Ain't it better?
1966         return (slap_get_time() - starttime) * 1000000;
1967          */
1968 #else /* _WIN32 */
1969         LARGE_INTEGER now;
1970
1971         if ( first_time ) {
1972                 first_time = 0;
1973                 performance_counter_present = QueryPerformanceCounter( &base_time );
1974                 QueryPerformanceFrequency( &performance_freq );
1975         }
1976
1977         if ( !performance_counter_present )
1978              return 0;
1979
1980         QueryPerformanceCounter( &now );
1981         return (1000000*(now.QuadPart-base_time.QuadPart))/performance_freq.QuadPart;
1982 #endif /* _WIN32 */
1983 }
1984
1985 /*
1986  * FIXME ?
1987  */
1988 unsigned long
1989 slapi_timer_get_time( char *label ) 
1990 {
1991         unsigned long start = slapi_timer_current_time();
1992         printf("%10ld %10d usec %s\n", start, 0, label);
1993         return start;
1994 }
1995
1996 /*
1997  * FIXME ?
1998  */
1999 void
2000 slapi_timer_elapsed_time(
2001         char *label,
2002         unsigned long start ) 
2003 {
2004         unsigned long stop = slapi_timer_current_time();
2005         printf ("%10ld %10ld usec %s\n", stop, stop - start, label);
2006 }
2007
2008 void
2009 slapi_free_search_results_internal( Slapi_PBlock *pb ) 
2010 {
2011         Slapi_Entry     **entries;
2012         int             k = 0, nEnt = 0;
2013
2014         slapi_pblock_get( pb, SLAPI_NENTRIES, &nEnt );
2015         slapi_pblock_get( pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries );
2016         if ( nEnt == 0 || entries == NULL ) {
2017                 return;
2018         }
2019
2020         for ( k = 0; k < nEnt; k++ ) {
2021                 slapi_entry_free( entries[k] );
2022                 entries[k] = NULL;
2023         }
2024         
2025         slapi_ch_free( (void **)&entries );
2026 }
2027
2028 int slapi_is_connection_ssl( Slapi_PBlock *pb, int *isSSL )
2029 {
2030         if ( pb == NULL )
2031                 return LDAP_PARAM_ERROR;
2032
2033         if ( pb->pb_conn == NULL )
2034                 return LDAP_PARAM_ERROR;
2035
2036 #ifdef HAVE_TLS
2037         *isSSL = pb->pb_conn->c_is_tls;
2038 #else
2039         *isSSL = 0;
2040 #endif
2041
2042         return LDAP_SUCCESS;
2043 }
2044
2045 /*
2046  * DS 5.x compatability API follow
2047  */
2048
2049 int slapi_attr_get_flags( const Slapi_Attr *attr, unsigned long *flags )
2050 {
2051         AttributeType *at;
2052
2053         if ( attr == NULL )
2054                 return LDAP_PARAM_ERROR;
2055
2056         at = attr->a_desc->ad_type;
2057
2058         *flags = SLAPI_ATTR_FLAG_STD_ATTR;
2059
2060         if ( is_at_single_value( at ) )
2061                 *flags |= SLAPI_ATTR_FLAG_SINGLE;
2062         if ( is_at_operational( at ) )
2063                 *flags |= SLAPI_ATTR_FLAG_OPATTR;
2064         if ( is_at_obsolete( at ) )
2065                 *flags |= SLAPI_ATTR_FLAG_OBSOLETE;
2066         if ( is_at_collective( at ) )
2067                 *flags |= SLAPI_ATTR_FLAG_COLLECTIVE;
2068         if ( is_at_no_user_mod( at ) )
2069                 *flags |= SLAPI_ATTR_FLAG_NOUSERMOD;
2070
2071         return LDAP_SUCCESS;
2072 }
2073
2074 int slapi_attr_flag_is_set( const Slapi_Attr *attr, unsigned long flag )
2075 {
2076         unsigned long flags;
2077
2078         if ( slapi_attr_get_flags( attr, &flags ) != 0 )
2079                 return 0;
2080         return (flags & flag) ? 1 : 0;
2081 }
2082
2083 Slapi_Attr *slapi_attr_new( void )
2084 {
2085         Attribute *ad;
2086
2087         ad = (Attribute  *)slapi_ch_calloc( 1, sizeof(*ad) );
2088
2089         return ad;
2090 }
2091
2092 Slapi_Attr *slapi_attr_init( Slapi_Attr *a, const char *type )
2093 {
2094         const char *text;
2095         AttributeDescription *ad = NULL;
2096
2097         if( slap_str2ad( type, &ad, &text ) != LDAP_SUCCESS ) {
2098                 return NULL;
2099         }
2100
2101         a->a_desc = ad;
2102         a->a_vals = NULL;
2103         a->a_nvals = NULL;
2104         a->a_next = NULL;
2105         a->a_flags = 0;
2106
2107         return a;
2108 }
2109
2110 void slapi_attr_free( Slapi_Attr **a )
2111 {
2112         attr_free( *a );
2113         *a = NULL;
2114 }
2115
2116 Slapi_Attr *slapi_attr_dup( const Slapi_Attr *attr )
2117 {
2118         return attr_dup( (Slapi_Attr *)attr );
2119 }
2120
2121 int slapi_attr_add_value( Slapi_Attr *a, const Slapi_Value *v )
2122 {
2123         struct berval nval;
2124         struct berval *nvalp;
2125         int rc;
2126         AttributeDescription *desc = a->a_desc;
2127
2128         if ( desc->ad_type->sat_equality &&
2129              desc->ad_type->sat_equality->smr_normalize ) {
2130                 rc = (*desc->ad_type->sat_equality->smr_normalize)(
2131                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2132                         desc->ad_type->sat_syntax,
2133                         desc->ad_type->sat_equality,
2134                         (Slapi_Value *)v, &nval, NULL );
2135                 if ( rc != LDAP_SUCCESS ) {
2136                         return rc;
2137                 }
2138                 nvalp = &nval;
2139         } else {
2140                 nvalp = NULL;
2141         }
2142
2143         rc = value_add_one( &a->a_vals, (Slapi_Value *)v );
2144         if ( rc == 0 && nvalp != NULL ) {
2145                 rc = value_add_one( &a->a_nvals, nvalp );
2146         } else {
2147                 a->a_nvals = a->a_vals;
2148         }
2149
2150         if ( nvalp != NULL ) {
2151                 slapi_ch_free_string( &nval.bv_val );
2152         }
2153
2154         return rc;
2155 }
2156
2157 int slapi_attr_type2plugin( const char *type, void **pi )
2158 {
2159         *pi = NULL;
2160
2161         return LDAP_OTHER;
2162 }
2163
2164 int slapi_attr_get_type( const Slapi_Attr *attr, char **type )
2165 {
2166         if ( attr == NULL ) {
2167                 return LDAP_PARAM_ERROR;
2168         }
2169
2170         *type = attr->a_desc->ad_cname.bv_val;
2171
2172         return LDAP_SUCCESS;
2173 }
2174
2175 int slapi_attr_get_oid_copy( const Slapi_Attr *attr, char **oidp )
2176 {
2177         if ( attr == NULL ) {
2178                 return LDAP_PARAM_ERROR;
2179         }
2180         *oidp = attr->a_desc->ad_type->sat_oid;
2181
2182         return LDAP_SUCCESS;
2183 }
2184
2185 int slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
2186 {
2187         MatchingRule *mr;
2188         int ret;
2189         int rc;
2190         const char *text;
2191
2192         mr = a->a_desc->ad_type->sat_equality;
2193         rc = value_match( &ret, a->a_desc, mr,
2194                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
2195                 (struct berval *)v1, (void *)v2, &text );
2196         if ( rc != LDAP_SUCCESS ) 
2197                 return -1;
2198
2199         return ( ret == 0 ) ? 0 : -1;
2200 }
2201
2202 int slapi_attr_value_find( const Slapi_Attr *a, struct berval *v )
2203 {
2204         MatchingRule *mr;
2205         struct berval *bv;
2206         int j;
2207         const char *text;
2208         int rc;
2209         int ret;
2210
2211         if ( a ->a_vals == NULL ) {
2212                 return -1;
2213         }
2214         mr = a->a_desc->ad_type->sat_equality;
2215         for ( bv = a->a_vals, j = 0; bv->bv_val != NULL; bv++, j++ ) {
2216                 rc = value_match( &ret, a->a_desc, mr,
2217                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, bv, v, &text );
2218                 if ( rc != LDAP_SUCCESS ) {
2219                         return -1;
2220                 }
2221                 if ( ret == 0 ) {
2222                         return 0;
2223                 }
2224         }
2225         return -1;
2226 }
2227
2228 int slapi_attr_type_cmp( const char *t1, const char *t2, int opt )
2229 {
2230         AttributeDescription *a1 = NULL;
2231         AttributeDescription *a2 = NULL;
2232         const char *text;
2233         int ret;
2234
2235         if ( slap_str2ad( t1, &a1, &text ) != LDAP_SUCCESS ) {
2236                 return -1;
2237         }
2238
2239         if ( slap_str2ad( t2, &a2, &text ) != LDAP_SUCCESS ) {
2240                 return 1;
2241         }
2242
2243 #define ad_base_cmp(l,r) (((l)->ad_type->sat_cname.bv_len < (r)->ad_type->sat_cname.bv_len) \
2244         ? -1 : (((l)->ad_type->sat_cname.bv_len > (r)->ad_type->sat_cname.bv_len) \
2245                 ? 1 : strcasecmp((l)->ad_type->sat_cname.bv_val, (r)->ad_type->sat_cname.bv_val )))
2246
2247         switch ( opt ) {
2248         case SLAPI_TYPE_CMP_EXACT:
2249                 ret = ad_cmp( a1, a2 );
2250                 break;
2251         case SLAPI_TYPE_CMP_BASE:
2252                 ret = ad_base_cmp( a1, a2 );
2253                 break;
2254         case SLAPI_TYPE_CMP_SUBTYPE:
2255                 ret = is_ad_subtype( a2, a2 );
2256                 break;
2257         default:
2258                 ret = -1;
2259                 break;
2260         }
2261
2262         return ret;
2263 }
2264
2265 int slapi_attr_types_equivalent( const char *t1, const char *t2 )
2266 {
2267         return ( slapi_attr_type_cmp( t1, t2, SLAPI_TYPE_CMP_EXACT ) == 0 );
2268 }
2269
2270 int slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
2271 {
2272         return slapi_valueset_first_value( &a->a_vals, v );
2273 }
2274
2275 int slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v )
2276 {
2277         return slapi_valueset_next_value( &a->a_vals, hint, v );
2278 }
2279
2280 int slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
2281 {
2282         *numValues = slapi_valueset_count( &a->a_vals );
2283
2284         return 0;
2285 }
2286
2287 int slapi_attr_get_valueset( const Slapi_Attr *a, Slapi_ValueSet **vs )
2288 {
2289         *vs = &((Slapi_Attr *)a)->a_vals;
2290
2291         return 0;
2292 }
2293
2294 int slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
2295 {
2296         return slapi_attr_get_values( a, vals );
2297 }
2298
2299 char *slapi_attr_syntax_normalize( const char *s )
2300 {
2301         AttributeDescription *ad = NULL;
2302         const char *text;
2303
2304         if ( slap_str2ad( s, &ad, &text ) != LDAP_SUCCESS ) {
2305                 return NULL;
2306         }
2307
2308         return ad->ad_cname.bv_val;
2309 }
2310
2311 Slapi_Value *slapi_value_new( void )
2312 {
2313         struct berval *bv;
2314
2315         bv = (struct berval *)slapi_ch_malloc( sizeof(*bv) );
2316
2317         return bv;
2318 }
2319
2320 Slapi_Value *slapi_value_new_berval(const struct berval *bval)
2321 {
2322         return ber_dupbv( NULL, (struct berval *)bval );
2323 }
2324
2325 Slapi_Value *slapi_value_new_value(const Slapi_Value *v)
2326 {
2327         return slapi_value_new_berval( v );
2328 }
2329
2330 Slapi_Value *slapi_value_new_string(const char *s)
2331 {
2332         struct berval bv;
2333
2334         bv.bv_val = (char *)s;
2335         bv.bv_len = strlen( s );
2336
2337         return slapi_value_new_berval( &bv );
2338 }
2339
2340 Slapi_Value *slapi_value_init(Slapi_Value *val)
2341 {
2342         val->bv_val = NULL;
2343         val->bv_len = 0;
2344
2345         return val;
2346 }
2347
2348 Slapi_Value *slapi_value_init_berval(Slapi_Value *v, struct berval *bval)
2349 {
2350         return ber_dupbv( v, bval );
2351 }
2352
2353 Slapi_Value *slapi_value_init_string(Slapi_Value *v, const char *s)
2354 {
2355         v->bv_val = slapi_ch_strdup( (char *)s );
2356         v->bv_len = strlen( s );
2357
2358         return v;
2359 }
2360
2361 Slapi_Value *slapi_value_dup(const Slapi_Value *v)
2362 {
2363         return slapi_value_new_value( v );
2364 }
2365
2366 void slapi_value_free(Slapi_Value **value)
2367 {
2368         if ( value == NULL ) {
2369                 return;
2370         }
2371
2372         if ( (*value) != NULL ) {
2373                 slapi_ch_free( (void **)&(*value)->bv_val );
2374                 slapi_ch_free( (void **)value );
2375         }
2376 }
2377
2378 const struct berval *slapi_value_get_berval( const Slapi_Value *value )
2379 {
2380         return value;
2381 }
2382
2383 Slapi_Value *slapi_value_set_berval( Slapi_Value *value, const struct berval *bval )
2384 {
2385         if ( value == NULL ) {
2386                 return NULL;
2387         }
2388         if ( value->bv_val != NULL ) {
2389                 slapi_ch_free( (void **)&value->bv_val );
2390         }
2391         slapi_value_init_berval( value, (struct berval *)bval );
2392
2393         return value;
2394 }
2395
2396 Slapi_Value *slapi_value_set_value( Slapi_Value *value, const Slapi_Value *vfrom)
2397 {
2398         if ( value == NULL ) {
2399                 return NULL;
2400         }
2401         return slapi_value_set_berval( value, vfrom );
2402 }
2403
2404 Slapi_Value *slapi_value_set( Slapi_Value *value, void *val, unsigned long len)
2405 {
2406         if ( value == NULL ) {
2407                 return NULL;
2408         }
2409         if ( value->bv_val != NULL ) {
2410                 slapi_ch_free( (void **)&value->bv_val );
2411         }
2412         value->bv_val = slapi_ch_malloc( len );
2413         value->bv_len = len;
2414         AC_MEMCPY( value->bv_val, val, len );
2415
2416         return value;
2417 }
2418
2419 int slapi_value_set_string(Slapi_Value *value, const char *strVal)
2420 {
2421         if ( value == NULL ) {
2422                 return -1;
2423         }
2424         slapi_value_set( value, (void *)strVal, strlen( strVal ) );
2425         return 0;
2426 }
2427
2428 int slapi_value_set_int(Slapi_Value *value, int intVal)
2429 {
2430         char buf[64];
2431
2432         snprintf( buf, sizeof( buf ), "%d", intVal );
2433
2434         return slapi_value_set_string( value, buf );
2435 }
2436
2437 const char *slapi_value_get_string(const Slapi_Value *value)
2438 {
2439         if ( value == NULL ) return NULL;
2440         if ( value->bv_val == NULL ) return NULL;
2441         if ( !checkBVString( value ) ) return NULL;
2442
2443         return value->bv_val;
2444 }
2445
2446 int slapi_value_get_int(const Slapi_Value *value)
2447 {
2448         if ( value == NULL ) return 0;
2449         if ( value->bv_val == NULL ) return 0;
2450         if ( !checkBVString( value ) ) return 0;
2451
2452         return (int)strtol( value->bv_val, NULL, 10 );
2453 }
2454
2455 unsigned int slapi_value_get_uint(const Slapi_Value *value)
2456 {
2457         if ( value == NULL ) return 0;
2458         if ( value->bv_val == NULL ) return 0;
2459         if ( !checkBVString( value ) ) return 0;
2460
2461         return (unsigned int)strtoul( value->bv_val, NULL, 10 );
2462 }
2463
2464 long slapi_value_get_long(const Slapi_Value *value)
2465 {
2466         if ( value == NULL ) return 0;
2467         if ( value->bv_val == NULL ) return 0;
2468         if ( !checkBVString( value ) ) return 0;
2469
2470         return strtol( value->bv_val, NULL, 10 );
2471 }
2472
2473 unsigned long slapi_value_get_ulong(const Slapi_Value *value)
2474 {
2475         if ( value == NULL ) return 0;
2476         if ( value->bv_val == NULL ) return 0;
2477         if ( !checkBVString( value ) ) return 0;
2478
2479         return strtoul( value->bv_val, NULL, 10 );
2480 }
2481
2482 size_t slapi_value_get_length(const Slapi_Value *value)
2483 {
2484         if ( value == NULL )
2485                 return 0;
2486
2487         return (size_t) value->bv_len;
2488 }
2489
2490 int slapi_value_compare(const Slapi_Attr *a, const Slapi_Value *v1, const Slapi_Value *v2)
2491 {
2492         return slapi_attr_value_cmp( a, v1, v2 );
2493 }
2494
2495 /* A ValueSet is a container for a BerVarray. */
2496 Slapi_ValueSet *slapi_valueset_new( void )
2497 {
2498         Slapi_ValueSet *vs;
2499
2500         vs = (Slapi_ValueSet *)slapi_ch_malloc( sizeof( *vs ) );
2501         *vs = NULL;
2502
2503         return vs;
2504 }
2505
2506 void slapi_valueset_free(Slapi_ValueSet *vs)
2507 {
2508         if ( vs != NULL ) {
2509                 BerVarray vp = *vs;
2510
2511                 ber_bvarray_free( vp );
2512                 slapi_ch_free( (void **)&vp );
2513
2514                 *vs = NULL;
2515         }
2516 }
2517
2518 void slapi_valueset_init(Slapi_ValueSet *vs)
2519 {
2520         if ( vs != NULL && *vs == NULL ) {
2521                 *vs = (Slapi_ValueSet)slapi_ch_calloc( 1, sizeof(struct berval) );
2522                 (*vs)->bv_val = NULL;
2523                 (*vs)->bv_len = 0;
2524         }
2525 }
2526
2527 void slapi_valueset_done(Slapi_ValueSet *vs)
2528 {
2529         BerVarray vp;
2530
2531         if ( vs == NULL )
2532                 return;
2533
2534         for ( vp = *vs; vp->bv_val != NULL; vp++ ) {
2535                 vp->bv_len = 0;
2536                 slapi_ch_free( (void **)&vp->bv_val );
2537         }
2538         /* but don't free *vs or vs */
2539 }
2540
2541 void slapi_valueset_add_value(Slapi_ValueSet *vs, const Slapi_Value *addval)
2542 {
2543         struct berval bv;
2544
2545         ber_dupbv( &bv, (Slapi_Value *)addval );
2546         ber_bvarray_add( vs, &bv );
2547 }
2548
2549 int slapi_valueset_first_value( Slapi_ValueSet *vs, Slapi_Value **v )
2550 {
2551         return slapi_valueset_next_value( vs, 0, v );
2552 }
2553
2554 int slapi_valueset_next_value( Slapi_ValueSet *vs, int index, Slapi_Value **v)
2555 {
2556         int i;
2557         BerVarray vp;
2558
2559         if ( vs == NULL )
2560                 return -1;
2561
2562         vp = *vs;
2563
2564         for ( i = 0; vp[i].bv_val != NULL; i++ ) {
2565                 if ( i == index ) {
2566                         *v = &vp[i];
2567                         return index + 1;
2568                 }
2569         }
2570
2571         return -1;
2572 }
2573
2574 int slapi_valueset_count( const Slapi_ValueSet *vs )
2575 {
2576         int i;
2577         BerVarray vp;
2578
2579         if ( vs == NULL )
2580                 return 0;
2581
2582         vp = *vs;
2583
2584         for ( i = 0; vp[i].bv_val != NULL; i++ )
2585                 ;
2586
2587         return i;
2588
2589 }
2590
2591 void slapi_valueset_set_valueset(Slapi_ValueSet *vs1, const Slapi_ValueSet *vs2)
2592 {
2593         BerVarray vp;
2594
2595         for ( vp = *vs2; vp->bv_val != NULL; vp++ ) {
2596                 slapi_valueset_add_value( vs1, vp );
2597         }
2598 }
2599
2600 int slapi_access_allowed( Slapi_PBlock *pb, Slapi_Entry *e, char *attr,
2601         struct berval *val, int access )
2602 {
2603         int rc;
2604         slap_access_t slap_access;
2605         AttributeDescription *ad = NULL;
2606         const char *text;
2607
2608         rc = slap_str2ad( attr, &ad, &text );
2609         if ( rc != LDAP_SUCCESS ) {
2610                 return rc;
2611         }
2612
2613         /*
2614          * Whilst the SLAPI access types are arranged as a bitmask, the
2615          * documentation indicates that they are to be used separately.
2616          */
2617         switch ( access & SLAPI_ACL_ALL ) {
2618         case SLAPI_ACL_COMPARE:
2619                 slap_access = ACL_COMPARE;
2620                 break;
2621         case SLAPI_ACL_SEARCH:
2622                 slap_access = ACL_SEARCH;
2623                 break;
2624         case SLAPI_ACL_READ:
2625                 slap_access = ACL_READ;
2626                 break;
2627         case SLAPI_ACL_WRITE:
2628                 slap_access = ACL_WRITE;
2629                 break;
2630         case SLAPI_ACL_DELETE:
2631                 slap_access = ACL_WDEL;
2632                 break;
2633         case SLAPI_ACL_ADD:
2634                 slap_access = ACL_WADD;
2635                 break;
2636         case SLAPI_ACL_SELF:  /* not documented */
2637         case SLAPI_ACL_PROXY: /* not documented */
2638         default:
2639                 return LDAP_INSUFFICIENT_ACCESS;
2640                 break;
2641         }
2642
2643         assert( pb->pb_op != NULL );
2644
2645         if ( access_allowed( pb->pb_op, e, ad, val, slap_access, NULL ) ) {
2646                 return LDAP_SUCCESS;
2647         }
2648
2649         return LDAP_INSUFFICIENT_ACCESS;
2650 }
2651
2652 int slapi_acl_check_mods(Slapi_PBlock *pb, Slapi_Entry *e, LDAPMod **mods, char **errbuf)
2653 {
2654         int rc = LDAP_SUCCESS;
2655         Modifications *ml;
2656
2657         if ( pb == NULL || pb->pb_op == NULL )
2658                 return LDAP_PARAM_ERROR;
2659
2660         ml = slapi_int_ldapmods2modifications( mods, 0, NULL );
2661         if ( ml == NULL ) {
2662                 return LDAP_OTHER;
2663         }
2664
2665         if ( rc == LDAP_SUCCESS ) {
2666                 rc = acl_check_modlist( pb->pb_op, e, ml ) ? LDAP_SUCCESS : LDAP_INSUFFICIENT_ACCESS;
2667         }
2668
2669         /* Careful when freeing the modlist because it has pointers into the mods array. */
2670         slapi_int_mods_free( ml );
2671
2672         return rc;
2673 }
2674
2675 /*
2676  * Synthesise an LDAPMod array from a Modifications list to pass
2677  * to SLAPI. This synthesis is destructive and as such the 
2678  * Modifications list may not be used after calling this 
2679  * function.
2680  * 
2681  * This function must also be called before slap_mods_check().
2682  */
2683 LDAPMod **slapi_int_modifications2ldapmods(
2684         Modifications **pmodlist,
2685         void *memctx
2686 )
2687 {
2688         Modifications *ml, *modlist;
2689         LDAPMod **mods, *modp;
2690         int i, j;
2691
2692         modlist = *pmodlist;
2693
2694         for( i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next )
2695                 ;
2696
2697         mods = (LDAPMod **)slap_sl_malloc( (i + 1) * sizeof(LDAPMod *), memctx );
2698
2699         for( i = 0, ml = modlist; ml != NULL; ml = ml->sml_next ) {
2700                 mods[i] = (LDAPMod *)slap_sl_malloc( sizeof(LDAPMod), memctx );
2701                 modp = mods[i];
2702                 modp->mod_op = ml->sml_op | LDAP_MOD_BVALUES;
2703                 modp->mod_type = slapi_ch_strdup( ml->sml_type.bv_val );
2704                 ml->sml_type.bv_val = NULL;
2705
2706                 if ( ml->sml_values != NULL ) {
2707                         for( j = 0; ml->sml_values[j].bv_val != NULL; j++ )
2708                                 ;
2709                         modp->mod_bvalues = (struct berval **)slap_sl_malloc( (j + 1) *
2710                                 sizeof(struct berval *), memctx );
2711                         for( j = 0; ml->sml_values[j].bv_val != NULL; j++ ) {
2712                                 /* Take ownership of original values. */
2713                                 modp->mod_bvalues[j] = (struct berval *)slap_sl_malloc(
2714                                                 sizeof(struct berval), memctx );
2715                                 modp->mod_bvalues[j]->bv_len = ml->sml_values[j].bv_len;
2716                                 modp->mod_bvalues[j]->bv_val = ml->sml_values[j].bv_val;
2717                                 ml->sml_values[j].bv_len = 0;
2718                                 ml->sml_values[j].bv_val = NULL;
2719                         }
2720                         modp->mod_bvalues[j] = NULL;
2721                         slapi_ch_free( (void **)&ml->sml_values );
2722                 } else {
2723                         modp->mod_bvalues = NULL;
2724                 }
2725                 i++;
2726         }
2727
2728         mods[i] = NULL;
2729
2730         slap_mods_free( modlist );
2731         *pmodlist = NULL;
2732
2733         return mods;
2734 }
2735
2736 /*
2737  * Convert a potentially modified array of LDAPMods back to a
2738  * Modification list. 
2739  * 
2740  * The returned Modification list contains pointers into the
2741  * LDAPMods array; the latter MUST be freed with
2742  * slapi_int_free_ldapmods() (see below).
2743  */
2744 Modifications *slapi_int_ldapmods2modifications ( LDAPMod **mods, int dup, void *memctx )
2745 {
2746         Modifications *modlist = NULL, **modtail;
2747         LDAPMod **modp;
2748
2749         if ( mods == NULL ) {
2750                 return NULL;
2751         }
2752
2753         modtail = &modlist;
2754
2755         for ( modp = mods; *modp != NULL; modp++ ) {
2756                 Modifications *mod;
2757                 LDAPMod *lmod = *modp;
2758                 int i;
2759                 const char *text;
2760                 AttributeDescription *ad = NULL;
2761
2762                 if ( slap_str2ad( lmod->mod_type, &ad, &text ) != LDAP_SUCCESS ) {
2763                         continue;
2764                 }
2765
2766                 mod = (Modifications *) slap_sl_malloc( sizeof(Modifications), memctx );
2767                 mod->sml_op = lmod->mod_op & ~(LDAP_MOD_BVALUES);
2768                 mod->sml_flags = 0;
2769                 mod->sml_type = ad->ad_cname;
2770                 mod->sml_desc = ad;
2771                 mod->sml_next = NULL;
2772
2773                 i = 0;
2774                 if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2775                         if ( lmod->mod_bvalues != NULL ) {
2776                                 while ( lmod->mod_bvalues[i] != NULL )
2777                                         i++;
2778                         }
2779                 } else {
2780                         if ( lmod->mod_values != NULL ) {
2781                                 while ( lmod->mod_values[i] != NULL )
2782                                         i++;
2783                         }
2784                 }
2785
2786                 if ( i == 0 ) {
2787                         mod->sml_values = NULL;
2788                 } else {
2789                         mod->sml_values = (BerVarray) slap_sl_malloc( (i + 1) * sizeof(struct berval), memctx );
2790
2791                         /* NB: This implicitly trusts a plugin to return valid modifications. */
2792                         if ( lmod->mod_op & LDAP_MOD_BVALUES ) {
2793                                 for ( i = 0; lmod->mod_bvalues[i] != NULL; i++ ) {
2794                                         if ( dup ) {
2795                                                 ber_dupbv( &mod->sml_values[i], lmod->mod_bvalues[i] );
2796                                         } else {
2797                                                 mod->sml_values[i].bv_val = lmod->mod_bvalues[i]->bv_val;
2798                                                 mod->sml_values[i].bv_len = lmod->mod_bvalues[i]->bv_len;
2799                                         }
2800                                 }
2801                         } else {
2802                                 for ( i = 0; lmod->mod_values[i] != NULL; i++ ) {
2803                                         mod->sml_values[i].bv_val = dup ? slapi_ch_strdup( lmod->mod_values[i] ) :
2804                                                 lmod->mod_values[i];
2805                                         mod->sml_values[i].bv_len = strlen( lmod->mod_values[i] );
2806                                 }
2807                         }
2808                         mod->sml_values[i].bv_val = NULL;
2809                         mod->sml_values[i].bv_len = 0;
2810                 }
2811                 mod->sml_nvalues = NULL;
2812
2813                 *modtail = mod;
2814                 modtail = &mod->sml_next;
2815         }
2816         
2817         return modlist;
2818 }
2819
2820 /*
2821  * For an internal operation, the unnormalized values are
2822  * owned by the caller (ie. the plugin making the internal
2823  * operation).
2824  */
2825 void
2826 slapi_int_mods_free( Modifications *ml )
2827 {
2828         Modifications           *next;
2829
2830         for ( ; ml != NULL; ml = next ) {
2831                 next = ml->sml_next;
2832
2833                 if ( ml->sml_nvalues != NULL ) {
2834                         ber_bvarray_free( ml->sml_nvalues );
2835                         ml->sml_nvalues = NULL;
2836                 }
2837                 slapi_ch_free( (void **)&ml->sml_values );
2838                 slapi_ch_free( (void **)&ml );
2839         }
2840 }
2841
2842 /*
2843  * This function only frees the parts of the mods array that
2844  * are not shared with the Modification list that was created
2845  * by slapi_int_ldapmods2modifications() (if dup == 0).
2846  */
2847 void
2848 slapi_int_free_ldapmods ( LDAPMod **mods )
2849 {
2850         int i, j;
2851
2852         if ( mods == NULL )
2853                 return;
2854
2855         for ( i = 0; mods[i] != NULL; i++ ) {
2856                 /*
2857                  * Don't free values themselves; they're owned by the
2858                  * Modification list. Do free the containing array.
2859                  */
2860                 if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
2861                         if ( mods[i]->mod_bvalues != NULL ) {
2862                                 for ( j = 0; mods[i]->mod_bvalues[j] != NULL; j++ )
2863                                         slapi_ch_free( (void **)&mods[i]->mod_bvalues[j] );
2864                                 slapi_ch_free( (void **)&mods[i]->mod_bvalues );
2865                         }
2866                 } else {
2867                         slapi_ch_free( (void **)&mods[i]->mod_values );
2868                 }
2869                 slapi_ch_free_string( &mods[i]->mod_type );
2870                 slapi_ch_free( (void **)&mods[i] );
2871         }
2872         slapi_ch_free( (void **)&mods );
2873 }
2874
2875 /*
2876  * Sun ONE DS 5.x computed attribute support. Computed attributes
2877  * allow for dynamically generated operational attributes, a very
2878  * useful thing indeed.
2879  */
2880
2881 /*
2882  * For some reason Sun don't use the normal plugin mechanism
2883  * registration path to register an "evaluator" function (an
2884  * "evaluator" is responsible for adding computed attributes;
2885  * the nomenclature is somewhat confusing).
2886  *
2887  * As such slapi_compute_add_evaluator() registers the 
2888  * function directly.
2889  */
2890 int slapi_compute_add_evaluator(slapi_compute_callback_t function)
2891 {
2892         Slapi_PBlock *pPlugin = NULL;
2893         int rc;
2894         int type = SLAPI_PLUGIN_OBJECT;
2895
2896         pPlugin = slapi_pblock_new();
2897         if ( pPlugin == NULL ) {
2898                 rc = LDAP_NO_MEMORY;
2899                 goto done;
2900         }
2901
2902         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2903         if ( rc != LDAP_SUCCESS ) {
2904                 goto done;
2905         }
2906
2907         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (void *)function );
2908         if ( rc != LDAP_SUCCESS ) {
2909                 goto done;
2910         }
2911
2912         rc = slapi_int_register_plugin( frontendDB, pPlugin );
2913         if ( rc != 0 ) {
2914                 rc = LDAP_OTHER;
2915                 goto done;
2916         }
2917
2918 done:
2919         if ( rc != LDAP_SUCCESS ) {
2920                 if ( pPlugin != NULL ) {
2921                         slapi_pblock_destroy( pPlugin );
2922                 }
2923                 return -1;
2924         }
2925
2926         return 0;
2927 }
2928
2929 /*
2930  * See notes above regarding slapi_compute_add_evaluator().
2931  */
2932 int slapi_compute_add_search_rewriter(slapi_search_rewrite_callback_t function)
2933 {
2934         Slapi_PBlock *pPlugin = NULL;
2935         int rc;
2936         int type = SLAPI_PLUGIN_OBJECT;
2937
2938         pPlugin = slapi_pblock_new();
2939         if ( pPlugin == NULL ) {
2940                 rc = LDAP_NO_MEMORY;
2941                 goto done;
2942         }
2943
2944         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_TYPE, (void *)&type );
2945         if ( rc != LDAP_SUCCESS ) {
2946                 goto done;
2947         }
2948
2949         rc = slapi_pblock_set( pPlugin, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, (void *)function );
2950         if ( rc != LDAP_SUCCESS ) {
2951                 goto done;
2952         }
2953
2954         rc = slapi_int_register_plugin( frontendDB, pPlugin );
2955         if ( rc != 0 ) {
2956                 rc = LDAP_OTHER;
2957                 goto done;
2958         }
2959
2960 done:
2961         if ( rc != LDAP_SUCCESS ) {
2962                 if ( pPlugin != NULL ) {
2963                         slapi_pblock_destroy( pPlugin );
2964                 }
2965                 return -1;
2966         }
2967
2968         return 0;
2969 }
2970
2971 /*
2972  * Call compute evaluators
2973  */
2974 int compute_evaluator(computed_attr_context *c, char *type, Slapi_Entry *e, slapi_compute_output_t outputfn)
2975 {
2976         int rc = 0;
2977         slapi_compute_callback_t *pGetPlugin, *tmpPlugin;
2978
2979         rc = slapi_int_get_plugins( frontendDB, SLAPI_PLUGIN_COMPUTE_EVALUATOR_FN, (SLAPI_FUNC **)&tmpPlugin );
2980         if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
2981                 /* Nothing to do; front-end should ignore. */
2982                 return 0;
2983         }
2984
2985         for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
2986                 /*
2987                  * -1: no attribute matched requested type
2988                  *  0: one attribute matched
2989                  * >0: error happened
2990                  */
2991                 rc = (*pGetPlugin)( c, type, e, outputfn );
2992                 if ( rc > 0 ) {
2993                         break;
2994                 }
2995         }
2996
2997         slapi_ch_free( (void **)&tmpPlugin );
2998
2999         return rc;
3000 }
3001
3002 int
3003 compute_rewrite_search_filter( Slapi_PBlock *pb )
3004 {
3005         if ( pb == NULL || pb->pb_op == NULL )
3006                 return LDAP_PARAM_ERROR;
3007
3008         return slapi_int_call_plugins( pb->pb_op->o_bd, SLAPI_PLUGIN_COMPUTE_SEARCH_REWRITER_FN, pb );
3009 }
3010
3011 /*
3012  * New API to provide the plugin with access to the search
3013  * pblock. Have informed Sun DS team.
3014  */
3015 int
3016 slapi_x_compute_get_pblock(computed_attr_context *c, Slapi_PBlock **pb)
3017 {
3018         if ( c == NULL )
3019                 return -1;
3020
3021         if ( c->cac_pb == NULL )
3022                 return -1;
3023
3024         *pb = c->cac_pb;
3025
3026         return 0;
3027 }
3028
3029 Slapi_Mutex *slapi_new_mutex( void )
3030 {
3031         Slapi_Mutex *m;
3032
3033         m = (Slapi_Mutex *)slapi_ch_malloc( sizeof(*m) );
3034         if ( ldap_pvt_thread_mutex_init( &m->mutex ) != 0 ) {
3035                 slapi_ch_free( (void **)&m );
3036                 return NULL;
3037         }
3038
3039         return m;
3040 }
3041
3042 void slapi_destroy_mutex( Slapi_Mutex *mutex )
3043 {
3044         if ( mutex != NULL ) {
3045                 ldap_pvt_thread_mutex_destroy( &mutex->mutex );
3046                 slapi_ch_free( (void **)&mutex);
3047         }
3048 }
3049
3050 void slapi_lock_mutex( Slapi_Mutex *mutex )
3051 {
3052         ldap_pvt_thread_mutex_lock( &mutex->mutex );
3053 }
3054
3055 int slapi_unlock_mutex( Slapi_Mutex *mutex )
3056 {
3057         return ldap_pvt_thread_mutex_unlock( &mutex->mutex );
3058 }
3059
3060 Slapi_CondVar *slapi_new_condvar( Slapi_Mutex *mutex )
3061 {
3062         Slapi_CondVar *cv;
3063
3064         if ( mutex == NULL ) {
3065                 return NULL;
3066         }
3067
3068         cv = (Slapi_CondVar *)slapi_ch_malloc( sizeof(*cv) );
3069         if ( ldap_pvt_thread_cond_init( &cv->cond ) != 0 ) {
3070                 slapi_ch_free( (void **)&cv );
3071                 return NULL;
3072         }
3073
3074         cv->mutex = mutex->mutex;
3075
3076         return cv;
3077 }
3078
3079 void slapi_destroy_condvar( Slapi_CondVar *cvar )
3080 {
3081         if ( cvar != NULL ) {
3082                 ldap_pvt_thread_cond_destroy( &cvar->cond );
3083                 slapi_ch_free( (void **)&cvar );
3084         }
3085 }
3086
3087 int slapi_wait_condvar( Slapi_CondVar *cvar, struct timeval *timeout )
3088 {
3089         if ( cvar == NULL ) {
3090                 return -1;
3091         }
3092
3093         return ldap_pvt_thread_cond_wait( &cvar->cond, &cvar->mutex );
3094 }
3095
3096 int slapi_notify_condvar( Slapi_CondVar *cvar, int notify_all )
3097 {
3098         if ( cvar == NULL ) {
3099                 return -1;
3100         }
3101
3102         if ( notify_all ) {
3103                 return ldap_pvt_thread_cond_broadcast( &cvar->cond );
3104         }
3105
3106         return ldap_pvt_thread_cond_signal( &cvar->cond );
3107 }
3108
3109 int slapi_int_access_allowed( Operation *op,
3110         Entry *entry,
3111         AttributeDescription *desc,
3112         struct berval *val,
3113         slap_access_t access,
3114         AccessControlState *state )
3115 {
3116         int rc, slap_access = 0;
3117         slapi_acl_callback_t *pGetPlugin, *tmpPlugin;
3118         Slapi_PBlock *pb;
3119
3120         pb = SLAPI_OPERATION_PBLOCK( op );
3121         if ( pb == NULL ) {
3122                 /* internal operation */
3123                 return 1;
3124         }
3125
3126         switch ( access ) {
3127         case ACL_COMPARE:
3128                 slap_access |= SLAPI_ACL_COMPARE;
3129                 break;
3130         case ACL_SEARCH:
3131                 slap_access |= SLAPI_ACL_SEARCH;
3132                 break;
3133         case ACL_READ:
3134                 slap_access |= SLAPI_ACL_READ;
3135                 break;
3136         case ACL_WRITE:
3137                 slap_access |= SLAPI_ACL_WRITE;
3138                 break;
3139         case ACL_WDEL:
3140                 slap_access |= SLAPI_ACL_DELETE;
3141                 break;
3142         case ACL_WADD:
3143                 slap_access |= SLAPI_ACL_ADD;
3144                 break;
3145         default:
3146                 break;
3147         }
3148
3149         rc = slapi_int_get_plugins( op->o_bd, SLAPI_PLUGIN_ACL_ALLOW_ACCESS, (SLAPI_FUNC **)&tmpPlugin );
3150         if ( rc != LDAP_SUCCESS || tmpPlugin == NULL ) {
3151                 /* nothing to do; allowed access */
3152                 return 1;
3153         }
3154
3155         rc = 1; /* default allow policy */
3156
3157         for ( pGetPlugin = tmpPlugin; *pGetPlugin != NULL; pGetPlugin++ ) {
3158                 /*
3159                  * 0    access denied
3160                  * 1    access granted
3161                  */
3162                 rc = (*pGetPlugin)( pb, entry, desc->ad_cname.bv_val,
3163                                     val, slap_access, (void *)state );
3164                 if ( rc == 0 ) {
3165                         break;
3166                 }
3167         }
3168
3169         slapi_ch_free( (void **)&tmpPlugin );
3170
3171         return rc;
3172 }
3173
3174 /*
3175  * There is no documentation for this.
3176  */
3177 int slapi_rdn2typeval( char *rdn, char **type, struct berval *bv )
3178 {
3179         LDAPRDN lrdn;
3180         LDAPAVA *ava;
3181         int rc;
3182         char *p;
3183
3184         *type = NULL;
3185
3186         bv->bv_len = 0;
3187         bv->bv_val = NULL;
3188
3189         rc = ldap_str2rdn( rdn, &lrdn, &p, LDAP_DN_FORMAT_LDAPV3 );
3190         if ( rc != LDAP_SUCCESS ) {
3191                 return -1;
3192         }
3193
3194         if ( lrdn[1] != NULL ) {
3195                 return -1; /* not single valued */
3196         }
3197
3198         ava = lrdn[0];
3199
3200         *type = slapi_ch_strdup( ava->la_attr.bv_val );
3201         ber_dupbv( bv, &ava->la_value );
3202
3203         ldap_rdnfree(lrdn);
3204
3205         return 0;
3206 }
3207
3208 char *slapi_dn_plus_rdn( const char *dn, const char *rdn )
3209 {
3210         struct berval new_dn, parent_dn, newrdn;
3211
3212         new_dn.bv_val = NULL;
3213
3214         parent_dn.bv_val = (char *)dn;
3215         parent_dn.bv_len = strlen( dn );
3216
3217         newrdn.bv_val = (char *)rdn;
3218         newrdn.bv_len = strlen( rdn );
3219
3220         build_new_dn( &new_dn, &parent_dn, &newrdn, NULL );
3221
3222         return new_dn.bv_val;
3223 }
3224
3225 int slapi_entry_schema_check( Slapi_PBlock *pb, Slapi_Entry *e )
3226 {
3227         Backend *be;
3228         const char *text;
3229         char textbuf[SLAP_TEXT_BUFLEN] = { '\0' };
3230         size_t textlen = sizeof textbuf;
3231         int rc = LDAP_SUCCESS;
3232
3233         be = select_backend( &e->e_nname, 0, 0 );
3234         if ( be != NULL )
3235                 rc = entry_schema_check( be, e, NULL, 0,
3236                         &text, textbuf, textlen );
3237
3238         return ( rc == LDAP_SUCCESS ) ? 0 : 1;
3239 }
3240
3241 int slapi_entry_rdn_values_present( const Slapi_Entry *e )
3242 {
3243         LDAPDN dn;
3244         int rc;
3245         int i = 0, match = 0;
3246
3247         rc = ldap_bv2dn( &((Entry *)e)->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3248         if ( rc != LDAP_SUCCESS ) {
3249                 return 0;
3250         }
3251
3252         if ( dn[0] != NULL ) {
3253                 LDAPRDN rdn = dn[0];
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( (Slapi_Entry *)e, ava->la_attr.bv_val, &a ) == 0 &&
3260                              slapi_attr_value_find( a, &ava->la_value ) == 0 )
3261                                 match++;
3262                 }
3263         }
3264
3265         ldap_dnfree( dn );
3266
3267         return ( i == match );
3268 }
3269
3270 int slapi_entry_add_rdn_values( Slapi_Entry *e )
3271 {
3272         LDAPDN dn;
3273         int i, rc;
3274
3275         rc = ldap_bv2dn( &e->e_name, &dn, LDAP_DN_FORMAT_LDAPV3 );
3276         if ( rc != LDAP_SUCCESS ) {
3277                 return rc;
3278         }
3279
3280         if ( dn[0] != NULL ) {
3281                 LDAPRDN rdn = dn[0];
3282                 struct berval *vals[2];
3283
3284                 for ( i = 0; rdn[i] != NULL; i++ ) {
3285                         LDAPAVA *ava = &rdn[0][i];
3286                         Slapi_Attr *a = NULL;
3287
3288                         if ( slapi_entry_attr_find( e, ava->la_attr.bv_val, &a ) == 0 &&
3289                              slapi_attr_value_find( a, &ava->la_value ) == 0 )
3290                                 continue;
3291
3292                         vals[0] = &ava->la_value;
3293                         vals[1] = NULL;
3294
3295                         slapi_entry_attr_merge( e, ava->la_attr.bv_val, vals );
3296                 }
3297         }
3298
3299         ldap_dnfree( dn );
3300
3301         return LDAP_SUCCESS;
3302 }
3303
3304 const char *slapi_entry_get_uniqueid( const Slapi_Entry *e )
3305 {
3306         Attribute *attr;
3307
3308         attr = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
3309         if ( attr == NULL ) {
3310                 return NULL;
3311         }
3312
3313         if ( attr->a_vals != NULL && attr->a_vals[0].bv_len != 0 ) {
3314                 return slapi_value_get_string( &attr->a_vals[0] );
3315         }
3316
3317         return NULL;
3318 }
3319
3320 void slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
3321 {
3322         struct berval bv;
3323
3324         attr_delete ( &e->e_attrs, slap_schema.si_ad_entryUUID );
3325
3326         bv.bv_val = uniqueid;
3327         bv.bv_len = strlen( uniqueid );
3328         attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, &bv, NULL );
3329 }
3330
3331 LDAP *slapi_ldap_init( char *ldaphost, int ldapport, int secure, int shared )
3332 {
3333         LDAP *ld;
3334         char *url;
3335         size_t size;
3336         int rc;
3337
3338         size = sizeof("ldap:///");
3339         if ( secure )
3340                 size++;
3341         size += strlen( ldaphost );
3342         if ( ldapport != 0 )
3343                 size += 32;
3344
3345         url = slapi_ch_malloc( size );
3346
3347         if ( ldapport != 0 ) {
3348                 sprintf( url, "ldap%s://%s:%d/", ( secure ? "s" : "" ), ldaphost, ldapport );
3349         } else {
3350                 sprintf( url, "ldap%s://%s/", ( secure ? "s" : "" ), ldaphost );
3351         }
3352
3353         rc = ldap_initialize( &ld, url );
3354
3355         slapi_ch_free_string( &url );
3356
3357         return ( rc == LDAP_SUCCESS ) ? ld : NULL;
3358 }
3359
3360 void slapi_ldap_unbind( LDAP *ld )
3361 {
3362         ldap_unbind_ext_s( ld, NULL, NULL );
3363 }
3364
3365 int slapi_x_backend_get_flags( const Slapi_Backend *be, unsigned long *flags )
3366 {
3367         if ( be == NULL )
3368                 return LDAP_PARAM_ERROR;
3369
3370         *flags = SLAP_DBFLAGS(be);
3371
3372         return LDAP_SUCCESS;
3373 }
3374
3375 int
3376 slapi_int_count_controls( LDAPControl **ctrls )
3377 {
3378         size_t i;
3379
3380         if ( ctrls == NULL )
3381                 return 0;
3382
3383         for ( i = 0; ctrls[i] != NULL; i++ )
3384                 ;
3385
3386         return i;
3387 }
3388
3389 int
3390 slapi_op_abandoned( Slapi_PBlock *pb )
3391 {
3392         if ( pb->pb_op == NULL )
3393                 return 0;
3394
3395         return ( pb->pb_op->o_abandon );
3396 }
3397
3398 char *
3399 slapi_op_type_to_string(unsigned long type)
3400 {
3401         char *str;
3402
3403         switch (type) {
3404         case SLAPI_OPERATION_BIND:
3405                 str = "bind";
3406                 break;
3407         case SLAPI_OPERATION_UNBIND:
3408                 str = "unbind";
3409                 break;
3410         case SLAPI_OPERATION_SEARCH:
3411                 str = "search";
3412                 break;
3413         case SLAPI_OPERATION_MODIFY:
3414                 str = "modify";
3415                 break;
3416         case SLAPI_OPERATION_ADD:
3417                 str = "add";
3418                 break;
3419         case SLAPI_OPERATION_DELETE:
3420                 str = "delete";
3421                 break;
3422         case SLAPI_OPERATION_MODDN:
3423                 str = "modrdn";
3424                 break;
3425         case SLAPI_OPERATION_COMPARE:
3426                 str = "compare";
3427                 break;
3428         case SLAPI_OPERATION_ABANDON:
3429                 str = "abandon";
3430                 break;
3431         case SLAPI_OPERATION_EXTENDED:
3432                 str = "extended";
3433                 break;
3434         default:
3435                 str = "unknown operation type";
3436                 break;
3437         }
3438         return str;
3439 }
3440
3441 unsigned long
3442 slapi_op_get_type(Slapi_Operation * op)
3443 {
3444         unsigned long type;
3445
3446         switch ( op->o_tag ) {
3447         case LDAP_REQ_BIND:
3448                 type = SLAPI_OPERATION_BIND;
3449                 break;
3450         case LDAP_REQ_UNBIND:
3451                 type = SLAPI_OPERATION_UNBIND;
3452                 break;
3453         case LDAP_REQ_SEARCH:
3454                 type = SLAPI_OPERATION_SEARCH;
3455                 break;
3456         case LDAP_REQ_MODIFY:
3457                 type = SLAPI_OPERATION_MODIFY;
3458                 break;
3459         case LDAP_REQ_ADD:
3460                 type = SLAPI_OPERATION_ADD;
3461                 break;
3462         case LDAP_REQ_DELETE:
3463                 type = SLAPI_OPERATION_DELETE;
3464                 break;
3465         case LDAP_REQ_MODRDN:
3466                 type = SLAPI_OPERATION_MODDN;
3467                 break;
3468         case LDAP_REQ_COMPARE:
3469                 type = SLAPI_OPERATION_COMPARE;
3470                 break;
3471         case LDAP_REQ_ABANDON:
3472                 type = SLAPI_OPERATION_ABANDON;
3473                 break;
3474         case LDAP_REQ_EXTENDED:
3475                 type = SLAPI_OPERATION_EXTENDED;
3476                 break;
3477         default:
3478                 type = SLAPI_OPERATION_NONE;
3479                 break;
3480         }
3481         return type;
3482 }
3483
3484 #if 0
3485 void
3486 slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag)
3487 {
3488 }
3489
3490 void
3491 slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag)
3492 {
3493 }
3494
3495 int
3496 slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag)
3497 {
3498 }
3499 #endif
3500
3501 #endif /* LDAP_SLAPI */
3502