2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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.
26 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
33 #include <ac/unistd.h>
38 int ldap_open_defconn( LDAP *ld )
40 ld->ld_defconn = ldap_new_connection( ld,
41 ld->ld_options.ldo_defludp, 1, 1, NULL );
43 if( ld->ld_defconn == NULL ) {
44 ld->ld_errno = LDAP_SERVER_DOWN;
48 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
53 * ldap_open - initialize and connect to an ldap server. A magic cookie to
54 * be used for future communication is returned on success, NULL on failure.
55 * "host" may be a space-separated list of hosts or IP addresses
59 * ld = ldap_open( hostname, port );
63 ldap_open( LDAP_CONST char *host, int port )
68 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
71 ld = ldap_init( host, port );
76 rc = ldap_open_defconn( ld );
79 ldap_ld_free( ld, 0, NULL, NULL );
83 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
84 ld == NULL ? "succeeded" : "failed", 0, 0 );
92 ldap_create( LDAP **ldp )
95 struct ldapoptions *gopts;
98 /* Get pointer to global option structure */
99 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
100 return LDAP_NO_MEMORY;
103 /* Initialize the global options, if not already done. */
104 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
105 ldap_int_initialize(gopts, NULL);
106 if ( gopts->ldo_valid != LDAP_INITIALIZED )
107 return LDAP_LOCAL_ERROR;
110 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
112 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
113 return( LDAP_NO_MEMORY );
116 /* copy the global options */
117 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
119 ld->ld_valid = LDAP_VALID_SESSION;
121 /* but not pointers to malloc'ed items */
122 ld->ld_options.ldo_sctrls = NULL;
123 ld->ld_options.ldo_cctrls = NULL;
124 ld->ld_options.ldo_tm_api = NULL;
125 ld->ld_options.ldo_tm_net = NULL;
126 ld->ld_options.ldo_defludp = 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;
140 /* We inherit the SSL_CTX, don't need the names/paths. Leave them
141 * empty to allow new SSL_CTX's to be created from scratch.
143 memset( &ld->ld_options.ldo_tls_info, 0,
144 sizeof( ld->ld_options.ldo_tls_info ));
148 if ( gopts->ldo_tm_api &&
149 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_api, gopts->ldo_tm_api ))
152 if ( gopts->ldo_tm_net &&
153 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_net, gopts->ldo_tm_net ))
156 if ( gopts->ldo_defludp ) {
157 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
159 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
162 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
164 ld->ld_lberoptions = LBER_USE_DER;
166 ld->ld_sb = ber_sockbuf_alloc( );
167 if ( ld->ld_sb == NULL ) goto nomem;
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 );
172 ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
178 ldap_free_select_info( ld->ld_selectinfo );
179 ldap_free_urllist( ld->ld_options.ldo_defludp );
180 LDAP_FREE( ld->ld_options.ldo_tm_net );
181 LDAP_FREE( ld->ld_options.ldo_tm_api );
182 #ifdef HAVE_CYRUS_SASL
183 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
184 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
185 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
186 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
188 LDAP_FREE( (char *)ld );
189 return LDAP_NO_MEMORY;
193 * ldap_init - initialize the LDAP library. A magic cookie to be used for
194 * future communication is returned on success, NULL on failure.
195 * "host" may be a space-separated list of hosts or IP addresses
199 * ld = ldap_init( host, port );
202 ldap_init( LDAP_CONST char *defhost, int defport )
207 rc = ldap_create(&ld);
208 if ( rc != LDAP_SUCCESS )
212 ld->ld_options.ldo_defport = defport;
214 if (defhost != NULL) {
215 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
216 if ( rc != LDAP_SUCCESS ) {
217 ldap_ld_free(ld, 1, NULL, NULL);
227 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
233 rc = ldap_create(&ld);
234 if ( rc != LDAP_SUCCESS )
238 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
239 if ( rc != LDAP_SUCCESS ) {
240 ldap_ld_free(ld, 1, NULL, NULL);
243 #ifdef LDAP_CONNECTIONLESS
244 if (ldap_is_ldapc_url(url))
254 ldap_int_open_connection(
264 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
266 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
268 port = srv->lud_port;
270 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
273 host = srv->lud_host;
277 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
284 rc = ldap_connect_to_host( ld, conn->lconn_sb,
285 proto, host, port, async );
287 if ( rc == -1 ) return rc;
290 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
291 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
293 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
294 LBER_SBIOD_LEVEL_PROVIDER, NULL );
298 #ifdef LDAP_CONNECTIONLESS
300 port = srv->lud_port;
302 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
305 host = srv->lud_host;
308 if( !port ) port = LDAP_PORT;
311 rc = ldap_connect_to_host( ld, conn->lconn_sb,
312 proto, host, port, async );
314 if ( rc == -1 ) return rc;
316 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
317 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
319 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
320 LBER_SBIOD_LEVEL_PROVIDER, NULL );
322 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
323 LBER_SBIOD_LEVEL_PROVIDER, NULL );
329 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
330 rc = ldap_connect_to_path( ld, conn->lconn_sb,
331 srv->lud_host, async );
332 if ( rc == -1 ) return rc;
334 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
335 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
337 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
338 LBER_SBIOD_LEVEL_PROVIDER, NULL );
341 #endif /* LDAP_PF_LOCAL */
348 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
349 INT_MAX, (void *)"ldap_" );
352 #ifdef LDAP_CONNECTIONLESS
353 if( proto == LDAP_PROTO_UDP ) return 0;
357 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
358 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
360 ++conn->lconn_refcnt; /* avoid premature free */
362 rc = ldap_int_tls_start( ld, conn, srv );
364 --conn->lconn_refcnt;
366 if (rc != LDAP_SUCCESS) {
372 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
373 if ( conn->lconn_krbinstance == NULL ) {
375 conn->lconn_krbinstance = ldap_host_connected_to(
376 conn->lconn_sb, host );
378 if( conn->lconn_krbinstance != NULL &&
379 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
383 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
389 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
395 rc = ldap_create( ldp );
396 if( rc != LDAP_SUCCESS ) {
401 /* Make it appear that a search request, msgid 0, was sent */
402 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
406 return( LDAP_NO_MEMORY );
408 memset(lr, 0, sizeof( LDAPRequest ));
410 lr->lr_status = LDAP_REQST_INPROGRESS;
411 lr->lr_res_errno = LDAP_SUCCESS;
412 /* no mutex lock needed, we just created this ld here */
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 );