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 *)calloc( 1, sizeof( LDAPServer ))) ==
51 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
52 strdup( ld->ld_defhost )) == NULL )) {
53 if(srv != NULL) 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 ) free( srv->lsrv_host );
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(!openldap_ldap_initialized) {
89 openldap_ldap_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 *) calloc( 1, sizeof(LDAP) )) == NULL ) {
137 /* copy the global options */
138 memcpy(&ld->ld_options, &openldap_ldap_global_options,
139 sizeof(ld->ld_options));
141 /* but not pointers to malloc'ed items */
142 ld->ld_options.ldo_defbase = NULL;
143 ld->ld_options.ldo_defhost = NULL;
144 ld->ld_options.ldo_sctrls = NULL;
145 ld->ld_options.ldo_cctrls = NULL;
147 if ( defhost != NULL ) {
148 ld->ld_options.ldo_defhost = strdup( defhost );
150 ld->ld_options.ldo_defhost = strdup(
151 openldap_ldap_global_options.ldo_defhost);
154 if ( ld->ld_options.ldo_defhost == NULL ) {
160 if ( openldap_ldap_global_options.ldo_defbase != NULL ) {
161 ld->ld_options.ldo_defbase = strdup(
162 openldap_ldap_global_options.ldo_defbase);
165 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
166 free( (char*) ld->ld_options.ldo_defhost );
167 if ( ld->ld_options.ldo_defbase == NULL ) {
168 free( (char*) ld->ld_options.ldo_defbase );
176 ld->ld_defport = defport;
179 ld->ld_lberoptions = LBER_USE_DER;
181 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
182 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
183 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
184 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
185 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
186 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
188 /* we'll assume we're talking version 2 for now */
189 ld->ld_version = LDAP_VERSION2;
191 ber_pvt_sb_init( &(ld->ld_sb) );
198 open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport,
199 char **krbinstancep, int async )
204 char *r, *curhost, hostname[ 2*MAXHOSTNAMELEN ];
206 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
208 defport = htons( (short) defport );
210 if ( host != NULL ) {
211 for ( p = host; p != NULL && *p != '\0'; p = q ) {
212 if (( q = strchr( p, ' ' )) != NULL ) {
213 strncpy( hostname, p, q - p );
214 hostname[ q - p ] = '\0';
216 while ( *q == ' ' ) {
220 curhost = (char *) p; /* avoid copy if possible */
224 if (( r = strchr( curhost, ':' )) != NULL ) {
225 if ( curhost != hostname ) {
226 strcpy( hostname, curhost ); /* now copy */
227 r = hostname + ( r - curhost );
231 port = htons( (short) atoi( r ) );
236 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
237 port, async )) != -1 ) {
242 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
250 ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
252 if ( krbinstancep != NULL ) {
255 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
256 ( c = strchr( *krbinstancep, '.' )) != NULL ) {
259 #else /* HAVE_KERBEROS */
261 #endif /* HAVE_KERBEROS */