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>
27 int ldap_open_defconn( LDAP *ld )
29 ld->ld_defconn = ldap_new_connection( ld,
30 ld->ld_options.ldo_defludp, 1, 1, NULL );
32 if( ld->ld_defconn == NULL ) {
33 ld->ld_errno = LDAP_SERVER_DOWN;
37 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
42 * ldap_open - initialize and connect to an ldap server. A magic cookie to
43 * be used for future communication is returned on success, NULL on failure.
44 * "host" may be a space-separated list of hosts or IP addresses
48 * ld = ldap_open( hostname, port );
52 ldap_open( LDAP_CONST char *host, int port )
57 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
60 ld = ldap_init( host, port );
65 rc = ldap_open_defconn( ld );
68 ldap_ld_free( ld, 0, NULL, NULL );
72 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
73 ld == NULL ? "succeeded" : "failed", 0, 0 );
81 ldap_create( LDAP **ldp )
84 struct ldapoptions *gopts;
87 /* Get pointer to global option structure */
88 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
89 return LDAP_NO_MEMORY;
92 /* Initialize the global options, if not already done. */
93 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
94 ldap_int_initialize(gopts, NULL);
95 if ( gopts->ldo_valid != LDAP_INITIALIZED )
96 return LDAP_LOCAL_ERROR;
99 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
101 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
102 return( LDAP_NO_MEMORY );
105 /* copy the global options */
106 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
108 ld->ld_valid = LDAP_VALID_SESSION;
110 /* but not pointers to malloc'ed items */
111 ld->ld_options.ldo_sctrls = NULL;
112 ld->ld_options.ldo_cctrls = NULL;
114 #ifdef HAVE_CYRUS_SASL
115 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
116 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
117 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
118 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
119 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
120 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
121 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
122 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
125 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
127 if ( ld->ld_options.ldo_defludp == NULL ) {
128 LDAP_FREE( (char*)ld );
129 return LDAP_NO_MEMORY;
132 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
133 ldap_free_urllist( ld->ld_options.ldo_defludp );
134 LDAP_FREE( (char*) ld );
135 return LDAP_NO_MEMORY;
138 ld->ld_lberoptions = LBER_USE_DER;
140 ld->ld_sb = ber_sockbuf_alloc( );
141 if ( ld->ld_sb == NULL ) {
142 ldap_free_urllist( ld->ld_options.ldo_defludp );
143 LDAP_FREE( (char*) ld );
144 return LDAP_NO_MEMORY;
152 * ldap_init - initialize the LDAP library. A magic cookie to be used for
153 * future communication is returned on success, NULL on failure.
154 * "host" may be a space-separated list of hosts or IP addresses
158 * ld = ldap_init( host, port );
161 ldap_init( LDAP_CONST char *defhost, int defport )
166 rc = ldap_create(&ld);
167 if ( rc != LDAP_SUCCESS )
171 ld->ld_options.ldo_defport = defport;
173 if (defhost != NULL) {
174 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
175 if ( rc != LDAP_SUCCESS ) {
176 ldap_ld_free(ld, 1, NULL, NULL);
186 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
192 rc = ldap_create(&ld);
193 if ( rc != LDAP_SUCCESS )
197 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
198 if ( rc != LDAP_SUCCESS ) {
199 ldap_ld_free(ld, 1, NULL, NULL);
202 #ifdef LDAP_CONNECTIONLESS
203 if (ldap_is_ldapc_url(url))
213 ldap_int_open_connection(
220 #ifdef HAVE_CYRUS_SASL
221 char *sasl_host = NULL;
228 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
230 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
232 port = srv->lud_port;
235 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
237 addr = htonl( INADDR_LOOPBACK );
239 host = srv->lud_host;
242 rc = ldap_connect_to_host( ld, conn->lconn_sb,
243 proto, host, addr, port, async );
245 if ( rc == -1 ) return rc;
248 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
249 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
251 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
252 LBER_SBIOD_LEVEL_PROVIDER, NULL );
254 #ifdef HAVE_CYRUS_SASL
255 sasl_host = ldap_host_connected_to( conn->lconn_sb );
258 #ifdef LDAP_CONNECTIONLESS
260 port = srv->lud_port;
263 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
265 addr = htonl( INADDR_LOOPBACK );
267 host = srv->lud_host;
270 rc = ldap_connect_to_host( ld, conn->lconn_sb,
271 proto, host, addr, port, async );
273 if ( rc == -1 ) return rc;
275 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
276 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
278 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
279 LBER_SBIOD_LEVEL_PROVIDER, NULL );
284 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
285 rc = ldap_connect_to_path( ld, conn->lconn_sb,
286 srv->lud_host, async );
287 if ( rc == -1 ) return rc;
289 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
290 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
292 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
293 LBER_SBIOD_LEVEL_PROVIDER, NULL );
295 #ifdef HAVE_CYRUS_SASL
296 sasl_host = ldap_host_connected_to( conn->lconn_sb );
297 sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
300 #endif /* LDAP_PF_LOCAL */
306 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
307 LBER_SBIOD_LEVEL_PROVIDER, NULL );
310 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
311 INT_MAX, (void *)"ldap_" );
314 #ifdef LDAP_CONNECTIONLESS
315 if( proto == LDAP_PROTO_UDP )
319 #ifdef HAVE_CYRUS_SASL
320 /* establish Cyrus SASL context prior to starting TLS so
321 that SASL EXTERNAL might be used */
322 if( sasl_host != NULL ) {
323 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
324 LDAP_FREE( sasl_host );
329 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
330 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
332 ++conn->lconn_refcnt; /* avoid premature free */
334 rc = ldap_int_tls_start( ld, conn, srv );
336 --conn->lconn_refcnt;
338 if (rc != LDAP_SUCCESS) {
344 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
345 if ( conn->lconn_krbinstance == NULL ) {
347 conn->lconn_krbinstance = ldap_host_connected_to( conn->lconn_sb );
349 if( conn->lconn_krbinstance != NULL &&
350 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
354 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
360 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
366 rc = ldap_create( ldp );
367 if( rc != LDAP_SUCCESS ) {
372 /* Make it appear that a search request, msgid 0, was sent */
373 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
377 return( LDAP_NO_MEMORY );
379 memset(lr, 0, sizeof( LDAPRequest ));
381 lr->lr_status = LDAP_REQST_INPROGRESS;
382 lr->lr_res_errno = LDAP_SUCCESS;
383 (*ldp)->ld_requests = lr;
385 /* Attach the passed socket as the *LDAP's connection */
386 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
390 return( LDAP_NO_MEMORY );
392 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
394 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
395 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
397 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
398 LBER_SBIOD_LEVEL_PROVIDER, NULL );
399 (*ldp)->ld_defconn = c;
401 /* Add the connection to the *LDAP's select pool */
402 ldap_mark_select_read( *ldp, c->lconn_sb );
403 ldap_mark_select_write( *ldp, c->lconn_sb );
405 /* Make this connection an LDAP V3 protocol connection */
407 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
409 return( LDAP_SUCCESS );