]> git.sur5r.net Git - openldap/blob - libraries/libldap/open.c
on my system need headers to define geteuid() and so
[openldap] / libraries / libldap / open.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*  Portions
7  *  Copyright (c) 1995 Regents of the University of Michigan.
8  *  All rights reserved.
9  *
10  *  open.c
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <limits.h>
17
18 #include <ac/stdlib.h>
19
20 #include <ac/param.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/time.h>
24
25 #include <unistd.h>
26 #include <sys/types.h>
27
28 #include "ldap-int.h"
29 #include "ldap_log.h"
30
31 int ldap_open_defconn( LDAP *ld )
32 {
33         ld->ld_defconn = ldap_new_connection( ld,
34                 ld->ld_options.ldo_defludp, 1, 1, NULL );
35
36         if( ld->ld_defconn == NULL ) {
37                 ld->ld_errno = LDAP_SERVER_DOWN;
38                 return -1;
39         }
40
41         ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
42         return 0;
43 }
44
45 /*
46  * ldap_open - initialize and connect to an ldap server.  A magic cookie to
47  * be used for future communication is returned on success, NULL on failure.
48  * "host" may be a space-separated list of hosts or IP addresses
49  *
50  * Example:
51  *      LDAP    *ld;
52  *      ld = ldap_open( hostname, port );
53  */
54
55 LDAP *
56 ldap_open( LDAP_CONST char *host, int port )
57 {
58         int rc;
59         LDAP            *ld;
60
61 #ifdef NEW_LOGGING
62         LDAP_LOG ( CONNECTION, ARGS, "ldap_open(%s, %d)\n", host, port, 0 );
63 #else
64         Debug( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
65                 host, port, 0 );
66 #endif
67
68         ld = ldap_init( host, port );
69         if ( ld == NULL ) {
70                 return( NULL );
71         }
72
73         rc = ldap_open_defconn( ld );
74
75         if( rc < 0 ) {
76                 ldap_ld_free( ld, 0, NULL, NULL );
77                 ld = NULL;
78         }
79
80 #ifdef NEW_LOGGING
81         LDAP_LOG ( CONNECTION, RESULTS, "ldap_open: %s\n",
82                 ld == NULL ? "succeeded" : "failed", 0, 0 );
83 #else
84         Debug( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
85                 ld == NULL ? "succeeded" : "failed", 0, 0 );
86 #endif
87
88         return ld;
89 }
90
91
92
93 int
94 ldap_create( LDAP **ldp )
95 {
96         LDAP                    *ld;
97         struct ldapoptions      *gopts;
98
99         *ldp = NULL;
100         /* Get pointer to global option structure */
101         if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
102                 return LDAP_NO_MEMORY;
103         }
104
105         /* Initialize the global options, if not already done. */
106         if( gopts->ldo_valid != LDAP_INITIALIZED ) {
107                 ldap_int_initialize(gopts, NULL);
108                 if ( gopts->ldo_valid != LDAP_INITIALIZED )
109                         return LDAP_LOCAL_ERROR;
110         }
111
112 #ifdef NEW_LOGGING
113         LDAP_LOG ( CONNECTION, ENTRY, "ldap_create\n", 0, 0, 0 );
114 #else
115         Debug( LDAP_DEBUG_TRACE, "ldap_create\n", 0, 0, 0 );
116 #endif
117
118         if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
119                 return( LDAP_NO_MEMORY );
120         }
121    
122         /* copy the global options */
123         AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
124
125         ld->ld_valid = LDAP_VALID_SESSION;
126
127         /* but not pointers to malloc'ed items */
128         ld->ld_options.ldo_sctrls = NULL;
129         ld->ld_options.ldo_cctrls = NULL;
130
131 #ifdef HAVE_CYRUS_SASL
132         ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
133                 ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
134         ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
135                 ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
136         ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
137                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
138         ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
139                 ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
140 #endif
141
142         ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
143
144         if ( ld->ld_options.ldo_defludp == NULL ) {
145                 LDAP_FREE( (char*)ld );
146                 return LDAP_NO_MEMORY;
147         }
148
149         if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) {
150                 ldap_free_urllist( ld->ld_options.ldo_defludp );
151                 LDAP_FREE( (char*) ld );
152                 return LDAP_NO_MEMORY;
153         }
154
155         ld->ld_lberoptions = LBER_USE_DER;
156
157         ld->ld_sb = ber_sockbuf_alloc( );
158         if ( ld->ld_sb == NULL ) {
159                 ldap_free_urllist( ld->ld_options.ldo_defludp );
160                 LDAP_FREE( (char*) ld );
161                 return LDAP_NO_MEMORY;
162         }
163
164         *ldp = ld;
165         return LDAP_SUCCESS;
166 }
167
168 /*
169  * ldap_init - initialize the LDAP library.  A magic cookie to be used for
170  * future communication is returned on success, NULL on failure.
171  * "host" may be a space-separated list of hosts or IP addresses
172  *
173  * Example:
174  *      LDAP    *ld;
175  *      ld = ldap_init( host, port );
176  */
177 LDAP *
178 ldap_init( LDAP_CONST char *defhost, int defport )
179 {
180         LDAP *ld;
181         int rc;
182
183         rc = ldap_create(&ld);
184         if ( rc != LDAP_SUCCESS )
185                 return NULL;
186
187         if (defport != 0)
188                 ld->ld_options.ldo_defport = defport;
189
190         if (defhost != NULL) {
191                 rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
192                 if ( rc != LDAP_SUCCESS ) {
193                         ldap_ld_free(ld, 1, NULL, NULL);
194                         return NULL;
195                 }
196         }
197
198         return( ld );
199 }
200
201
202 int
203 ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
204 {
205         int rc;
206         LDAP *ld;
207
208         *ldp = NULL;
209         rc = ldap_create(&ld);
210         if ( rc != LDAP_SUCCESS )
211                 return rc;
212
213         if (url != NULL) {
214                 rc = ldap_set_option(ld, LDAP_OPT_URI, url);
215                 if ( rc != LDAP_SUCCESS ) {
216                         ldap_ld_free(ld, 1, NULL, NULL);
217                         return rc;
218                 }
219 #ifdef LDAP_CONNECTIONLESS
220                 if (ldap_is_ldapc_url(url))
221                         LDAP_IS_UDP(ld) = 1;
222 #endif
223         }
224
225         *ldp = ld;
226         return LDAP_SUCCESS;
227 }
228
229 int
230 ldap_int_open_connection(
231         LDAP *ld,
232         LDAPConn *conn,
233         LDAPURLDesc *srv,
234         int async )
235 {
236         int rc = -1;
237 #ifdef HAVE_CYRUS_SASL
238         char *sasl_host = NULL;
239         int sasl_ssf = 0;
240 #endif
241         char *host;
242         int port, proto;
243         long addr;
244
245 #ifdef NEW_LOGGING
246         LDAP_LOG ( CONNECTION, ENTRY, "ldap_int_open_connection\n", 0, 0, 0 );
247 #else
248         Debug( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n", 0, 0, 0 );
249 #endif
250
251         switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
252                 case LDAP_PROTO_TCP:
253                         port = srv->lud_port;
254
255                         addr = 0;
256                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
257                                 host = NULL;
258                                 addr = htonl( INADDR_LOOPBACK );
259                         } else {
260                                 host = srv->lud_host;
261                         }
262
263                         if( !port ) {
264                                 if( strcmp(srv->lud_scheme, "ldaps") == 0 ) {
265                                         port = LDAPS_PORT;
266                                 } else {
267                                         port = LDAP_PORT;
268                                 }
269                         }
270
271                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
272                                 proto, host, addr, port, async );
273
274                         if ( rc == -1 ) return rc;
275
276 #ifdef LDAP_DEBUG
277                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
278                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
279 #endif
280                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
281                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
282
283 #ifdef HAVE_CYRUS_SASL
284                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
285 #endif
286                         break;
287 #ifdef LDAP_CONNECTIONLESS
288
289                 case LDAP_PROTO_UDP:
290                         port = srv->lud_port;
291
292                         addr = 0;
293                         if ( srv->lud_host == NULL || *srv->lud_host == 0 ) {
294                                 host = NULL;
295                                 addr = htonl( INADDR_LOOPBACK );
296                         } else {
297                                 host = srv->lud_host;
298                         }
299
300                         if( !port ) port = LDAP_PORT;
301
302                         LDAP_IS_UDP(ld) = 1;
303                         rc = ldap_connect_to_host( ld, conn->lconn_sb,
304                                 proto, host, addr, port, async );
305
306                         if ( rc == -1 ) return rc;
307 #ifdef LDAP_DEBUG
308                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
309                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
310 #endif
311                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
312                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
313                         break;
314 #endif
315                 case LDAP_PROTO_IPC:
316 #ifdef LDAP_PF_LOCAL
317                         /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
318                         rc = ldap_connect_to_path( ld, conn->lconn_sb,
319                                 srv->lud_host, async );
320                         if ( rc == -1 ) return rc;
321 #ifdef LDAP_DEBUG
322                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
323                                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
324 #endif
325                         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
326                                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
327
328 #ifdef HAVE_CYRUS_SASL
329                         sasl_host = ldap_host_connected_to( conn->lconn_sb );
330                         sasl_ssf = LDAP_PVT_SASL_LOCAL_SSF;
331 #endif
332                         break;
333 #endif /* LDAP_PF_LOCAL */
334                 default:
335                         return -1;
336                         break;
337         }
338
339         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
340                 LBER_SBIOD_LEVEL_PROVIDER, NULL );
341
342 #ifdef LDAP_DEBUG
343         ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
344                 INT_MAX, (void *)"ldap_" );
345 #endif
346
347 #ifdef LDAP_CONNECTIONLESS
348         if( proto == LDAP_PROTO_UDP )
349                 return 0;
350 #endif
351
352 #ifdef HAVE_CYRUS_SASL
353         /* establish Cyrus SASL context prior to starting TLS so
354                 that SASL EXTERNAL might be used */
355         if( sasl_host != NULL ) {
356                 ldap_int_sasl_open( ld, conn, sasl_host, sasl_ssf );
357                 LDAP_FREE( sasl_host );
358         }
359         /* sasl_ssf is set redundantly. Should probably remove it from
360          * the ldap_int_sasl_open call since the TLS ssf isn't known
361          * yet anyway.
362          */
363         if( proto == LDAP_PROTO_IPC ) {
364                 char authid[64];
365                 sprintf( authid, "uid=%d+gid=%d", geteuid(), getegid() );
366                 ldap_int_sasl_external( ld, conn, authid, sasl_ssf );
367         }
368 #endif
369
370 #ifdef HAVE_TLS
371         if (ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
372                 strcmp( srv->lud_scheme, "ldaps" ) == 0 )
373         {
374                 ++conn->lconn_refcnt;   /* avoid premature free */
375
376                 rc = ldap_int_tls_start( ld, conn, srv );
377
378                 --conn->lconn_refcnt;
379
380                 if (rc != LDAP_SUCCESS) {
381                         return -1;
382                 }
383         }
384 #endif
385
386 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
387         if ( conn->lconn_krbinstance == NULL ) {
388                 char *c;
389                 conn->lconn_krbinstance = ldap_host_connected_to( conn->lconn_sb );
390
391                 if( conn->lconn_krbinstance != NULL && 
392                     ( c = strchr( conn->lconn_krbinstance, '.' )) != NULL ) {
393                         *c = '\0';
394                 }
395         }
396 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
397
398         return( 0 );
399 }
400
401
402 int ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
403 {
404         int rc;
405         LDAPConn *c;
406         LDAPRequest *lr;
407
408         rc = ldap_create( ldp );
409         if( rc != LDAP_SUCCESS ) {
410                 *ldp = NULL;
411                 return( rc );
412         }
413
414         /* Make it appear that a search request, msgid 0, was sent */
415         lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
416         if( lr == NULL ) {
417                 ldap_unbind( *ldp );
418                 *ldp = NULL;
419                 return( LDAP_NO_MEMORY );
420         }
421         memset(lr, 0, sizeof( LDAPRequest ));
422         lr->lr_msgid = 0;
423         lr->lr_status = LDAP_REQST_INPROGRESS;
424         lr->lr_res_errno = LDAP_SUCCESS;
425         (*ldp)->ld_requests = lr;
426
427         /* Attach the passed socket as the *LDAP's connection */
428         c = ldap_new_connection( *ldp, NULL, 1, 0, NULL);
429         if( c == NULL ) {
430                 ldap_unbind( *ldp );
431                 *ldp = NULL;
432                 return( LDAP_NO_MEMORY );
433         }
434         ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
435 #ifdef LDAP_DEBUG
436         ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
437                 LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
438 #endif
439         ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
440           LBER_SBIOD_LEVEL_PROVIDER, NULL );
441         (*ldp)->ld_defconn = c;
442
443         /* Add the connection to the *LDAP's select pool */
444         ldap_mark_select_read( *ldp, c->lconn_sb );
445         ldap_mark_select_write( *ldp, c->lconn_sb );
446
447         /* Make this connection an LDAP V3 protocol connection */
448         rc = LDAP_VERSION3;
449         ldap_set_option( *ldp, LDAP_OPT_PROTOCOL_VERSION, &rc );
450
451         return( LDAP_SUCCESS );
452 }