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