3 * Copyright 1998-2002 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>
28 int ldap_open_defconn( LDAP *ld )
30 ld->ld_defconn = ldap_new_connection( ld,
31 ld->ld_options.ldo_defludp, 1, 1, NULL );
33 if( ld->ld_defconn == NULL ) {
34 ld->ld_errno = LDAP_SERVER_DOWN;
38 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
43 * ldap_open - initialize and connect to an ldap server. A magic cookie to
44 * be used for future communication is returned on success, NULL on failure.
45 * "host" may be a space-separated list of hosts or IP addresses
49 * ld = ldap_open( hostname, port );
53 ldap_open( LDAP_CONST char *host, int port )
59 LDAP_LOG ( CONNECTION, ARGS, "ldap_open(%s, %d)\n", host, port, 0 );
61 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
65 ld = ldap_init( host, port );
70 rc = ldap_open_defconn( ld );
73 ldap_ld_free( ld, 0, NULL, NULL );
78 LDAP_LOG ( CONNECTION, RESULTS, "ldap_open: %s\n",
79 ld == NULL ? "succeeded" : "failed", 0, 0 );
81 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
82 ld == NULL ? "succeeded" : "failed", 0, 0 );
91 ldap_create( LDAP **ldp )
94 struct ldapoptions *gopts;
97 /* Get pointer to global option structure */
98 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
99 return LDAP_NO_MEMORY;
102 /* Initialize the global options, if not already done. */
103 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
104 ldap_int_initialize(gopts, NULL);
105 if ( gopts->ldo_valid != LDAP_INITIALIZED )
106 return LDAP_LOCAL_ERROR;
110 LDAP_LOG ( CONNECTION, ENTRY, "ldap_create\n", 0, 0, 0 );
112 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
115 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
116 return( LDAP_NO_MEMORY );
119 /* copy the global options */
120 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
122 ld->ld_valid = LDAP_VALID_SESSION;
124 /* but not pointers to malloc'ed items */
125 ld->ld_options.ldo_sctrls = NULL;
126 ld->ld_options.ldo_cctrls = 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;
139 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
141 if ( ld->ld_options.ldo_defludp == NULL ) {
142 LDAP_FREE( (char*)ld );
143 return LDAP_NO_MEMORY;
146 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
147 ldap_free_urllist( ld->ld_options.ldo_defludp );
148 LDAP_FREE( (char*) ld );
149 return LDAP_NO_MEMORY;
152 ld->ld_lberoptions = LBER_USE_DER;
154 ld->ld_sb = ber_sockbuf_alloc( );
155 if ( ld->ld_sb == NULL ) {
156 ldap_free_urllist( ld->ld_options.ldo_defludp );
157 LDAP_FREE( (char*) ld );
158 return LDAP_NO_MEMORY;
166 * ldap_init - initialize the LDAP library. A magic cookie to be used for
167 * future communication is returned on success, NULL on failure.
168 * "host" may be a space-separated list of hosts or IP addresses
172 * ld = ldap_init( host, port );
175 ldap_init( LDAP_CONST char *defhost, int defport )
180 rc = ldap_create(&ld);
181 if ( rc != LDAP_SUCCESS )
185 ld->ld_options.ldo_defport = defport;
187 if (defhost != NULL) {
188 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
189 if ( rc != LDAP_SUCCESS ) {
190 ldap_ld_free(ld, 1, NULL, NULL);
200 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
206 rc = ldap_create(&ld);
207 if ( rc != LDAP_SUCCESS )
211 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
212 if ( rc != LDAP_SUCCESS ) {
213 ldap_ld_free(ld, 1, NULL, NULL);
216 #ifdef LDAP_CONNECTIONLESS
217 if (ldap_is_ldapc_url(url))
227 ldap_int_open_connection(
234 #ifdef HAVE_CYRUS_SASL
235 char *sasl_host = NULL;
243 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_open_connection\n", 0, 0, 0 );
245 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
248 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
250 port = srv->lud_port;
253 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
255 addr = htonl( INADDR_LOOPBACK );
257 host = srv->lud_host;
261 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
268 rc = ldap_connect_to_host( ld, conn->lconn_sb,
269 proto, host, addr, port, async );
271 if ( rc == -1 ) return rc;
274 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
275 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
277 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
278 LBER_SBIOD_LEVEL_PROVIDER, NULL );
280 #ifdef HAVE_CYRUS_SASL
281 sasl_host = ldap_host_connected_to( conn->lconn_sb );
284 #ifdef LDAP_CONNECTIONLESS
287 port = srv->lud_port;
290 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
292 addr = htonl( INADDR_LOOPBACK );
294 host = srv->lud_host;
297 if( !port ) port = LDAP_PORT;
300 rc = ldap_connect_to_host( ld, conn->lconn_sb,
301 proto, host, addr, 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 );
314 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
315 rc = ldap_connect_to_path( ld, conn->lconn_sb,
316 srv->lud_host, async );
317 if ( rc == -1 ) return rc;
319 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
320 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
322 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
323 LBER_SBIOD_LEVEL_PROVIDER, NULL );
325 #ifdef HAVE_CYRUS_SASL
326 sasl_host = ldap_host_connected_to( conn->lconn_sb );
327 sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
330 #endif /* LDAP_PF_LOCAL */
336 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
337 LBER_SBIOD_LEVEL_PROVIDER, NULL );
340 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
341 INT_MAX, (void *)"ldap_" );
344 #ifdef LDAP_CONNECTIONLESS
345 if( proto == LDAP_PROTO_UDP )
349 #ifdef HAVE_CYRUS_SASL
350 /* establish Cyrus SASL context prior to starting TLS so
351 that SASL EXTERNAL might be used */
352 if( sasl_host != NULL ) {
353 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
354 LDAP_FREE( sasl_host );
359 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
360 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
362 ++conn->lconn_refcnt; /* avoid premature free */
364 rc = ldap_int_tls_start( ld, conn, srv );
366 --conn->lconn_refcnt;
368 if (rc != LDAP_SUCCESS) {
374 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
375 if ( conn->lconn_krbinstance == NULL ) {
377 conn->lconn_krbinstance = ldap_host_connected_to( conn->lconn_sb );
379 if( conn->lconn_krbinstance != NULL &&
380 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
384 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
390 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
396 rc = ldap_create( ldp );
397 if( rc != LDAP_SUCCESS ) {
402 /* Make it appear that a search request, msgid 0, was sent */
403 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
407 return( LDAP_NO_MEMORY );
409 memset(lr, 0, sizeof( LDAPRequest ));
411 lr->lr_status = LDAP_REQST_INPROGRESS;
412 lr->lr_res_errno = LDAP_SUCCESS;
413 (*ldp)->ld_requests = lr;
415 /* Attach the passed socket as the *LDAP's connection */
416 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
420 return( LDAP_NO_MEMORY );
422 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
424 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
425 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
427 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
428 LBER_SBIOD_LEVEL_PROVIDER, NULL );
429 (*ldp)->ld_defconn = c;
431 /* Add the connection to the *LDAP's select pool */
432 ldap_mark_select_read( *ldp, c->lconn_sb );
433 ldap_mark_select_write( *ldp, c->lconn_sb );
435 /* Make this connection an LDAP V3 protocol connection */
437 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
439 return( LDAP_SUCCESS );