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 ld->ld_sb = ber_sockbuf_alloc( );
159 if ( ld->ld_sb == NULL ) {
160 ldap_free_urllist( ld->ld_options.ldo_defludp );
161 LDAP_FREE( (char*) ld );
163 return LDAP_NO_MEMORY;
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 ( ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
286 rc = ldap_connect_to_host( ld, sb, srv->lud_host,
290 ber_sockbuf_add_io( sb, &ber_sockbuf_io_tcp,
291 LBER_SBIOD_LEVEL_PROVIDER, NULL );
294 rc = ldap_connect_to_host( ld, sb, srv->lud_host,
298 ber_sockbuf_add_io( sb, &ber_sockbuf_io_udp,
299 LBER_SBIOD_LEVEL_PROVIDER, NULL );
302 case LDAP_PROTO_LOCAL:
303 rc = ldap_connect_to_path( ld, sb, srv->lud_host,
307 ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd,
308 LBER_SBIOD_LEVEL_PROVIDER, NULL );
310 #endif /* LDAP_PF_LOCAL */
316 ber_sockbuf_add_io( sb, &ber_sockbuf_io_readahead,
317 LBER_SBIOD_LEVEL_PROVIDER, NULL );
319 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
323 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
324 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
326 rc = ldap_pvt_tls_start( ld, sb, ld->ld_options.ldo_tls_ctx );
327 if (rc != LDAP_SUCCESS)
332 if ( krbinstancep != NULL ) {
333 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
335 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
336 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
339 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
340 *krbinstancep = NULL;
341 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */