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 /* Caller should hold the req_mutex if simultaneous accesses are possible */
39 int ldap_open_defconn( LDAP *ld )
41 ld->ld_defconn = ldap_new_connection( ld,
42 &ld->ld_options.ldo_defludp, 1, 1, NULL );
44 if( ld->ld_defconn == NULL ) {
45 ld->ld_errno = LDAP_SERVER_DOWN;
49 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
54 * ldap_open - initialize and connect to an ldap server. A magic cookie to
55 * be used for future communication is returned on success, NULL on failure.
56 * "host" may be a space-separated list of hosts or IP addresses
60 * ld = ldap_open( hostname, port );
64 ldap_open( LDAP_CONST char *host, int port )
69 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
72 ld = ldap_init( host, port );
77 rc = ldap_open_defconn( ld );
80 ldap_ld_free( ld, 0, NULL, NULL );
84 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
85 ld == NULL ? "succeeded" : "failed", 0, 0 );
93 ldap_create( LDAP **ldp )
96 struct ldapoptions *gopts;
99 /* Get pointer to global option structure */
100 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
101 return LDAP_NO_MEMORY;
104 /* Initialize the global options, if not already done. */
105 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
106 ldap_int_initialize(gopts, NULL);
107 if ( gopts->ldo_valid != LDAP_INITIALIZED )
108 return LDAP_LOCAL_ERROR;
111 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
113 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
114 return( LDAP_NO_MEMORY );
117 /* copy the global options */
118 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
120 ld->ld_valid = LDAP_VALID_SESSION;
122 /* but not pointers to malloc'ed items */
123 ld->ld_options.ldo_sctrls = NULL;
124 ld->ld_options.ldo_cctrls = NULL;
125 ld->ld_options.ldo_tm_api = NULL;
126 ld->ld_options.ldo_tm_net = NULL;
127 ld->ld_options.ldo_defludp = NULL;
129 #ifdef HAVE_CYRUS_SASL
130 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
131 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
132 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
133 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
134 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
135 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
136 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
137 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
141 /* We explicitly inherit the SSL_CTX, don't need the names/paths. Leave
142 * them empty to allow new SSL_CTX's to be created from scratch.
144 memset( &ld->ld_options.ldo_tls_info, 0,
145 sizeof( ld->ld_options.ldo_tls_info ));
146 ld->ld_options.ldo_tls_ctx = NULL;
149 if ( gopts->ldo_tm_api &&
150 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_api, gopts->ldo_tm_api ))
153 if ( gopts->ldo_tm_net &&
154 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_net, gopts->ldo_tm_net ))
157 if ( gopts->ldo_defludp ) {
158 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
160 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
163 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
165 ld->ld_lberoptions = LBER_USE_DER;
167 ld->ld_sb = ber_sockbuf_alloc( );
168 if ( ld->ld_sb == NULL ) goto nomem;
170 #ifdef LDAP_R_COMPILE
171 ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
172 ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
173 ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
179 ldap_free_select_info( ld->ld_selectinfo );
180 ldap_free_urllist( ld->ld_options.ldo_defludp );
181 LDAP_FREE( ld->ld_options.ldo_tm_net );
182 LDAP_FREE( ld->ld_options.ldo_tm_api );
183 #ifdef HAVE_CYRUS_SASL
184 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
185 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
186 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
187 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
189 LDAP_FREE( (char *)ld );
190 return LDAP_NO_MEMORY;
194 * ldap_init - initialize the LDAP library. A magic cookie to be used for
195 * future communication is returned on success, NULL on failure.
196 * "host" may be a space-separated list of hosts or IP addresses
200 * ld = ldap_init( host, port );
203 ldap_init( LDAP_CONST char *defhost, int defport )
208 rc = ldap_create(&ld);
209 if ( rc != LDAP_SUCCESS )
213 ld->ld_options.ldo_defport = defport;
215 if (defhost != NULL) {
216 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
217 if ( rc != LDAP_SUCCESS ) {
218 ldap_ld_free(ld, 1, NULL, NULL);
228 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
234 rc = ldap_create(&ld);
235 if ( rc != LDAP_SUCCESS )
239 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
240 if ( rc != LDAP_SUCCESS ) {
241 ldap_ld_free(ld, 1, NULL, NULL);
244 #ifdef LDAP_CONNECTIONLESS
245 if (ldap_is_ldapc_url(url))
255 ldap_int_open_connection(
265 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
267 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
269 port = srv->lud_port;
271 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
274 host = srv->lud_host;
278 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
285 rc = ldap_connect_to_host( ld, conn->lconn_sb,
286 proto, host, port, async );
288 if ( rc == -1 ) return rc;
291 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
292 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
294 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
295 LBER_SBIOD_LEVEL_PROVIDER, NULL );
299 #ifdef LDAP_CONNECTIONLESS
301 port = srv->lud_port;
303 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
306 host = srv->lud_host;
309 if( !port ) port = LDAP_PORT;
312 rc = ldap_connect_to_host( ld, conn->lconn_sb,
313 proto, host, port, async );
315 if ( rc == -1 ) return rc;
317 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
318 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
320 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
321 LBER_SBIOD_LEVEL_PROVIDER, NULL );
323 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
324 LBER_SBIOD_LEVEL_PROVIDER, NULL );
330 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
331 rc = ldap_connect_to_path( ld, conn->lconn_sb,
332 srv->lud_host, async );
333 if ( rc == -1 ) return rc;
335 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
336 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
338 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
339 LBER_SBIOD_LEVEL_PROVIDER, NULL );
342 #endif /* LDAP_PF_LOCAL */
349 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
350 INT_MAX, (void *)"ldap_" );
353 #ifdef LDAP_CONNECTIONLESS
354 if( proto == LDAP_PROTO_UDP ) return 0;
358 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
359 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
361 ++conn->lconn_refcnt; /* avoid premature free */
363 rc = ldap_int_tls_start( ld, conn, srv );
365 --conn->lconn_refcnt;
367 if (rc != LDAP_SUCCESS) {
373 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
374 if ( conn->lconn_krbinstance == NULL ) {
376 conn->lconn_krbinstance = ldap_host_connected_to(
377 conn->lconn_sb, host );
379 if( conn->lconn_krbinstance != NULL &&
380 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
384 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
391 ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
397 rc = ldap_create( ldp );
398 if( rc != LDAP_SUCCESS ) {
403 /* Make it appear that a search request, msgid 0, was sent */
404 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
406 ldap_unbind_ext( *ldp, NULL, NULL );
408 return( LDAP_NO_MEMORY );
410 memset(lr, 0, sizeof( LDAPRequest ));
412 lr->lr_status = LDAP_REQST_INPROGRESS;
413 lr->lr_res_errno = LDAP_SUCCESS;
414 /* no mutex lock needed, we just created this ld here */
415 (*ldp)->ld_requests = lr;
417 /* Attach the passed socket as the *LDAP's connection */
418 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
420 ldap_unbind_ext( *ldp, NULL, NULL );
422 return( LDAP_NO_MEMORY );
424 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
426 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
427 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
429 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
430 LBER_SBIOD_LEVEL_PROVIDER, NULL );
431 (*ldp)->ld_defconn = c;
433 /* Add the connection to the *LDAP's select pool */
434 ldap_mark_select_read( *ldp, c->lconn_sb );
435 ldap_mark_select_write( *ldp, c->lconn_sb );
437 /* Make this connection an LDAP V3 protocol connection */
439 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
441 return( LDAP_SUCCESS );