2 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 * Copyright (c) 1995 Regents of the University of Michigan.
16 #include <ac/stdlib.h>
18 #include <ac/socket.h>
19 #include <ac/string.h>
22 #ifdef HAVE_SYS_PARAM_H
23 #include <sys/param.h>
30 * ldap_open - initialize and connect to an ldap server. A magic cookie to
31 * be used for future communication is returned on success, NULL on failure.
32 * "host" may be a space-separated list of hosts or IP addresses
36 * ld = ldap_open( hostname, port );
40 ldap_open( LDAP_CONST char *host, int port )
45 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
47 if (( ld = ldap_init( host, port )) == NULL ) {
51 if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) ==
52 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
53 LDAP_STRDUP( ld->ld_defhost )) == NULL )) {
54 if(srv != NULL) LDAP_FREE( (char*) srv );
55 ldap_ld_free( ld, 0, NULL, NULL );
58 srv->lsrv_port = ld->ld_defport;
60 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
61 if ( ld->ld_defhost != NULL ) LDAP_FREE( srv->lsrv_host );
62 LDAP_FREE( (char *)srv );
63 ldap_ld_free( ld, 0, NULL, NULL );
66 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
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 )
89 if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
90 ldap_int_initialize();
93 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
96 { WORD wVersionRequested;
100 wVersionRequested = MAKEWORD( 2, 0 );
102 err = WSAStartup( wVersionRequested, &wsaData );
104 /* Tell the user that we couldn't find a usable */
109 /* Confirm that the WinSock DLL supports 2.0.*/
110 /* Note that if the DLL supports versions greater */
111 /* than 2.0 in addition to 2.0, it will still return */
112 /* 2.0 in wVersion since that is the version we */
115 if ( LOBYTE( wsaData.wVersion ) != 2 ||
116 HIBYTE( wsaData.wVersion ) != 0 )
118 /* Tell the user that we couldn't find a usable */
123 } /* The WinSock DLL is acceptable. Proceed. */
127 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
133 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
138 /* copy the global options */
139 memcpy(&ld->ld_options, &ldap_int_global_options,
140 sizeof(ld->ld_options));
142 ld->ld_valid = LDAP_VALID_SESSION;
144 /* but not pointers to malloc'ed items */
145 ld->ld_options.ldo_defbase = NULL;
146 ld->ld_options.ldo_defhost = NULL;
147 ld->ld_options.ldo_sctrls = NULL;
148 ld->ld_options.ldo_cctrls = NULL;
150 if ( defhost != NULL ) {
151 ld->ld_options.ldo_defhost = LDAP_STRDUP( defhost );
153 ld->ld_options.ldo_defhost = LDAP_STRDUP(
154 ldap_int_global_options.ldo_defhost);
157 if ( ld->ld_options.ldo_defhost == NULL ) {
158 LDAP_FREE( (char*)ld );
163 if ( ldap_int_global_options.ldo_defbase != NULL ) {
164 ld->ld_options.ldo_defbase = LDAP_STRDUP(
165 ldap_int_global_options.ldo_defbase);
168 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
169 LDAP_FREE( (char*) ld->ld_options.ldo_defhost );
170 if ( ld->ld_options.ldo_defbase == NULL ) {
171 LDAP_FREE( (char*) ld->ld_options.ldo_defbase );
173 LDAP_FREE( (char*) ld );
179 ld->ld_defport = defport;
182 ld->ld_lberoptions = LBER_USE_DER;
184 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
185 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
186 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
187 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
188 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
189 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
191 /* we'll assume we're talking version 2 for now */
192 ld->ld_version = LDAP_VERSION2;
194 ber_pvt_sb_init( &(ld->ld_sb) );
201 open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport,
202 char **krbinstancep, int async )
207 char *r, *curhost, hostname[ 2*MAXHOSTNAMELEN ];
209 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
211 defport = htons( (short) defport );
213 if ( host != NULL ) {
214 for ( p = host; p != NULL && *p != '\0'; p = q ) {
215 if (( q = strchr( p, ' ' )) != NULL ) {
216 strncpy( hostname, p, q - p );
217 hostname[ q - p ] = '\0';
219 while ( *q == ' ' ) {
223 curhost = (char *) p; /* avoid copy if possible */
227 if (( r = strchr( curhost, ':' )) != NULL ) {
228 if ( curhost != hostname ) {
229 strcpy( hostname, curhost ); /* now copy */
230 r = hostname + ( r - curhost );
234 port = htons( (short) atoi( r ) );
239 if (( rc = ldap_connect_to_host( ld, sb, curhost, 0L,
240 port, async )) != -1 ) {
245 rc = ldap_connect_to_host( ld, sb, 0, htonl( INADDR_LOOPBACK ),
253 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
256 if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ) {
258 * Fortunately, the lib uses blocking io...
260 if ( ldap_pvt_tls_connect( sb, ld->ld_options.ldo_tls_ctx ) <
264 /* FIXME: hostname of server must be compared with name in
269 if ( krbinstancep != NULL ) {
272 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
273 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
276 #else /* HAVE_KERBEROS */
278 #endif /* HAVE_KERBEROS */