]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
e42c95cd0ee650c825359a8cd9ab2c6f5d08c4a0
[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 strings */
153         ld->ld_options.ldo_defbase = NULL;
154         ld->ld_options.ldo_defhost = NULL;
155
156         if ( defhost != NULL ) {
157                 ld->ld_options.ldo_defhost = strdup( defhost );
158         } else {
159                 ld->ld_options.ldo_defhost = strdup(
160                         openldap_ldap_global_options.ldo_defhost);
161         }
162
163         if ( ld->ld_options.ldo_defhost == NULL ) {
164                 free( (char*)ld );
165             WSACleanup( );
166                 return( NULL );
167         }
168
169         if ( openldap_ldap_global_options.ldo_defbase != NULL ) {
170                 ld->ld_options.ldo_defbase = strdup(
171                         openldap_ldap_global_options.ldo_defbase);
172         }
173
174 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
175         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
176                 free( (char*) ld->ld_options.ldo_defhost );
177                 if ( ld->ld_options.ldo_defbase == NULL ) {
178                         free( (char*) ld->ld_options.ldo_defbase );
179                 }
180                 free( (char*) ld );
181             WSACleanup( );
182                 return( NULL );
183         }
184 #endif
185
186         if(defport != 0) {
187                 ld->ld_defport = defport;
188         }
189
190         ld->ld_lberoptions = LBER_USE_DER;
191
192 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
193         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
194 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
195         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
196 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
197 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
198
199         /* we'll assume we're talking version 2 for now */
200         ld->ld_version = LDAP_VERSION2;
201
202         ber_pvt_sb_init( &(ld->ld_sb) );
203
204         return( ld );
205 }
206
207
208 int
209 open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport,
210         char **krbinstancep, int async )
211 {
212         int                     rc = -1;
213         int                             port;
214         const char              *p, *q;
215         char                    *r, *curhost, hostname[ 2*MAXHOSTNAMELEN ];
216
217         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
218
219         defport = htons( (short) defport );
220
221         if ( host != NULL ) {
222                 for ( p = host; p != NULL && *p != '\0'; p = q ) {
223                         if (( q = strchr( p, ' ' )) != NULL ) {
224                                 strncpy( hostname, p, q - p );
225                                 hostname[ q - p ] = '\0';
226                                 curhost = hostname;
227                                 while ( *q == ' ' ) {
228                                     ++q;
229                                 }
230                         } else {
231                                 curhost = (char *) p;   /* avoid copy if possible */
232                                 q = NULL;
233                         }
234
235                         if (( r = strchr( curhost, ':' )) != NULL ) {
236                             if ( curhost != hostname ) {
237                                 strcpy( hostname, curhost );    /* now copy */
238                                 r = hostname + ( r - curhost );
239                                 curhost = hostname;
240                             }
241                             *r++ = '\0';
242                             port = htons( (short) atoi( r ) );
243                         } else {
244                             port = defport;   
245                         }
246
247                         if (( rc = ldap_connect_to_host( sb, curhost, 0L,
248                             port, async )) != -1 ) {
249                                 break;
250                         }
251                 }
252         } else {
253                 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
254                     defport, async );
255         }
256
257         if ( rc == -1 ) {
258                 return( rc );
259         }
260    
261         ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
262
263         if ( krbinstancep != NULL ) {
264 #ifdef HAVE_KERBEROS
265                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
266                     ( p = strchr( *krbinstancep, '.' )) != NULL ) {
267                         *p = '\0';
268                 }
269 #else /* HAVE_KERBEROS */
270                 krbinstancep = NULL;
271 #endif /* HAVE_KERBEROS */
272         }
273
274         return( 0 );
275 }