]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
3d9a7c630cd614ca36209104f5a24d8291d42160
[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 #ifndef lint 
9 static char copyright[] = "@(#) Copyright (c) 1995 Regents of the University of Michigan.\nAll rights reserved.\n";
10 #endif
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15
16 #ifdef MACOS
17 #include "macos.h"
18 #endif /* MACOS */
19
20 #if defined( DOS ) || defined( _WIN32 )
21 #include "msdos.h"
22 #endif /* DOS */
23
24 #if !defined(MACOS) && !defined(DOS) && !defined( _WIN32 )
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #ifndef VMS
29 #include <sys/param.h>
30 #endif
31 #include <netinet/in.h>
32 #endif
33 #include "lber.h"
34 #include "ldap.h"
35 #include "ldap-int.h"
36
37 #ifdef LDAP_DEBUG
38 int     ldap_debug;
39 #endif
40
41 #ifndef INADDR_LOOPBACK
42 #define INADDR_LOOPBACK ((unsigned long) 0x7f000001)
43 #endif
44
45 #ifndef MAXHOSTNAMELEN
46 #define MAXHOSTNAMELEN  64
47 #endif
48
49
50 /*
51  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
52  * be used for future communication is returned on success, NULL on failure.
53  * "host" may be a space-separated list of hosts or IP addresses
54  *
55  * Example:
56  *      LDAP    *ld;
57  *      ld = ldap_open( hostname, port );
58  */
59
60 LDAP *
61 ldap_open( char *host, int port )
62 {
63         LDAP            *ld;
64 #ifdef LDAP_REFERRALS
65         LDAPServer      *srv;
66 #endif /* LDAP_REFERRALS */
67 #ifdef HAVE_WINSOCK_2
68         WORD wVersionRequested;
69         WSADATA wsaData;
70 #endif
71
72         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
73
74 #ifdef HAVE_WINSOCK_2
75         wVersionRequested = MAKEWORD( 2, 0 );
76         if ( WSAStartup( wVersionRequested, &wsadata ) != 0 ) {
77                 return( NULL );
78         }
79 #endif
80
81         if (( ld = ldap_init( host, port )) == NULL ) {
82                 return( NULL );
83         }
84
85 #ifdef LDAP_REFERRALS
86         if (( srv = (LDAPServer *)calloc( 1, sizeof( LDAPServer ))) ==
87             NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
88             strdup( ld->ld_defhost )) == NULL )) {
89                 ldap_ld_free( ld, 0 );
90 #ifdef HAVE_WINSOCK_2
91                 WSACleanup();
92 #endif
93                 return( NULL );
94         }
95         srv->lsrv_port = ld->ld_defport;
96
97         if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
98                 if ( ld->ld_defhost != NULL ) free( srv->lsrv_host );
99                 free( (char *)srv );
100                 ldap_ld_free( ld, 0 );
101 #ifdef HAVE_WINSOCK_2
102                 WSACleanup();
103 #endif
104                 return( NULL );
105         }
106         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
107
108 #else /* LDAP_REFERRALS */
109         if ( open_ldap_connection( ld, &ld->ld_sb, ld->ld_defhost,
110             ld->ld_defport, &ld->ld_host, 0 ) < 0 ) {
111                 ldap_ld_free( ld, 0 );
112 #ifdef HAVE_WINSOCK_2
113                 WSACleanup();
114 #endif
115                 return( NULL );
116         }
117 #endif /* LDAP_REFERRALS */
118
119         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
120                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
121
122         return( ld );
123 }
124
125
126 /*
127  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
128  * future communication is returned on success, NULL on failure.
129  * "defhost" may be a space-separated list of hosts or IP addresses
130  *
131  * Example:
132  *      LDAP    *ld;
133  *      ld = ldap_open( default_hostname, default_port );
134  */
135 LDAP *
136 ldap_init( char *defhost, int defport )
137 {
138         LDAP                    *ld;
139
140         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
141
142
143         if ( (ld = (LDAP *) calloc( 1, sizeof(LDAP) )) == NULL ) {
144                 return( NULL );
145         }
146
147 #ifdef LDAP_REFERRALS
148         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
149                 free( (char*)ld );
150                 return( NULL );
151         }
152         ld->ld_options = LDAP_OPT_REFERRALS;
153 #endif /* LDAP_REFERRALS */
154
155         if ( defhost != NULL &&
156             ( ld->ld_defhost = strdup( defhost )) == NULL ) {
157 #ifdef LDAP_REFERRALS
158                 ldap_free_select_info( ld->ld_selectinfo );
159 #endif /* LDAP_REFERRALS */
160                 free( (char*)ld );
161                 return( NULL );
162         }
163
164
165         ld->ld_defport = ( defport == 0 ) ? LDAP_PORT : defport;
166         ld->ld_version = LDAP_VERSION;
167         ld->ld_lberoptions = LBER_USE_DER;
168         ld->ld_refhoplimit = LDAP_DEFAULT_REFHOPLIMIT;
169
170 #ifdef LDAP_REFERRALS
171         ld->ld_options |= LDAP_OPT_REFERRALS;
172 #endif /* LDAP_REFERRALS */
173
174 #if defined( STR_TRANSLATION ) && defined( LDAP_DEFAULT_CHARSET )
175         ld->ld_lberoptions |= LBER_TRANSLATE_STRINGS;
176 #if LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET
177         ldap_set_string_translators( ld, ldap_8859_to_t61, ldap_t61_to_8859 );
178 #endif /* LDAP_CHARSET_8859 == LDAP_DEFAULT_CHARSET */
179 #endif /* STR_TRANSLATION && LDAP_DEFAULT_CHARSET */
180
181         return( ld );
182 }
183
184
185 int
186 open_ldap_connection( LDAP *ld, Sockbuf *sb, char *host, int defport,
187         char **krbinstancep, int async )
188 {
189         int                     rc = -1;
190         int                             port;
191         char                    *p, *q, *r;
192         char                    *curhost, hostname[ 2*MAXHOSTNAMELEN ];
193
194         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
195
196         defport = htons( defport );
197
198         if ( host != NULL ) {
199                 for ( p = host; p != NULL && *p != '\0'; p = q ) {
200                         if (( q = strchr( p, ' ' )) != NULL ) {
201                                 strncpy( hostname, p, q - p );
202                                 hostname[ q - p ] = '\0';
203                                 curhost = hostname;
204                                 while ( *q == ' ' ) {
205                                     ++q;
206                                 }
207                         } else {
208                                 curhost = p;    /* avoid copy if possible */
209                                 q = NULL;
210                         }
211
212                         if (( r = strchr( curhost, ':' )) != NULL ) {
213                             if ( curhost != hostname ) {
214                                 strcpy( hostname, curhost );    /* now copy */
215                                 r = hostname + ( r - curhost );
216                                 curhost = hostname;
217                             }
218                             *r++ = '\0';
219                             port = htons( (short)atoi( r ));
220                         } else {
221                             port = defport;   
222                         }
223
224                         if (( rc = ldap_connect_to_host( sb, curhost, 0L,
225                             port, async )) != -1 ) {
226                                 break;
227                         }
228                 }
229         } else {
230                 rc = ldap_connect_to_host( sb, NULL, htonl( INADDR_LOOPBACK ),
231                     defport, async );
232         }
233
234         if ( rc == -1 ) {
235                 return( rc );
236         }
237
238         if ( krbinstancep != NULL ) {
239 #ifdef KERBEROS
240                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
241                     ( p = strchr( *krbinstancep, '.' )) != NULL ) {
242                         *p = '\0';
243                 }
244 #else /* KERBEROS */
245                 krbinstancep = NULL;
246 #endif /* KERBEROS */
247         }
248
249         return( 0 );
250 }