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