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