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