1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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
25 #include "curl_setup.h"
26
27 #ifdef USE_NGHTTP2
28 #include <nghttp2/nghttp2.h>
29 #endif
30
31 #include <curl/curl.h>
32 #include "urldata.h"
33 #include "vtls/vtls.h"
34 #include "http2.h"
35 #include "vssh/ssh.h"
36 #include "vquic/vquic.h"
37 #include "curl_printf.h"
38 #include "easy_lock.h"
39
40 #ifdef USE_ARES
41 # if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
42 defined(_WIN32)
43 # define CARES_STATICLIB
44 # endif
45 # include <ares.h>
46 #endif
47
48 #ifdef USE_LIBIDN2
49 #include <idn2.h>
50 #endif
51
52 #ifdef USE_LIBPSL
53 #include <libpsl.h>
54 #endif
55
56 #ifdef USE_LIBRTMP
57 #include <librtmp/rtmp.h>
58 #endif
59
60 #ifdef HAVE_LIBZ
61 #include <zlib.h>
62 #endif
63
64 #ifdef HAVE_BROTLI
65 #if defined(__GNUC__)
66 /* Ignore -Wvla warnings in brotli headers */
67 #pragma GCC diagnostic push
68 #pragma GCC diagnostic ignored "-Wvla"
69 #endif
70 #include <brotli/decode.h>
71 #if defined(__GNUC__)
72 #pragma GCC diagnostic pop
73 #endif
74 #endif
75
76 #ifdef HAVE_ZSTD
77 #include <zstd.h>
78 #endif
79
80 #ifdef USE_GSASL
81 #include <gsasl.h>
82 #endif
83
84 #ifdef USE_OPENLDAP
85 #include <ldap.h>
86 #endif
87
88 #ifdef HAVE_BROTLI
brotli_version(char * buf,size_t bufsz)89 static void brotli_version(char *buf, size_t bufsz)
90 {
91 uint32_t brotli_version = BrotliDecoderVersion();
92 unsigned int major = brotli_version >> 24;
93 unsigned int minor = (brotli_version & 0x00FFFFFF) >> 12;
94 unsigned int patch = brotli_version & 0x00000FFF;
95 (void)msnprintf(buf, bufsz, "%u.%u.%u", major, minor, patch);
96 }
97 #endif
98
99 #ifdef HAVE_ZSTD
zstd_version(char * buf,size_t bufsz)100 static void zstd_version(char *buf, size_t bufsz)
101 {
102 unsigned long zstd_version = (unsigned long)ZSTD_versionNumber();
103 unsigned int major = (unsigned int)(zstd_version / (100 * 100));
104 unsigned int minor = (unsigned int)((zstd_version -
105 (major * 100 * 100)) / 100);
106 unsigned int patch = (unsigned int)(zstd_version -
107 (major * 100 * 100) - (minor * 100));
108 (void)msnprintf(buf, bufsz, "%u.%u.%u", major, minor, patch);
109 }
110 #endif
111
112 /*
113 * curl_version() returns a pointer to a static buffer.
114 *
115 * It is implemented to work multi-threaded by making sure repeated invokes
116 * generate the exact same string and never write any temporary data like
117 * zeros in the data.
118 */
119
120 #define VERSION_PARTS 16 /* number of substrings we can concatenate */
121
curl_version(void)122 char *curl_version(void)
123 {
124 static char out[300];
125 char *outp;
126 size_t outlen;
127 const char *src[VERSION_PARTS];
128 #ifdef USE_SSL
129 char ssl_version[200];
130 #endif
131 #ifdef HAVE_LIBZ
132 char z_version[40];
133 #endif
134 #ifdef HAVE_BROTLI
135 char br_version[40] = "brotli/";
136 #endif
137 #ifdef HAVE_ZSTD
138 char zst_version[40] = "zstd/";
139 #endif
140 #ifdef USE_ARES
141 char cares_version[40];
142 #endif
143 #if defined(USE_LIBIDN2)
144 char idn_version[40];
145 #endif
146 #ifdef USE_LIBPSL
147 char psl_version[40];
148 #endif
149 #ifdef USE_SSH
150 char ssh_version[40];
151 #endif
152 #ifdef USE_NGHTTP2
153 char h2_version[40];
154 #endif
155 #ifdef ENABLE_QUIC
156 char h3_version[40];
157 #endif
158 #ifdef USE_LIBRTMP
159 char rtmp_version[40];
160 #endif
161 #ifdef USE_HYPER
162 char hyper_buf[30];
163 #endif
164 #ifdef USE_GSASL
165 char gsasl_buf[30];
166 #endif
167 #ifdef USE_OPENLDAP
168 char ldap_buf[30];
169 #endif
170 int i = 0;
171 int j;
172
173 #ifdef DEBUGBUILD
174 /* Override version string when environment variable CURL_VERSION is set */
175 const char *debugversion = getenv("CURL_VERSION");
176 if(debugversion) {
177 strncpy(out, debugversion, sizeof(out)-1);
178 out[sizeof(out)-1] = '\0';
179 return out;
180 }
181 #endif
182
183 src[i++] = LIBCURL_NAME "/" LIBCURL_VERSION;
184 #ifdef USE_SSL
185 Curl_ssl_version(ssl_version, sizeof(ssl_version));
186 src[i++] = ssl_version;
187 #endif
188 #ifdef HAVE_LIBZ
189 msnprintf(z_version, sizeof(z_version), "zlib/%s", zlibVersion());
190 src[i++] = z_version;
191 #endif
192 #ifdef HAVE_BROTLI
193 brotli_version(&br_version[7], sizeof(br_version) - 7);
194 src[i++] = br_version;
195 #endif
196 #ifdef HAVE_ZSTD
197 zstd_version(&zst_version[5], sizeof(zst_version) - 5);
198 src[i++] = zst_version;
199 #endif
200 #ifdef USE_ARES
201 msnprintf(cares_version, sizeof(cares_version),
202 "c-ares/%s", ares_version(NULL));
203 src[i++] = cares_version;
204 #endif
205 #ifdef USE_LIBIDN2
206 msnprintf(idn_version, sizeof(idn_version),
207 "libidn2/%s", idn2_check_version(NULL));
208 src[i++] = idn_version;
209 #elif defined(USE_WIN32_IDN)
210 src[i++] = (char *)"WinIDN";
211 #endif
212
213 #ifdef USE_LIBPSL
214 {
215 int num = psl_check_version_number(0);
216 msnprintf(psl_version, sizeof(psl_version), "libpsl/%d.%d.%d",
217 num >> 16, (num >> 8) & 0xff, num & 0xff);
218 src[i++] = psl_version;
219 }
220 #endif
221
222 #ifdef USE_SSH
223 Curl_ssh_version(ssh_version, sizeof(ssh_version));
224 src[i++] = ssh_version;
225 #endif
226 #ifdef USE_NGHTTP2
227 Curl_http2_ver(h2_version, sizeof(h2_version));
228 src[i++] = h2_version;
229 #endif
230 #ifdef ENABLE_QUIC
231 Curl_quic_ver(h3_version, sizeof(h3_version));
232 src[i++] = h3_version;
233 #endif
234 #ifdef USE_LIBRTMP
235 {
236 char suff[2];
237 if(RTMP_LIB_VERSION & 0xff) {
238 suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
239 suff[1] = '\0';
240 }
241 else
242 suff[0] = '\0';
243
244 msnprintf(rtmp_version, sizeof(rtmp_version), "librtmp/%d.%d%s",
245 RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
246 suff);
247 src[i++] = rtmp_version;
248 }
249 #endif
250 #ifdef USE_HYPER
251 msnprintf(hyper_buf, sizeof(hyper_buf), "Hyper/%s", hyper_version());
252 src[i++] = hyper_buf;
253 #endif
254 #ifdef USE_GSASL
255 msnprintf(gsasl_buf, sizeof(gsasl_buf), "libgsasl/%s",
256 gsasl_check_version(NULL));
257 src[i++] = gsasl_buf;
258 #endif
259 #ifdef USE_OPENLDAP
260 {
261 LDAPAPIInfo api;
262 api.ldapai_info_version = LDAP_API_INFO_VERSION;
263
264 if(ldap_get_option(NULL, LDAP_OPT_API_INFO, &api) == LDAP_OPT_SUCCESS) {
265 unsigned int patch = api.ldapai_vendor_version % 100;
266 unsigned int major = api.ldapai_vendor_version / 10000;
267 unsigned int minor =
268 ((api.ldapai_vendor_version - major * 10000) - patch) / 100;
269 msnprintf(ldap_buf, sizeof(ldap_buf), "%s/%u.%u.%u",
270 api.ldapai_vendor_name, major, minor, patch);
271 src[i++] = ldap_buf;
272 ldap_memfree(api.ldapai_vendor_name);
273 ber_memvfree((void **)api.ldapai_extensions);
274 }
275 }
276 #endif
277
278 DEBUGASSERT(i <= VERSION_PARTS);
279
280 outp = &out[0];
281 outlen = sizeof(out);
282 for(j = 0; j < i; j++) {
283 size_t n = strlen(src[j]);
284 /* we need room for a space, the string and the final zero */
285 if(outlen <= (n + 2))
286 break;
287 if(j) {
288 /* prepend a space if not the first */
289 *outp++ = ' ';
290 outlen--;
291 }
292 memcpy(outp, src[j], n);
293 outp += n;
294 outlen -= n;
295 }
296 *outp = 0;
297
298 return out;
299 }
300
301 /* data for curl_version_info
302
303 Keep the list sorted alphabetically. It is also written so that each
304 protocol line has its own #if line to make things easier on the eye.
305 */
306
307 static const char * const supported_protocols[] = {
308 #ifndef CURL_DISABLE_DICT
309 "dict",
310 #endif
311 #ifndef CURL_DISABLE_FILE
312 "file",
313 #endif
314 #ifndef CURL_DISABLE_FTP
315 "ftp",
316 #endif
317 #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
318 "ftps",
319 #endif
320 #ifndef CURL_DISABLE_GOPHER
321 "gopher",
322 #endif
323 #if defined(USE_SSL) && !defined(CURL_DISABLE_GOPHER)
324 "gophers",
325 #endif
326 #ifndef CURL_DISABLE_HTTP
327 "http",
328 #endif
329 #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
330 "https",
331 #endif
332 #ifndef CURL_DISABLE_IMAP
333 "imap",
334 #endif
335 #if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)
336 "imaps",
337 #endif
338 #ifndef CURL_DISABLE_LDAP
339 "ldap",
340 #if !defined(CURL_DISABLE_LDAPS) && \
341 ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
342 (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
343 "ldaps",
344 #endif
345 #endif
346 #ifndef CURL_DISABLE_MQTT
347 "mqtt",
348 #endif
349 #ifndef CURL_DISABLE_POP3
350 "pop3",
351 #endif
352 #if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)
353 "pop3s",
354 #endif
355 #ifdef USE_LIBRTMP
356 "rtmp",
357 "rtmpe",
358 "rtmps",
359 "rtmpt",
360 "rtmpte",
361 "rtmpts",
362 #endif
363 #ifndef CURL_DISABLE_RTSP
364 "rtsp",
365 #endif
366 #if defined(USE_SSH) && !defined(USE_WOLFSSH)
367 "scp",
368 #endif
369 #ifdef USE_SSH
370 "sftp",
371 #endif
372 #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE)
373 "smb",
374 # ifdef USE_SSL
375 "smbs",
376 # endif
377 #endif
378 #ifndef CURL_DISABLE_SMTP
379 "smtp",
380 #endif
381 #if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)
382 "smtps",
383 #endif
384 #ifndef CURL_DISABLE_TELNET
385 "telnet",
386 #endif
387 #ifndef CURL_DISABLE_TFTP
388 "tftp",
389 #endif
390 #ifdef USE_WEBSOCKETS
391 "ws",
392 #endif
393 #if defined(USE_SSL) && defined(USE_WEBSOCKETS)
394 "wss",
395 #endif
396
397 NULL
398 };
399
400 /*
401 * Feature presence run-time check functions.
402 *
403 * Warning: the value returned by these should not change between
404 * curl_global_init() and curl_global_cleanup() calls.
405 */
406
407 #if defined(USE_LIBIDN2)
idn_present(curl_version_info_data * info)408 static int idn_present(curl_version_info_data *info)
409 {
410 return info->libidn != NULL;
411 }
412 #else
413 #define idn_present NULL
414 #endif
415
416 #if defined(USE_SSL) && !defined(CURL_DISABLE_PROXY) && \
417 !defined(CURL_DISABLE_HTTP)
https_proxy_present(curl_version_info_data * info)418 static int https_proxy_present(curl_version_info_data *info)
419 {
420 (void) info;
421 return Curl_ssl_supports(NULL, SSLSUPP_HTTPS_PROXY);
422 }
423 #endif
424
425 /*
426 * Features table.
427 *
428 * Keep the features alphabetically sorted.
429 * Use FEATURE() macro to define an entry: this allows documentation check.
430 */
431
432 #define FEATURE(name, present, bitmask) {(name), (present), (bitmask)}
433
434 struct feat {
435 const char *name;
436 int (*present)(curl_version_info_data *info);
437 int bitmask;
438 };
439
440 static const struct feat features_table[] = {
441 #ifndef CURL_DISABLE_ALTSVC
442 FEATURE("alt-svc", NULL, CURL_VERSION_ALTSVC),
443 #endif
444 #ifdef CURLRES_ASYNCH
445 FEATURE("AsynchDNS", NULL, CURL_VERSION_ASYNCHDNS),
446 #endif
447 #ifdef HAVE_BROTLI
448 FEATURE("brotli", NULL, CURL_VERSION_BROTLI),
449 #endif
450 #ifdef DEBUGBUILD
451 FEATURE("Debug", NULL, CURL_VERSION_DEBUG),
452 #endif
453 #ifdef USE_GSASL
454 FEATURE("gsasl", NULL, CURL_VERSION_GSASL),
455 #endif
456 #ifdef HAVE_GSSAPI
457 FEATURE("GSS-API", NULL, CURL_VERSION_GSSAPI),
458 #endif
459 #ifndef CURL_DISABLE_HSTS
460 FEATURE("HSTS", NULL, CURL_VERSION_HSTS),
461 #endif
462 #if defined(USE_NGHTTP2)
463 FEATURE("HTTP2", NULL, CURL_VERSION_HTTP2),
464 #endif
465 #if defined(ENABLE_QUIC)
466 FEATURE("HTTP3", NULL, CURL_VERSION_HTTP3),
467 #endif
468 #if defined(USE_SSL) && !defined(CURL_DISABLE_PROXY) && \
469 !defined(CURL_DISABLE_HTTP)
470 FEATURE("HTTPS-proxy", https_proxy_present, CURL_VERSION_HTTPS_PROXY),
471 #endif
472 #if defined(USE_LIBIDN2) || defined(USE_WIN32_IDN)
473 FEATURE("IDN", idn_present, CURL_VERSION_IDN),
474 #endif
475 #ifdef ENABLE_IPV6
476 FEATURE("IPv6", NULL, CURL_VERSION_IPV6),
477 #endif
478 #ifdef USE_KERBEROS5
479 FEATURE("Kerberos", NULL, CURL_VERSION_KERBEROS5),
480 #endif
481 #if (SIZEOF_CURL_OFF_T > 4) && \
482 ( (SIZEOF_OFF_T > 4) || defined(USE_WIN32_LARGE_FILES) )
483 FEATURE("Largefile", NULL, CURL_VERSION_LARGEFILE),
484 #endif
485 #ifdef HAVE_LIBZ
486 FEATURE("libz", NULL, CURL_VERSION_LIBZ),
487 #endif
488 #ifdef CURL_WITH_MULTI_SSL
489 FEATURE("MultiSSL", NULL, CURL_VERSION_MULTI_SSL),
490 #endif
491 #ifdef USE_NTLM
492 FEATURE("NTLM", NULL, CURL_VERSION_NTLM),
493 #endif
494 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM) && \
495 defined(NTLM_WB_ENABLED)
496 FEATURE("NTLM_WB", NULL, CURL_VERSION_NTLM_WB),
497 #endif
498 #if defined(USE_LIBPSL)
499 FEATURE("PSL", NULL, CURL_VERSION_PSL),
500 #endif
501 #ifdef USE_SPNEGO
502 FEATURE("SPNEGO", NULL, CURL_VERSION_SPNEGO),
503 #endif
504 #ifdef USE_SSL
505 FEATURE("SSL", NULL, CURL_VERSION_SSL),
506 #endif
507 #ifdef USE_WINDOWS_SSPI
508 FEATURE("SSPI", NULL, CURL_VERSION_SSPI),
509 #endif
510 #ifdef GLOBAL_INIT_IS_THREADSAFE
511 FEATURE("threadsafe", NULL, CURL_VERSION_THREADSAFE),
512 #endif
513 #ifdef USE_TLS_SRP
514 FEATURE("TLS-SRP", NULL, CURL_VERSION_TLSAUTH_SRP),
515 #endif
516 #ifdef CURLDEBUG
517 FEATURE("TrackMemory", NULL, CURL_VERSION_CURLDEBUG),
518 #endif
519 #if defined(_WIN32) && defined(UNICODE) && defined(_UNICODE)
520 FEATURE("Unicode", NULL, CURL_VERSION_UNICODE),
521 #endif
522 #ifdef USE_UNIX_SOCKETS
523 FEATURE("UnixSockets", NULL, CURL_VERSION_UNIX_SOCKETS),
524 #endif
525 #ifdef HAVE_ZSTD
526 FEATURE("zstd", NULL, CURL_VERSION_ZSTD),
527 #endif
528 {NULL, NULL, 0}
529 };
530
531 static const char *feature_names[sizeof(features_table) /
532 sizeof(features_table[0])] = {NULL};
533
534
535 static curl_version_info_data version_info = {
536 CURLVERSION_NOW,
537 LIBCURL_VERSION,
538 LIBCURL_VERSION_NUM,
539 OS, /* as found by configure or set by hand at build-time */
540 0, /* features bitmask is built at run-time */
541 NULL, /* ssl_version */
542 0, /* ssl_version_num, this is kept at zero */
543 NULL, /* zlib_version */
544 supported_protocols,
545 NULL, /* c-ares version */
546 0, /* c-ares version numerical */
547 NULL, /* libidn version */
548 0, /* iconv version */
549 NULL, /* ssh lib version */
550 0, /* brotli_ver_num */
551 NULL, /* brotli version */
552 0, /* nghttp2 version number */
553 NULL, /* nghttp2 version string */
554 NULL, /* quic library string */
555 #ifdef CURL_CA_BUNDLE
556 CURL_CA_BUNDLE, /* cainfo */
557 #else
558 NULL,
559 #endif
560 #ifdef CURL_CA_PATH
561 CURL_CA_PATH, /* capath */
562 #else
563 NULL,
564 #endif
565 0, /* zstd_ver_num */
566 NULL, /* zstd version */
567 NULL, /* Hyper version */
568 NULL, /* gsasl version */
569 feature_names
570 };
571
curl_version_info(CURLversion stamp)572 curl_version_info_data *curl_version_info(CURLversion stamp)
573 {
574 size_t n;
575 const struct feat *p;
576 int features = 0;
577
578 #if defined(USE_SSH)
579 static char ssh_buffer[80];
580 #endif
581 #ifdef USE_SSL
582 #ifdef CURL_WITH_MULTI_SSL
583 static char ssl_buffer[200];
584 #else
585 static char ssl_buffer[80];
586 #endif
587 #endif
588 #ifdef HAVE_BROTLI
589 static char brotli_buffer[80];
590 #endif
591 #ifdef HAVE_ZSTD
592 static char zstd_buffer[80];
593 #endif
594
595 (void)stamp; /* avoid compiler warnings, we don't use this */
596
597 #ifdef USE_SSL
598 Curl_ssl_version(ssl_buffer, sizeof(ssl_buffer));
599 version_info.ssl_version = ssl_buffer;
600 #endif
601
602 #ifdef HAVE_LIBZ
603 version_info.libz_version = zlibVersion();
604 /* libz left NULL if non-existing */
605 #endif
606 #ifdef USE_ARES
607 {
608 int aresnum;
609 version_info.ares = ares_version(&aresnum);
610 version_info.ares_num = aresnum;
611 }
612 #endif
613 #ifdef USE_LIBIDN2
614 /* This returns a version string if we use the given version or later,
615 otherwise it returns NULL */
616 version_info.libidn = idn2_check_version(IDN2_VERSION);
617 #endif
618
619 #if defined(USE_SSH)
620 Curl_ssh_version(ssh_buffer, sizeof(ssh_buffer));
621 version_info.libssh_version = ssh_buffer;
622 #endif
623
624 #ifdef HAVE_BROTLI
625 version_info.brotli_ver_num = BrotliDecoderVersion();
626 brotli_version(brotli_buffer, sizeof(brotli_buffer));
627 version_info.brotli_version = brotli_buffer;
628 #endif
629
630 #ifdef HAVE_ZSTD
631 version_info.zstd_ver_num = (unsigned int)ZSTD_versionNumber();
632 zstd_version(zstd_buffer, sizeof(zstd_buffer));
633 version_info.zstd_version = zstd_buffer;
634 #endif
635
636 #ifdef USE_NGHTTP2
637 {
638 nghttp2_info *h2 = nghttp2_version(0);
639 version_info.nghttp2_ver_num = h2->version_num;
640 version_info.nghttp2_version = h2->version_str;
641 }
642 #endif
643
644 #ifdef ENABLE_QUIC
645 {
646 static char quicbuffer[80];
647 Curl_quic_ver(quicbuffer, sizeof(quicbuffer));
648 version_info.quic_version = quicbuffer;
649 }
650 #endif
651
652 #ifdef USE_HYPER
653 {
654 static char hyper_buffer[30];
655 msnprintf(hyper_buffer, sizeof(hyper_buffer), "Hyper/%s", hyper_version());
656 version_info.hyper_version = hyper_buffer;
657 }
658 #endif
659
660 #ifdef USE_GSASL
661 {
662 version_info.gsasl_version = gsasl_check_version(NULL);
663 }
664 #endif
665
666 /* Get available features, build bitmask and names array. */
667 n = 0;
668 for(p = features_table; p->name; p++)
669 if(!p->present || p->present(&version_info)) {
670 features |= p->bitmask;
671 feature_names[n++] = p->name;
672 }
673
674 feature_names[n] = NULL; /* Terminate array. */
675 version_info.features = features;
676
677 return &version_info;
678 }
679