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