2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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_connect - Connect to an ldap server.
58 * ldap_initialize( &ld, url );
62 ldap_connect( LDAP *ld )
64 ber_socket_t sd = AC_SOCKET_INVALID;
65 int rc = LDAP_SUCCESS;
67 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
68 if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd ) == -1 ) {
69 rc = ldap_open_defconn( ld );
71 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
77 * ldap_open - initialize and connect to an ldap server. A magic cookie to
78 * be used for future communication is returned on success, NULL on failure.
79 * "host" may be a space-separated list of hosts or IP addresses
83 * ld = ldap_open( hostname, port );
87 ldap_open( LDAP_CONST char *host, int port )
92 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
95 ld = ldap_init( host, port );
100 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
101 rc = ldap_open_defconn( ld );
102 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
105 ldap_ld_free( ld, 0, NULL, NULL );
109 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
110 ld != NULL ? "succeeded" : "failed", 0, 0 );
118 ldap_create( LDAP **ldp )
121 struct ldapoptions *gopts;
124 /* Get pointer to global option structure */
125 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
126 return LDAP_NO_MEMORY;
129 /* Initialize the global options, if not already done. */
130 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
131 ldap_int_initialize(gopts, NULL);
132 if ( gopts->ldo_valid != LDAP_INITIALIZED )
133 return LDAP_LOCAL_ERROR;
136 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
138 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
139 return( LDAP_NO_MEMORY );
142 if ( (ld->ldc = (struct ldap_common *) LDAP_CALLOC( 1,
143 sizeof(struct ldap_common) )) == NULL ) {
144 LDAP_FREE( (char *)ld );
145 return( LDAP_NO_MEMORY );
147 /* copy the global options */
148 LDAP_MUTEX_LOCK( &gopts->ldo_mutex );
149 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
150 #ifdef LDAP_R_COMPILE
151 /* Properly initialize the structs mutex */
152 ldap_pvt_thread_mutex_init( &(ld->ld_ldopts_mutex) );
154 LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
156 ld->ld_valid = LDAP_VALID_SESSION;
158 /* but not pointers to malloc'ed items */
159 ld->ld_options.ldo_sctrls = NULL;
160 ld->ld_options.ldo_cctrls = NULL;
161 ld->ld_options.ldo_defludp = NULL;
162 ld->ld_options.ldo_conn_cbs = NULL;
164 #ifdef HAVE_CYRUS_SASL
165 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
166 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
167 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
168 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
169 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
170 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
171 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
172 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
176 /* We explicitly inherit the SSL_CTX, don't need the names/paths. Leave
177 * them empty to allow new SSL_CTX's to be created from scratch.
179 memset( &ld->ld_options.ldo_tls_info, 0,
180 sizeof( ld->ld_options.ldo_tls_info ));
181 ld->ld_options.ldo_tls_ctx = NULL;
184 if ( gopts->ldo_defludp ) {
185 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
187 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
190 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
192 ld->ld_lberoptions = LBER_USE_DER;
194 ld->ld_sb = ber_sockbuf_alloc( );
195 if ( ld->ld_sb == NULL ) goto nomem;
197 #ifdef LDAP_R_COMPILE
198 ldap_pvt_thread_mutex_init( &ld->ld_msgid_mutex );
199 ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
200 ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
201 ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
202 ldap_pvt_thread_mutex_init( &ld->ld_abandon_mutex );
203 ldap_pvt_thread_mutex_init( &ld->ld_ldcmutex );
205 ld->ld_ldcrefcnt = 1;
210 ldap_free_select_info( ld->ld_selectinfo );
211 ldap_free_urllist( ld->ld_options.ldo_defludp );
212 #ifdef HAVE_CYRUS_SASL
213 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
214 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
215 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
216 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
218 LDAP_FREE( (char *)ld );
219 return LDAP_NO_MEMORY;
223 * ldap_init - initialize the LDAP library. A magic cookie to be used for
224 * future communication is returned on success, NULL on failure.
225 * "host" may be a space-separated list of hosts or IP addresses
229 * ld = ldap_init( host, port );
232 ldap_init( LDAP_CONST char *defhost, int defport )
237 rc = ldap_create(&ld);
238 if ( rc != LDAP_SUCCESS )
242 ld->ld_options.ldo_defport = defport;
244 if (defhost != NULL) {
245 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
246 if ( rc != LDAP_SUCCESS ) {
247 ldap_ld_free(ld, 1, NULL, NULL);
257 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
263 rc = ldap_create(&ld);
264 if ( rc != LDAP_SUCCESS )
268 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
269 if ( rc != LDAP_SUCCESS ) {
270 ldap_ld_free(ld, 1, NULL, NULL);
273 #ifdef LDAP_CONNECTIONLESS
274 if (ldap_is_ldapc_url(url))
287 LDAP_CONST char *url,
294 #ifdef LDAP_CONNECTIONLESS
299 rc = ldap_create( &ld );
300 if( rc != LDAP_SUCCESS )
304 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
305 if ( rc != LDAP_SUCCESS ) {
306 ldap_ld_free(ld, 1, NULL, NULL);
311 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
312 /* Attach the passed socket as the LDAP's connection */
313 conn = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
315 ldap_unbind_ext( ld, NULL, NULL );
316 return( LDAP_NO_MEMORY );
319 conn->lconn_server = ldap_url_dup( ld->ld_options.ldo_defludp );
320 ber_sockbuf_ctrl( conn->lconn_sb, LBER_SB_OPT_SET_FD, &fd );
321 ld->ld_defconn = conn;
322 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
323 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
328 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
329 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
331 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
332 LBER_SBIOD_LEVEL_PROVIDER, NULL );
335 #ifdef LDAP_CONNECTIONLESS
338 if( ld->ld_options.ldo_peer )
339 ldap_memfree( ld->ld_options.ldo_peer );
340 ld->ld_options.ldo_peer = ldap_memcalloc( 1, sizeof( struct sockaddr_storage ) );
341 len = sizeof( struct sockaddr_storage );
342 if( getpeername ( fd, ld->ld_options.ldo_peer, &len ) < 0) {
343 ldap_unbind_ext( ld, NULL, NULL );
344 return( AC_SOCKET_ERROR );
347 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
348 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
350 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
351 LBER_SBIOD_LEVEL_PROVIDER, NULL );
352 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
353 LBER_SBIOD_LEVEL_PROVIDER, NULL );
355 #endif /* LDAP_CONNECTIONLESS */
359 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
360 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
362 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
363 LBER_SBIOD_LEVEL_PROVIDER, NULL );
367 /* caller must supply sockbuf handlers */
371 ldap_unbind_ext( ld, NULL, NULL );
372 return LDAP_PARAM_ERROR;
376 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
377 INT_MAX, (void *)"ldap_" );
380 /* Add the connection to the *LDAP's select pool */
381 ldap_mark_select_read( ld, conn->lconn_sb );
387 /* Protected by ld_conn_mutex */
389 ldap_int_open_connection(
398 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
400 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
402 rc = ldap_connect_to_host( 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 *)"tcp_" );
410 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
411 LBER_SBIOD_LEVEL_PROVIDER, NULL );
415 #ifdef LDAP_CONNECTIONLESS
418 rc = ldap_connect_to_host( ld, conn->lconn_sb,
421 if ( rc == -1 ) return rc;
423 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
424 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
426 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
427 LBER_SBIOD_LEVEL_PROVIDER, NULL );
429 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
430 LBER_SBIOD_LEVEL_PROVIDER, NULL );
436 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
437 rc = ldap_connect_to_path( ld, conn->lconn_sb,
439 if ( rc == -1 ) return rc;
441 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
442 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
444 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
445 LBER_SBIOD_LEVEL_PROVIDER, NULL );
448 #endif /* LDAP_PF_LOCAL */
454 conn->lconn_created = time( NULL );
457 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
458 INT_MAX, (void *)"ldap_" );
461 #ifdef LDAP_CONNECTIONLESS
462 if( proto == LDAP_PROTO_UDP ) return 0;
466 if (rc == 0 && ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
467 strcmp( srv->lud_scheme, "ldaps" ) == 0 ))
469 ++conn->lconn_refcnt; /* avoid premature free */
471 rc = ldap_int_tls_start( ld, conn, srv );
473 --conn->lconn_refcnt;
475 if (rc != LDAP_SUCCESS) {
476 /* process connection callbacks */
478 struct ldapoptions *lo;
482 lo = &ld->ld_options;
483 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
484 if ( lo->ldo_conn_cbs ) {
485 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
487 cb->lc_del( ld, conn->lconn_sb, cb );
490 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
491 lo = LDAP_INT_GLOBAL_OPT();
492 LDAP_MUTEX_LOCK( &lo->ldo_mutex );
493 if ( lo->ldo_conn_cbs ) {
494 for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
496 cb->lc_del( ld, conn->lconn_sb, cb );
499 LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
510 * ldap_open_internal_connection - open connection and set file descriptor
512 * note: ldap_init_fd() may be preferable
516 ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
523 rc = ldap_create( &ld );
524 if( rc != LDAP_SUCCESS ) {
529 /* Make it appear that a search request, msgid 0, was sent */
530 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
532 ldap_unbind_ext( ld, NULL, NULL );
534 return( LDAP_NO_MEMORY );
536 memset(lr, 0, sizeof( LDAPRequest ));
538 lr->lr_status = LDAP_REQST_INPROGRESS;
539 lr->lr_res_errno = LDAP_SUCCESS;
540 /* no mutex lock needed, we just created this ld here */
541 ld->ld_requests = lr;
543 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
544 /* Attach the passed socket as the *LDAP's connection */
545 c = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
547 ldap_unbind_ext( ld, NULL, NULL );
549 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
550 return( LDAP_NO_MEMORY );
552 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
554 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
555 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
557 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
558 LBER_SBIOD_LEVEL_PROVIDER, NULL );
560 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
562 /* Add the connection to the *LDAP's select pool */
563 ldap_mark_select_read( ld, c->lconn_sb );
565 /* Make this connection an LDAP V3 protocol connection */
567 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &rc );
570 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
572 return( LDAP_SUCCESS );
576 ldap_dup( LDAP *old )
584 Debug( LDAP_DEBUG_TRACE, "ldap_dup\n", 0, 0, 0 );
586 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
590 LDAP_MUTEX_LOCK( &old->ld_ldcmutex );
593 LDAP_MUTEX_UNLOCK( &old->ld_ldcmutex );
598 ldap_int_check_async_open( LDAP *ld, ber_socket_t sd )
600 struct timeval tv = { 0 };
603 rc = ldap_int_poll( ld, sd, &tv, 1 );
606 /* now ready to start tls */
607 ld->ld_defconn->lconn_status = LDAP_CONNST_CONNECTED;
611 ld->ld_errno = LDAP_CONNECT_ERROR;
615 /* connect not completed yet */
616 ld->ld_errno = LDAP_X_CONNECTING;
621 if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
622 !strcmp( ld->ld_defconn->lconn_server->lud_scheme, "ldaps" )) {
624 ++ld->ld_defconn->lconn_refcnt; /* avoid premature free */
626 rc = ldap_int_tls_start( ld, ld->ld_defconn, ld->ld_defconn->lconn_server );
628 --ld->ld_defconn->lconn_refcnt;