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>
26 #include <sys/types.h>
31 int ldap_open_defconn( LDAP *ld )
33 ld->ld_defconn = ldap_new_connection( ld,
34 ld->ld_options.ldo_defludp, 1, 1, NULL );
36 if( ld->ld_defconn == NULL ) {
37 ld->ld_errno = LDAP_SERVER_DOWN;
41 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
46 * ldap_open - initialize and connect to an ldap server. A magic cookie to
47 * be used for future communication is returned on success, NULL on failure.
48 * "host" may be a space-separated list of hosts or IP addresses
52 * ld = ldap_open( hostname, port );
56 ldap_open( LDAP_CONST char *host, int port )
62 LDAP_LOG ( CONNECTION, ARGS, "ldap_open(%s, %d)\n", host, port, 0 );
64 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
68 ld = ldap_init( host, port );
73 rc = ldap_open_defconn( ld );
76 ldap_ld_free( ld, 0, NULL, NULL );
81 LDAP_LOG ( CONNECTION, RESULTS, "ldap_open: %s\n",
82 ld == NULL ? "succeeded" : "failed", 0, 0 );
84 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
85 ld == NULL ? "succeeded" : "failed", 0, 0 );
94 ldap_create( LDAP **ldp )
97 struct ldapoptions *gopts;
100 /* Get pointer to global option structure */
101 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
102 return LDAP_NO_MEMORY;
105 /* Initialize the global options, if not already done. */
106 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
107 ldap_int_initialize(gopts, NULL);
108 if ( gopts->ldo_valid != LDAP_INITIALIZED )
109 return LDAP_LOCAL_ERROR;
113 LDAP_LOG ( CONNECTION, ENTRY, "ldap_create\n", 0, 0, 0 );
115 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
118 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
119 return( LDAP_NO_MEMORY );
122 /* copy the global options */
123 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
125 ld->ld_valid = LDAP_VALID_SESSION;
127 /* but not pointers to malloc'ed items */
128 ld->ld_options.ldo_sctrls = NULL;
129 ld->ld_options.ldo_cctrls = NULL;
131 #ifdef HAVE_CYRUS_SASL
132 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
133 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
134 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
135 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
136 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
137 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
138 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
139 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
142 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
144 if ( ld->ld_options.ldo_defludp == NULL ) {
145 LDAP_FREE( (char*)ld );
146 return LDAP_NO_MEMORY;
149 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
150 ldap_free_urllist( ld->ld_options.ldo_defludp );
151 LDAP_FREE( (char*) ld );
152 return LDAP_NO_MEMORY;
155 ld->ld_lberoptions = LBER_USE_DER;
157 ld->ld_sb = ber_sockbuf_alloc( );
158 if ( ld->ld_sb == NULL ) {
159 ldap_free_urllist( ld->ld_options.ldo_defludp );
160 LDAP_FREE( (char*) ld );
161 return LDAP_NO_MEMORY;
169 * ldap_init - initialize the LDAP library. A magic cookie to be used for
170 * future communication is returned on success, NULL on failure.
171 * "host" may be a space-separated list of hosts or IP addresses
175 * ld = ldap_init( host, port );
178 ldap_init( LDAP_CONST char *defhost, int defport )
183 rc = ldap_create(&ld);
184 if ( rc != LDAP_SUCCESS )
188 ld->ld_options.ldo_defport = defport;
190 if (defhost != NULL) {
191 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
192 if ( rc != LDAP_SUCCESS ) {
193 ldap_ld_free(ld, 1, NULL, NULL);
203 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
209 rc = ldap_create(&ld);
210 if ( rc != LDAP_SUCCESS )
214 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
215 if ( rc != LDAP_SUCCESS ) {
216 ldap_ld_free(ld, 1, NULL, NULL);
219 #ifdef LDAP_CONNECTIONLESS
220 if (ldap_is_ldapc_url(url))
230 ldap_int_open_connection(
237 #ifdef HAVE_CYRUS_SASL
238 char *sasl_host = NULL;
246 LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_open_connection\n", 0, 0, 0 );
248 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
251 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
253 port = srv->lud_port;
256 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
258 addr = htonl( INADDR_LOOPBACK );
260 host = srv->lud_host;
264 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
271 rc = ldap_connect_to_host( ld, conn->lconn_sb,
272 proto, host, addr, port, async );
274 if ( rc == -1 ) return rc;
277 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
278 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
280 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
281 LBER_SBIOD_LEVEL_PROVIDER, NULL );
283 #ifdef HAVE_CYRUS_SASL
284 sasl_host = ldap_host_connected_to( conn->lconn_sb );
287 #ifdef LDAP_CONNECTIONLESS
290 port = srv->lud_port;
293 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
295 addr = htonl( INADDR_LOOPBACK );
297 host = srv->lud_host;
300 if( !port ) port = LDAP_PORT;
303 rc = ldap_connect_to_host( ld, conn->lconn_sb,
304 proto, host, addr, port, async );
306 if ( rc == -1 ) return rc;
308 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
309 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
311 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
312 LBER_SBIOD_LEVEL_PROVIDER, NULL );
317 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
318 rc = ldap_connect_to_path( ld, conn->lconn_sb,
319 srv->lud_host, async );
320 if ( rc == -1 ) return rc;
322 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
323 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
325 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
326 LBER_SBIOD_LEVEL_PROVIDER, NULL );
328 #ifdef HAVE_CYRUS_SASL
329 sasl_host = ldap_host_connected_to( conn->lconn_sb );
330 sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
333 #endif /* LDAP_PF_LOCAL */
339 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
340 LBER_SBIOD_LEVEL_PROVIDER, NULL );
343 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
344 INT_MAX, (void *)"ldap_" );
347 #ifdef LDAP_CONNECTIONLESS
348 if( proto == LDAP_PROTO_UDP )
352 #ifdef HAVE_CYRUS_SASL
353 /* establish Cyrus SASL context prior to starting TLS so
354 that SASL EXTERNAL might be used */
355 if( sasl_host != NULL ) {
356 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
357 LDAP_FREE( sasl_host );
359 /* sasl_ssf is set redundantly. Should probably remove it from
360 * the ldap_int_sasl_open call since the TLS ssf isn't known
363 if( proto == LDAP_PROTO_IPC ) {
365 sprintf( authid, "uid=%d+gid=%d", geteuid(), getegid() );
366 ldap_int_sasl_external( ld, conn, authid, sasl_ssf );
371 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
372 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
374 ++conn->lconn_refcnt; /* avoid premature free */
376 rc = ldap_int_tls_start( ld, conn, srv );
378 --conn->lconn_refcnt;
380 if (rc != LDAP_SUCCESS) {
386 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
387 if ( conn->lconn_krbinstance == NULL ) {
389 conn->lconn_krbinstance = ldap_host_connected_to( conn->lconn_sb );
391 if( conn->lconn_krbinstance != NULL &&
392 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
396 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
402 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
408 rc = ldap_create( ldp );
409 if( rc != LDAP_SUCCESS ) {
414 /* Make it appear that a search request, msgid 0, was sent */
415 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
419 return( LDAP_NO_MEMORY );
421 memset(lr, 0, sizeof( LDAPRequest ));
423 lr->lr_status = LDAP_REQST_INPROGRESS;
424 lr->lr_res_errno = LDAP_SUCCESS;
425 (*ldp)->ld_requests = lr;
427 /* Attach the passed socket as the *LDAP's connection */
428 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
432 return( LDAP_NO_MEMORY );
434 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
436 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
437 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
439 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
440 LBER_SBIOD_LEVEL_PROVIDER, NULL );
441 (*ldp)->ld_defconn = c;
443 /* Add the connection to the *LDAP's select pool */
444 ldap_mark_select_read( *ldp, c->lconn_sb );
445 ldap_mark_select_write( *ldp, c->lconn_sb );
447 /* Make this connection an LDAP V3 protocol connection */
449 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
451 return( LDAP_SUCCESS );