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