2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 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 should hold the req_mutex if 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 );
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 rc = ldap_open_defconn( ld );
80 ldap_ld_free( ld, 0, NULL, NULL );
84 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
85 ld != NULL ? "succeeded" : "failed", 0, 0 );
93 ldap_create( LDAP **ldp )
96 struct ldapoptions *gopts;
99 /* Get pointer to global option structure */
100 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
101 return LDAP_NO_MEMORY;
104 /* Initialize the global options, if not already done. */
105 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
106 ldap_int_initialize(gopts, NULL);
107 if ( gopts->ldo_valid != LDAP_INITIALIZED )
108 return LDAP_LOCAL_ERROR;
111 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
113 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
114 return( LDAP_NO_MEMORY );
117 /* copy the global options */
118 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
120 ld->ld_valid = LDAP_VALID_SESSION;
122 /* but not pointers to malloc'ed items */
123 ld->ld_options.ldo_sctrls = NULL;
124 ld->ld_options.ldo_cctrls = NULL;
125 ld->ld_options.ldo_defludp = NULL;
126 ld->ld_options.ldo_conn_cbs = NULL;
128 #ifdef HAVE_CYRUS_SASL
129 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
130 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
131 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
132 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
133 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
134 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
135 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
136 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
140 /* We explicitly inherit the SSL_CTX, don't need the names/paths. Leave
141 * them empty to allow new SSL_CTX's to be created from scratch.
143 memset( &ld->ld_options.ldo_tls_info, 0,
144 sizeof( ld->ld_options.ldo_tls_info ));
145 ld->ld_options.ldo_tls_ctx = NULL;
148 if ( gopts->ldo_defludp ) {
149 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
151 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
154 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
156 ld->ld_lberoptions = LBER_USE_DER;
158 ld->ld_sb = ber_sockbuf_alloc( );
159 if ( ld->ld_sb == NULL ) goto nomem;
161 #ifdef LDAP_R_COMPILE
162 ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
163 ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
164 ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
170 ldap_free_select_info( ld->ld_selectinfo );
171 ldap_free_urllist( ld->ld_options.ldo_defludp );
172 #ifdef HAVE_CYRUS_SASL
173 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
174 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
175 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
176 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
178 LDAP_FREE( (char *)ld );
179 return LDAP_NO_MEMORY;
183 * ldap_init - initialize the LDAP library. A magic cookie to be used for
184 * future communication is returned on success, NULL on failure.
185 * "host" may be a space-separated list of hosts or IP addresses
189 * ld = ldap_init( host, port );
192 ldap_init( LDAP_CONST char *defhost, int defport )
197 rc = ldap_create(&ld);
198 if ( rc != LDAP_SUCCESS )
202 ld->ld_options.ldo_defport = defport;
204 if (defhost != NULL) {
205 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
206 if ( rc != LDAP_SUCCESS ) {
207 ldap_ld_free(ld, 1, NULL, NULL);
217 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
223 rc = ldap_create(&ld);
224 if ( rc != LDAP_SUCCESS )
228 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
229 if ( rc != LDAP_SUCCESS ) {
230 ldap_ld_free(ld, 1, NULL, NULL);
233 #ifdef LDAP_CONNECTIONLESS
234 if (ldap_is_ldapc_url(url))
247 LDAP_CONST char *url,
256 rc = ldap_create( &ld );
257 if( rc != LDAP_SUCCESS )
261 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
262 if ( rc != LDAP_SUCCESS ) {
263 ldap_ld_free(ld, 1, NULL, NULL);
268 /* Attach the passed socket as the LDAP's connection */
269 conn = ldap_new_connection( ld, NULL, 1, 0, NULL);
271 ldap_unbind_ext( ld, NULL, NULL );
272 return( LDAP_NO_MEMORY );
274 ber_sockbuf_ctrl( conn->lconn_sb, LBER_SB_OPT_SET_FD, &fd );
275 ld->ld_defconn = conn;
276 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
281 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
282 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
284 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
285 LBER_SBIOD_LEVEL_PROVIDER, NULL );
288 #ifdef LDAP_CONNECTIONLESS
291 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
292 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
294 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
295 LBER_SBIOD_LEVEL_PROVIDER, NULL );
296 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
297 LBER_SBIOD_LEVEL_PROVIDER, NULL );
299 #endif /* LDAP_CONNECTIONLESS */
303 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
304 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
306 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
307 LBER_SBIOD_LEVEL_PROVIDER, NULL );
311 /* caller must supply sockbuf handlers */
315 ldap_unbind_ext( ld, NULL, NULL );
316 return LDAP_PARAM_ERROR;
320 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
321 INT_MAX, (void *)"ldap_" );
324 /* Add the connection to the *LDAP's select pool */
325 ldap_mark_select_read( ld, conn->lconn_sb );
326 ldap_mark_select_write( ld, conn->lconn_sb );
333 ldap_int_open_connection(
342 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
344 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
346 rc = ldap_connect_to_host( ld, conn->lconn_sb,
349 if ( rc == -1 ) return rc;
351 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
352 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
354 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
355 LBER_SBIOD_LEVEL_PROVIDER, NULL );
359 #ifdef LDAP_CONNECTIONLESS
362 rc = ldap_connect_to_host( ld, conn->lconn_sb,
365 if ( rc == -1 ) return rc;
367 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
368 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
370 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
371 LBER_SBIOD_LEVEL_PROVIDER, NULL );
373 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
374 LBER_SBIOD_LEVEL_PROVIDER, NULL );
380 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
381 rc = ldap_connect_to_path( ld, conn->lconn_sb,
383 if ( rc == -1 ) return rc;
385 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
386 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
388 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
389 LBER_SBIOD_LEVEL_PROVIDER, NULL );
392 #endif /* LDAP_PF_LOCAL */
398 conn->lconn_created = time( NULL );
401 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
402 INT_MAX, (void *)"ldap_" );
405 #ifdef LDAP_CONNECTIONLESS
406 if( proto == LDAP_PROTO_UDP ) return 0;
410 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
411 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
413 ++conn->lconn_refcnt; /* avoid premature free */
415 rc = ldap_int_tls_start( ld, conn, srv );
417 --conn->lconn_refcnt;
419 if (rc != LDAP_SUCCESS) {
430 ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
436 rc = ldap_create( ldp );
437 if( rc != LDAP_SUCCESS ) {
442 /* Make it appear that a search request, msgid 0, was sent */
443 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
445 ldap_unbind_ext( *ldp, NULL, NULL );
447 return( LDAP_NO_MEMORY );
449 memset(lr, 0, sizeof( LDAPRequest ));
451 lr->lr_status = LDAP_REQST_INPROGRESS;
452 lr->lr_res_errno = LDAP_SUCCESS;
453 /* no mutex lock needed, we just created this ld here */
454 (*ldp)->ld_requests = lr;
456 /* Attach the passed socket as the *LDAP's connection */
457 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
459 ldap_unbind_ext( *ldp, NULL, NULL );
461 return( LDAP_NO_MEMORY );
463 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
465 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
466 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
468 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
469 LBER_SBIOD_LEVEL_PROVIDER, NULL );
470 (*ldp)->ld_defconn = c;
472 /* Add the connection to the *LDAP's select pool */
473 ldap_mark_select_read( *ldp, c->lconn_sb );
474 ldap_mark_select_write( *ldp, c->lconn_sb );
476 /* Make this connection an LDAP V3 protocol connection */
478 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
480 return( LDAP_SUCCESS );