]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
f2266c98af1da68808acb4bce035acf0491fb344
[openldap] / libraries / libldap / open.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 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\n", 0, 0, 0 );
58
59         if (( ld = ldap_init( host, port )) == NULL ) {
60                 return( NULL );
61         }
62
63         rc = ldap_open_defconn( ld );
64
65         if( rc < 0 ) {
66                 ldap_ld_free( ld, 0, NULL, NULL );
67                 return( NULL );
68         }
69
70         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
71                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
72
73         return( ld );
74 }
75
76
77
78 int
79 ldap_create( LDAP **ldp )
80 {
81         LDAP                    *ld;
82         struct ldapoptions      *gopts;
83
84         *ldp = NULL;
85         /* Get pointer to global option structure */
86         if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
87                 return LDAP_NO_MEMORY;
88         }
89
90         /* Initialize the global options, if not already done. */
91         if( gopts->ldo_valid != LDAP_INITIALIZED ) {
92                 ldap_int_initialize(gopts, NULL);
93         }
94
95         Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
96
97 #ifdef HAVE_WINSOCK2
98 {       WORD wVersionRequested;
99         WSADATA wsaData;
100  
101         wVersionRequested = MAKEWORD( 2, 0 );
102         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
103                 /* Tell the user that we couldn't find a usable */
104                 /* WinSock DLL.                                  */
105                 return LDAP_LOCAL_ERROR;
106         }
107  
108         /* Confirm that the WinSock DLL supports 2.0.*/
109         /* Note that if the DLL supports versions greater    */
110         /* than 2.0 in addition to 2.0, it will still return */
111         /* 2.0 in wVersion since that is the version we      */
112         /* requested.                                        */
113  
114         if ( LOBYTE( wsaData.wVersion ) != 2 ||
115                 HIBYTE( wsaData.wVersion ) != 0 )
116         {
117             /* Tell the user that we couldn't find a usable */
118             /* WinSock DLL.                                  */
119             WSACleanup( );
120             return LDAP_LOCAL_ERROR; 
121         }
122 }       /* The WinSock DLL is acceptable. Proceed. */
123
124 #elif HAVE_WINSOCK
125 {       WSADATA wsaData;
126         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
127             return LDAP_LOCAL_ERROR;
128         }
129 }
130 #endif
131
132         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
133             WSACleanup( );
134                 return( LDAP_NO_MEMORY );
135         }
136    
137         /* copy the global options */
138         AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
139
140         ld->ld_valid = LDAP_VALID_SESSION;
141
142         /* but not pointers to malloc'ed items */
143         ld->ld_options.ldo_sctrls = NULL;
144         ld->ld_options.ldo_cctrls = NULL;
145
146 #ifdef HAVE_CYRUS_SASL
147         ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
148                 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
149         ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
150                 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
151         ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
152                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
153         ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
154                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
155 #endif
156
157         ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
158
159         if ( ld->ld_options.ldo_defludp == NULL ) {
160                 LDAP_FREE( (char*)ld );
161             WSACleanup( );
162                 return LDAP_NO_MEMORY;
163         }
164
165         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
166                 ldap_free_urllist( ld->ld_options.ldo_defludp );
167                 LDAP_FREE( (char*) ld );
168             WSACleanup( );
169                 return LDAP_NO_MEMORY;
170         }
171
172         ld->ld_lberoptions = LBER_USE_DER;
173
174         ld->ld_sb = ber_sockbuf_alloc( );
175         if ( ld->ld_sb == NULL ) {
176                 ldap_free_urllist( ld->ld_options.ldo_defludp );
177                 LDAP_FREE( (char*) ld );
178                 WSACleanup( );
179                 return LDAP_NO_MEMORY;
180         }
181
182         *ldp = ld;
183         return LDAP_SUCCESS;
184 }
185
186 /*
187  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
188  * future communication is returned on success, NULL on failure.
189  * "host" may be a space-separated list of hosts or IP addresses
190  *
191  * Example:
192  *      LDAP    *ld;
193  *      ld = ldap_init( host, port );
194  */
195 LDAP *
196 ldap_init( LDAP_CONST char *defhost, int defport )
197 {
198         LDAP *ld;
199         int rc;
200
201         rc = ldap_create(&ld);
202         if ( rc != LDAP_SUCCESS )
203                 return NULL;
204
205         if (defport != 0)
206                 ld->ld_options.ldo_defport = defport;
207
208         if (defhost != NULL) {
209                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
210                 if ( rc != LDAP_SUCCESS ) {
211                         ldap_ld_free(ld, 1, NULL, NULL);
212                         return NULL;
213                 }
214         }
215
216         return( ld );
217 }
218
219
220 int
221 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
222 {
223         int rc;
224         LDAP *ld;
225
226         *ldp = NULL;
227         rc = ldap_create(&ld);
228         if ( rc != LDAP_SUCCESS )
229                 return rc;
230
231         if (url != NULL) {
232                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
233                 if ( rc != LDAP_SUCCESS ) {
234                         ldap_ld_free(ld, 1, NULL, NULL);
235                         return rc;
236                 }
237         }
238
239         *ldp = ld;
240         return LDAP_SUCCESS;
241 }
242
243 int
244 ldap_int_open_connection(
245         LDAP *ld,
246         LDAPConn *conn,
247         LDAPURLDesc *srv,
248         int async )
249 {
250         int rc = -1;
251 #ifdef HAVE_CYRUS_SASL
252         char *sasl_host = NULL;
253         int sasl_ssf = 0;
254 #endif
255         int port;
256         long addr;
257
258         Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
259
260         switch ( ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
261                 case LDAP_PROTO_TCP:
262                         port = htons( (short) srv->lud_port );
263
264                         addr = 0;
265                         if ( srv->lud_host == NULL || *srv->lud_host == 0 )
266                                 addr = htonl( INADDR_LOOPBACK );
267
268                         rc = ldap_connect_to_host( ld, conn->lconn_sb, 0,
269                                 srv->lud_host, addr, port, async );
270
271                         if ( rc == -1 ) return rc;
272                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
273                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
274
275 #ifdef HAVE_CYRUS_SASL
276                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
277 #endif
278                         break;
279                 case LDAP_PROTO_IPC:
280 #ifdef LDAP_PF_LOCAL
281                         /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
282                         rc = ldap_connect_to_path( ld, conn->lconn_sb, 0,
283                                 srv->lud_host, async );
284                         if ( rc == -1 ) return rc;
285                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
286                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
287
288 #ifdef HAVE_CYRUS_SASL
289                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
290                         sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
291 #endif
292                         break;
293 #endif /* LDAP_PF_LOCAL */
294                 default:
295                         return -1;
296                         break;
297         }
298
299 #ifdef HAVE_CYRUS_SASL
300         if( sasl_host != NULL ) {
301                 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
302         }
303 #endif
304
305         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
306                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
307 #ifdef LDAP_DEBUG
308         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
309                 INT_MAX, NULL );
310 #endif
311
312 #ifdef HAVE_TLS
313         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
314                 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
315         {
316                 rc = ldap_pvt_tls_start( ld, conn->lconn_sb,
317                         ld->ld_options.ldo_tls_ctx );
318
319                 if (rc != LDAP_SUCCESS) {
320                         return -1;
321                 }
322         }
323 #endif
324
325 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
326         if ( conn->lconn_krbinstance == NULL ) {
327                 char *c;
328                 conn->lconn_krbinstance = ldap_host_connected_to( conn->lconn_sb );
329
330                 if( conn->lconn_krbinstance != NULL && 
331                     ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
332                         *c = '\0';
333                 }
334         }
335 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
336
337         return( 0 );
338 }