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