]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Fix ldap_rename parameter order (ITS#387)
[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         LDAPServer      *srv;
29
30         if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) ==
31             NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
32             LDAP_STRDUP( ld->ld_defhost )) == NULL ))
33         {
34                 if( srv != NULL ) LDAP_FREE( (char*) srv );
35                 ld->ld_errno = LDAP_NO_MEMORY;
36                 return -1;
37         }
38
39         srv->lsrv_port = ld->ld_defport;
40
41         if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL )
42         {
43                 if ( ld->ld_defhost != NULL ) LDAP_FREE( srv->lsrv_host );
44                 LDAP_FREE( (char *)srv );
45                 ld->ld_errno = LDAP_SERVER_DOWN;
46                 return -1;
47         }
48
49         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
50
51         return 0;
52 }
53
54 /*
55  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
56  * be used for future communication is returned on success, NULL on failure.
57  * "host" may be a space-separated list of hosts or IP addresses
58  *
59  * Example:
60  *      LDAP    *ld;
61  *      ld = ldap_open( hostname, port );
62  */
63
64 LDAP *
65 ldap_open( LDAP_CONST char *host, int port )
66 {
67         int rc;
68         LDAP            *ld;
69
70         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
71
72         if (( ld = ldap_init( host, port )) == NULL ) {
73                 return( NULL );
74         }
75
76         rc = ldap_open_defconn( ld );
77
78         if( rc < 0 ) {
79                 ldap_ld_free( ld, 0, NULL, NULL );
80                 return( NULL );
81         }
82
83         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
84                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
85
86         return( ld );
87 }
88
89
90 /*
91  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
92  * future communication is returned on success, NULL on failure.
93  * "host" may be a space-separated list of hosts or IP addresses
94  *
95  * Example:
96  *      LDAP    *ld;
97  *      ld = ldap_open( host, port );
98  */
99 LDAP *
100 ldap_init( LDAP_CONST char *defhost, int defport )
101 {
102         LDAP                    *ld;
103
104         if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
105                 ldap_int_initialize();
106         }
107
108         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
109
110 #ifdef HAVE_WINSOCK2
111 {       WORD wVersionRequested;
112         WSADATA wsaData;
113         int err;
114  
115         wVersionRequested = MAKEWORD( 2, 0 );
116  
117         err = WSAStartup( wVersionRequested, &wsaData );
118         if ( err != 0 ) {
119                 /* Tell the user that we couldn't find a usable */
120                 /* WinSock DLL.                                  */
121                 return NULL;
122         }
123  
124         /* Confirm that the WinSock DLL supports 2.0.*/
125         /* Note that if the DLL supports versions greater    */
126         /* than 2.0 in addition to 2.0, it will still return */
127         /* 2.0 in wVersion since that is the version we      */
128         /* requested.                                        */
129  
130         if ( LOBYTE( wsaData.wVersion ) != 2 ||
131                 HIBYTE( wsaData.wVersion ) != 0 )
132         {
133             /* Tell the user that we couldn't find a usable */
134             /* WinSock DLL.                                  */
135             WSACleanup( );
136             return NULL; 
137         }
138 }       /* The WinSock DLL is acceptable. Proceed. */
139
140 #elif HAVE_WINSOCK
141 {       WSADATA wsaData;
142         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
143             return( NULL );
144         }
145 }
146 #endif
147
148         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
149             WSACleanup( );
150                 return( NULL );
151         }
152    
153         /* copy the global options */
154         memcpy(&ld->ld_options, &ldap_int_global_options,
155                 sizeof(ld->ld_options));
156
157         ld->ld_valid = LDAP_VALID_SESSION;
158
159         /* but not pointers to malloc'ed items */
160         ld->ld_options.ldo_defbase = NULL;
161         ld->ld_options.ldo_defhost = NULL;
162         ld->ld_options.ldo_sctrls = NULL;
163         ld->ld_options.ldo_cctrls = NULL;
164
165         if ( defhost != NULL ) {
166                 ld->ld_options.ldo_defhost = LDAP_STRDUP( defhost );
167         } else {
168                 ld->ld_options.ldo_defhost = LDAP_STRDUP(
169                         ldap_int_global_options.ldo_defhost);
170         }
171
172         if ( ld->ld_options.ldo_defhost == NULL ) {
173                 LDAP_FREE( (char*)ld );
174             WSACleanup( );
175                 return( NULL );
176         }
177
178         if ( ldap_int_global_options.ldo_defbase != NULL ) {
179                 ld->ld_options.ldo_defbase = LDAP_STRDUP(
180                         ldap_int_global_options.ldo_defbase);
181         }
182
183         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
184                 LDAP_FREE( (char*) ld->ld_options.ldo_defhost );
185                 if ( ld->ld_options.ldo_defbase == NULL ) {
186                         LDAP_FREE( (char*) ld->ld_options.ldo_defbase );
187                 }
188                 LDAP_FREE( (char*) ld );
189             WSACleanup( );
190                 return( NULL );
191         }
192
193         if(defport != 0) {
194                 ld->ld_defport = defport;
195         }
196
197         ld->ld_lberoptions = LBER_USE_DER;
198
199 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
200         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
201 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
202         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
203 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
204 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
205
206         /* we'll assume we're talking version 2 for now */
207         ld->ld_version = LDAP_VERSION2;
208
209         ber_pvt_sb_init( &(ld->ld_sb) );
210
211         return( ld );
212 }
213
214
215 int
216 open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport,
217         char **krbinstancep, int async )
218 {
219         int                     rc = -1;
220         int                             port;
221         const char              *p, *q;
222         char                    *r, *curhost, hostname[ 2*MAXHOSTNAMELEN ];
223
224         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
225
226         defport = htons( (short) defport );
227
228         if ( host != NULL ) {
229                 for ( p = host; p != NULL && *p != '\0'; p = q ) {
230                         if (( q = strchr( p, ' ' )) != NULL ) {
231                                 strncpy( hostname, p, q - p );
232                                 hostname[ q - p ] = '\0';
233                                 curhost = hostname;
234                                 while ( *q == ' ' ) {
235                                     ++q;
236                                 }
237                         } else {
238                                 curhost = (char *) p;   /* avoid copy if possible */
239                                 q = NULL;
240                         }
241
242                         if (( r = strchr( curhost, ':' )) != NULL ) {
243                             if ( curhost != hostname ) {
244                                 strcpy( hostname, curhost );    /* now copy */
245                                 r = hostname + ( r - curhost );
246                                 curhost = hostname;
247                             }
248                             *r++ = '\0';
249                             port = htons( (short) atoi( r ) );
250                         } else {
251                             port = defport;   
252                         }
253
254                         if (( rc = ldap_connect_to_host( ld, sb, curhost, 0L,
255                             port, async )) != -1 ) {
256                                 break;
257                         }
258                 }
259         } else {
260                 rc = ldap_connect_to_host( ld, sb, 0, htonl( INADDR_LOOPBACK ),
261                     defport, async );
262         }
263
264         if ( rc == -1 ) {
265                 return( rc );
266         }
267    
268         ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
269
270 #ifdef HAVE_TLS
271         if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ) {
272                 /*
273                  * Fortunately, the lib uses blocking io...
274                  */
275                 if ( ldap_pvt_tls_connect( sb, ld->ld_options.ldo_tls_ctx ) < 
276                      0 ) {
277                         return -1;
278                 }
279                 /* FIXME: hostname of server must be compared with name in
280                  * certificate....
281                  */
282         }
283 #endif
284         if ( krbinstancep != NULL ) {
285 #ifdef HAVE_KERBEROS
286                 char *c;
287                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
288                     ( c = strchr( *krbinstancep, '.' )) != NULL ) {
289                         *c = '\0';
290                 }
291 #else /* HAVE_KERBEROS */
292                 krbinstancep = NULL;
293 #endif /* HAVE_KERBEROS */
294         }
295
296         return( 0 );
297 }