]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
replace with autoconf version
[openldap] / libraries / libldap / open.c
1 /*
2  *  Copyright (c) 1995 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  open.c
6  */
7
8 #include "portable.h"
9
10 #ifndef lint 
11 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
12 #endif
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 "lber.h"
26 #include "ldap.h"
27 #include "ldap-int.h"
28
29 #ifdef LDAP_DEBUG
30 int     ldap_debug;
31 #endif
32
33
34 /*
35  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
36  * be used for future communication is returned on success, NULL on failure.
37  * "host" may be a space-separated list of hosts or IP addresses
38  *
39  * Example:
40  *      LDAP    *ld;
41  *      ld = ldap_open( hostname, port );
42  */
43
44 LDAP *
45 ldap_open( char *host, int port )
46 {
47         LDAP            *ld;
48 #ifdef LDAP_REFERRALS
49         LDAPServer      *srv;
50 #endif /* LDAP_REFERRALS */
51
52         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
53
54         if (( ld = ldap_init( host, port )) == NULL ) {
55                 return( NULL );
56         }
57
58 #ifdef LDAP_REFERRALS
59         if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
60             NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
61             strdup( ld->ld_defhost )) == NULL )) {
62                 ldap_ld_free( ld, 0 );
63                 return( NULL );
64         }
65         srv->lsrv_port = ld->ld_defport;
66
67         if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
68                 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
69                 free( (char *)srv );
70                 ldap_ld_free( ld, 0 );
71                 return( NULL );
72         }
73         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
74
75 #else /* LDAP_REFERRALS */
76         if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
77             ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
78                 ldap_ld_free( ld, 0 );
79                 return( NULL );
80         }
81 #endif /* LDAP_REFERRALS */
82
83         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
84                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
85
86         return( ld );
87 }
88
89
90 /*
91  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
92  * future communication is returned on success, NULL on failure.
93  * "defhost" may be a space-separated list of hosts or IP addresses
94  *
95  * Example:
96  *      LDAP    *ld;
97  *      ld = ldap_open( default_hostname, default_port );
98  */
99 LDAP *
100 ldap_init( char *defhost, int defport )
101 {
102         LDAP                    *ld;
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 #ifdef LDAP_REFERRALS
148         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
149                 free( (char*)ld );
150             WSACleanup( );
151                 return( NULL );
152         }
153         ld->ld_options = LDAP_OPT_REFERRALS;
154 #endif /* LDAP_REFERRALS */
155
156         if ( defhost != NULL &&
157             ( ld->ld_defhost = strdup( defhost )) == NULL ) {
158 #ifdef LDAP_REFERRALS
159                 ldap_free_select_info( ld->ld_selectinfo );
160 #endif /* LDAP_REFERRALS */
161                 free( (char*)ld );
162             WSACleanup( );
163                 return( NULL );
164         }
165
166
167         ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
168         ld->ld_version = LDAP_VERSION;
169         ld->ld_lberoptions = LBER_USE_DER;
170         ld->ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
171
172 #ifdef LDAP_REFERRALS
173         ld->ld_options |= LDAP_OPT_REFERRALS;
174 #endif /* LDAP_REFERRALS */
175
176 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
177         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
178 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
179         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
180 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
181 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
182
183         return( ld );
184 }
185
186
187 int
188 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
189         char **krbinstancep, int async )
190 {
191         int                     rc = -1;
192         int                             port;
193         char                    *p, *q, *r;
194         char                    *curhost, hostname[ 2*MAXHOSTNAMELEN ];
195
196         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
197
198         defport = htons( (short) defport );
199
200         if ( host != NULL ) {
201                 for ( p = host; p != NULL && *p != '\0'; p = q ) {
202                         if (( q = strchr( p, ' ' )) != NULL ) {
203                                 strncpy( hostname, p, q - p );
204                                 hostname[ q - p ] = '\0';
205                                 curhost = hostname;
206                                 while ( *q == ' ' ) {
207                                     ++q;
208                                 }
209                         } else {
210                                 curhost = p;    /* avoid copy if possible */
211                                 q = NULL;
212                         }
213
214                         if (( r = strchr( curhost, ':' )) != NULL ) {
215                             if ( curhost != hostname ) {
216                                 strcpy( hostname, curhost );    /* now copy */
217                                 r = hostname + ( r - curhost );
218                                 curhost = hostname;
219                             }
220                             *r++ = '\0';
221                             port = htons( (short) atoi( r ) );
222                         } else {
223                             port = defport;   
224                         }
225
226                         if (( rc = ldap_connect_to_host( sb, curhost, 0L,
227                             port, async )) != -1 ) {
228                                 break;
229                         }
230                 }
231         } else {
232                 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
233                     defport, async );
234         }
235
236         if ( rc == -1 ) {
237                 return( rc );
238         }
239
240         if ( krbinstancep != NULL ) {
241 #ifdef HAVE_KERBEROS
242                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
243                     ( p = strchr( *krbinstancep, '.' )) != NULL ) {
244                         *p = '\0';
245                 }
246 #else /* HAVE_KERBEROS */
247                 krbinstancep = NULL;
248 #endif /* HAVE_KERBEROS */
249         }
250
251         return( 0 );
252 }