result.c 47.7 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* result.c - routines to send ldap results, errors, and referrals */
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 1998-2021 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.
25
 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
26
27

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

#include <stdio.h>

31
#include <ac/socket.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
32
33
#include <ac/errno.h>
#include <ac/string.h>
Hallvard Furuseth's avatar
Hallvard Furuseth committed
34
#include <ac/ctype.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
35
#include <ac/time.h>
36
#include <ac/unistd.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
37

Kurt Zeilenga's avatar
Kurt Zeilenga committed
38
#include "slap.h"
Howard Chu's avatar
Howard Chu committed
39
40
41
42

#if SLAP_STATS_ETIME
#define ETIME_SETUP \
	struct timeval now; \
Howard Chu's avatar
Howard Chu committed
43
	char timestr[64]; \
Howard Chu's avatar
Howard Chu committed
44
45
	(void) gettimeofday( &now, NULL ); \
	now.tv_sec -= op->o_time; \
Howard Chu's avatar
Howard Chu committed
46
	now.tv_usec -= op->o_tusec; \
Howard Chu's avatar
Howard Chu committed
47
48
	if ( now.tv_usec < 0 ) { \
		--now.tv_sec; now.tv_usec += 1000000; \
Howard Chu's avatar
Howard Chu committed
49
50
51
52
53
	} \
	sprintf(timestr, "qtime=%d.%06d etime=%d.%06d", \
		(int)op->o_qtime.tv_sec, (int)op->o_qtime.tv_usec, \
		(int)now.tv_sec, (int)now.tv_usec);
#define ETIME_LOGFMT	"%s "
Ondřej Kuzník's avatar
Ondřej Kuzník committed
54
55
#define StatslogEtime(lvl,fmt,pfx,tag,err,...) \
	Debug(lvl,fmt,pfx,tag,err,timestr,__VA_ARGS__)
Howard Chu's avatar
Howard Chu committed
56
57
58
#else
#define ETIME_SETUP
#define ETIME_LOGFMT	""
Ondřej Kuzník's avatar
Ondřej Kuzník committed
59
#define StatslogEtime	Debug
Howard Chu's avatar
Howard Chu committed
60
#endif	/* SLAP_STATS_ETIME */
61

62
63
const struct berval slap_dummy_bv = BER_BVNULL;

Howard Chu's avatar
Howard Chu committed
64
65
66
67
68
int slap_null_cb( Operation *op, SlapReply *rs )
{
	return 0;
}

69
70
int slap_freeself_cb( Operation *op, SlapReply *rs )
{
71
	assert( op->o_callback != NULL );
72
73
74
75
76
77
78

	op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
	op->o_callback = NULL;

	return SLAP_CB_CONTINUE;
}

79
static char *v2ref( BerVarray ref, const char *text )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
80
{
81
	size_t len = 0, i = 0;
82
	char *v2;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
83

Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
84
	if(ref == NULL) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
85
86
87
88
89
		if (text) {
			return ch_strdup(text);
		} else {
			return NULL;
		}
90
91
	}
	
Kurt Zeilenga's avatar
Kurt Zeilenga committed
92
	if ( text != NULL ) {
93
		len = strlen( text );
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
94
		if (text[len-1] != '\n') {
95
		    i = 1;
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
96
		}
97
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
98

99
	v2 = ch_malloc( len+i+sizeof("Referral:") );
Julius Enarusai's avatar
   
Julius Enarusai committed
100

Kurt Zeilenga's avatar
Kurt Zeilenga committed
101
	if( text != NULL ) {
102
		strcpy(v2, text);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
103
		if( i ) {
104
			v2[len++] = '\n';
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
105
		}
106
107
	}
	strcpy( v2+len, "Referral:" );
Howard Chu's avatar
Howard Chu committed
108
	len += sizeof("Referral:");
109

110
	for( i=0; ref[i].bv_val != NULL; i++ ) {
111
		v2 = ch_realloc( v2, len + ref[i].bv_len + 1 );
112
		v2[len-1] = '\n';
113
114
115
		AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
		len += ref[i].bv_len;
		if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
116
			++len;
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
117
		}
118
	}
119

120
121
122
	v2[len-1] = '\0';
	return v2;
}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
123

124
125
ber_tag_t
slap_req2res( ber_tag_t tag )
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{
	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++;
		break;

	case LDAP_REQ_DELETE:
		tag = LDAP_RES_DELETE;
		break;

	case LDAP_REQ_ABANDON:
	case LDAP_REQ_UNBIND:
		tag = LBER_SEQUENCE;
		break;

	case LDAP_REQ_SEARCH:
		tag = LDAP_RES_SEARCH_RESULT;
		break;

	default:
151
		tag = LBER_SEQUENCE;
152
	}
153

154
155
	return tag;
}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
156

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * SlapReply debugging enabled by USE_RS_ASSERT.
 *
 * Disabled by default, but compiled in (but still unused) when
 * LDAP_TEST.  #define USE_RS_ASSERT as nonzero to enable some
 * assertions which check the SlapReply.  USE_RS_ASSERT = 2 or higher
 * check aggressively, currently some code fail these tests.
 *
 * Environment variable $NO_RS_ASSERT controls how USE_RS_ASSERT handles
 * errors.  > 0: ignore errors, 0: abort (the default), < 0: just warn.
 *
 * Wrap LDAP operation calls in macros SLAP_OP() & co from proto-slap.h
 * to check the SlapReply.  contrib/slapd-tools/wrap_slap_ops converts
 * source code to use the macros.
 */
172
#if defined(LDAP_TEST) || (defined(USE_RS_ASSERT) && (USE_RS_ASSERT))
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

int rs_suppress_assert = 0;

/* RS_ASSERT() helper function */
void rs_assert_(const char*file, unsigned line, const char*fn, const char*cond)
{
	int no_assert = rs_suppress_assert, save_errno = errno;
	const char *s;

	if ( no_assert >= 0 ) {
		if ( no_assert == 0 && (s = getenv( "NO_RS_ASSERT" )) && *s ) {
			no_assert = rs_suppress_assert = atoi( s );
		}
		if ( no_assert > 0 ) {
			errno = save_errno;
			return;
		}
	}

#ifdef rs_assert_	/* proto-slap.h #defined away the fn parameter */
	fprintf( stderr,"%s:%u: "  "RS_ASSERT(%s) failed.\n", file,line,cond );
#else
	fprintf( stderr,"%s:%u: %s: RS_ASSERT(%s) failed.\n", file,line,fn,cond );
#endif
	fflush( stderr );

	errno = save_errno;
	/* $NO_RS_ASSERT > 0: ignore rs_asserts, 0: abort, < 0: just warn */
201
	if ( !no_assert /* from $NO_RS_ASSERT */ ) abort();
202
203
204
}

/* SlapReply is consistent */
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
void
(rs_assert_ok)( const SlapReply *rs )
{
	const slap_mask_t flags = rs->sr_flags;

	if ( flags & REP_ENTRY_MASK ) {
		RS_ASSERT( !(flags & REP_ENTRY_MUSTRELEASE)
			|| !(flags & (REP_ENTRY_MASK ^ REP_ENTRY_MUSTRELEASE)) );
		RS_ASSERT( rs->sr_entry != NULL );
		RS_ASSERT( (1 << rs->sr_type) &
			((1 << REP_SEARCH) | (1 << REP_SEARCHREF) |
			 (1 << REP_RESULT) | (1 << REP_GLUE_RESULT)) );
	}
#if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
	if ( (flags & (REP_MATCHED_MASK | REP_REF_MASK | REP_CTRLS_MASK)) ) {
		RS_ASSERT( !(flags & REP_MATCHED_MASK) || rs->sr_matched );
		RS_ASSERT( !(flags & REP_CTRLS_MASK  ) || rs->sr_ctrls   );
		/* Note: LDAP_REFERRAL + !sr_ref is OK, becomes LDAP_NO_SUCH_OBJECT */
	}
#if (USE_RS_ASSERT) > 2
	if ( rs->sr_err == LDAP_SUCCESS ) {
		RS_ASSERT( rs->sr_text == NULL );
		RS_ASSERT( rs->sr_matched == NULL );
	}
#endif
#endif
}
232
233

/* Ready for calling a new backend operation */
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
void
(rs_assert_ready)( const SlapReply *rs )
{
	RS_ASSERT( !rs->sr_entry   );
#if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
	RS_ASSERT( !rs->sr_text    );
	RS_ASSERT( !rs->sr_ref     );
	RS_ASSERT( !rs->sr_matched );
	RS_ASSERT( !rs->sr_ctrls   );
	RS_ASSERT( !rs->sr_flags   );
#if (USE_RS_ASSERT) > 2
	RS_ASSERT( rs->sr_err == LDAP_SUCCESS );
#endif
#else
	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
#endif
}
251
252

/* Backend operation done */
253
254
255
256
257
258
void
(rs_assert_done)( const SlapReply *rs )
{
#if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
	RS_ASSERT( !(rs->sr_flags & ~(REP_ENTRY_MODIFIABLE|REP_NO_OPERATIONALS)) );
	rs_assert_ok( rs );
259
#else
260
	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MUSTFLUSH) );
261
#endif
262
}
263

264
#endif /* LDAP_TEST || USE_RS_ASSERT */
265

266
/* Reset a used SlapReply whose contents has been flushed (freed/released) */
267
void
268
(rs_reinit)( SlapReply *rs, slap_reply_t type )
269
{
270
	rs_reinit( rs, type );		/* proto-slap.h macro */
271
272
273
274
275
}

/* Obey and clear rs->sr_flags & REP_ENTRY_MASK.  Clear sr_entry if freed. */
void
rs_flush_entry( Operation *op, SlapReply *rs, slap_overinst *on )
276
{
277
	rs_assert_ok( rs );
278

279
280
	if ( (rs->sr_flags & REP_ENTRY_MUSTFLUSH) && rs->sr_entry != NULL ) {
		if ( !(rs->sr_flags & REP_ENTRY_MUSTRELEASE) ) {
281
282
283
284
285
286
			entry_free( rs->sr_entry );
		} else if ( on != NULL ) {
			overlay_entry_release_ov( op, rs->sr_entry, 0, on );
		} else {
			be_entry_release_rw( op, rs->sr_entry, 0 );
		}
287
		rs->sr_entry = NULL;
288
	}
289

290
	rs->sr_flags &= ~REP_ENTRY_MASK;
291
292
293
294
295
296
297
}

/* Set rs->sr_entry after obeying and clearing sr_flags & REP_ENTRY_MASK. */
void
rs_replace_entry( Operation *op, SlapReply *rs, slap_overinst *on, Entry *e )
{
	rs_flush_entry( op, rs, on );
298
299
300
301
302
303
304
305
306
	rs->sr_entry = e;
}

/*
 * Ensure rs->sr_entry is modifiable, by duplicating it if necessary.
 * Obey sr_flags.  Set REP_ENTRY_<MODIFIABLE, and MUSTBEFREED if duplicated>.
 * Return nonzero if rs->sr_entry was replaced.
 */
int
307
rs_entry2modifiable( Operation *op, SlapReply *rs, slap_overinst *on )
308
309
{
	if ( rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
310
		rs_assert_ok( rs );
311
312
313
314
315
316
317
		return 0;
	}
	rs_replace_entry( op, rs, on, entry_dup( rs->sr_entry ));
	rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
	return 1;
}

Howard Chu's avatar
Howard Chu committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* Check for any callbacks that want to be informed about being blocked
 * on output. These callbacks are expected to leave the callback list
 * unmodified. Their result is ignored.
 */
static void
slap_writewait_play(
	Operation *op )
{
	slap_callback	*sc = op->o_callback;

	for ( ; sc; sc = sc->sc_next ) {
		if ( sc->sc_writewait )
			sc->sc_writewait( op, sc );
	}
}

334
static long send_ldap_ber(
Howard Chu's avatar
Howard Chu committed
335
	Operation *op,
336
337
	BerElement *ber )
{
Howard Chu's avatar
Howard Chu committed
338
	Connection *conn = op->o_conn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
339
	ber_len_t bytes;
340
	long ret = 0;
Howard Chu's avatar
Howard Chu committed
341
	char *close_reason;
Howard Chu's avatar
Howard Chu committed
342
	int do_resume = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
343
344

	ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
345

Kurt Zeilenga's avatar
Kurt Zeilenga committed
346
	/* write only one pdu at a time - wait til it's our turn */
347
	ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
348
349
	if (( op->o_abandon && !op->o_cancel ) || !connection_valid( conn ) ||
		conn->c_writers < 0 ) {
350
351
352
		ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
		return 0;
	}
353
354
355
356

	conn->c_writers++;

	while ( conn->c_writers > 0 && conn->c_writing ) {
Howard Chu's avatar
Howard Chu committed
357
		ldap_pvt_thread_pool_idle( &connection_pool );
358
		ldap_pvt_thread_cond_wait( &conn->c_write1_cv, &conn->c_write1_mutex );
Howard Chu's avatar
Howard Chu committed
359
		ldap_pvt_thread_pool_unidle( &connection_pool );
360
	}
361

362
363
364
365
366
	/* connection was closed under us */
	if ( conn->c_writers < 0 ) {
		/* we're the last waiter, let the closer continue */
		if ( conn->c_writers == -1 )
			ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
367
		conn->c_writers++;
Howard Chu's avatar
Howard Chu committed
368
		ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
369
		return 0;
Howard Chu's avatar
Howard Chu committed
370
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
371

372
373
374
	/* Our turn */
	conn->c_writing = 1;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
375
	/* write the pdu */
376
	while( 1 ) {
377
		int err;
378
		char ebuf[128];
379

Pierangelo Masarati's avatar
Pierangelo Masarati committed
380
		if ( ber_flush2( conn->c_sb, ber, LBER_FLUSH_FREE_NEVER ) == 0 ) {
381
			ret = bytes;
382
383
384
			break;
		}

385
		err = sock_errno();
386

Kurt Zeilenga's avatar
Kurt Zeilenga committed
387
388
389
390
391
392
		/*
		 * 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.
		 */

Pierangelo Masarati's avatar
Pierangelo Masarati committed
393
		Debug( LDAP_DEBUG_CONNS, "ber_flush2 failed errno=%d reason=\"%s\"\n",
394
		    err, sock_errstr(err, ebuf, sizeof(ebuf)) );
395

396
		if ( err != EWOULDBLOCK && err != EAGAIN ) {
Howard Chu's avatar
Howard Chu committed
397
398
			close_reason = "connection lost on write";
fail:
Howard Chu's avatar
Howard Chu committed
399
			conn->c_writers--;
400
			conn->c_writing = 0;
Howard Chu's avatar
Howard Chu committed
401
			ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
402
			ldap_pvt_thread_mutex_lock( &conn->c_mutex );
Howard Chu's avatar
Howard Chu committed
403
			connection_closing( conn, close_reason );
404
			ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
Howard Chu's avatar
Howard Chu committed
405
			return -1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
406
407
408
		}

		/* wait for socket to be write-ready */
Howard Chu's avatar
Howard Chu committed
409
		do_resume = 1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
410
		conn->c_writewaiter = 1;
Howard Chu's avatar
Howard Chu committed
411
		ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
412
		ldap_pvt_thread_pool_idle( &connection_pool );
Howard Chu's avatar
Howard Chu committed
413
		slap_writewait_play( op );
Howard Chu's avatar
Howard Chu committed
414
		err = slapd_wait_writer( conn->c_sd );
Howard Chu's avatar
Howard Chu committed
415
		conn->c_writewaiter = 0;
416
		ldap_pvt_thread_pool_unidle( &connection_pool );
417
		ldap_pvt_thread_mutex_lock( &conn->c_write1_mutex );
Howard Chu's avatar
Howard Chu committed
418
419
420
421
422
423
424
425
426
427
428
		/* 0 is timeout, so we close it.
		 * -1 is an error, close it.
		 */
		if ( err <= 0 ) {
			if ( err == 0 )
				close_reason = "writetimeout";
			else
				close_reason = "connection lost on writewait";
			goto fail;
		}

Howard Chu's avatar
Howard Chu committed
429
		if ( conn->c_writers < 0 ) {
430
431
432
			ret = 0;
			break;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
433
434
	}

435
	conn->c_writing = 0;
436
	if ( conn->c_writers < 0 ) {
Howard Chu's avatar
Howard Chu committed
437
438
		/* shutting down, don't resume any ops */
		do_resume = 0;
439
440
441
442
443
		conn->c_writers++;
		if ( !conn->c_writers )
			ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
	} else {
		conn->c_writers--;
Howard Chu's avatar
Howard Chu committed
444
445
446
		/* other writers are waiting, don't resume any ops */
		if ( conn->c_writers )
			do_resume = 0;
447
448
449
		ldap_pvt_thread_cond_signal( &conn->c_write1_cv );
	}
	ldap_pvt_thread_mutex_unlock( &conn->c_write1_mutex );
450

Howard Chu's avatar
Howard Chu committed
451
452
453
454
	/* If there are no more writers, release a pending op */
	if ( do_resume )
		connection_write_resume( conn );

455
	return ret;
456
457
}

458
static int
459
send_ldap_control( BerElement *ber, LDAPControl *c )
460
461
{
	int rc;
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

	assert( c != NULL );

	rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );

	if( c->ldctl_iscritical ) {
		rc = ber_printf( ber, "b",
			(ber_int_t) c->ldctl_iscritical ) ;
		if( rc == -1 ) return rc;
	}

	if( c->ldctl_value.bv_val != NULL ) {
		rc = ber_printf( ber, "O", &c->ldctl_value ); 
		if( rc == -1 ) return rc;
	}

	rc = ber_printf( ber, /*{*/"N}" );
	if( rc == -1 ) return rc;

	return 0;
}

static int
send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
{
	int rc;

489
490
	if( c == NULL )
		return 0;
491
492
493
494

	rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
	if( rc == -1 ) return rc;

495
496
497
	for( ; *c != NULL; c++) {
		rc = send_ldap_control( ber, *c );
		if( rc == -1 ) return rc;
498
499
	}

Howard Chu's avatar
Howard Chu committed
500
#ifdef SLAP_CONTROL_X_SORTEDRESULTS
501
502
503
504
505
506
507
508
509
510
511
	/* this is a hack to avoid having to modify op->s_ctrls */
	if( o->o_sortedresults ) {
		BerElementBuffer berbuf;
		BerElement *sber = (BerElement *) &berbuf;
		LDAPControl sorted;
		BER_BVZERO( &sorted.ldctl_value );
		sorted.ldctl_oid = LDAP_CONTROL_SORTRESPONSE;
		sorted.ldctl_iscritical = 0;

		ber_init2( sber, NULL, LBER_USE_DER );

512
		ber_printf( sber, "{e}", LDAP_UNWILLING_TO_PERFORM );
513

514
		if( ber_flatten2( sber, &sorted.ldctl_value, 0 ) == -1 ) {
515
			return -1;
516
517
		}

518
		(void) ber_free_buf( sber );
519
520

		rc = send_ldap_control( ber, &sorted );
521
522
		if( rc == -1 ) return rc;
	}
523
#endif
524
525
526
527
528
529

	rc = ber_printf( ber, /*{*/"N}" );

	return rc;
}

530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
 * slap_response_play()
 *
 * plays the callback list; rationale: a callback can
 *   - remove itself from the list, by setting op->o_callback = NULL;
 *     malloc()'ed callbacks should free themselves from inside the
 *     sc_response() function.
 *   - replace itself with another (list of) callback(s), by setting
 *     op->o_callback = a new (list of) callback(s); in this case, it
 *     is the callback's responsibility to to append existing subsequent
 *     callbacks to the end of the list that is passed to the sc_response()
 *     function.
 *   - modify the list of subsequent callbacks by modifying the value
 *     of the sc_next field from inside the sc_response() function; this
 *     case does not require any handling from inside slap_response_play()
 *
 * To stop execution of the playlist, the sc_response() function must return
 * a value different from SLAP_SC_CONTINUE.
 *
 * The same applies to slap_cleanup_play(); only, there is no means to stop
 * execution of the playlist, since all cleanup functions must be called.
 */
552
static int
553
slap_response_play(
554
555
556
557
558
	Operation *op,
	SlapReply *rs )
{
	int rc;

559
	slap_callback	*sc = op->o_callback, **scp;
560
561

	rc = SLAP_CB_CONTINUE;
562
563
	for ( scp = &sc; *scp; ) {
		slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
564

565
		op->o_callback = *scp;
566
567
568
		if ( op->o_callback->sc_response ) {
			rc = op->o_callback->sc_response( op, rs );
			if ( op->o_callback == NULL ) {
569
570
571
572
573
574
575
				/* the callback has been removed;
				 * repair the list */
				*scp = sc_next;
				sc_nextp = scp;

			} else if ( op->o_callback != *scp ) {
				/* a new callback has been inserted
576
577
578
				 * in place of the existing one; repair the list */
				*scp = op->o_callback;
				sc_nextp = scp;
579
580
581
			}
			if ( rc != SLAP_CB_CONTINUE ) break;
		}
582
		scp = sc_nextp;
583
584
585
586
587
588
589
	}

	op->o_callback = sc;
	return rc;
}

static int
590
slap_cleanup_play(
591
592
593
	Operation *op,
	SlapReply *rs )
{
594
	slap_callback	*sc = op->o_callback, **scp;
595

596
597
	for ( scp = &sc; *scp; ) {
		slap_callback *sc_next = (*scp)->sc_next, **sc_nextp = &(*scp)->sc_next;
598

599
		op->o_callback = *scp;
600
601
602
		if ( op->o_callback->sc_cleanup ) {
			(void)op->o_callback->sc_cleanup( op, rs );
			if ( op->o_callback == NULL ) {
603
604
605
606
607
608
609
610
				/* the callback has been removed;
				 * repair the list */
				*scp = sc_next;
				sc_nextp = scp;

			} else if ( op->o_callback != *scp ) {
				/* a new callback has been inserted
				 * after the existing one; repair the list */
611
612
613
614
				/* a new callback has been inserted
				 * in place of the existing one; repair the list */
				*scp = op->o_callback;
				sc_nextp = scp;
615
616
617
			}
			/* don't care about the result; do all cleanup */
		}
618
		scp = sc_nextp;
619
620
621
622
623
624
	}

	op->o_callback = sc;
	return LDAP_SUCCESS;
}

625
static int
626
send_ldap_response(
627
628
	Operation *op,
	SlapReply *rs )
629
{
630
631
	BerElementBuffer berbuf;
	BerElement	*ber = (BerElement *) &berbuf;
632
	int		rc = LDAP_SUCCESS;
633
634
	long	bytes;

635
636
	/* op was actually aborted, bypass everything if client didn't Cancel */
	if (( rs->sr_err == SLAPD_ABANDON ) && !op->o_cancel ) {
637
638
		rc = SLAPD_ABANDON;
		goto clean2;
639
640
	}

641
	if ( op->o_callback ) {
642
		rc = slap_response_play( op, rs );
643
644
		if ( rc != SLAP_CB_CONTINUE ) {
			goto clean2;
645
		}
646
	}
647

648
649
650
651
652
653
	/* op completed, connection aborted, bypass sending response */
	if ( op->o_abandon && !op->o_cancel ) {
		rc = SLAPD_ABANDON;
		goto clean2;
	}

654
#ifdef LDAP_CONNECTIONLESS
655
	if (op->o_conn && op->o_conn->c_is_udp)
656
657
658
		ber = op->o_res_ber;
	else
#endif
659
660
	{
		ber_init_w_nullc( ber, LBER_USE_DER );
Howard Chu's avatar
Howard Chu committed
661
		ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
662
	}
663

664
665
666
667
	rc = rs->sr_err;
	if ( rc == SLAPD_ABANDON && op->o_cancel )
		rc = LDAP_CANCELLED;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
668
	Debug( LDAP_DEBUG_TRACE,
Pierangelo Masarati's avatar
Pierangelo Masarati committed
669
		"send_ldap_response: msgid=%d tag=%lu err=%d\n",
670
		rs->sr_msgid, rs->sr_tag, rc );
671

672
	if( rs->sr_ref ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
673
		Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
674
			rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
675
	}
676

677
#ifdef LDAP_CONNECTIONLESS
Kurt Zeilenga's avatar
Kurt Zeilenga committed
678
679
680
	if (op->o_conn && op->o_conn->c_is_udp &&
		op->o_protocol == LDAP_VERSION2 )
	{
681
		rc = ber_printf( ber, "t{ess" /*"}"*/,
682
			rs->sr_tag, rc,
683
684
		rs->sr_matched == NULL ? "" : rs->sr_matched,
		rs->sr_text == NULL ? "" : rs->sr_text );
685
	} else 
686
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
687
688
689
690
691
	if ( rs->sr_type == REP_INTERMEDIATE ) {
	    rc = ber_printf( ber, "{it{" /*"}}"*/,
			rs->sr_msgid, rs->sr_tag );

	} else {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
692
	    rc = ber_printf( ber, "{it{ess" /*"}}"*/,
693
		rs->sr_msgid, rs->sr_tag, rc,
694
695
		rs->sr_matched == NULL ? "" : rs->sr_matched,
		rs->sr_text == NULL ? "" : rs->sr_text );
696
	}
697

Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
698
	if( rc != -1 ) {
699
700
		if ( rs->sr_ref != NULL ) {
			assert( rs->sr_err == LDAP_REFERRAL );
701
			rc = ber_printf( ber, "t{W}",
702
				LDAP_TAG_REFERRAL, rs->sr_ref );
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
703
		} else {
704
			assert( rs->sr_err != LDAP_REFERRAL );
705
		}
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
706
	}
707

708
	if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
709
		rc = ber_printf( ber, "tO",
710
			LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
711
	}
712

Kurt Zeilenga's avatar
Kurt Zeilenga committed
713
714
715
	if( rc != -1 &&
		( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
	{
716
717
		if ( rs->sr_rspoid != NULL ) {
			rc = ber_printf( ber, "ts",
718
719
720
				rs->sr_type == REP_EXTENDED
					? LDAP_TAG_EXOP_RES_OID : LDAP_TAG_IM_RES_OID,
				rs->sr_rspoid );
721
722
723
		}
		if( rc != -1 && rs->sr_rspdata != NULL ) {
			rc = ber_printf( ber, "tO",
724
725
726
				rs->sr_type == REP_EXTENDED
					? LDAP_TAG_EXOP_RES_VALUE : LDAP_TAG_IM_RES_VALUE,
				rs->sr_rspdata );
727
		}
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
728
729
730
	}

	if( rc != -1 ) {
731
		rc = ber_printf( ber, /*"{"*/ "N}" );
732
	}
733

734
735
	if( rc != -1 ) {
		rc = send_ldap_controls( op, ber, rs->sr_ctrls );
736
737
738
739
740
741
	}

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

742
#ifdef LDAP_CONNECTIONLESS
743
744
745
	if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
		&& rc != -1 )
	{
746
747
748
749
		rc = ber_printf( ber, /*"{"*/ "N}" );
	}
#endif
		
750
	if ( rc == -1 ) {
751
		Debug( LDAP_DEBUG_ANY, "ber_printf failed\n" );
752

753
#ifdef LDAP_CONNECTIONLESS
754
		if (!op->o_conn || op->o_conn->c_is_udp == 0)
755
#endif
756
757
758
		{
			ber_free_buf( ber );
		}
759
		goto cleanup;
760
761
762
	}

	/* send BER */
Howard Chu's avatar
Howard Chu committed
763
	bytes = send_ldap_ber( op, ber );
764
#ifdef LDAP_CONNECTIONLESS
765
	if (!op->o_conn || op->o_conn->c_is_udp == 0)
766
#endif
767
768
769
	{
		ber_free_buf( ber );
	}
770
771
772

	if ( bytes < 0 ) {
		Debug( LDAP_DEBUG_ANY,
773
			"send_ldap_response: ber write failed\n" );
774

775
		goto cleanup;
776
777
	}

778
779
780
781
	ldap_pvt_thread_mutex_lock( &op->o_counters->sc_mutex );
	ldap_pvt_mp_add_ulong( op->o_counters->sc_pdu, 1 );
	ldap_pvt_mp_add_ulong( op->o_counters->sc_bytes, (unsigned long)bytes );
	ldap_pvt_thread_mutex_unlock( &op->o_counters->sc_mutex );
782

783
cleanup:;
784
785
786
787
788
	/* Tell caller that we did this for real, as opposed to being
	 * overridden by a callback
	 */
	rc = SLAP_CB_CONTINUE;

789
790
clean2:;
	if ( op->o_callback ) {
791
		(void)slap_cleanup_play( op, rs );
792
793
	}

Hallvard Furuseth's avatar
Hallvard Furuseth committed
794
795
796
797
798
799
	if ( rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
		rs->sr_flags ^= REP_MATCHED_MUSTBEFREED; /* paranoia */
		if ( rs->sr_matched ) {
			free( (char *)rs->sr_matched );
			rs->sr_matched = NULL;
		}
800
801
	}

Hallvard Furuseth's avatar
Hallvard Furuseth committed
802
803
804
805
806
807
	if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
		rs->sr_flags ^= REP_REF_MUSTBEFREED; /* paranoia */
		if ( rs->sr_ref ) {
			ber_bvarray_free( rs->sr_ref );
			rs->sr_ref = NULL;
		}
808
809
	}

810
811
812
	if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
		rs->sr_flags ^= REP_CTRLS_MUSTBEFREED; /* paranoia */
		if ( rs->sr_ctrls ) {
813
			slap_free_ctrls( op, rs->sr_ctrls );
814
815
816
817
			rs->sr_ctrls = NULL;
		}
	}

818
	return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
819
820
}

821

Kurt Zeilenga's avatar
Kurt Zeilenga committed
822
void
823
send_ldap_disconnect( Operation	*op, SlapReply *rs )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
824
{
825
826
827
828
829
#define LDAP_UNSOLICITED_ERROR(e) \
	(  (e) == LDAP_PROTOCOL_ERROR \
	|| (e) == LDAP_STRONG_AUTH_REQUIRED \
	|| (e) == LDAP_UNAVAILABLE )

830
831
	Debug( LDAP_DEBUG_TRACE,
		"send_ldap_disconnect %d:%s\n",
832
		rs->sr_err, rs->sr_text ? rs->sr_text : "" );
833
834
	assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );

835
836
837
838
	/* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */

839
	rs->sr_type = REP_EXTENDED;
840
	rs->sr_rspdata = NULL;
841
842

	if ( op->o_protocol < LDAP_VERSION3 ) {
843
		rs->sr_rspoid = NULL;
844
		rs->sr_tag = slap_req2res( op->o_tag );
845
		rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
846
847

	} else {
848
849
		rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
		rs->sr_tag = LDAP_RES_EXTENDED;
850
		rs->sr_msgid = LDAP_RES_UNSOLICITED;
851
	}
852

853
	if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
Howard Chu's avatar
Howard Chu committed
854
855
856
		ETIME_SETUP;
		StatslogEtime( LDAP_DEBUG_STATS,
			"%s DISCONNECT tag=%lu err=%d "ETIME_LOGFMT"text=%s\n",
857
			op->o_log_prefix, rs->sr_tag, rs->sr_err,
858
			rs->sr_text ? rs->sr_text : "" );
859
	}
860
861
862
}

void
863
slap_send_ldap_result( Operation *op, SlapReply *rs )
864
{
865
	char *tmp = NULL;
866
867
	const char *otext = rs->sr_text;
	BerVarray oref = rs->sr_ref;
868

Howard Chu's avatar
Howard Chu committed
869
870
	rs->sr_type = REP_RESULT;

871
	/* Propagate Abandons so that cleanup callbacks can be processed */
872
	if ( rs->sr_err == SLAPD_ABANDON || op->o_abandon )
873
874
		goto abandon;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
875
	Debug( LDAP_DEBUG_TRACE,
876
		"send_ldap_result: %s p=%d\n",
877
		op->o_log_prefix, op->o_protocol );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
878
879
	Debug( LDAP_DEBUG_ARGS,
		"send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
880
881
		rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
		rs->sr_text ? rs->sr_text : "" );
882
	if( rs->sr_ref ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
883
884
		Debug( LDAP_DEBUG_ARGS,
			"send_ldap_result: referral=\"%s\"\n",
885
			rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
886
	}
887
	assert( !LDAP_API_ERROR( rs->sr_err ) );
888
	assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
889

890
	if ( rs->sr_err == LDAP_REFERRAL ) {
891
892
		if( op->o_domain_scope ) rs->sr_ref = NULL;

893
894
		if( rs->sr_ref == NULL ) {
			rs->sr_err = LDAP_NO_SUCH_OBJECT;
895
		} else if ( op->o_protocol < LDAP_VERSION3 ) {
896
			rs->sr_err = LDAP_PARTIAL_RESULTS;
897
898
899
		}
	}

900
	if ( op->o_protocol < LDAP_VERSION3 ) {
901
902
903
		tmp = v2ref( rs->sr_ref, rs->sr_text );
		rs->sr_text = tmp;
		rs->sr_ref = NULL;
904
905
	}

906
abandon:
907
	rs->sr_tag = slap_req2res( op->o_tag );
908
	rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
909

Hallvard Furuseth's avatar
Hallvard Furuseth committed
910
911
912
913
914
915
916
917
	if ( rs->sr_flags & REP_REF_MUSTBEFREED ) {
		if ( rs->sr_ref == NULL ) {
			rs->sr_flags ^= REP_REF_MUSTBEFREED;
			ber_bvarray_free( oref );
		}
		oref = NULL; /* send_ldap_response() will free rs->sr_ref if != NULL */
	}

918
	if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
Howard Chu's avatar
Howard Chu committed
919
		ETIME_SETUP;
920
		if ( op->o_tag == LDAP_REQ_SEARCH ) {
Howard Chu's avatar
Howard Chu committed
921
922
			StatslogEtime( LDAP_DEBUG_STATS,
				"%s SEARCH RESULT tag=%lu err=%d "ETIME_LOGFMT"nentries=%d text=%s\n",
923
924
				op->o_log_prefix, rs->sr_tag, rs->sr_err,
				rs->sr_nentries, rs->sr_text ? rs->sr_text : "" );
925
		} else {
Howard Chu's avatar
Howard Chu committed
926
927
			StatslogEtime( LDAP_DEBUG_STATS,
				"%s RESULT tag=%lu err=%d "ETIME_LOGFMT"text=%s\n",
928
				op->o_log_prefix, rs->sr_tag, rs->sr_err,
929
				rs->sr_text ? rs->sr_text : "" );
930
		}
931
	}
932

933
	if( tmp != NULL ) ch_free(tmp);
934
935
	rs->sr_text = otext;
	rs->sr_ref = oref;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
936
937
}

938
void
939
send_ldap_sasl( Operation *op, SlapReply *rs )
940
{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
941
	Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
942
		rs->sr_err,
943
		rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1 );
944

945
946
947
	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */

948
	rs->sr_type = REP_SASL;
949
	rs->sr_tag = slap_req2res( op->o_tag );
950
	rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
951

952
	if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
Howard Chu's avatar
Howard Chu committed
953
954
955
		ETIME_SETUP;
		StatslogEtime( LDAP_DEBUG_STATS,
			"%s RESULT tag=%lu err=%d "ETIME_LOGFMT"text=%s\n",
956
			op->o_log_prefix, rs->sr_tag, rs->sr_err,
957
			rs->sr_text ? rs->sr_text : "" );
958
	}
959
960
}

961
void
962
slap_send_ldap_extended( Operation *op, SlapReply *rs )