]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Changes for URI spport: New routines ldap_initialize and ldap_create; LDAPURLDesc...
[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  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
77  * future communication is returned on success, NULL on failure.
78  * "host" may be a space-separated list of hosts or IP addresses
79  *
80  * Example:
81  *      LDAP    *ld;
82  *      ld = ldap_open( host, port );
83  */
84 LDAP *
85 ldap_init( LDAP_CONST char *defhost, int defport )
86 {
87         LDAP *ld;
88         int rc;
89
90         rc = ldap_create(&ld);
91         if ( rc != LDAP_SUCCESS )
92                 return NULL;
93
94         if (defport != 0)
95                 ld->ld_options.ldo_defport = defport;
96
97         if (defhost != NULL) {
98                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
99                 if ( rc != LDAP_SUCCESS ) {
100                         ldap_ld_free(ld, 1, NULL, NULL);
101                         return NULL;
102                 }
103         }
104
105         return( ld );
106 }
107
108 int
109 ldap_create( LDAP **ldp )
110 {
111         LDAP                    *ld;
112
113         *ldp = NULL;
114         if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
115                 ldap_int_initialize();
116         }
117
118         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
119
120 #ifdef HAVE_WINSOCK2
121 {       WORD wVersionRequested;
122         WSADATA wsaData;
123  
124         wVersionRequested = MAKEWORD( 2, 0 );
125         if ( WSAStartup( wVersionRequested, &wsaData ) != 0 ) {
126                 /* Tell the user that we couldn't find a usable */
127                 /* WinSock DLL.                                  */
128                 return LDAP_LOCAL_ERROR;
129         }
130  
131         /* Confirm that the WinSock DLL supports 2.0.*/
132         /* Note that if the DLL supports versions greater    */
133         /* than 2.0 in addition to 2.0, it will still return */
134         /* 2.0 in wVersion since that is the version we      */
135         /* requested.                                        */
136  
137         if ( LOBYTE( wsaData.wVersion ) != 2 ||
138                 HIBYTE( wsaData.wVersion ) != 0 )
139         {
140             /* Tell the user that we couldn't find a usable */
141             /* WinSock DLL.                                  */
142             WSACleanup( );
143             return LDAP_LOCAL_ERROR; 
144         }
145 }       /* The WinSock DLL is acceptable. Proceed. */
146
147 #elif HAVE_WINSOCK
148 {       WSADATA wsaData;
149         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
150             return LDAP_LOCAL_ERROR;
151         }
152 }
153 #endif
154
155         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
156             WSACleanup( );
157                 return( LDAP_NO_MEMORY );
158         }
159    
160         /* copy the global options */
161         memcpy(&ld->ld_options, &ldap_int_global_options,
162                 sizeof(ld->ld_options));
163
164         ld->ld_valid = LDAP_VALID_SESSION;
165
166         /* but not pointers to malloc'ed items */
167         ld->ld_options.ldo_defludp = NULL;
168         ld->ld_options.ldo_sctrls = NULL;
169         ld->ld_options.ldo_cctrls = NULL;
170
171         ld->ld_options.ldo_defludp =
172                         ldap_url_duplist(ldap_int_global_options.ldo_defludp);
173
174         if ( ld->ld_options.ldo_defludp == NULL ) {
175                 LDAP_FREE( (char*)ld );
176             WSACleanup( );
177                 return LDAP_NO_MEMORY;
178         }
179
180         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
181                 ldap_free_urllist( ld->ld_options.ldo_defludp );
182                 LDAP_FREE( (char*) ld );
183             WSACleanup( );
184                 return LDAP_NO_MEMORY;
185         }
186
187         ld->ld_lberoptions = LBER_USE_DER;
188
189 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
190         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
191 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
192         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
193 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
194 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
195
196         /* we'll assume we're talking version 2 for now */
197         ld->ld_version = LDAP_VERSION2;
198
199         ber_pvt_sb_init( &(ld->ld_sb) );
200
201         *ldp = ld;
202         return LDAP_SUCCESS;
203 }
204
205 int
206 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
207 {
208         int rc;
209         LDAP *ld;
210
211         *ldp = NULL;
212         rc = ldap_create(&ld);
213         if ( rc != LDAP_SUCCESS )
214                 return rc;
215
216         if (url != NULL) {
217                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
218                 if ( rc != LDAP_SUCCESS ) {
219                         ldap_ld_free(ld, 1, NULL, NULL);
220                         return rc;
221                 }
222         }
223
224         *ldp = ld;
225         return LDAP_SUCCESS;
226 }
227
228 int
229 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
230         char **krbinstancep, int async )
231 {
232         int                     rc = -1;
233         int port;
234         long addr;
235
236         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
237
238         port = srv->lud_port;
239         if (port == 0)
240                 port = ld->ld_options.ldo_defport;
241         port = htons( (short) port );
242
243         addr = 0;
244         if ( srv->lud_host == NULL )
245                 addr = htonl( INADDR_LOOPBACK );
246
247         rc = ldap_connect_to_host( ld, sb, srv->lud_host, addr, port, async );
248         if ( rc == -1 ) {
249                 return( rc );
250         }
251    
252         ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
253
254 #ifdef HAVE_TLS
255         if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD 
256                 || srv->lud_ldaps != 0 )
257         {
258                 /*
259                  * Fortunately, the lib uses blocking io...
260                  */
261                 if ( ldap_pvt_tls_connect( sb, ld->ld_options.ldo_tls_ctx ) < 
262                      0 ) {
263                         return -1;
264                 }
265                 /* FIXME: hostname of server must be compared with name in
266                  * certificate....
267                  */
268         }
269 #endif
270         if ( krbinstancep != NULL ) {
271 #ifdef HAVE_KERBEROS
272                 char *c;
273                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
274                     ( c = strchr( *krbinstancep, '.' )) != NULL ) {
275                         *c = '\0';
276                 }
277 #else /* HAVE_KERBEROS */
278                 krbinstancep = NULL;
279 #endif /* HAVE_KERBEROS */
280         }
281
282         return( 0 );
283 }