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.
17 #include <ac/socket.h>
18 #include <ac/string.h>
21 #ifdef HAVE_SYS_PARAM_H
22 #include <sys/param.h>
29 * ldap_open - initialize and connect to an ldap server. A magic cookie to
30 * be used for future communication is returned on success, NULL on failure.
31 * "host" may be a space-separated list of hosts or IP addresses
35 * ld = ldap_open( hostname, port );
39 ldap_open( LDAP_CONST char *host, int port )
44 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
46 if (( ld = ldap_init( host, port )) == NULL ) {
50 if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) ==
51 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
52 strdup( ld->ld_defhost )) == NULL )) {
53 if(srv != NULL) LDAP_FREE( (char*) srv );
54 ldap_ld_free( ld, 0, NULL, NULL );
57 srv->lsrv_port = ld->ld_defport;
59 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
60 if ( ld->ld_defhost != NULL ) LDAP_FREE( srv->lsrv_host );
61 LDAP_FREE( (char *)srv );
62 ldap_ld_free( ld, 0, NULL, NULL );
65 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
67 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
68 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
75 * ldap_init - initialize the LDAP library. A magic cookie to be used for
76 * future communication is returned on success, NULL on failure.
77 * "host" may be a space-separated list of hosts or IP addresses
81 * ld = ldap_open( host, port );
84 ldap_init( LDAP_CONST char *defhost, int defport )
88 if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
89 ldap_int_initialize();
92 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
95 { WORD wVersionRequested;
99 wVersionRequested = MAKEWORD( 2, 0 );
101 err = WSAStartup( wVersionRequested, &wsaData );
103 /* Tell the user that we couldn't find a usable */
108 /* Confirm that the WinSock DLL supports 2.0.*/
109 /* Note that if the DLL supports versions greater */
110 /* than 2.0 in addition to 2.0, it will still return */
111 /* 2.0 in wVersion since that is the version we */
114 if ( LOBYTE( wsaData.wVersion ) != 2 ||
115 HIBYTE( wsaData.wVersion ) != 0 )
117 /* Tell the user that we couldn't find a usable */
122 } /* The WinSock DLL is acceptable. Proceed. */
126 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
132 if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
137 /* copy the global options */
138 memcpy(&ld->ld_options, &ldap_int_global_options,
139 sizeof(ld->ld_options));
141 ld->ld_valid = LDAP_VALID_SESSION;
143 /* but not pointers to malloc'ed items */
144 ld->ld_options.ldo_defbase = NULL;
145 ld->ld_options.ldo_defhost = NULL;
146 ld->ld_options.ldo_sctrls = NULL;
147 ld->ld_options.ldo_cctrls = NULL;
149 if ( defhost != NULL ) {
150 ld->ld_options.ldo_defhost = strdup( defhost );
152 ld->ld_options.ldo_defhost = strdup(
153 ldap_int_global_options.ldo_defhost);
156 if ( ld->ld_options.ldo_defhost == NULL ) {
157 LDAP_FREE( (char*)ld );
162 if ( ldap_int_global_options.ldo_defbase != NULL ) {
163 ld->ld_options.ldo_defbase = strdup(
164 ldap_int_global_options.ldo_defbase);
167 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
168 LDAP_FREE( (char*) ld->ld_options.ldo_defhost );
169 if ( ld->ld_options.ldo_defbase == NULL ) {
170 LDAP_FREE( (char*) ld->ld_options.ldo_defbase );
172 LDAP_FREE( (char*) ld );
178 ld->ld_defport = defport;
181 ld->ld_lberoptions = LBER_USE_DER;
183 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
184 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
185 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
186 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
187 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
188 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
190 /* we'll assume we're talking version 2 for now */
191 ld->ld_version = LDAP_VERSION2;
193 ber_pvt_sb_init( &(ld->ld_sb) );
200 open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport,
201 char **krbinstancep, int async )
206 char *r, *curhost, hostname[ 2*MAXHOSTNAMELEN ];
208 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
210 defport = htons( (short) defport );
212 if ( host != NULL ) {
213 for ( p = host; p != NULL && *p != '\0'; p = q ) {
214 if (( q = strchr( p, ' ' )) != NULL ) {
215 strncpy( hostname, p, q - p );
216 hostname[ q - p ] = '\0';
218 while ( *q == ' ' ) {
222 curhost = (char *) p; /* avoid copy if possible */
226 if (( r = strchr( curhost, ':' )) != NULL ) {
227 if ( curhost != hostname ) {
228 strcpy( hostname, curhost ); /* now copy */
229 r = hostname + ( r - curhost );
233 port = htons( (short) atoi( r ) );
238 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
239 port, async )) != -1 ) {
244 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
252 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
254 if ( krbinstancep != NULL ) {
257 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
258 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
261 #else /* HAVE_KERBEROS */
263 #endif /* HAVE_KERBEROS */