request.c 22.6 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/*
2
3
4
5
 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */
/*  Portions
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
8
9
10
11
 *  Copyright (c) 1995 Regents of the University of Michigan.
 *  All rights reserved.
 *
 *  request.c - sending of ldap requests; handling of referrals
 */

12
13
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
14
15
#include <stdio.h>
#include <stdlib.h>
16
17
18
19
20
21
22

#include <ac/errno.h>
#include <ac/socket.h>
#include <ac/string.h>
#include <ac/time.h>
#include <ac/unistd.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
23
24
25
26
27
#include "lber.h"
#include "ldap.h"
#include "ldap-int.h"

#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
28
29
30
static LDAPConn *find_connection LDAP_P(( LDAP *ld, LDAPServer *srv, int any ));
static void use_connection LDAP_P(( LDAP *ld, LDAPConn *lc ));
static void free_servers LDAP_P(( LDAPServer *srvlist ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
31
32
33
34
#endif /* LDAP_REFERRALS || LDAP_DNS */


#ifdef LDAP_DNS
35
static LDAPServer *dn2servers LDAP_P(( LDAP *ld, char *dn ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
36
37
38
#endif /* LDAP_DNS */

#ifdef LDAP_REFERRALS
39
40
static BerElement *re_encode_request LDAP_P(( LDAP *ld, BerElement *origber,
    int msgid, char **dnp ));
Kurt Zeilenga's avatar
Kurt Zeilenga committed
41
42
43
44
#endif /* LDAP_REFERRALS */


BerElement *
45
ldap_alloc_ber_with_options( LDAP *ld )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
46
47
48
49
50
51
52
{
	BerElement	*ber;

    	if (( ber = ber_alloc_t( ld->ld_lberoptions )) == NULLBER ) {
		ld->ld_errno = LDAP_NO_MEMORY;
#ifdef STR_TRANSLATION
	} else {
53
		ldap_set_ber_options( ld, ber );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
54
55
56
57
58
59
60
61
#endif /* STR_TRANSLATION */
	}

	return( ber );
}


void
62
ldap_set_ber_options( LDAP *ld, BerElement *ber )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
63
64
65
66
67
68
69
70
71
72
73
74
{
	ber->ber_options = ld->ld_lberoptions;
#ifdef STR_TRANSLATION
	if (( ld->ld_lberoptions & LBER_TRANSLATE_STRINGS ) != 0 ) {
		ber_set_string_translators( ber,
		    ld->ld_lber_encode_translate_proc,
		    ld->ld_lber_decode_translate_proc );
	}
#endif /* STR_TRANSLATION */
}

int
75
ldap_delayed_open( LDAP *ld )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
76
77
{
#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
78
	LDAPServer	*servers, *srv;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
79
80
#endif /* LDAP_REFERRALS || LDAP_DNS */

81
	Debug( LDAP_DEBUG_TRACE, "ldap_delayed_open\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
82

Kurt Zeilenga's avatar
Kurt Zeilenga committed
83
84
85
86
87
88
	if ( ld->ld_sb.sb_sd == -1 ) {
		/* not connected yet */

#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
		if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
		    NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
Kurt Zeilenga's avatar
Kurt Zeilenga committed
89
		    ldap_strdup( ld->ld_defhost )) == NULL ))
Kurt Zeilenga's avatar
Kurt Zeilenga committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
		{
			if (srv != NULL) free( srv );
			ld->ld_errno = LDAP_NO_MEMORY;
			return( -1 );
		}

		srv->lsrv_port = ld->ld_defport;

		if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 ))
			== NULL )
		{
			if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
			free( (char *)srv );
			ld->ld_errno = LDAP_SERVER_DOWN;
			return( -1 );
		}
		++ld->ld_defconn->lconn_refcnt;	/* so it never gets closed/freed */

#else /* LDAP_REFERRALS */
		if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
		    ld->ld_defport, &ld->ld_host, 0 ) < 0 )
		{
			ldap_ld_free( ld, 0 );
			ld->ld_errno = LDAP_SERVER_DOWN;
			return( -1 );
		}
#endif /* LDAP_REFERRALS */
	}

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
	return( 0 );
}

int
ldap_send_initial_request( LDAP *ld, unsigned long msgtype, char *dn,
	BerElement *ber )
{
#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
	LDAPServer	*servers, *srv;
#endif /* LDAP_REFERRALS || LDAP_DNS */

	Debug( LDAP_DEBUG_TRACE, "ldap_send_initial_request\n", 0, 0, 0 );

	if ( ldap_delayed_open( ld ) < 0 ) {
		return( -1 );
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
135

Kurt Zeilenga's avatar
Kurt Zeilenga committed
136
#if !defined( LDAP_REFERRALS ) && !defined( LDAP_DNS )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
137

Kurt Zeilenga's avatar
Kurt Zeilenga committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
	if ( ber_flush( &ld->ld_sb, ber, 1 ) != 0 ) {
		ld->ld_errno = LDAP_SERVER_DOWN;
		return( -1 );
	}

	ld->ld_errno = LDAP_SUCCESS;
	return( ld->ld_msgid );
#else /* !LDAP_REFERRALS && !LDAP_DNS */

#ifdef LDAP_DNS
	if (( ld->ld_options & LDAP_OPT_DNS ) != 0 && ldap_is_dns_dn( dn )) {
		if (( servers = dn2servers( ld, dn )) == NULL ) {
			ber_free( ber, 1 );
			return( -1 );
		}

#ifdef LDAP_DEBUG
		if ( ldap_debug & LDAP_DEBUG_TRACE ) {
			LDAPServer	*srv;

			for ( srv = servers; srv != NULL;
			    srv = srv->lsrv_next ) {
				fprintf( stderr,
				    "LDAP server %s:  dn %s, port %d\n",
				    srv->lsrv_host, ( srv->lsrv_dn == NULL ) ?
				    "(default)" : srv->lsrv_dn,
				    srv->lsrv_port );
			}
		}
#endif /* LDAP_DEBUG */
	} else {
#endif /* LDAP_DNS */
		/*
		 * use of DNS is turned off or this is an X.500 DN...
		 * use our default connection
		 */
		servers = NULL;
#ifdef LDAP_DNS
	}	
#endif /* LDAP_DNS */

179
	return( ldap_send_server_request( ld, ber, ld->ld_msgid, NULL, servers,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
180
181
182
183
184
185
186
187
	    NULL, 0 ));
#endif /* !LDAP_REFERRALS && !LDAP_DNS */
}



#if defined( LDAP_REFERRALS ) || defined( LDAP_DNS )
int
188
ldap_send_server_request( LDAP *ld, BerElement *ber, int msgid, LDAPRequest
Kurt Zeilenga's avatar
Kurt Zeilenga committed
189
190
191
	*parentreq, LDAPServer *srvlist, LDAPConn *lc, int bind )
{
	LDAPRequest	*lr;
192
	int incparent;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
193

194
	Debug( LDAP_DEBUG_TRACE, "ldap_send_server_request\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
195

196
	incparent = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
197
198
199
200
201
202
203
204
	ld->ld_errno = LDAP_SUCCESS;	/* optimistic */

	if ( lc == NULL ) {
		if ( srvlist == NULL ) {
			lc = ld->ld_defconn;
		} else {
			if (( lc = find_connection( ld, srvlist, 1 )) ==
			    NULL ) {
205
206
207
208
209
210
				if ( bind && (parentreq != NULL) ) {
					/* Remember the bind in the parent */
					incparent = 1;
					++parentreq->lr_outrefcnt;
				}
				lc = ldap_new_connection( ld, &srvlist, 0, 1, bind );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
211
212
213
214
215
216
217
218
219
220
			}
			free_servers( srvlist );
		}
	}

	if ( lc == NULL || lc->lconn_status != LDAP_CONNST_CONNECTED ) {
		ber_free( ber, 1 );
		if ( ld->ld_errno == LDAP_SUCCESS ) {
			ld->ld_errno = LDAP_SERVER_DOWN;
		}
221
222
223
224
		if ( incparent ) {
			/* Forget about the bind */
			--parentreq->lr_outrefcnt; 
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
225
226
227
228
229
230
231
		return( -1 );
	}

	use_connection( ld, lc );
	if (( lr = (LDAPRequest *)calloc( 1, sizeof( LDAPRequest ))) ==
	    NULL ) {
		ld->ld_errno = LDAP_NO_MEMORY;
232
		ldap_free_connection( ld, lc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
233
		ber_free( ber, 1 );
234
235
236
237
		if ( incparent ) {
			/* Forget about the bind */
			--parentreq->lr_outrefcnt; 
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
238
239
240
241
242
243
244
245
		return( -1 );
	} 
	lr->lr_msgid = msgid;
	lr->lr_status = LDAP_REQST_INPROGRESS;
	lr->lr_res_errno = LDAP_SUCCESS;	/* optimistic */
	lr->lr_ber = ber;
	lr->lr_conn = lc;
	if ( parentreq != NULL ) {	/* sub-request */
246
247
248
249
		if ( !incparent ) { 
			/* Increment if we didn't do it before the bind */
			++parentreq->lr_outrefcnt;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
		lr->lr_origid = parentreq->lr_origid;
		lr->lr_parentcnt = parentreq->lr_parentcnt + 1;
		lr->lr_parent = parentreq;
		lr->lr_refnext = parentreq->lr_refnext;
		parentreq->lr_refnext = lr;
	} else {			/* original request */
		lr->lr_origid = lr->lr_msgid;
	}

	if (( lr->lr_next = ld->ld_requests ) != NULL ) {
		lr->lr_next->lr_prev = lr;
	}
	ld->ld_requests = lr;
	lr->lr_prev = NULL;

	if ( ber_flush( lc->lconn_sb, ber, 0 ) != 0 ) {
#ifdef notyet
		if ( errno == EWOULDBLOCK ) {
			/* need to continue write later */
			lr->lr_status = LDAP_REQST_WRITING;
270
			ldap_mark_select_write( ld, lc->lconn_sb );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
271
272
273
		} else {
#else /* notyet */
			ld->ld_errno = LDAP_SERVER_DOWN;
274
275
			ldap_free_request( ld, lr );
			ldap_free_connection( ld, lc, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
276
277
278
279
280
281
282
283
284
285
286
287
			return( -1 );
#endif /* notyet */
#ifdef notyet
		}
#endif /* notyet */
	} else {
		if ( parentreq == NULL ) {
			ber->ber_end = ber->ber_ptr;
			ber->ber_ptr = ber->ber_buf;
		}

		/* sent -- waiting for a response */
288
		ldap_mark_select_read( ld, lc->lconn_sb );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
289
290
291
292
293
294
295
296
	}

	ld->ld_errno = LDAP_SUCCESS;
	return( msgid );
}


LDAPConn *
297
ldap_new_connection( LDAP *ld, LDAPServer **srvlistp, int use_ldsb,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
298
299
300
301
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
336
337
338
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
	int connect, int bind )
{
	LDAPConn	*lc;
	LDAPServer	*prevsrv, *srv;
	Sockbuf		*sb;

	/*
	 * make a new LDAP server connection
	 * XXX open connection synchronously for now
	 */
	if (( lc = (LDAPConn *)calloc( 1, sizeof( LDAPConn ))) == NULL ||
	    ( !use_ldsb && ( sb = (Sockbuf *)calloc( 1, sizeof( Sockbuf )))
	    == NULL )) {
		if ( lc != NULL ) {
			free( (char *)lc );
		}
		ld->ld_errno = LDAP_NO_MEMORY;
		return( NULL );
	}

	lc->lconn_sb = ( use_ldsb ) ? &ld->ld_sb : sb;

	if ( connect ) {
		prevsrv = NULL;

		for ( srv = *srvlistp; srv != NULL; srv = srv->lsrv_next ) {
			if ( open_ldap_connection( ld, lc->lconn_sb,
			    srv->lsrv_host, srv->lsrv_port,
			    &lc->lconn_krbinstance, 0 ) != -1 ) {
				break;
			}
			prevsrv = srv;
		}

		if ( srv == NULL ) {
		    if ( !use_ldsb ) {
			free( (char *)lc->lconn_sb );
		    }
		    free( (char *)lc );
		    ld->ld_errno = LDAP_SERVER_DOWN;
		    return( NULL );
		}

		if ( prevsrv == NULL ) {
		    *srvlistp = srv->lsrv_next;
		} else {
		    prevsrv->lsrv_next = srv->lsrv_next;
		}
		lc->lconn_server = srv;
	}

	lc->lconn_status = LDAP_CONNST_CONNECTED;
	lc->lconn_next = ld->ld_conns;
	ld->ld_conns = lc;

	/*
	 * XXX for now, we always do a synchronous bind.  This will have
	 * to change in the long run...
	 */
	if ( bind ) {
		int		err, freepasswd, authmethod;
		char		*binddn, *passwd;
		LDAPConn	*savedefconn;

		freepasswd = err = 0;

		if ( ld->ld_rebindproc == NULL ) {
			binddn = passwd = "";
			authmethod = LDAP_AUTH_SIMPLE;
		} else {
			if (( err = (*ld->ld_rebindproc)( ld, &binddn, &passwd,
			    &authmethod, 0 )) == LDAP_SUCCESS ) {
				freepasswd = 1;
			} else {
				ld->ld_errno = err;
				err = -1;
			}
		}


		if ( err == 0 ) {
			savedefconn = ld->ld_defconn;
			ld->ld_defconn = lc;
			++lc->lconn_refcnt;	/* avoid premature free */

			if ( ldap_bind_s( ld, binddn, passwd, authmethod ) !=
			    LDAP_SUCCESS ) {
				err = -1;
			}
			--lc->lconn_refcnt;
			ld->ld_defconn = savedefconn;
		}

		if ( freepasswd ) {
			(*ld->ld_rebindproc)( ld, &binddn, &passwd,
				&authmethod, 1 );
		}

		if ( err != 0 ) {
397
			ldap_free_connection( ld, lc, 1, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
			lc = NULL;
		}
	}

	return( lc );
}


static LDAPConn *
find_connection( LDAP *ld, LDAPServer *srv, int any )
/*
 * return an existing connection (if any) to the server srv
 * if "any" is non-zero, check for any server in the "srv" chain
 */
{
	LDAPConn	*lc;
	LDAPServer	*ls;

	for ( lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next ) {
		for ( ls = srv; ls != NULL; ls = ls->lsrv_next ) {
			if ( lc->lconn_server->lsrv_host != NULL &&
			    ls->lsrv_host != NULL && strcasecmp(
			    ls->lsrv_host, lc->lconn_server->lsrv_host ) == 0
			    && ls->lsrv_port == lc->lconn_server->lsrv_port ) {
				return( lc );
			}
			if ( !any ) {
				break;
			}
		}
	}

	return( NULL );
}



static void
use_connection( LDAP *ld, LDAPConn *lc )
{
	++lc->lconn_refcnt;
	lc->lconn_lastused = time( 0 );
}


void
444
ldap_free_connection( LDAP *ld, LDAPConn *lc, int force, int unbind )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
445
446
447
{
	LDAPConn	*tmplc, *prevlc;

448
	Debug( LDAP_DEBUG_TRACE, "ldap_free_connection\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
449
450
451

	if ( force || --lc->lconn_refcnt <= 0 ) {
		if ( lc->lconn_status == LDAP_CONNST_CONNECTED ) {
452
			ldap_mark_select_clear( ld, lc->lconn_sb );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
453
			if ( unbind ) {
454
				ldap_send_unbind( ld, lc->lconn_sb );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
455
			}
456
			ldap_close_connection( lc->lconn_sb );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
			if ( lc->lconn_sb->sb_ber.ber_buf != NULL ) {
				free( lc->lconn_sb->sb_ber.ber_buf );
			}
		}
		prevlc = NULL;
		for ( tmplc = ld->ld_conns; tmplc != NULL;
		    tmplc = tmplc->lconn_next ) {
			if ( tmplc == lc ) {
				if ( prevlc == NULL ) {
				    ld->ld_conns = tmplc->lconn_next;
				} else {
				    prevlc->lconn_next = tmplc->lconn_next;
				}
				break;
			}
472
			prevlc = tmplc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
473
474
475
476
477
478
479
480
481
		}
		free_servers( lc->lconn_server );
		if ( lc->lconn_krbinstance != NULL ) {
			free( lc->lconn_krbinstance );
		}
		if ( lc->lconn_sb != &ld->ld_sb ) {
			free( (char *)lc->lconn_sb );
		}
		free( lc );
482
		Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: actually freed\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
483
484
485
		    0, 0, 0 );
	} else {
		lc->lconn_lastused = time( 0 );
486
		Debug( LDAP_DEBUG_TRACE, "ldap_free_connection: refcnt %d\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
487
488
489
490
491
492
493
		    lc->lconn_refcnt, 0, 0 );
	}
}


#ifdef LDAP_DEBUG
void
494
ldap_dump_connection( LDAP *ld, LDAPConn *lconns, int all )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
{
	LDAPConn	*lc;

	fprintf( stderr, "** Connection%s:\n", all ? "s" : "" );
	for ( lc = lconns; lc != NULL; lc = lc->lconn_next ) {
		if ( lc->lconn_server != NULL ) {
			fprintf( stderr, "* host: %s  port: %d%s\n",
			    ( lc->lconn_server->lsrv_host == NULL ) ? "(null)"
			    : lc->lconn_server->lsrv_host,
			    lc->lconn_server->lsrv_port, ( lc->lconn_sb ==
			    &ld->ld_sb ) ? "  (default)" : "" );
		}
		fprintf( stderr, "  refcnt: %d  status: %s\n", lc->lconn_refcnt,
		    ( lc->lconn_status == LDAP_CONNST_NEEDSOCKET ) ?
		    "NeedSocket" : ( lc->lconn_status ==
		    LDAP_CONNST_CONNECTING ) ? "Connecting" : "Connected" );
		fprintf( stderr, "  last used: %s\n",
		    ctime( &lc->lconn_lastused ));
		if ( !all ) {
			break;
		}
	}
}


void
521
ldap_dump_requests_and_responses( LDAP *ld )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
522
523
524
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
551
552
553
554
555
556
557
558
559
560
561
{
	LDAPRequest	*lr;
	LDAPMessage	*lm, *l;

	fprintf( stderr, "** Outstanding Requests:\n" );
	if (( lr = ld->ld_requests ) == NULL ) {
		fprintf( stderr, "   Empty\n" );
	}
	for ( ; lr != NULL; lr = lr->lr_next ) {
	    fprintf( stderr, " * msgid %d,  origid %d, status %s\n",
		lr->lr_msgid, lr->lr_origid, ( lr->lr_status ==
		LDAP_REQST_INPROGRESS ) ? "InProgress" :
		( lr->lr_status == LDAP_REQST_CHASINGREFS ) ? "ChasingRefs" :
		( lr->lr_status == LDAP_REQST_NOTCONNECTED ) ? "NotConnected" :
		"Writing" );
	    fprintf( stderr, "   outstanding referrals %d, parent count %d\n",
		    lr->lr_outrefcnt, lr->lr_parentcnt );
	}

	fprintf( stderr, "** Response Queue:\n" );
	if (( lm = ld->ld_responses ) == NULLMSG ) {
		fprintf( stderr, "   Empty\n" );
	}
	for ( ; lm != NULLMSG; lm = lm->lm_next ) {
		fprintf( stderr, " * msgid %d,  type %d\n",
		    lm->lm_msgid, lm->lm_msgtype );
		if (( l = lm->lm_chain ) != NULL ) {
			fprintf( stderr, "   chained responses:\n" );
			for ( ; l != NULLMSG; l = l->lm_chain ) {
				fprintf( stderr,
				    "  * msgid %d,  type %d\n",
				    l->lm_msgid, l->lm_msgtype );
			}
		}
	}
}
#endif /* LDAP_DEBUG */


void
562
ldap_free_request( LDAP *ld, LDAPRequest *lr )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
563
564
565
{
	LDAPRequest	*tmplr, *nextlr;

566
	Debug( LDAP_DEBUG_TRACE, "ldap_free_request (origid %d, msgid %d)\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
567
568
569
570
571
572
573
574
		lr->lr_origid, lr->lr_msgid, 0 );

	if ( lr->lr_parent != NULL ) {
		--lr->lr_parent->lr_outrefcnt;
	} else {
		/* free all referrals (child requests) */
		for ( tmplr = lr->lr_refnext; tmplr != NULL; tmplr = nextlr ) {
			nextlr = tmplr->lr_refnext;
575
			ldap_free_request( ld, tmplr );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
		}
	}

	if ( lr->lr_prev == NULL ) {
		ld->ld_requests = lr->lr_next;
	} else {
		lr->lr_prev->lr_next = lr->lr_next;
	}

	if ( lr->lr_next != NULL ) {
		lr->lr_next->lr_prev = lr->lr_prev;
	}

	if ( lr->lr_ber != NULL ) {
		ber_free( lr->lr_ber, 1 );
	}

	if ( lr->lr_res_error != NULL ) {
		free( lr->lr_res_error );
	}

	if ( lr->lr_res_matched != NULL ) {
		free( lr->lr_res_matched );
	}

	free( lr );
}


static void
free_servers( LDAPServer *srvlist )
{
    LDAPServer	*nextsrv;

    while ( srvlist != NULL ) {
	nextsrv = srvlist->lsrv_next;
	if ( srvlist->lsrv_dn != NULL ) {
		free( srvlist->lsrv_dn );
	}
	if ( srvlist->lsrv_host != NULL ) {
		free( srvlist->lsrv_host );
	}
	free( srvlist );
	srvlist = nextsrv;
    }
}
#endif /* LDAP_REFERRALS || LDAP_DNS */


#ifdef LDAP_REFERRALS
/*
 * XXX merging of errors in this routine needs to be improved
 */
int
630
ldap_chase_referrals( LDAP *ld, LDAPRequest *lr, char **errstrp, int *hadrefp )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
631
632
633
634
635
636
637
638
639
640
{
	int		rc, count, len, newdn;
#ifdef LDAP_DNS
	int		ldapref;
#endif /* LDAP_DNS */
	char		*p, *ports, *ref, *tmpref, *refdn, *unfollowed;
	LDAPRequest	*origreq;
	LDAPServer	*srv;
	BerElement	*ber;

641
	Debug( LDAP_DEBUG_TRACE, "ldap_chase_referrals\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711

	ld->ld_errno = LDAP_SUCCESS;	/* optimistic */
	*hadrefp = 0;

	if ( *errstrp == NULL ) {
		return( 0 );
	}

	len = strlen( *errstrp );
	for ( p = *errstrp; len >= LDAP_REF_STR_LEN; ++p, --len ) {
		if (( *p == 'R' || *p == 'r' ) && strncasecmp( p,
		    LDAP_REF_STR, LDAP_REF_STR_LEN ) == 0 ) {
			*p = '\0';
			p += LDAP_REF_STR_LEN;
			break;
		}
	}

	if ( len < LDAP_REF_STR_LEN ) {
		return( 0 );
	}

	if ( lr->lr_parentcnt >= ld->ld_refhoplimit ) {
		Debug( LDAP_DEBUG_ANY,
		    "more than %d referral hops (dropping)\n",
		    ld->ld_refhoplimit, 0, 0 );
		    /* XXX report as error in ld->ld_errno? */
		    return( 0 );
	}

	/* find original request */
	for ( origreq = lr; origreq->lr_parent != NULL;
	     origreq = origreq->lr_parent ) {
		;
	}

	unfollowed = NULL;
	rc = count = 0;

	/* parse out & follow referrals */
	for ( ref = p; rc == 0 && ref != NULL; ref = p ) {
#ifdef LDAP_DNS
		ldapref = 0;
#endif /* LDAP_DNS */

		if (( p = strchr( ref, '\n' )) != NULL ) {
			*p++ = '\0';
		} else {
			p = NULL;
		}

		len = strlen( ref );
		if ( len > LDAP_LDAP_REF_STR_LEN && strncasecmp( ref,
		    LDAP_LDAP_REF_STR, LDAP_LDAP_REF_STR_LEN ) == 0 ) {
			Debug( LDAP_DEBUG_TRACE,
			    "chasing LDAP referral: <%s>\n", ref, 0, 0 );
#ifdef LDAP_DNS
			ldapref = 1;
#endif /* LDAP_DNS */
			tmpref = ref + LDAP_LDAP_REF_STR_LEN;
#ifdef LDAP_DNS
		} else if ( len > LDAP_DX_REF_STR_LEN && strncasecmp( ref,
		    LDAP_DX_REF_STR, LDAP_DX_REF_STR_LEN ) == 0 ) {
			Debug( LDAP_DEBUG_TRACE,
			    "chasing DX referral: <%s>\n", ref, 0, 0 );
			tmpref = ref + LDAP_DX_REF_STR_LEN;
#endif /* LDAP_DNS */
		} else {
			Debug( LDAP_DEBUG_TRACE,
			    "ignoring unknown referral <%s>\n", ref, 0, 0 );
712
			rc = ldap_append_referral( ld, &unfollowed, ref );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
			*hadrefp = 1;
			continue;
		}

		*hadrefp = 1;
		if (( refdn = strchr( tmpref, '/' )) != NULL ) {
			*refdn++ = '\0';
			newdn = 1;
		} else {
			newdn = 0;
		}

		if (( ber = re_encode_request( ld, origreq->lr_ber,
		    ++ld->ld_msgid, &refdn )) == NULL ) {
			return( -1 );
		}

#ifdef LDAP_DNS
		if ( ldapref ) {
#endif /* LDAP_DNS */
			if (( srv = (LDAPServer *)calloc( 1,
			    sizeof( LDAPServer ))) == NULL ) {
				ber_free( ber, 1 );
				ld->ld_errno = LDAP_NO_MEMORY;
				return( -1 );
			}

740
			if (( srv->lsrv_host = ldap_strdup( tmpref )) == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
741
742
743
744
745
746
747
748
749
750
				free( (char *)srv );
				ber_free( ber, 1 );
				ld->ld_errno = LDAP_NO_MEMORY;
				return( -1 );
			}

			if (( ports = strchr( srv->lsrv_host, ':' )) != NULL ) {
				*ports++ = '\0';
				srv->lsrv_port = atoi( ports );
			} else {
751
				srv->lsrv_port = openldap_ldap_global_options.ldo_defport;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
752
753
754
755
756
757
758
			}
#ifdef LDAP_DNS
		} else {
			srv = dn2servers( ld, tmpref );
		}
#endif /* LDAP_DNS */

759
		if ( srv != NULL && ldap_send_server_request( ld, ber, ld->ld_msgid,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
760
761
762
763
764
765
		    lr, srv, NULL, 1 ) >= 0 ) {
			++count;
		} else {
			Debug( LDAP_DEBUG_ANY,
			    "Unable to chase referral (%s)\n", 
			    ldap_err2string( ld->ld_errno ), 0, 0 );
766
			rc = ldap_append_referral( ld, &unfollowed, ref );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
		}

		if ( !newdn && refdn != NULL ) {
			free( refdn );
		}
	}

	free( *errstrp );
	*errstrp = unfollowed;

	return(( rc == 0 ) ? count : rc );
}


int
782
ldap_append_referral( LDAP *ld, char **referralsp, char *s )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
{
	int	first;

	if ( *referralsp == NULL ) {
		first = 1;
		*referralsp = (char *)malloc( strlen( s ) + LDAP_REF_STR_LEN
		    + 1 );
	} else {
		first = 0;
		*referralsp = (char *)realloc( *referralsp,
		    strlen( *referralsp ) + strlen( s ) + 2 );
	}

	if ( *referralsp == NULL ) {
		ld->ld_errno = LDAP_NO_MEMORY;
		return( -1 );
	}

	if ( first ) {
		strcpy( *referralsp, LDAP_REF_STR );
	} else {
		strcat( *referralsp, "\n" );
	}
	strcat( *referralsp, s );

	return( 0 );
}



static BerElement *
re_encode_request( LDAP *ld, BerElement *origber, int msgid, char **dnp )
{
/*
 * XXX this routine knows way too much about how the lber library works!
 */
	unsigned long	along, tag;
	long		ver;
	int		rc;
	BerElement	tmpber, *ber;
	char		*orig_dn;

	Debug( LDAP_DEBUG_TRACE,
	    "re_encode_request: new msgid %d, new dn <%s>\n",
	    msgid, ( *dnp == NULL ) ? "NONE" : *dnp, 0 );

	tmpber = *origber;

	/*
832
833
834
835
	 * all LDAP requests are sequences that start with a message id.
	 * For all except delete, this is followed by a sequence that is
	 * tagged with the operation code.  For delete, the provided DN
	 * is not wrapped by a sequence.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
836
	 */
837
838
839
840
	rc = ber_scanf( &tmpber, "{it", /*}*/ &along, &tag );

	if ( rc == LBER_ERROR ) {
		ld->ld_errno = LDAP_DECODING_ERROR;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
841
842
843
		return( NULL );
	}

844
845
846
	if ( tag == LDAP_REQ_BIND ) {
		/* bind requests have a version number before the DN & other stuff */
		rc = ber_scanf( &tmpber, "{ia" /*}*/, &ver, &orig_dn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
847

848
849
850
851
852
853
	} else if ( tag == LDAP_REQ_DELETE ) {
		/* delete requests don't have a DN wrapping sequence */
		rc = ber_scanf( &tmpber, "a", &orig_dn );

	} else {
		rc = ber_scanf( &tmpber, "{a" /*}*/, &orig_dn );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
854
855
	}

856
857
858
	if( rc == LBER_ERROR ) {
		ld->ld_errno = LDAP_DECODING_ERROR;
		return NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
859
860
861
862
863
864
865
866
	}

	if ( *dnp == NULL ) {
		*dnp = orig_dn;
	} else {
		free( orig_dn );
	}

867
868
869
870
	if (( ber = ldap_alloc_ber_with_options( ld )) == NULL ) {
		return( NULL );
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
871
	if ( tag == LDAP_REQ_BIND ) {
872
873
874
		rc = ber_printf( ber, "{it{is" /*}}*/, msgid, tag, ver, *dnp );
	} else if ( tag == LDAP_REQ_DELETE ) {
		rc = ber_printf( ber, "{its}", msgid, tag, *dnp );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
875
876
877
878
879
	} else {
		rc = ber_printf( ber, "{it{s", msgid, tag, *dnp );
	}

	if ( rc == -1 ) {
880
		ld->ld_errno = LDAP_ENCODING_ERROR;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
881
882
883
884
		ber_free( ber, 1 );
		return( NULL );
	}

885
886
887
888
889
	if ( tag != LDAP_REQ_DELETE && (
		ber_write(ber, tmpber.ber_ptr, ( tmpber.ber_end - tmpber.ber_ptr ), 0)
		!= ( tmpber.ber_end - tmpber.ber_ptr ) ||
	    ber_printf( ber, /*{{*/ "}}" ) == -1 ) )
	{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
		ld->ld_errno = LDAP_ENCODING_ERROR;
		ber_free( ber, 1 );
		return( NULL );
	}

#ifdef LDAP_DEBUG
	if ( ldap_debug & LDAP_DEBUG_PACKETS ) {
		Debug( LDAP_DEBUG_ANY, "re_encode_request new request is:\n",
		    0, 0, 0 );
		ber_dump( ber, 0 );
	}
#endif /* LDAP_DEBUG */

	return( ber );
}


LDAPRequest *
908
ldap_find_request_by_msgid( LDAP *ld, int msgid )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
{
    	LDAPRequest	*lr;

	for ( lr = ld->ld_requests; lr != NULL; lr = lr->lr_next ) {
		if ( msgid == lr->lr_msgid ) {
			break;
		}
	}

	return( lr );
}
#endif /* LDAP_REFERRALS */


#ifdef LDAP_DNS
static LDAPServer *
dn2servers( LDAP *ld, char *dn )	/* dn can also be a domain.... */
{
	char		*p, *domain, *host, *server_dn, **dxs;
	int		i, port;
	LDAPServer	*srvlist, *prevsrv, *srv;

	if (( domain = strrchr( dn, '@' )) != NULL ) {
		++domain;
	} else {
		domain = dn;
	}

937
	if (( dxs = ldap_getdxbyname( domain )) == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
938
939
940
941
942
943
944
		ld->ld_errno = LDAP_NO_MEMORY;
		return( NULL );
	}

	srvlist = NULL;

	for ( i = 0; dxs[ i ] != NULL; ++i ) {
945
		port = openldap_ldap_global_options.ldo_defport;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
		server_dn = NULL;
		if ( strchr( dxs[ i ], ':' ) == NULL ) {
			host = dxs[ i ];
		} else if ( strlen( dxs[ i ] ) >= 7 &&
		    strncmp( dxs[ i ], "ldap://", 7 ) == 0 ) {
			host = dxs[ i ] + 7;
			if (( p = strchr( host, ':' )) == NULL ) {
				p = host;
			} else {
				*p++ = '\0';
				port = atoi( p );
			}
			if (( p = strchr( p, '/' )) != NULL ) {
				server_dn = ++p;
				if ( *server_dn == '\0' ) {
					server_dn = NULL;
				}
			}
		} else {
			host = NULL;
		}

		if ( host != NULL ) {	/* found a server we can use */
			if (( srv = (LDAPServer *)calloc( 1,
			    sizeof( LDAPServer ))) == NULL ) {
				free_servers( srvlist );
				srvlist = NULL;
				break;		/* exit loop & return */
			}

			/* add to end of list of servers */
			if ( srvlist == NULL ) {
				srvlist = srv;
			} else {
				prevsrv->lsrv_next = srv;
			}
			prevsrv = srv;
			
			/* copy in info. */
985
			if (( srv->lsrv_host = ldap_strdup( host )) == NULL ||
Kurt Zeilenga's avatar
Kurt Zeilenga committed
986
			    ( server_dn != NULL && ( srv->lsrv_dn =
987
			    ldap_strdup( server_dn )) == NULL )) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
988
989
990
991
992
993
994
995
996
997
998
999
1000
				free_servers( srvlist );
				srvlist = NULL;
				break;		/* exit loop & return */
			}
			srv->lsrv_port = port;
		}
	}

	ldap_value_free( dxs );

	if ( srvlist == NULL ) {
		ld->ld_errno = LDAP_SERVER_DOWN;
	}
For faster browsing, not all history is shown. View entire blame