2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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 )
66 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
69 ld = ldap_init( host, port );
74 rc = ldap_open_defconn( ld );
77 ldap_ld_free( ld, 0, NULL, NULL );
81 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
82 ld == NULL ? "succeeded" : "failed", 0, 0 );
90 ldap_create( LDAP **ldp )
93 struct ldapoptions *gopts;
96 /* Get pointer to global option structure */
97 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
98 return LDAP_NO_MEMORY;
101 /* Initialize the global options, if not already done. */
102 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
103 ldap_int_initialize(gopts, NULL);
104 if ( gopts->ldo_valid != LDAP_INITIALIZED )
105 return LDAP_LOCAL_ERROR;
108 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
110 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
111 return( LDAP_NO_MEMORY );
114 /* copy the global options */
115 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
117 ld->ld_valid = LDAP_VALID_SESSION;
119 /* but not pointers to malloc'ed items */
120 ld->ld_options.ldo_sctrls = NULL;
121 ld->ld_options.ldo_cctrls = NULL;
123 #ifdef HAVE_CYRUS_SASL
124 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
125 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
126 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
127 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
128 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
129 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
130 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
131 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
134 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
136 if ( ld->ld_options.ldo_defludp == NULL ) {
137 LDAP_FREE( (char*)ld );
138 return LDAP_NO_MEMORY;
141 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
142 ldap_free_urllist( ld->ld_options.ldo_defludp );
143 LDAP_FREE( (char*) ld );
144 return LDAP_NO_MEMORY;
147 ld->ld_lberoptions = LBER_USE_DER;
149 ld->ld_sb = ber_sockbuf_alloc( );
150 if ( ld->ld_sb == NULL ) {
151 ldap_free_urllist( ld->ld_options.ldo_defludp );
152 LDAP_FREE( (char*) ld );
153 return LDAP_NO_MEMORY;
156 #ifdef LDAP_R_COMPILE
157 ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
158 ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
165 * ldap_init - initialize the LDAP library. A magic cookie to be used for
166 * future communication is returned on success, NULL on failure.
167 * "host" may be a space-separated list of hosts or IP addresses
171 * ld = ldap_init( host, port );
174 ldap_init( LDAP_CONST char *defhost, int defport )
179 rc = ldap_create(&ld);
180 if ( rc != LDAP_SUCCESS )
184 ld->ld_options.ldo_defport = defport;
186 if (defhost != NULL) {
187 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
188 if ( rc != LDAP_SUCCESS ) {
189 ldap_ld_free(ld, 1, NULL, NULL);
199 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
205 rc = ldap_create(&ld);
206 if ( rc != LDAP_SUCCESS )
210 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
211 if ( rc != LDAP_SUCCESS ) {
212 ldap_ld_free(ld, 1, NULL, NULL);
215 #ifdef LDAP_CONNECTIONLESS
216 if (ldap_is_ldapc_url(url))
226 ldap_int_open_connection(
236 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
238 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
240 port = srv->lud_port;
242 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
245 host = srv->lud_host;
249 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
256 rc = ldap_connect_to_host( ld, conn->lconn_sb,
257 proto, host, port, async );
259 if ( rc == -1 ) return rc;
262 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
263 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
265 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
266 LBER_SBIOD_LEVEL_PROVIDER, NULL );
270 #ifdef LDAP_CONNECTIONLESS
272 port = srv->lud_port;
274 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
277 host = srv->lud_host;
280 if( !port ) port = LDAP_PORT;
283 rc = ldap_connect_to_host( ld, conn->lconn_sb,
284 proto, host, port, async );
286 if ( rc == -1 ) return rc;
288 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
289 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
291 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
292 LBER_SBIOD_LEVEL_PROVIDER, NULL );
294 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
295 LBER_SBIOD_LEVEL_PROVIDER, NULL );
301 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
302 rc = ldap_connect_to_path( ld, conn->lconn_sb,
303 srv->lud_host, async );
304 if ( rc == -1 ) return rc;
306 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
307 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
309 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
310 LBER_SBIOD_LEVEL_PROVIDER, NULL );
313 #endif /* LDAP_PF_LOCAL */
320 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
321 INT_MAX, (void *)"ldap_" );
324 #ifdef LDAP_CONNECTIONLESS
325 if( proto == LDAP_PROTO_UDP ) return 0;
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(
348 conn->lconn_sb, host );
350 if( conn->lconn_krbinstance != NULL &&
351 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
355 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
361 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
367 rc = ldap_create( ldp );
368 if( rc != LDAP_SUCCESS ) {
373 /* Make it appear that a search request, msgid 0, was sent */
374 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
378 return( LDAP_NO_MEMORY );
380 memset(lr, 0, sizeof( LDAPRequest ));
382 lr->lr_status = LDAP_REQST_INPROGRESS;
383 lr->lr_res_errno = LDAP_SUCCESS;
384 /* no mutex lock needed, we just created this ld here */
385 (*ldp)->ld_requests = lr;
387 /* Attach the passed socket as the *LDAP's connection */
388 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
392 return( LDAP_NO_MEMORY );
394 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
396 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
397 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
399 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
400 LBER_SBIOD_LEVEL_PROVIDER, NULL );
401 (*ldp)->ld_defconn = c;
403 /* Add the connection to the *LDAP's select pool */
404 ldap_mark_select_read( *ldp, c->lconn_sb );
405 ldap_mark_select_write( *ldp, c->lconn_sb );
407 /* Make this connection an LDAP V3 protocol connection */
409 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
411 return( LDAP_SUCCESS );