3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
18 #include <ac/stdlib.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
27 int ldap_open_defconn( LDAP *ld )
29 ld->ld_defconn = ldap_new_connection( ld,
30 ld->ld_options.ldo_defludp, 1, 1, NULL );
32 if( ld->ld_defconn == NULL ) {
33 ld->ld_errno = LDAP_SERVER_DOWN;
37 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
42 * ldap_open - initialize and connect to an ldap server. A magic cookie to
43 * be used for future communication is returned on success, NULL on failure.
44 * "host" may be a space-separated list of hosts or IP addresses
48 * ld = ldap_open( hostname, port );
52 ldap_open( LDAP_CONST char *host, int port )
57 Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
60 ld = ldap_init( host, port );
65 rc = ldap_open_defconn( ld );
68 ldap_ld_free( ld, 0, NULL, NULL );
72 Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
73 ld == NULL ? "succeeded" : "failed", 0, 0 );
81 ldap_create( LDAP **ldp )
84 struct ldapoptions *gopts;
87 /* Get pointer to global option structure */
88 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
89 return LDAP_NO_MEMORY;
92 /* Initialize the global options, if not already done. */
93 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
94 ldap_int_initialize(gopts, NULL);
97 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
100 { WORD wVersionRequested;
103 wVersionRequested = MAKEWORD( 2, 0 );
104 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
105 /* Tell the user that we couldn't find a usable */
107 return LDAP_LOCAL_ERROR;
110 /* Confirm that the WinSock DLL supports 2.0.*/
111 /* Note that if the DLL supports versions greater */
112 /* than 2.0 in addition to 2.0, it will still return */
113 /* 2.0 in wVersion since that is the version we */
116 if ( LOBYTE( wsaData.wVersion ) != 2 ||
117 HIBYTE( wsaData.wVersion ) != 0 )
119 /* Tell the user that we couldn't find a usable */
122 return LDAP_LOCAL_ERROR;
124 } /* The WinSock DLL is acceptable. Proceed. */
128 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
129 return LDAP_LOCAL_ERROR;
134 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
136 return( LDAP_NO_MEMORY );
139 /* copy the global options */
140 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
142 ld->ld_valid = LDAP_VALID_SESSION;
144 /* but not pointers to malloc'ed items */
145 ld->ld_options.ldo_sctrls = NULL;
146 ld->ld_options.ldo_cctrls = NULL;
148 #ifdef HAVE_CYRUS_SASL
149 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
150 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
151 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
152 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
153 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
154 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
155 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
156 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
159 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
161 if ( ld->ld_options.ldo_defludp == NULL ) {
162 LDAP_FREE( (char*)ld );
164 return LDAP_NO_MEMORY;
167 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
168 ldap_free_urllist( ld->ld_options.ldo_defludp );
169 LDAP_FREE( (char*) ld );
171 return LDAP_NO_MEMORY;
174 ld->ld_lberoptions = LBER_USE_DER;
176 ld->ld_sb = ber_sockbuf_alloc( );
177 if ( ld->ld_sb == NULL ) {
178 ldap_free_urllist( ld->ld_options.ldo_defludp );
179 LDAP_FREE( (char*) ld );
181 return LDAP_NO_MEMORY;
189 * ldap_init - initialize the LDAP library. A magic cookie to be used for
190 * future communication is returned on success, NULL on failure.
191 * "host" may be a space-separated list of hosts or IP addresses
195 * ld = ldap_init( host, port );
198 ldap_init( LDAP_CONST char *defhost, int defport )
203 rc = ldap_create(&ld);
204 if ( rc != LDAP_SUCCESS )
208 ld->ld_options.ldo_defport = defport;
210 if (defhost != NULL) {
211 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
212 if ( rc != LDAP_SUCCESS ) {
213 ldap_ld_free(ld, 1, NULL, NULL);
223 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
229 rc = ldap_create(&ld);
230 if ( rc != LDAP_SUCCESS )
234 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
235 if ( rc != LDAP_SUCCESS ) {
236 ldap_ld_free(ld, 1, NULL, NULL);
239 if (ldap_is_ldapc_url(url))
240 ld->ld_options.ldo_valid |= LDAP_UDP_SESSION;
248 ldap_int_open_connection(
255 #ifdef HAVE_CYRUS_SASL
256 char *sasl_host = NULL;
263 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
265 switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
267 port = htons( (short) srv->lud_port );
270 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
272 addr = htonl( INADDR_LOOPBACK );
274 host = srv->lud_host;
277 rc = ldap_connect_to_host( ld, conn->lconn_sb,
278 proto, host, addr, port, async );
280 if ( rc == -1 ) return rc;
283 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
284 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
286 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
287 LBER_SBIOD_LEVEL_PROVIDER, NULL );
289 #ifdef HAVE_CYRUS_SASL
290 sasl_host = ldap_host_connected_to( conn->lconn_sb );
293 #ifdef LDAP_CONNECTIONLESS
295 port = htons( (short) srv->lud_port );
298 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
300 addr = htonl( INADDR_LOOPBACK );
302 host = srv->lud_host;
304 ld->ld_options.ldo_valid |= LDAP_UDP_SESSION;
305 rc = ldap_connect_to_host( ld, conn->lconn_sb,
306 proto, host, addr, port, async );
308 if ( rc == -1 ) return rc;
310 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
311 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
313 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
314 LBER_SBIOD_LEVEL_PROVIDER, NULL );
319 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
320 rc = ldap_connect_to_path( ld, conn->lconn_sb,
321 srv->lud_host, async );
322 if ( rc == -1 ) return rc;
324 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
325 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
327 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
328 LBER_SBIOD_LEVEL_PROVIDER, NULL );
330 #ifdef HAVE_CYRUS_SASL
331 sasl_host = ldap_host_connected_to( conn->lconn_sb );
332 sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
335 #endif /* LDAP_PF_LOCAL */
341 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
342 LBER_SBIOD_LEVEL_PROVIDER, NULL );
345 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
346 INT_MAX, (void *)"ldap_" );
349 #ifdef LDAP_CONNECTIONLESS
350 if( proto == LDAP_PROTO_UDP )
354 #ifdef HAVE_CYRUS_SASL
355 /* establish Cyrus SASL context prior to starting TLS so
356 that SASL EXTERNAL might be used */
357 if( sasl_host != NULL ) {
358 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
363 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
364 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
366 ++conn->lconn_refcnt; /* avoid premature free */
368 rc = ldap_int_tls_start( ld, conn, srv );
370 --conn->lconn_refcnt;
372 if (rc != LDAP_SUCCESS) {
378 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
379 if ( conn->lconn_krbinstance == NULL ) {
381 conn->lconn_krbinstance = ldap_host_connected_to( conn->lconn_sb );
383 if( conn->lconn_krbinstance != NULL &&
384 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
388 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
394 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
400 rc = ldap_create( ldp );
401 if( rc != LDAP_SUCCESS ) {
406 /* Make it appear that a search request, msgid 0, was sent */
407 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
411 return( LDAP_NO_MEMORY );
413 memset(lr, 0, sizeof( LDAPRequest ));
415 lr->lr_status = LDAP_REQST_INPROGRESS;
416 lr->lr_res_errno = LDAP_SUCCESS;
417 (*ldp)->ld_requests = lr;
419 /* Attach the passed socket as the *LDAP's connection */
420 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
424 return( LDAP_NO_MEMORY );
426 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
428 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
429 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
431 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
432 LBER_SBIOD_LEVEL_PROVIDER, NULL );
433 (*ldp)->ld_defconn = c;
435 /* Add the connection to the *LDAP's select pool */
436 ldap_mark_select_read( *ldp, c->lconn_sb );
437 ldap_mark_select_write( *ldp, c->lconn_sb );
439 /* Make this connection an LDAP V3 protocol connection */
441 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
443 return( LDAP_SUCCESS );