]> 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-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
123 #ifdef HAVE_CYRUS_SASL
124         ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
125                 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
126         ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
127                 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
128         ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
129                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
130         ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
131                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
132 #endif
133
134         ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
135
136         if ( ld->ld_options.ldo_defludp == NULL ) {
137                 LDAP_FREE( (char*)ld );
138                 return LDAP_NO_MEMORY;
139         }
140
141         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
142                 ldap_free_urllist( ld->ld_options.ldo_defludp );
143                 LDAP_FREE( (char*) ld );
144                 return LDAP_NO_MEMORY;
145         }
146
147         ld->ld_lberoptions = LBER_USE_DER;
148
149         ld->ld_sb = ber_sockbuf_alloc( );
150         if ( ld->ld_sb == NULL ) {
151                 ldap_free_urllist( ld->ld_options.ldo_defludp );
152                 LDAP_FREE( (char*) ld );
153                 return LDAP_NO_MEMORY;
154         }
155
156 #ifdef LDAP_R_COMPILE
157         ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
158         ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
159 #endif
160         *ldp = ld;
161         return LDAP_SUCCESS;
162 }
163
164 /*
165  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
166  * future communication is returned on success, NULL on failure.
167  * "host" may be a space-separated list of hosts or IP addresses
168  *
169  * Example:
170  *      LDAP    *ld;
171  *      ld = ldap_init( host, port );
172  */
173 LDAP *
174 ldap_init( LDAP_CONST char *defhost, int defport )
175 {
176         LDAP *ld;
177         int rc;
178
179         rc = ldap_create(&ld);
180         if ( rc != LDAP_SUCCESS )
181                 return NULL;
182
183         if (defport != 0)
184                 ld->ld_options.ldo_defport = defport;
185
186         if (defhost != NULL) {
187                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
188                 if ( rc != LDAP_SUCCESS ) {
189                         ldap_ld_free(ld, 1, NULL, NULL);
190                         return NULL;
191                 }
192         }
193
194         return( ld );
195 }
196
197
198 int
199 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
200 {
201         int rc;
202         LDAP *ld;
203
204         *ldp = NULL;
205         rc = ldap_create(&ld);
206         if ( rc != LDAP_SUCCESS )
207                 return rc;
208
209         if (url != NULL) {
210                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
211                 if ( rc != LDAP_SUCCESS ) {
212                         ldap_ld_free(ld, 1, NULL, NULL);
213                         return rc;
214                 }
215 #ifdef LDAP_CONNECTIONLESS
216                 if (ldap_is_ldapc_url(url))
217                         LDAP_IS_UDP(ld) = 1;
218 #endif
219         }
220
221         *ldp = ld;
222         return LDAP_SUCCESS;
223 }
224
225 int
226 ldap_int_open_connection(
227         LDAP *ld,
228         LDAPConn *conn,
229         LDAPURLDesc *srv,
230         int async )
231 {
232         int rc = -1;
233         char *host;
234         int port, proto;
235
236         Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
237
238         switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
239                 case LDAP_PROTO_TCP:
240                         port = srv->lud_port;
241
242                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
243                                 host = NULL;
244                         } else {
245                                 host = srv->lud_host;
246                         }
247
248                         if( !port ) {
249                                 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
250                                         port = LDAPS_PORT;
251                                 } else {
252                                         port = LDAP_PORT;
253                                 }
254                         }
255
256                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
257                                 proto, host, port, async );
258
259                         if ( rc == -1 ) return rc;
260
261 #ifdef LDAP_DEBUG
262                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
263                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
264 #endif
265                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
266                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
267
268                         break;
269
270 #ifdef LDAP_CONNECTIONLESS
271                 case LDAP_PROTO_UDP:
272                         port = srv->lud_port;
273
274                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
275                                 host = NULL;
276                         } else {
277                                 host = srv->lud_host;
278                         }
279
280                         if( !port ) port = LDAP_PORT;
281
282                         LDAP_IS_UDP(ld) = 1;
283                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
284                                 proto, host, port, async );
285
286                         if ( rc == -1 ) return rc;
287 #ifdef LDAP_DEBUG
288                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
289                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
290 #endif
291                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
292                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
293
294                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
295                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
296
297                         break;
298 #endif
299                 case LDAP_PROTO_IPC:
300 #ifdef LDAP_PF_LOCAL
301                         /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
302                         rc = ldap_connect_to_path( ld, conn->lconn_sb,
303                                 srv->lud_host, async );
304                         if ( rc == -1 ) return rc;
305 #ifdef LDAP_DEBUG
306                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
307                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
308 #endif
309                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
310                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
311
312                         break;
313 #endif /* LDAP_PF_LOCAL */
314                 default:
315                         return -1;
316                         break;
317         }
318
319 #ifdef LDAP_DEBUG
320         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
321                 INT_MAX, (void *)"ldap_" );
322 #endif
323
324 #ifdef LDAP_CONNECTIONLESS
325         if( proto == LDAP_PROTO_UDP ) return 0;
326 #endif
327
328 #ifdef HAVE_TLS
329         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
330                 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
331         {
332                 ++conn->lconn_refcnt;   /* avoid premature free */
333
334                 rc = ldap_int_tls_start( ld, conn, srv );
335
336                 --conn->lconn_refcnt;
337
338                 if (rc != LDAP_SUCCESS) {
339                         return -1;
340                 }
341         }
342 #endif
343
344 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
345         if ( conn->lconn_krbinstance == NULL ) {
346                 char *c;
347                 conn->lconn_krbinstance = ldap_host_connected_to(
348                         conn->lconn_sb, host );
349
350                 if( conn->lconn_krbinstance != NULL && 
351                     ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
352                         *c = '\0';
353                 }
354         }
355 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
356
357         return( 0 );
358 }
359
360
361 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
362 {
363         int rc;
364         LDAPConn *c;
365         LDAPRequest *lr;
366
367         rc = ldap_create( ldp );
368         if( rc != LDAP_SUCCESS ) {
369                 *ldp = NULL;
370                 return( rc );
371         }
372
373         /* Make it appear that a search request, msgid 0, was sent */
374         lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
375         if( lr == NULL ) {
376                 ldap_unbind( *ldp );
377                 *ldp = NULL;
378                 return( LDAP_NO_MEMORY );
379         }
380         memset(lr, 0, sizeof( LDAPRequest ));
381         lr->lr_msgid = 0;
382         lr->lr_status = LDAP_REQST_INPROGRESS;
383         lr->lr_res_errno = LDAP_SUCCESS;
384         /* no mutex lock needed, we just created this ld here */
385         (*ldp)->ld_requests = lr;
386
387         /* Attach the passed socket as the *LDAP's connection */
388         c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
389         if( c == NULL ) {
390                 ldap_unbind( *ldp );
391                 *ldp = NULL;
392                 return( LDAP_NO_MEMORY );
393         }
394         ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
395 #ifdef LDAP_DEBUG
396         ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
397                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
398 #endif
399         ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
400           LBER_SBIOD_LEVEL_PROVIDER, NULL );
401         (*ldp)->ld_defconn = c;
402
403         /* Add the connection to the *LDAP's select pool */
404         ldap_mark_select_read( *ldp, c->lconn_sb );
405         ldap_mark_select_write( *ldp, c->lconn_sb );
406
407         /* Make this connection an LDAP V3 protocol connection */
408         rc = LDAP_VERSION3;
409         ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
410
411         return( LDAP_SUCCESS );
412 }