2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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(
343 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
345 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
347 rc = ldap_connect_to_host( ld, conn->lconn_sb,
350 if ( rc == -1 ) return rc;
352 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
353 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
355 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
356 LBER_SBIOD_LEVEL_PROVIDER, NULL );
360 #ifdef LDAP_CONNECTIONLESS
363 rc = ldap_connect_to_host( ld, conn->lconn_sb,
366 if ( rc == -1 ) return rc;
368 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
369 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
371 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
372 LBER_SBIOD_LEVEL_PROVIDER, NULL );
374 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
375 LBER_SBIOD_LEVEL_PROVIDER, NULL );
381 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
382 rc = ldap_connect_to_path( ld, conn->lconn_sb,
384 if ( rc == -1 ) return rc;
386 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
387 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
389 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
390 LBER_SBIOD_LEVEL_PROVIDER, NULL );
393 #endif /* LDAP_PF_LOCAL */
399 conn->lconn_created = time( NULL );
402 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
403 INT_MAX, (void *)"ldap_" );
406 #ifdef LDAP_CONNECTIONLESS
407 if( proto == LDAP_PROTO_UDP ) return 0;
411 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
412 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
414 ++conn->lconn_refcnt; /* avoid premature free */
416 rc = ldap_int_tls_start( ld, conn, srv );
418 --conn->lconn_refcnt;
420 if (rc != LDAP_SUCCESS) {
431 ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
437 rc = ldap_create( ldp );
438 if( rc != LDAP_SUCCESS ) {
443 /* Make it appear that a search request, msgid 0, was sent */
444 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
446 ldap_unbind_ext( *ldp, NULL, NULL );
448 return( LDAP_NO_MEMORY );
450 memset(lr, 0, sizeof( LDAPRequest ));
452 lr->lr_status = LDAP_REQST_INPROGRESS;
453 lr->lr_res_errno = LDAP_SUCCESS;
454 /* no mutex lock needed, we just created this ld here */
455 (*ldp)->ld_requests = lr;
457 /* Attach the passed socket as the *LDAP's connection */
458 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
460 ldap_unbind_ext( *ldp, NULL, NULL );
462 return( LDAP_NO_MEMORY );
464 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
466 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
467 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
469 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
470 LBER_SBIOD_LEVEL_PROVIDER, NULL );
471 (*ldp)->ld_defconn = c;
473 /* Add the connection to the *LDAP's select pool */
474 ldap_mark_select_read( *ldp, c->lconn_sb );
475 ldap_mark_select_write( *ldp, c->lconn_sb );
477 /* Make this connection an LDAP V3 protocol connection */
479 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
481 return( LDAP_SUCCESS );