3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
18 #include <ac/stdlib.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
25 #include <ac/unistd.h>
30 int ldap_open_defconn( LDAP *ld )
32 ld->ld_defconn = ldap_new_connection( ld,
33 ld->ld_options.ldo_defludp, 1, 1, NULL );
35 if( ld->ld_defconn == NULL ) {
36 ld->ld_errno = LDAP_SERVER_DOWN;
40 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
45 * ldap_open - initialize and connect to an ldap server. A magic cookie to
46 * be used for future communication is returned on success, NULL on failure.
47 * "host" may be a space-separated list of hosts or IP addresses
51 * ld = ldap_open( hostname, port );
55 ldap_open( LDAP_CONST char *host, int port )
61 LDAP_LOG ( CONNECTION, ARGS, "ldap_open(%s, %d)\n", host, port, 0 );
63 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
67 ld = ldap_init( host, port );
72 rc = ldap_open_defconn( ld );
75 ldap_ld_free( ld, 0, NULL, NULL );
80 LDAP_LOG ( CONNECTION, RESULTS, "ldap_open: %s\n",
81 ld == NULL ? "succeeded" : "failed", 0, 0 );
83 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
84 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;
112 LDAP_LOG ( CONNECTION, ENTRY, "ldap_create\n", 0, 0, 0 );
114 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
117 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
118 return( LDAP_NO_MEMORY );
121 /* copy the global options */
122 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
124 ld->ld_valid = LDAP_VALID_SESSION;
126 /* but not pointers to malloc'ed items */
127 ld->ld_options.ldo_sctrls = NULL;
128 ld->ld_options.ldo_cctrls = NULL;
130 #ifdef HAVE_CYRUS_SASL
131 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
132 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
133 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
134 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
135 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
136 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
137 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
138 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
141 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
143 if ( ld->ld_options.ldo_defludp == NULL ) {
144 LDAP_FREE( (char*)ld );
145 return LDAP_NO_MEMORY;
148 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
149 ldap_free_urllist( ld->ld_options.ldo_defludp );
150 LDAP_FREE( (char*) ld );
151 return LDAP_NO_MEMORY;
154 ld->ld_lberoptions = LBER_USE_DER;
156 ld->ld_sb = ber_sockbuf_alloc( );
157 if ( ld->ld_sb == NULL ) {
158 ldap_free_urllist( ld->ld_options.ldo_defludp );
159 LDAP_FREE( (char*) ld );
160 return LDAP_NO_MEMORY;
163 #ifdef LDAP_R_COMPILE
164 ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
165 ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
172 * ldap_init - initialize the LDAP library. A magic cookie to be used for
173 * future communication is returned on success, NULL on failure.
174 * "host" may be a space-separated list of hosts or IP addresses
178 * ld = ldap_init( host, port );
181 ldap_init( LDAP_CONST char *defhost, int defport )
186 rc = ldap_create(&ld);
187 if ( rc != LDAP_SUCCESS )
191 ld->ld_options.ldo_defport = defport;
193 if (defhost != NULL) {
194 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
195 if ( rc != LDAP_SUCCESS ) {
196 ldap_ld_free(ld, 1, NULL, NULL);
206 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
212 rc = ldap_create(&ld);
213 if ( rc != LDAP_SUCCESS )
217 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
218 if ( rc != LDAP_SUCCESS ) {
219 ldap_ld_free(ld, 1, NULL, NULL);
222 #ifdef LDAP_CONNECTIONLESS
223 if (ldap_is_ldapc_url(url))
233 ldap_int_open_connection(
244 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_open_connection\n", 0, 0, 0 );
246 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
249 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
251 port = srv->lud_port;
253 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
256 host = srv->lud_host;
260 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
267 rc = ldap_connect_to_host( ld, conn->lconn_sb,
268 proto, host, port, async );
270 if ( rc == -1 ) return rc;
273 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
274 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
276 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
277 LBER_SBIOD_LEVEL_PROVIDER, NULL );
281 #ifdef LDAP_CONNECTIONLESS
283 port = srv->lud_port;
285 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
288 host = srv->lud_host;
291 if( !port ) port = LDAP_PORT;
294 rc = ldap_connect_to_host( ld, conn->lconn_sb,
295 proto, host, port, async );
297 if ( rc == -1 ) return rc;
299 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
300 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
302 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
303 LBER_SBIOD_LEVEL_PROVIDER, NULL );
305 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
306 LBER_SBIOD_LEVEL_PROVIDER, NULL );
312 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
313 rc = ldap_connect_to_path( ld, conn->lconn_sb,
314 srv->lud_host, async );
315 if ( rc == -1 ) return rc;
317 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
318 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
320 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
321 LBER_SBIOD_LEVEL_PROVIDER, NULL );
324 #endif /* LDAP_PF_LOCAL */
331 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
332 INT_MAX, (void *)"ldap_" );
335 #ifdef LDAP_CONNECTIONLESS
336 if( proto == LDAP_PROTO_UDP ) return 0;
340 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
341 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
343 ++conn->lconn_refcnt; /* avoid premature free */
345 rc = ldap_int_tls_start( ld, conn, srv );
347 --conn->lconn_refcnt;
349 if (rc != LDAP_SUCCESS) {
355 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
356 if ( conn->lconn_krbinstance == NULL ) {
358 conn->lconn_krbinstance = ldap_host_connected_to(
359 conn->lconn_sb, host );
361 if( conn->lconn_krbinstance != NULL &&
362 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
366 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
372 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
378 rc = ldap_create( ldp );
379 if( rc != LDAP_SUCCESS ) {
384 /* Make it appear that a search request, msgid 0, was sent */
385 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
389 return( LDAP_NO_MEMORY );
391 memset(lr, 0, sizeof( LDAPRequest ));
393 lr->lr_status = LDAP_REQST_INPROGRESS;
394 lr->lr_res_errno = LDAP_SUCCESS;
395 /* no mutex lock needed, we just created this ld here */
396 (*ldp)->ld_requests = lr;
398 /* Attach the passed socket as the *LDAP's connection */
399 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
403 return( LDAP_NO_MEMORY );
405 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
407 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
408 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
410 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
411 LBER_SBIOD_LEVEL_PROVIDER, NULL );
412 (*ldp)->ld_defconn = c;
414 /* Add the connection to the *LDAP's select pool */
415 ldap_mark_select_read( *ldp, c->lconn_sb );
416 ldap_mark_select_write( *ldp, c->lconn_sb );
418 /* Make this connection an LDAP V3 protocol connection */
420 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
422 return( LDAP_SUCCESS );