]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Add simple copyright notice.
[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         /* we'll assume we're talking version 2 for now */
52         ld->ld_version = LDAP_VERSION2;
53
54 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS
55         if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
56             NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
57             ldap_strdup( ld->ld_defhost )) == NULL )) {
58                 ldap_ld_free( ld, 0 );
59                 return( NULL );
60         }
61         srv->lsrv_port = ld->ld_defport;
62
63         if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
64                 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
65                 free( (char *)srv );
66                 ldap_ld_free( ld, 0 );
67                 return( NULL );
68         }
69         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
70
71 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
72         if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
73             ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
74                 ldap_ld_free( ld, 0 );
75                 return( NULL );
76         }
77 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_REFERRALS */
78
79         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
80                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
81
82         return( ld );
83 }
84
85
86 /*
87  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
88  * future communication is returned on success, NULL on failure.
89  * "host" may be a space-separated list of hosts or IP addresses
90  *
91  * Example:
92  *      LDAP    *ld;
93  *      ld = ldap_open( host, port );
94  */
95 LDAP *
96 ldap_init( char *defhost, int defport )
97 {
98         LDAP                    *ld;
99
100         if(!openldap_ldap_initialized) {
101                 openldap_ldap_initialize();
102         }
103
104         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
105
106 #ifdef HAVE_WINSOCK2
107 {       WORD wVersionRequested;
108         WSADATA wsaData;
109         int err;
110  
111         wVersionRequested = MAKEWORD( 2, 0 );
112  
113         err = WSAStartup( wVersionRequested, &wsaData );
114         if ( err != 0 ) {
115                 /* Tell the user that we couldn't find a usable */
116                 /* WinSock DLL.                                  */
117                 return NULL;
118         }
119  
120         /* Confirm that the WinSock DLL supports 2.0.*/
121         /* Note that if the DLL supports versions greater    */
122         /* than 2.0 in addition to 2.0, it will still return */
123         /* 2.0 in wVersion since that is the version we      */
124         /* requested.                                        */
125  
126         if ( LOBYTE( wsaData.wVersion ) != 2 ||
127                 HIBYTE( wsaData.wVersion ) != 0 )
128         {
129             /* Tell the user that we couldn't find a usable */
130             /* WinSock DLL.                                  */
131             WSACleanup( );
132             return NULL; 
133         }
134 }       /* The WinSock DLL is acceptable. Proceed. */
135
136 #elif HAVE_WINSOCK
137         if ( WSAStartup( 0x0101, &wsadata ) != 0 ) {
138             return( NULL );
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 = ldap_strdup( defhost );
157         } else {
158                 ld->ld_options.ldo_defhost = ldap_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 = ldap_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         return( ld );
199 }
200
201
202 int
203 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
204         char **krbinstancep, int async )
205 {
206         int                     rc = -1;
207         int                             port;
208         char                    *p, *q, *r;
209         char                    *curhost, hostname[ 2*MAXHOSTNAMELEN ];
210
211         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
212
213         defport = htons( (short) defport );
214
215         if ( host != NULL ) {
216                 for ( p = host; p != NULL && *p != '\0'; p = q ) {
217                         if (( q = strchr( p, ' ' )) != NULL ) {
218                                 strncpy( hostname, p, q - p );
219                                 hostname[ q - p ] = '\0';
220                                 curhost = hostname;
221                                 while ( *q == ' ' ) {
222                                     ++q;
223                                 }
224                         } else {
225                                 curhost = p;    /* avoid copy if possible */
226                                 q = NULL;
227                         }
228
229                         if (( r = strchr( curhost, ':' )) != NULL ) {
230                             if ( curhost != hostname ) {
231                                 strcpy( hostname, curhost );    /* now copy */
232                                 r = hostname + ( r - curhost );
233                                 curhost = hostname;
234                             }
235                             *r++ = '\0';
236                             port = htons( (short) atoi( r ) );
237                         } else {
238                             port = defport;   
239                         }
240
241                         if (( rc = ldap_connect_to_host( sb, curhost, 0L,
242                             port, async )) != -1 ) {
243                                 break;
244                         }
245                 }
246         } else {
247                 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
248                     defport, async );
249         }
250
251         if ( rc == -1 ) {
252                 return( rc );
253         }
254
255         if ( krbinstancep != NULL ) {
256 #ifdef HAVE_KERBEROS
257                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
258                     ( p = strchr( *krbinstancep, '.' )) != NULL ) {
259                         *p = '\0';
260                 }
261 #else /* HAVE_KERBEROS */
262                 krbinstancep = NULL;
263 #endif /* HAVE_KERBEROS */
264         }
265
266         return( 0 );
267 }