2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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.
24 #include <ac/stdlib.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
31 #include <ac/unistd.h>
36 int ldap_open_defconn( LDAP *ld )
38 ld->ld_defconn = ldap_new_connection( ld,
39 ld->ld_options.ldo_defludp, 1, 1, NULL );
41 if( ld->ld_defconn == NULL ) {
42 ld->ld_errno = LDAP_SERVER_DOWN;
46 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
51 * ldap_open - initialize and connect to an ldap server. A magic cookie to
52 * be used for future communication is returned on success, NULL on failure.
53 * "host" may be a space-separated list of hosts or IP addresses
57 * ld = ldap_open( hostname, port );
61 ldap_open( LDAP_CONST char *host, int port )
67 LDAP_LOG ( CONNECTION, ARGS, "ldap_open(%s, %d)\n", host, port, 0 );
69 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
73 ld = ldap_init( host, port );
78 rc = ldap_open_defconn( ld );
81 ldap_ld_free( ld, 0, NULL, NULL );
86 LDAP_LOG ( CONNECTION, RESULTS, "ldap_open: %s\n",
87 ld == NULL ? "succeeded" : "failed", 0, 0 );
89 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
90 ld == NULL ? "succeeded" : "failed", 0, 0 );
99 ldap_create( LDAP **ldp )
102 struct ldapoptions *gopts;
105 /* Get pointer to global option structure */
106 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
107 return LDAP_NO_MEMORY;
110 /* Initialize the global options, if not already done. */
111 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
112 ldap_int_initialize(gopts, NULL);
113 if ( gopts->ldo_valid != LDAP_INITIALIZED )
114 return LDAP_LOCAL_ERROR;
118 LDAP_LOG ( CONNECTION, ENTRY, "ldap_create\n", 0, 0, 0 );
120 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
123 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
124 return( LDAP_NO_MEMORY );
127 /* copy the global options */
128 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
130 ld->ld_valid = LDAP_VALID_SESSION;
132 /* but not pointers to malloc'ed items */
133 ld->ld_options.ldo_sctrls = NULL;
134 ld->ld_options.ldo_cctrls = NULL;
136 #ifdef HAVE_CYRUS_SASL
137 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
138 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
139 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
140 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
141 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
142 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
143 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
144 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
147 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
149 if ( ld->ld_options.ldo_defludp == NULL ) {
150 LDAP_FREE( (char*)ld );
151 return LDAP_NO_MEMORY;
154 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
155 ldap_free_urllist( ld->ld_options.ldo_defludp );
156 LDAP_FREE( (char*) ld );
157 return LDAP_NO_MEMORY;
160 ld->ld_lberoptions = LBER_USE_DER;
162 ld->ld_sb = ber_sockbuf_alloc( );
163 if ( ld->ld_sb == NULL ) {
164 ldap_free_urllist( ld->ld_options.ldo_defludp );
165 LDAP_FREE( (char*) ld );
166 return LDAP_NO_MEMORY;
169 #ifdef LDAP_R_COMPILE
170 ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
171 ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
178 * ldap_init - initialize the LDAP library. A magic cookie to be used for
179 * future communication is returned on success, NULL on failure.
180 * "host" may be a space-separated list of hosts or IP addresses
184 * ld = ldap_init( host, port );
187 ldap_init( LDAP_CONST char *defhost, int defport )
192 rc = ldap_create(&ld);
193 if ( rc != LDAP_SUCCESS )
197 ld->ld_options.ldo_defport = defport;
199 if (defhost != NULL) {
200 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
201 if ( rc != LDAP_SUCCESS ) {
202 ldap_ld_free(ld, 1, NULL, NULL);
212 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
218 rc = ldap_create(&ld);
219 if ( rc != LDAP_SUCCESS )
223 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
224 if ( rc != LDAP_SUCCESS ) {
225 ldap_ld_free(ld, 1, NULL, NULL);
228 #ifdef LDAP_CONNECTIONLESS
229 if (ldap_is_ldapc_url(url))
239 ldap_int_open_connection(
250 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_open_connection\n", 0, 0, 0 );
252 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
255 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
257 port = srv->lud_port;
259 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
262 host = srv->lud_host;
266 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
273 rc = ldap_connect_to_host( ld, conn->lconn_sb,
274 proto, host, port, async );
276 if ( rc == -1 ) return rc;
279 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
280 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
282 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
283 LBER_SBIOD_LEVEL_PROVIDER, NULL );
287 #ifdef LDAP_CONNECTIONLESS
289 port = srv->lud_port;
291 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
294 host = srv->lud_host;
297 if( !port ) port = LDAP_PORT;
300 rc = ldap_connect_to_host( ld, conn->lconn_sb,
301 proto, host, port, async );
303 if ( rc == -1 ) return rc;
305 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
306 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
308 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
309 LBER_SBIOD_LEVEL_PROVIDER, NULL );
311 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
312 LBER_SBIOD_LEVEL_PROVIDER, NULL );
318 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
319 rc = ldap_connect_to_path( ld, conn->lconn_sb,
320 srv->lud_host, async );
321 if ( rc == -1 ) return rc;
323 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
324 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
326 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
327 LBER_SBIOD_LEVEL_PROVIDER, NULL );
330 #endif /* LDAP_PF_LOCAL */
337 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
338 INT_MAX, (void *)"ldap_" );
341 #ifdef LDAP_CONNECTIONLESS
342 if( proto == LDAP_PROTO_UDP ) return 0;
346 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
347 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
349 ++conn->lconn_refcnt; /* avoid premature free */
351 rc = ldap_int_tls_start( ld, conn, srv );
353 --conn->lconn_refcnt;
355 if (rc != LDAP_SUCCESS) {
361 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
362 if ( conn->lconn_krbinstance == NULL ) {
364 conn->lconn_krbinstance = ldap_host_connected_to(
365 conn->lconn_sb, host );
367 if( conn->lconn_krbinstance != NULL &&
368 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
372 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
378 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
384 rc = ldap_create( ldp );
385 if( rc != LDAP_SUCCESS ) {
390 /* Make it appear that a search request, msgid 0, was sent */
391 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
395 return( LDAP_NO_MEMORY );
397 memset(lr, 0, sizeof( LDAPRequest ));
399 lr->lr_status = LDAP_REQST_INPROGRESS;
400 lr->lr_res_errno = LDAP_SUCCESS;
401 /* no mutex lock needed, we just created this ld here */
402 (*ldp)->ld_requests = lr;
404 /* Attach the passed socket as the *LDAP's connection */
405 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
409 return( LDAP_NO_MEMORY );
411 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
413 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
414 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
416 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
417 LBER_SBIOD_LEVEL_PROVIDER, NULL );
418 (*ldp)->ld_defconn = c;
420 /* Add the connection to the *LDAP's select pool */
421 ldap_mark_select_read( *ldp, c->lconn_sb );
422 ldap_mark_select_write( *ldp, c->lconn_sb );
424 /* Make this connection an LDAP V3 protocol connection */
426 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
428 return( LDAP_SUCCESS );