3 * Copyright 1998-1999 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 );
76 * ldap_init - initialize the LDAP library. A magic cookie to be used for
77 * future communication is returned on success, NULL on failure.
78 * "host" may be a space-separated list of hosts or IP addresses
82 * ld = ldap_open( host, port );
85 ldap_init( LDAP_CONST char *defhost, int defport )
90 rc = ldap_create(&ld);
91 if ( rc != LDAP_SUCCESS )
95 ld->ld_options.ldo_defport = defport;
97 if (defhost != NULL) {
98 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
99 if ( rc != LDAP_SUCCESS ) {
100 ldap_ld_free(ld, 1, NULL, NULL);
109 ldap_create( LDAP **ldp )
114 if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
115 ldap_int_initialize();
118 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
121 { WORD wVersionRequested;
124 wVersionRequested = MAKEWORD( 2, 0 );
125 if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
126 /* Tell the user that we couldn't find a usable */
128 return LDAP_LOCAL_ERROR;
131 /* Confirm that the WinSock DLL supports 2.0.*/
132 /* Note that if the DLL supports versions greater */
133 /* than 2.0 in addition to 2.0, it will still return */
134 /* 2.0 in wVersion since that is the version we */
137 if ( LOBYTE( wsaData.wVersion ) != 2 ||
138 HIBYTE( wsaData.wVersion ) != 0 )
140 /* Tell the user that we couldn't find a usable */
143 return LDAP_LOCAL_ERROR;
145 } /* The WinSock DLL is acceptable. Proceed. */
149 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
150 return LDAP_LOCAL_ERROR;
155 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
157 return( LDAP_NO_MEMORY );
160 /* copy the global options */
161 memcpy(&ld->ld_options, &ldap_int_global_options,
162 sizeof(ld->ld_options));
164 ld->ld_valid = LDAP_VALID_SESSION;
166 /* but not pointers to malloc'ed items */
167 ld->ld_options.ldo_defludp = NULL;
168 ld->ld_options.ldo_sctrls = NULL;
169 ld->ld_options.ldo_cctrls = NULL;
171 ld->ld_options.ldo_defludp =
172 ldap_url_duplist(ldap_int_global_options.ldo_defludp);
174 if ( ld->ld_options.ldo_defludp == NULL ) {
175 LDAP_FREE( (char*)ld );
177 return LDAP_NO_MEMORY;
180 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
181 ldap_free_urllist( ld->ld_options.ldo_defludp );
182 LDAP_FREE( (char*) ld );
184 return LDAP_NO_MEMORY;
187 ld->ld_lberoptions = LBER_USE_DER;
189 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
190 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
191 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
192 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
193 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
194 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
196 /* we'll assume we're talking version 2 for now */
197 ld->ld_version = LDAP_VERSION2;
199 ber_pvt_sb_init( &(ld->ld_sb) );
206 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
212 rc = ldap_create(&ld);
213 if ( rc != LDAP_SUCCESS )
217 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
218 if ( rc != LDAP_SUCCESS ) {
219 ldap_ld_free(ld, 1, NULL, NULL);
229 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
230 char **krbinstancep, int async )
236 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
238 port = srv->lud_port;
240 port = ld->ld_options.ldo_defport;
241 port = htons( (short) port );
244 if ( srv->lud_host == NULL )
245 addr = htonl( INADDR_LOOPBACK );
247 rc = ldap_connect_to_host( ld, sb, srv->lud_host, addr, port, async );
252 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
255 if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD
256 || srv->lud_ldaps != 0 )
259 * Fortunately, the lib uses blocking io...
261 if ( ldap_pvt_tls_connect( sb, ld->ld_options.ldo_tls_ctx ) <
265 /* FIXME: hostname of server must be compared with name in
270 if ( krbinstancep != NULL ) {
273 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
274 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
277 #else /* HAVE_KERBEROS */
279 #endif /* HAVE_KERBEROS */