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