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