io.c 15.8 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* io.c - ber general i/o routines */
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-2010 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
 * 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>.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
16
/* Portions Copyright (c) 1990 Regents of the University of Michigan.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
17
18
19
20
21
22
23
24
25
 * 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
26
27
28
29
/* ACKNOWLEDGEMENTS:
 * This work was originally developed by the University of Michigan
 * (as part of U-MICH LDAP).
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
30

Kurt Zeilenga's avatar
Kurt Zeilenga committed
31
32
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
33
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
34
35

#include <ac/stdlib.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
36

Kurt Zeilenga's avatar
Kurt Zeilenga committed
37
38
39
40
41
#include <ac/ctype.h>
#include <ac/errno.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
42

Kurt Zeilenga's avatar
Kurt Zeilenga committed
43
44
#ifdef HAVE_IO_H
#include <io.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
45
46
#endif

47
#include "lber-int.h"
Julius Enarusai's avatar
   
Julius Enarusai committed
48
#include "ldap_log.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
ber_slen_t
ber_skip_data(
	BerElement *ber,
	ber_len_t len )
{
	ber_len_t	actuallen, nleft;

	assert( ber != NULL );
	assert( LBER_VALID( ber ) );

	nleft = ber_pvt_ber_remaining( ber );
	actuallen = nleft < len ? nleft : len;
	ber->ber_ptr += actuallen;
	ber->ber_tag = *(unsigned char *)ber->ber_ptr;

	return( (ber_slen_t) actuallen );
}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
68
69
70
71
/*
 * Read from the ber buffer.  The caller must maintain ber->ber_tag.
 * Do not use to read whole tags.  See ber_get_tag() and ber_skip_data().
 */
72
73
74
75
76
ber_slen_t
ber_read(
	BerElement *ber,
	char *buf,
	ber_len_t len )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
77
{
78
	ber_len_t	actuallen, nleft;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
79

Kurt Zeilenga's avatar
Kurt Zeilenga committed
80
81
	assert( ber != NULL );
	assert( buf != NULL );
82
	assert( LBER_VALID( ber ) );
83

Kurt Zeilenga's avatar
Kurt Zeilenga committed
84
	nleft = ber_pvt_ber_remaining( ber );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
85
86
	actuallen = nleft < len ? nleft : len;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
87
	AC_MEMCPY( buf, ber->ber_ptr, actuallen );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
88
89
90

	ber->ber_ptr += actuallen;

91
	return( (ber_slen_t) actuallen );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
92
93
}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
94
95
96
97
/*
 * Write to the ber buffer.
 * Note that ber_start_seqorset/ber_put_seqorset() bypass ber_write().
 */
98
ber_slen_t
Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
100
101
ber_write(
	BerElement *ber,
	LDAP_CONST char *buf,
102
	ber_len_t len,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
103
	int zero )	/* nonzero is unsupported from OpenLDAP 2.4.18 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
104
{
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
105
106
	char **p;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
107
108
	assert( ber != NULL );
	assert( buf != NULL );
109
	assert( LBER_VALID( ber ) );
110

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
111
112
113
114
115
	if ( zero != 0 ) {
		ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug, "%s",
			"ber_write: nonzero 4th argument not supported\n" );
		return( -1 );
	}
116

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
117
118
119
	p = ber->ber_sos_ptr == NULL ? &ber->ber_ptr : &ber->ber_sos_ptr;
	if ( len > (ber_len_t) (ber->ber_end - *p) ) {
		if ( ber_realloc( ber, len ) != 0 ) return( -1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
120
	}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
121
122
123
124
	AC_MEMCPY( *p, buf, len );
	*p += len;

	return( (ber_slen_t) len );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
125
126
}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
127
/* Resize the ber buffer */
128
int
129
ber_realloc( BerElement *ber, ber_len_t len )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
130
{
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
131
132
	ber_len_t	total, offset, sos_offset;
	char		*buf;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
133

Kurt Zeilenga's avatar
Kurt Zeilenga committed
134
	assert( ber != NULL );
135
	assert( LBER_VALID( ber ) );
136

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
137
138
139
140
141
	/* leave room for ber_flatten() to \0-terminate ber_buf */
	if ( ++len == 0 ) {
		return( -1 );
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
142
	total = ber_pvt_ber_total( ber );
143

Kurt Zeilenga's avatar
Kurt Zeilenga committed
144
#define LBER_EXBUFSIZ	4060 /* a few words less than 2^N for binary buddy */
145
146
147
148
149
#if defined( LBER_EXBUFSIZ ) && LBER_EXBUFSIZ > 0
# ifndef notdef
	/* don't realloc by small amounts */
	total += len < LBER_EXBUFSIZ ? LBER_EXBUFSIZ : len;
# else
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
150
	{	/* not sure what value this adds.  reduce fragmentation? */
151
152
153
154
155
156
157
158
		ber_len_t have = (total + (LBER_EXBUFSIZE - 1)) / LBER_EXBUFSIZ;
		ber_len_t need = (len + (LBER_EXBUFSIZ - 1)) / LBER_EXBUFSIZ;
		total = ( have + need ) * LBER_EXBUFSIZ;
	}
# endif
#else
	total += len;	/* realloc just what's needed */
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
159

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
160
	if ( total < len || total > (ber_len_t)-1 / 2 /* max ber_slen_t */ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
161
		return( -1 );
162
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
163

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
164
165
166
167
	buf = ber->ber_buf;
	offset = ber->ber_ptr - buf;
	sos_offset = ber->ber_sos_ptr ? ber->ber_sos_ptr - buf : 0;
	/* if ber_sos_ptr != NULL, it is > ber_buf so that sos_offset > 0 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
168

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
169
170
171
	buf = (char *) ber_memrealloc_x( buf, total, ber->ber_memctx );
	if ( buf == NULL ) {
		return( -1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
172
173
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
174
175
176
177
178
179
	ber->ber_buf = buf;
	ber->ber_end = buf + total;
	ber->ber_ptr = buf + offset;
	if ( sos_offset )
		ber->ber_sos_ptr = buf + sos_offset;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
180
181
182
183
	return( 0 );
}

void
Howard Chu's avatar
Howard Chu committed
184
ber_free_buf( BerElement *ber )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
185
{
186
	assert( LBER_VALID( ber ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
187

Howard Chu's avatar
Howard Chu committed
188
	if ( ber->ber_buf) ber_memfree_x( ber->ber_buf, ber->ber_memctx );
189

Kurt Zeilenga's avatar
Kurt Zeilenga committed
190
	ber->ber_buf = NULL;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
191
	ber->ber_sos_ptr = NULL;
192
	ber->ber_valid = LBER_UNINITIALIZED;
Howard Chu's avatar
Howard Chu committed
193
194
195
196
197
198
}

void
ber_free( BerElement *ber, int freebuf )
{
	if( ber == NULL ) {
199
		LDAP_MEMORY_DEBUG_ASSERT( ber != NULL );
Howard Chu's avatar
Howard Chu committed
200
201
202
		return;
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
203
	if( freebuf ) ber_free_buf( ber );
204

Howard Chu's avatar
Howard Chu committed
205
	ber_memfree_x( (char *) ber, ber->ber_memctx );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
206
207
208
209
}

int
ber_flush( Sockbuf *sb, BerElement *ber, int freeit )
Pierangelo Masarati's avatar
Pierangelo Masarati committed
210
211
212
213
214
215
216
217
{
	return ber_flush2( sb, ber,
		freeit ? LBER_FLUSH_FREE_ON_SUCCESS
			: LBER_FLUSH_FREE_NEVER );
}

int
ber_flush2( Sockbuf *sb, BerElement *ber, int freeit )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
218
{
219
	ber_len_t	towrite;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
220
	ber_slen_t	rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
221

Kurt Zeilenga's avatar
Kurt Zeilenga committed
222
223
	assert( sb != NULL );
	assert( ber != NULL );
224
	assert( SOCKBUF_VALID( sb ) );
225
	assert( LBER_VALID( ber ) );
226

Kurt Zeilenga's avatar
Kurt Zeilenga committed
227
228
229
230
231
	if ( ber->ber_rwptr == NULL ) {
		ber->ber_rwptr = ber->ber_buf;
	}
	towrite = ber->ber_ptr - ber->ber_rwptr;

232
	if ( sb->sb_debug ) {
233
		ber_log_printf( LDAP_DEBUG_TRACE, sb->sb_debug,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
234
			"ber_flush2: %ld bytes to sd %ld%s\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
235
236
			towrite, (long) sb->sb_fd,
			ber->ber_rwptr != ber->ber_buf ?  " (re-flush)" : "" );
237
		ber_log_bprint( LDAP_DEBUG_BER, sb->sb_debug,
238
			ber->ber_rwptr, towrite );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
239
	}
240

241
	while ( towrite > 0 ) {
242
243
244
245
#ifdef LBER_TRICKLE
		sleep(1);
		rc = ber_int_sb_write( sb, ber->ber_rwptr, 1 );
#else
246
		rc = ber_int_sb_write( sb, ber->ber_rwptr, towrite );
247
#endif
Pierangelo Masarati's avatar
Pierangelo Masarati committed
248
		if ( rc <= 0 ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
249
250
			if ( freeit & LBER_FLUSH_FREE_ON_ERROR ) ber_free( ber, 1 );
			return -1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
251
252
253
		}
		towrite -= rc;
		ber->ber_rwptr += rc;
254
	} 
Kurt Zeilenga's avatar
Kurt Zeilenga committed
255

Pierangelo Masarati's avatar
Pierangelo Masarati committed
256
	if ( freeit & LBER_FLUSH_FREE_ON_SUCCESS ) ber_free( ber, 1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
257

Pierangelo Masarati's avatar
Pierangelo Masarati committed
258
	return 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
259
260
261
262
263
264
265
}

BerElement *
ber_alloc_t( int options )
{
	BerElement	*ber;

266
	ber = (BerElement *) LBER_CALLOC( 1, sizeof(BerElement) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
267

Kurt Zeilenga's avatar
Kurt Zeilenga committed
268
269
270
	if ( ber == NULL ) {
		return NULL;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
271

272
	ber->ber_valid = LBER_VALID_BERELEMENT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
273
	ber->ber_tag = LBER_DEFAULT;
274
	ber->ber_options = options;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
275
	ber->ber_debug = ber_int_debug;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
276

277
	assert( LBER_VALID( ber ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
278
	return ber;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
279
280
281
}

BerElement *
282
ber_alloc( void )	/* deprecated */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
283
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
284
	return ber_alloc_t( 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
285
286
287
}

BerElement *
288
der_alloc( void )	/* deprecated */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
289
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
290
	return ber_alloc_t( LBER_USE_DER );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
291
292
293
}

BerElement *
294
ber_dup( BerElement *ber )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
295
296
297
{
	BerElement	*new;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
298
	assert( ber != NULL );
299
	assert( LBER_VALID( ber ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
300

301
	if ( (new = ber_alloc_t( ber->ber_options )) == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
302
		return NULL;
303
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
304
305
306

	*new = *ber;

307
	assert( LBER_VALID( new ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
308
309
310
311
312
	return( new );
}


void
313
ber_init2( BerElement *ber, struct berval *bv, int options )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
314
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
315
316
	assert( ber != NULL );

317
	(void) memset( (char *)ber, '\0', sizeof( BerElement ));
318
	ber->ber_valid = LBER_VALID_BERELEMENT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
319
	ber->ber_tag = LBER_DEFAULT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
320
	ber->ber_options = (char) options;
321
	ber->ber_debug = ber_int_debug;
322

323
324
325
326
327
328
	if ( bv != NULL ) {
		ber->ber_buf = bv->bv_val;
		ber->ber_ptr = ber->ber_buf;
		ber->ber_end = ber->ber_buf + bv->bv_len;
	}

329
	assert( LBER_VALID( ber ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
330
331
}

332
333
334
335
336
337
338
/* OLD U-Mich ber_init() */
void
ber_init_w_nullc( BerElement *ber, int options )
{
	ber_init2( ber, NULL, options );
}

339
340
341
342
/* New C-API ber_init() */
/* This function constructs a BerElement containing a copy
** of the data in the bv argument.
*/
343
344
345
BerElement *
ber_init( struct berval *bv )
{
346
347
	BerElement *ber;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
348
349
	assert( bv != NULL );

350
351
352
353
354
355
	if ( bv == NULL ) {
		return NULL;
	}

	ber = ber_alloc_t( 0 );

356
	if( ber == NULL ) {
357
		/* allocation failed */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
358
		return NULL;
359
360
361
	}

	/* copy the data */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
362
363
364
	if ( ((ber_len_t) ber_write ( ber, bv->bv_val, bv->bv_len, 0 ))
		!= bv->bv_len )
	{
365
366
		/* write failed, so free and return NULL */
		ber_free( ber, 1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
367
		return NULL;
368
369
370
	}

	ber_reset( ber, 1 );	/* reset the pointer to the start of the buffer */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
371
	return ber;
372
373
}

374
375
/* New C-API ber_flatten routine */
/* This routine allocates a struct berval whose contents are a BER
376
** encoding taken from the ber argument.  The bvPtr pointer points to
377
** the returned berval.
378
379
380
**
** ber_flatten2 is the same, but uses a struct berval passed by
** the caller. If alloc is 0 the returned bv uses the ber buf directly.
381
*/
382
int ber_flatten2(
383
	BerElement *ber,
384
385
	struct berval *bv,
	int alloc )
386
{
387
	assert( bv != NULL );
388

Kurt Zeilenga's avatar
Kurt Zeilenga committed
389
390
	if ( bv == NULL ) {
		return -1;
391
392
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
393
394
395
396
397
	/* unmatched "{" and "}" */
	if ( ber->ber_sos_ptr != NULL ) {
		return -1;
	}

398
399
400
401
402
403
404
	if ( ber == NULL ) {
		/* ber is null, create an empty berval */
		bv->bv_val = NULL;
		bv->bv_len = 0;

	} else {
		/* copy the berval */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
405
		ber_len_t len = ber_pvt_ber_write( ber );
406

407
		if ( alloc ) {
408
			bv->bv_val = (char *) ber_memalloc_x( len + 1, ber->ber_memctx );
409
410
411
412
			if ( bv->bv_val == NULL ) {
				return -1;
			}
			AC_MEMCPY( bv->bv_val, ber->ber_buf, len );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
413
414
			bv->bv_val[len] = '\0';
		} else if ( ber->ber_buf != NULL ) {
415
			bv->bv_val = ber->ber_buf;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
416
417
418
			bv->bv_val[len] = '\0';
		} else {
			bv->bv_val = "";
419
420
421
		}
		bv->bv_len = len;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
422
	return 0;
423
}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
424

425
426
427
428
429
430
431
432
433
434
435
436
437
int ber_flatten(
	BerElement *ber,
	struct berval **bvPtr)
{
	struct berval *bv;
	int rc;
 
	assert( bvPtr != NULL );

	if(bvPtr == NULL) {
		return -1;
	}

438
	bv = ber_memalloc_x( sizeof(struct berval), ber->ber_memctx );
439
440
441
442
443
	if ( bv == NULL ) {
		return -1;
	}
	rc = ber_flatten2(ber, bv, 1);
	if (rc == -1) {
444
		ber_memfree_x(bv, ber->ber_memctx);
445
446
447
448
449
450
	} else {
		*bvPtr = bv;
	}
	return rc;
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
451
452
453
void
ber_reset( BerElement *ber, int was_writing )
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
454
	assert( ber != NULL );
455
	assert( LBER_VALID( ber ) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
456

Kurt Zeilenga's avatar
Kurt Zeilenga committed
457
458
459
	if ( was_writing ) {
		ber->ber_end = ber->ber_ptr;
		ber->ber_ptr = ber->ber_buf;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
460

Kurt Zeilenga's avatar
Kurt Zeilenga committed
461
462
463
464
465
466
467
	} else {
		ber->ber_ptr = ber->ber_end;
	}

	ber->ber_rwptr = NULL;
}

468
/*
Kurt Zeilenga's avatar
Kurt Zeilenga committed
469
 * A rewrite of ber_get_next that can safely be called multiple times 
470
 * for the same packet. It will simply continue where it stopped until
471
472
 * a full packet is read.
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
473

474
475
#define LENSIZE	4

476
477
478
479
480
ber_tag_t
ber_get_next(
	Sockbuf *sb,
	ber_len_t *len,
	BerElement *ber )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
481
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
482
483
484
	assert( sb != NULL );
	assert( len != NULL );
	assert( ber != NULL );
485
	assert( SOCKBUF_VALID( sb ) );
486
	assert( LBER_VALID( ber ) );
487

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
488
489
490
491
	if ( ber->ber_debug & LDAP_DEBUG_TRACE ) {
		ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
			"ber_get_next\n" );
	}
492

Kurt Zeilenga's avatar
Kurt Zeilenga committed
493
494
495
496
497
498
499
500
501
502
	/*
	 * Any ber element looks like this: tag length contents.
	 * Assuming everything's ok, we return the tag byte (we
	 * can assume a single byte), return the length in len,
	 * and the rest of the undecoded element in buf.
	 *
	 * Assumptions:
	 *	1) small tags (less than 128)
	 *	2) definite lengths
	 *	3) primitive encodings used whenever possible
503
504
505
506
507
508
509
510
511
	 *
	 * The code also handles multi-byte tags. The first few bytes
	 * of the message are read to check for multi-byte tags and
	 * lengths. These bytes are temporarily stored in the ber_tag,
	 * ber_len, and ber_usertag fields of the berelement until
	 * tag/len parsing is complete. After this parsing, any leftover
	 * bytes and the rest of the message are copied into the ber_buf.
	 *
	 * We expect tag and len to be at most 32 bits wide.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
512
	 */
513

514
	if (ber->ber_rwptr == NULL) {
515
		assert( ber->ber_buf == NULL );
516
517
		ber->ber_rwptr = (char *) &ber->ber_len-1;
		ber->ber_ptr = ber->ber_rwptr;
518
519
		ber->ber_tag = 0;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
520

521
	while (ber->ber_rwptr > (char *)&ber->ber_tag && ber->ber_rwptr <
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
522
		(char *)&ber->ber_len + LENSIZE*2) {
523
		ber_slen_t sblen;
524
		char buf[sizeof(ber->ber_len)-1];
525
		ber_len_t tlen = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
526

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
527
		/* The tag & len can be at most 9 bytes; we try to read up to 8 here */
528
		sock_errset(0);
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
529
530
531
532
533
		sblen=((char *)&ber->ber_len + LENSIZE*2 - 1)-ber->ber_rwptr;
		/* Trying to read the last len byte of a 9 byte tag+len */
		if (sblen<1)
			sblen = 1;
		sblen=ber_int_sb_read( sb, ber->ber_rwptr, sblen );
534
535
		if (sblen<=0) return LBER_DEFAULT;
		ber->ber_rwptr += sblen;
536
537
538
539
540
541
542

		/* We got at least one byte, try to parse the tag. */
		if (ber->ber_ptr == (char *)&ber->ber_len-1) {
			ber_tag_t tag;
			unsigned char *p = (unsigned char *)ber->ber_ptr;
			tag = *p++;
			if ((tag & LBER_BIG_TAG_MASK) == LBER_BIG_TAG_MASK) {
543
				ber_len_t i;
Howard Chu's avatar
Howard Chu committed
544
				for (i=1; (char *)p<ber->ber_rwptr; i++) {
545
					tag <<= 8;
Howard Chu's avatar
Howard Chu committed
546
547
					tag |= *p++;
					if (!(tag & LBER_MORE_TAG_MASK))
548
549
550
						break;
					/* Is the tag too big? */
					if (i == sizeof(ber_tag_t)-1) {
551
						sock_errset(ERANGE);
552
553
554
555
556
						return LBER_DEFAULT;
					}
				}
				/* Did we run out of bytes? */
				if ((char *)p == ber->ber_rwptr) {
557
					sock_errset(EWOULDBLOCK);
558
559
					return LBER_DEFAULT;
				}
560
			}
561
562
			ber->ber_tag = tag;
			ber->ber_ptr = (char *)p;
563
		}
564

Howard Chu's avatar
Howard Chu committed
565
		if ( ber->ber_ptr == ber->ber_rwptr ) {
566
			sock_errset(EWOULDBLOCK);
Howard Chu's avatar
Howard Chu committed
567
568
569
			return LBER_DEFAULT;
		}

570
571
		/* Now look for the length */
		if (*ber->ber_ptr & 0x80) {	/* multi-byte */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
572
			int i;
573
574
			unsigned char *p = (unsigned char *)ber->ber_ptr;
			int llen = *p++ & 0x7f;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
575
			if (llen > LENSIZE) {
576
				sock_errset(ERANGE);
577
				return LBER_DEFAULT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
578
			}
579
			/* Not enough bytes? */
580
			if (ber->ber_rwptr - (char *)p < llen) {
581
				sock_errset(EWOULDBLOCK);
582
583
				return LBER_DEFAULT;
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
584
			for (i=0; i<llen; i++) {
585
				tlen <<=8;
586
				tlen |= *p++;
587
			}
588
			ber->ber_ptr = (char *)p;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
589
		} else {
590
			tlen = *(unsigned char *)ber->ber_ptr++;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
591
		}
592

593
		/* Are there leftover data bytes inside ber->ber_len? */
594
		if (ber->ber_ptr < (char *)&ber->ber_usertag) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
595
			if (ber->ber_rwptr < (char *)&ber->ber_usertag) {
596
				sblen = ber->ber_rwptr - ber->ber_ptr;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
597
			} else {
598
				sblen = (char *)&ber->ber_usertag - ber->ber_ptr;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
599
			}
600
601
			AC_MEMCPY(buf, ber->ber_ptr, sblen);
			ber->ber_ptr += sblen;
602
		} else {
603
			sblen = 0;
604
		}
605
		ber->ber_len = tlen;
606

607
		/* now fill the buffer. */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
608

609
610
		/* make sure length is reasonable */
		if ( ber->ber_len == 0 ) {
611
			sock_errset(ERANGE);
Kurt Zeilenga's avatar
ITS#133    
Kurt Zeilenga committed
612
			return LBER_DEFAULT;
613
614
615
		}

		if ( sb->sb_max_incoming && ber->ber_len > sb->sb_max_incoming ) {
616
			ber_log_printf( LDAP_DEBUG_CONNS, ber->ber_debug,
617
				"ber_get_next: sockbuf_max_incoming exceeded "
618
				"(%ld > %ld)\n", ber->ber_len, sb->sb_max_incoming );
619
			sock_errset(ERANGE);
620
621
			return LBER_DEFAULT;
		}
622

Kurt Zeilenga's avatar
Kurt Zeilenga committed
623
		if (ber->ber_buf==NULL) {
624
625
626
627
628
			ber_len_t l = ber->ber_rwptr - ber->ber_ptr;
			/* ber->ber_ptr is always <= ber->ber->ber_rwptr.
			 * make sure ber->ber_len agrees with what we've
			 * already read.
			 */
629
			if ( ber->ber_len < sblen + l ) {
630
				sock_errset(ERANGE);
631
632
				return LBER_DEFAULT;
			}
633
			ber->ber_buf = (char *) ber_memalloc_x( ber->ber_len + 1, ber->ber_memctx );
634
635
636
637
			if (ber->ber_buf==NULL) {
				return LBER_DEFAULT;
			}
			ber->ber_end = ber->ber_buf + ber->ber_len;
638
639
			if (sblen) {
				AC_MEMCPY(ber->ber_buf, buf, sblen);
640
			}
641
			if (l > 0) {
642
643
				AC_MEMCPY(ber->ber_buf + sblen, ber->ber_ptr, l);
				sblen += l;
644
			}
645
			*ber->ber_end = '\0';
646
647
			ber->ber_ptr = ber->ber_buf;
			ber->ber_usertag = 0;
648
			if ((ber_len_t)sblen == ber->ber_len) {
649
650
				goto done;
			}
651
			ber->ber_rwptr = ber->ber_buf + sblen;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
652
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
653
	}
654

655
	if ((ber->ber_rwptr>=ber->ber_buf) && (ber->ber_rwptr<ber->ber_end)) {
656
657
		ber_slen_t res;
		ber_slen_t to_go;
658
659
660
661
		
		to_go = ber->ber_end - ber->ber_rwptr;
		assert( to_go > 0 );
		
662
		sock_errset(0);
663
		res = ber_int_sb_read( sb, ber->ber_rwptr, to_go );
664
		if (res<=0) return LBER_DEFAULT;
665
666
667
		ber->ber_rwptr+=res;
		
		if (res<to_go) {
668
			sock_errset(EWOULDBLOCK);
669
			return LBER_DEFAULT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
670
		}
671
done:
672
673
674
		ber->ber_rwptr = NULL;
		*len = ber->ber_len;
		if ( ber->ber_debug ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
675
			ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
676
677
				"ber_get_next: tag 0x%lx len %ld contents:\n",
				ber->ber_tag, ber->ber_len );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
678
			ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
679
680
		}
		return (ber->ber_tag);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
681
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
682

683
684
685
	assert( 0 ); /* ber structure is messed up ?*/
	return LBER_DEFAULT;
}
Howard Chu's avatar
Howard Chu committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708

char *
ber_start( BerElement* ber )
{
	return ber->ber_buf;
}

int
ber_len( BerElement* ber )
{
	return ( ber->ber_end - ber->ber_buf );
}

int
ber_ptrlen( BerElement* ber )
{
	return ( ber->ber_ptr - ber->ber_buf );
}

void
ber_rewind ( BerElement * ber )
{
	ber->ber_rwptr = NULL;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
709
	ber->ber_sos_ptr = NULL;
Howard Chu's avatar
Howard Chu committed
710
711
	ber->ber_end = ber->ber_ptr;
	ber->ber_ptr = ber->ber_buf;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
712
713
714
715
#if 0	/* TODO: Should we add this? */
	ber->ber_tag = LBER_DEFAULT;
	ber->ber_usertag = 0;
#endif
Howard Chu's avatar
Howard Chu committed
716
}
717
718
719
720
721
722

int
ber_remaining( BerElement * ber )
{
	return ber_pvt_ber_remaining( ber );
}