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 )
81 struct ldapoptions *gopts;
84 /* Get pointer to global option structure */
85 if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
86 return LDAP_NO_MEMORY;
89 /* Initialize the global options, if not already done. */
90 if( gopts->ldo_valid != LDAP_INITIALIZED ) {
91 ldap_int_initialize(gopts, NULL);
94 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
97 { WORD wVersionRequested;
100 wVersionRequested = MAKEWORD( 2, 0 );
101 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
102 /* Tell the user that we couldn't find a usable */
104 return LDAP_LOCAL_ERROR;
107 /* Confirm that the WinSock DLL supports 2.0.*/
108 /* Note that if the DLL supports versions greater */
109 /* than 2.0 in addition to 2.0, it will still return */
110 /* 2.0 in wVersion since that is the version we */
113 if ( LOBYTE( wsaData.wVersion ) != 2 ||
114 HIBYTE( wsaData.wVersion ) != 0 )
116 /* Tell the user that we couldn't find a usable */
119 return LDAP_LOCAL_ERROR;
121 } /* The WinSock DLL is acceptable. Proceed. */
125 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
126 return LDAP_LOCAL_ERROR;
131 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
133 return( LDAP_NO_MEMORY );
136 /* copy the global options */
137 memcpy(&ld->ld_options, gopts, sizeof(ld->ld_options));
139 ld->ld_valid = LDAP_VALID_SESSION;
141 /* but not pointers to malloc'ed items */
142 ld->ld_options.ldo_defludp = NULL;
143 ld->ld_options.ldo_sctrls = NULL;
144 ld->ld_options.ldo_cctrls = NULL;
146 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
148 if ( ld->ld_options.ldo_defludp == NULL ) {
149 LDAP_FREE( (char*)ld );
151 return LDAP_NO_MEMORY;
154 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
155 ldap_free_urllist( ld->ld_options.ldo_defludp );
156 LDAP_FREE( (char*) ld );
158 return LDAP_NO_MEMORY;
161 ld->ld_lberoptions = LBER_USE_DER;
163 ld->ld_sb = ber_sockbuf_alloc( );
164 if ( ld->ld_sb == NULL ) {
165 ldap_free_urllist( ld->ld_options.ldo_defludp );
166 LDAP_FREE( (char*) ld );
168 return LDAP_NO_MEMORY;
176 * ldap_init - initialize the LDAP library. A magic cookie to be used for
177 * future communication is returned on success, NULL on failure.
178 * "host" may be a space-separated list of hosts or IP addresses
182 * ld = ldap_open( host, port );
185 ldap_init( LDAP_CONST char *defhost, int defport )
190 rc = ldap_create(&ld);
191 if ( rc != LDAP_SUCCESS )
195 ld->ld_options.ldo_defport = defport;
197 if (defhost != NULL) {
198 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
199 if ( rc != LDAP_SUCCESS ) {
200 ldap_ld_free(ld, 1, NULL, NULL);
210 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
216 rc = ldap_create(&ld);
217 if ( rc != LDAP_SUCCESS )
221 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
222 if ( rc != LDAP_SUCCESS ) {
223 ldap_ld_free(ld, 1, NULL, NULL);
233 ldap_start_tls ( LDAP *ld,
234 LDAPControl **serverctrls,
235 LDAPControl **clientctrls )
241 struct berval *rspdata = NULL;
243 if (ld->ld_conns == NULL) {
244 rc = ldap_open_defconn( ld );
245 if (rc != LDAP_SUCCESS)
249 for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
250 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
251 return LDAP_OPERATIONS_ERROR;
252 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
253 NULL, serverctrls, clientctrls, &rspoid, &rspdata);
254 if (rc != LDAP_SUCCESS)
260 rc = ldap_pvt_tls_start( ld, lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
261 if (rc != LDAP_SUCCESS)
266 return LDAP_NOT_SUPPORTED;
271 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
272 char **krbinstancep, int async )
278 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
280 port = srv->lud_port;
282 port = ld->ld_options.ldo_defport;
283 port = htons( (short) port );
286 if ( srv->lud_host == NULL || *srv->lud_host == 0 )
287 addr = htonl( INADDR_LOOPBACK );
289 switch ( ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
291 rc = ldap_connect_to_host( ld, sb, srv->lud_host,
295 ber_sockbuf_add_io( sb, &ber_sockbuf_io_tcp,
296 LBER_SBIOD_LEVEL_PROVIDER, NULL );
299 rc = ldap_connect_to_host( ld, sb, srv->lud_host,
303 ber_sockbuf_add_io( sb, &ber_sockbuf_io_udp,
304 LBER_SBIOD_LEVEL_PROVIDER, NULL );
307 case LDAP_PROTO_LOCAL:
308 rc = ldap_connect_to_path( ld, sb, srv->lud_host,
312 ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd,
313 LBER_SBIOD_LEVEL_PROVIDER, NULL );
315 #endif /* LDAP_PF_LOCAL */
321 ber_sockbuf_add_io( sb, &ber_sockbuf_io_readahead,
322 LBER_SBIOD_LEVEL_PROVIDER, NULL );
324 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
328 if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
329 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
331 rc = ldap_pvt_tls_start( ld, sb, ld->ld_options.ldo_tls_ctx );
332 if (rc != LDAP_SUCCESS)
337 if ( krbinstancep != NULL ) {
338 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
340 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
341 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
344 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
345 *krbinstancep = NULL;
346 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */