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.
17 #include <ac/stdlib.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
26 int ldap_open_defconn( LDAP *ld )
28 if (( ld->ld_defconn = ldap_new_connection( ld, ld->ld_options.ldo_defludp, 1,1,0 )) == NULL )
30 ld->ld_errno = LDAP_SERVER_DOWN;
34 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
40 * ldap_open - initialize and connect to an ldap server. A magic cookie to
41 * be used for future communication is returned on success, NULL on failure.
42 * "host" may be a space-separated list of hosts or IP addresses
46 * ld = ldap_open( hostname, port );
50 ldap_open( LDAP_CONST char *host, int port )
55 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
57 if (( ld = ldap_init( host, port )) == NULL ) {
61 rc = ldap_open_defconn( ld );
64 ldap_ld_free( ld, 0, NULL, NULL );
68 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
69 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
77 ldap_create( LDAP **ldp )
82 if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
83 ldap_int_initialize(NULL);
86 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
89 { WORD wVersionRequested;
92 wVersionRequested = MAKEWORD( 2, 0 );
93 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
94 /* Tell the user that we couldn't find a usable */
96 return LDAP_LOCAL_ERROR;
99 /* Confirm that the WinSock DLL supports 2.0.*/
100 /* Note that if the DLL supports versions greater */
101 /* than 2.0 in addition to 2.0, it will still return */
102 /* 2.0 in wVersion since that is the version we */
105 if ( LOBYTE( wsaData.wVersion ) != 2 ||
106 HIBYTE( wsaData.wVersion ) != 0 )
108 /* Tell the user that we couldn't find a usable */
111 return LDAP_LOCAL_ERROR;
113 } /* The WinSock DLL is acceptable. Proceed. */
117 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
118 return LDAP_LOCAL_ERROR;
123 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
125 return( LDAP_NO_MEMORY );
128 /* copy the global options */
129 memcpy(&ld->ld_options, &ldap_int_global_options,
130 sizeof(ld->ld_options));
132 ld->ld_valid = LDAP_VALID_SESSION;
134 /* but not pointers to malloc'ed items */
135 ld->ld_options.ldo_defludp = NULL;
136 ld->ld_options.ldo_sctrls = NULL;
137 ld->ld_options.ldo_cctrls = NULL;
139 ld->ld_options.ldo_defludp =
140 ldap_url_duplist(ldap_int_global_options.ldo_defludp);
142 if ( ld->ld_options.ldo_defludp == NULL ) {
143 LDAP_FREE( (char*)ld );
145 return LDAP_NO_MEMORY;
148 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
149 ldap_free_urllist( ld->ld_options.ldo_defludp );
150 LDAP_FREE( (char*) ld );
152 return LDAP_NO_MEMORY;
155 ld->ld_lberoptions = LBER_USE_DER;
157 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
158 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
159 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
160 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
161 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
162 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
164 ber_pvt_sb_init( &(ld->ld_sb) );
171 * ldap_init - initialize the LDAP library. A magic cookie to be used for
172 * future communication is returned on success, NULL on failure.
173 * "host" may be a space-separated list of hosts or IP addresses
177 * ld = ldap_open( host, port );
180 ldap_init( LDAP_CONST char *defhost, int defport )
185 rc = ldap_create(&ld);
186 if ( rc != LDAP_SUCCESS )
190 ld->ld_options.ldo_defport = defport;
192 if (defhost != NULL) {
193 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
194 if ( rc != LDAP_SUCCESS ) {
195 ldap_ld_free(ld, 1, NULL, NULL);
205 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
211 rc = ldap_create(&ld);
212 if ( rc != LDAP_SUCCESS )
216 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
217 if ( rc != LDAP_SUCCESS ) {
218 ldap_ld_free(ld, 1, NULL, NULL);
228 ldap_start_tls ( LDAP *ld,
229 LDAPControl **serverctrls,
230 LDAPControl **clientctrls )
236 struct berval *rspdata = NULL;
238 if (ld->ld_conns == NULL) {
239 rc = ldap_open_defconn( ld );
240 if (rc != LDAP_SUCCESS)
244 for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
245 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
246 return LDAP_OPERATIONS_ERROR;
247 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
248 NULL, serverctrls, clientctrls, &rspoid, &rspdata);
249 if (rc != LDAP_SUCCESS)
255 rc = ldap_pvt_tls_start( ld, lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
256 if (rc != LDAP_SUCCESS)
261 return LDAP_NOT_SUPPORTED;
266 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
267 char **krbinstancep, int async )
273 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
275 port = srv->lud_port;
277 port = ld->ld_options.ldo_defport;
278 port = htons( (short) port );
281 if ( srv->lud_host == NULL || *srv->lud_host == 0 )
282 addr = htonl( INADDR_LOOPBACK );
284 switch ( srv->lud_protocol ) {
287 rc = ldap_connect_to_host( ld, sb, srv->lud_host, addr, port, async );
290 case LDAP_PROTO_LOCAL:
291 rc = ldap_connect_to_path( ld, sb, srv->lud_host, async );
293 #endif /* LDAP_PF_LOCAL */
303 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
306 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
307 (srv->lud_properties & LDAP_URL_USE_SSL)) {
308 rc = ldap_pvt_tls_start( ld, sb, ld->ld_options.ldo_tls_ctx );
309 if (rc != LDAP_SUCCESS)
314 if ( krbinstancep != NULL ) {
315 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
317 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
318 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
321 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
322 *krbinstancep = NULL;
323 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */