]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
41d4f1ceda9a1a534eb442ca31cd6c102de6b161
[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         struct ldapoptions      *gopts;
82
83         *ldp = NULL;
84         /* Get pointer to global option structure */
85         if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
86                 return LDAP_NO_MEMORY;
87         }
88
89         /* Initialize the global options, if not already done. */
90         if( gopts->ldo_valid != LDAP_INITIALIZED ) {
91                 ldap_int_initialize(gopts, NULL);
92         }
93
94         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
95
96 #ifdef HAVE_WINSOCK2
97 {       WORD wVersionRequested;
98         WSADATA wsaData;
99  
100         wVersionRequested = MAKEWORD( 2, 0 );
101         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
102                 /* Tell the user that we couldn't find a usable */
103                 /* WinSock DLL.                                  */
104                 return LDAP_LOCAL_ERROR;
105         }
106  
107         /* Confirm that the WinSock DLL supports 2.0.*/
108         /* Note that if the DLL supports versions greater    */
109         /* than 2.0 in addition to 2.0, it will still return */
110         /* 2.0 in wVersion since that is the version we      */
111         /* requested.                                        */
112  
113         if ( LOBYTE( wsaData.wVersion ) != 2 ||
114                 HIBYTE( wsaData.wVersion ) != 0 )
115         {
116             /* Tell the user that we couldn't find a usable */
117             /* WinSock DLL.                                  */
118             WSACleanup( );
119             return LDAP_LOCAL_ERROR; 
120         }
121 }       /* The WinSock DLL is acceptable. Proceed. */
122
123 #elif HAVE_WINSOCK
124 {       WSADATA wsaData;
125         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
126             return LDAP_LOCAL_ERROR;
127         }
128 }
129 #endif
130
131         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
132             WSACleanup( );
133                 return( LDAP_NO_MEMORY );
134         }
135    
136         /* copy the global options */
137         memcpy(&ld->ld_options, gopts, sizeof(ld->ld_options));
138
139         ld->ld_valid = LDAP_VALID_SESSION;
140
141         /* but not pointers to malloc'ed items */
142         ld->ld_options.ldo_defludp = NULL;
143         ld->ld_options.ldo_sctrls = NULL;
144         ld->ld_options.ldo_cctrls = NULL;
145
146         ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
147
148         if ( ld->ld_options.ldo_defludp == NULL ) {
149                 LDAP_FREE( (char*)ld );
150             WSACleanup( );
151                 return LDAP_NO_MEMORY;
152         }
153
154         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
155                 ldap_free_urllist( ld->ld_options.ldo_defludp );
156                 LDAP_FREE( (char*) ld );
157             WSACleanup( );
158                 return LDAP_NO_MEMORY;
159         }
160
161         ld->ld_lberoptions = LBER_USE_DER;
162
163         ld->ld_sb = ber_sockbuf_alloc( );
164         if ( ld->ld_sb == NULL ) {
165                 ldap_free_urllist( ld->ld_options.ldo_defludp );
166                 LDAP_FREE( (char*) ld );
167                 WSACleanup( );
168                 return LDAP_NO_MEMORY;
169         }
170
171         *ldp = ld;
172         return LDAP_SUCCESS;
173 }
174
175 /*
176  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
177  * future communication is returned on success, NULL on failure.
178  * "host" may be a space-separated list of hosts or IP addresses
179  *
180  * Example:
181  *      LDAP    *ld;
182  *      ld = ldap_open( host, port );
183  */
184 LDAP *
185 ldap_init( LDAP_CONST char *defhost, int defport )
186 {
187         LDAP *ld;
188         int rc;
189
190         rc = ldap_create(&ld);
191         if ( rc != LDAP_SUCCESS )
192                 return NULL;
193
194         if (defport != 0)
195                 ld->ld_options.ldo_defport = defport;
196
197         if (defhost != NULL) {
198                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
199                 if ( rc != LDAP_SUCCESS ) {
200                         ldap_ld_free(ld, 1, NULL, NULL);
201                         return NULL;
202                 }
203         }
204
205         return( ld );
206 }
207
208
209 int
210 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
211 {
212         int rc;
213         LDAP *ld;
214
215         *ldp = NULL;
216         rc = ldap_create(&ld);
217         if ( rc != LDAP_SUCCESS )
218                 return rc;
219
220         if (url != NULL) {
221                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
222                 if ( rc != LDAP_SUCCESS ) {
223                         ldap_ld_free(ld, 1, NULL, NULL);
224                         return rc;
225                 }
226         }
227
228         *ldp = ld;
229         return LDAP_SUCCESS;
230 }
231
232 int
233 ldap_start_tls ( LDAP *ld,
234                                 LDAPControl **serverctrls,
235                                 LDAPControl **clientctrls )
236 {
237 #ifdef HAVE_TLS
238         LDAPConn *lc;
239         int rc;
240         char *rspoid = NULL;
241         struct berval *rspdata = NULL;
242
243         if (ld->ld_conns == NULL) {
244                 rc = ldap_open_defconn( ld );
245                 if (rc != LDAP_SUCCESS)
246                         return(rc);
247         }
248
249         for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
250                 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
251                         return LDAP_OPERATIONS_ERROR;
252                 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
253                                                         NULL, serverctrls, clientctrls, &rspoid, &rspdata);
254                 if (rc != LDAP_SUCCESS)
255                         return rc;
256                 if (rspoid != NULL)
257                         LDAP_FREE(rspoid);
258                 if (rspdata != NULL)
259                         ber_bvfree(rspdata);
260                 rc = ldap_pvt_tls_start( ld, lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
261                 if (rc != LDAP_SUCCESS)
262                         return rc;
263         }
264         return LDAP_SUCCESS;
265 #else
266         return LDAP_NOT_SUPPORTED;
267 #endif
268 }
269
270 int
271 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
272         char **krbinstancep, int async )
273 {
274         int rc = -1;
275         int port;
276         long addr;
277
278         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
279
280         port = srv->lud_port;
281         if (port == 0)
282                 port = ld->ld_options.ldo_defport;
283         port = htons( (short) port );
284
285         addr = 0;
286         if ( srv->lud_host == NULL || *srv->lud_host == 0 )
287                 addr = htonl( INADDR_LOOPBACK );
288
289         switch ( ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
290                 case LDAP_PROTO_TCP:
291                         rc = ldap_connect_to_host( ld, sb, srv->lud_host,
292                                 addr, port, async );
293                         if ( rc == -1 )
294                                 return rc;
295                         ber_sockbuf_add_io( sb, &ber_sockbuf_io_tcp,
296                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
297                         break;
298                 case LDAP_PROTO_UDP:
299                         rc = ldap_connect_to_host( ld, sb, srv->lud_host,
300                                 addr, port, async );
301                         if ( rc == -1 )
302                                 return rc;
303                         ber_sockbuf_add_io( sb, &ber_sockbuf_io_udp,
304                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
305                         break;
306 #ifdef LDAP_PF_LOCAL
307                 case LDAP_PROTO_LOCAL:
308                         rc = ldap_connect_to_path( ld, sb, srv->lud_host,
309                                 async );
310                         if ( rc == -1 )
311                                 return rc;
312                         ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd,
313                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
314                         break;
315 #endif /* LDAP_PF_LOCAL */
316                 default:
317                         return -1;
318                         break;
319         }
320
321         ber_sockbuf_add_io( sb, &ber_sockbuf_io_readahead,
322                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
323 #ifdef LDAP_DEBUG
324         ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
325 #endif
326
327 #ifdef HAVE_TLS
328         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
329                 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
330         {
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 }