]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Update for new password codes for MSVC5
[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         /* we'll assume we're talking version 2 for now */
165         ld->ld_version = LDAP_VERSION2;
166
167         ber_pvt_sb_init( &(ld->ld_sb) );
168
169         *ldp = ld;
170         return LDAP_SUCCESS;
171 }
172
173 /*
174  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
175  * future communication is returned on success, NULL on failure.
176  * "host" may be a space-separated list of hosts or IP addresses
177  *
178  * Example:
179  *      LDAP    *ld;
180  *      ld = ldap_open( host, port );
181  */
182 LDAP *
183 ldap_init( LDAP_CONST char *defhost, int defport )
184 {
185         LDAP *ld;
186         int rc;
187
188         rc = ldap_create(&ld);
189         if ( rc != LDAP_SUCCESS )
190                 return NULL;
191
192         if (defport != 0)
193                 ld->ld_options.ldo_defport = defport;
194
195         if (defhost != NULL) {
196                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
197                 if ( rc != LDAP_SUCCESS ) {
198                         ldap_ld_free(ld, 1, NULL, NULL);
199                         return NULL;
200                 }
201         }
202
203         return( ld );
204 }
205
206
207 int
208 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
209 {
210         int rc;
211         LDAP *ld;
212
213         *ldp = NULL;
214         rc = ldap_create(&ld);
215         if ( rc != LDAP_SUCCESS )
216                 return rc;
217
218         if (url != NULL) {
219                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
220                 if ( rc != LDAP_SUCCESS ) {
221                         ldap_ld_free(ld, 1, NULL, NULL);
222                         return rc;
223                 }
224         }
225
226         *ldp = ld;
227         return LDAP_SUCCESS;
228 }
229
230 int
231 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
232         char **krbinstancep, int async )
233 {
234         int                     rc = -1;
235         int port;
236         long addr;
237
238         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
239
240         port = srv->lud_port;
241         if (port == 0)
242                 port = ld->ld_options.ldo_defport;
243         port = htons( (short) port );
244
245         addr = 0;
246         if ( srv->lud_host == NULL )
247                 addr = htonl( INADDR_LOOPBACK );
248
249         rc = ldap_connect_to_host( ld, sb, srv->lud_host, addr, port, async );
250         if ( rc == -1 ) {
251                 return( rc );
252         }
253    
254         ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
255
256 #ifdef HAVE_TLS
257         if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD 
258                 || srv->lud_ldaps != 0 )
259         {
260                 /*
261                  * Fortunately, the lib uses blocking io...
262                  */
263                 if ( ldap_pvt_tls_connect( sb, ld->ld_options.ldo_tls_ctx ) < 
264                      0 ) {
265                         return -1;
266                 }
267                 /* FIXME: hostname of server must be compared with name in
268                  * certificate....
269                  */
270         }
271 #endif
272         if ( krbinstancep != NULL ) {
273 #ifdef HAVE_KERBEROS
274                 char *c;
275                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
276                     ( c = strchr( *krbinstancep, '.' )) != NULL ) {
277                         *c = '\0';
278                 }
279 #else /* HAVE_KERBEROS */
280                 krbinstancep = NULL;
281 #endif /* HAVE_KERBEROS */
282         }
283
284         return( 0 );
285 }