2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 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 );
275 conn->lconn_server = ldap_url_dup( ld->ld_options.ldo_defludp );
276 ber_sockbuf_ctrl( conn->lconn_sb, LBER_SB_OPT_SET_FD, &fd );
277 ld->ld_defconn = conn;
278 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
283 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
284 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
286 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
287 LBER_SBIOD_LEVEL_PROVIDER, NULL );
290 #ifdef LDAP_CONNECTIONLESS
293 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
294 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
296 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
297 LBER_SBIOD_LEVEL_PROVIDER, NULL );
298 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
299 LBER_SBIOD_LEVEL_PROVIDER, NULL );
301 #endif /* LDAP_CONNECTIONLESS */
305 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
306 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
308 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
309 LBER_SBIOD_LEVEL_PROVIDER, NULL );
313 /* caller must supply sockbuf handlers */
317 ldap_unbind_ext( ld, NULL, NULL );
318 return LDAP_PARAM_ERROR;
322 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
323 INT_MAX, (void *)"ldap_" );
326 /* Add the connection to the *LDAP's select pool */
327 ldap_mark_select_read( ld, conn->lconn_sb );
328 ldap_mark_select_write( ld, conn->lconn_sb );
335 ldap_int_open_connection(
344 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
346 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
348 rc = ldap_connect_to_host( ld, conn->lconn_sb,
351 if ( rc == -1 ) return rc;
353 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
354 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
356 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
357 LBER_SBIOD_LEVEL_PROVIDER, NULL );
361 #ifdef LDAP_CONNECTIONLESS
364 rc = ldap_connect_to_host( ld, conn->lconn_sb,
367 if ( rc == -1 ) return rc;
369 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
370 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
372 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
373 LBER_SBIOD_LEVEL_PROVIDER, NULL );
375 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
376 LBER_SBIOD_LEVEL_PROVIDER, NULL );
382 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
383 rc = ldap_connect_to_path( ld, conn->lconn_sb,
385 if ( rc == -1 ) return rc;
387 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
388 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
390 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
391 LBER_SBIOD_LEVEL_PROVIDER, NULL );
394 #endif /* LDAP_PF_LOCAL */
400 conn->lconn_created = time( NULL );
403 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
404 INT_MAX, (void *)"ldap_" );
407 #ifdef LDAP_CONNECTIONLESS
408 if( proto == LDAP_PROTO_UDP ) return 0;
412 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
413 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
415 ++conn->lconn_refcnt; /* avoid premature free */
417 rc = ldap_int_tls_start( ld, conn, srv );
419 --conn->lconn_refcnt;
421 if (rc != LDAP_SUCCESS) {
432 ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
438 rc = ldap_create( ldp );
439 if( rc != LDAP_SUCCESS ) {
444 /* Make it appear that a search request, msgid 0, was sent */
445 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
447 ldap_unbind_ext( *ldp, NULL, NULL );
449 return( LDAP_NO_MEMORY );
451 memset(lr, 0, sizeof( LDAPRequest ));
453 lr->lr_status = LDAP_REQST_INPROGRESS;
454 lr->lr_res_errno = LDAP_SUCCESS;
455 /* no mutex lock needed, we just created this ld here */
456 (*ldp)->ld_requests = lr;
458 /* Attach the passed socket as the *LDAP's connection */
459 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
461 ldap_unbind_ext( *ldp, NULL, NULL );
463 return( LDAP_NO_MEMORY );
465 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
467 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
468 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
470 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
471 LBER_SBIOD_LEVEL_PROVIDER, NULL );
472 (*ldp)->ld_defconn = c;
474 /* Add the connection to the *LDAP's select pool */
475 ldap_mark_select_read( *ldp, c->lconn_sb );
476 ldap_mark_select_write( *ldp, c->lconn_sb );
478 /* Make this connection an LDAP V3 protocol connection */
480 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
482 return( LDAP_SUCCESS );