]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Fix ldap_send_initial_request() to open connection if not already
[openldap] / libraries / libldap / open.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1995 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  open.c
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #include <ac/socket.h>
18 #include <ac/string.h>
19 #include <ac/time.h>
20
21 #ifdef HAVE_SYS_PARAM_H
22 #include <sys/param.h>
23 #endif
24
25 #include "ldap-int.h"
26
27 /*
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
31  *
32  * Example:
33  *      LDAP    *ld;
34  *      ld = ldap_open( hostname, port );
35  */
36
37 LDAP *
38 ldap_open( char *host, int port )
39 {
40         LDAP            *ld;
41 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
42         LDAPServer      *srv;
43 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
44
45         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
46
47         if (( ld = ldap_init( host, port )) == NULL ) {
48                 return( NULL );
49         }
50
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 );
57                 return( NULL );
58         }
59         srv->lsrv_port = ld->ld_defport;
60
61         if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
62                 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
63                 free( (char *)srv );
64                 ldap_ld_free( ld, 0 );
65                 return( NULL );
66         }
67         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
68
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 );
73                 return( NULL );
74         }
75 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
76
77         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
78                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
79
80         return( ld );
81 }
82
83
84 /*
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
88  *
89  * Example:
90  *      LDAP    *ld;
91  *      ld = ldap_open( host, port );
92  */
93 LDAP *
94 ldap_init( char *defhost, int defport )
95 {
96         LDAP                    *ld;
97
98         if(!openldap_ldap_initialized) {
99                 openldap_ldap_initialize();
100         }
101
102         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
103
104 #ifdef HAVE_WINSOCK2
105 {       WORD wVersionRequested;
106         WSADATA wsaData;
107         int err;
108  
109         wVersionRequested = MAKEWORD( 2, 0 );
110  
111         err = WSAStartup( wVersionRequested, &wsaData );
112         if ( err != 0 ) {
113                 /* Tell the user that we couldn't find a usable */
114                 /* WinSock DLL.                                  */
115                 return NULL;
116         }
117  
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      */
122         /* requested.                                        */
123  
124         if ( LOBYTE( wsaData.wVersion ) != 2 ||
125                 HIBYTE( wsaData.wVersion ) != 0 )
126         {
127             /* Tell the user that we couldn't find a usable */
128             /* WinSock DLL.                                  */
129             WSACleanup( );
130             return NULL; 
131         }
132 }       /* The WinSock DLL is acceptable. Proceed. */
133
134 #elif HAVE_WINSOCK
135         if ( WSAStartup( 0x0101, &wsadata ) != 0 ) {
136             return( NULL );
137         }
138 #endif
139
140         if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
141             WSACleanup( );
142                 return( NULL );
143         }
144
145         /* copy the global options */
146         memcpy(&ld->ld_options, &openldap_ldap_global_options,
147                 sizeof(ld->ld_options));
148
149         /* but not pointers to malloc'ed strings */
150         ld->ld_options.ldo_defbase = NULL;
151         ld->ld_options.ldo_defhost = NULL;
152
153         if ( defhost != NULL ) {
154                 ld->ld_options.ldo_defhost = strdup( defhost );
155         } else {
156                 ld->ld_options.ldo_defhost = strdup(
157                         openldap_ldap_global_options.ldo_defhost);
158         }
159
160         if ( ld->ld_options.ldo_defhost == NULL ) {
161                 free( (char*)ld );
162             WSACleanup( );
163                 return( NULL );
164         }
165
166         if ( openldap_ldap_global_options.ldo_defbase != NULL ) {
167                 ld->ld_options.ldo_defbase = strdup(
168                         openldap_ldap_global_options.ldo_defbase);
169         }
170
171 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
172         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
173                 free( (char*) ld->ld_options.ldo_defhost );
174                 if ( ld->ld_options.ldo_defbase == NULL ) {
175                         free( (char*) ld->ld_options.ldo_defbase );
176                 }
177                 free( (char*) ld );
178             WSACleanup( );
179                 return( NULL );
180         }
181 #endif
182
183         if(defport != 0) {
184                 ld->ld_defport = defport;
185         }
186
187         ld->ld_lberoptions = LBER_USE_DER;
188
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 */
195
196         /* we'll assume we're talking version 2 for now */
197         ld->ld_version = LDAP_VERSION2;
198
199         ld->ld_sb.sb_sd = -1;
200         return( ld );
201 }
202
203
204 int
205 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
206         char **krbinstancep, int async )
207 {
208         int                     rc = -1;
209         int                             port;
210         char                    *p, *q, *r;
211         char                    *curhost, hostname[ 2*MAXHOSTNAMELEN ];
212
213         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
214
215         defport = htons( (short) defport );
216
217         if ( host != NULL ) {
218                 for ( p = host; p != NULL && *p != '\0'; p = q ) {
219                         if (( q = strchr( p, ' ' )) != NULL ) {
220                                 strncpy( hostname, p, q - p );
221                                 hostname[ q - p ] = '\0';
222                                 curhost = hostname;
223                                 while ( *q == ' ' ) {
224                                     ++q;
225                                 }
226                         } else {
227                                 curhost = p;    /* avoid copy if possible */
228                                 q = NULL;
229                         }
230
231                         if (( r = strchr( curhost, ':' )) != NULL ) {
232                             if ( curhost != hostname ) {
233                                 strcpy( hostname, curhost );    /* now copy */
234                                 r = hostname + ( r - curhost );
235                                 curhost = hostname;
236                             }
237                             *r++ = '\0';
238                             port = htons( (short) atoi( r ) );
239                         } else {
240                             port = defport;   
241                         }
242
243                         if (( rc = ldap_connect_to_host( sb, curhost, 0L,
244                             port, async )) != -1 ) {
245                                 break;
246                         }
247                 }
248         } else {
249                 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
250                     defport, async );
251         }
252
253         if ( rc == -1 ) {
254                 return( rc );
255         }
256
257         if ( krbinstancep != NULL ) {
258 #ifdef HAVE_KERBEROS
259                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
260                     ( p = strchr( *krbinstancep, '.' )) != NULL ) {
261                         *p = '\0';
262                 }
263 #else /* HAVE_KERBEROS */
264                 krbinstancep = NULL;
265 #endif /* HAVE_KERBEROS */
266         }
267
268         return( 0 );
269 }