version.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <[email protected]>, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "urldata.h"
  25. #include "vtls/vtls.h"
  26. #include "http2.h"
  27. #include "vssh/ssh.h"
  28. #include "quic.h"
  29. #include "curl_printf.h"
  30. #ifdef USE_ARES
  31. # if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
  32. defined(WIN32)
  33. # define CARES_STATICLIB
  34. # endif
  35. # include <ares.h>
  36. #endif
  37. #ifdef USE_LIBIDN2
  38. #include <idn2.h>
  39. #endif
  40. #ifdef USE_LIBPSL
  41. #include <libpsl.h>
  42. #endif
  43. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  44. #include <iconv.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 17 /* 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. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  122. char iconv_version[40]="iconv";
  123. #endif
  124. #ifdef USE_SSH
  125. char ssh_version[40];
  126. #endif
  127. #ifdef USE_NGHTTP2
  128. char h2_version[40];
  129. #endif
  130. #ifdef ENABLE_QUIC
  131. char h3_version[40];
  132. #endif
  133. #ifdef USE_LIBRTMP
  134. char rtmp_version[40];
  135. #endif
  136. #ifdef USE_HYPER
  137. char hyper_buf[30];
  138. #endif
  139. #ifdef USE_GSASL
  140. char gsasl_buf[30];
  141. #endif
  142. #ifdef USE_OPENLDAP
  143. char ldap_buf[30];
  144. #endif
  145. int i = 0;
  146. int j;
  147. #ifdef DEBUGBUILD
  148. /* Override version string when environment variable CURL_VERSION is set */
  149. const char *debugversion = getenv("CURL_VERSION");
  150. if(debugversion) {
  151. strncpy(out, debugversion, sizeof(out)-1);
  152. out[sizeof(out)-1] = '\0';
  153. return out;
  154. }
  155. #endif
  156. src[i++] = LIBCURL_NAME "/" LIBCURL_VERSION;
  157. #ifdef USE_SSL
  158. Curl_ssl_version(ssl_version, sizeof(ssl_version));
  159. src[i++] = ssl_version;
  160. #endif
  161. #ifdef HAVE_LIBZ
  162. msnprintf(z_version, sizeof(z_version), "zlib/%s", zlibVersion());
  163. src[i++] = z_version;
  164. #endif
  165. #ifdef HAVE_BROTLI
  166. brotli_version(&br_version[7], sizeof(br_version) - 7);
  167. src[i++] = br_version;
  168. #endif
  169. #ifdef HAVE_ZSTD
  170. zstd_version(&zst_version[5], sizeof(zst_version) - 5);
  171. src[i++] = zst_version;
  172. #endif
  173. #ifdef USE_ARES
  174. msnprintf(cares_version, sizeof(cares_version),
  175. "c-ares/%s", ares_version(NULL));
  176. src[i++] = cares_version;
  177. #endif
  178. #ifdef USE_LIBIDN2
  179. msnprintf(idn_version, sizeof(idn_version),
  180. "libidn2/%s", idn2_check_version(NULL));
  181. src[i++] = idn_version;
  182. #elif defined(USE_WIN32_IDN)
  183. src[i++] = (char *)"WinIDN";
  184. #endif
  185. #ifdef USE_LIBPSL
  186. msnprintf(psl_version, sizeof(psl_version), "libpsl/%s", psl_get_version());
  187. src[i++] = psl_version;
  188. #endif
  189. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  190. #ifdef _LIBICONV_VERSION
  191. msnprintf(iconv_version, sizeof(iconv_version), "iconv/%d.%d",
  192. _LIBICONV_VERSION >> 8, _LIBICONV_VERSION & 255);
  193. #else
  194. /* version unknown, let the default stand */
  195. #endif /* _LIBICONV_VERSION */
  196. src[i++] = iconv_version;
  197. #endif
  198. #ifdef USE_SSH
  199. Curl_ssh_version(ssh_version, sizeof(ssh_version));
  200. src[i++] = ssh_version;
  201. #endif
  202. #ifdef USE_NGHTTP2
  203. Curl_http2_ver(h2_version, sizeof(h2_version));
  204. src[i++] = h2_version;
  205. #endif
  206. #ifdef ENABLE_QUIC
  207. Curl_quic_ver(h3_version, sizeof(h3_version));
  208. src[i++] = h3_version;
  209. #endif
  210. #ifdef USE_LIBRTMP
  211. {
  212. char suff[2];
  213. if(RTMP_LIB_VERSION & 0xff) {
  214. suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
  215. suff[1] = '\0';
  216. }
  217. else
  218. suff[0] = '\0';
  219. msnprintf(rtmp_version, sizeof(rtmp_version), "librtmp/%d.%d%s",
  220. RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
  221. suff);
  222. src[i++] = rtmp_version;
  223. }
  224. #endif
  225. #ifdef USE_HYPER
  226. msnprintf(hyper_buf, sizeof(hyper_buf), "Hyper/%s", hyper_version());
  227. src[i++] = hyper_buf;
  228. #endif
  229. #ifdef USE_GSASL
  230. msnprintf(gsasl_buf, sizeof(gsasl_buf), "libgsasl/%s",
  231. gsasl_check_version(NULL));
  232. src[i++] = gsasl_buf;
  233. #endif
  234. #ifdef USE_OPENLDAP
  235. {
  236. LDAPAPIInfo api;
  237. api.ldapai_info_version = LDAP_API_INFO_VERSION;
  238. if(ldap_get_option(NULL, LDAP_OPT_API_INFO, &api) == LDAP_OPT_SUCCESS) {
  239. unsigned int patch = api.ldapai_vendor_version % 100;
  240. unsigned int major = api.ldapai_vendor_version / 10000;
  241. unsigned int minor =
  242. ((api.ldapai_vendor_version - major * 10000) - patch) / 100;
  243. msnprintf(ldap_buf, sizeof(ldap_buf), "%s/%u.%u.%u",
  244. api.ldapai_vendor_name, major, minor, patch);
  245. src[i++] = ldap_buf;
  246. ldap_memfree(api.ldapai_vendor_name);
  247. ber_memvfree((void **)api.ldapai_extensions);
  248. }
  249. }
  250. #endif
  251. DEBUGASSERT(i <= VERSION_PARTS);
  252. outp = &out[0];
  253. outlen = sizeof(out);
  254. for(j = 0; j < i; j++) {
  255. size_t n = strlen(src[j]);
  256. /* we need room for a space, the string and the final zero */
  257. if(outlen <= (n + 2))
  258. break;
  259. if(j) {
  260. /* prepend a space if not the first */
  261. *outp++ = ' ';
  262. outlen--;
  263. }
  264. memcpy(outp, src[j], n);
  265. outp += n;
  266. outlen -= n;
  267. }
  268. *outp = 0;
  269. return out;
  270. }
  271. /* data for curl_version_info
  272. Keep the list sorted alphabetically. It is also written so that each
  273. protocol line has its own #if line to make things easier on the eye.
  274. */
  275. static const char * const protocols[] = {
  276. #ifndef CURL_DISABLE_DICT
  277. "dict",
  278. #endif
  279. #ifndef CURL_DISABLE_FILE
  280. "file",
  281. #endif
  282. #ifndef CURL_DISABLE_FTP
  283. "ftp",
  284. #endif
  285. #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
  286. "ftps",
  287. #endif
  288. #ifndef CURL_DISABLE_GOPHER
  289. "gopher",
  290. #endif
  291. #if defined(USE_SSL) && !defined(CURL_DISABLE_GOPHER)
  292. "gophers",
  293. #endif
  294. #ifndef CURL_DISABLE_HTTP
  295. "http",
  296. #endif
  297. #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
  298. "https",
  299. #endif
  300. #ifndef CURL_DISABLE_IMAP
  301. "imap",
  302. #endif
  303. #if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)
  304. "imaps",
  305. #endif
  306. #ifndef CURL_DISABLE_LDAP
  307. "ldap",
  308. #if !defined(CURL_DISABLE_LDAPS) && \
  309. ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
  310. (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
  311. "ldaps",
  312. #endif
  313. #endif
  314. #ifndef CURL_DISABLE_MQTT
  315. "mqtt",
  316. #endif
  317. #ifndef CURL_DISABLE_POP3
  318. "pop3",
  319. #endif
  320. #if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)
  321. "pop3s",
  322. #endif
  323. #ifdef USE_LIBRTMP
  324. "rtmp",
  325. #endif
  326. #ifndef CURL_DISABLE_RTSP
  327. "rtsp",
  328. #endif
  329. #if defined(USE_SSH) && !defined(USE_WOLFSSH)
  330. "scp",
  331. #endif
  332. #ifdef USE_SSH
  333. "sftp",
  334. #endif
  335. #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) && \
  336. (SIZEOF_CURL_OFF_T > 4)
  337. "smb",
  338. # ifdef USE_SSL
  339. "smbs",
  340. # endif
  341. #endif
  342. #ifndef CURL_DISABLE_SMTP
  343. "smtp",
  344. #endif
  345. #if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)
  346. "smtps",
  347. #endif
  348. #ifndef CURL_DISABLE_TELNET
  349. "telnet",
  350. #endif
  351. #ifndef CURL_DISABLE_TFTP
  352. "tftp",
  353. #endif
  354. NULL
  355. };
  356. static curl_version_info_data version_info = {
  357. CURLVERSION_NOW,
  358. LIBCURL_VERSION,
  359. LIBCURL_VERSION_NUM,
  360. OS, /* as found by configure or set by hand at build-time */
  361. 0 /* features is 0 by default */
  362. #ifdef ENABLE_IPV6
  363. | CURL_VERSION_IPV6
  364. #endif
  365. #ifdef USE_SSL
  366. | CURL_VERSION_SSL
  367. #endif
  368. #ifdef USE_NTLM
  369. | CURL_VERSION_NTLM
  370. #endif
  371. #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
  372. defined(NTLM_WB_ENABLED)
  373. | CURL_VERSION_NTLM_WB
  374. #endif
  375. #ifdef USE_SPNEGO
  376. | CURL_VERSION_SPNEGO
  377. #endif
  378. #ifdef USE_KERBEROS5
  379. | CURL_VERSION_KERBEROS5
  380. #endif
  381. #ifdef HAVE_GSSAPI
  382. | CURL_VERSION_GSSAPI
  383. #endif
  384. #ifdef USE_WINDOWS_SSPI
  385. | CURL_VERSION_SSPI
  386. #endif
  387. #ifdef HAVE_LIBZ
  388. | CURL_VERSION_LIBZ
  389. #endif
  390. #ifdef DEBUGBUILD
  391. | CURL_VERSION_DEBUG
  392. #endif
  393. #ifdef CURLDEBUG
  394. | CURL_VERSION_CURLDEBUG
  395. #endif
  396. #ifdef CURLRES_ASYNCH
  397. | CURL_VERSION_ASYNCHDNS
  398. #endif
  399. #if (SIZEOF_CURL_OFF_T > 4) && \
  400. ( (SIZEOF_OFF_T > 4) || defined(USE_WIN32_LARGE_FILES) )
  401. | CURL_VERSION_LARGEFILE
  402. #endif
  403. #if defined(WIN32) && defined(UNICODE) && defined(_UNICODE)
  404. | CURL_VERSION_UNICODE
  405. #endif
  406. #if defined(CURL_DOES_CONVERSIONS)
  407. | CURL_VERSION_CONV
  408. #endif
  409. #if defined(USE_TLS_SRP)
  410. | CURL_VERSION_TLSAUTH_SRP
  411. #endif
  412. #if defined(USE_NGHTTP2) || defined(USE_HYPER)
  413. | CURL_VERSION_HTTP2
  414. #endif
  415. #if defined(ENABLE_QUIC)
  416. | CURL_VERSION_HTTP3
  417. #endif
  418. #if defined(USE_UNIX_SOCKETS)
  419. | CURL_VERSION_UNIX_SOCKETS
  420. #endif
  421. #if defined(USE_LIBPSL)
  422. | CURL_VERSION_PSL
  423. #endif
  424. #if defined(CURL_WITH_MULTI_SSL)
  425. | CURL_VERSION_MULTI_SSL
  426. #endif
  427. #if defined(HAVE_BROTLI)
  428. | CURL_VERSION_BROTLI
  429. #endif
  430. #if defined(HAVE_ZSTD)
  431. | CURL_VERSION_ZSTD
  432. #endif
  433. #ifndef CURL_DISABLE_ALTSVC
  434. | CURL_VERSION_ALTSVC
  435. #endif
  436. #ifndef CURL_DISABLE_HSTS
  437. | CURL_VERSION_HSTS
  438. #endif
  439. #if defined(USE_GSASL)
  440. | CURL_VERSION_GSASL
  441. #endif
  442. ,
  443. NULL, /* ssl_version */
  444. 0, /* ssl_version_num, this is kept at zero */
  445. NULL, /* zlib_version */
  446. protocols,
  447. NULL, /* c-ares version */
  448. 0, /* c-ares version numerical */
  449. NULL, /* libidn version */
  450. 0, /* iconv version */
  451. NULL, /* ssh lib version */
  452. 0, /* brotli_ver_num */
  453. NULL, /* brotli version */
  454. 0, /* nghttp2 version number */
  455. NULL, /* nghttp2 version string */
  456. NULL, /* quic library string */
  457. #ifdef CURL_CA_BUNDLE
  458. CURL_CA_BUNDLE, /* cainfo */
  459. #else
  460. NULL,
  461. #endif
  462. #ifdef CURL_CA_PATH
  463. CURL_CA_PATH, /* capath */
  464. #else
  465. NULL,
  466. #endif
  467. 0, /* zstd_ver_num */
  468. NULL, /* zstd version */
  469. NULL, /* Hyper version */
  470. NULL /* gsasl version */
  471. };
  472. curl_version_info_data *curl_version_info(CURLversion stamp)
  473. {
  474. #if defined(USE_SSH)
  475. static char ssh_buffer[80];
  476. #endif
  477. #ifdef USE_SSL
  478. #ifdef CURL_WITH_MULTI_SSL
  479. static char ssl_buffer[200];
  480. #else
  481. static char ssl_buffer[80];
  482. #endif
  483. #endif
  484. #ifdef HAVE_BROTLI
  485. static char brotli_buffer[80];
  486. #endif
  487. #ifdef HAVE_ZSTD
  488. static char zstd_buffer[80];
  489. #endif
  490. #ifdef USE_SSL
  491. Curl_ssl_version(ssl_buffer, sizeof(ssl_buffer));
  492. version_info.ssl_version = ssl_buffer;
  493. #ifndef CURL_DISABLE_PROXY
  494. if(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY)
  495. version_info.features |= CURL_VERSION_HTTPS_PROXY;
  496. else
  497. version_info.features &= ~CURL_VERSION_HTTPS_PROXY;
  498. #endif
  499. #endif
  500. #ifdef HAVE_LIBZ
  501. version_info.libz_version = zlibVersion();
  502. /* libz left NULL if non-existing */
  503. #endif
  504. #ifdef USE_ARES
  505. {
  506. int aresnum;
  507. version_info.ares = ares_version(&aresnum);
  508. version_info.ares_num = aresnum;
  509. }
  510. #endif
  511. #ifdef USE_LIBIDN2
  512. /* This returns a version string if we use the given version or later,
  513. otherwise it returns NULL */
  514. version_info.libidn = idn2_check_version(IDN2_VERSION);
  515. if(version_info.libidn)
  516. version_info.features |= CURL_VERSION_IDN;
  517. #elif defined(USE_WIN32_IDN)
  518. version_info.features |= CURL_VERSION_IDN;
  519. #endif
  520. #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
  521. #ifdef _LIBICONV_VERSION
  522. version_info.iconv_ver_num = _LIBICONV_VERSION;
  523. #else
  524. /* version unknown */
  525. version_info.iconv_ver_num = -1;
  526. #endif /* _LIBICONV_VERSION */
  527. #endif
  528. #if defined(USE_SSH)
  529. Curl_ssh_version(ssh_buffer, sizeof(ssh_buffer));
  530. version_info.libssh_version = ssh_buffer;
  531. #endif
  532. #ifdef HAVE_BROTLI
  533. version_info.brotli_ver_num = BrotliDecoderVersion();
  534. brotli_version(brotli_buffer, sizeof(brotli_buffer));
  535. version_info.brotli_version = brotli_buffer;
  536. #endif
  537. #ifdef HAVE_ZSTD
  538. version_info.zstd_ver_num = (unsigned int)ZSTD_versionNumber();
  539. zstd_version(zstd_buffer, sizeof(zstd_buffer));
  540. version_info.zstd_version = zstd_buffer;
  541. #endif
  542. #ifdef USE_NGHTTP2
  543. {
  544. nghttp2_info *h2 = nghttp2_version(0);
  545. version_info.nghttp2_ver_num = h2->version_num;
  546. version_info.nghttp2_version = h2->version_str;
  547. }
  548. #endif
  549. #ifdef ENABLE_QUIC
  550. {
  551. static char quicbuffer[80];
  552. Curl_quic_ver(quicbuffer, sizeof(quicbuffer));
  553. version_info.quic_version = quicbuffer;
  554. }
  555. #endif
  556. #ifdef USE_HYPER
  557. {
  558. static char hyper_buffer[30];
  559. msnprintf(hyper_buffer, sizeof(hyper_buffer), "Hyper/%s", hyper_version());
  560. version_info.hyper_version = hyper_buffer;
  561. }
  562. #endif
  563. #ifdef USE_GSASL
  564. {
  565. version_info.gsasl_version = gsasl_check_version(NULL);
  566. }
  567. #endif
  568. (void)stamp; /* avoid compiler warnings, we don't use this */
  569. return &version_info;
  570. }