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