]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Remove lint.
[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 ldap_start_tls ( LDAP *ld,
232                                 LDAPControl **serverctrls,
233                                 LDAPControl **clientctrls )
234 {
235 #ifdef HAVE_TLS
236         LDAPConn *lc;
237         int rc;
238         char *rspoid = NULL;
239         struct berval *rspdata = NULL;
240
241         if (ld->ld_conns == NULL) {
242                 rc = ldap_open_defconn( ld );
243                 if (rc != LDAP_SUCCESS)
244                         return(rc);
245         }
246
247         for (lc = ld->ld_conns; lc != NULL; lc = lc->lconn_next) {
248                 if (ldap_pvt_tls_inplace(lc->lconn_sb) != 0)
249                         return LDAP_OPERATIONS_ERROR;
250                 rc = ldap_extended_operation_s(ld, LDAP_EXOP_START_TLS,
251                                                         NULL, serverctrls, clientctrls, &rspoid, &rspdata);
252                 if (rc != LDAP_SUCCESS)
253                         return rc;
254                 if (rspoid != NULL)
255                         LDAP_FREE(rspoid);
256                 if (rspdata != NULL)
257                         ber_bvfree(rspdata);
258                 rc = ldap_pvt_tls_start( lc->lconn_sb, ld->ld_options.ldo_tls_ctx );
259                 if (rc != LDAP_SUCCESS)
260                         return rc;
261         }
262         return LDAP_SUCCESS;
263 #else
264         return LDAP_NOT_SUPPORTED;
265 #endif
266 }
267
268 int
269 open_ldap_connection( LDAP *ld, Sockbuf *sb, LDAPURLDesc *srv,
270         char **krbinstancep, int async )
271 {
272         int rc = -1;
273         int port;
274 #ifdef HAVE_TLS
275         int tls;
276 #endif
277         long addr;
278
279         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
280
281         port = srv->lud_port;
282         if (port == 0)
283                 port = ld->ld_options.ldo_defport;
284         port = htons( (short) port );
285
286         addr = 0;
287         if ( srv->lud_host == NULL || *srv->lud_host == 0 )
288                 addr = htonl( INADDR_LOOPBACK );
289
290         rc = ldap_connect_to_host( ld, sb, srv->lud_host, addr, port, async );
291         if ( rc == -1 ) {
292                 return( rc );
293         }
294    
295         ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
296
297 #ifdef HAVE_TLS
298         tls = srv->lud_ldaps;
299         if (tls == -1)
300                 tls = ld->ld_options.ldo_tls_mode;
301         if ( tls != 0 ) {
302                 rc = ldap_pvt_tls_start( sb, ld->ld_options.ldo_tls_ctx );
303                 if (rc != LDAP_SUCCESS)
304                         return rc;
305         }
306 #endif
307
308         if ( krbinstancep != NULL ) {
309 #ifdef HAVE_KERBEROS
310                 char *c;
311                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
312                     ( c = strchr( *krbinstancep, '.' )) != NULL ) {
313                         *c = '\0';
314                 }
315 #else /* HAVE_KERBEROS */
316                 *krbinstancep = NULL;
317 #endif /* HAVE_KERBEROS */
318         }
319
320         return( 0 );
321 }