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 );
77 ldap_create( LDAP **ldp )
82 if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
83 ldap_int_initialize();
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 /* we'll assume we're talking version 2 for now */
165 ld->ld_version = LDAP_VERSION2;
167 ber_pvt_sb_init( &(ld->ld_sb) );
174 * ldap_init - initialize the LDAP library. A magic cookie to be used for
175 * future communication is returned on success, NULL on failure.
176 * "host" may be a space-separated list of hosts or IP addresses
180 * ld = ldap_open( host, port );
183 ldap_init( LDAP_CONST char *defhost, int defport )
188 rc = ldap_create(&ld);
189 if ( rc != LDAP_SUCCESS )
193 ld->ld_options.ldo_defport = defport;
195 if (defhost != NULL) {
196 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
197 if ( rc != LDAP_SUCCESS ) {
198 ldap_ld_free(ld, 1, NULL, NULL);
208 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
214 rc = ldap_create(&ld);
215 if ( rc != LDAP_SUCCESS )
219 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
220 if ( rc != LDAP_SUCCESS ) {
221 ldap_ld_free(ld, 1, NULL, NULL);
231 ldap_start_tls ( LDAP *ld,
232 LDAPControl **serverctrls,
233 LDAPControl **clientctrls )
239 struct berval *rspdata;
241 if (ld->ld_conns == NULL) {
242 rc = ldap_open_defconn( ld );
243 if (rc != LDAP_SUCCESS)
247 for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
248 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
249 return LDAP_OPERATIONS_ERROR;
250 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
251 NULL, serverctrls, clientctrls, &rspoid, &rspdata);
252 if (rc != LDAP_SUCCESS)
254 rc = ldap_pvt_tls_start( lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
255 if (rc != LDAP_SUCCESS)
260 return LDAP_NOT_SUPPORTED;
265 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
266 char **krbinstancep, int async )
272 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
274 port = srv->lud_port;
276 port = ld->ld_options.ldo_defport;
277 port = htons( (short) port );
280 if ( srv->lud_host == NULL )
281 addr = htonl( INADDR_LOOPBACK );
283 rc = ldap_connect_to_host( ld, sb, srv->lud_host, addr, port, async );
288 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
291 tls = srv->lud_ldaps;
293 tls = ld->ld_options.ldo_tls_mode;
295 rc = ldap_pvt_tls_start( sb, ld->ld_options.ldo_tls_ctx );
296 if (rc != LDAP_SUCCESS)
300 if ( krbinstancep != NULL ) {
303 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
304 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
307 #else /* HAVE_KERBEROS */
308 *krbinstancep = NULL;
309 #endif /* HAVE_KERBEROS */