2 * Copyright (c) 1995 Regents of the University of Michigan.
9 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
20 #if defined( DOS ) || defined( _WIN32 )
24 #if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 )
26 #include <sys/types.h>
27 #include <sys/socket.h>
29 #include <sys/param.h>
31 #include <netinet/in.h>
41 #ifndef INADDR_LOOPBACK
42 #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
45 #ifndef MAXHOSTNAMELEN
46 #define MAXHOSTNAMELEN 64
51 * ldap_open - initialize and connect to an ldap server. A magic cookie to
52 * be used for future communication is returned on success, NULL on failure.
53 * "host" may be a space-separated list of hosts or IP addresses
57 * ld = ldap_open( hostname, port );
61 ldap_open( char *host, int port )
66 #endif /* LDAP_REFERRALS */
68 WORD wVersionRequested;
72 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
75 wVersionRequested = MAKEWORD( 2, 0 );
76 if ( WSAStartup( wVersionRequested, &wsadata ) != 0 ) {
81 if (( ld = ldap_init( host, port )) == NULL ) {
86 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
87 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
88 strdup( ld->ld_defhost )) == NULL )) {
89 ldap_ld_free( ld, 0 );
95 srv->lsrv_port = ld->ld_defport;
97 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
98 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
100 ldap_ld_free( ld, 0 );
101 #ifdef HAVE_WINSOCK_2
106 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
108 #else /* LDAP_REFERRALS */
109 if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
110 ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
111 ldap_ld_free( ld, 0 );
112 #ifdef HAVE_WINSOCK_2
117 #endif /* LDAP_REFERRALS */
119 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
120 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
127 * ldap_init - initialize the LDAP library. A magic cookie to be used for
128 * future communication is returned on success, NULL on failure.
129 * "defhost" may be a space-separated list of hosts or IP addresses
133 * ld = ldap_open( default_hostname, default_port );
136 ldap_init( char *defhost, int defport )
140 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
143 if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
147 #ifdef LDAP_REFERRALS
148 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
152 ld->ld_options = LDAP_OPT_REFERRALS;
153 #endif /* LDAP_REFERRALS */
155 if ( defhost != NULL &&
156 ( ld->ld_defhost = strdup( defhost )) == NULL ) {
157 #ifdef LDAP_REFERRALS
158 ldap_free_select_info( ld->ld_selectinfo );
159 #endif /* LDAP_REFERRALS */
165 ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
166 ld->ld_version = LDAP_VERSION;
167 ld->ld_lberoptions = LBER_USE_DER;
168 ld->ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
170 #ifdef LDAP_REFERRALS
171 ld->ld_options |= LDAP_OPT_REFERRALS;
172 #endif /* LDAP_REFERRALS */
174 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
175 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
176 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
177 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
178 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
179 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
186 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
187 char **krbinstancep, int async )
192 char *curhost, hostname[ 2*MAXHOSTNAMELEN ];
194 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
196 defport = htons( defport );
198 if ( host != NULL ) {
199 for ( p = host; p != NULL && *p != '\0'; p = q ) {
200 if (( q = strchr( p, ' ' )) != NULL ) {
201 strncpy( hostname, p, q - p );
202 hostname[ q - p ] = '\0';
204 while ( *q == ' ' ) {
208 curhost = p; /* avoid copy if possible */
212 if (( r = strchr( curhost, ':' )) != NULL ) {
213 if ( curhost != hostname ) {
214 strcpy( hostname, curhost ); /* now copy */
215 r = hostname + ( r - curhost );
219 port = htons( (short)atoi( r ));
224 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
225 port, async )) != -1 ) {
230 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
238 if ( krbinstancep != NULL ) {
240 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
241 ( p = strchr( *krbinstancep, '.' )) != NULL ) {
246 #endif /* KERBEROS */