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