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