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.haxx.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 14 /* 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   int i = 0;
148   int j;
149 
150 #ifdef DEBUGBUILD
151   /* Override version string when environment variable CURL_VERSION is set */
152   const char *debugversion = getenv("CURL_VERSION");
153   if(debugversion) {
154     strncpy(out, debugversion, sizeof(out)-1);
155     out[sizeof(out)-1] = '\0';
156     return out;
157   }
158 #endif
159 
160   src[i++] = LIBCURL_NAME "/" LIBCURL_VERSION;
161 #ifdef USE_SSL
162   Curl_ssl_version(ssl_version, sizeof(ssl_version));
163   src[i++] = ssl_version;
164 #endif
165 #ifdef HAVE_LIBZ
166   msnprintf(z_version, sizeof(z_version), "zlib/%s", zlibVersion());
167   src[i++] = z_version;
168 #endif
169 #ifdef HAVE_BROTLI
170   brotli_version(&br_version[7], sizeof(br_version) - 7);
171   src[i++] = br_version;
172 #endif
173 #ifdef HAVE_ZSTD
174   zstd_version(&zst_version[5], sizeof(zst_version) - 5);
175   src[i++] = zst_version;
176 #endif
177 #ifdef USE_ARES
178   msnprintf(cares_version, sizeof(cares_version),
179             "c-ares/%s", ares_version(NULL));
180   src[i++] = cares_version;
181 #endif
182 #ifdef USE_LIBIDN2
183   msnprintf(idn_version, sizeof(idn_version),
184             "libidn2/%s", idn2_check_version(NULL));
185   src[i++] = idn_version;
186 #elif defined(USE_WIN32_IDN)
187   src[i++] = (char *)"WinIDN";
188 #endif
189 
190 #ifdef USE_LIBPSL
191   msnprintf(psl_version, sizeof(psl_version), "libpsl/%s", psl_get_version());
192   src[i++] = psl_version;
193 #endif
194 #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
195 #ifdef _LIBICONV_VERSION
196   msnprintf(iconv_version, sizeof(iconv_version), "iconv/%d.%d",
197             _LIBICONV_VERSION >> 8, _LIBICONV_VERSION & 255);
198 #else
199   /* version unknown, let the default stand */
200 #endif /* _LIBICONV_VERSION */
201   src[i++] = iconv_version;
202 #endif
203 #ifdef USE_SSH
204   Curl_ssh_version(ssh_version, sizeof(ssh_version));
205   src[i++] = ssh_version;
206 #endif
207 #ifdef USE_NGHTTP2
208   Curl_http2_ver(h2_version, sizeof(h2_version));
209   src[i++] = h2_version;
210 #endif
211 #ifdef ENABLE_QUIC
212   Curl_quic_ver(h3_version, sizeof(h3_version));
213   src[i++] = h3_version;
214 #endif
215 #ifdef USE_LIBRTMP
216   {
217     char suff[2];
218     if(RTMP_LIB_VERSION & 0xff) {
219       suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
220       suff[1] = '\0';
221     }
222     else
223       suff[0] = '\0';
224 
225     msnprintf(rtmp_version, sizeof(rtmp_version), "librtmp/%d.%d%s",
226               RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
227               suff);
228     src[i++] = rtmp_version;
229   }
230 #endif
231 
232   DEBUGASSERT(i <= VERSION_PARTS);
233 
234   outp = &out[0];
235   outlen = sizeof(out);
236   for(j = 0; j < i; j++) {
237     size_t n = strlen(src[j]);
238     /* we need room for a space, the string and the final zero */
239     if(outlen <= (n + 2))
240       break;
241     if(j) {
242       /* prepend a space if not the first */
243       *outp++ = ' ';
244       outlen--;
245     }
246     memcpy(outp, src[j], n);
247     outp += n;
248     outlen -= n;
249   }
250   *outp = 0;
251 
252   return out;
253 }
254 
255 /* data for curl_version_info
256 
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 
261 static const char * const protocols[] = {
262 #ifndef CURL_DISABLE_DICT
263   "dict",
264 #endif
265 #ifndef CURL_DISABLE_FILE
266   "file",
267 #endif
268 #ifndef CURL_DISABLE_FTP
269   "ftp",
270 #endif
271 #if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
272   "ftps",
273 #endif
274 #ifndef CURL_DISABLE_GOPHER
275   "gopher",
276 #endif
277 #ifndef CURL_DISABLE_HTTP
278   "http",
279 #endif
280 #if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
281   "https",
282 #endif
283 #ifndef CURL_DISABLE_IMAP
284   "imap",
285 #endif
286 #if defined(USE_SSL) && !defined(CURL_DISABLE_IMAP)
287   "imaps",
288 #endif
289 #ifndef CURL_DISABLE_LDAP
290   "ldap",
291 #if !defined(CURL_DISABLE_LDAPS) && \
292     ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
293      (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
294   "ldaps",
295 #endif
296 #endif
297 #ifndef CURL_DISABLE_MQTT
298   "mqtt",
299 #endif
300 #ifndef CURL_DISABLE_POP3
301   "pop3",
302 #endif
303 #if defined(USE_SSL) && !defined(CURL_DISABLE_POP3)
304   "pop3s",
305 #endif
306 #ifdef USE_LIBRTMP
307   "rtmp",
308 #endif
309 #ifndef CURL_DISABLE_RTSP
310   "rtsp",
311 #endif
312 #if defined(USE_SSH) && !defined(USE_WOLFSSH)
313   "scp",
314 #endif
315 #ifdef USE_SSH
316   "sftp",
317 #endif
318 #if !defined(CURL_DISABLE_SMB) && defined(USE_CURL_NTLM_CORE) && \
319    (CURL_SIZEOF_CURL_OFF_T > 4)
320   "smb",
321 #  ifdef USE_SSL
322   "smbs",
323 #  endif
324 #endif
325 #ifndef CURL_DISABLE_SMTP
326   "smtp",
327 #endif
328 #if defined(USE_SSL) && !defined(CURL_DISABLE_SMTP)
329   "smtps",
330 #endif
331 #ifndef CURL_DISABLE_TELNET
332   "telnet",
333 #endif
334 #ifndef CURL_DISABLE_TFTP
335   "tftp",
336 #endif
337 
338   NULL
339 };
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 (CURL_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(CURL_DOES_CONVERSIONS)
392   | CURL_VERSION_CONV
393 #endif
394 #if defined(USE_TLS_SRP)
395   | CURL_VERSION_TLSAUTH_SRP
396 #endif
397 #if defined(USE_NGHTTP2)
398   | CURL_VERSION_HTTP2
399 #endif
400 #if defined(ENABLE_QUIC)
401   | CURL_VERSION_HTTP3
402 #endif
403 #if defined(USE_UNIX_SOCKETS)
404   | CURL_VERSION_UNIX_SOCKETS
405 #endif
406 #if defined(USE_LIBPSL)
407   | CURL_VERSION_PSL
408 #endif
409 #if defined(CURL_WITH_MULTI_SSL)
410   | CURL_VERSION_MULTI_SSL
411 #endif
412 #if defined(HAVE_BROTLI)
413   | CURL_VERSION_BROTLI
414 #endif
415 #if defined(HAVE_ZSTD)
416   | CURL_VERSION_ZSTD
417 #endif
418 #if defined(USE_ALTSVC)
419   | CURL_VERSION_ALTSVC
420 #endif
421   ,
422   NULL, /* ssl_version */
423   0,    /* ssl_version_num, this is kept at zero */
424   NULL, /* zlib_version */
425   protocols,
426   NULL, /* c-ares version */
427   0,    /* c-ares version numerical */
428   NULL, /* libidn version */
429   0,    /* iconv version */
430   NULL, /* ssh lib version */
431   0,    /* brotli_ver_num */
432   NULL, /* brotli version */
433   0,    /* nghttp2 version number */
434   NULL, /* nghttp2 version string */
435   NULL, /* quic library string */
436 #ifdef CURL_CA_BUNDLE
437   CURL_CA_BUNDLE, /* cainfo */
438 #else
439   NULL,
440 #endif
441 #ifdef CURL_CA_PATH
442   CURL_CA_PATH,  /* capath */
443 #else
444   NULL,
445 #endif
446   0,    /* zstd_ver_num */
447   NULL  /* zstd version */
448 };
449 
curl_version_info(CURLversion stamp)450 curl_version_info_data *curl_version_info(CURLversion stamp)
451 {
452 #if defined(USE_SSH)
453   static char ssh_buffer[80];
454 #endif
455 #ifdef USE_SSL
456 #ifdef CURL_WITH_MULTI_SSL
457   static char ssl_buffer[200];
458 #else
459   static char ssl_buffer[80];
460 #endif
461 #endif
462 #ifdef HAVE_BROTLI
463   static char brotli_buffer[80];
464 #endif
465 #ifdef HAVE_ZSTD
466   static char zstd_buffer[80];
467 #endif
468 
469 
470 #ifdef USE_SSL
471   Curl_ssl_version(ssl_buffer, sizeof(ssl_buffer));
472   version_info.ssl_version = ssl_buffer;
473 #ifndef CURL_DISABLE_PROXY
474   if(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY)
475     version_info.features |= CURL_VERSION_HTTPS_PROXY;
476   else
477     version_info.features &= ~CURL_VERSION_HTTPS_PROXY;
478 #endif
479 #endif
480 
481 #ifdef HAVE_LIBZ
482   version_info.libz_version = zlibVersion();
483   /* libz left NULL if non-existing */
484 #endif
485 #ifdef USE_ARES
486   {
487     int aresnum;
488     version_info.ares = ares_version(&aresnum);
489     version_info.ares_num = aresnum;
490   }
491 #endif
492 #ifdef USE_LIBIDN2
493   /* This returns a version string if we use the given version or later,
494      otherwise it returns NULL */
495   version_info.libidn = idn2_check_version(IDN2_VERSION);
496   if(version_info.libidn)
497     version_info.features |= CURL_VERSION_IDN;
498 #elif defined(USE_WIN32_IDN)
499   version_info.features |= CURL_VERSION_IDN;
500 #endif
501 
502 #if defined(HAVE_ICONV) && defined(CURL_DOES_CONVERSIONS)
503 #ifdef _LIBICONV_VERSION
504   version_info.iconv_ver_num = _LIBICONV_VERSION;
505 #else
506   /* version unknown */
507   version_info.iconv_ver_num = -1;
508 #endif /* _LIBICONV_VERSION */
509 #endif
510 
511 #if defined(USE_SSH)
512   Curl_ssh_version(ssh_buffer, sizeof(ssh_buffer));
513   version_info.libssh_version = ssh_buffer;
514 #endif
515 
516 #ifdef HAVE_BROTLI
517   version_info.brotli_ver_num = BrotliDecoderVersion();
518   brotli_version(brotli_buffer, sizeof(brotli_buffer));
519   version_info.brotli_version = brotli_buffer;
520 #endif
521 
522 #ifdef HAVE_ZSTD
523   version_info.zstd_ver_num = (unsigned int)ZSTD_versionNumber();
524   zstd_version(zstd_buffer, sizeof(zstd_buffer));
525   version_info.zstd_version = zstd_buffer;
526 #endif
527 
528 #ifdef USE_NGHTTP2
529   {
530     nghttp2_info *h2 = nghttp2_version(0);
531     version_info.nghttp2_ver_num = h2->version_num;
532     version_info.nghttp2_version = h2->version_str;
533   }
534 #endif
535 
536 #ifdef ENABLE_QUIC
537   {
538     static char quicbuffer[80];
539     Curl_quic_ver(quicbuffer, sizeof(quicbuffer));
540     version_info.quic_version = quicbuffer;
541   }
542 #endif
543 
544   (void)stamp; /* avoid compiler warnings, we don't use this */
545   return &version_info;
546 }
547