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