2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2014 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,
271 #ifdef LDAP_CONNECTIONLESS
276 rc = ldap_create( &ld );
277 if( rc != LDAP_SUCCESS )
281 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
282 if ( rc != LDAP_SUCCESS ) {
283 ldap_ld_free(ld, 1, NULL, NULL);
288 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
289 /* Attach the passed socket as the LDAP's connection */
290 conn = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
292 ldap_unbind_ext( ld, NULL, NULL );
293 return( LDAP_NO_MEMORY );
296 conn->lconn_server = ldap_url_dup( ld->ld_options.ldo_defludp );
297 ber_sockbuf_ctrl( conn->lconn_sb, LBER_SB_OPT_SET_FD, &fd );
298 ld->ld_defconn = conn;
299 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
300 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
305 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
306 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
308 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
309 LBER_SBIOD_LEVEL_PROVIDER, NULL );
312 #ifdef LDAP_CONNECTIONLESS
315 if( ld->ld_options.ldo_peer )
316 ldap_memfree( ld->ld_options.ldo_peer );
317 ld->ld_options.ldo_peer = ldap_memcalloc( 1, sizeof( struct sockaddr_storage ) );
318 len = sizeof( struct sockaddr_storage );
319 if( getpeername ( fd, ld->ld_options.ldo_peer, &len ) < 0) {
320 ldap_unbind_ext( ld, NULL, NULL );
321 return( AC_SOCKET_ERROR );
324 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
325 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
327 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
328 LBER_SBIOD_LEVEL_PROVIDER, NULL );
329 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
330 LBER_SBIOD_LEVEL_PROVIDER, NULL );
332 #endif /* LDAP_CONNECTIONLESS */
336 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
337 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
339 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
340 LBER_SBIOD_LEVEL_PROVIDER, NULL );
344 /* caller must supply sockbuf handlers */
348 ldap_unbind_ext( ld, NULL, NULL );
349 return LDAP_PARAM_ERROR;
353 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
354 INT_MAX, (void *)"ldap_" );
357 /* Add the connection to the *LDAP's select pool */
358 ldap_mark_select_read( ld, conn->lconn_sb );
364 /* Protected by ld_conn_mutex */
366 ldap_int_open_connection(
375 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
377 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
379 rc = ldap_connect_to_host( ld, conn->lconn_sb,
382 if ( rc == -1 ) return rc;
384 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
385 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
387 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
388 LBER_SBIOD_LEVEL_PROVIDER, NULL );
392 #ifdef LDAP_CONNECTIONLESS
395 rc = ldap_connect_to_host( ld, conn->lconn_sb,
398 if ( rc == -1 ) return rc;
400 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
401 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
403 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
404 LBER_SBIOD_LEVEL_PROVIDER, NULL );
406 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
407 LBER_SBIOD_LEVEL_PROVIDER, NULL );
413 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
414 rc = ldap_connect_to_path( ld, conn->lconn_sb,
416 if ( rc == -1 ) return rc;
418 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
419 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
421 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
422 LBER_SBIOD_LEVEL_PROVIDER, NULL );
425 #endif /* LDAP_PF_LOCAL */
431 conn->lconn_created = time( NULL );
434 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
435 INT_MAX, (void *)"ldap_" );
438 #ifdef LDAP_CONNECTIONLESS
439 if( proto == LDAP_PROTO_UDP ) return 0;
443 if (rc == 0 && ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
444 strcmp( srv->lud_scheme, "ldaps" ) == 0 ))
446 ++conn->lconn_refcnt; /* avoid premature free */
448 rc = ldap_int_tls_start( ld, conn, srv );
450 --conn->lconn_refcnt;
452 if (rc != LDAP_SUCCESS) {
462 * ldap_open_internal_connection - open connection and set file descriptor
464 * note: ldap_init_fd() may be preferable
468 ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
475 rc = ldap_create( &ld );
476 if( rc != LDAP_SUCCESS ) {
481 /* Make it appear that a search request, msgid 0, was sent */
482 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
484 ldap_unbind_ext( ld, NULL, NULL );
486 return( LDAP_NO_MEMORY );
488 memset(lr, 0, sizeof( LDAPRequest ));
490 lr->lr_status = LDAP_REQST_INPROGRESS;
491 lr->lr_res_errno = LDAP_SUCCESS;
492 /* no mutex lock needed, we just created this ld here */
493 ld->ld_requests = lr;
495 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
496 /* Attach the passed socket as the *LDAP's connection */
497 c = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
499 ldap_unbind_ext( ld, NULL, NULL );
501 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
502 return( LDAP_NO_MEMORY );
504 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
506 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
507 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
509 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
510 LBER_SBIOD_LEVEL_PROVIDER, NULL );
512 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
514 /* Add the connection to the *LDAP's select pool */
515 ldap_mark_select_read( ld, c->lconn_sb );
517 /* Make this connection an LDAP V3 protocol connection */
519 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &rc );
522 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
524 return( LDAP_SUCCESS );
528 ldap_dup( LDAP *old )
536 Debug( LDAP_DEBUG_TRACE, "ldap_dup\n", 0, 0, 0 );
538 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
542 LDAP_MUTEX_LOCK( &old->ld_ldcmutex );
545 LDAP_MUTEX_UNLOCK( &old->ld_ldcmutex );
550 ldap_int_check_async_open( LDAP *ld, ber_socket_t sd )
552 struct timeval tv = { 0 };
555 rc = ldap_int_poll( ld, sd, &tv, 1 );
558 /* now ready to start tls */
559 ld->ld_defconn->lconn_status = LDAP_CONNST_CONNECTED;
563 ld->ld_errno = LDAP_CONNECT_ERROR;
567 /* connect not completed yet */
568 ld->ld_errno = LDAP_X_CONNECTING;
573 if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
574 !strcmp( ld->ld_defconn->lconn_server->lud_scheme, "ldaps" )) {
576 ++ld->ld_defconn->lconn_refcnt; /* avoid premature free */
578 rc = ldap_int_tls_start( ld, ld->ld_defconn, ld->ld_defconn->lconn_server );
580 --ld->ld_defconn->lconn_refcnt;