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