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