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 )
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, NULL, NULL );
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, NULL, NULL );
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, NULL, NULL );
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( LDAP_CONST 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 items */
153 ld->ld_options.ldo_defbase = NULL;
154 ld->ld_options.ldo_defhost = NULL;
155 ld->ld_options.ldo_sctrls = NULL;
156 ld->ld_options.ldo_cctrls = NULL;
158 if ( defhost != NULL ) {
159 ld->ld_options.ldo_defhost = strdup( defhost );
161 ld->ld_options.ldo_defhost = strdup(
162 openldap_ldap_global_options.ldo_defhost);
165 if ( ld->ld_options.ldo_defhost == NULL ) {
171 if ( openldap_ldap_global_options.ldo_defbase != NULL ) {
172 ld->ld_options.ldo_defbase = strdup(
173 openldap_ldap_global_options.ldo_defbase);
176 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
177 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
178 free( (char*) ld->ld_options.ldo_defhost );
179 if ( ld->ld_options.ldo_defbase == NULL ) {
180 free( (char*) ld->ld_options.ldo_defbase );
189 ld->ld_defport = defport;
192 ld->ld_lberoptions = LBER_USE_DER;
194 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
195 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
196 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
197 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
198 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
199 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
201 /* we'll assume we're talking version 2 for now */
202 ld->ld_version = LDAP_VERSION2;
204 ber_pvt_sb_init( &(ld->ld_sb) );
211 open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport,
212 char **krbinstancep, int async )
217 char *r, *curhost, hostname[ 2*MAXHOSTNAMELEN ];
219 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
221 defport = htons( (short) defport );
223 if ( host != NULL ) {
224 for ( p = host; p != NULL && *p != '\0'; p = q ) {
225 if (( q = strchr( p, ' ' )) != NULL ) {
226 strncpy( hostname, p, q - p );
227 hostname[ q - p ] = '\0';
229 while ( *q == ' ' ) {
233 curhost = (char *) p; /* avoid copy if possible */
237 if (( r = strchr( curhost, ':' )) != NULL ) {
238 if ( curhost != hostname ) {
239 strcpy( hostname, curhost ); /* now copy */
240 r = hostname + ( r - curhost );
244 port = htons( (short) atoi( r ) );
249 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
250 port, async )) != -1 ) {
255 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
263 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
265 if ( krbinstancep != NULL ) {
268 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
269 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
272 #else /* HAVE_KERBEROS */
274 #endif /* HAVE_KERBEROS */