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