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