]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
ITS#1730
[openldap] / libraries / libldap / open.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1995 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  open.c
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <limits.h>
17
18 #include <ac/stdlib.h>
19
20 #include <ac/param.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/time.h>
24
25 #include "ldap-int.h"
26
27 int ldap_open_defconn( LDAP *ld )
28 {
29         ld->ld_defconn = ldap_new_connection( ld,
30                 ld->ld_options.ldo_defludp, 1, 1, NULL );
31
32         if( ld->ld_defconn == NULL ) {
33                 ld->ld_errno = LDAP_SERVER_DOWN;
34                 return -1;
35         }
36
37         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
38         return 0;
39 }
40
41 /*
42  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
43  * be used for future communication is returned on success, NULL on failure.
44  * "host" may be a space-separated list of hosts or IP addresses
45  *
46  * Example:
47  *      LDAP    *ld;
48  *      ld = ldap_open( hostname, port );
49  */
50
51 LDAP *
52 ldap_open( LDAP_CONST char *host, int port )
53 {
54         int rc;
55         LDAP            *ld;
56
57 #ifdef NEW_LOGGING
58         LDAP_LOG (( "open", LDAP_LEVEL_ARGS, "ldap_open(%s, %d)\n",
59                 host, port ));
60 #else
61         Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
62                 host, port, 0 );
63 #endif
64
65         ld = ldap_init( host, port );
66         if ( ld == NULL ) {
67                 return( NULL );
68         }
69
70         rc = ldap_open_defconn( ld );
71
72         if( rc < 0 ) {
73                 ldap_ld_free( ld, 0, NULL, NULL );
74                 ld = NULL;
75         }
76
77 #ifdef NEW_LOGGING
78         LDAP_LOG (( "open", LDAP_LEVEL_RESULTS, "ldap_open: %s\n",
79                 ld == NULL ? "succeeded" : "failed" ));
80 #else
81         Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
82                 ld == NULL ? "succeeded" : "failed", 0, 0 );
83 #endif
84
85         return ld;
86 }
87
88
89
90 int
91 ldap_create( LDAP **ldp )
92 {
93         LDAP                    *ld;
94         struct ldapoptions      *gopts;
95
96         *ldp = NULL;
97         /* Get pointer to global option structure */
98         if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
99                 return LDAP_NO_MEMORY;
100         }
101
102         /* Initialize the global options, if not already done. */
103         if( gopts->ldo_valid != LDAP_INITIALIZED ) {
104                 ldap_int_initialize(gopts, NULL);
105                 if ( gopts->ldo_valid != LDAP_INITIALIZED )
106                         return LDAP_LOCAL_ERROR;
107         }
108
109 #ifdef NEW_LOGGING
110         LDAP_LOG (( "open", LDAP_LEVEL_ENTRY, "ldap_create\n" ));
111 #else
112         Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
113 #endif
114
115         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
116                 return( LDAP_NO_MEMORY );
117         }
118    
119         /* copy the global options */
120         AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
121
122         ld->ld_valid = LDAP_VALID_SESSION;
123
124         /* but not pointers to malloc'ed items */
125         ld->ld_options.ldo_sctrls = NULL;
126         ld->ld_options.ldo_cctrls = 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         ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
140
141         if ( ld->ld_options.ldo_defludp == NULL ) {
142                 LDAP_FREE( (char*)ld );
143                 return LDAP_NO_MEMORY;
144         }
145
146         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
147                 ldap_free_urllist( ld->ld_options.ldo_defludp );
148                 LDAP_FREE( (char*) ld );
149                 return LDAP_NO_MEMORY;
150         }
151
152         ld->ld_lberoptions = LBER_USE_DER;
153
154         ld->ld_sb = ber_sockbuf_alloc( );
155         if ( ld->ld_sb == NULL ) {
156                 ldap_free_urllist( ld->ld_options.ldo_defludp );
157                 LDAP_FREE( (char*) ld );
158                 return LDAP_NO_MEMORY;
159         }
160
161         *ldp = ld;
162         return LDAP_SUCCESS;
163 }
164
165 /*
166  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
167  * future communication is returned on success, NULL on failure.
168  * "host" may be a space-separated list of hosts or IP addresses
169  *
170  * Example:
171  *      LDAP    *ld;
172  *      ld = ldap_init( host, port );
173  */
174 LDAP *
175 ldap_init( LDAP_CONST char *defhost, int defport )
176 {
177         LDAP *ld;
178         int rc;
179
180         rc = ldap_create(&ld);
181         if ( rc != LDAP_SUCCESS )
182                 return NULL;
183
184         if (defport != 0)
185                 ld->ld_options.ldo_defport = defport;
186
187         if (defhost != NULL) {
188                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
189                 if ( rc != LDAP_SUCCESS ) {
190                         ldap_ld_free(ld, 1, NULL, NULL);
191                         return NULL;
192                 }
193         }
194
195         return( ld );
196 }
197
198
199 int
200 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
201 {
202         int rc;
203         LDAP *ld;
204
205         *ldp = NULL;
206         rc = ldap_create(&ld);
207         if ( rc != LDAP_SUCCESS )
208                 return rc;
209
210         if (url != NULL) {
211                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
212                 if ( rc != LDAP_SUCCESS ) {
213                         ldap_ld_free(ld, 1, NULL, NULL);
214                         return rc;
215                 }
216 #ifdef LDAP_CONNECTIONLESS
217                 if (ldap_is_ldapc_url(url))
218                         LDAP_IS_UDP(ld) = 1;
219 #endif
220         }
221
222         *ldp = ld;
223         return LDAP_SUCCESS;
224 }
225
226 int
227 ldap_int_open_connection(
228         LDAP *ld,
229         LDAPConn *conn,
230         LDAPURLDesc *srv,
231         int async )
232 {
233         int rc = -1;
234 #ifdef HAVE_CYRUS_SASL
235         char *sasl_host = NULL;
236         int sasl_ssf = 0;
237 #endif
238         char *host;
239         int port, proto;
240         long addr;
241
242 #ifdef NEW_LOGGING
243         LDAP_LOG (( "open", LDAP_LEVEL_ENTRY, "ldap_int_open_connection\n" ));
244 #else
245         Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
246 #endif
247
248         switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
249                 case LDAP_PROTO_TCP:
250                         port = srv->lud_port;
251
252                         addr = 0;
253                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
254                                 host = NULL;
255                                 addr = htonl( INADDR_LOOPBACK );
256                         } else {
257                                 host = srv->lud_host;
258                         }
259
260                         if( !port ) {
261                                 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
262                                         port = LDAPS_PORT;
263                                 } else {
264                                         port = LDAP_PORT;
265                                 }
266                         }
267
268                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
269                                 proto, host, addr, port, async );
270
271                         if ( rc == -1 ) return rc;
272
273 #ifdef LDAP_DEBUG
274                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
275                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
276 #endif
277                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
278                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
279
280 #ifdef HAVE_CYRUS_SASL
281                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
282 #endif
283                         break;
284 #ifdef LDAP_CONNECTIONLESS
285
286                 case LDAP_PROTO_UDP:
287                         port = srv->lud_port;
288
289                         addr = 0;
290                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
291                                 host = NULL;
292                                 addr = htonl( INADDR_LOOPBACK );
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, addr, 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                         break;
311 #endif
312                 case LDAP_PROTO_IPC:
313 #ifdef LDAP_PF_LOCAL
314                         /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
315                         rc = ldap_connect_to_path( ld, conn->lconn_sb,
316                                 srv->lud_host, async );
317                         if ( rc == -1 ) return rc;
318 #ifdef LDAP_DEBUG
319                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
320                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
321 #endif
322                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
323                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
324
325 #ifdef HAVE_CYRUS_SASL
326                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
327                         sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
328 #endif
329                         break;
330 #endif /* LDAP_PF_LOCAL */
331                 default:
332                         return -1;
333                         break;
334         }
335
336         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
337                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
338
339 #ifdef LDAP_DEBUG
340         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
341                 INT_MAX, (void *)"ldap_" );
342 #endif
343
344 #ifdef LDAP_CONNECTIONLESS
345         if( proto == LDAP_PROTO_UDP )
346                 return 0;
347 #endif
348
349 #ifdef HAVE_CYRUS_SASL
350         /* establish Cyrus SASL context prior to starting TLS so
351                 that SASL EXTERNAL might be used */
352         if( sasl_host != NULL ) {
353                 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
354                 LDAP_FREE( sasl_host );
355         }
356 #endif
357
358 #ifdef HAVE_TLS
359         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
360                 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
361         {
362                 ++conn->lconn_refcnt;   /* avoid premature free */
363
364                 rc = ldap_int_tls_start( ld, conn, srv );
365
366                 --conn->lconn_refcnt;
367
368                 if (rc != LDAP_SUCCESS) {
369                         return -1;
370                 }
371         }
372 #endif
373
374 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
375         if ( conn->lconn_krbinstance == NULL ) {
376                 char *c;
377                 conn->lconn_krbinstance = ldap_host_connected_to( conn->lconn_sb );
378
379                 if( conn->lconn_krbinstance != NULL && 
380                     ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
381                         *c = '\0';
382                 }
383         }
384 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
385
386         return( 0 );
387 }
388
389
390 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
391 {
392         int rc;
393         LDAPConn *c;
394         LDAPRequest *lr;
395
396         rc = ldap_create( ldp );
397         if( rc != LDAP_SUCCESS ) {
398                 *ldp = NULL;
399                 return( rc );
400         }
401
402         /* Make it appear that a search request, msgid 0, was sent */
403         lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
404         if( lr == NULL ) {
405                 ldap_unbind( *ldp );
406                 *ldp = NULL;
407                 return( LDAP_NO_MEMORY );
408         }
409         memset(lr, 0, sizeof( LDAPRequest ));
410         lr->lr_msgid = 0;
411         lr->lr_status = LDAP_REQST_INPROGRESS;
412         lr->lr_res_errno = LDAP_SUCCESS;
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 }