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 ldap_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 = ldap_strdup( defhost );
163 ld->ld_options.ldo_defhost = ldap_strdup(
164 openldap_ldap_global_options.ldo_defhost);
167 if ( ld->ld_options.ldo_defhost == NULL ) {
173 if ( openldap_ldap_global_options.ldo_defbase != NULL ) {
174 ld->ld_options.ldo_defbase = ldap_strdup(
175 openldap_ldap_global_options.ldo_defbase);
178 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
179 if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
180 free( (char*) ld->ld_options.ldo_defhost );
181 if ( ld->ld_options.ldo_defbase == NULL ) {
182 free( (char*) ld->ld_options.ldo_defbase );
191 ld->ld_defport = defport;
194 ld->ld_lberoptions = LBER_USE_DER;
196 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
197 ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
198 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
199 ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
200 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
201 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
208 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
209 char **krbinstancep, int async )
214 char *curhost, hostname[ 2*MAXHOSTNAMELEN ];
216 Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
218 defport = htons( (short) defport );
220 if ( host != NULL ) {
221 for ( p = host; p != NULL && *p != '\0'; p = q ) {
222 if (( q = strchr( p, ' ' )) != NULL ) {
223 strncpy( hostname, p, q - p );
224 hostname[ q - p ] = '\0';
226 while ( *q == ' ' ) {
230 curhost = p; /* avoid copy if possible */
234 if (( r = strchr( curhost, ':' )) != NULL ) {
235 if ( curhost != hostname ) {
236 strcpy( hostname, curhost ); /* now copy */
237 r = hostname + ( r - curhost );
241 port = htons( (short) atoi( r ) );
246 if (( rc = ldap_connect_to_host( sb, curhost, 0L,
247 port, async )) != -1 ) {
252 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
260 if ( krbinstancep != NULL ) {
262 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
263 ( p = strchr( *krbinstancep, '.' )) != NULL ) {
266 #else /* HAVE_KERBEROS */
268 #endif /* HAVE_KERBEROS */