2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2011 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
26 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
33 #include <ac/unistd.h>
38 /* Caller must hold the conn_mutex since simultaneous accesses are possible */
39 int ldap_open_defconn( LDAP *ld )
41 ld->ld_defconn = ldap_new_connection( ld,
42 &ld->ld_options.ldo_defludp, 1, 1, NULL, 0, 0 );
44 if( ld->ld_defconn == NULL ) {
45 ld->ld_errno = LDAP_SERVER_DOWN;
49 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
54 * ldap_open - initialize and connect to an ldap server. A magic cookie to
55 * be used for future communication is returned on success, NULL on failure.
56 * "host" may be a space-separated list of hosts or IP addresses
60 * ld = ldap_open( hostname, port );
64 ldap_open( LDAP_CONST char *host, int port )
69 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
72 ld = ldap_init( host, port );
77 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
78 rc = ldap_open_defconn( ld );
79 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
82 ldap_ld_free( ld, 0, NULL, NULL );
86 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
87 ld != NULL ? "succeeded" : "failed", 0, 0 );
95 ldap_create( LDAP **ldp )
98 struct ldapoptions *gopts;
101 /* Get pointer to global option structure */
102 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
103 return LDAP_NO_MEMORY;
106 /* Initialize the global options, if not already done. */
107 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
108 ldap_int_initialize(gopts, NULL);
109 if ( gopts->ldo_valid != LDAP_INITIALIZED )
110 return LDAP_LOCAL_ERROR;
113 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
115 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
116 return( LDAP_NO_MEMORY );
119 if ( (ld->ldc = (struct ldap_common *) LDAP_CALLOC( 1,
120 sizeof(struct ldap_common) )) == NULL ) {
121 LDAP_FREE( (char *)ld );
122 return( LDAP_NO_MEMORY );
124 /* copy the global options */
125 LDAP_MUTEX_LOCK( &gopts->ldo_mutex );
126 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
127 #ifdef LDAP_R_COMPILE
128 /* Properly initialize the structs mutex */
129 ldap_pvt_thread_mutex_init( &(ld->ld_ldopts_mutex) );
131 LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
133 ld->ld_valid = LDAP_VALID_SESSION;
135 /* but not pointers to malloc'ed items */
136 ld->ld_options.ldo_sctrls = NULL;
137 ld->ld_options.ldo_cctrls = NULL;
138 ld->ld_options.ldo_defludp = NULL;
139 ld->ld_options.ldo_conn_cbs = NULL;
141 #ifdef HAVE_CYRUS_SASL
142 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
143 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
144 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
145 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
146 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
147 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
148 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
149 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
153 /* We explicitly inherit the SSL_CTX, don't need the names/paths. Leave
154 * them empty to allow new SSL_CTX's to be created from scratch.
156 memset( &ld->ld_options.ldo_tls_info, 0,
157 sizeof( ld->ld_options.ldo_tls_info ));
158 ld->ld_options.ldo_tls_ctx = NULL;
161 if ( gopts->ldo_defludp ) {
162 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
164 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
167 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
169 ld->ld_lberoptions = LBER_USE_DER;
171 ld->ld_sb = ber_sockbuf_alloc( );
172 if ( ld->ld_sb == NULL ) goto nomem;
174 #ifdef LDAP_R_COMPILE
175 ldap_pvt_thread_mutex_init( &ld->ld_msgid_mutex );
176 ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
177 ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
178 ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
179 ldap_pvt_thread_mutex_init( &ld->ld_abandon_mutex );
180 ldap_pvt_thread_mutex_init( &ld->ld_ldcmutex );
182 ld->ld_ldcrefcnt = 1;
187 ldap_free_select_info( ld->ld_selectinfo );
188 ldap_free_urllist( ld->ld_options.ldo_defludp );
189 #ifdef HAVE_CYRUS_SASL
190 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
191 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
192 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
193 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
195 LDAP_FREE( (char *)ld );
196 return LDAP_NO_MEMORY;
200 * ldap_init - initialize the LDAP library. A magic cookie to be used for
201 * future communication is returned on success, NULL on failure.
202 * "host" may be a space-separated list of hosts or IP addresses
206 * ld = ldap_init( host, port );
209 ldap_init( LDAP_CONST char *defhost, int defport )
214 rc = ldap_create(&ld);
215 if ( rc != LDAP_SUCCESS )
219 ld->ld_options.ldo_defport = defport;
221 if (defhost != NULL) {
222 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
223 if ( rc != LDAP_SUCCESS ) {
224 ldap_ld_free(ld, 1, NULL, NULL);
234 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
240 rc = ldap_create(&ld);
241 if ( rc != LDAP_SUCCESS )
245 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
246 if ( rc != LDAP_SUCCESS ) {
247 ldap_ld_free(ld, 1, NULL, NULL);
250 #ifdef LDAP_CONNECTIONLESS
251 if (ldap_is_ldapc_url(url))
264 LDAP_CONST char *url,
273 rc = ldap_create( &ld );
274 if( rc != LDAP_SUCCESS )
278 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
279 if ( rc != LDAP_SUCCESS ) {
280 ldap_ld_free(ld, 1, NULL, NULL);
285 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
286 /* Attach the passed socket as the LDAP's connection */
287 conn = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
289 ldap_unbind_ext( ld, NULL, NULL );
290 return( LDAP_NO_MEMORY );
293 conn->lconn_server = ldap_url_dup( ld->ld_options.ldo_defludp );
294 ber_sockbuf_ctrl( conn->lconn_sb, LBER_SB_OPT_SET_FD, &fd );
295 ld->ld_defconn = conn;
296 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
297 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
302 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
303 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
305 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
306 LBER_SBIOD_LEVEL_PROVIDER, NULL );
309 #ifdef LDAP_CONNECTIONLESS
312 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
313 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
315 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
316 LBER_SBIOD_LEVEL_PROVIDER, NULL );
317 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
318 LBER_SBIOD_LEVEL_PROVIDER, NULL );
320 #endif /* LDAP_CONNECTIONLESS */
324 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
325 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
327 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
328 LBER_SBIOD_LEVEL_PROVIDER, NULL );
332 /* caller must supply sockbuf handlers */
336 ldap_unbind_ext( ld, NULL, NULL );
337 return LDAP_PARAM_ERROR;
341 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
342 INT_MAX, (void *)"ldap_" );
345 /* Add the connection to the *LDAP's select pool */
346 ldap_mark_select_read( ld, conn->lconn_sb );
347 ldap_mark_select_write( ld, conn->lconn_sb );
353 /* Protected by ld_conn_mutex */
355 ldap_int_open_connection(
364 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
366 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
368 rc = ldap_connect_to_host( ld, conn->lconn_sb,
371 if ( rc == -1 ) return rc;
373 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
374 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
376 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
377 LBER_SBIOD_LEVEL_PROVIDER, NULL );
381 #ifdef LDAP_CONNECTIONLESS
384 rc = ldap_connect_to_host( ld, conn->lconn_sb,
387 if ( rc == -1 ) return rc;
389 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
390 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
392 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
393 LBER_SBIOD_LEVEL_PROVIDER, NULL );
395 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
396 LBER_SBIOD_LEVEL_PROVIDER, NULL );
402 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
403 rc = ldap_connect_to_path( ld, conn->lconn_sb,
405 if ( rc == -1 ) return rc;
407 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
408 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
410 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
411 LBER_SBIOD_LEVEL_PROVIDER, NULL );
414 #endif /* LDAP_PF_LOCAL */
420 conn->lconn_created = time( NULL );
423 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
424 INT_MAX, (void *)"ldap_" );
427 #ifdef LDAP_CONNECTIONLESS
428 if( proto == LDAP_PROTO_UDP ) return 0;
432 if (rc == 0 && ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
433 strcmp( srv->lud_scheme, "ldaps" ) == 0 ))
435 ++conn->lconn_refcnt; /* avoid premature free */
437 rc = ldap_int_tls_start( ld, conn, srv );
439 --conn->lconn_refcnt;
441 if (rc != LDAP_SUCCESS) {
451 * ldap_open_internal_connection - open connection and set file descriptor
453 * note: ldap_init_fd() may be preferable
457 ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
464 rc = ldap_create( &ld );
465 if( rc != LDAP_SUCCESS ) {
470 /* Make it appear that a search request, msgid 0, was sent */
471 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
473 ldap_unbind_ext( ld, NULL, NULL );
475 return( LDAP_NO_MEMORY );
477 memset(lr, 0, sizeof( LDAPRequest ));
479 lr->lr_status = LDAP_REQST_INPROGRESS;
480 lr->lr_res_errno = LDAP_SUCCESS;
481 /* no mutex lock needed, we just created this ld here */
482 ld->ld_requests = lr;
484 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
485 /* Attach the passed socket as the *LDAP's connection */
486 c = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
488 ldap_unbind_ext( ld, NULL, NULL );
490 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
491 return( LDAP_NO_MEMORY );
493 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
495 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
496 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
498 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
499 LBER_SBIOD_LEVEL_PROVIDER, NULL );
501 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
503 /* Add the connection to the *LDAP's select pool */
504 ldap_mark_select_read( ld, c->lconn_sb );
505 ldap_mark_select_write( ld, c->lconn_sb );
507 /* Make this connection an LDAP V3 protocol connection */
509 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &rc );
512 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
514 return( LDAP_SUCCESS );
518 ldap_dup( LDAP *old )
526 Debug( LDAP_DEBUG_TRACE, "ldap_dup\n", 0, 0, 0 );
528 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
532 LDAP_MUTEX_LOCK( &old->ld_ldcmutex );
535 LDAP_MUTEX_UNLOCK( &old->ld_ldcmutex );
540 ldap_int_check_async_open( LDAP *ld, ber_socket_t sd )
542 struct timeval tv = { 0 };
545 rc = ldap_int_poll( ld, sd, &tv );
548 /* now ready to start tls */
549 ld->ld_defconn->lconn_status = LDAP_CONNST_CONNECTED;
553 ld->ld_errno = LDAP_CONNECT_ERROR;
557 /* connect not completed yet */
558 ld->ld_errno = LDAP_X_CONNECTING;
563 if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
564 !strcmp( ld->ld_defconn->lconn_server->lud_scheme, "ldaps" )) {
566 ++ld->ld_defconn->lconn_refcnt; /* avoid premature free */
568 rc = ldap_int_tls_start( ld, ld->ld_defconn, ld->ld_defconn->lconn_server );
570 --ld->ld_defconn->lconn_refcnt;