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\n", 0, 0, 0 );
59 if (( ld = ldap_init( host, port )) == NULL ) {
63 rc = ldap_open_defconn( ld );
66 ldap_ld_free( ld, 0, NULL, NULL );
70 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
71 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
79 ldap_create( LDAP **ldp )
82 struct ldapoptions *gopts;
85 /* Get pointer to global option structure */
86 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
87 return LDAP_NO_MEMORY;
90 /* Initialize the global options, if not already done. */
91 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
92 ldap_int_initialize(gopts, NULL);
95 Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
98 { WORD wVersionRequested;
101 wVersionRequested = MAKEWORD( 2, 0 );
102 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
103 /* Tell the user that we couldn't find a usable */
105 return LDAP_LOCAL_ERROR;
108 /* Confirm that the WinSock DLL supports 2.0.*/
109 /* Note that if the DLL supports versions greater */
110 /* than 2.0 in addition to 2.0, it will still return */
111 /* 2.0 in wVersion since that is the version we */
114 if ( LOBYTE( wsaData.wVersion ) != 2 ||
115 HIBYTE( wsaData.wVersion ) != 0 )
117 /* Tell the user that we couldn't find a usable */
120 return LDAP_LOCAL_ERROR;
122 } /* The WinSock DLL is acceptable. Proceed. */
126 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
127 return LDAP_LOCAL_ERROR;
132 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
134 return( LDAP_NO_MEMORY );
137 /* copy the global options */
138 AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
140 ld->ld_valid = LDAP_VALID_SESSION;
142 /* but not pointers to malloc'ed items */
143 ld->ld_options.ldo_sctrls = NULL;
144 ld->ld_options.ldo_cctrls = NULL;
146 #ifdef HAVE_CYRUS_SASL
147 ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
148 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
149 ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
150 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
151 ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
152 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
153 ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
154 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
157 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
159 if ( ld->ld_options.ldo_defludp == NULL ) {
160 LDAP_FREE( (char*)ld );
162 return LDAP_NO_MEMORY;
165 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
166 ldap_free_urllist( ld->ld_options.ldo_defludp );
167 LDAP_FREE( (char*) ld );
169 return LDAP_NO_MEMORY;
172 ld->ld_lberoptions = LBER_USE_DER;
174 ld->ld_sb = ber_sockbuf_alloc( );
175 if ( ld->ld_sb == NULL ) {
176 ldap_free_urllist( ld->ld_options.ldo_defludp );
177 LDAP_FREE( (char*) ld );
179 return LDAP_NO_MEMORY;
187 * ldap_init - initialize the LDAP library. A magic cookie to be used for
188 * future communication is returned on success, NULL on failure.
189 * "host" may be a space-separated list of hosts or IP addresses
193 * ld = ldap_init( host, port );
196 ldap_init( LDAP_CONST char *defhost, int defport )
201 rc = ldap_create(&ld);
202 if ( rc != LDAP_SUCCESS )
206 ld->ld_options.ldo_defport = defport;
208 if (defhost != NULL) {
209 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
210 if ( rc != LDAP_SUCCESS ) {
211 ldap_ld_free(ld, 1, NULL, NULL);
221 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
227 rc = ldap_create(&ld);
228 if ( rc != LDAP_SUCCESS )
232 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
233 if ( rc != LDAP_SUCCESS ) {
234 ldap_ld_free(ld, 1, NULL, NULL);
244 ldap_int_open_connection(
251 #ifdef HAVE_CYRUS_SASL
252 char *sasl_host = NULL;
259 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
261 switch ( ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
263 port = htons( (short) srv->lud_port );
266 if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
268 addr = htonl( INADDR_LOOPBACK );
270 host = srv->lud_host;
273 rc = ldap_connect_to_host( ld, conn->lconn_sb, 0,
274 host, addr, port, async );
276 if ( rc == -1 ) return rc;
279 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
280 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
282 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
283 LBER_SBIOD_LEVEL_PROVIDER, NULL );
285 #ifdef HAVE_CYRUS_SASL
286 sasl_host = ldap_host_connected_to( conn->lconn_sb );
291 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
292 rc = ldap_connect_to_path( ld, conn->lconn_sb,
293 srv->lud_host, async );
294 if ( rc == -1 ) return rc;
297 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
298 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
300 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
301 LBER_SBIOD_LEVEL_PROVIDER, NULL );
303 #ifdef HAVE_CYRUS_SASL
304 sasl_host = ldap_host_connected_to( conn->lconn_sb );
305 sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
308 #endif /* LDAP_PF_LOCAL */
314 #ifdef HAVE_CYRUS_SASL
315 if( sasl_host != NULL ) {
316 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
320 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
321 LBER_SBIOD_LEVEL_PROVIDER, NULL );
324 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
325 INT_MAX, (void *)"ldap_" );
329 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
330 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
332 LDAPConn *savedefconn = ld->ld_defconn;
333 ++conn->lconn_refcnt; /* avoid premature free */
334 ld->ld_defconn = conn;
336 rc = ldap_pvt_tls_start( ld, conn->lconn_sb,
337 ld->ld_options.ldo_tls_ctx );
339 ld->ld_defconn = savedefconn;
340 --conn->lconn_refcnt;
342 if (rc != LDAP_SUCCESS) {
348 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
349 if ( conn->lconn_krbinstance == NULL ) {
351 conn->lconn_krbinstance = ldap_host_connected_to( conn->lconn_sb );
353 if( conn->lconn_krbinstance != NULL &&
354 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
358 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
364 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
370 rc = ldap_create( ldp );
371 if( rc != LDAP_SUCCESS ) {
376 /* Make it appear that a search request, msgid 0, was sent */
377 lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
381 return( LDAP_NO_MEMORY );
383 memset(lr, 0, sizeof( LDAPRequest ));
385 lr->lr_status = LDAP_REQST_INPROGRESS;
386 lr->lr_res_errno = LDAP_SUCCESS;
387 (*ldp)->ld_requests = lr;
389 /* Attach the passed socket as the *LDAP's connection */
390 c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
394 return( LDAP_NO_MEMORY );
396 ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
398 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
399 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
401 ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
402 LBER_SBIOD_LEVEL_PROVIDER, NULL );
403 (*ldp)->ld_defconn = c;
405 /* Add the connection to the *LDAP's select pool */
406 ldap_mark_select_read( *ldp, c->lconn_sb );
407 ldap_mark_select_write( *ldp, c->lconn_sb );
409 /* Make this connection an LDAP V3 protocol connection */
411 ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
413 return( LDAP_SUCCESS );