open.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /* $OpenLDAP$ */
  2. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  3. *
  4. * Copyright 1998-2024 The OpenLDAP Foundation.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted only as authorized by the OpenLDAP
  9. * Public License.
  10. *
  11. * A copy of this license is available in the file LICENSE in the
  12. * top-level directory of the distribution or, alternatively, at
  13. * <http://www.OpenLDAP.org/license.html>.
  14. */
  15. /* Portions Copyright (c) 1995 Regents of the University of Michigan.
  16. * All rights reserved.
  17. */
  18. #include "portable.h"
  19. #include <stdio.h>
  20. #ifdef HAVE_LIMITS_H
  21. #include <limits.h>
  22. #endif
  23. #include <ac/stdlib.h>
  24. #include <ac/param.h>
  25. #include <ac/socket.h>
  26. #include <ac/string.h>
  27. #include <ac/time.h>
  28. #include <ac/unistd.h>
  29. #include "ldap-int.h"
  30. #include "ldap.h"
  31. #include "ldap_log.h"
  32. /* Caller must hold the conn_mutex since simultaneous accesses are possible */
  33. int ldap_open_defconn( LDAP *ld )
  34. {
  35. ld->ld_defconn = ldap_new_connection( ld,
  36. &ld->ld_options.ldo_defludp, 1, 1, NULL, 0, 0 );
  37. if( ld->ld_defconn == NULL ) {
  38. ld->ld_errno = LDAP_SERVER_DOWN;
  39. return -1;
  40. }
  41. ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
  42. return 0;
  43. }
  44. /*
  45. * ldap_connect - Connect to an ldap server.
  46. *
  47. * Example:
  48. * LDAP *ld;
  49. * ldap_initialize( &ld, url );
  50. * ldap_connect( ld );
  51. */
  52. int
  53. ldap_connect( LDAP *ld )
  54. {
  55. ber_socket_t sd = AC_SOCKET_INVALID;
  56. int rc = LDAP_SUCCESS;
  57. LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
  58. if ( ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd ) == -1 ) {
  59. rc = ldap_open_defconn( ld );
  60. }
  61. LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
  62. return rc;
  63. }
  64. /*
  65. * ldap_open - initialize and connect to an ldap server. A magic cookie to
  66. * be used for future communication is returned on success, NULL on failure.
  67. * "host" may be a space-separated list of hosts or IP addresses
  68. *
  69. * Example:
  70. * LDAP *ld;
  71. * ld = ldap_open( hostname, port );
  72. */
  73. LDAP *
  74. ldap_open( LDAP_CONST char *host, int port )
  75. {
  76. int rc;
  77. LDAP *ld;
  78. Debug2( LDAP_DEBUG_TRACE, "ldap_open(%s, %d)\n",
  79. host, port );
  80. ld = ldap_init( host, port );
  81. if ( ld == NULL ) {
  82. return( NULL );
  83. }
  84. LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
  85. rc = ldap_open_defconn( ld );
  86. LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
  87. if( rc < 0 ) {
  88. ldap_ld_free( ld, 0, NULL, NULL );
  89. ld = NULL;
  90. }
  91. Debug1( LDAP_DEBUG_TRACE, "ldap_open: %s\n",
  92. ld != NULL ? "succeeded" : "failed" );
  93. return ld;
  94. }
  95. int
  96. ldap_create( LDAP **ldp )
  97. {
  98. LDAP *ld;
  99. struct ldapoptions *gopts;
  100. *ldp = NULL;
  101. /* Get pointer to global option structure */
  102. if ( (gopts = LDAP_INT_GLOBAL_OPT()) == NULL) {
  103. return LDAP_NO_MEMORY;
  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. Debug0( LDAP_DEBUG_TRACE, "ldap_create\n" );
  112. if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
  113. return( LDAP_NO_MEMORY );
  114. }
  115. if ( (ld->ldc = (struct ldap_common *) LDAP_CALLOC( 1,
  116. sizeof(struct ldap_common) )) == NULL ) {
  117. LDAP_FREE( (char *)ld );
  118. return( LDAP_NO_MEMORY );
  119. }
  120. /* copy the global options */
  121. LDAP_MUTEX_LOCK( &gopts->ldo_mutex );
  122. AC_MEMCPY(&ld->ld_options, gopts, sizeof(ld->ld_options));
  123. #ifdef LDAP_R_COMPILE
  124. /* Properly initialize the structs mutex */
  125. ldap_pvt_thread_mutex_init( &(ld->ld_ldopts_mutex) );
  126. #endif
  127. #ifdef HAVE_TLS
  128. if ( ld->ld_options.ldo_tls_pin_hashalg ) {
  129. int len = strlen( gopts->ldo_tls_pin_hashalg );
  130. ld->ld_options.ldo_tls_pin_hashalg =
  131. LDAP_MALLOC( len + 1 + gopts->ldo_tls_pin.bv_len );
  132. if ( !ld->ld_options.ldo_tls_pin_hashalg ) goto nomem;
  133. ld->ld_options.ldo_tls_pin.bv_val = ld->ld_options.ldo_tls_pin_hashalg
  134. + len + 1;
  135. AC_MEMCPY( ld->ld_options.ldo_tls_pin_hashalg, gopts->ldo_tls_pin_hashalg,
  136. len + 1 + gopts->ldo_tls_pin.bv_len );
  137. } else if ( !BER_BVISEMPTY(&ld->ld_options.ldo_tls_pin) ) {
  138. ber_dupbv( &ld->ld_options.ldo_tls_pin, &gopts->ldo_tls_pin );
  139. }
  140. #endif
  141. LDAP_MUTEX_UNLOCK( &gopts->ldo_mutex );
  142. ld->ld_valid = LDAP_VALID_SESSION;
  143. /* but not pointers to malloc'ed items */
  144. ld->ld_options.ldo_sctrls = NULL;
  145. ld->ld_options.ldo_cctrls = NULL;
  146. ld->ld_options.ldo_defludp = NULL;
  147. ld->ld_options.ldo_conn_cbs = NULL;
  148. ld->ld_options.ldo_defbase = gopts->ldo_defbase
  149. ? LDAP_STRDUP( gopts->ldo_defbase ) : NULL;
  150. #ifdef HAVE_CYRUS_SASL
  151. ld->ld_options.ldo_def_sasl_mech = gopts->ldo_def_sasl_mech
  152. ? LDAP_STRDUP( gopts->ldo_def_sasl_mech ) : NULL;
  153. ld->ld_options.ldo_def_sasl_realm = gopts->ldo_def_sasl_realm
  154. ? LDAP_STRDUP( gopts->ldo_def_sasl_realm ) : NULL;
  155. ld->ld_options.ldo_def_sasl_authcid = gopts->ldo_def_sasl_authcid
  156. ? LDAP_STRDUP( gopts->ldo_def_sasl_authcid ) : NULL;
  157. ld->ld_options.ldo_def_sasl_authzid = gopts->ldo_def_sasl_authzid
  158. ? LDAP_STRDUP( gopts->ldo_def_sasl_authzid ) : NULL;
  159. #endif
  160. #ifdef HAVE_TLS
  161. /* We explicitly inherit the SSL_CTX, don't need the names/paths. Leave
  162. * them empty to allow new SSL_CTX's to be created from scratch.
  163. */
  164. memset( &ld->ld_options.ldo_tls_info, 0,
  165. sizeof( ld->ld_options.ldo_tls_info ));
  166. ld->ld_options.ldo_tls_ctx = NULL;
  167. #endif
  168. if ( gopts->ldo_defludp ) {
  169. ld->ld_options.ldo_defludp = ldap_url_duplist(gopts->ldo_defludp);
  170. if ( ld->ld_options.ldo_defludp == NULL ) goto nomem;
  171. }
  172. if (( ld->ld_selectinfo = ldap_new_select_info()) == NULL ) goto nomem;
  173. ld->ld_options.ldo_local_ip_addrs.local_ip_addrs = NULL;
  174. if( gopts->ldo_local_ip_addrs.local_ip_addrs ) {
  175. ld->ld_options.ldo_local_ip_addrs.local_ip_addrs =
  176. LDAP_STRDUP( gopts->ldo_local_ip_addrs.local_ip_addrs );
  177. if ( ld->ld_options.ldo_local_ip_addrs.local_ip_addrs == NULL )
  178. goto nomem;
  179. }
  180. ld->ld_lberoptions = LBER_USE_DER;
  181. ld->ld_sb = ber_sockbuf_alloc( );
  182. if ( ld->ld_sb == NULL ) goto nomem;
  183. #ifdef LDAP_R_COMPILE
  184. ldap_pvt_thread_mutex_init( &ld->ld_msgid_mutex );
  185. ldap_pvt_thread_mutex_init( &ld->ld_conn_mutex );
  186. ldap_pvt_thread_mutex_init( &ld->ld_req_mutex );
  187. ldap_pvt_thread_mutex_init( &ld->ld_res_mutex );
  188. ldap_pvt_thread_mutex_init( &ld->ld_abandon_mutex );
  189. ldap_pvt_thread_mutex_init( &ld->ld_ldcmutex );
  190. #endif
  191. ld->ld_ldcrefcnt = 1;
  192. *ldp = ld;
  193. return LDAP_SUCCESS;
  194. nomem:
  195. ldap_free_select_info( ld->ld_selectinfo );
  196. ldap_free_urllist( ld->ld_options.ldo_defludp );
  197. #ifdef HAVE_CYRUS_SASL
  198. LDAP_FREE( ld->ld_options.ldo_def_sasl_authzid );
  199. LDAP_FREE( ld->ld_options.ldo_def_sasl_authcid );
  200. LDAP_FREE( ld->ld_options.ldo_def_sasl_realm );
  201. LDAP_FREE( ld->ld_options.ldo_def_sasl_mech );
  202. #endif
  203. #ifdef HAVE_TLS
  204. /* tls_pin_hashalg and tls_pin share the same buffer */
  205. if ( ld->ld_options.ldo_tls_pin_hashalg ) {
  206. LDAP_FREE( ld->ld_options.ldo_tls_pin_hashalg );
  207. } else {
  208. LDAP_FREE( ld->ld_options.ldo_tls_pin.bv_val );
  209. }
  210. #endif
  211. LDAP_FREE( (char *)ld );
  212. return LDAP_NO_MEMORY;
  213. }
  214. /*
  215. * ldap_init - initialize the LDAP library. A magic cookie to be used for
  216. * future communication is returned on success, NULL on failure.
  217. * "host" may be a space-separated list of hosts or IP addresses
  218. *
  219. * Example:
  220. * LDAP *ld;
  221. * ld = ldap_init( host, port );
  222. */
  223. LDAP *
  224. ldap_init( LDAP_CONST char *defhost, int defport )
  225. {
  226. LDAP *ld;
  227. int rc;
  228. rc = ldap_create(&ld);
  229. if ( rc != LDAP_SUCCESS )
  230. return NULL;
  231. if (defport != 0)
  232. ld->ld_options.ldo_defport = defport;
  233. if (defhost != NULL) {
  234. rc = ldap_set_option(ld, LDAP_OPT_HOST_NAME, defhost);
  235. if ( rc != LDAP_SUCCESS ) {
  236. ldap_ld_free(ld, 1, NULL, NULL);
  237. return NULL;
  238. }
  239. }
  240. return( ld );
  241. }
  242. int
  243. ldap_initialize( LDAP **ldp, LDAP_CONST char *url )
  244. {
  245. int rc;
  246. LDAP *ld;
  247. *ldp = NULL;
  248. rc = ldap_create(&ld);
  249. if ( rc != LDAP_SUCCESS )
  250. return rc;
  251. if (url != NULL) {
  252. rc = ldap_set_option(ld, LDAP_OPT_URI, url);
  253. if ( rc != LDAP_SUCCESS ) {
  254. ldap_ld_free(ld, 1, NULL, NULL);
  255. return rc;
  256. }
  257. #ifdef LDAP_CONNECTIONLESS
  258. if (ldap_is_ldapc_url(url))
  259. LDAP_IS_UDP(ld) = 1;
  260. #endif
  261. }
  262. *ldp = ld;
  263. return LDAP_SUCCESS;
  264. }
  265. int
  266. ldap_init_fd(
  267. ber_socket_t fd,
  268. int proto,
  269. LDAP_CONST char *url,
  270. LDAP **ldp
  271. )
  272. {
  273. int rc;
  274. LDAP *ld;
  275. LDAPConn *conn;
  276. #ifdef LDAP_CONNECTIONLESS
  277. ber_socklen_t len;
  278. #endif
  279. *ldp = NULL;
  280. rc = ldap_create( &ld );
  281. if( rc != LDAP_SUCCESS )
  282. return( rc );
  283. if (url != NULL) {
  284. rc = ldap_set_option(ld, LDAP_OPT_URI, url);
  285. if ( rc != LDAP_SUCCESS ) {
  286. ldap_ld_free(ld, 1, NULL, NULL);
  287. return rc;
  288. }
  289. }
  290. LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
  291. /* Attach the passed socket as the LDAP's connection */
  292. conn = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
  293. if( conn == NULL ) {
  294. LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
  295. ldap_unbind_ext( ld, NULL, NULL );
  296. return( LDAP_NO_MEMORY );
  297. }
  298. if( url )
  299. conn->lconn_server = ldap_url_dup( ld->ld_options.ldo_defludp );
  300. ber_sockbuf_ctrl( conn->lconn_sb, LBER_SB_OPT_SET_FD, &fd );
  301. ld->ld_defconn = conn;
  302. ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
  303. LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
  304. switch( proto ) {
  305. case LDAP_PROTO_TCP:
  306. #ifdef LDAP_DEBUG
  307. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
  308. LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
  309. #endif
  310. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
  311. LBER_SBIOD_LEVEL_PROVIDER, NULL );
  312. break;
  313. #ifdef LDAP_CONNECTIONLESS
  314. case LDAP_PROTO_UDP:
  315. LDAP_IS_UDP(ld) = 1;
  316. if( ld->ld_options.ldo_peer )
  317. ldap_memfree( ld->ld_options.ldo_peer );
  318. ld->ld_options.ldo_peer = ldap_memcalloc( 1, sizeof( struct sockaddr_storage ) );
  319. len = sizeof( struct sockaddr_storage );
  320. if( getpeername ( fd, ld->ld_options.ldo_peer, &len ) < 0) {
  321. ldap_unbind_ext( ld, NULL, NULL );
  322. return( AC_SOCKET_ERROR );
  323. }
  324. #ifdef LDAP_DEBUG
  325. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
  326. LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
  327. #endif
  328. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
  329. LBER_SBIOD_LEVEL_PROVIDER, NULL );
  330. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
  331. LBER_SBIOD_LEVEL_PROVIDER, NULL );
  332. break;
  333. #endif /* LDAP_CONNECTIONLESS */
  334. case LDAP_PROTO_IPC:
  335. #ifdef LDAP_DEBUG
  336. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
  337. LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
  338. #endif
  339. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
  340. LBER_SBIOD_LEVEL_PROVIDER, NULL );
  341. break;
  342. case LDAP_PROTO_EXT:
  343. /* caller must supply sockbuf handlers */
  344. break;
  345. default:
  346. ldap_unbind_ext( ld, NULL, NULL );
  347. return LDAP_PARAM_ERROR;
  348. }
  349. #ifdef LDAP_DEBUG
  350. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
  351. INT_MAX, (void *)"ldap_" );
  352. #endif
  353. /* Add the connection to the *LDAP's select pool */
  354. ldap_mark_select_read( ld, conn->lconn_sb );
  355. *ldp = ld;
  356. return LDAP_SUCCESS;
  357. }
  358. /* Protected by ld_conn_mutex */
  359. int
  360. ldap_int_open_connection(
  361. LDAP *ld,
  362. LDAPConn *conn,
  363. LDAPURLDesc *srv,
  364. int async )
  365. {
  366. int rc = -1;
  367. int proto;
  368. Debug0( LDAP_DEBUG_TRACE, "ldap_int_open_connection\n" );
  369. switch ( proto = ldap_pvt_url_scheme2proto( srv->lud_scheme ) ) {
  370. case LDAP_PROTO_TCP:
  371. rc = ldap_connect_to_host( ld, conn->lconn_sb,
  372. proto, srv, async );
  373. if ( rc == -1 ) return rc;
  374. #ifdef LDAP_DEBUG
  375. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
  376. LBER_SBIOD_LEVEL_PROVIDER, (void *)"tcp_" );
  377. #endif
  378. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_tcp,
  379. LBER_SBIOD_LEVEL_PROVIDER, NULL );
  380. break;
  381. #ifdef LDAP_CONNECTIONLESS
  382. case LDAP_PROTO_UDP:
  383. LDAP_IS_UDP(ld) = 1;
  384. rc = ldap_connect_to_host( ld, conn->lconn_sb,
  385. proto, srv, async );
  386. if ( rc == -1 ) return rc;
  387. #ifdef LDAP_DEBUG
  388. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
  389. LBER_SBIOD_LEVEL_PROVIDER, (void *)"udp_" );
  390. #endif
  391. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_udp,
  392. LBER_SBIOD_LEVEL_PROVIDER, NULL );
  393. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_readahead,
  394. LBER_SBIOD_LEVEL_PROVIDER, NULL );
  395. break;
  396. #endif
  397. case LDAP_PROTO_IPC:
  398. #ifdef LDAP_PF_LOCAL
  399. /* only IPC mechanism supported is PF_LOCAL (PF_UNIX) */
  400. rc = ldap_connect_to_path( ld, conn->lconn_sb,
  401. srv, async );
  402. if ( rc == -1 ) return rc;
  403. #ifdef LDAP_DEBUG
  404. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
  405. LBER_SBIOD_LEVEL_PROVIDER, (void *)"ipc_" );
  406. #endif
  407. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_fd,
  408. LBER_SBIOD_LEVEL_PROVIDER, NULL );
  409. break;
  410. #endif /* LDAP_PF_LOCAL */
  411. default:
  412. return -1;
  413. break;
  414. }
  415. conn->lconn_created = time( NULL );
  416. #ifdef LDAP_DEBUG
  417. ber_sockbuf_add_io( conn->lconn_sb, &ber_sockbuf_io_debug,
  418. INT_MAX, (void *)"ldap_" );
  419. #endif
  420. #ifdef LDAP_CONNECTIONLESS
  421. if( proto == LDAP_PROTO_UDP ) return 0;
  422. #endif
  423. if ( async && rc == -2) {
  424. /* Need to let the connect complete asynchronously before we continue */
  425. return -2;
  426. }
  427. #ifdef HAVE_TLS
  428. if ((rc == 0 || rc == -2) && ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
  429. strcmp( srv->lud_scheme, "ldaps" ) == 0 ))
  430. {
  431. ++conn->lconn_refcnt; /* avoid premature free */
  432. rc = ldap_int_tls_start( ld, conn, srv );
  433. --conn->lconn_refcnt;
  434. if (rc != LDAP_SUCCESS) {
  435. /* process connection callbacks */
  436. {
  437. struct ldapoptions *lo;
  438. ldaplist *ll;
  439. ldap_conncb *cb;
  440. lo = &ld->ld_options;
  441. LDAP_MUTEX_LOCK( &lo->ldo_mutex );
  442. if ( lo->ldo_conn_cbs ) {
  443. for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
  444. cb = ll->ll_data;
  445. cb->lc_del( ld, conn->lconn_sb, cb );
  446. }
  447. }
  448. LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
  449. lo = LDAP_INT_GLOBAL_OPT();
  450. LDAP_MUTEX_LOCK( &lo->ldo_mutex );
  451. if ( lo->ldo_conn_cbs ) {
  452. for ( ll=lo->ldo_conn_cbs; ll; ll=ll->ll_next ) {
  453. cb = ll->ll_data;
  454. cb->lc_del( ld, conn->lconn_sb, cb );
  455. }
  456. }
  457. LDAP_MUTEX_UNLOCK( &lo->ldo_mutex );
  458. }
  459. ber_int_sb_close( conn->lconn_sb );
  460. ber_int_sb_destroy( conn->lconn_sb );
  461. return -1;
  462. }
  463. }
  464. #endif
  465. return( 0 );
  466. }
  467. /*
  468. * ldap_open_internal_connection - open connection and set file descriptor
  469. *
  470. * note: ldap_init_fd() may be preferable
  471. */
  472. int
  473. ldap_open_internal_connection( LDAP **ldp, ber_socket_t *fdp )
  474. {
  475. int rc;
  476. LDAPConn *c;
  477. LDAPRequest *lr;
  478. LDAP *ld;
  479. rc = ldap_create( &ld );
  480. if( rc != LDAP_SUCCESS ) {
  481. *ldp = NULL;
  482. return( rc );
  483. }
  484. /* Make it appear that a search request, msgid 0, was sent */
  485. lr = (LDAPRequest *)LDAP_CALLOC( 1, sizeof( LDAPRequest ));
  486. if( lr == NULL ) {
  487. ldap_unbind_ext( ld, NULL, NULL );
  488. *ldp = NULL;
  489. return( LDAP_NO_MEMORY );
  490. }
  491. memset(lr, 0, sizeof( LDAPRequest ));
  492. lr->lr_msgid = 0;
  493. lr->lr_status = LDAP_REQST_INPROGRESS;
  494. lr->lr_res_errno = LDAP_SUCCESS;
  495. /* no mutex lock needed, we just created this ld here */
  496. rc = ldap_tavl_insert( &ld->ld_requests, lr, ldap_req_cmp, ldap_avl_dup_error );
  497. assert( rc == LDAP_SUCCESS );
  498. LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
  499. /* Attach the passed socket as the *LDAP's connection */
  500. c = ldap_new_connection( ld, NULL, 1, 0, NULL, 0, 0 );
  501. if( c == NULL ) {
  502. LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
  503. ldap_unbind_ext( ld, NULL, NULL );
  504. *ldp = NULL;
  505. return( LDAP_NO_MEMORY );
  506. }
  507. ber_sockbuf_ctrl( c->lconn_sb, LBER_SB_OPT_SET_FD, fdp );
  508. #ifdef LDAP_DEBUG
  509. ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_debug,
  510. LBER_SBIOD_LEVEL_PROVIDER, (void *)"int_" );
  511. #endif
  512. ber_sockbuf_add_io( c->lconn_sb, &ber_sockbuf_io_tcp,
  513. LBER_SBIOD_LEVEL_PROVIDER, NULL );
  514. ld->ld_defconn = c;
  515. LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
  516. /* Add the connection to the *LDAP's select pool */
  517. ldap_mark_select_read( ld, c->lconn_sb );
  518. /* Make this connection an LDAP V3 protocol connection */
  519. rc = LDAP_VERSION3;
  520. ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &rc );
  521. *ldp = ld;
  522. ++ld->ld_defconn->lconn_refcnt; /* so it never gets closed/freed */
  523. return( LDAP_SUCCESS );
  524. }
  525. LDAP *
  526. ldap_dup( LDAP *old )
  527. {
  528. LDAP *ld;
  529. if ( old == NULL ) {
  530. return( NULL );
  531. }
  532. Debug0( LDAP_DEBUG_TRACE, "ldap_dup\n" );
  533. if ( (ld = (LDAP *) LDAP_CALLOC( 1, sizeof(LDAP) )) == NULL ) {
  534. return( NULL );
  535. }
  536. LDAP_MUTEX_LOCK( &old->ld_ldcmutex );
  537. ld->ldc = old->ldc;
  538. old->ld_ldcrefcnt++;
  539. LDAP_MUTEX_UNLOCK( &old->ld_ldcmutex );
  540. return ( ld );
  541. }
  542. int
  543. ldap_int_check_async_open( LDAP *ld, ber_socket_t sd )
  544. {
  545. struct timeval tv = { 0 };
  546. int rc;
  547. rc = ldap_int_poll( ld, sd, &tv, 1 );
  548. switch ( rc ) {
  549. case 0:
  550. /* now ready to start tls */
  551. ld->ld_defconn->lconn_status = LDAP_CONNST_CONNECTED;
  552. break;
  553. default:
  554. ld->ld_errno = LDAP_CONNECT_ERROR;
  555. return -1;
  556. case -2:
  557. /* connect not completed yet */
  558. ld->ld_errno = LDAP_X_CONNECTING;
  559. return rc;
  560. }
  561. #ifdef HAVE_TLS
  562. if ( ld->ld_options.ldo_tls_mode == LDAP_OPT_X_TLS_HARD ||
  563. !strcmp( ld->ld_defconn->lconn_server->lud_scheme, "ldaps" )) {
  564. ++ld->ld_defconn->lconn_refcnt; /* avoid premature free */
  565. rc = ldap_int_tls_start( ld, ld->ld_defconn, ld->ld_defconn->lconn_server );
  566. --ld->ld_defconn->lconn_refcnt;
  567. }
  568. #endif
  569. return rc;
  570. }