]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
s/slapentry/slapadd/
[openldap] / libraries / libldap / open.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1995 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  open.c
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15
16 #include <ac/stdlib.h>
17
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21
22 #ifdef HAVE_SYS_PARAM_H
23 #include <sys/param.h>
24 #endif
25
26 #include "ldap-int.h"
27
28
29 /*
30  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
31  * be used for future communication is returned on success, NULL on failure.
32  * "host" may be a space-separated list of hosts or IP addresses
33  *
34  * Example:
35  *      LDAP    *ld;
36  *      ld = ldap_open( hostname, port );
37  */
38
39 LDAP *
40 ldap_open( LDAP_CONST char *host, int port )
41 {
42         LDAP            *ld;
43         LDAPServer      *srv;
44
45         Debug( LDAP_DEBUG_TRACE, "ldap_open\n", 0, 0, 0 );
46
47         if (( ld = ldap_init( host, port )) == NULL ) {
48                 return( NULL );
49         }
50
51         if (( srv = (LDAPServer *)LDAP_CALLOC( 1, sizeof( LDAPServer ))) ==
52             NULL || ( ld->ld_defhost != NULL && ( srv->lsrv_host =
53             LDAP_STRDUP( ld->ld_defhost )) == NULL )) {
54                 if(srv != NULL) LDAP_FREE( (char*) srv );
55                 ldap_ld_free( ld, 0, NULL, NULL );
56                 return( NULL );
57         }
58         srv->lsrv_port = ld->ld_defport;
59
60         if (( ld->ld_defconn = ldap_new_connection( ld, &srv, 1,1,0 )) == NULL ) {
61                 if ( ld->ld_defhost != NULL ) LDAP_FREE( srv->lsrv_host );
62                 LDAP_FREE( (char *)srv );
63                 ldap_ld_free( ld, 0, NULL, NULL );
64                 return( NULL );
65         }
66         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
67
68         Debug( LDAP_DEBUG_TRACE, "ldap_open successful, ld_host is %s\n",
69                 ( ld->ld_host == NULL ) ? "(null)" : ld->ld_host, 0, 0 );
70
71         return( ld );
72 }
73
74
75 /*
76  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
77  * future communication is returned on success, NULL on failure.
78  * "host" may be a space-separated list of hosts or IP addresses
79  *
80  * Example:
81  *      LDAP    *ld;
82  *      ld = ldap_open( host, port );
83  */
84 LDAP *
85 ldap_init( LDAP_CONST char *defhost, int defport )
86 {
87         LDAP                    *ld;
88
89         if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
90                 ldap_int_initialize();
91         }
92
93         Debug( LDAP_DEBUG_TRACE, "ldap_init\n", 0, 0, 0 );
94
95 #ifdef HAVE_WINSOCK2
96 {       WORD wVersionRequested;
97         WSADATA wsaData;
98         int err;
99  
100         wVersionRequested = MAKEWORD( 2, 0 );
101  
102         err = WSAStartup( wVersionRequested, &wsaData );
103         if ( err != 0 ) {
104                 /* Tell the user that we couldn't find a usable */
105                 /* WinSock DLL.                                  */
106                 return NULL;
107         }
108  
109         /* Confirm that the WinSock DLL supports 2.0.*/
110         /* Note that if the DLL supports versions greater    */
111         /* than 2.0 in addition to 2.0, it will still return */
112         /* 2.0 in wVersion since that is the version we      */
113         /* requested.                                        */
114  
115         if ( LOBYTE( wsaData.wVersion ) != 2 ||
116                 HIBYTE( wsaData.wVersion ) != 0 )
117         {
118             /* Tell the user that we couldn't find a usable */
119             /* WinSock DLL.                                  */
120             WSACleanup( );
121             return NULL; 
122         }
123 }       /* The WinSock DLL is acceptable. Proceed. */
124
125 #elif HAVE_WINSOCK
126 {       WSADATA wsaData;
127         if ( WSAStartup( 0x0101, &wsaData ) != 0 ) {
128             return( NULL );
129         }
130 }
131 #endif
132
133         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
134             WSACleanup( );
135                 return( NULL );
136         }
137    
138         /* copy the global options */
139         memcpy(&ld->ld_options, &ldap_int_global_options,
140                 sizeof(ld->ld_options));
141
142         ld->ld_valid = LDAP_VALID_SESSION;
143
144         /* but not pointers to malloc'ed items */
145         ld->ld_options.ldo_defbase = NULL;
146         ld->ld_options.ldo_defhost = NULL;
147         ld->ld_options.ldo_sctrls = NULL;
148         ld->ld_options.ldo_cctrls = NULL;
149
150         if ( defhost != NULL ) {
151                 ld->ld_options.ldo_defhost = LDAP_STRDUP( defhost );
152         } else {
153                 ld->ld_options.ldo_defhost = LDAP_STRDUP(
154                         ldap_int_global_options.ldo_defhost);
155         }
156
157         if ( ld->ld_options.ldo_defhost == NULL ) {
158                 LDAP_FREE( (char*)ld );
159             WSACleanup( );
160                 return( NULL );
161         }
162
163         if ( ldap_int_global_options.ldo_defbase != NULL ) {
164                 ld->ld_options.ldo_defbase = LDAP_STRDUP(
165                         ldap_int_global_options.ldo_defbase);
166         }
167
168         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
169                 LDAP_FREE( (char*) ld->ld_options.ldo_defhost );
170                 if ( ld->ld_options.ldo_defbase == NULL ) {
171                         LDAP_FREE( (char*) ld->ld_options.ldo_defbase );
172                 }
173                 LDAP_FREE( (char*) ld );
174             WSACleanup( );
175                 return( NULL );
176         }
177
178         if(defport != 0) {
179                 ld->ld_defport = defport;
180         }
181
182         ld->ld_lberoptions = LBER_USE_DER;
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         /* we'll assume we're talking version 2 for now */
192         ld->ld_version = LDAP_VERSION2;
193
194         ber_pvt_sb_init( &(ld->ld_sb) );
195
196         return( ld );
197 }
198
199
200 int
201 open_ldap_connection( LDAP *ld, Sockbuf *sb, const char *host, int defport,
202         char **krbinstancep, int async )
203 {
204         int                     rc = -1;
205         int                             port;
206         const char              *p, *q;
207         char                    *r, *curhost, hostname[ 2*MAXHOSTNAMELEN ];
208
209         Debug( LDAP_DEBUG_TRACE, "open_ldap_connection\n", 0, 0, 0 );
210
211         defport = htons( (short) defport );
212
213         if ( host != NULL ) {
214                 for ( p = host; p != NULL && *p != '\0'; p = q ) {
215                         if (( q = strchr( p, ' ' )) != NULL ) {
216                                 strncpy( hostname, p, q - p );
217                                 hostname[ q - p ] = '\0';
218                                 curhost = hostname;
219                                 while ( *q == ' ' ) {
220                                     ++q;
221                                 }
222                         } else {
223                                 curhost = (char *) p;   /* avoid copy if possible */
224                                 q = NULL;
225                         }
226
227                         if (( r = strchr( curhost, ':' )) != NULL ) {
228                             if ( curhost != hostname ) {
229                                 strcpy( hostname, curhost );    /* now copy */
230                                 r = hostname + ( r - curhost );
231                                 curhost = hostname;
232                             }
233                             *r++ = '\0';
234                             port = htons( (short) atoi( r ) );
235                         } else {
236                             port = defport;   
237                         }
238
239                         if (( rc = ldap_connect_to_host( ld, sb, curhost, 0L,
240                             port, async )) != -1 ) {
241                                 break;
242                         }
243                 }
244         } else {
245                 rc = ldap_connect_to_host( ld, sb, 0, htonl( INADDR_LOOPBACK ),
246                     defport, async );
247         }
248
249         if ( rc == -1 ) {
250                 return( rc );
251         }
252    
253         ber_pvt_sb_set_io( sb, &ber_pvt_sb_io_tcp, NULL );
254
255 #ifdef HAVE_TLS
256         if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ) {
257                 /*
258                  * Fortunately, the lib uses blocking io...
259                  */
260                 if ( ldap_pvt_tls_connect( sb, ld->ld_options.ldo_tls_ctx ) < 
261                      0 ) {
262                         return -1;
263                 }
264                 /* FIXME: hostname of server must be compared with name in
265                  * certificate....
266                  */
267         }
268 #endif
269         if ( krbinstancep != NULL ) {
270 #ifdef HAVE_KERBEROS
271                 char *c;
272                 if (( *krbinstancep = ldap_host_connected_to( sb )) != NULL &&
273                     ( c = strchr( *krbinstancep, '.' )) != NULL ) {
274                         *c = '\0';
275                 }
276 #else /* HAVE_KERBEROS */
277                 krbinstancep = NULL;
278 #endif /* HAVE_KERBEROS */
279         }
280
281         return( 0 );
282 }