2 * Copyright (c) 1995 Regents of the University of Michigan.
11 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
17 #include <ac/socket.h>
18 #include <ac/string.h>
21 #ifdef HAVE_SYS_PARAM_H
22 #include <sys/param.h>
33 * ldap_open - initialize and connect to an ldap server. A magic cookie to
34 * be used for future communication is returned on success, NULL on failure.
35 * "host" may be a space-separated list of hosts or IP addresses
39 * ld = ldap_open( hostname, port );
43 ldap_open( char *host, int port )
46 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
48 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
50 Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
52 if (( ld = ldap_init( host, port )) == NULL ) {
56 /* we'll assume we're talking version 2 for now */
57 ld->ld_version = LDAP_VERSION2;
59 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
60 if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
61 NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
62 strdup( ld->ld_defhost )) == NULL )) {
63 ldap_ld_free( ld, 0 );
66 srv->lsrv_port = ld->ld_defport;
68 if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
69 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
71 ldap_ld_free( ld, 0 );
74 ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
76 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
77 if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
78 ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
79 ldap_ld_free( ld, 0 );
82 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
84 Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
85 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
92 * ldap_init - initialize the LDAP library. A magic cookie to be used for
93 * future communication is returned on success, NULL on failure.
94 * "host" may be a space-separated list of hosts or IP addresses
98 * ld = ldap_open( host, port );
101 ldap_init( char *defhost, int defport )
105 if(!openldap_ldap_initialized) {
106 openldap_ldap_initialize();
109 Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
112 { WORD wVersionRequested;
116 wVersionRequested = MAKEWORD( 2, 0 );
118 err = WSAStartup( wVersionRequested, &wsaData );
120 /* Tell the user that we couldn't find a usable */
125 /* Confirm that the WinSock DLL supports 2.0.*/
126 /* Note that if the DLL supports versions greater */
127 /* than 2.0 in addition to 2.0, it will still return */
128 /* 2.0 in wVersion since that is the version we */
131 if ( LOBYTE( wsaData.wVersion ) != 2 ||
132 HIBYTE( wsaData.wVersion ) != 0 )
134 /* Tell the user that we couldn't find a usable */
139 } /* The WinSock DLL is acceptable. Proceed. */
142 if ( WSAStartup( 0x0101, &wsadata ) != 0 ) {
147 if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
152 /* copy the global options */
153 memcpy(&ld->ld_options, &openldap_ldap_global_options,
154 sizeof(ld->ld_options));
156 /* but not pointers to malloc'ed strings */
157 ld->ld_options.ldo_defbase = NULL;
158 ld->ld_options.ldo_defhost = NULL;
160 if ( defhost != NULL ) {
161 ld->ld_options.ldo_defhost = strdup( defhost );
163 ld->ld_options.ldo_defhost = strdup(
164 openldap_ldap_global_options.ldo_defhost);
167 if ( ld->ld_options.ldo_defhost == NULL ) {
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 free( (char*) ld->ld_options.ldo_defbase );
184 ld->ld_defport = defport;
187 ld->ld_lberoptions = LBER_USE_DER;
189 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
190 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
191 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
192 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
193 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
194 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
201 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
202 char **krbinstancep, int async )
207 char *curhost, hostname[ 2*MAXHOSTNAMELEN ];
209 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
211 defport = htons( (short) defport );
213 if ( host != NULL ) {
214 for ( p = host; p != NULL && *p != '\0'; p = q ) {
215 if (( q = strchr( p, ' ' )) != NULL ) {
216 strncpy( hostname, p, q - p );
217 hostname[ q - p ] = '\0';
219 while ( *q == ' ' ) {
223 curhost = p; /* avoid copy if possible */
227 if (( r = strchr( curhost, ':' )) != NULL ) {
228 if ( curhost != hostname ) {
229 strcpy( hostname, curhost ); /* now copy */
230 r = hostname + ( r - curhost );
234 port = htons( (short) atoi( r ) );
239 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
240 port, async )) != -1 ) {
245 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
253 if ( krbinstancep != NULL ) {
255 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
256 ( p = strchr( *krbinstancep, '.' )) != NULL ) {
259 #else /* HAVE_KERBEROS */
261 #endif /* HAVE_KERBEROS */