result.c 14.5 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
4
/* result.c - routines to send ldap results, errors, and referrals */

#include "portable.h"

5
#include <stdio.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6

7
8
9
10
11
12
#include <ac/socket.h>
#include <ac/errno.h>
#include <ac/signal.h>
#include <ac/string.h>
#include <ac/time.h>
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
13

14
15
16
17
18
19
#include "slap.h"

/* we need LBER internals */
#include "../../libraries/liblber/lber-int.h"

static char *v2ref( struct berval **ref )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
20
{
21
22
	size_t len, i;
	char *v2;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
23

24
	if(ref == NULL) return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
25

26
27
28
29
30
31
32
33
34
	len = sizeof("Referral:");
	v2 = ch_strdup("Referral:");

	for( i=0; ref[i] != NULL; i++ ) {
		v2 = ch_realloc( v2, len + ref[i]->bv_len + 1 );
		v2[len-1] = '\n';
		memcpy(&v2[len], ref[i]->bv_val, ref[i]->bv_len );
		len += ref[i]->bv_len;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
35

36
37
38
39
40
41
42
43
44
45
46
47
48
49
	v2[len-1] = '\0';
	return v2;
}

static ber_tag_t req2res( ber_tag_t tag )
{
	switch( tag ) {
	case LDAP_REQ_ADD:
	case LDAP_REQ_BIND:
	case LDAP_REQ_COMPARE:
	case LDAP_REQ_EXTENDED:
	case LDAP_REQ_MODIFY:
	case LDAP_REQ_MODRDN:
		tag++;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
50
51
52
53
54
55
		break;

	case LDAP_REQ_DELETE:
		tag = LDAP_RES_DELETE;
		break;

56
57
58
	case LDAP_REQ_ABANDON:
	case LDAP_REQ_UNBIND:
		tag = LBER_SEQUENCE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
59
		break;
60
61
62
63
64
65
66
67

	case LDAP_REQ_SEARCH:
		tag = LDAP_RES_SEARCH_RESULT;
		break;

	default:
		assert( 0 );
		tag = LBER_ERROR;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
68
	}
69
70
	return tag;
}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
71
72


73
74
75
76
77
long send_ldap_ber(
	Connection *conn,
	BerElement *ber )
{
	ber_len_t bytes = ber_pvt_ber_bytes( ber );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
78
79

	/* write only one pdu at a time - wait til it's our turn */
80
81
82
83
	ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );

	/* lock the connection */ 
	ldap_pvt_thread_mutex_lock( &conn->c_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
84
85

	/* write the pdu */
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
	while( 1 ) {
		int err;

		if ( connection_state_closing( conn ) ) {
			ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
			ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
			return 0;
		}

		if ( ber_flush( conn->c_sb, ber, 1 ) == 0 ) {
			break;
		}

		err = errno;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
101
102
103
104
105
106
107
		/*
		 * we got an error.  if it's ewouldblock, we need to
		 * wait on the socket being writable.  otherwise, figure
		 * it's a hard error and return.
		 */

		Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno %d msg (%s)\n",
108
		    err, err > -1 && err < sys_nerr ? sys_errlist[err]
Kurt Zeilenga's avatar
Kurt Zeilenga committed
109
110
		    : "unknown", 0 );

111
112
		if ( err != EWOULDBLOCK && err != EAGAIN ) {
			connection_closing( conn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
113

114
115
116
			ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
			ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
			return( -1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
117
118
119
120
		}

		/* wait for socket to be write-ready */
		conn->c_writewaiter = 1;
121
		slapd_set_write( ber_pvt_sb_get_desc( conn->c_sb ), 1 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
122

123
124
		ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
		conn->c_writewaiter = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
125
126
	}

127
128
	ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
	ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
129

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
	return bytes;
}

static void
send_ldap_response(
    Connection	*conn,
    Operation	*op,
	ber_tag_t	tag,
	ber_int_t	msgid,
    ber_int_t	err,
    char	*matched,
    char	*text,
	struct berval	**ref,
	char	*resoid,
	struct berval	*resdata
)
{
	BerElement	*ber;
	int		rc;
	long	bytes;

	ber = ber_alloc_t( LBER_USE_DER );

153
154
155
	Debug( LDAP_DEBUG_TRACE, "send_ldap_response: tag=%ld msgid=%ld err=%ld\n",
		(long) tag, (long) msgid, (long) err );

156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
	if ( ber == NULL ) {
		Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
		return;
	}

#ifdef LDAP_CONNECTIONLESS
	if ( op->o_cldap ) {
		rc = ber_printf( ber, "{is{t{ess}}}", msgid, "", tag,
		    err, matched ? matched : "", text ? text : "" );
	} else
#endif
	{
		rc = ber_printf( ber, "{it{ess",
			msgid, tag, err,
			matched == NULL ? "" : matched,
			text == NULL ? "" : text );

		if( rc != -1 && ref != NULL ) {
174
			rc = ber_printf( ber, "{V}", ref );
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
		}

		if( rc != -1 && resoid != NULL ) {
			rc = ber_printf( ber, "s", resoid );
		}

		if( rc != -1 && resdata != NULL ) {
			rc = ber_printf( ber, "O", resdata );

		}

		if( rc != -1 ) {
			rc = ber_printf( ber, "}}" );
		}
	}

	if ( rc == -1 ) {
		Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
		return;
	}

	/* send BER */
	bytes = send_ldap_ber( conn, ber );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
198

199
200
201
202
203
204
205
206
207
208
209
	if ( bytes < 0 ) {
		Debug( LDAP_DEBUG_ANY,
			"send_ldap_response: ber write failed\n",
			0, 0, 0 );
		return;
	}

	ldap_pvt_thread_mutex_lock( &num_sent_mutex );
	num_bytes_sent += bytes;
	num_pdu_sent++;
	ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
210
211
212
	return;
}

213

Kurt Zeilenga's avatar
Kurt Zeilenga committed
214
void
215
send_ldap_disconnect(
Kurt Zeilenga's avatar
Kurt Zeilenga committed
216
217
    Connection	*conn,
    Operation	*op,
218
    ber_int_t	err,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
219
220
221
    char	*text
)
{
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
	ber_tag_t tag;
	ber_int_t msgid;
	char *reqoid;

#define LDAP_UNSOLICITED_ERROR(e) \
	(  (e) == LDAP_PROTOCOL_ERROR \
	|| (e) == LDAP_STRONG_AUTH_REQUIRED \
	|| (e) == LDAP_UNAVAILABLE )

	assert( LDAP_UNSOLICITED_ERROR( err ) );

	Debug( LDAP_DEBUG_TRACE,
		"send_ldap_disconnect %d:%s\n",
		err, text ? text : "", NULL );

	if ( op->o_protocol < LDAP_VERSION3 ) {
		reqoid = NULL;
		tag = req2res( op->o_tag );
		msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;

	} else {
		reqoid = LDAP_NOTICE_DISCONNECT;
		tag = LDAP_RES_EXTENDED;
		msgid = 0;
	}

#ifdef LDAP_CONNECTIONLESS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
249
	if ( op->o_cldap ) {
250
		ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
251
252
		Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
		    inet_ntoa(((struct sockaddr_in *)
253
254
		    &op->o_clientaddr)->sin_addr ),
		    ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
255
256
257
		    0 );
	}
#endif
258
259
260
261
262
263
264
265
	send_ldap_response( conn, op, tag, msgid,
		err, NULL, text, NULL,
		reqoid, NULL );

	Statslog( LDAP_DEBUG_STATS,
	    "conn=%ld op=%ld DISCONNECT err=%ld tag=%lu text=%s\n",
		(long) conn->c_connid, (long) op->o_opid,
		(long) tag, (long) err, text );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
266
267
268
}

void
269
send_ldap_result(
Kurt Zeilenga's avatar
Kurt Zeilenga committed
270
271
    Connection	*conn,
    Operation	*op,
272
    ber_int_t	err,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
273
274
    char	*matched,
    char	*text,
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
	struct berval **ref
)
{
	ber_tag_t tag;
	ber_int_t msgid;
	char *tmp = NULL;

	assert( !LDAP_API_ERROR( err ) );

	Debug( LDAP_DEBUG_TRACE, "send_ldap_result %d:%s:%s\n",
		err, matched ?  matched : "", text ? text : "" );

	assert( err != LDAP_PARTIAL_RESULTS );

	if ( err == LDAP_REFERRAL ) {
		if( ref == NULL ) {
			err = LDAP_NO_SUCH_OBJECT;
		} else if ( op->o_protocol < LDAP_VERSION3 ) {
			err = LDAP_PARTIAL_RESULTS;
			tmp = text = v2ref( ref );
			ref = NULL;
		}
	}

299
300
301
	tag = req2res( op->o_tag );
	msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;

302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#ifdef LDAP_CONNECTIONLESS
	if ( op->o_cldap ) {
		ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
		Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
		    inet_ntoa(((struct sockaddr_in *)
		    &op->o_clientaddr)->sin_addr ),
		    ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
		    0 );
	}
#endif

	send_ldap_response( conn, op, tag, msgid,
		err, matched, text, ref,
		NULL, NULL );

	Statslog( LDAP_DEBUG_STATS,
	    "conn=%ld op=%ld RESULT err=%ld tag=%lu text=%s\n",
		(long) conn->c_connid, (long) op->o_opid,
		(long) err, (long) tag, text );

	if( tmp != NULL ) {
		free(tmp);
	}
}


void
send_search_result(
    Connection	*conn,
    Operation	*op,
    ber_int_t	err,
    char	*matched,
	char	*text,
    struct berval **refs,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
336
337
338
    int		nentries
)
{
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
	ber_tag_t tag;
	ber_int_t msgid;
	char *tmp = NULL;
	assert( !LDAP_API_ERROR( err ) );

	Debug( LDAP_DEBUG_TRACE, "send_ldap_search_result %d:%s:%s\n",
		err, matched ?  matched : "", text ? text : "" );

	assert( err != LDAP_PARTIAL_RESULTS );

	if( op->o_protocol < LDAP_VERSION3 ) {
		/* send references in search results */
		if( err == LDAP_REFERRAL ) {
			err = LDAP_PARTIAL_RESULTS;
			tmp = text = v2ref( refs );
			refs = NULL;
		}

	} else {
		/* don't send references in search results */
		if( err == LDAP_REFERRAL ) {
			err = LDAP_SUCCESS;
			refs = NULL;
		}
	}

365
366
367
	tag = req2res( op->o_tag );
	msgid = (tag != LBER_SEQUENCE) ? op->o_msgid : 0;

368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#ifdef LDAP_CONNECTIONLESS
	if ( op->o_cldap ) {
		ber_pvt_sb_udp_set_dst( &conn->c_sb, &op->o_clientaddr );
		Debug( LDAP_DEBUG_TRACE, "UDP response to %s port %d\n", 
		    inet_ntoa(((struct sockaddr_in *)
		    &op->o_clientaddr)->sin_addr ),
		    ((struct sockaddr_in *) &op->o_clientaddr)->sin_port,
		    0 );
	}
#endif

	send_ldap_response( conn, op, tag, msgid,
		err, matched, text, refs,
		NULL, NULL );

	Statslog( LDAP_DEBUG_STATS,
	    "conn=%ld op=%ld SEARCH RESULT err=%ld tag=%lu text=%s\n",
		(long) conn->c_connid, (long) op->o_opid,
		(long) err, (long) tag, text );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
388
389
}

390

Kurt Zeilenga's avatar
Kurt Zeilenga committed
391
392
393
394
395
396
397
int
send_search_entry(
    Backend	*be,
    Connection	*conn,
    Operation	*op,
    Entry	*e,
    char	**attrs,
398
399
    int		attrsonly,
	int		opattrs
Kurt Zeilenga's avatar
Kurt Zeilenga committed
400
401
402
403
)
{
	BerElement	*ber;
	Attribute	*a;
404
	int		i, rc=-1, bytes;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
405
	struct acl	*acl;
406
	char            *edn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
407
408
409

	Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );

410
411
412
413
414
415
416
417
418
419
420
421
422
423
#if defined( SLAPD_SCHEMA_DN )
	{
		/* this could be backend specific */
		struct	berval	val;
		val.bv_val = SLAPD_SCHEMA_DN;
		val.bv_len = strlen( val.bv_val );
		attr_merge( e, "subschemaSubentry", vals );
		ldap_memfree( val.bv_val );
	}
#endif

	if ( ! access_allowed( be, conn, op, e,
		"entry", NULL, ACL_READ ) )
	{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
424
425
426
427
428
		Debug( LDAP_DEBUG_ACL, "acl: access to entry not allowed\n",
		    0, 0, 0 );
		return( 1 );
	}

429
430
431
432
433
	edn = e->e_ndn;

	ber = ber_alloc_t( LBER_USE_DER );

	if ( ber == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
434
		Debug( LDAP_DEBUG_ANY, "ber_alloc failed\n", 0, 0, 0 );
435
436
437
		send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
			NULL, "allocating BER error", NULL );
		goto error_return;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
438
439
	}

440
441
	rc = ber_printf( ber, "{it{s{", op->o_msgid,
		LDAP_RES_SEARCH_ENTRY, e->e_dn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
442
443
444
445

	if ( rc == -1 ) {
		Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
		ber_free( ber, 1 );
446
447
448
		send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
		    NULL, "encoding dn error", NULL );
		goto error_return;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
449
450
451
	}

	for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
		regmatch_t       matches[MAXREMATCHES];

		if ( attrs == NULL ) {
			/* all addrs request, skip operational attributes */
			if( !opattrs && oc_check_operational_attr( a->a_type )) {
				continue;
			}

		} else {
			/* specific addrs requested */
			if ( !charray_inlist( attrs, a->a_type )
				&& !charray_inlist( attrs, LDAP_ALL_USER_ATTRIBUTES ) )
			{
				continue;
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
467
468
		}

469
470
		acl = acl_get_applicable( be, op, e, a->a_type,
			MAXREMATCHES, matches );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
471

472
473
474
		if ( ! acl_access_allowed( acl, be, conn, e,
			NULL, op, ACL_READ, edn, matches ) ) 
		{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
475
476
477
			continue;
		}

478
		if (( rc = ber_printf( ber, "{s[" /*]}*/ , a->a_type )) == -1 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
479
480
481
			Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
			ber_free( ber, 1 );
			send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
482
483
			    NULL, "encoding type error", NULL );
			goto error_return;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
484
485
486
487
		}

		if ( ! attrsonly ) {
			for ( i = 0; a->a_vals[i] != NULL; i++ ) {
488
489
490
				if ( a->a_syntax & SYNTAX_DN && 
					! acl_access_allowed( acl, be, conn, e, a->a_vals[i], op,
						ACL_READ, edn, matches) )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
491
492
493
494
				{
					continue;
				}

495
				if (( rc = ber_printf( ber, "O", a->a_vals[i] )) == -1 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
496
497
498
					Debug( LDAP_DEBUG_ANY,
					    "ber_printf failed\n", 0, 0, 0 );
					ber_free( ber, 1 );
499
500
501
					send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
						NULL, "encoding value error", NULL );
					goto error_return;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
502
503
504
505
				}
			}
		}

506
		if (( rc = ber_printf( ber, /*{[*/ "]}" )) == -1 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
507
508
509
			Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
			ber_free( ber, 1 );
			send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
510
511
			    NULL, "encode end error", NULL );
			goto error_return;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
512
513
514
		}
	}

515
	rc = ber_printf( ber, /*{{{*/ "}}}" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
516
517
518
519

	if ( rc == -1 ) {
		Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
		ber_free( ber, 1 );
520
521
		send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
			NULL, "encode entry end error", NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
522
523
524
		return( 1 );
	}

525
	bytes = send_ldap_ber( conn, ber );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
526

527
528
529
530
531
532
	if ( bytes < 0 ) {
		Debug( LDAP_DEBUG_ANY,
			"send_ldap_response: ber write failed\n",
			0, 0, 0 );
		return -1;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
533

534
535
536
537
538
	ldap_pvt_thread_mutex_lock( &num_sent_mutex );
	num_bytes_sent += bytes;
	num_entries_sent++;
	num_pdu_sent++;
	ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
539

540
541
	Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
	    (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
542

543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
	Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );

	rc = 0;

error_return:;
	return( rc );
}

int
send_search_reference(
    Backend	*be,
    Connection	*conn,
    Operation	*op,
    Entry	*e,
	struct berval **refs,
    struct berval ***v2refs
)
{
	BerElement	*ber;
	int rc;
	int bytes;

	Debug( LDAP_DEBUG_TRACE, "=> send_search_entry (%s)\n", e->e_dn, 0, 0 );

	if ( ! access_allowed( be, conn, op, e,
		"entry", NULL, ACL_READ ) )
	{
		Debug( LDAP_DEBUG_ACL,
			"send_search_reference: access to entry not allowed\n",
		    0, 0, 0 );
		return( 1 );
	}

	if ( ! access_allowed( be, conn, op, e,
		"ref", NULL, ACL_READ ) )
	{
		Debug( LDAP_DEBUG_ACL,
			"send_search_reference: access to reference not allowed\n",
		    0, 0, 0 );
		return( 1 );
	}

	if( refs == NULL ) {
		Debug( LDAP_DEBUG_ANY,
			"send_search_reference: null ref in (%s)\n", 
			e->e_dn, 0, 0 );
		return( 1 );
	}

	if( op->o_protocol < LDAP_VERSION3 ) {
		/* save the references for the result */
		if( *refs == NULL ) {
			value_add( v2refs, refs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
596
		}
597
598
		return 0;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
599

600
	ber = ber_alloc_t( LBER_USE_DER );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
601

602
603
604
605
606
607
	if ( ber == NULL ) {
		Debug( LDAP_DEBUG_ANY,
			"send_search_reference: ber_alloc failed\n", 0, 0, 0 );
		send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
			NULL, "alloc BER error", NULL );
		return -1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
608
609
	}

610
611
	rc = ber_printf( ber, "{it{V}}", op->o_msgid,
		LDAP_RES_SEARCH_REFERENCE, refs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
612

613
614
615
616
617
618
619
	if ( rc == -1 ) {
		Debug( LDAP_DEBUG_ANY,
			"send_search_reference: ber_printf failed\n", 0, 0, 0 );
		ber_free( ber, 1 );
		send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
			NULL, "encode dn error", NULL );
		return -1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
620
	}
621
622
623
624
625
626
627
628
629
630
631

	bytes = send_ldap_ber( conn, ber );

	ldap_pvt_thread_mutex_lock( &num_sent_mutex );
	num_bytes_sent += bytes;
	num_refs_sent++;
	num_pdu_sent++;
	ldap_pvt_thread_mutex_unlock( &num_sent_mutex );

	Statslog( LDAP_DEBUG_STATS2, "conn=%ld op=%ld ENTRY dn=\"%s\"\n",
	    (long) conn->c_connid, (long) op->o_opid, e->e_dn, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
632
633
634

	Debug( LDAP_DEBUG_TRACE, "<= send_search_entry\n", 0, 0, 0 );

635
	return 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
636
637
}

638

Kurt Zeilenga's avatar
Kurt Zeilenga committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
int
str2result(
    char	*s,
    int		*code,
    char	**matched,
    char	**info
)
{
	int	rc;
	char	*c;

	*code = LDAP_SUCCESS;
	*matched = NULL;
	*info = NULL;

	if ( strncasecmp( s, "RESULT", 6 ) != 0 ) {
		Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
		    s, 0, 0 );

		return( -1 );
	}

	rc = 0;
	while ( (s = strchr( s, '\n' )) != NULL ) {
		*s++ = '\0';
		if ( *s == '\0' ) {
			break;
		}
		if ( (c = strchr( s, ':' )) != NULL ) {
			c++;
		}

		if ( strncasecmp( s, "code", 4 ) == 0 ) {
			if ( c != NULL ) {
				*code = atoi( c );
			}
		} else if ( strncasecmp( s, "matched", 7 ) == 0 ) {
			if ( c != NULL ) {
				*matched = c;
			}
		} else if ( strncasecmp( s, "info", 4 ) == 0 ) {
			if ( c != NULL ) {
				*info = c;
			}
		} else {
			Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
			    s, 0, 0 );
			rc = -1;
		}
	}

	return( rc );
}