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 if (( ld->ld_defconn = ldap_new_connection( ld, ld->ld_options.ldo_defludp, 1,1,NULL )) == NULL )
31 ld->ld_errno = LDAP_SERVER_DOWN;
35 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
41 * ldap_open - initialize and connect to an ldap server. A magic cookie to
42 * be used for future communication is returned on success, NULL on failure.
43 * "host" may be a space-separated list of hosts or IP addresses
47 * ld = ldap_open( hostname, port );
51 ldap_open( LDAP_CONST char *host, int port )
56 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
58 if (( ld = ldap_init( host, port )) == NULL ) {
62 rc = ldap_open_defconn( ld );
65 ldap_ld_free( ld, 0, NULL, NULL );
69 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
70 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
78 ldap_create( LDAP **ldp )
83 if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
84 ldap_int_initialize(NULL);
87 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
90 { WORD wVersionRequested;
93 wVersionRequested = MAKEWORD( 2, 0 );
94 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
95 /* Tell the user that we couldn't find a usable */
97 return LDAP_LOCAL_ERROR;
100 /* Confirm that the WinSock DLL supports 2.0.*/
101 /* Note that if the DLL supports versions greater */
102 /* than 2.0 in addition to 2.0, it will still return */
103 /* 2.0 in wVersion since that is the version we */
106 if ( LOBYTE( wsaData.wVersion ) != 2 ||
107 HIBYTE( wsaData.wVersion ) != 0 )
109 /* Tell the user that we couldn't find a usable */
112 return LDAP_LOCAL_ERROR;
114 } /* The WinSock DLL is acceptable. Proceed. */
118 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
119 return LDAP_LOCAL_ERROR;
124 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
126 return( LDAP_NO_MEMORY );
129 /* copy the global options */
130 memcpy(&ld->ld_options, &ldap_int_global_options,
131 sizeof(ld->ld_options));
133 ld->ld_valid = LDAP_VALID_SESSION;
135 /* but not pointers to malloc'ed items */
136 ld->ld_options.ldo_defludp = NULL;
137 ld->ld_options.ldo_sctrls = NULL;
138 ld->ld_options.ldo_cctrls = NULL;
140 ld->ld_options.ldo_defludp =
141 ldap_url_duplist(ldap_int_global_options.ldo_defludp);
143 if ( ld->ld_options.ldo_defludp == NULL ) {
144 LDAP_FREE( (char*)ld );
146 return LDAP_NO_MEMORY;
149 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
150 ldap_free_urllist( ld->ld_options.ldo_defludp );
151 LDAP_FREE( (char*) ld );
153 return LDAP_NO_MEMORY;
156 ld->ld_lberoptions = LBER_USE_DER;
158 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
159 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
160 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
161 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
162 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
163 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
165 ld->ld_sb = ber_sockbuf_alloc( );
166 if ( ld->ld_sb == NULL ) {
167 ldap_free_urllist( ld->ld_options.ldo_defludp );
168 LDAP_FREE( (char*) ld );
170 return LDAP_NO_MEMORY;
178 * ldap_init - initialize the LDAP library. A magic cookie to be used for
179 * future communication is returned on success, NULL on failure.
180 * "host" may be a space-separated list of hosts or IP addresses
184 * ld = ldap_open( host, port );
187 ldap_init( LDAP_CONST char *defhost, int defport )
192 rc = ldap_create(&ld);
193 if ( rc != LDAP_SUCCESS )
197 ld->ld_options.ldo_defport = defport;
199 if (defhost != NULL) {
200 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
201 if ( rc != LDAP_SUCCESS ) {
202 ldap_ld_free(ld, 1, NULL, NULL);
212 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
218 rc = ldap_create(&ld);
219 if ( rc != LDAP_SUCCESS )
223 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
224 if ( rc != LDAP_SUCCESS ) {
225 ldap_ld_free(ld, 1, NULL, NULL);
235 ldap_start_tls ( LDAP *ld,
236 LDAPControl **serverctrls,
237 LDAPControl **clientctrls )
243 struct berval *rspdata = NULL;
245 if (ld->ld_conns == NULL) {
246 rc = ldap_open_defconn( ld );
247 if (rc != LDAP_SUCCESS)
251 for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
252 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
253 return LDAP_OPERATIONS_ERROR;
254 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
255 NULL, serverctrls, clientctrls, &rspoid, &rspdata);
256 if (rc != LDAP_SUCCESS)
262 rc = ldap_pvt_tls_start( ld, lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
263 if (rc != LDAP_SUCCESS)
268 return LDAP_NOT_SUPPORTED;
273 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
274 char **krbinstancep, int async )
280 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
282 port = srv->lud_port;
284 port = ld->ld_options.ldo_defport;
285 port = htons( (short) port );
288 if ( srv->lud_host == NULL || *srv->lud_host == 0 )
289 addr = htonl( INADDR_LOOPBACK );
291 switch ( srv->lud_protocol ) {
293 rc = ldap_connect_to_host( ld, sb, srv->lud_host,
297 ber_sockbuf_add_io( sb, &ber_sockbuf_io_tcp,
298 LBER_SBIOD_LEVEL_PROVIDER, NULL );
301 rc = ldap_connect_to_host( ld, sb, srv->lud_host,
305 ber_sockbuf_add_io( sb, &ber_sockbuf_io_udp,
306 LBER_SBIOD_LEVEL_PROVIDER, NULL );
309 case LDAP_PROTO_LOCAL:
310 rc = ldap_connect_to_path( ld, sb, srv->lud_host,
314 ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd,
315 LBER_SBIOD_LEVEL_PROVIDER, NULL );
317 #endif /* LDAP_PF_LOCAL */
323 ber_sockbuf_add_io( sb, &ber_sockbuf_io_readahead,
324 LBER_SBIOD_LEVEL_PROVIDER, NULL );
326 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
330 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
331 (srv->lud_properties & LDAP_URL_USE_SSL)) {
332 rc = ldap_pvt_tls_start( ld, sb, ld->ld_options.ldo_tls_ctx );
333 if (rc != LDAP_SUCCESS)
338 if ( krbinstancep != NULL ) {
339 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
341 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
342 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
345 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
346 *krbinstancep = NULL;
347 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */