]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
Split out ldapoptions from struct ldap to facilate global options.
[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         if(!openldap_ldap_initialized) {
103                 openldap_ldap_initialize();
104         }
105
106         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
107
108 #ifdef HAVE_WINSOCK2
109 {       WORD wVersionRequested;
110         WSADATA wsaData;
111         int err;
112  
113         wVersionRequested = MAKEWORD( 2, 0 );
114  
115         err = WSAStartup( wVersionRequested, &wsaData );
116         if ( err != 0 ) {
117                 /* Tell the user that we couldn't find a usable */
118                 /* WinSock DLL.                                  */
119                 return NULL;
120         }
121  
122         /* Confirm that the WinSock DLL supports 2.0.*/
123         /* Note that if the DLL supports versions greater    */
124         /* than 2.0 in addition to 2.0, it will still return */
125         /* 2.0 in wVersion since that is the version we      */
126         /* requested.                                        */
127  
128         if ( LOBYTE( wsaData.wVersion ) != 2 ||
129                 HIBYTE( wsaData.wVersion ) != 0 )
130         {
131             /* Tell the user that we couldn't find a usable */
132             /* WinSock DLL.                                  */
133             WSACleanup( );
134             return NULL; 
135         }
136 }       /* The WinSock DLL is acceptable. Proceed. */
137
138 #elif HAVE_WINSOCK
139         if ( WSAStartup( 0x0101, &wsadata ) != 0 ) {
140             return( NULL );
141         }
142 #endif
143
144         if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
145             WSACleanup( );
146                 return( NULL );
147         }
148
149 #ifdef LDAP_REFERRALS
150         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
151                 free( (char*)ld );
152             WSACleanup( );
153                 return( NULL );
154         }
155
156         LDAP_BOOL_ZERO(&ld->ld_options);
157         LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
158 #else
159         LDAP_BOOL_ZERO(&ld->ld_options);
160 #endif
161
162         if ( defhost != NULL &&
163             ( ld->ld_defhost = strdup( defhost )) == NULL ) {
164 #ifdef LDAP_REFERRALS
165                 ldap_free_select_info( ld->ld_selectinfo );
166 #endif /* LDAP_REFERRALS */
167                 free( (char*)ld );
168             WSACleanup( );
169                 return( NULL );
170         }
171
172
173         ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
174         ld->ld_version = LDAP_VERSION;
175         ld->ld_lberoptions = LBER_USE_DER;
176         ld->ld_options.ldo_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
177
178 #ifdef LDAP_REFERRALS
179         LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_REFERRALS);
180 #endif /* LDAP_REFERRALS */
181
182 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
183         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
184 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
185         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
186 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
187 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
188
189         return( ld );
190 }
191
192
193 int
194 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
195         char **krbinstancep, int async )
196 {
197         int                     rc = -1;
198         int                             port;
199         char                    *p, *q, *r;
200         char                    *curhost, hostname[ 2*MAXHOSTNAMELEN ];
201
202         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
203
204         defport = htons( (short) defport );
205
206         if ( host != NULL ) {
207                 for ( p = host; p != NULL && *p != '\0'; p = q ) {
208                         if (( q = strchr( p, ' ' )) != NULL ) {
209                                 strncpy( hostname, p, q - p );
210                                 hostname[ q - p ] = '\0';
211                                 curhost = hostname;
212                                 while ( *q == ' ' ) {
213                                     ++q;
214                                 }
215                         } else {
216                                 curhost = p;    /* avoid copy if possible */
217                                 q = NULL;
218                         }
219
220                         if (( r = strchr( curhost, ':' )) != NULL ) {
221                             if ( curhost != hostname ) {
222                                 strcpy( hostname, curhost );    /* now copy */
223                                 r = hostname + ( r - curhost );
224                                 curhost = hostname;
225                             }
226                             *r++ = '\0';
227                             port = htons( (short) atoi( r ) );
228                         } else {
229                             port = defport;   
230                         }
231
232                         if (( rc = ldap_connect_to_host( sb, curhost, 0L,
233                             port, async )) != -1 ) {
234                                 break;
235                         }
236                 }
237         } else {
238                 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
239                     defport, async );
240         }
241
242         if ( rc == -1 ) {
243                 return( rc );
244         }
245
246         if ( krbinstancep != NULL ) {
247 #ifdef HAVE_KERBEROS
248                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
249                     ( p = strchr( *krbinstancep, '.' )) != NULL ) {
250                         *p = '\0';
251                 }
252 #else /* HAVE_KERBEROS */
253                 krbinstancep = NULL;
254 #endif /* HAVE_KERBEROS */
255         }
256
257         return( 0 );
258 }