]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
eaa6a6cd1f139e26c757fd4bd981f51e86bac5fb
[openldap] / libraries / libldap / open.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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         if ( gopts->ldo_tm_api &&
140                 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_api, gopts->ldo_tm_api ))
141                 goto nomem;
142
143         if ( gopts->ldo_tm_net &&
144                 ldap_int_timeval_dup( &ld->ld_options.ldo_tm_net, gopts->ldo_tm_net ))
145                 goto nomem;
146
147         if ( gopts->ldo_defludp ) {
148                 ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
149
150                 if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
151         }
152
153         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
154
155         ld->ld_lberoptions = LBER_USE_DER;
156
157         ld->ld_sb = ber_sockbuf_alloc( );
158         if ( ld->ld_sb == NULL ) goto nomem;
159
160 #ifdef LDAP_R_COMPILE
161         ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
162         ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
163         ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
164 #endif
165         *ldp = ld;
166         return LDAP_SUCCESS;
167
168 nomem:
169         ldap_free_select_info( ld->ld_selectinfo );
170         ldap_free_urllist( ld->ld_options.ldo_defludp );
171         LDAP_FREE( ld->ld_options.ldo_tm_net );
172         LDAP_FREE( ld->ld_options.ldo_tm_api );
173 #ifdef HAVE_CYRUS_SASL
174         LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
175         LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
176         LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
177         LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
178 #endif
179         LDAP_FREE( (char *)ld );
180         return LDAP_NO_MEMORY;
181 }
182
183 /*
184  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
185  * future communication is returned on success, NULL on failure.
186  * "host" may be a space-separated list of hosts or IP addresses
187  *
188  * Example:
189  *      LDAP    *ld;
190  *      ld = ldap_init( host, port );
191  */
192 LDAP *
193 ldap_init( LDAP_CONST char *defhost, int defport )
194 {
195         LDAP *ld;
196         int rc;
197
198         rc = ldap_create(&ld);
199         if ( rc != LDAP_SUCCESS )
200                 return NULL;
201
202         if (defport != 0)
203                 ld->ld_options.ldo_defport = defport;
204
205         if (defhost != NULL) {
206                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
207                 if ( rc != LDAP_SUCCESS ) {
208                         ldap_ld_free(ld, 1, NULL, NULL);
209                         return NULL;
210                 }
211         }
212
213         return( ld );
214 }
215
216
217 int
218 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
219 {
220         int rc;
221         LDAP *ld;
222
223         *ldp = NULL;
224         rc = ldap_create(&ld);
225         if ( rc != LDAP_SUCCESS )
226                 return rc;
227
228         if (url != NULL) {
229                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
230                 if ( rc != LDAP_SUCCESS ) {
231                         ldap_ld_free(ld, 1, NULL, NULL);
232                         return rc;
233                 }
234 #ifdef LDAP_CONNECTIONLESS
235                 if (ldap_is_ldapc_url(url))
236                         LDAP_IS_UDP(ld) = 1;
237 #endif
238         }
239
240         *ldp = ld;
241         return LDAP_SUCCESS;
242 }
243
244 int
245 ldap_int_open_connection(
246         LDAP *ld,
247         LDAPConn *conn,
248         LDAPURLDesc *srv,
249         int async )
250 {
251         int rc = -1;
252         char *host;
253         int port, proto;
254
255         Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
256
257         switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
258                 case LDAP_PROTO_TCP:
259                         port = srv->lud_port;
260
261                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
262                                 host = NULL;
263                         } else {
264                                 host = srv->lud_host;
265                         }
266
267                         if( !port ) {
268                                 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
269                                         port = LDAPS_PORT;
270                                 } else {
271                                         port = LDAP_PORT;
272                                 }
273                         }
274
275                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
276                                 proto, host, port, async );
277
278                         if ( rc == -1 ) return rc;
279
280 #ifdef LDAP_DEBUG
281                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
282                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
283 #endif
284                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
285                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
286
287                         break;
288
289 #ifdef LDAP_CONNECTIONLESS
290                 case LDAP_PROTO_UDP:
291                         port = srv->lud_port;
292
293                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
294                                 host = NULL;
295                         } else {
296                                 host = srv->lud_host;
297                         }
298
299                         if( !port ) port = LDAP_PORT;
300
301                         LDAP_IS_UDP(ld) = 1;
302                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
303                                 proto, host, port, async );
304
305                         if ( rc == -1 ) return rc;
306 #ifdef LDAP_DEBUG
307                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
308                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
309 #endif
310                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
311                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
312
313                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
314                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
315
316                         break;
317 #endif
318                 case LDAP_PROTO_IPC:
319 #ifdef LDAP_PF_LOCAL
320                         /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
321                         rc = ldap_connect_to_path( ld, conn->lconn_sb,
322                                 srv->lud_host, async );
323                         if ( rc == -1 ) return rc;
324 #ifdef LDAP_DEBUG
325                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
326                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
327 #endif
328                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
329                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
330
331                         break;
332 #endif /* LDAP_PF_LOCAL */
333                 default:
334                         return -1;
335                         break;
336         }
337
338 #ifdef LDAP_DEBUG
339         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
340                 INT_MAX, (void *)"ldap_" );
341 #endif
342
343 #ifdef LDAP_CONNECTIONLESS
344         if( proto == LDAP_PROTO_UDP ) return 0;
345 #endif
346
347 #ifdef HAVE_TLS
348         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
349                 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
350         {
351                 ++conn->lconn_refcnt;   /* avoid premature free */
352
353                 rc = ldap_int_tls_start( ld, conn, srv );
354
355                 --conn->lconn_refcnt;
356
357                 if (rc != LDAP_SUCCESS) {
358                         return -1;
359                 }
360         }
361 #endif
362
363 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
364         if ( conn->lconn_krbinstance == NULL ) {
365                 char *c;
366                 conn->lconn_krbinstance = ldap_host_connected_to(
367                         conn->lconn_sb, host );
368
369                 if( conn->lconn_krbinstance != NULL && 
370                     ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
371                         *c = '\0';
372                 }
373         }
374 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
375
376         return( 0 );
377 }
378
379
380 int 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( *ldp );
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( *ldp );
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 }