]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
5ef67831e57b5de371ddd88ec811cf3741685987
[openldap] / libraries / libldap / open.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1995 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  open.c
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <limits.h>
17
18 #include <ac/stdlib.h>
19
20 #include <ac/param.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/time.h>
24
25 #include "ldap-int.h"
26
27 int ldap_open_defconn( LDAP *ld )
28 {
29         if (( ld->ld_defconn = ldap_new_connection( ld, ld->ld_options.ldo_defludp, 1,1,NULL )) == NULL )
30         {
31                 ld->ld_errno = LDAP_SERVER_DOWN;
32                 return -1;
33         }
34
35         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
36
37         return 0;
38 }
39
40 /*
41  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
42  * be used for future communication is returned on success, NULL on failure.
43  * "host" may be a space-separated list of hosts or IP addresses
44  *
45  * Example:
46  *      LDAP    *ld;
47  *      ld = ldap_open( hostname, port );
48  */
49
50 LDAP *
51 ldap_open( LDAP_CONST char *host, int port )
52 {
53         int rc;
54         LDAP            *ld;
55
56         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
57
58         if (( ld = ldap_init( host, port )) == NULL ) {
59                 return( NULL );
60         }
61
62         rc = ldap_open_defconn( ld );
63
64         if( rc < 0 ) {
65                 ldap_ld_free( ld, 0, NULL, NULL );
66                 return( NULL );
67         }
68
69         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
70                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
71
72         return( ld );
73 }
74
75
76
77 int
78 ldap_create( LDAP **ldp )
79 {
80         LDAP                    *ld;
81
82         *ldp = NULL;
83         if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
84                 ldap_int_initialize(NULL);
85         }
86
87         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
88
89 #ifdef HAVE_WINSOCK2
90 {       WORD wVersionRequested;
91         WSADATA wsaData;
92  
93         wVersionRequested = MAKEWORD( 2, 0 );
94         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
95                 /* Tell the user that we couldn't find a usable */
96                 /* WinSock DLL.                                  */
97                 return LDAP_LOCAL_ERROR;
98         }
99  
100         /* Confirm that the WinSock DLL supports 2.0.*/
101         /* Note that if the DLL supports versions greater    */
102         /* than 2.0 in addition to 2.0, it will still return */
103         /* 2.0 in wVersion since that is the version we      */
104         /* requested.                                        */
105  
106         if ( LOBYTE( wsaData.wVersion ) != 2 ||
107                 HIBYTE( wsaData.wVersion ) != 0 )
108         {
109             /* Tell the user that we couldn't find a usable */
110             /* WinSock DLL.                                  */
111             WSACleanup( );
112             return LDAP_LOCAL_ERROR; 
113         }
114 }       /* The WinSock DLL is acceptable. Proceed. */
115
116 #elif HAVE_WINSOCK
117 {       WSADATA wsaData;
118         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
119             return LDAP_LOCAL_ERROR;
120         }
121 }
122 #endif
123
124         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
125             WSACleanup( );
126                 return( LDAP_NO_MEMORY );
127         }
128    
129         /* copy the global options */
130         memcpy(&ld->ld_options, &ldap_int_global_options,
131                 sizeof(ld->ld_options));
132
133         ld->ld_valid = LDAP_VALID_SESSION;
134
135         /* but not pointers to malloc'ed items */
136         ld->ld_options.ldo_defludp = NULL;
137         ld->ld_options.ldo_sctrls = NULL;
138         ld->ld_options.ldo_cctrls = NULL;
139
140         ld->ld_options.ldo_defludp =
141                         ldap_url_duplist(ldap_int_global_options.ldo_defludp);
142
143         if ( ld->ld_options.ldo_defludp == NULL ) {
144                 LDAP_FREE( (char*)ld );
145             WSACleanup( );
146                 return LDAP_NO_MEMORY;
147         }
148
149         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
150                 ldap_free_urllist( ld->ld_options.ldo_defludp );
151                 LDAP_FREE( (char*) ld );
152             WSACleanup( );
153                 return LDAP_NO_MEMORY;
154         }
155
156         ld->ld_lberoptions = LBER_USE_DER;
157
158 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
159         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
160 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
161         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
162 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
163 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
164
165         ld->ld_sb = ber_sockbuf_alloc( );
166         if ( ld->ld_sb == NULL ) {
167                 ldap_free_urllist( ld->ld_options.ldo_defludp );
168                 LDAP_FREE( (char*) ld );
169                 WSACleanup( );
170                 return LDAP_NO_MEMORY;
171         }
172
173         *ldp = ld;
174         return LDAP_SUCCESS;
175 }
176
177 /*
178  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
179  * future communication is returned on success, NULL on failure.
180  * "host" may be a space-separated list of hosts or IP addresses
181  *
182  * Example:
183  *      LDAP    *ld;
184  *      ld = ldap_open( host, port );
185  */
186 LDAP *
187 ldap_init( LDAP_CONST char *defhost, int defport )
188 {
189         LDAP *ld;
190         int rc;
191
192         rc = ldap_create(&ld);
193         if ( rc != LDAP_SUCCESS )
194                 return NULL;
195
196         if (defport != 0)
197                 ld->ld_options.ldo_defport = defport;
198
199         if (defhost != NULL) {
200                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
201                 if ( rc != LDAP_SUCCESS ) {
202                         ldap_ld_free(ld, 1, NULL, NULL);
203                         return NULL;
204                 }
205         }
206
207         return( ld );
208 }
209
210
211 int
212 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
213 {
214         int rc;
215         LDAP *ld;
216
217         *ldp = NULL;
218         rc = ldap_create(&ld);
219         if ( rc != LDAP_SUCCESS )
220                 return rc;
221
222         if (url != NULL) {
223                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
224                 if ( rc != LDAP_SUCCESS ) {
225                         ldap_ld_free(ld, 1, NULL, NULL);
226                         return rc;
227                 }
228         }
229
230         *ldp = ld;
231         return LDAP_SUCCESS;
232 }
233
234 int
235 ldap_start_tls ( LDAP *ld,
236                                 LDAPControl **serverctrls,
237                                 LDAPControl **clientctrls )
238 {
239 #ifdef HAVE_TLS
240         LDAPConn *lc;
241         int rc;
242         char *rspoid = NULL;
243         struct berval *rspdata = NULL;
244
245         if (ld->ld_conns == NULL) {
246                 rc = ldap_open_defconn( ld );
247                 if (rc != LDAP_SUCCESS)
248                         return(rc);
249         }
250
251         for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
252                 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
253                         return LDAP_OPERATIONS_ERROR;
254                 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
255                                                         NULL, serverctrls, clientctrls, &rspoid, &rspdata);
256                 if (rc != LDAP_SUCCESS)
257                         return rc;
258                 if (rspoid != NULL)
259                         LDAP_FREE(rspoid);
260                 if (rspdata != NULL)
261                         ber_bvfree(rspdata);
262                 rc = ldap_pvt_tls_start( ld, lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
263                 if (rc != LDAP_SUCCESS)
264                         return rc;
265         }
266         return LDAP_SUCCESS;
267 #else
268         return LDAP_NOT_SUPPORTED;
269 #endif
270 }
271
272 int
273 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
274         char **krbinstancep, int async )
275 {
276         int rc = -1;
277         int port;
278         long addr;
279
280         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
281
282         port = srv->lud_port;
283         if (port == 0)
284                 port = ld->ld_options.ldo_defport;
285         port = htons( (short) port );
286
287         addr = 0;
288         if ( srv->lud_host == NULL || *srv->lud_host == 0 )
289                 addr = htonl( INADDR_LOOPBACK );
290
291         switch ( srv->lud_protocol ) {
292                 case LDAP_PROTO_TCP:
293                         rc = ldap_connect_to_host( ld, sb, srv->lud_host,
294                                 addr, port, async );
295                         if ( rc == -1 )
296                                 return rc;
297                         ber_sockbuf_add_io( sb, &ber_sockbuf_io_tcp,
298                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
299                         break;
300                 case LDAP_PROTO_UDP:
301                         rc = ldap_connect_to_host( ld, sb, srv->lud_host,
302                                 addr, port, async );
303                         if ( rc == -1 )
304                                 return rc;
305                         ber_sockbuf_add_io( sb, &ber_sockbuf_io_udp,
306                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
307                         break;
308 #ifdef LDAP_PF_LOCAL
309                 case LDAP_PROTO_LOCAL:
310                         rc = ldap_connect_to_path( ld, sb, srv->lud_host,
311                                 async );
312                         if ( rc == -1 )
313                                 return rc;
314                         ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd,
315                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
316                         break;
317 #endif /* LDAP_PF_LOCAL */
318                 default:
319                         return -1;
320                         break;
321         }
322
323         ber_sockbuf_add_io( sb, &ber_sockbuf_io_readahead,
324                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
325 #ifdef LDAP_DEBUG
326         ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
327 #endif
328
329 #ifdef HAVE_TLS
330         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
331             (srv->lud_properties & LDAP_URL_USE_SSL)) {
332                 rc = ldap_pvt_tls_start( ld, sb, ld->ld_options.ldo_tls_ctx );
333                 if (rc != LDAP_SUCCESS)
334                         return rc;
335         }
336 #endif
337
338         if ( krbinstancep != NULL ) {
339 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
340                 char *c;
341                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
342                     ( c = strchr( *krbinstancep, '.' )) != NULL ) {
343                         *c = '\0';
344                 }
345 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
346                 *krbinstancep = NULL;
347 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
348         }
349
350         return( 0 );
351 }