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