]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Update SASL code to reuse context through life of session.
[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         ld->ld_defconn = ldap_new_connection( ld,
30                 ld->ld_options.ldo_defludp, 1, 1, NULL );
31
32         if( ld->ld_defconn == NULL ) {
33                 ld->ld_errno = LDAP_SERVER_DOWN;
34                 return -1;
35         }
36
37         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
38         return 0;
39 }
40
41 /*
42  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
43  * be used for future communication is returned on success, NULL on failure.
44  * "host" may be a space-separated list of hosts or IP addresses
45  *
46  * Example:
47  *      LDAP    *ld;
48  *      ld = ldap_open( hostname, port );
49  */
50
51 LDAP *
52 ldap_open( LDAP_CONST char *host, int port )
53 {
54         int rc;
55         LDAP            *ld;
56
57         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
58
59         if (( ld = ldap_init( host, port )) == NULL ) {
60                 return( NULL );
61         }
62
63         rc = ldap_open_defconn( ld );
64
65         if( rc < 0 ) {
66                 ldap_ld_free( ld, 0, NULL, NULL );
67                 return( NULL );
68         }
69
70         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
71                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
72
73         return( ld );
74 }
75
76
77
78 int
79 ldap_create( LDAP **ldp )
80 {
81         LDAP                    *ld;
82         struct ldapoptions      *gopts;
83
84         *ldp = NULL;
85         /* Get pointer to global option structure */
86         if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
87                 return LDAP_NO_MEMORY;
88         }
89
90         /* Initialize the global options, if not already done. */
91         if( gopts->ldo_valid != LDAP_INITIALIZED ) {
92                 ldap_int_initialize(gopts, NULL);
93         }
94
95         Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
96
97 #ifdef HAVE_WINSOCK2
98 {       WORD wVersionRequested;
99         WSADATA wsaData;
100  
101         wVersionRequested = MAKEWORD( 2, 0 );
102         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
103                 /* Tell the user that we couldn't find a usable */
104                 /* WinSock DLL.                                  */
105                 return LDAP_LOCAL_ERROR;
106         }
107  
108         /* Confirm that the WinSock DLL supports 2.0.*/
109         /* Note that if the DLL supports versions greater    */
110         /* than 2.0 in addition to 2.0, it will still return */
111         /* 2.0 in wVersion since that is the version we      */
112         /* requested.                                        */
113  
114         if ( LOBYTE( wsaData.wVersion ) != 2 ||
115                 HIBYTE( wsaData.wVersion ) != 0 )
116         {
117             /* Tell the user that we couldn't find a usable */
118             /* WinSock DLL.                                  */
119             WSACleanup( );
120             return LDAP_LOCAL_ERROR; 
121         }
122 }       /* The WinSock DLL is acceptable. Proceed. */
123
124 #elif HAVE_WINSOCK
125 {       WSADATA wsaData;
126         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
127             return LDAP_LOCAL_ERROR;
128         }
129 }
130 #endif
131
132         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
133             WSACleanup( );
134                 return( LDAP_NO_MEMORY );
135         }
136    
137         /* copy the global options */
138         memcpy(&ld->ld_options, gopts, sizeof(ld->ld_options));
139
140         ld->ld_valid = LDAP_VALID_SESSION;
141
142         /* but not pointers to malloc'ed items */
143         ld->ld_options.ldo_defludp = NULL;
144         ld->ld_options.ldo_sctrls = NULL;
145         ld->ld_options.ldo_cctrls = NULL;
146
147         ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
148
149         if ( ld->ld_options.ldo_defludp == NULL ) {
150                 LDAP_FREE( (char*)ld );
151             WSACleanup( );
152                 return LDAP_NO_MEMORY;
153         }
154
155         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
156                 ldap_free_urllist( ld->ld_options.ldo_defludp );
157                 LDAP_FREE( (char*) ld );
158             WSACleanup( );
159                 return LDAP_NO_MEMORY;
160         }
161
162         ld->ld_lberoptions = LBER_USE_DER;
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_init( 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_s ( 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
254                 /* XXYYZ: this initiates operaton only on default connection! */
255                 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
256                         NULL, serverctrls, clientctrls, &rspoid, &rspdata);
257
258                 if (rc != LDAP_SUCCESS)
259                         return rc;
260                 if (rspoid != NULL)
261                         LDAP_FREE(rspoid);
262                 if (rspdata != NULL)
263                         ber_bvfree(rspdata);
264                 rc = ldap_pvt_tls_start( ld, lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
265                 if (rc != LDAP_SUCCESS)
266                         return rc;
267         }
268         return LDAP_SUCCESS;
269 #else
270         return LDAP_NOT_SUPPORTED;
271 #endif
272 }
273
274 int
275 ldap_int_open_connection(
276         LDAP *ld,
277         LDAPConn *conn,
278         LDAPURLDesc *srv,
279         int async )
280 {
281         int rc = -1;
282 #ifdef HAVE_CYRUS_SASL
283         char *sasl_host = NULL;
284         int sasl_ssf = 0;
285 #endif
286         int port;
287         long addr;
288
289         Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
290
291         port = srv->lud_port;
292         if (port == 0)
293                 port = ld->ld_options.ldo_defport;
294         port = htons( (short) port );
295
296         addr = 0;
297         if ( srv->lud_host == NULL || *srv->lud_host == 0 )
298                 addr = htonl( INADDR_LOOPBACK );
299
300         switch ( ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
301                 case LDAP_PROTO_TCP:
302                         rc = ldap_connect_to_host( ld, conn->lconn_sb, 0,
303                                 srv->lud_host, addr, port, async );
304                         if ( rc == -1 ) return rc;
305                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
306                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
307
308 #ifdef HAVE_CYRUS_SASL
309                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
310 #endif
311                         break;
312                 case LDAP_PROTO_UDP:
313                         rc = ldap_connect_to_host( ld, conn->lconn_sb, 1,
314                                 srv->lud_host, addr, port, async );
315                         if ( rc == -1 ) return rc;
316                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
317                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
318                         break;
319                 case LDAP_PROTO_IPC:
320 #ifdef LDAP_PF_LOCAL
321                         /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
322                         rc = ldap_connect_to_path( ld, conn->lconn_sb, 0,
323                                 srv->lud_host, async );
324                         if ( rc == -1 ) return rc;
325                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
326                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
327
328 #ifdef HAVE_CYRUS_SASL
329                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
330                         sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
331 #endif
332                         break;
333 #endif /* LDAP_PF_LOCAL */
334                 default:
335                         return -1;
336                         break;
337         }
338
339 #ifdef HAVE_CYRUS_SASL
340         if( sasl_host != NULL ) {
341                 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
342         }
343 #endif
344
345         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
346                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
347 #ifdef LDAP_DEBUG
348         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
349                 INT_MAX, NULL );
350 #endif
351
352 #ifdef HAVE_TLS
353         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
354                 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
355         {
356                 rc = ldap_pvt_tls_start( ld, conn->lconn_sb,
357                         ld->ld_options.ldo_tls_ctx );
358                 if (rc != LDAP_SUCCESS)
359                         return rc;
360         }
361 #endif
362
363         if ( conn->lconn_krbinstance != NULL ) {
364 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
365                 char *c;
366                 conn->lconn_krbinstance = ldap_host_connected_to( conn->sb );
367
368                 if( conn->lconn_krbinstance != NULL && 
369                     ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
370                         *c = '\0';
371                 }
372 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
373                 conn->lconn_krbinstance = NULL;
374 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
375         }
376
377         return( 0 );
378 }