]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
a243637fc41829fd9a05d5f57e21d112584616c2
[openldap] / libraries / libldap / open.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22 #ifdef HAVE_LIMITS_H
23 #include <limits.h>
24 #endif
25
26 #include <ac/stdlib.h>
27
28 #include <ac/param.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31 #include <ac/time.h>
32
33 #include <ac/unistd.h>
34
35 #include "ldap-int.h"
36 #include "ldap_log.h"
37
38 /* Caller should hold the req_mutex if simultaneous accesses are possible */
39 int ldap_open_defconn( LDAP *ld )
40 {
41         ld->ld_defconn = ldap_new_connection( ld,
42                 &ld->ld_options.ldo_defludp, 1, 1, NULL );
43
44         if( ld->ld_defconn == NULL ) {
45                 ld->ld_errno = LDAP_SERVER_DOWN;
46                 return -1;
47         }
48
49         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
50         return 0;
51 }
52
53 /*
54  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
55  * be used for future communication is returned on success, NULL on failure.
56  * "host" may be a space-separated list of hosts or IP addresses
57  *
58  * Example:
59  *      LDAP    *ld;
60  *      ld = ldap_open( hostname, port );
61  */
62
63 LDAP *
64 ldap_open( LDAP_CONST char *host, int port )
65 {
66         int rc;
67         LDAP            *ld;
68
69         Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
70                 host, port, 0 );
71
72         ld = ldap_init( host, port );
73         if ( ld == NULL ) {
74                 return( NULL );
75         }
76
77         rc = ldap_open_defconn( ld );
78
79         if( rc < 0 ) {
80                 ldap_ld_free( ld, 0, NULL, NULL );
81                 ld = NULL;
82         }
83
84         Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
85                 ld == NULL ? "succeeded" : "failed", 0, 0 );
86
87         return ld;
88 }
89
90
91
92 int
93 ldap_create( LDAP **ldp )
94 {
95         LDAP                    *ld;
96         struct ldapoptions      *gopts;
97
98         *ldp = NULL;
99         /* Get pointer to global option structure */
100         if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
101                 return LDAP_NO_MEMORY;
102         }
103
104         /* Initialize the global options, if not already done. */
105         if( gopts->ldo_valid != LDAP_INITIALIZED ) {
106                 ldap_int_initialize(gopts, NULL);
107                 if ( gopts->ldo_valid != LDAP_INITIALIZED )
108                         return LDAP_LOCAL_ERROR;
109         }
110
111         Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
112
113         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
114                 return( LDAP_NO_MEMORY );
115         }
116    
117         /* copy the global options */
118         AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
119
120         ld->ld_valid = LDAP_VALID_SESSION;
121
122         /* but not pointers to malloc'ed items */
123         ld->ld_options.ldo_sctrls = NULL;
124         ld->ld_options.ldo_cctrls = NULL;
125         ld->ld_options.ldo_tm_api = NULL;
126         ld->ld_options.ldo_tm_net = NULL;
127         ld->ld_options.ldo_defludp = NULL;
128
129 #ifdef HAVE_CYRUS_SASL
130         ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
131                 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
132         ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
133                 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
134         ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
135                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
136         ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
137                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
138 #endif
139
140 #ifdef HAVE_TLS
141         /* We explicitly inherit the SSL_CTX, don't need the names/paths. Leave
142          * them empty to allow new SSL_CTX's to be created from scratch.
143          */
144         memset( &ld->ld_options.ldo_tls_info, 0,
145                 sizeof( ld->ld_options.ldo_tls_info ));
146         ld->ld_options.ldo_tls_ctx = NULL;
147 #endif
148
149         if ( gopts->ldo_tm_api &&
150                 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_api, gopts->ldo_tm_api ))
151                 goto nomem;
152
153         if ( gopts->ldo_tm_net &&
154                 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_net, gopts->ldo_tm_net ))
155                 goto nomem;
156
157         if ( gopts->ldo_defludp ) {
158                 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
159
160                 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
161         }
162
163         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
164
165         ld->ld_lberoptions = LBER_USE_DER;
166
167         ld->ld_sb = ber_sockbuf_alloc( );
168         if ( ld->ld_sb == NULL ) goto nomem;
169
170 #ifdef LDAP_R_COMPILE
171         ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
172         ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
173         ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
174 #endif
175         *ldp = ld;
176         return LDAP_SUCCESS;
177
178 nomem:
179         ldap_free_select_info( ld->ld_selectinfo );
180         ldap_free_urllist( ld->ld_options.ldo_defludp );
181         LDAP_FREE( ld->ld_options.ldo_tm_net );
182         LDAP_FREE( ld->ld_options.ldo_tm_api );
183 #ifdef HAVE_CYRUS_SASL
184         LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
185         LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
186         LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
187         LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
188 #endif
189         LDAP_FREE( (char *)ld );
190         return LDAP_NO_MEMORY;
191 }
192
193 /*
194  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
195  * future communication is returned on success, NULL on failure.
196  * "host" may be a space-separated list of hosts or IP addresses
197  *
198  * Example:
199  *      LDAP    *ld;
200  *      ld = ldap_init( host, port );
201  */
202 LDAP *
203 ldap_init( LDAP_CONST char *defhost, int defport )
204 {
205         LDAP *ld;
206         int rc;
207
208         rc = ldap_create(&ld);
209         if ( rc != LDAP_SUCCESS )
210                 return NULL;
211
212         if (defport != 0)
213                 ld->ld_options.ldo_defport = defport;
214
215         if (defhost != NULL) {
216                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
217                 if ( rc != LDAP_SUCCESS ) {
218                         ldap_ld_free(ld, 1, NULL, NULL);
219                         return NULL;
220                 }
221         }
222
223         return( ld );
224 }
225
226
227 int
228 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
229 {
230         int rc;
231         LDAP *ld;
232
233         *ldp = NULL;
234         rc = ldap_create(&ld);
235         if ( rc != LDAP_SUCCESS )
236                 return rc;
237
238         if (url != NULL) {
239                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
240                 if ( rc != LDAP_SUCCESS ) {
241                         ldap_ld_free(ld, 1, NULL, NULL);
242                         return rc;
243                 }
244 #ifdef LDAP_CONNECTIONLESS
245                 if (ldap_is_ldapc_url(url))
246                         LDAP_IS_UDP(ld) = 1;
247 #endif
248         }
249
250         *ldp = ld;
251         return LDAP_SUCCESS;
252 }
253
254 int
255 ldap_int_open_connection(
256         LDAP *ld,
257         LDAPConn *conn,
258         LDAPURLDesc *srv,
259         int async )
260 {
261         int rc = -1;
262         char *host;
263         int port, proto;
264
265         Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
266
267         switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
268                 case LDAP_PROTO_TCP:
269                         port = srv->lud_port;
270
271                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
272                                 host = NULL;
273                         } else {
274                                 host = srv->lud_host;
275                         }
276
277                         if( !port ) {
278                                 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
279                                         port = LDAPS_PORT;
280                                 } else {
281                                         port = LDAP_PORT;
282                                 }
283                         }
284
285                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
286                                 proto, host, port, async );
287
288                         if ( rc == -1 ) return rc;
289
290 #ifdef LDAP_DEBUG
291                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
292                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
293 #endif
294                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
295                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
296
297                         break;
298
299 #ifdef LDAP_CONNECTIONLESS
300                 case LDAP_PROTO_UDP:
301                         port = srv->lud_port;
302
303                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
304                                 host = NULL;
305                         } else {
306                                 host = srv->lud_host;
307                         }
308
309                         if( !port ) port = LDAP_PORT;
310
311                         LDAP_IS_UDP(ld) = 1;
312                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
313                                 proto, host, port, async );
314
315                         if ( rc == -1 ) return rc;
316 #ifdef LDAP_DEBUG
317                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
318                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
319 #endif
320                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
321                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
322
323                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
324                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
325
326                         break;
327 #endif
328                 case LDAP_PROTO_IPC:
329 #ifdef LDAP_PF_LOCAL
330                         /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
331                         rc = ldap_connect_to_path( ld, conn->lconn_sb,
332                                 srv->lud_host, async );
333                         if ( rc == -1 ) return rc;
334 #ifdef LDAP_DEBUG
335                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
336                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
337 #endif
338                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
339                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
340
341                         break;
342 #endif /* LDAP_PF_LOCAL */
343                 default:
344                         return -1;
345                         break;
346         }
347
348         conn->lconn_created = time( NULL );
349
350 #ifdef LDAP_DEBUG
351         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
352                 INT_MAX, (void *)"ldap_" );
353 #endif
354
355 #ifdef LDAP_CONNECTIONLESS
356         if( proto == LDAP_PROTO_UDP ) return 0;
357 #endif
358
359 #ifdef HAVE_TLS
360         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
361                 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
362         {
363                 ++conn->lconn_refcnt;   /* avoid premature free */
364
365                 rc = ldap_int_tls_start( ld, conn, srv );
366
367                 --conn->lconn_refcnt;
368
369                 if (rc != LDAP_SUCCESS) {
370                         return -1;
371                 }
372         }
373 #endif
374
375         return( 0 );
376 }
377
378
379 int
380 ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
381 {
382         int rc;
383         LDAPConn *c;
384         LDAPRequest *lr;
385
386         rc = ldap_create( ldp );
387         if( rc != LDAP_SUCCESS ) {
388                 *ldp = NULL;
389                 return( rc );
390         }
391
392         /* Make it appear that a search request, msgid 0, was sent */
393         lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
394         if( lr == NULL ) {
395                 ldap_unbind_ext( *ldp, NULL, NULL );
396                 *ldp = NULL;
397                 return( LDAP_NO_MEMORY );
398         }
399         memset(lr, 0, sizeof( LDAPRequest ));
400         lr->lr_msgid = 0;
401         lr->lr_status = LDAP_REQST_INPROGRESS;
402         lr->lr_res_errno = LDAP_SUCCESS;
403         /* no mutex lock needed, we just created this ld here */
404         (*ldp)->ld_requests = lr;
405
406         /* Attach the passed socket as the *LDAP's connection */
407         c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
408         if( c == NULL ) {
409                 ldap_unbind_ext( *ldp, NULL, NULL );
410                 *ldp = NULL;
411                 return( LDAP_NO_MEMORY );
412         }
413         ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
414 #ifdef LDAP_DEBUG
415         ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
416                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
417 #endif
418         ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
419           LBER_SBIOD_LEVEL_PROVIDER, NULL );
420         (*ldp)->ld_defconn = c;
421
422         /* Add the connection to the *LDAP's select pool */
423         ldap_mark_select_read( *ldp, c->lconn_sb );
424         ldap_mark_select_write( *ldp, c->lconn_sb );
425
426         /* Make this connection an LDAP V3 protocol connection */
427         rc = LDAP_VERSION3;
428         ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
429
430         return( LDAP_SUCCESS );
431 }