2
0

version.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "urldata.h"
  27. #include "vtls/vtls.h"
  28. #include "http2.h"
  29. #include "vssh/ssh.h"
  30. #include "quic.h"
  31. #include "curl_printf.h"
  32. #include "easy_lock.h"
  33. #ifdef USE_ARES
  34. # if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
  35. defined(WIN32)
  36. # define CARES_STATICLIB
  37. # endif
  38. # include <ares.h>
  39. #endif
  40. #ifdef USE_LIBIDN2
  41. #include <idn2.h>
  42. #endif
  43. #ifdef USE_LIBPSL
  44. #include <libpsl.h>
  45. #endif
  46. #ifdef USE_LIBRTMP
  47. #include <librtmp/rtmp.h>
  48. #endif
  49. #ifdef HAVE_LIBZ
  50. #include <zlib.h>
  51. #endif
  52. #ifdef HAVE_BROTLI
  53. #include <brotli/decode.h>
  54. #endif
  55. #ifdef HAVE_ZSTD
  56. #include <zstd.h>
  57. #endif
  58. #ifdef USE_GSASL
  59. #include <gsasl.h>
  60. #endif
  61. #ifdef USE_OPENLDAP
  62. #include <ldap.h>
  63. #endif
  64. #ifdef HAVE_BROTLI
  65. static void brotli_version(char *buf, size_t bufsz)
  66. {
  67. uint32_t brotli_version = BrotliDecoderVersion();
  68. unsigned int major = brotli_version >> 24;
  69. unsigned int minor = (brotli_version & 0x00FFFFFF) >> 12;
  70. unsigned int patch = brotli_version & 0x00000FFF;
  71. (void)msnprintf(buf, bufsz, "%u.%u.%u", major, minor, patch);
  72. }
  73. #endif
  74. #ifdef HAVE_ZSTD
  75. static void zstd_version(char *buf, size_t bufsz)
  76. {
  77. unsigned long zstd_version = (unsigned long)ZSTD_versionNumber();
  78. unsigned int major = (unsigned int)(zstd_version / (100 * 100));
  79. unsigned int minor = (unsigned int)((zstd_version -
  80. (major * 100 * 100)) / 100);
  81. unsigned int patch = (unsigned int)(zstd_version -
  82. (major * 100 * 100) - (minor * 100));
  83. (void)msnprintf(buf, bufsz, "%u.%u.%u", major, minor, patch);
  84. }
  85. #endif
  86. /*
  87. * curl_version() returns a pointer to a static buffer.
  88. *
  89. * It is implemented to work multi-threaded by making sure repeated invokes
  90. * generate the exact same string and never write any temporary data like
  91. * zeros in the data.
  92. */
  93. #define VERSION_PARTS 16 /* number of substrings we can concatenate */
  94. char *curl_version(void)
  95. {
  96. static char out[300];
  97. char *outp;
  98. size_t outlen;
  99. const char *src[VERSION_PARTS];
  100. #ifdef USE_SSL
  101. char ssl_version[200];
  102. #endif
  103. #ifdef HAVE_LIBZ
  104. char z_version[40];
  105. #endif
  106. #ifdef HAVE_BROTLI
  107. char br_version[40] = "brotli/";
  108. #endif
  109. #ifdef HAVE_ZSTD
  110. char zst_version[40] = "zstd/";
  111. #endif
  112. #ifdef USE_ARES
  113. char cares_version[40];
  114. #endif
  115. #if defined(USE_LIBIDN2)
  116. char idn_version[40];
  117. #endif
  118. #ifdef USE_LIBPSL
  119. char psl_version[40];
  120. #endif
  121. #ifdef USE_SSH
  122. char ssh_version[40];
  123. #endif
  124. #ifdef USE_NGHTTP2
  125. char h2_version[40];
  126. #endif
  127. #ifdef ENABLE_QUIC
  128. char h3_version[40];
  129. #endif
  130. #ifdef USE_LIBRTMP
  131. char rtmp_version[40];
  132. #endif
  133. #ifdef USE_HYPER
  134. char hyper_buf[30];
  135. #endif
  136. #ifdef USE_GSASL
  137. char gsasl_buf[30];
  138. #endif
  139. #ifdef USE_OPENLDAP
  140. char ldap_buf[30];
  141. #endif
  142. int i = 0;
  143. int j;
  144. #ifdef DEBUGBUILD
  145. /* Override version string when environment variable CURL_VERSION is set */
  146. const char *debugversion = getenv("CURL_VERSION");
  147. if(debugversion) {
  148. strncpy(out, debugversion, sizeof(out)-1);
  149. out[sizeof(out)-1] = '\0';
  150. return out;
  151. }
  152. #endif
  153. src[i++] = LIBCURL_NAME "/" LIBCURL_VERSION;
  154. #ifdef USE_SSL
  155. Curl_ssl_version(ssl_version, sizeof(ssl_version));
  156. src[i++] = ssl_version;
  157. #endif
  158. #ifdef HAVE_LIBZ
  159. msnprintf(z_version, sizeof(z_version), "zlib/%s", zlibVersion());
  160. src[i++] = z_version;
  161. #endif
  162. #ifdef HAVE_BROTLI
  163. brotli_version(&br_version[7], sizeof(br_version) - 7);
  164. src[i++] = br_version;
  165. #endif
  166. #ifdef HAVE_ZSTD
  167. zstd_version(&zst_version[5], sizeof(zst_version) - 5);
  168. src[i++] = zst_version;
  169. #endif
  170. #ifdef USE_ARES
  171. msnprintf(cares_version, sizeof(cares_version),
  172. "c-ares/%s", ares_version(NULL));
  173. src[i++] = cares_version;
  174. #endif
  175. #ifdef USE_LIBIDN2
  176. msnprintf(idn_version, sizeof(idn_version),
  177. "libidn2/%s", idn2_check_version(NULL));
  178. src[i++] = idn_version;
  179. #elif defined(USE_WIN32_IDN)
  180. src[i++] = (char *)"WinIDN";
  181. #endif
  182. #ifdef USE_LIBPSL
  183. msnprintf(psl_version, sizeof(psl_version), "libpsl/%s", psl_get_version());
  184. src[i++] = psl_version;
  185. #endif
  186. #ifdef USE_SSH
  187. Curl_ssh_version(ssh_version, sizeof(ssh_version));
  188. src[i++] = ssh_version;
  189. #endif
  190. #ifdef USE_NGHTTP2
  191. Curl_http2_ver(h2_version, sizeof(h2_version));
  192. src[i++] = h2_version;
  193. #endif
  194. #ifdef ENABLE_QUIC
  195. Curl_quic_ver(h3_version, sizeof(h3_version));
  196. src[i++] = h3_version;
  197. #endif
  198. #ifdef USE_LIBRTMP
  199. {
  200. char suff[2];
  201. if(RTMP_LIB_VERSION & 0xff) {
  202. suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
  203. suff[1] = '\0';
  204. }
  205. else
  206. suff[0] = '\0';
  207. msnprintf(rtmp_version, sizeof(rtmp_version), "librtmp/%d.%d%s",
  208. RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
  209. suff);
  210. src[i++] = rtmp_version;
  211. }
  212. #endif
  213. #ifdef USE_HYPER
  214. msnprintf(hyper_buf, sizeof(hyper_buf), "Hyper/%s", hyper_version());
  215. src[i++] = hyper_buf;
  216. #endif
  217. #ifdef USE_GSASL
  218. msnprintf(gsasl_buf, sizeof(gsasl_buf), "libgsasl/%s",
  219. gsasl_check_version(NULL));
  220. src[i++] = gsasl_buf;
  221. #endif
  222. #ifdef USE_OPENLDAP
  223. {
  224. LDAPAPIInfo api;
  225. api.ldapai_info_version = LDAP_API_INFO_VERSION;
  226. if(ldap_get_option(NULL, LDAP_OPT_API_INFO, &api) == LDAP_OPT_SUCCESS) {
  227. unsigned int patch = api.ldapai_vendor_version % 100;
  228. unsigned int major = api.ldapai_vendor_version / 10000;
  229. unsigned int minor =
  230. ((api.ldapai_vendor_version - major * 10000) - patch) / 100;
  231. msnprintf(ldap_buf, sizeof(ldap_buf), "%s/%u.%u.%u",
  232. api.ldapai_vendor_name, major, minor, patch);
  233. src[i++] = ldap_buf;
  234. ldap_memfree(api.ldapai_vendor_name);
  235. ber_memvfree((void **)api.ldapai_extensions);
  236. }
  237. }
  238. #endif
  239. DEBUGASSERT(i <= VERSION_PARTS);
  240. outp = &out[0];
  241. outlen = sizeof(out);
  242. for(j = 0; j < i; j++) {
  243. size_t n = strlen(src[j]);
  244. /* we need room for a space, the string and the final zero */
  245. if(outlen <= (n + 2))
  246. break;
  247. if(j) {
  248. /* prepend a space if not the first */
  249. *outp++ = ' ';
  250. outlen--;
  251. }
  252. memcpy(outp, src[j], n);
  253. outp += n;
  254. outlen -= n;
  255. }
  256. *outp = 0;
  257. return out;
  258. }
  259. /* data for curl_version_info
  260. Keep the list sorted alphabetically. It is also written so that each
  261. protocol line has its own #if line to make things easier on the eye.
  262. */
  263. static const char * const protocols[] = {
  264. #ifndef CURL_DISABLE_DICT
  265. "dict",
  266. #endif
  267. #ifndef CURL_DISABLE_FILE
  268. "file",
  269. #endif
  270. #ifndef CURL_DISABLE_FTP
  271. "ftp",
  272. #endif
  273. #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
  274. "ftps",
  275. #endif
  276. #ifndef CURL_DISABLE_GOPHER
  277. "gopher",
  278. #endif
  279. #if defined(USE_SSL) && !defined(CURL_DISABLE_GOPHER)
  280. "gophers",
  281. #endif
  282. #ifndef CURL_DISABLE_HTTP
  283. "http",
  284. #endif
  285. #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
  286. "https",
  287. #endif
  288. #ifndef CURL_DISABLE_IMAP
  289. "imap",
  290. #endif
  291. #if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)
  292. "imaps",
  293. #endif
  294. #ifndef CURL_DISABLE_LDAP
  295. "ldap",
  296. #if !defined(CURL_DISABLE_LDAPS) && \
  297. ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
  298. (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
  299. "ldaps",
  300. #endif
  301. #endif
  302. #ifndef CURL_DISABLE_MQTT
  303. "mqtt",
  304. #endif
  305. #ifndef CURL_DISABLE_POP3
  306. "pop3",
  307. #endif
  308. #if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)
  309. "pop3s",
  310. #endif
  311. #ifdef USE_LIBRTMP
  312. "rtmp",
  313. "rtmpe",
  314. "rtmps",
  315. "rtmpt",
  316. "rtmpte",
  317. "rtmpts",
  318. #endif
  319. #ifndef CURL_DISABLE_RTSP
  320. "rtsp",
  321. #endif
  322. #if defined(USE_SSH) && !defined(USE_WOLFSSH)
  323. "scp",
  324. #endif
  325. #ifdef USE_SSH
  326. "sftp",
  327. #endif
  328. #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) && \
  329. (SIZEOF_CURL_OFF_T > 4)
  330. "smb",
  331. # ifdef USE_SSL
  332. "smbs",
  333. # endif
  334. #endif
  335. #ifndef CURL_DISABLE_SMTP
  336. "smtp",
  337. #endif
  338. #if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)
  339. "smtps",
  340. #endif
  341. #ifndef CURL_DISABLE_TELNET
  342. "telnet",
  343. #endif
  344. #ifndef CURL_DISABLE_TFTP
  345. "tftp",
  346. #endif
  347. #ifdef USE_WEBSOCKETS
  348. "ws",
  349. #endif
  350. #if defined(USE_SSL) && defined(USE_WEBSOCKETS)
  351. "wss",
  352. #endif
  353. NULL
  354. };
  355. static curl_version_info_data version_info = {
  356. CURLVERSION_NOW,
  357. LIBCURL_VERSION,
  358. LIBCURL_VERSION_NUM,
  359. OS, /* as found by configure or set by hand at build-time */
  360. 0 /* features is 0 by default */
  361. #ifdef ENABLE_IPV6
  362. | CURL_VERSION_IPV6
  363. #endif
  364. #ifdef USE_SSL
  365. | CURL_VERSION_SSL
  366. #endif
  367. #ifdef USE_NTLM
  368. | CURL_VERSION_NTLM
  369. #endif
  370. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  371. defined(NTLM_WB_ENABLED)
  372. | CURL_VERSION_NTLM_WB
  373. #endif
  374. #ifdef USE_SPNEGO
  375. | CURL_VERSION_SPNEGO
  376. #endif
  377. #ifdef USE_KERBEROS5
  378. | CURL_VERSION_KERBEROS5
  379. #endif
  380. #ifdef HAVE_GSSAPI
  381. | CURL_VERSION_GSSAPI
  382. #endif
  383. #ifdef USE_WINDOWS_SSPI
  384. | CURL_VERSION_SSPI
  385. #endif
  386. #ifdef HAVE_LIBZ
  387. | CURL_VERSION_LIBZ
  388. #endif
  389. #ifdef DEBUGBUILD
  390. | CURL_VERSION_DEBUG
  391. #endif
  392. #ifdef CURLDEBUG
  393. | CURL_VERSION_CURLDEBUG
  394. #endif
  395. #ifdef CURLRES_ASYNCH
  396. | CURL_VERSION_ASYNCHDNS
  397. #endif
  398. #if (SIZEOF_CURL_OFF_T > 4) && \
  399. ( (SIZEOF_OFF_T > 4) || defined(USE_WIN32_LARGE_FILES) )
  400. | CURL_VERSION_LARGEFILE
  401. #endif
  402. #if defined(WIN32) && defined(UNICODE) && defined(_UNICODE)
  403. | CURL_VERSION_UNICODE
  404. #endif
  405. #if defined(USE_TLS_SRP)
  406. | CURL_VERSION_TLSAUTH_SRP
  407. #endif
  408. #if defined(USE_NGHTTP2) || defined(USE_HYPER)
  409. | CURL_VERSION_HTTP2
  410. #endif
  411. #if defined(ENABLE_QUIC)
  412. | CURL_VERSION_HTTP3
  413. #endif
  414. #if defined(USE_UNIX_SOCKETS)
  415. | CURL_VERSION_UNIX_SOCKETS
  416. #endif
  417. #if defined(USE_LIBPSL)
  418. | CURL_VERSION_PSL
  419. #endif
  420. #if defined(CURL_WITH_MULTI_SSL)
  421. | CURL_VERSION_MULTI_SSL
  422. #endif
  423. #if defined(HAVE_BROTLI)
  424. | CURL_VERSION_BROTLI
  425. #endif
  426. #if defined(HAVE_ZSTD)
  427. | CURL_VERSION_ZSTD
  428. #endif
  429. #ifndef CURL_DISABLE_ALTSVC
  430. | CURL_VERSION_ALTSVC
  431. #endif
  432. #ifndef CURL_DISABLE_HSTS
  433. | CURL_VERSION_HSTS
  434. #endif
  435. #if defined(USE_GSASL)
  436. | CURL_VERSION_GSASL
  437. #endif
  438. #if defined(GLOBAL_INIT_IS_THREADSAFE)
  439. | CURL_VERSION_THREADSAFE
  440. #endif
  441. ,
  442. NULL, /* ssl_version */
  443. 0, /* ssl_version_num, this is kept at zero */
  444. NULL, /* zlib_version */
  445. protocols,
  446. NULL, /* c-ares version */
  447. 0, /* c-ares version numerical */
  448. NULL, /* libidn version */
  449. 0, /* iconv version */
  450. NULL, /* ssh lib version */
  451. 0, /* brotli_ver_num */
  452. NULL, /* brotli version */
  453. 0, /* nghttp2 version number */
  454. NULL, /* nghttp2 version string */
  455. NULL, /* quic library string */
  456. #ifdef CURL_CA_BUNDLE
  457. CURL_CA_BUNDLE, /* cainfo */
  458. #else
  459. NULL,
  460. #endif
  461. #ifdef CURL_CA_PATH
  462. CURL_CA_PATH, /* capath */
  463. #else
  464. NULL,
  465. #endif
  466. 0, /* zstd_ver_num */
  467. NULL, /* zstd version */
  468. NULL, /* Hyper version */
  469. NULL /* gsasl version */
  470. };
  471. curl_version_info_data *curl_version_info(CURLversion stamp)
  472. {
  473. #if defined(USE_SSH)
  474. static char ssh_buffer[80];
  475. #endif
  476. #ifdef USE_SSL
  477. #ifdef CURL_WITH_MULTI_SSL
  478. static char ssl_buffer[200];
  479. #else
  480. static char ssl_buffer[80];
  481. #endif
  482. #endif
  483. #ifdef HAVE_BROTLI
  484. static char brotli_buffer[80];
  485. #endif
  486. #ifdef HAVE_ZSTD
  487. static char zstd_buffer[80];
  488. #endif
  489. #ifdef USE_SSL
  490. Curl_ssl_version(ssl_buffer, sizeof(ssl_buffer));
  491. version_info.ssl_version = ssl_buffer;
  492. #ifndef CURL_DISABLE_PROXY
  493. if(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY)
  494. version_info.features |= CURL_VERSION_HTTPS_PROXY;
  495. else
  496. version_info.features &= ~CURL_VERSION_HTTPS_PROXY;
  497. #endif
  498. #endif
  499. #ifdef HAVE_LIBZ
  500. version_info.libz_version = zlibVersion();
  501. /* libz left NULL if non-existing */
  502. #endif
  503. #ifdef USE_ARES
  504. {
  505. int aresnum;
  506. version_info.ares = ares_version(&aresnum);
  507. version_info.ares_num = aresnum;
  508. }
  509. #endif
  510. #ifdef USE_LIBIDN2
  511. /* This returns a version string if we use the given version or later,
  512. otherwise it returns NULL */
  513. version_info.libidn = idn2_check_version(IDN2_VERSION);
  514. if(version_info.libidn)
  515. version_info.features |= CURL_VERSION_IDN;
  516. #elif defined(USE_WIN32_IDN)
  517. version_info.features |= CURL_VERSION_IDN;
  518. #endif
  519. #if defined(USE_SSH)
  520. Curl_ssh_version(ssh_buffer, sizeof(ssh_buffer));
  521. version_info.libssh_version = ssh_buffer;
  522. #endif
  523. #ifdef HAVE_BROTLI
  524. version_info.brotli_ver_num = BrotliDecoderVersion();
  525. brotli_version(brotli_buffer, sizeof(brotli_buffer));
  526. version_info.brotli_version = brotli_buffer;
  527. #endif
  528. #ifdef HAVE_ZSTD
  529. version_info.zstd_ver_num = (unsigned int)ZSTD_versionNumber();
  530. zstd_version(zstd_buffer, sizeof(zstd_buffer));
  531. version_info.zstd_version = zstd_buffer;
  532. #endif
  533. #ifdef USE_NGHTTP2
  534. {
  535. nghttp2_info *h2 = nghttp2_version(0);
  536. version_info.nghttp2_ver_num = h2->version_num;
  537. version_info.nghttp2_version = h2->version_str;
  538. }
  539. #endif
  540. #ifdef ENABLE_QUIC
  541. {
  542. static char quicbuffer[80];
  543. Curl_quic_ver(quicbuffer, sizeof(quicbuffer));
  544. version_info.quic_version = quicbuffer;
  545. }
  546. #endif
  547. #ifdef USE_HYPER
  548. {
  549. static char hyper_buffer[30];
  550. msnprintf(hyper_buffer, sizeof(hyper_buffer), "Hyper/%s", hyper_version());
  551. version_info.hyper_version = hyper_buffer;
  552. }
  553. #endif
  554. #ifdef USE_GSASL
  555. {
  556. version_info.gsasl_version = gsasl_check_version(NULL);
  557. }
  558. #endif
  559. (void)stamp; /* avoid compiler warnings, we don't use this */
  560. return &version_info;
  561. }