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>
28 * ldap_open - initialize and connect to an ldap server. A magic cookie to
29 * be used for future communication is returned on success, NULL on failure.
30 * "host" may be a space-separated list of hosts or IP addresses
34 * ld = ldap_open( hostname, port );
38 ldap_open( char *host, int port )
41 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
43 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
45 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
47 if (( ld = ldap_init( host, port )) == NULL ) {
51 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
52 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
53 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
54 strdup( ld->ld_defhost )) == NULL )) {
55 if(srv != NULL) free( (char*) srv );
56 ldap_ld_free( ld, 0 );
59 srv->lsrv_port = ld->ld_defport;
61 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
62 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
64 ldap_ld_free( ld, 0 );
67 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
69 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
70 if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
71 ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
72 ldap_ld_free( ld, 0 );
75 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
77 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
78 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
85 * ldap_init - initialize the LDAP library. A magic cookie to be used for
86 * future communication is returned on success, NULL on failure.
87 * "host" may be a space-separated list of hosts or IP addresses
91 * ld = ldap_open( host, port );
94 ldap_init( char *defhost, int defport )
98 if(!openldap_ldap_initialized) {
99 openldap_ldap_initialize();
102 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
105 { WORD wVersionRequested;
109 wVersionRequested = MAKEWORD( 2, 0 );
111 err = WSAStartup( wVersionRequested, &wsaData );
113 /* Tell the user that we couldn't find a usable */
118 /* Confirm that the WinSock DLL supports 2.0.*/
119 /* Note that if the DLL supports versions greater */
120 /* than 2.0 in addition to 2.0, it will still return */
121 /* 2.0 in wVersion since that is the version we */
124 if ( LOBYTE( wsaData.wVersion ) != 2 ||
125 HIBYTE( wsaData.wVersion ) != 0 )
127 /* Tell the user that we couldn't find a usable */
132 } /* The WinSock DLL is acceptable. Proceed. */
136 if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
142 if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
147 /* copy the global options */
148 memcpy(&ld->ld_options, &openldap_ldap_global_options,
149 sizeof(ld->ld_options));
151 /* but not pointers to malloc'ed strings */
152 ld->ld_options.ldo_defbase = NULL;
153 ld->ld_options.ldo_defhost = NULL;
155 if ( defhost != NULL ) {
156 ld->ld_options.ldo_defhost = strdup( defhost );
158 ld->ld_options.ldo_defhost = strdup(
159 openldap_ldap_global_options.ldo_defhost);
162 if ( ld->ld_options.ldo_defhost == NULL ) {
168 if ( openldap_ldap_global_options.ldo_defbase != NULL ) {
169 ld->ld_options.ldo_defbase = strdup(
170 openldap_ldap_global_options.ldo_defbase);
173 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
174 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
175 free( (char*) ld->ld_options.ldo_defhost );
176 if ( ld->ld_options.ldo_defbase == NULL ) {
177 free( (char*) ld->ld_options.ldo_defbase );
186 ld->ld_defport = defport;
189 ld->ld_lberoptions = LBER_USE_DER;
191 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
192 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
193 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
194 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
195 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
196 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
198 /* we'll assume we're talking version 2 for now */
199 ld->ld_version = LDAP_VERSION2;
201 ld->ld_sb.sb_sd = -1;
207 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
208 char **krbinstancep, int async )
213 char *curhost, hostname[ 2*MAXHOSTNAMELEN ];
215 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
217 defport = htons( (short) defport );
219 if ( host != NULL ) {
220 for ( p = host; p != NULL && *p != '\0'; p = q ) {
221 if (( q = strchr( p, ' ' )) != NULL ) {
222 strncpy( hostname, p, q - p );
223 hostname[ q - p ] = '\0';
225 while ( *q == ' ' ) {
229 curhost = p; /* avoid copy if possible */
233 if (( r = strchr( curhost, ':' )) != NULL ) {
234 if ( curhost != hostname ) {
235 strcpy( hostname, curhost ); /* now copy */
236 r = hostname + ( r - curhost );
240 port = htons( (short) atoi( r ) );
245 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
246 port, async )) != -1 ) {
251 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
259 if ( krbinstancep != NULL ) {
261 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
262 ( p = strchr( *krbinstancep, '.' )) != NULL ) {
265 #else /* HAVE_KERBEROS */
267 #endif /* HAVE_KERBEROS */