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 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_defludp = NULL;
144 ld->ld_options.ldo_sctrls = NULL;
145 ld->ld_options.ldo_cctrls = NULL;
147 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
149 if ( ld->ld_options.ldo_defludp == NULL ) {
150 LDAP_FREE( (char*)ld );
152 return LDAP_NO_MEMORY;
155 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
156 ldap_free_urllist( ld->ld_options.ldo_defludp );
157 LDAP_FREE( (char*) ld );
159 return LDAP_NO_MEMORY;
162 ld->ld_lberoptions = LBER_USE_DER;
164 ld->ld_sb = ber_sockbuf_alloc( );
165 if ( ld->ld_sb == NULL ) {
166 ldap_free_urllist( ld->ld_options.ldo_defludp );
167 LDAP_FREE( (char*) ld );
169 return LDAP_NO_MEMORY;
177 * ldap_init - initialize the LDAP library. A magic cookie to be used for
178 * future communication is returned on success, NULL on failure.
179 * "host" may be a space-separated list of hosts or IP addresses
183 * ld = ldap_init( host, port );
186 ldap_init( LDAP_CONST char *defhost, int defport )
191 rc = ldap_create(&ld);
192 if ( rc != LDAP_SUCCESS )
196 ld->ld_options.ldo_defport = defport;
198 if (defhost != NULL) {
199 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
200 if ( rc != LDAP_SUCCESS ) {
201 ldap_ld_free(ld, 1, NULL, NULL);
211 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
217 rc = ldap_create(&ld);
218 if ( rc != LDAP_SUCCESS )
222 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
223 if ( rc != LDAP_SUCCESS ) {
224 ldap_ld_free(ld, 1, NULL, NULL);
234 ldap_start_tls_s ( LDAP *ld,
235 LDAPControl **serverctrls,
236 LDAPControl **clientctrls )
242 struct berval *rspdata = NULL;
244 if (ld->ld_conns == NULL) {
245 rc = ldap_open_defconn( ld );
246 if (rc != LDAP_SUCCESS)
250 for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
251 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
252 return LDAP_OPERATIONS_ERROR;
254 /* XXYYZ: this initiates operaton only on default connection! */
255 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
256 NULL, serverctrls, clientctrls, &rspoid, &rspdata);
258 if (rc != LDAP_SUCCESS)
264 rc = ldap_pvt_tls_start( ld, lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
265 if (rc != LDAP_SUCCESS)
270 return LDAP_NOT_SUPPORTED;
275 ldap_int_open_connection(
282 #ifdef HAVE_CYRUS_SASL
283 char *sasl_host = NULL;
289 Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
291 port = srv->lud_port;
293 port = ld->ld_options.ldo_defport;
294 port = htons( (short) port );
297 if ( srv->lud_host == NULL || *srv->lud_host == 0 )
298 addr = htonl( INADDR_LOOPBACK );
300 switch ( ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
302 rc = ldap_connect_to_host( ld, conn->lconn_sb, 0,
303 srv->lud_host, addr, port, async );
304 if ( rc == -1 ) return rc;
305 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
306 LBER_SBIOD_LEVEL_PROVIDER, NULL );
308 #ifdef HAVE_CYRUS_SASL
309 sasl_host = ldap_host_connected_to( conn->lconn_sb );
313 rc = ldap_connect_to_host( ld, conn->lconn_sb, 1,
314 srv->lud_host, addr, port, async );
315 if ( rc == -1 ) return rc;
316 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
317 LBER_SBIOD_LEVEL_PROVIDER, NULL );
321 /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
322 rc = ldap_connect_to_path( ld, conn->lconn_sb, 0,
323 srv->lud_host, async );
324 if ( rc == -1 ) return rc;
325 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
326 LBER_SBIOD_LEVEL_PROVIDER, NULL );
328 #ifdef HAVE_CYRUS_SASL
329 sasl_host = ldap_host_connected_to( conn->lconn_sb );
330 sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
333 #endif /* LDAP_PF_LOCAL */
339 #ifdef HAVE_CYRUS_SASL
340 if( sasl_host != NULL ) {
341 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
345 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
346 LBER_SBIOD_LEVEL_PROVIDER, NULL );
348 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
353 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
354 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
356 rc = ldap_pvt_tls_start( ld, conn->lconn_sb,
357 ld->ld_options.ldo_tls_ctx );
358 if (rc != LDAP_SUCCESS)
363 if ( conn->lconn_krbinstance != NULL ) {
364 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
366 conn->lconn_krbinstance = ldap_host_connected_to( conn->sb );
368 if( conn->lconn_krbinstance != NULL &&
369 ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
372 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
373 conn->lconn_krbinstance = NULL;
374 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */