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