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( char *host, int port )
42 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
44 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
46 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
48 if (( ld = ldap_init( host, port )) == NULL ) {
52 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
53 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
54 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
55 strdup( ld->ld_defhost )) == NULL )) {
56 if(srv != NULL) free( (char*) srv );
57 ldap_ld_free( ld, 0 );
60 srv->lsrv_port = ld->ld_defport;
62 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
63 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
65 ldap_ld_free( ld, 0 );
68 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
70 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
71 if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
72 ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
73 ldap_ld_free( ld, 0 );
76 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
78 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
79 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
86 * ldap_init - initialize the LDAP library. A magic cookie to be used for
87 * future communication is returned on success, NULL on failure.
88 * "host" may be a space-separated list of hosts or IP addresses
92 * ld = ldap_open( host, port );
95 ldap_init( char *defhost, int defport )
99 if(!openldap_ldap_initialized) {
100 openldap_ldap_initialize();
103 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
106 { WORD wVersionRequested;
110 wVersionRequested = MAKEWORD( 2, 0 );
112 err = WSAStartup( wVersionRequested, &wsaData );
114 /* Tell the user that we couldn't find a usable */
119 /* Confirm that the WinSock DLL supports 2.0.*/
120 /* Note that if the DLL supports versions greater */
121 /* than 2.0 in addition to 2.0, it will still return */
122 /* 2.0 in wVersion since that is the version we */
125 if ( LOBYTE( wsaData.wVersion ) != 2 ||
126 HIBYTE( wsaData.wVersion ) != 0 )
128 /* Tell the user that we couldn't find a usable */
133 } /* The WinSock DLL is acceptable. Proceed. */
137 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
143 if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
148 /* copy the global options */
149 memcpy(&ld->ld_options, &openldap_ldap_global_options,
150 sizeof(ld->ld_options));
152 /* but not pointers to malloc'ed strings */
153 ld->ld_options.ldo_defbase = NULL;
154 ld->ld_options.ldo_defhost = NULL;
156 if ( defhost != NULL ) {
157 ld->ld_options.ldo_defhost = strdup( defhost );
159 ld->ld_options.ldo_defhost = strdup(
160 openldap_ldap_global_options.ldo_defhost);
163 if ( ld->ld_options.ldo_defhost == NULL ) {
169 if ( openldap_ldap_global_options.ldo_defbase != NULL ) {
170 ld->ld_options.ldo_defbase = strdup(
171 openldap_ldap_global_options.ldo_defbase);
174 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
175 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
176 free( (char*) ld->ld_options.ldo_defhost );
177 if ( ld->ld_options.ldo_defbase == NULL ) {
178 free( (char*) ld->ld_options.ldo_defbase );
187 ld->ld_defport = defport;
190 ld->ld_lberoptions = LBER_USE_DER;
192 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
193 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
194 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
195 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
196 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
197 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
199 /* we'll assume we're talking version 2 for now */
200 ld->ld_version = LDAP_VERSION2;
202 lber_pvt_sb_init( &(ld->ld_sb) );
209 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
210 char **krbinstancep, int async )
215 char *curhost, hostname[ 2*MAXHOSTNAMELEN ];
217 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
219 defport = htons( (short) defport );
221 if ( host != NULL ) {
222 for ( p = host; p != NULL && *p != '\0'; p = q ) {
223 if (( q = strchr( p, ' ' )) != NULL ) {
224 strncpy( hostname, p, q - p );
225 hostname[ q - p ] = '\0';
227 while ( *q == ' ' ) {
231 curhost = p; /* avoid copy if possible */
235 if (( r = strchr( curhost, ':' )) != NULL ) {
236 if ( curhost != hostname ) {
237 strcpy( hostname, curhost ); /* now copy */
238 r = hostname + ( r - curhost );
242 port = htons( (short) atoi( r ) );
247 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
248 port, async )) != -1 ) {
253 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
261 lber_pvt_sb_set_io( sb, &lber_pvt_sb_io_tcp, NULL );
263 if ( krbinstancep != NULL ) {
265 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
266 ( p = strchr( *krbinstancep, '.' )) != NULL ) {
269 #else /* HAVE_KERBEROS */
271 #endif /* HAVE_KERBEROS */