entry.c 19.5 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* entry.c - routines for dealing with entries */
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 1998-2007 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* Portions Copyright (c) 1995 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
25
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
26

Kurt Zeilenga's avatar
Kurt Zeilenga committed
27
28
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
29
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
30
31

#include <ac/ctype.h>
32
#include <ac/errno.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
33
34
35
#include <ac/socket.h>
#include <ac/string.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
36
#include "slap.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
37
#include "ldif.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
38

Kurt Zeilenga's avatar
Kurt Zeilenga committed
39
40
static char		*ebuf;	/* buf returned by entry2str		 */
static char		*ecur;	/* pointer to end of currently used ebuf */
Gary Williams's avatar
Gary Williams committed
41
static int		emaxsize;/* max size of ebuf			 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
42

43
44
45
/*
 * Empty root entry
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
46
47
48
const Entry slap_entry_root = {
	NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
};
49

Kurt Zeilenga's avatar
Kurt Zeilenga committed
50
51
static const struct berval dn_bv = BER_BVC("dn");

52
53
int entry_destroy(void)
{
Howard Chu's avatar
Howard Chu committed
54
	if ( ebuf ) free( ebuf );
55
56
57
58
59
60
	ebuf = NULL;
	ecur = NULL;
	emaxsize = 0;
	return 0;
}

61

Kurt Zeilenga's avatar
Kurt Zeilenga committed
62
Entry *
63
str2entry( char *s )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
64
65
66
67
68
69
{
	return str2entry2( s, 1 );
}

Entry *
str2entry2( char *s, int checkvals )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
70
{
71
	int rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
72
	Entry		*e;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
73
74
	struct berval	*type, *vals, *nvals;
	char 	*freeval;
75
	AttributeDescription *ad, *ad_prev;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
76
	const char *text;
77
	char	*next;
78
	int		attr_cnt;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
79
80
	int		i, lines;
	Attribute	ahead, *atail;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
81
82

	/*
83
84
	 * LDIF is used as the string format.
	 * An entry looks like this:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
85
86
87
88
89
90
91
92
93
94
95
96
	 *
	 *	dn: <dn>\n
	 *	[<attr>:[:] <value>\n]
	 *	[<tab><continuedvalue>\n]*
	 *	...
	 *
	 * If a double colon is used after a type, it means the
	 * following value is encoded as a base 64 string.  This
	 * happens if the value contains a non-printing character
	 * or newline.
	 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
97
	Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
98
		s ? s : "NULL", 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99

Hallvard Furuseth's avatar
Hallvard Furuseth committed
100
	/* initialize reader/writer lock */
101
	e = (Entry *) ch_calloc( 1, sizeof(Entry) );
102
103

	if( e == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
		Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
105
106
			"<= str2entry NULL (entry allocation failed)\n",
			0, 0, 0 );
107
108
109
		return( NULL );
	}

110
	/* initialize entry */
111
	e->e_id = NOID;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
112

Kurt Zeilenga's avatar
Kurt Zeilenga committed
113
	/* dn + attributes */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
114
115
	atail = &ahead;
	ahead.a_next = NULL;
116
117
118
	ad = NULL;
	ad_prev = NULL;
	attr_cnt = 0;
119
	next = s;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
120
121
122
123
124
125
126
127
128

	lines = ldif_countlines( s );
	type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
	vals = type+lines+1;
	nvals = vals+lines+1;
	freeval = (char *)(nvals+lines+1);
	i = -1;

	/* parse into individual values, record DN */
129
	while ( (s = ldif_getline( &next )) != NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
130
		int freev;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
131
132
133
		if ( *s == '\n' || *s == '\0' ) {
			break;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
134
		i++;
135
136
137
138
139
		if (i >= lines) {
			Debug( LDAP_DEBUG_TRACE,
				"<= str2entry ran past end of entry\n", 0, 0, 0 );
			goto fail;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
140

Kurt Zeilenga's avatar
Kurt Zeilenga committed
141
142
143
		rc = ldif_parse_line2( s, type+i, vals+i, &freev );
		freeval[i] = freev;
		if ( rc ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
144
			Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
145
				"<= str2entry NULL (parse_line)\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
146
147
148
			continue;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
149
150
		if ( type[i].bv_len == dn_bv.bv_len &&
			strcasecmp( type[i].bv_val, dn_bv.bv_val ) == 0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
151

Kurt Zeilenga's avatar
Kurt Zeilenga committed
152
			if ( e->e_dn != NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
153
				Debug( LDAP_DEBUG_ANY, "str2entry: "
Kurt Zeilenga's avatar
Kurt Zeilenga committed
154
					"entry %ld has multiple DNs \"%s\" and \"%s\"\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
155
156
					(long) e->e_id, e->e_dn, vals[i].bv_val );
				goto fail;
157
158
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
159
			rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
160
161
162
			if( rc != LDAP_SUCCESS ) {
				Debug( LDAP_DEBUG_ANY, "str2entry: "
					"entry %ld has invalid DN \"%s\"\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
163
164
					(long) e->e_id, vals[i].bv_val, 0 );
				goto fail;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
165
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
166
167
168
			if ( freeval[i] ) free( vals[i].bv_val );
			vals[i].bv_val = NULL;
			i--;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
169
170
			continue;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
171
172
	}
	lines = i+1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
173

Kurt Zeilenga's avatar
Kurt Zeilenga committed
174
175
176
177
178
179
180
	/* check to make sure there was a dn: line */
	if ( BER_BVISNULL( &e->e_name )) {
		Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
			(long) e->e_id, 0, 0 );
		goto fail;
	}

Howard Chu's avatar
Howard Chu committed
181
182
#define bvcasematch(bv1, bv2)	( ((bv1)->bv_len == (bv2)->bv_len) && (strncasecmp((bv1)->bv_val, (bv2)->bv_val, (bv1)->bv_len) == 0) )

Kurt Zeilenga's avatar
Kurt Zeilenga committed
183
184
185
186
187
188
189
190
	/* Make sure all attributes with multiple values are contiguous */
	if ( checkvals ) {
		int j, k;
		struct berval bv;
		int fv;

		for (i=0; i<lines; i++) {
			for ( j=i+1; j<lines; j++ ) {
Howard Chu's avatar
Howard Chu committed
191
				if ( bvcasematch( type+i, type+j )) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
192
					/* out of order, move intervening attributes down */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
193
					if ( j != i+1 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
194
195
						bv = vals[j];
						fv = freeval[j];
Kurt Zeilenga's avatar
Kurt Zeilenga committed
196
197
198
199
						for ( k=j; k>i; k-- ) {
							type[k] = type[k-1];
							vals[k] = vals[k-1];
							freeval[k] = freeval[k-1];
Kurt Zeilenga's avatar
Kurt Zeilenga committed
200
						}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
201
202
203
204
						k++;
						type[k] = type[i];
						vals[k] = bv;
						freeval[k] = fv;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
205
					}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
206
					i++;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
207
				}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
208
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
209
210
211
212
213
		}
	}

	for ( i=0; i<=lines; i++ ) {
		ad_prev = ad;
Howard Chu's avatar
Howard Chu committed
214
		if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
215
216
			ad = NULL;
			rc = slap_bv2ad( type+i, &ad, &text );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
217

218
			if( rc != LDAP_SUCCESS ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
219
220
221
222
223
224
225
				Debug( slapMode & SLAP_TOOL_MODE
					? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
					"<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
				if( slapMode & SLAP_TOOL_MODE ) {
					goto fail;
				}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
226
				rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
227
228
				if( rc != LDAP_SUCCESS ) {
					Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
229
						"<= str2entry: slap_str2undef_ad(%s): %s\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
230
231
232
							type[i].bv_val, text, 0 );
					goto fail;
				}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
233
			}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
234
235
236
237
238
239
240
241
242
243

			/* require ';binary' when appropriate (ITS#5071) */
			if ( slap_syntax_is_binary( ad->ad_type->sat_syntax ) && !slap_ad_is_binary( ad ) ) {
				Debug( LDAP_DEBUG_ANY,
					"str2entry: attributeType %s #%d: "
					"needs ';binary' transfer as per syntax %s\n", 
					ad->ad_cname.bv_val, 0,
					ad->ad_type->sat_syntax->ssyn_oid );
				goto fail;
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
244
245
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
246
247
248
249
		if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
			int j, k;
			atail->a_next = (Attribute *) ch_malloc( sizeof(Attribute) );
			atail = atail->a_next;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
250
			atail->a_flags = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
			atail->a_desc = ad_prev;
			atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
			if( ad_prev->ad_type->sat_equality &&
				ad_prev->ad_type->sat_equality->smr_normalize )
				atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
			else
				atail->a_nvals = NULL;
			k = i - attr_cnt;
			for ( j=0; j<attr_cnt; j++ ) {
				if ( freeval[k] )
					atail->a_vals[j] = vals[k];
				else
					ber_dupbv( atail->a_vals+j, &vals[k] );
				vals[k].bv_val = NULL;
				if ( atail->a_nvals ) {
					atail->a_nvals[j] = nvals[k];
					nvals[k].bv_val = NULL;
				}
				k++;
			}
			BER_BVZERO( &atail->a_vals[j] );
			if ( atail->a_nvals ) {
				BER_BVZERO( &atail->a_nvals[j] );
			} else {
				atail->a_nvals = atail->a_vals;
			}
277
			attr_cnt = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
278
			if ( i == lines ) break;
279
280
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
281
		if( slapMode & SLAP_TOOL_MODE ) {
282
			struct berval pval;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
283
284
			slap_syntax_validate_func *validate =
				ad->ad_type->sat_syntax->ssyn_validate;
285
286
			slap_syntax_transform_func *pretty =
				ad->ad_type->sat_syntax->ssyn_pretty;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
287

Kurt Zeilenga's avatar
Kurt Zeilenga committed
288
289
290
291
292
			if ( pretty ) {
#ifdef SLAP_ORDERED_PRETTYNORM
				rc = ordered_value_pretty( ad,
					&vals[i], &pval, NULL );
#else /* ! SLAP_ORDERED_PRETTYNORM */
293
				rc = pretty( ad->ad_type->sat_syntax,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
294
					&vals[i], &pval, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
295
#endif /* ! SLAP_ORDERED_PRETTYNORM */
296

Kurt Zeilenga's avatar
Kurt Zeilenga committed
297
			} else if ( validate ) {
298
299
300
				/*
			 	 * validate value per syntax
			 	 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
301
#ifdef SLAP_ORDERED_PRETTYNORM
302
				rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
303
#else /* ! SLAP_ORDERED_PRETTYNORM */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
304
				rc = validate( ad->ad_type->sat_syntax, &vals[i] );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
305
#endif /* ! SLAP_ORDERED_PRETTYNORM */
306
307

			} else {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
308
				Debug( LDAP_DEBUG_ANY,
309
310
311
312
					"str2entry: attributeType %s #%d: "
					"no validator for syntax %s\n", 
					ad->ad_cname.bv_val, attr_cnt,
					ad->ad_type->sat_syntax->ssyn_oid );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
313
				goto fail;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
314
315
316
			}

			if( rc != 0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
317
				Debug( LDAP_DEBUG_ANY,
318
319
320
321
					"str2entry: invalid value "
					"for attributeType %s #%d (syntax %s)\n",
					ad->ad_cname.bv_val, attr_cnt,
					ad->ad_type->sat_syntax->ssyn_oid );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
322
				goto fail;
323
			}
324
325

			if( pretty ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
326
327
328
				if ( freeval[i] ) free( vals[i].bv_val );
				vals[i] = pval;
				freeval[i] = 1;
329
			}
330
331
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
332
		if ( ad->ad_type->sat_equality &&
Kurt Zeilenga's avatar
Kurt Zeilenga committed
333
			ad->ad_type->sat_equality->smr_normalize )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
334
		{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
335
336
337
338
339
340
341
#ifdef SLAP_ORDERED_PRETTYNORM
			rc = ordered_value_normalize(
				SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
				ad,
				ad->ad_type->sat_equality,
				&vals[i], &nvals[i], NULL );
#else /* ! SLAP_ORDERED_PRETTYNORM */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
342
343
			rc = ad->ad_type->sat_equality->smr_normalize(
				SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
344
				ad->ad_type->sat_syntax,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
345
				ad->ad_type->sat_equality,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
346
				&vals[i], &nvals[i], NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
347
#endif /* ! SLAP_ORDERED_PRETTYNORM */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
348

Kurt Zeilenga's avatar
Kurt Zeilenga committed
349
			if ( rc ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
350
				Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
351
			   		"<= str2entry NULL (smr_normalize %d)\n", rc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
352
				goto fail;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
353
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
354
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
355

356
		attr_cnt++;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
357
358
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
359
360
361
	free( type );
	atail->a_next = NULL;
	e->e_attrs = ahead.a_next;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
362

Kurt Zeilenga's avatar
Kurt Zeilenga committed
363
364
	Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
		e->e_dn, (unsigned long) e, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
365
	return( e );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
366
367
368
369
370
371
372
373
374

fail:
	for ( i=0; i<lines; i++ ) {
		if ( freeval[i] ) free( vals[i].bv_val );
		free( nvals[i].bv_val );
	}
	free( type );
	entry_free( e );
	return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
375
376
}

377

Kurt Zeilenga's avatar
Kurt Zeilenga committed
378
379
380
381
#define GRABSIZE	BUFSIZ

#define MAKE_SPACE( n )	{ \
		while ( ecur + (n) > ebuf + emaxsize ) { \
382
			ptrdiff_t	offset; \
Kurt Zeilenga's avatar
Kurt Zeilenga committed
383
			offset = (int) (ecur - ebuf); \
Kurt Zeilenga's avatar
Kurt Zeilenga committed
384
			ebuf = ch_realloc( ebuf, \
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
385
				emaxsize + GRABSIZE ); \
Kurt Zeilenga's avatar
Kurt Zeilenga committed
386
387
388
			emaxsize += GRABSIZE; \
			ecur = ebuf + offset; \
		} \
389
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
390
391
392

char *
entry2str(
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
393
394
	Entry	*e,
	int		*len )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
395
396
397
{
	Attribute	*a;
	struct berval	*bv;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
398
399
	int		i;
	ber_len_t tmplen;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
400

401
402
	assert( e != NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
403
404
405
406
407
408
409
410
411
412
413
	/*
	 * In string format, an entry looks like this:
	 *	dn: <dn>\n
	 *	[<attr>: <value>\n]*
	 */

	ecur = ebuf;

	/* put the dn */
	if ( e->e_dn != NULL ) {
		/* put "dn: <dn>" */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
414
		tmplen = e->e_name.bv_len;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
415
		MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
416
		ldif_sput( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
417
418
419
420
421
	}

	/* put the attributes */
	for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
		/* put "<type>:[:] <value>" line for each value */
422
423
		for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
			bv = &a->a_vals[i];
424
			tmplen = a->a_desc->ad_cname.bv_len;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
425
			MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
426
			ldif_sput( &ecur, LDIF_PUT_VALUE,
427
				a->a_desc->ad_cname.bv_val,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
428
				bv->bv_val, bv->bv_len );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
429
430
431
432
433
434
		}
	}
	MAKE_SPACE( 1 );
	*ecur = '\0';
	*len = ecur - ebuf;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
435
	return( ebuf );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
436
437
438
}

void
439
entry_clean( Entry *e )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
440
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
441
442
	/* free an entry structure */
	assert( e != NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
443

Kurt Zeilenga's avatar
Kurt Zeilenga committed
444
445
446
447
448
	/* e_private must be freed by the caller */
	assert( e->e_private == NULL );
	e->e_private = NULL;

	/* free DNs */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
449
450
451
	if ( !BER_BVISNULL( &e->e_name ) ) {
		free( e->e_name.bv_val );
		BER_BVZERO( &e->e_name );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
452
	}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
453
454
455
	if ( !BER_BVISNULL( &e->e_nname ) ) {
		free( e->e_nname.bv_val );
		BER_BVZERO( &e->e_nname );
456
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
457

Pierangelo Masarati's avatar
Pierangelo Masarati committed
458
	if ( !BER_BVISNULL( &e->e_bv ) ) {
459
		free( e->e_bv.bv_val );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
460
		BER_BVZERO( &e->e_bv );
461
462
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
463
464
	/* free attributes */
	attrs_free( e->e_attrs );
465
	e->e_attrs = NULL;
466
467
468
469
470
471
}

void
entry_free( Entry *e )
{
	entry_clean( e );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
472

Kurt Zeilenga's avatar
Kurt Zeilenga committed
473
474
	free( e );
}
475

476
477
478
479
480
/*
 * These routines are used only by Backend.
 *
 * the Entry has three entry points (ways to find things):
 *
Gary Williams's avatar
Gary Williams committed
481
 *	by entry	e.g., if you already have an entry from the cache
482
483
484
485
486
487
488
489
490
491
 *			and want to delete it. (really by entry ptr)
 *	by dn		e.g., when looking for the base object of a search
 *	by id		e.g., for search candidates
 *
 * these correspond to three different avl trees that are maintained.
 */

int
entry_cmp( Entry *e1, Entry *e2 )
{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
492
	return SLAP_PTRCMP( e1, e2 );
493
494
495
}

int
496
entry_dn_cmp( const void *v_e1, const void *v_e2 )
497
498
{
	/* compare their normalized UPPERCASED dn's */
499
	const Entry *e1 = v_e1, *e2 = v_e2;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
500
501

	return ber_bvcmp( &e1->e_nname, &e2->e_nname );
502
503
504
}

int
505
entry_id_cmp( const void *v_e1, const void *v_e2 )
506
{
507
	const Entry *e1 = v_e1, *e2 = v_e2;
508
509
510
	return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
}

511
/* This is like a ber_len */
512
513
#define entry_lenlen(l)	(((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
	((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
514
515
516
517
518
519
520
521
522
523

static void
entry_putlen(unsigned char **buf, ber_len_t len)
{
	ber_len_t lenlen = entry_lenlen(len);

	if (lenlen == 1) {
		**buf = (unsigned char) len;
	} else {
		int i;
524
		**buf = 0x80 | ((unsigned char) lenlen - 1);
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
		for (i=lenlen-1; i>0; i--) {
			(*buf)[i] = (unsigned char) len;
			len >>= 8;
		}
	}
	*buf += lenlen;
}

static ber_len_t
entry_getlen(unsigned char **buf)
{
	ber_len_t len;
	int i;

	len = *(*buf)++;
	if (len <= 0x7f)
		return len;
	i = len & 0x7f;
	len = 0;
	for (;i > 0; i--) {
		len <<= 8;
		len |= *(*buf)++;
	}
	return len;
}

551
552
553
/* Count up the sizes of the components of an entry */
void entry_partsize(Entry *e, ber_len_t *plen,
	int *pnattrs, int *pnvals, int norm)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
554
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
555
	ber_len_t len, dnlen, ndnlen;
556
	int i, nat = 0, nval = 0;
557
	Attribute *a;
558

Kurt Zeilenga's avatar
Kurt Zeilenga committed
559
	dnlen = e->e_name.bv_len;
Howard Chu's avatar
Howard Chu committed
560
	len = dnlen + 1;	/* trailing NUL byte */
561
	len += entry_lenlen(dnlen);
Howard Chu's avatar
Howard Chu committed
562
563
564
565
566
	if (norm) {
		ndnlen = e->e_nname.bv_len;
		len += ndnlen + 1;
		len += entry_lenlen(ndnlen);
	}
567
568
	for (a=e->e_attrs; a; a=a->a_next) {
		/* For AttributeDesc, we only store the attr name */
569
		nat++;
570
		len += a->a_desc->ad_cname.bv_len+1;
571
		len += entry_lenlen(a->a_desc->ad_cname.bv_len);
572
		for (i=0; a->a_vals[i].bv_val; i++) {
573
			nval++;
574
575
			len += a->a_vals[i].bv_len + 1;
			len += entry_lenlen(a->a_vals[i].bv_len);
576
		}
577
		len += entry_lenlen(i);
578
		nval++;	/* empty berval at end */
Howard Chu's avatar
Howard Chu committed
579
		if (norm && a->a_nvals != a->a_vals) {
580
			for (i=0; a->a_nvals[i].bv_val; i++) {
581
				nval++;
582
583
584
585
				len += a->a_nvals[i].bv_len + 1;
				len += entry_lenlen(a->a_nvals[i].bv_len);
			}
			len += entry_lenlen(i);	/* i nvals */
586
			nval++;
587
588
589
		} else {
			len += entry_lenlen(0);	/* 0 nvals */
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
590
	}
591
592
	len += entry_lenlen(nat);
	len += entry_lenlen(nval);
Howard Chu's avatar
Howard Chu committed
593
	*plen = len;
594
595
596
597
598
599
600
601
602
603
604
605
606
607
	*pnattrs = nat;
	*pnvals = nval;
}

/* Add up the size of the entry for a flattened buffer */
ber_len_t entry_flatsize(Entry *e, int norm)
{
	ber_len_t len;
	int nattrs, nvals;

	entry_partsize(e, &len, &nattrs, &nvals, norm);
	len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
		(nvals * sizeof(struct berval));
	return len;
Howard Chu's avatar
Howard Chu committed
608
609
610
611
612
613
614
615
616
617
618
619
}

/* Flatten an Entry into a buffer. The buffer is filled with just the
 * strings/bervals of all the entry components. Each field is preceded
 * by its length, encoded the way ber_put_len works. Every field is NUL
 * terminated.  The entire buffer size is precomputed so that a single
 * malloc can be performed. The entry size is also recorded,
 * to aid in entry_decode.
 */
int entry_encode(Entry *e, struct berval *bv)
{
	ber_len_t len, dnlen, ndnlen;
620
	int i, nattrs, nvals;
Howard Chu's avatar
Howard Chu committed
621
622
623
624
625
626
627
628
	Attribute *a;
	unsigned char *ptr;

	Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
		(long) e->e_id, e->e_dn, 0 );
	dnlen = e->e_name.bv_len;
	ndnlen = e->e_nname.bv_len;

629
	entry_partsize( e, &len, &nattrs, &nvals, 1 );
Howard Chu's avatar
Howard Chu committed
630

631
632
633
	bv->bv_len = len;
	bv->bv_val = ch_malloc(len);
	ptr = (unsigned char *)bv->bv_val;
634
635
	entry_putlen(&ptr, nattrs);
	entry_putlen(&ptr, nvals);
636
	entry_putlen(&ptr, dnlen);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
637
	AC_MEMCPY(ptr, e->e_dn, dnlen);
638
639
	ptr += dnlen;
	*ptr++ = '\0';
640
	entry_putlen(&ptr, ndnlen);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
641
	AC_MEMCPY(ptr, e->e_ndn, ndnlen);
642
	ptr += ndnlen;
643
	*ptr++ = '\0';
644

645
	for (a=e->e_attrs; a; a=a->a_next) {
646
		entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
647
		AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
648
649
650
651
			a->a_desc->ad_cname.bv_len);
		ptr += a->a_desc->ad_cname.bv_len;
		*ptr++ = '\0';
		if (a->a_vals) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
652
653
			for (i=0; a->a_vals[i].bv_val; i++);
				entry_putlen(&ptr, i);
654
655
656
657
658
				for (i=0; a->a_vals[i].bv_val; i++) {
				entry_putlen(&ptr, a->a_vals[i].bv_len);
				AC_MEMCPY(ptr, a->a_vals[i].bv_val,
					a->a_vals[i].bv_len);
				ptr += a->a_vals[i].bv_len;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
659
660
				*ptr++ = '\0';
			}
661
662
663
664
665
666
667
668
669
			if (a->a_nvals != a->a_vals) {
				entry_putlen(&ptr, i);
				for (i=0; a->a_nvals[i].bv_val; i++) {
					entry_putlen(&ptr, a->a_nvals[i].bv_len);
					AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
					a->a_nvals[i].bv_len);
					ptr += a->a_nvals[i].bv_len;
					*ptr++ = '\0';
				}
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
670
671
			} else {
				entry_putlen(&ptr, 0);
672
			}
673
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
674
	}
675
676
	return 0;
}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
677

678
/* Retrieve an Entry that was stored using entry_encode above.
679
 * We malloc a single block with the size stored above for the Entry
680
 * and all of its Attributes. We also must lookup the stored
681
682
683
 * attribute names to get AttributeDescriptions. To detect if the
 * attributes of an Entry are later modified, we note that e->e_attr
 * is always a constant offset from (e).
684
685
686
687
688
 *
 * Note: everything is stored in a single contiguous block, so
 * you can not free individual attributes or names from this
 * structure. Attempting to do so will likely corrupt memory.
 */
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
689
690
691
#ifdef SLAP_ZONE_ALLOC
int entry_decode(struct berval *bv, Entry **e, void *ctx)
#else
692
int entry_decode(struct berval *bv, Entry **e)
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
693
#endif
694
{
695
	int i, j, count, nattrs, nvals;
696
	int rc;
697
	Attribute *a;
698
	Entry *x;
699
700
	const char *text;
	AttributeDescription *ad;
701
	unsigned char *ptr = (unsigned char *)bv->bv_val;
702
	BerVarray bptr;
703

704
705
706
707
708
709
710
711
	nattrs = entry_getlen(&ptr);
	if (!nattrs) {
		Debug( LDAP_DEBUG_ANY,
			"entry_decode: attribute count was zero\n", 0, 0, 0);
		return LDAP_OTHER;
	}
	nvals = entry_getlen(&ptr);
	if (!nvals) {
712
		Debug( LDAP_DEBUG_ANY,
713
			"entry_decode: value count was zero\n", 0, 0, 0);
714
715
		return LDAP_OTHER;
	}
716
717
	i = sizeof(Entry) + (nattrs * sizeof(Attribute)) +
		(nvals * sizeof(struct berval));
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
718
719
720
721
722
#ifdef SLAP_ZONE_ALLOC
	x = slap_zn_calloc(1, i + bv->bv_len, ctx);
	AC_MEMCPY((char*)x + i, bv->bv_val, bv->bv_len);
	bv->bv_val = (char*)x + i;
	ptr = (unsigned char *)bv->bv_val;
723
724
725
	/* pointer is reset, now advance past nattrs and nvals again */
	entry_getlen(&ptr);
	entry_getlen(&ptr);
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
726
#else
727
	x = ch_calloc(1, i);
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
728
#endif
729
	i = entry_getlen(&ptr);
730
	x->e_name.bv_val = (char *) ptr;
731
	x->e_name.bv_len = i;
732
733
	ptr += i+1;
	i = entry_getlen(&ptr);
734
	x->e_nname.bv_val = (char *) ptr;
735
	x->e_nname.bv_len = i;
736
	ptr += i+1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
737
	Debug( LDAP_DEBUG_TRACE,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
738
739
		"entry_decode: \"%s\"\n",
		x->e_dn, 0, 0 );
740
	x->e_bv = *bv;
741
742
743
744
745

	/* A valid entry must have at least one attr, so this
	 * pointer can never be NULL
	 */
	x->e_attrs = (Attribute *)(x+1);
746
	bptr = (BerVarray)x->e_attrs;
747
748
	a = NULL;

749
	while ((i = entry_getlen(&ptr))) {
750
751
		struct berval bv;
		bv.bv_len = i;
752
		bv.bv_val = (char *) ptr;
753
754
755
756
		if (a) {
			a->a_next = (Attribute *)bptr;
		}
		a = (Attribute *)bptr;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
757
		ad = NULL;
758
		rc = slap_bv2ad( &bv, &ad, &text );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
759

760
		if( rc != LDAP_SUCCESS ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
761
			Debug( LDAP_DEBUG_TRACE,
762
				"<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
763
			rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
764

765
			if( rc != LDAP_SUCCESS ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
766
				Debug( LDAP_DEBUG_ANY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
767
					"<= entry_decode: slap_str2undef_ad(%s): %s\n",
768
769
						ptr, text, 0 );
				return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
770
771
			}
		}
772
		ptr += i + 1;
773
		a->a_desc = ad;
774
		bptr = (BerVarray)(a+1);
775
		a->a_vals = bptr;
776
		a->a_flags = 0;
777
#ifdef LDAP_COMP_MATCH
778
		a->a_comp_data = NULL;
779
#endif
780
		count = j = entry_getlen(&ptr);
781
782
783

		while (j) {
			i = entry_getlen(&ptr);
784
785
			bptr->bv_len = i;
			bptr->bv_val = (char *)ptr;
786
787
788
			ptr += i+1;
			bptr++;
			j--;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
789
		}
Howard Chu's avatar
Howard Chu committed
790
791
792
		bptr->bv_val = NULL;
		bptr->bv_len = 0;
		bptr++;
793

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
		j = entry_getlen(&ptr);
		if (j) {
			a->a_nvals = bptr;
			while (j) {
				i = entry_getlen(&ptr);
				bptr->bv_len = i;
				bptr->bv_val = (char *)ptr;
				ptr += i+1;
				bptr++;
				j--;
			}
			bptr->bv_val = NULL;
			bptr->bv_len = 0;
			bptr++;
		} else {
809
			a->a_nvals = a->a_vals;
810
		}
811
812
813
		nattrs--;
		if ( !nattrs )
			break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
814
	}
815
816

	if (a) a->a_next = NULL;
817
	Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
818
819
820
		x->e_dn, 0, 0 );
	*e = x;
	return 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
821
}
822
823
824
825
826
827
828
829
830
831
832
833

Entry *entry_dup( Entry *e )
{
	Entry *ret;

	ret = (Entry *)ch_calloc( 1, sizeof(*ret) );

	ret->e_id = e->e_id;
	ber_dupbv( &ret->e_name, &e->e_name );
	ber_dupbv( &ret->e_nname, &e->e_nname );
	ret->e_attrs = attrs_dup( e->e_attrs );
	ret->e_ocflags = e->e_ocflags;
834
835
	ret->e_bv.bv_val = NULL;
	ret->e_bv.bv_len = 0;
836
	ret->e_private = NULL;
837
838

	return ret;
839
840
}