]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Happy New Year!
[openldap] / libraries / libldap / open.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2017 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 must hold the conn_mutex since 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, 0, 0 );
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         LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
78         rc = ldap_open_defconn( ld );
79         LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
80
81         if( rc < 0 ) {
82                 ldap_ld_free( ld, 0, NULL, NULL );
83                 ld = NULL;
84         }
85
86         Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
87                 ld != NULL ? "succeeded" : "failed", 0, 0 );
88
89         return ld;
90 }
91
92
93
94 int
95 ldap_create( LDAP **ldp )
96 {
97         LDAP                    *ld;
98         struct ldapoptions      *gopts;
99
100         *ldp = NULL;
101         /* Get pointer to global option structure */
102         if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
103                 return LDAP_NO_MEMORY;
104         }
105
106         /* Initialize the global options, if not already done. */
107         if( gopts->ldo_valid != LDAP_INITIALIZED ) {
108                 ldap_int_initialize(gopts, NULL);
109                 if ( gopts->ldo_valid != LDAP_INITIALIZED )
110                         return LDAP_LOCAL_ERROR;
111         }
112
113         Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
114
115         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
116                 return( LDAP_NO_MEMORY );
117         }
118    
119         if ( (ld->ldc = (struct ldap_common *) LDAP_CALLOC( 1,
120                         sizeof(struct ldap_common) )) == NULL ) {
121                 LDAP_FREE( (char *)ld );
122                 return( LDAP_NO_MEMORY );
123         }
124         /* copy the global options */
125         LDAP_MUTEX_LOCK( &gopts->ldo_mutex );
126         AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
127 #ifdef LDAP_R_COMPILE
128         /* Properly initialize the structs mutex */
129         ldap_pvt_thread_mutex_init( &(ld->ld_ldopts_mutex) );
130 #endif
131         LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
132
133         ld->ld_valid = LDAP_VALID_SESSION;
134
135         /* but not pointers to malloc'ed items */
136         ld->ld_options.ldo_sctrls = NULL;
137         ld->ld_options.ldo_cctrls = NULL;
138         ld->ld_options.ldo_defludp = NULL;
139         ld->ld_options.ldo_conn_cbs = NULL;
140
141 #ifdef HAVE_CYRUS_SASL
142         ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
143                 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
144         ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
145                 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
146         ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
147                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
148         ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
149                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
150 #endif
151
152 #ifdef HAVE_TLS
153         /* We explicitly inherit the SSL_CTX, don't need the names/paths. Leave
154          * them empty to allow new SSL_CTX's to be created from scratch.
155          */
156         memset( &ld->ld_options.ldo_tls_info, 0,
157                 sizeof( ld->ld_options.ldo_tls_info ));
158         ld->ld_options.ldo_tls_ctx = NULL;
159 #endif
160
161         if ( gopts->ldo_defludp ) {
162                 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
163
164                 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
165         }
166
167         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
168
169         ld->ld_lberoptions = LBER_USE_DER;
170
171         ld->ld_sb = ber_sockbuf_alloc( );
172         if ( ld->ld_sb == NULL ) goto nomem;
173
174 #ifdef LDAP_R_COMPILE
175         ldap_pvt_thread_mutex_init( &ld->ld_msgid_mutex );
176         ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
177         ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
178         ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
179         ldap_pvt_thread_mutex_init( &ld->ld_abandon_mutex );
180         ldap_pvt_thread_mutex_init( &ld->ld_ldcmutex );
181 #endif
182         ld->ld_ldcrefcnt = 1;
183         *ldp = ld;
184         return LDAP_SUCCESS;
185
186 nomem:
187         ldap_free_select_info( ld->ld_selectinfo );
188         ldap_free_urllist( ld->ld_options.ldo_defludp );
189 #ifdef HAVE_CYRUS_SASL
190         LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
191         LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
192         LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
193         LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
194 #endif
195         LDAP_FREE( (char *)ld );
196         return LDAP_NO_MEMORY;
197 }
198
199 /*
200  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
201  * future communication is returned on success, NULL on failure.
202  * "host" may be a space-separated list of hosts or IP addresses
203  *
204  * Example:
205  *      LDAP    *ld;
206  *      ld = ldap_init( host, port );
207  */
208 LDAP *
209 ldap_init( LDAP_CONST char *defhost, int defport )
210 {
211         LDAP *ld;
212         int rc;
213
214         rc = ldap_create(&ld);
215         if ( rc != LDAP_SUCCESS )
216                 return NULL;
217
218         if (defport != 0)
219                 ld->ld_options.ldo_defport = defport;
220
221         if (defhost != NULL) {
222                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
223                 if ( rc != LDAP_SUCCESS ) {
224                         ldap_ld_free(ld, 1, NULL, NULL);
225                         return NULL;
226                 }
227         }
228
229         return( ld );
230 }
231
232
233 int
234 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
235 {
236         int rc;
237         LDAP *ld;
238
239         *ldp = NULL;
240         rc = ldap_create(&ld);
241         if ( rc != LDAP_SUCCESS )
242                 return rc;
243
244         if (url != NULL) {
245                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
246                 if ( rc != LDAP_SUCCESS ) {
247                         ldap_ld_free(ld, 1, NULL, NULL);
248                         return rc;
249                 }
250 #ifdef LDAP_CONNECTIONLESS
251                 if (ldap_is_ldapc_url(url))
252                         LDAP_IS_UDP(ld) = 1;
253 #endif
254         }
255
256         *ldp = ld;
257         return LDAP_SUCCESS;
258 }
259
260 int
261 ldap_init_fd(
262         ber_socket_t fd,
263         int proto,
264         LDAP_CONST char *url,
265         LDAP **ldp
266 )
267 {
268         int rc;
269         LDAP *ld;
270         LDAPConn *conn;
271 #ifdef LDAP_CONNECTIONLESS
272         ber_socklen_t   len;
273 #endif
274
275         *ldp = NULL;
276         rc = ldap_create( &ld );
277         if( rc != LDAP_SUCCESS )
278                 return( rc );
279
280         if (url != NULL) {
281                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
282                 if ( rc != LDAP_SUCCESS ) {
283                         ldap_ld_free(ld, 1, NULL, NULL);
284                         return rc;
285                 }
286         }
287
288         LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
289         /* Attach the passed socket as the LDAP's connection */
290         conn = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
291         if( conn == NULL ) {
292                 ldap_unbind_ext( ld, NULL, NULL );
293                 return( LDAP_NO_MEMORY );
294         }
295         if( url )
296                 conn->lconn_server = ldap_url_dup( ld->ld_options.ldo_defludp );
297         ber_sockbuf_ctrl( conn->lconn_sb, LBER_SB_OPT_SET_FD, &fd );
298         ld->ld_defconn = conn;
299         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
300         LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
301
302         switch( proto ) {
303         case LDAP_PROTO_TCP:
304 #ifdef LDAP_DEBUG
305                 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
306                         LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
307 #endif
308                 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
309                         LBER_SBIOD_LEVEL_PROVIDER, NULL );
310                 break;
311
312 #ifdef LDAP_CONNECTIONLESS
313         case LDAP_PROTO_UDP:
314                 LDAP_IS_UDP(ld) = 1;
315                 if( ld->ld_options.ldo_peer )
316                         ldap_memfree( ld->ld_options.ldo_peer );
317                 ld->ld_options.ldo_peer = ldap_memcalloc( 1, sizeof( struct sockaddr_storage ) );
318                 len = sizeof( struct sockaddr_storage );
319                 if( getpeername ( fd, ld->ld_options.ldo_peer, &len ) < 0) {
320                         ldap_unbind_ext( ld, NULL, NULL );
321                         return( AC_SOCKET_ERROR );
322                 }
323 #ifdef LDAP_DEBUG
324                 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
325                         LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
326 #endif
327                 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
328                         LBER_SBIOD_LEVEL_PROVIDER, NULL );
329                 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
330                         LBER_SBIOD_LEVEL_PROVIDER, NULL );
331                 break;
332 #endif /* LDAP_CONNECTIONLESS */
333
334         case LDAP_PROTO_IPC:
335 #ifdef LDAP_DEBUG
336                 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
337                         LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
338 #endif
339                 ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
340                         LBER_SBIOD_LEVEL_PROVIDER, NULL );
341                 break;
342
343         case LDAP_PROTO_EXT:
344                 /* caller must supply sockbuf handlers */
345                 break;
346
347         default:
348                 ldap_unbind_ext( ld, NULL, NULL );
349                 return LDAP_PARAM_ERROR;
350         }
351
352 #ifdef LDAP_DEBUG
353         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
354                 INT_MAX, (void *)"ldap_" );
355 #endif
356
357         /* Add the connection to the *LDAP's select pool */
358         ldap_mark_select_read( ld, conn->lconn_sb );
359         
360         *ldp = ld;
361         return LDAP_SUCCESS;
362 }
363
364 /* Protected by ld_conn_mutex */
365 int
366 ldap_int_open_connection(
367         LDAP *ld,
368         LDAPConn *conn,
369         LDAPURLDesc *srv,
370         int async )
371 {
372         int rc = -1;
373         int proto;
374
375         Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
376
377         switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
378                 case LDAP_PROTO_TCP:
379                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
380                                 proto, srv, async );
381
382                         if ( rc == -1 ) return rc;
383 #ifdef LDAP_DEBUG
384                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
385                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
386 #endif
387                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
388                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
389
390                         break;
391
392 #ifdef LDAP_CONNECTIONLESS
393                 case LDAP_PROTO_UDP:
394                         LDAP_IS_UDP(ld) = 1;
395                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
396                                 proto, srv, async );
397
398                         if ( rc == -1 ) return rc;
399 #ifdef LDAP_DEBUG
400                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
401                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
402 #endif
403                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
404                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
405
406                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
407                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
408
409                         break;
410 #endif
411                 case LDAP_PROTO_IPC:
412 #ifdef LDAP_PF_LOCAL
413                         /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
414                         rc = ldap_connect_to_path( ld, conn->lconn_sb,
415                                 srv, async );
416                         if ( rc == -1 ) return rc;
417 #ifdef LDAP_DEBUG
418                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
419                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
420 #endif
421                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
422                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
423
424                         break;
425 #endif /* LDAP_PF_LOCAL */
426                 default:
427                         return -1;
428                         break;
429         }
430
431         conn->lconn_created = time( NULL );
432
433 #ifdef LDAP_DEBUG
434         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
435                 INT_MAX, (void *)"ldap_" );
436 #endif
437
438 #ifdef LDAP_CONNECTIONLESS
439         if( proto == LDAP_PROTO_UDP ) return 0;
440 #endif
441
442 #ifdef HAVE_TLS
443         if (rc == 0 && ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
444                 strcmp( srv->lud_scheme, "ldaps" ) == 0 ))
445         {
446                 ++conn->lconn_refcnt;   /* avoid premature free */
447
448                 rc = ldap_int_tls_start( ld, conn, srv );
449
450                 --conn->lconn_refcnt;
451
452                 if (rc != LDAP_SUCCESS) {
453                         return -1;
454                 }
455         }
456 #endif
457
458         return( 0 );
459 }
460
461 /*
462  * ldap_open_internal_connection - open connection and set file descriptor
463  *
464  * note: ldap_init_fd() may be preferable
465  */
466
467 int
468 ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
469 {
470         int rc;
471         LDAPConn *c;
472         LDAPRequest *lr;
473         LDAP    *ld;
474
475         rc = ldap_create( &ld );
476         if( rc != LDAP_SUCCESS ) {
477                 *ldp = NULL;
478                 return( rc );
479         }
480
481         /* Make it appear that a search request, msgid 0, was sent */
482         lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
483         if( lr == NULL ) {
484                 ldap_unbind_ext( ld, NULL, NULL );
485                 *ldp = NULL;
486                 return( LDAP_NO_MEMORY );
487         }
488         memset(lr, 0, sizeof( LDAPRequest ));
489         lr->lr_msgid = 0;
490         lr->lr_status = LDAP_REQST_INPROGRESS;
491         lr->lr_res_errno = LDAP_SUCCESS;
492         /* no mutex lock needed, we just created this ld here */
493         ld->ld_requests = lr;
494
495         LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
496         /* Attach the passed socket as the *LDAP's connection */
497         c = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
498         if( c == NULL ) {
499                 ldap_unbind_ext( ld, NULL, NULL );
500                 *ldp = NULL;
501                 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
502                 return( LDAP_NO_MEMORY );
503         }
504         ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
505 #ifdef LDAP_DEBUG
506         ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
507                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
508 #endif
509         ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
510           LBER_SBIOD_LEVEL_PROVIDER, NULL );
511         ld->ld_defconn = c;
512         LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
513
514         /* Add the connection to the *LDAP's select pool */
515         ldap_mark_select_read( ld, c->lconn_sb );
516
517         /* Make this connection an LDAP V3 protocol connection */
518         rc = LDAP_VERSION3;
519         ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &rc );
520         *ldp = ld;
521
522         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
523
524         return( LDAP_SUCCESS );
525 }
526
527 LDAP *
528 ldap_dup( LDAP *old )
529 {
530         LDAP                    *ld;
531
532         if ( old == NULL ) {
533                 return( NULL );
534         }
535
536         Debug( LDAP_DEBUG_TRACE, "ldap_dup\n", 0, 0, 0 );
537
538         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
539                 return( NULL );
540         }
541    
542         LDAP_MUTEX_LOCK( &old->ld_ldcmutex );
543         ld->ldc = old->ldc;
544         old->ld_ldcrefcnt++;
545         LDAP_MUTEX_UNLOCK( &old->ld_ldcmutex );
546         return ( ld );
547 }
548
549 int
550 ldap_int_check_async_open( LDAP *ld, ber_socket_t sd )
551 {
552         struct timeval tv = { 0 };
553         int rc;
554
555         rc = ldap_int_poll( ld, sd, &tv, 1 );
556         switch ( rc ) {
557         case 0:
558                 /* now ready to start tls */
559                 ld->ld_defconn->lconn_status = LDAP_CONNST_CONNECTED;
560                 break;
561
562         default:
563                 ld->ld_errno = LDAP_CONNECT_ERROR;
564                 return -1;
565
566         case -2:
567                 /* connect not completed yet */
568                 ld->ld_errno = LDAP_X_CONNECTING;
569                 return rc;
570         }
571
572 #ifdef HAVE_TLS
573         if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
574                 !strcmp( ld->ld_defconn->lconn_server->lud_scheme, "ldaps" )) {
575
576                 ++ld->ld_defconn->lconn_refcnt; /* avoid premature free */
577
578                 rc = ldap_int_tls_start( ld, ld->ld_defconn, ld->ld_defconn->lconn_server );
579
580                 --ld->ld_defconn->lconn_refcnt;
581         }
582 #endif
583         return rc;
584 }