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.
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;
122 ld->ld_options.ldo_tm_api = NULL;
123 ld->ld_options.ldo_tm_net = NULL;
124 ld->ld_options.ldo_defludp = NULL;
126 #ifdef HAVE_CYRUS_SASL
127 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
128 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
129 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
130 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
131 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
132 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
133 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
134 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
137 if ( gopts->ldo_tm_api &&
138 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_api, gopts->ldo_tm_api ))
141 if ( gopts->ldo_tm_net &&
142 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_net, gopts->ldo_tm_net ))
145 if ( gopts->ldo_defludp ) {
146 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
148 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
151 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
153 ld->ld_lberoptions = LBER_USE_DER;
155 ld->ld_sb = ber_sockbuf_alloc( );
156 if ( ld->ld_sb == NULL ) goto nomem;
158 #ifdef LDAP_R_COMPILE
159 ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
160 ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
166 ldap_free_select_info( ld->ld_selectinfo );
167 ldap_free_urllist( ld->ld_options.ldo_defludp );
168 LDAP_FREE( ld->ld_options.ldo_tm_net );
169 LDAP_FREE( ld->ld_options.ldo_tm_api );
170 #ifdef HAVE_CYRUS_SASL
171 LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
172 LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
173 LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
174 LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
176 LDAP_FREE( (char *)ld );
177 return LDAP_NO_MEMORY;
181 * ldap_init - initialize the LDAP library. A magic cookie to be used for
182 * future communication is returned on success, NULL on failure.
183 * "host" may be a space-separated list of hosts or IP addresses
187 * ld = ldap_init( host, port );
190 ldap_init( LDAP_CONST char *defhost, int defport )
195 rc = ldap_create(&ld);
196 if ( rc != LDAP_SUCCESS )
200 ld->ld_options.ldo_defport = defport;
202 if (defhost != NULL) {
203 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
204 if ( rc != LDAP_SUCCESS ) {
205 ldap_ld_free(ld, 1, NULL, NULL);
215 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
221 rc = ldap_create(&ld);
222 if ( rc != LDAP_SUCCESS )
226 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
227 if ( rc != LDAP_SUCCESS ) {
228 ldap_ld_free(ld, 1, NULL, NULL);
231 #ifdef LDAP_CONNECTIONLESS
232 if (ldap_is_ldapc_url(url))
242 ldap_int_open_connection(
252 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
254 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
256 port = srv->lud_port;
258 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
261 host = srv->lud_host;
265 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
272 rc = ldap_connect_to_host( ld, conn->lconn_sb,
273 proto, host, port, async );
275 if ( rc == -1 ) return rc;
278 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
279 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
281 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
282 LBER_SBIOD_LEVEL_PROVIDER, NULL );
286 #ifdef LDAP_CONNECTIONLESS
288 port = srv->lud_port;
290 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
293 host = srv->lud_host;
296 if( !port ) port = LDAP_PORT;
299 rc = ldap_connect_to_host( ld, conn->lconn_sb,
300 proto, host, port, async );
302 if ( rc == -1 ) return rc;
304 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
305 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
307 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
308 LBER_SBIOD_LEVEL_PROVIDER, NULL );
310 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
311 LBER_SBIOD_LEVEL_PROVIDER, NULL );
317 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
318 rc = ldap_connect_to_path( ld, conn->lconn_sb,
319 srv->lud_host, async );
320 if ( rc == -1 ) return rc;
322 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
323 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
325 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
326 LBER_SBIOD_LEVEL_PROVIDER, NULL );
329 #endif /* LDAP_PF_LOCAL */
336 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
337 INT_MAX, (void *)"ldap_" );
340 #ifdef LDAP_CONNECTIONLESS
341 if( proto == LDAP_PROTO_UDP ) return 0;
345 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
346 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
348 ++conn->lconn_refcnt; /* avoid premature free */
350 rc = ldap_int_tls_start( ld, conn, srv );
352 --conn->lconn_refcnt;
354 if (rc != LDAP_SUCCESS) {
360 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
361 if ( conn->lconn_krbinstance == NULL ) {
363 conn->lconn_krbinstance = ldap_host_connected_to(
364 conn->lconn_sb, host );
366 if( conn->lconn_krbinstance != NULL &&
367 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
371 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
377 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
383 rc = ldap_create( ldp );
384 if( rc != LDAP_SUCCESS ) {
389 /* Make it appear that a search request, msgid 0, was sent */
390 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
394 return( LDAP_NO_MEMORY );
396 memset(lr, 0, sizeof( LDAPRequest ));
398 lr->lr_status = LDAP_REQST_INPROGRESS;
399 lr->lr_res_errno = LDAP_SUCCESS;
400 /* no mutex lock needed, we just created this ld here */
401 (*ldp)->ld_requests = lr;
403 /* Attach the passed socket as the *LDAP's connection */
404 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
408 return( LDAP_NO_MEMORY );
410 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
412 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
413 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
415 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
416 LBER_SBIOD_LEVEL_PROVIDER, NULL );
417 (*ldp)->ld_defconn = c;
419 /* Add the connection to the *LDAP's select pool */
420 ldap_mark_select_read( *ldp, c->lconn_sb );
421 ldap_mark_select_write( *ldp, c->lconn_sb );
423 /* Make this connection an LDAP V3 protocol connection */
425 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
427 return( LDAP_SUCCESS );