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