version.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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_ZLIB_H
  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. #endif
  314. #ifndef CURL_DISABLE_RTSP
  315. "rtsp",
  316. #endif
  317. #if defined(USE_SSH) && !defined(USE_WOLFSSH)
  318. "scp",
  319. #endif
  320. #ifdef USE_SSH
  321. "sftp",
  322. #endif
  323. #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) && \
  324. (SIZEOF_CURL_OFF_T > 4)
  325. "smb",
  326. # ifdef USE_SSL
  327. "smbs",
  328. # endif
  329. #endif
  330. #ifndef CURL_DISABLE_SMTP
  331. "smtp",
  332. #endif
  333. #if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)
  334. "smtps",
  335. #endif
  336. #ifndef CURL_DISABLE_TELNET
  337. "telnet",
  338. #endif
  339. #ifndef CURL_DISABLE_TFTP
  340. "tftp",
  341. #endif
  342. #ifdef USE_WEBSOCKETS
  343. "ws",
  344. #endif
  345. #if defined(USE_SSL) && defined(USE_WEBSOCKETS)
  346. "wss",
  347. #endif
  348. NULL
  349. };
  350. static curl_version_info_data version_info = {
  351. CURLVERSION_NOW,
  352. LIBCURL_VERSION,
  353. LIBCURL_VERSION_NUM,
  354. OS, /* as found by configure or set by hand at build-time */
  355. 0 /* features is 0 by default */
  356. #ifdef ENABLE_IPV6
  357. | CURL_VERSION_IPV6
  358. #endif
  359. #ifdef USE_SSL
  360. | CURL_VERSION_SSL
  361. #endif
  362. #ifdef USE_NTLM
  363. | CURL_VERSION_NTLM
  364. #endif
  365. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  366. defined(NTLM_WB_ENABLED)
  367. | CURL_VERSION_NTLM_WB
  368. #endif
  369. #ifdef USE_SPNEGO
  370. | CURL_VERSION_SPNEGO
  371. #endif
  372. #ifdef USE_KERBEROS5
  373. | CURL_VERSION_KERBEROS5
  374. #endif
  375. #ifdef HAVE_GSSAPI
  376. | CURL_VERSION_GSSAPI
  377. #endif
  378. #ifdef USE_WINDOWS_SSPI
  379. | CURL_VERSION_SSPI
  380. #endif
  381. #ifdef HAVE_LIBZ
  382. | CURL_VERSION_LIBZ
  383. #endif
  384. #ifdef DEBUGBUILD
  385. | CURL_VERSION_DEBUG
  386. #endif
  387. #ifdef CURLDEBUG
  388. | CURL_VERSION_CURLDEBUG
  389. #endif
  390. #ifdef CURLRES_ASYNCH
  391. | CURL_VERSION_ASYNCHDNS
  392. #endif
  393. #if (SIZEOF_CURL_OFF_T > 4) && \
  394. ( (SIZEOF_OFF_T > 4) || defined(USE_WIN32_LARGE_FILES) )
  395. | CURL_VERSION_LARGEFILE
  396. #endif
  397. #if defined(WIN32) && defined(UNICODE) && defined(_UNICODE)
  398. | CURL_VERSION_UNICODE
  399. #endif
  400. #if defined(USE_TLS_SRP)
  401. | CURL_VERSION_TLSAUTH_SRP
  402. #endif
  403. #if defined(USE_NGHTTP2) || defined(USE_HYPER)
  404. | CURL_VERSION_HTTP2
  405. #endif
  406. #if defined(ENABLE_QUIC)
  407. | CURL_VERSION_HTTP3
  408. #endif
  409. #if defined(USE_UNIX_SOCKETS)
  410. | CURL_VERSION_UNIX_SOCKETS
  411. #endif
  412. #if defined(USE_LIBPSL)
  413. | CURL_VERSION_PSL
  414. #endif
  415. #if defined(CURL_WITH_MULTI_SSL)
  416. | CURL_VERSION_MULTI_SSL
  417. #endif
  418. #if defined(HAVE_BROTLI)
  419. | CURL_VERSION_BROTLI
  420. #endif
  421. #if defined(HAVE_ZSTD)
  422. | CURL_VERSION_ZSTD
  423. #endif
  424. #ifndef CURL_DISABLE_ALTSVC
  425. | CURL_VERSION_ALTSVC
  426. #endif
  427. #ifndef CURL_DISABLE_HSTS
  428. | CURL_VERSION_HSTS
  429. #endif
  430. #if defined(USE_GSASL)
  431. | CURL_VERSION_GSASL
  432. #endif
  433. #if defined(GLOBAL_INIT_IS_THREADSAFE)
  434. | CURL_VERSION_THREADSAFE
  435. #endif
  436. ,
  437. NULL, /* ssl_version */
  438. 0, /* ssl_version_num, this is kept at zero */
  439. NULL, /* zlib_version */
  440. protocols,
  441. NULL, /* c-ares version */
  442. 0, /* c-ares version numerical */
  443. NULL, /* libidn version */
  444. 0, /* iconv version */
  445. NULL, /* ssh lib version */
  446. 0, /* brotli_ver_num */
  447. NULL, /* brotli version */
  448. 0, /* nghttp2 version number */
  449. NULL, /* nghttp2 version string */
  450. NULL, /* quic library string */
  451. #ifdef CURL_CA_BUNDLE
  452. CURL_CA_BUNDLE, /* cainfo */
  453. #else
  454. NULL,
  455. #endif
  456. #ifdef CURL_CA_PATH
  457. CURL_CA_PATH, /* capath */
  458. #else
  459. NULL,
  460. #endif
  461. 0, /* zstd_ver_num */
  462. NULL, /* zstd version */
  463. NULL, /* Hyper version */
  464. NULL /* gsasl version */
  465. };
  466. curl_version_info_data *curl_version_info(CURLversion stamp)
  467. {
  468. #if defined(USE_SSH)
  469. static char ssh_buffer[80];
  470. #endif
  471. #ifdef USE_SSL
  472. #ifdef CURL_WITH_MULTI_SSL
  473. static char ssl_buffer[200];
  474. #else
  475. static char ssl_buffer[80];
  476. #endif
  477. #endif
  478. #ifdef HAVE_BROTLI
  479. static char brotli_buffer[80];
  480. #endif
  481. #ifdef HAVE_ZSTD
  482. static char zstd_buffer[80];
  483. #endif
  484. #ifdef USE_SSL
  485. Curl_ssl_version(ssl_buffer, sizeof(ssl_buffer));
  486. version_info.ssl_version = ssl_buffer;
  487. #ifndef CURL_DISABLE_PROXY
  488. if(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY)
  489. version_info.features |= CURL_VERSION_HTTPS_PROXY;
  490. else
  491. version_info.features &= ~CURL_VERSION_HTTPS_PROXY;
  492. #endif
  493. #endif
  494. #ifdef HAVE_LIBZ
  495. version_info.libz_version = zlibVersion();
  496. /* libz left NULL if non-existing */
  497. #endif
  498. #ifdef USE_ARES
  499. {
  500. int aresnum;
  501. version_info.ares = ares_version(&aresnum);
  502. version_info.ares_num = aresnum;
  503. }
  504. #endif
  505. #ifdef USE_LIBIDN2
  506. /* This returns a version string if we use the given version or later,
  507. otherwise it returns NULL */
  508. version_info.libidn = idn2_check_version(IDN2_VERSION);
  509. if(version_info.libidn)
  510. version_info.features |= CURL_VERSION_IDN;
  511. #elif defined(USE_WIN32_IDN)
  512. version_info.features |= CURL_VERSION_IDN;
  513. #endif
  514. #if defined(USE_SSH)
  515. Curl_ssh_version(ssh_buffer, sizeof(ssh_buffer));
  516. version_info.libssh_version = ssh_buffer;
  517. #endif
  518. #ifdef HAVE_BROTLI
  519. version_info.brotli_ver_num = BrotliDecoderVersion();
  520. brotli_version(brotli_buffer, sizeof(brotli_buffer));
  521. version_info.brotli_version = brotli_buffer;
  522. #endif
  523. #ifdef HAVE_ZSTD
  524. version_info.zstd_ver_num = (unsigned int)ZSTD_versionNumber();
  525. zstd_version(zstd_buffer, sizeof(zstd_buffer));
  526. version_info.zstd_version = zstd_buffer;
  527. #endif
  528. #ifdef USE_NGHTTP2
  529. {
  530. nghttp2_info *h2 = nghttp2_version(0);
  531. version_info.nghttp2_ver_num = h2->version_num;
  532. version_info.nghttp2_version = h2->version_str;
  533. }
  534. #endif
  535. #ifdef ENABLE_QUIC
  536. {
  537. static char quicbuffer[80];
  538. Curl_quic_ver(quicbuffer, sizeof(quicbuffer));
  539. version_info.quic_version = quicbuffer;
  540. }
  541. #endif
  542. #ifdef USE_HYPER
  543. {
  544. static char hyper_buffer[30];
  545. msnprintf(hyper_buffer, sizeof(hyper_buffer), "Hyper/%s", hyper_version());
  546. version_info.hyper_version = hyper_buffer;
  547. }
  548. #endif
  549. #ifdef USE_GSASL
  550. {
  551. version_info.gsasl_version = gsasl_check_version(NULL);
  552. }
  553. #endif
  554. (void)stamp; /* avoid compiler warnings, we don't use this */
  555. return &version_info;
  556. }