version.c 13 KB

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