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