2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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;
139 if ( gopts->ldo_tm_api &&
140 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_api, gopts->ldo_tm_api ))
143 if ( gopts->ldo_tm_net &&
144 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_net, gopts->ldo_tm_net ))
147 if ( gopts->ldo_defludp ) {
148 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
150 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
153 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
155 ld->ld_lberoptions = LBER_USE_DER;
157 ld->ld_sb = ber_sockbuf_alloc( );
158 if ( ld->ld_sb == NULL ) goto nomem;
160 #ifdef LDAP_R_COMPILE
161 ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
162 ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
163 ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
169 ldap_free_select_info( ld->ld_selectinfo );
170 ldap_free_urllist( ld->ld_options.ldo_defludp );
171 LDAP_FREE( ld->ld_options.ldo_tm_net );
172 LDAP_FREE( ld->ld_options.ldo_tm_api );
173 #ifdef HAVE_CYRUS_SASL
174 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
175 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
176 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
177 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
179 LDAP_FREE( (char *)ld );
180 return LDAP_NO_MEMORY;
184 * ldap_init - initialize the LDAP library. A magic cookie to be used for
185 * future communication is returned on success, NULL on failure.
186 * "host" may be a space-separated list of hosts or IP addresses
190 * ld = ldap_init( host, port );
193 ldap_init( LDAP_CONST char *defhost, int defport )
198 rc = ldap_create(&ld);
199 if ( rc != LDAP_SUCCESS )
203 ld->ld_options.ldo_defport = defport;
205 if (defhost != NULL) {
206 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
207 if ( rc != LDAP_SUCCESS ) {
208 ldap_ld_free(ld, 1, NULL, NULL);
218 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
224 rc = ldap_create(&ld);
225 if ( rc != LDAP_SUCCESS )
229 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
230 if ( rc != LDAP_SUCCESS ) {
231 ldap_ld_free(ld, 1, NULL, NULL);
234 #ifdef LDAP_CONNECTIONLESS
235 if (ldap_is_ldapc_url(url))
245 ldap_int_open_connection(
255 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
257 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
259 port = srv->lud_port;
261 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
264 host = srv->lud_host;
268 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
275 rc = ldap_connect_to_host( ld, conn->lconn_sb,
276 proto, host, port, async );
278 if ( rc == -1 ) return rc;
281 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
282 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
284 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
285 LBER_SBIOD_LEVEL_PROVIDER, NULL );
289 #ifdef LDAP_CONNECTIONLESS
291 port = srv->lud_port;
293 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
296 host = srv->lud_host;
299 if( !port ) port = LDAP_PORT;
302 rc = ldap_connect_to_host( ld, conn->lconn_sb,
303 proto, host, port, async );
305 if ( rc == -1 ) return rc;
307 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
308 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
310 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
311 LBER_SBIOD_LEVEL_PROVIDER, NULL );
313 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
314 LBER_SBIOD_LEVEL_PROVIDER, NULL );
320 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
321 rc = ldap_connect_to_path( ld, conn->lconn_sb,
322 srv->lud_host, async );
323 if ( rc == -1 ) return rc;
325 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
326 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
328 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
329 LBER_SBIOD_LEVEL_PROVIDER, NULL );
332 #endif /* LDAP_PF_LOCAL */
339 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
340 INT_MAX, (void *)"ldap_" );
343 #ifdef LDAP_CONNECTIONLESS
344 if( proto == LDAP_PROTO_UDP ) return 0;
348 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
349 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
351 ++conn->lconn_refcnt; /* avoid premature free */
353 rc = ldap_int_tls_start( ld, conn, srv );
355 --conn->lconn_refcnt;
357 if (rc != LDAP_SUCCESS) {
363 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
364 if ( conn->lconn_krbinstance == NULL ) {
366 conn->lconn_krbinstance = ldap_host_connected_to(
367 conn->lconn_sb, host );
369 if( conn->lconn_krbinstance != NULL &&
370 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
374 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
380 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
386 rc = ldap_create( ldp );
387 if( rc != LDAP_SUCCESS ) {
392 /* Make it appear that a search request, msgid 0, was sent */
393 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
397 return( LDAP_NO_MEMORY );
399 memset(lr, 0, sizeof( LDAPRequest ));
401 lr->lr_status = LDAP_REQST_INPROGRESS;
402 lr->lr_res_errno = LDAP_SUCCESS;
403 /* no mutex lock needed, we just created this ld here */
404 (*ldp)->ld_requests = lr;
406 /* Attach the passed socket as the *LDAP's connection */
407 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
411 return( LDAP_NO_MEMORY );
413 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
415 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
416 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
418 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
419 LBER_SBIOD_LEVEL_PROVIDER, NULL );
420 (*ldp)->ld_defconn = c;
422 /* Add the connection to the *LDAP's select pool */
423 ldap_mark_select_read( *ldp, c->lconn_sb );
424 ldap_mark_select_write( *ldp, c->lconn_sb );
426 /* Make this connection an LDAP V3 protocol connection */
428 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
430 return( LDAP_SUCCESS );