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