]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
57eec85a359d0544a0b689181ac21875f73ac9c8
[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         AC_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_sctrls = NULL;
144         ld->ld_options.ldo_cctrls = NULL;
145
146 #ifdef HAVE_CYRUS_SASL
147         ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
148                 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
149         ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
150                 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
151         ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
152                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
153         ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
154                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
155 #endif
156
157         ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
158
159         if ( ld->ld_options.ldo_defludp == NULL ) {
160                 LDAP_FREE( (char*)ld );
161             WSACleanup( );
162                 return LDAP_NO_MEMORY;
163         }
164
165         if (( ld->ld_selectinfo = ldap_new_select_info()) == 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         ld->ld_lberoptions = LBER_USE_DER;
173
174         ld->ld_sb = ber_sockbuf_alloc( );
175         if ( ld->ld_sb == NULL ) {
176                 ldap_free_urllist( ld->ld_options.ldo_defludp );
177                 LDAP_FREE( (char*) ld );
178                 WSACleanup( );
179                 return LDAP_NO_MEMORY;
180         }
181
182         *ldp = ld;
183         return LDAP_SUCCESS;
184 }
185
186 /*
187  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
188  * future communication is returned on success, NULL on failure.
189  * "host" may be a space-separated list of hosts or IP addresses
190  *
191  * Example:
192  *      LDAP    *ld;
193  *      ld = ldap_init( host, port );
194  */
195 LDAP *
196 ldap_init( LDAP_CONST char *defhost, int defport )
197 {
198         LDAP *ld;
199         int rc;
200
201         rc = ldap_create(&ld);
202         if ( rc != LDAP_SUCCESS )
203                 return NULL;
204
205         if (defport != 0)
206                 ld->ld_options.ldo_defport = defport;
207
208         if (defhost != NULL) {
209                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
210                 if ( rc != LDAP_SUCCESS ) {
211                         ldap_ld_free(ld, 1, NULL, NULL);
212                         return NULL;
213                 }
214         }
215
216         return( ld );
217 }
218
219
220 int
221 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
222 {
223         int rc;
224         LDAP *ld;
225
226         *ldp = NULL;
227         rc = ldap_create(&ld);
228         if ( rc != LDAP_SUCCESS )
229                 return rc;
230
231         if (url != NULL) {
232                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
233                 if ( rc != LDAP_SUCCESS ) {
234                         ldap_ld_free(ld, 1, NULL, NULL);
235                         return rc;
236                 }
237         }
238
239         *ldp = ld;
240         return LDAP_SUCCESS;
241 }
242
243 int
244 ldap_start_tls_s ( LDAP *ld,
245                                 LDAPControl **serverctrls,
246                                 LDAPControl **clientctrls )
247 {
248 #ifdef HAVE_TLS
249         LDAPConn *lc;
250         int rc;
251         char *rspoid = NULL;
252         struct berval *rspdata = NULL;
253
254         if (ld->ld_conns == NULL) {
255                 rc = ldap_open_defconn( ld );
256                 if (rc != LDAP_SUCCESS)
257                         return(rc);
258         }
259
260         for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
261                 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
262                         return LDAP_OPERATIONS_ERROR;
263
264                 /* XXYYZ: this initiates operaton only on default connection! */
265                 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
266                         NULL, serverctrls, clientctrls, &rspoid, &rspdata);
267
268                 if (rc != LDAP_SUCCESS)
269                         return rc;
270                 if (rspoid != NULL)
271                         LDAP_FREE(rspoid);
272                 if (rspdata != NULL)
273                         ber_bvfree(rspdata);
274                 rc = ldap_pvt_tls_start( ld, lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
275                 if (rc != LDAP_SUCCESS)
276                         return rc;
277         }
278         return LDAP_SUCCESS;
279 #else
280         return LDAP_NOT_SUPPORTED;
281 #endif
282 }
283
284 int
285 ldap_int_open_connection(
286         LDAP *ld,
287         LDAPConn *conn,
288         LDAPURLDesc *srv,
289         int async )
290 {
291         int rc = -1;
292 #ifdef HAVE_CYRUS_SASL
293         char *sasl_host = NULL;
294         int sasl_ssf = 0;
295 #endif
296         int port;
297         long addr;
298
299         Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
300
301         port = srv->lud_port;
302         if (port == 0)
303                 port = ld->ld_options.ldo_defport;
304         port = htons( (short) port );
305
306         addr = 0;
307         if ( srv->lud_host == NULL || *srv->lud_host == 0 )
308                 addr = htonl( INADDR_LOOPBACK );
309
310         switch ( ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
311                 case LDAP_PROTO_TCP:
312                         rc = ldap_connect_to_host( ld, conn->lconn_sb, 0,
313                                 srv->lud_host, addr, port, async );
314                         if ( rc == -1 ) return rc;
315                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
316                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
317
318 #ifdef HAVE_CYRUS_SASL
319                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
320 #endif
321                         break;
322                 case LDAP_PROTO_IPC:
323 #ifdef LDAP_PF_LOCAL
324                         /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
325                         rc = ldap_connect_to_path( ld, conn->lconn_sb, 0,
326                                 srv->lud_host, async );
327                         if ( rc == -1 ) return rc;
328                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
329                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
330
331 #ifdef HAVE_CYRUS_SASL
332                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
333                         sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
334 #endif
335                         break;
336 #endif /* LDAP_PF_LOCAL */
337                 default:
338                         return -1;
339                         break;
340         }
341
342 #ifdef HAVE_CYRUS_SASL
343         if( sasl_host != NULL ) {
344                 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
345         }
346 #endif
347
348         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
349                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
350 #ifdef LDAP_DEBUG
351         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
352                 INT_MAX, NULL );
353 #endif
354
355 #ifdef HAVE_TLS
356         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
357                 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
358         {
359                 rc = ldap_pvt_tls_start( ld, conn->lconn_sb,
360                         ld->ld_options.ldo_tls_ctx );
361                 if (rc != LDAP_SUCCESS)
362                         return rc;
363         }
364 #endif
365
366         if ( conn->lconn_krbinstance != NULL ) {
367 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
368                 char *c;
369                 conn->lconn_krbinstance = ldap_host_connected_to( conn->sb );
370
371                 if( conn->lconn_krbinstance != NULL && 
372                     ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
373                         *c = '\0';
374                 }
375 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
376                 conn->lconn_krbinstance = NULL;
377 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
378         }
379
380         return( 0 );
381 }