]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
349df3808e516614dc38b024237b5834b30395e9
[openldap] / libraries / libldap / open.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 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
17 #include <ac/stdlib.h>
18
19 #include <ac/param.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23
24 #include "ldap-int.h"
25
26 int ldap_open_defconn( LDAP *ld )
27 {
28         if (( ld->ld_defconn = ldap_new_connection( ld, ld->ld_options.ldo_defludp, 1,1,0 )) == NULL )
29         {
30                 ld->ld_errno = LDAP_SERVER_DOWN;
31                 return -1;
32         }
33
34         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
35
36         return 0;
37 }
38
39 /*
40  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
41  * be used for future communication is returned on success, NULL on failure.
42  * "host" may be a space-separated list of hosts or IP addresses
43  *
44  * Example:
45  *      LDAP    *ld;
46  *      ld = ldap_open( hostname, port );
47  */
48
49 LDAP *
50 ldap_open( LDAP_CONST char *host, int port )
51 {
52         int rc;
53         LDAP            *ld;
54
55         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
56
57         if (( ld = ldap_init( host, port )) == NULL ) {
58                 return( NULL );
59         }
60
61         rc = ldap_open_defconn( ld );
62
63         if( rc < 0 ) {
64                 ldap_ld_free( ld, 0, NULL, NULL );
65                 return( NULL );
66         }
67
68         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
69                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
70
71         return( ld );
72 }
73
74
75
76 int
77 ldap_create( LDAP **ldp )
78 {
79         LDAP                    *ld;
80
81         *ldp = NULL;
82         if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
83                 ldap_int_initialize();
84         }
85
86         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
87
88 #ifdef HAVE_WINSOCK2
89 {       WORD wVersionRequested;
90         WSADATA wsaData;
91  
92         wVersionRequested = MAKEWORD( 2, 0 );
93         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
94                 /* Tell the user that we couldn't find a usable */
95                 /* WinSock DLL.                                  */
96                 return LDAP_LOCAL_ERROR;
97         }
98  
99         /* Confirm that the WinSock DLL supports 2.0.*/
100         /* Note that if the DLL supports versions greater    */
101         /* than 2.0 in addition to 2.0, it will still return */
102         /* 2.0 in wVersion since that is the version we      */
103         /* requested.                                        */
104  
105         if ( LOBYTE( wsaData.wVersion ) != 2 ||
106                 HIBYTE( wsaData.wVersion ) != 0 )
107         {
108             /* Tell the user that we couldn't find a usable */
109             /* WinSock DLL.                                  */
110             WSACleanup( );
111             return LDAP_LOCAL_ERROR; 
112         }
113 }       /* The WinSock DLL is acceptable. Proceed. */
114
115 #elif HAVE_WINSOCK
116 {       WSADATA wsaData;
117         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
118             return LDAP_LOCAL_ERROR;
119         }
120 }
121 #endif
122
123         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
124             WSACleanup( );
125                 return( LDAP_NO_MEMORY );
126         }
127    
128         /* copy the global options */
129         memcpy(&ld->ld_options, &ldap_int_global_options,
130                 sizeof(ld->ld_options));
131
132         ld->ld_valid = LDAP_VALID_SESSION;
133
134         /* but not pointers to malloc'ed items */
135         ld->ld_options.ldo_defludp = NULL;
136         ld->ld_options.ldo_sctrls = NULL;
137         ld->ld_options.ldo_cctrls = NULL;
138
139         ld->ld_options.ldo_defludp =
140                         ldap_url_duplist(ldap_int_global_options.ldo_defludp);
141
142         if ( ld->ld_options.ldo_defludp == NULL ) {
143                 LDAP_FREE( (char*)ld );
144             WSACleanup( );
145                 return LDAP_NO_MEMORY;
146         }
147
148         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
149                 ldap_free_urllist( ld->ld_options.ldo_defludp );
150                 LDAP_FREE( (char*) ld );
151             WSACleanup( );
152                 return LDAP_NO_MEMORY;
153         }
154
155         ld->ld_lberoptions = LBER_USE_DER;
156
157 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
158         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
159 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
160         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
161 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
162 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
163
164         ber_pvt_sb_init( &(ld->ld_sb) );
165
166         *ldp = ld;
167         return LDAP_SUCCESS;
168 }
169
170 /*
171  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
172  * future communication is returned on success, NULL on failure.
173  * "host" may be a space-separated list of hosts or IP addresses
174  *
175  * Example:
176  *      LDAP    *ld;
177  *      ld = ldap_open( host, port );
178  */
179 LDAP *
180 ldap_init( LDAP_CONST char *defhost, int defport )
181 {
182         LDAP *ld;
183         int rc;
184
185         rc = ldap_create(&ld);
186         if ( rc != LDAP_SUCCESS )
187                 return NULL;
188
189         if (defport != 0)
190                 ld->ld_options.ldo_defport = defport;
191
192         if (defhost != NULL) {
193                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
194                 if ( rc != LDAP_SUCCESS ) {
195                         ldap_ld_free(ld, 1, NULL, NULL);
196                         return NULL;
197                 }
198         }
199
200         return( ld );
201 }
202
203
204 int
205 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
206 {
207         int rc;
208         LDAP *ld;
209
210         *ldp = NULL;
211         rc = ldap_create(&ld);
212         if ( rc != LDAP_SUCCESS )
213                 return rc;
214
215         if (url != NULL) {
216                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
217                 if ( rc != LDAP_SUCCESS ) {
218                         ldap_ld_free(ld, 1, NULL, NULL);
219                         return rc;
220                 }
221         }
222
223         *ldp = ld;
224         return LDAP_SUCCESS;
225 }
226
227 int
228 ldap_start_tls ( LDAP *ld,
229                                 LDAPControl **serverctrls,
230                                 LDAPControl **clientctrls )
231 {
232 #ifdef HAVE_TLS
233         LDAPConn *lc;
234         int rc;
235         char *rspoid = NULL;
236         struct berval *rspdata = NULL;
237
238         if (ld->ld_conns == NULL) {
239                 rc = ldap_open_defconn( ld );
240                 if (rc != LDAP_SUCCESS)
241                         return(rc);
242         }
243
244         for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
245                 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
246                         return LDAP_OPERATIONS_ERROR;
247                 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
248                                                         NULL, serverctrls, clientctrls, &rspoid, &rspdata);
249                 if (rc != LDAP_SUCCESS)
250                         return rc;
251                 if (rspoid != NULL)
252                         LDAP_FREE(rspoid);
253                 if (rspdata != NULL)
254                         ber_bvfree(rspdata);
255                 rc = ldap_pvt_tls_start( lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
256                 if (rc != LDAP_SUCCESS)
257                         return rc;
258         }
259         return LDAP_SUCCESS;
260 #else
261         return LDAP_NOT_SUPPORTED;
262 #endif
263 }
264
265 int
266 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
267         char **krbinstancep, int async )
268 {
269         int rc = -1;
270         int port;
271 #ifdef HAVE_TLS
272         int tls;
273 #endif
274         long addr;
275
276         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
277
278         port = srv->lud_port;
279         if (port == 0)
280                 port = ld->ld_options.ldo_defport;
281         port = htons( (short) port );
282
283         addr = 0;
284         if ( srv->lud_host == NULL || *srv->lud_host == 0 )
285                 addr = htonl( INADDR_LOOPBACK );
286
287         switch ( srv->lud_protocol ) {
288                 case LDAP_PROTO_TCP:
289                 case LDAP_PROTO_UDP:
290                         rc = ldap_connect_to_host( ld, sb, srv->lud_host, addr, port, async );
291                         break;
292 #ifdef LDAP_PF_LOCAL
293                 case LDAP_PROTO_LOCAL:
294                         rc = ldap_connect_to_path( ld, sb, srv->lud_host, async );
295                         break;
296 #endif /* LDAP_PF_LOCAL */
297                 default:
298                         rc = -1;
299                         break;
300         }
301
302         if ( rc == -1 ) {
303                 return( rc );
304         }
305    
306         ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
307
308 #ifdef HAVE_TLS
309         tls = (srv->lud_properties & LDAP_URL_USE_SSL);
310
311         if ( tls != 0 ) {
312                 rc = ldap_pvt_tls_start( sb, ld->ld_options.ldo_tls_ctx );
313                 if (rc != LDAP_SUCCESS)
314                         return rc;
315         }
316 #endif
317
318         if ( krbinstancep != NULL ) {
319 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
320                 char *c;
321                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
322                     ( c = strchr( *krbinstancep, '.' )) != NULL ) {
323                         *c = '\0';
324                 }
325 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
326                 *krbinstancep = NULL;
327 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
328         }
329
330         return( 0 );
331 }