]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
d8b1aaeb55dcc0ccce14a7bd1650f13eaf1ebf98
[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         ld->ld_sb = ber_sockbuf_alloc( );
159         if ( ld->ld_sb == NULL ) {
160                 ldap_free_urllist( ld->ld_options.ldo_defludp );
161                 LDAP_FREE( (char*) ld );
162                 WSACleanup( );
163                 return LDAP_NO_MEMORY;
164         }
165
166         *ldp = ld;
167         return LDAP_SUCCESS;
168 }
169
170 /*
171  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
172  * future communication is returned on success, NULL on failure.
173  * "host" may be a space-separated list of hosts or IP addresses
174  *
175  * Example:
176  *      LDAP    *ld;
177  *      ld = ldap_open( host, port );
178  */
179 LDAP *
180 ldap_init( LDAP_CONST char *defhost, int defport )
181 {
182         LDAP *ld;
183         int rc;
184
185         rc = ldap_create(&ld);
186         if ( rc != LDAP_SUCCESS )
187                 return NULL;
188
189         if (defport != 0)
190                 ld->ld_options.ldo_defport = defport;
191
192         if (defhost != NULL) {
193                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
194                 if ( rc != LDAP_SUCCESS ) {
195                         ldap_ld_free(ld, 1, NULL, NULL);
196                         return NULL;
197                 }
198         }
199
200         return( ld );
201 }
202
203
204 int
205 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
206 {
207         int rc;
208         LDAP *ld;
209
210         *ldp = NULL;
211         rc = ldap_create(&ld);
212         if ( rc != LDAP_SUCCESS )
213                 return rc;
214
215         if (url != NULL) {
216                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
217                 if ( rc != LDAP_SUCCESS ) {
218                         ldap_ld_free(ld, 1, NULL, NULL);
219                         return rc;
220                 }
221         }
222
223         *ldp = ld;
224         return LDAP_SUCCESS;
225 }
226
227 int
228 ldap_start_tls ( LDAP *ld,
229                                 LDAPControl **serverctrls,
230                                 LDAPControl **clientctrls )
231 {
232 #ifdef HAVE_TLS
233         LDAPConn *lc;
234         int rc;
235         char *rspoid = NULL;
236         struct berval *rspdata = NULL;
237
238         if (ld->ld_conns == NULL) {
239                 rc = ldap_open_defconn( ld );
240                 if (rc != LDAP_SUCCESS)
241                         return(rc);
242         }
243
244         for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
245                 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
246                         return LDAP_OPERATIONS_ERROR;
247                 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
248                                                         NULL, serverctrls, clientctrls, &rspoid, &rspdata);
249                 if (rc != LDAP_SUCCESS)
250                         return rc;
251                 if (rspoid != NULL)
252                         LDAP_FREE(rspoid);
253                 if (rspdata != NULL)
254                         ber_bvfree(rspdata);
255                 rc = ldap_pvt_tls_start( ld, lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
256                 if (rc != LDAP_SUCCESS)
257                         return rc;
258         }
259         return LDAP_SUCCESS;
260 #else
261         return LDAP_NOT_SUPPORTED;
262 #endif
263 }
264
265 int
266 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
267         char **krbinstancep, int async )
268 {
269         int rc = -1;
270         int port;
271         long addr;
272
273         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
274
275         port = srv->lud_port;
276         if (port == 0)
277                 port = ld->ld_options.ldo_defport;
278         port = htons( (short) port );
279
280         addr = 0;
281         if ( srv->lud_host == NULL || *srv->lud_host == 0 )
282                 addr = htonl( INADDR_LOOPBACK );
283
284         switch ( srv->lud_protocol ) {
285                 case LDAP_PROTO_TCP:
286                         rc = ldap_connect_to_host( ld, sb, srv->lud_host,
287                                 addr, port, async );
288                         if ( rc == -1 )
289                                 return rc;
290                         ber_sockbuf_add_io( sb, &ber_sockbuf_io_tcp,
291                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
292                         break;
293                 case LDAP_PROTO_UDP:
294                         rc = ldap_connect_to_host( ld, sb, srv->lud_host,
295                                 addr, port, async );
296                         if ( rc == -1 )
297                                 return rc;
298                         ber_sockbuf_add_io( sb, &ber_sockbuf_io_udp,
299                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
300                         break;
301 #ifdef LDAP_PF_LOCAL
302                 case LDAP_PROTO_LOCAL:
303                         rc = ldap_connect_to_path( ld, sb, srv->lud_host,
304                                 async );
305                         if ( rc == -1 )
306                                 return rc;
307                         ber_sockbuf_add_io( sb, &ber_sockbuf_io_fd,
308                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
309                         break;
310 #endif /* LDAP_PF_LOCAL */
311                 default:
312                         return -1;
313                         break;
314         }
315
316         ber_sockbuf_add_io( sb, &ber_sockbuf_io_readahead,
317                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
318 #ifdef LDAP_DEBUG
319         ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug, INT_MAX, NULL );
320 #endif
321
322 #ifdef HAVE_TLS
323         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
324             (srv->lud_properties & LDAP_URL_USE_SSL)) {
325                 rc = ldap_pvt_tls_start( ld, sb, ld->ld_options.ldo_tls_ctx );
326                 if (rc != LDAP_SUCCESS)
327                         return rc;
328         }
329 #endif
330
331         if ( krbinstancep != NULL ) {
332 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
333                 char *c;
334                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
335                     ( c = strchr( *krbinstancep, '.' )) != NULL ) {
336                         *c = '\0';
337                 }
338 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
339                 *krbinstancep = NULL;
340 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
341         }
342
343         return( 0 );
344 }