1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2021, 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 #include "tool_setup.h"
23 #if defined(HAVE_STRCASECMP) && defined(HAVE_STRINGS_H)
24 #include <strings.h>
25 #endif
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
28 #include "curlx.h"
29 
30 #include "tool_panykey.h"
31 #include "tool_help.h"
32 #include "tool_libinfo.h"
33 #include "tool_version.h"
34 
35 #include "memdebug.h" /* keep this as LAST include */
36 
37 #ifdef MSDOS
38 #  define USE_WATT32
39 #endif
40 
41 struct category_descriptors {
42   const char *opt;
43   const char *desc;
44   curlhelp_t category;
45 };
46 
47 static const struct category_descriptors categories[] = {
48   {"auth", "Different types of authentication methods", CURLHELP_AUTH},
49   {"connection", "Low level networking operations",
50    CURLHELP_CONNECTION},
51   {"curl", "The command line tool itself", CURLHELP_CURL},
52   {"dns", "General DNS options", CURLHELP_DNS},
53   {"file", "FILE protocol options", CURLHELP_FILE},
54   {"ftp", "FTP protocol options", CURLHELP_FTP},
55   {"http", "HTTP and HTTPS protocol options", CURLHELP_HTTP},
56   {"imap", "IMAP protocol options", CURLHELP_IMAP},
57   /* important is left out because it is the default help page */
58   {"misc", "Options that don't fit into any other category", CURLHELP_MISC},
59   {"output", "Filesystem output", CURLHELP_OUTPUT},
60   {"pop3", "POP3 protocol options", CURLHELP_POP3},
61   {"post", "HTTP Post specific options", CURLHELP_POST},
62   {"proxy", "All options related to proxies", CURLHELP_PROXY},
63   {"scp", "SCP protocol options", CURLHELP_SCP},
64   {"sftp", "SFTP protocol options", CURLHELP_SFTP},
65   {"smtp", "SMTP protocol options", CURLHELP_SMTP},
66   {"ssh", "SSH protocol options", CURLHELP_SSH},
67   {"telnet", "TELNET protocol options", CURLHELP_TELNET},
68   {"tftp", "TFTP protocol options", CURLHELP_TFTP},
69   {"tls", "All TLS/SSL related options", CURLHELP_TLS},
70   {"upload", "All options for uploads",
71    CURLHELP_UPLOAD},
72   {"verbose", "Options related to any kind of command line output of curl",
73    CURLHELP_VERBOSE},
74   {NULL, NULL, CURLHELP_HIDDEN}
75 };
76 
77 extern const struct helptxt helptext[];
78 
79 struct feat {
80   const char *name;
81   int bitmask;
82 };
83 
84 static const struct feat feats[] = {
85   {"AsynchDNS",      CURL_VERSION_ASYNCHDNS},
86   {"Debug",          CURL_VERSION_DEBUG},
87   {"TrackMemory",    CURL_VERSION_CURLDEBUG},
88   {"IDN",            CURL_VERSION_IDN},
89   {"IPv6",           CURL_VERSION_IPV6},
90   {"Largefile",      CURL_VERSION_LARGEFILE},
91   {"Unicode",        CURL_VERSION_UNICODE},
92   {"SSPI",           CURL_VERSION_SSPI},
93   {"GSS-API",        CURL_VERSION_GSSAPI},
94   {"Kerberos",       CURL_VERSION_KERBEROS5},
95   {"SPNEGO",         CURL_VERSION_SPNEGO},
96   {"NTLM",           CURL_VERSION_NTLM},
97   {"NTLM_WB",        CURL_VERSION_NTLM_WB},
98   {"SSL",            CURL_VERSION_SSL},
99   {"libz",           CURL_VERSION_LIBZ},
100   {"brotli",         CURL_VERSION_BROTLI},
101   {"zstd",           CURL_VERSION_ZSTD},
102   {"CharConv",       CURL_VERSION_CONV},
103   {"TLS-SRP",        CURL_VERSION_TLSAUTH_SRP},
104   {"HTTP2",          CURL_VERSION_HTTP2},
105   {"HTTP3",          CURL_VERSION_HTTP3},
106   {"UnixSockets",    CURL_VERSION_UNIX_SOCKETS},
107   {"HTTPS-proxy",    CURL_VERSION_HTTPS_PROXY},
108   {"MultiSSL",       CURL_VERSION_MULTI_SSL},
109   {"PSL",            CURL_VERSION_PSL},
110   {"alt-svc",        CURL_VERSION_ALTSVC},
111   {"HSTS",           CURL_VERSION_HSTS},
112   {"gsasl",          CURL_VERSION_GSASL},
113 };
114 
print_category(curlhelp_t category)115 static void print_category(curlhelp_t category)
116 {
117   unsigned int i;
118   size_t longopt = 5;
119   size_t longdesc = 5;
120 
121   for(i = 0; helptext[i].opt; ++i) {
122     size_t len;
123     if(!(helptext[i].categories & category))
124       continue;
125     len = strlen(helptext[i].opt);
126     if(len > longopt)
127       longopt = len;
128     len = strlen(helptext[i].desc);
129     if(len > longdesc)
130       longdesc = len;
131   }
132   if(longopt + longdesc > 80)
133     longopt = 80 - longdesc;
134 
135   for(i = 0; helptext[i].opt; ++i)
136     if(helptext[i].categories & category) {
137       printf(" %-*s %s\n", (int)longopt, helptext[i].opt, helptext[i].desc);
138     }
139 }
140 
141 /* Prints category if found. If not, it returns 1 */
get_category_content(const char * category)142 static int get_category_content(const char *category)
143 {
144   unsigned int i;
145   for(i = 0; categories[i].opt; ++i)
146     if(curl_strequal(categories[i].opt, category)) {
147       printf("%s: %s\n", categories[i].opt, categories[i].desc);
148       print_category(categories[i].category);
149       return 0;
150     }
151   return 1;
152 }
153 
154 /* Prints all categories and their description */
get_categories(void)155 static void get_categories(void)
156 {
157   unsigned int i;
158   for(i = 0; categories[i].opt; ++i)
159     printf(" %-11s %s\n", categories[i].opt, categories[i].desc);
160 }
161 
162 
tool_help(char * category)163 void tool_help(char *category)
164 {
165   puts("Usage: curl [options...] <url>");
166   /* If no category was provided */
167   if(!category) {
168     const char *category_note = "\nThis is not the full help, this "
169       "menu is stripped into categories.\nUse \"--help category\" to get "
170       "an overview of all categories.\nFor all options use the manual"
171       " or \"--help all\".";
172     print_category(CURLHELP_IMPORTANT);
173     puts(category_note);
174   }
175   /* Lets print everything if "all" was provided */
176   else if(curl_strequal(category, "all"))
177     /* Print everything except hidden */
178     print_category(~(CURLHELP_HIDDEN));
179   /* Lets handle the string "category" differently to not print an errormsg */
180   else if(curl_strequal(category, "category"))
181     get_categories();
182   /* Otherwise print category and handle the case if the cat was not found */
183   else if(get_category_content(category)) {
184     puts("Invalid category provided, here is a list of all categories:\n");
185     get_categories();
186   }
187   free(category);
188 }
189 
190 static int
featcomp(const void * p1,const void * p2)191 featcomp(const void *p1, const void *p2)
192 {
193   /* The arguments to this function are "pointers to pointers to char", but
194      the comparison arguments are "pointers to char", hence the following cast
195      plus dereference */
196 #ifdef HAVE_STRCASECMP
197   return strcasecmp(* (char * const *) p1, * (char * const *) p2);
198 #elif defined(HAVE_STRCMPI)
199   return strcmpi(* (char * const *) p1, * (char * const *) p2);
200 #else
201   return strcmp(* (char * const *) p1, * (char * const *) p2);
202 #endif
203 }
204 
tool_version_info(void)205 void tool_version_info(void)
206 {
207   const char *const *proto;
208 
209   printf(CURL_ID "%s\n", curl_version());
210 #ifdef CURL_PATCHSTAMP
211   printf("Release-Date: %s, security patched: %s\n",
212          LIBCURL_TIMESTAMP, CURL_PATCHSTAMP);
213 #else
214   printf("Release-Date: %s\n", LIBCURL_TIMESTAMP);
215 #endif
216   if(curlinfo->protocols) {
217     printf("Protocols: ");
218     for(proto = curlinfo->protocols; *proto; ++proto) {
219       printf("%s ", *proto);
220     }
221     puts(""); /* newline */
222   }
223   if(curlinfo->features) {
224     char *featp[ sizeof(feats) / sizeof(feats[0]) + 1];
225     size_t numfeat = 0;
226     unsigned int i;
227     printf("Features:");
228     for(i = 0; i < sizeof(feats)/sizeof(feats[0]); i++) {
229       if(curlinfo->features & feats[i].bitmask)
230         featp[numfeat++] = (char *)feats[i].name;
231     }
232     qsort(&featp[0], numfeat, sizeof(char *), featcomp);
233     for(i = 0; i< numfeat; i++)
234       printf(" %s", featp[i]);
235     puts(""); /* newline */
236   }
237   if(strcmp(CURL_VERSION, curlinfo->version)) {
238     printf("WARNING: curl and libcurl versions do not match. "
239            "Functionality may be affected.\n");
240   }
241 }
242 
tool_list_engines(void)243 void tool_list_engines(void)
244 {
245   CURL *curl = curl_easy_init();
246   struct curl_slist *engines = NULL;
247 
248   /* Get the list of engines */
249   curl_easy_getinfo(curl, CURLINFO_SSL_ENGINES, &engines);
250 
251   puts("Build-time engines:");
252   if(engines) {
253     for(; engines; engines = engines->next)
254       printf("  %s\n", engines->data);
255   }
256   else {
257     puts("  <none>");
258   }
259 
260   /* Cleanup the list of engines */
261   curl_slist_free_all(engines);
262   curl_easy_cleanup(curl);
263 }
264