1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
31 # include "tool_hugehelp.h"
34 #include "tool_binmode.h"
35 #include "tool_cfgable.h"
36 #include "tool_cb_prg.h"
37 #include "tool_formparse.h"
38 #include "tool_getparam.h"
39 #include "tool_help.h"
40 #include "tool_helpers.h"
41 #include "tool_libinfo.h"
42 #include "tool_metalink.h"
43 #include "tool_msgs.h"
44 #include "tool_paramhlp.h"
45 #include "tool_parsecfg.h"
46 #include "tool_version.h"
48 #include "memdebug.h" /* keep this as LAST include */
54 #define GetStr(str,val) do { \
60 *(str) = strdup((val)); \
62 return PARAM_NO_MEM; \
67 const char *letter; /* short name option */
68 const char *lname; /* long name option */
69 bool extraparam; /* whether it takes an additional argument */
72 static const struct LongShort aliases[]= {
73 /* all these ones, starting with "*" or "$" as a short-option have *no*
74 short option to mention. */
76 {"*a", "random-file", TRUE},
77 {"*b", "egd-file", TRUE},
78 {"*c", "connect-timeout", TRUE},
79 {"*d", "ciphers", TRUE},
80 {"*e", "disable-epsv", FALSE},
81 {"*E", "epsv", FALSE},
82 /* 'epsv' made like this to make --no-epsv and --epsv to work
83 although --disable-epsv is the documented option */
84 #ifdef USE_ENVIRONMENT
85 {"*f", "environment", FALSE},
87 {"*g", "trace", TRUE},
88 {"*h", "trace-ascii", TRUE},
89 {"*i", "limit-rate", TRUE},
90 {"*j", "compressed", FALSE},
91 {"*J", "tr-encoding", FALSE},
92 {"*k", "digest", FALSE},
93 {"*l", "negotiate", FALSE},
94 {"*m", "ntlm", FALSE},
95 {"*M", "ntlm-wb", FALSE},
96 {"*n", "basic", FALSE},
97 {"*o", "anyauth", FALSE},
99 {"*p", "wdebug", FALSE},
101 {"*q", "ftp-create-dirs", FALSE},
102 {"*r", "create-dirs", FALSE},
103 {"*s", "max-redirs", TRUE},
104 {"*t", "proxy-ntlm", FALSE},
105 {"*u", "crlf", FALSE},
106 {"*v", "stderr", TRUE},
107 {"*w", "interface", TRUE},
108 {"*x", "krb" , TRUE},
109 {"*x", "krb4" , TRUE},
110 /* 'krb4' is the previous name */
111 {"*y", "max-filesize", TRUE},
112 {"*z", "disable-eprt", FALSE},
113 {"*Z", "eprt", FALSE},
114 /* 'eprt' made like this to make --no-eprt and --eprt to work
115 although --disable-eprt is the documented option */
116 {"$a", "ftp-ssl", FALSE},
117 /* 'ftp-ssl' deprecated name since 7.20.0 */
118 {"$a", "ssl", FALSE},
119 /* 'ssl' new option name in 7.20.0, previously this was ftp-ssl */
120 {"$b", "ftp-pasv", FALSE},
121 {"$c", "socks5", TRUE},
122 {"$c", "socks", TRUE},
123 /* 'socks' is how the option once was documented but we prefer
124 the --socks5 version for explicit version */
125 {"$d", "tcp-nodelay", FALSE},
126 {"$e", "proxy-digest", FALSE},
127 {"$f", "proxy-basic", FALSE},
128 {"$g", "retry", TRUE},
129 {"$h", "retry-delay", TRUE},
130 {"$i", "retry-max-time", TRUE},
131 {"$k", "proxy-negotiate", FALSE},
132 {"$m", "ftp-account", TRUE},
133 {"$n", "proxy-anyauth", FALSE},
134 {"$o", "trace-time", FALSE},
135 {"$p", "ignore-content-length", FALSE},
136 {"$q", "ftp-skip-pasv-ip", FALSE},
137 {"$r", "ftp-method", TRUE},
138 {"$s", "local-port", TRUE},
139 {"$t", "socks4", TRUE},
140 {"$T", "socks4a", TRUE},
141 {"$u", "ftp-alternative-to-user", TRUE},
142 {"$v", "ftp-ssl-reqd", FALSE},
143 /* 'ftp-ssl-reqd' deprecated name since 7.20.0 */
144 {"$v", "ssl-reqd", FALSE},
145 /* 'ssl-reqd' new in 7.20.0, previously this was ftp-ssl-reqd */
146 {"$w", "sessionid", FALSE},
147 /* ¡sessionid' listed as --no-sessionid in the help */
148 {"$x", "ftp-ssl-control", FALSE},
149 {"$y", "ftp-ssl-ccc", FALSE},
150 {"$j", "ftp-ssl-ccc-mode", TRUE},
151 {"$z", "libcurl", TRUE},
152 {"$#", "raw", FALSE},
153 {"$0", "post301", FALSE},
154 {"$1", "keepalive", FALSE},
155 /* 'keepalive' listed as --no-keepalive in the help */
156 {"$2", "socks5-hostname", TRUE},
157 {"$3", "keepalive-time", TRUE},
158 {"$4", "post302", FALSE},
159 {"$5", "noproxy", TRUE},
160 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
161 {"$6", "socks5-gssapi-service", TRUE},
162 {"$7", "socks5-gssapi-nec", FALSE},
164 {"$8", "proxy1.0", TRUE},
165 {"$9", "tftp-blksize", TRUE},
166 {"$A", "mail-from", TRUE},
167 {"$B", "mail-rcpt", TRUE},
168 {"$C", "ftp-pret", FALSE},
169 {"$D", "proto", TRUE},
170 {"$E", "proto-redir", TRUE},
171 {"$F", "resolve", TRUE},
172 {"$G", "delegation", TRUE},
173 {"$H", "mail-auth", TRUE},
174 {"$I", "post303", FALSE},
175 {"$J", "metalink", FALSE},
176 {"$K", "sasl-ir", FALSE},
177 {"0", "http1.0", FALSE},
178 {"1", "tlsv1", FALSE},
179 {"2", "sslv2", FALSE},
180 {"3", "sslv3", FALSE},
181 {"4", "ipv4", FALSE},
182 {"6", "ipv6", FALSE},
183 {"a", "append", FALSE},
184 {"A", "user-agent", TRUE},
185 {"b", "cookie", TRUE},
186 {"B", "use-ascii", FALSE},
187 {"c", "cookie-jar", TRUE},
188 {"C", "continue-at", TRUE},
190 {"da", "data-ascii", TRUE},
191 {"db", "data-binary", TRUE},
192 {"de", "data-urlencode", TRUE},
193 {"D", "dump-header", TRUE},
194 {"e", "referer", TRUE},
196 {"Ea", "cacert", TRUE},
197 {"Eb", "cert-type", TRUE},
199 {"Ed", "key-type", TRUE},
200 {"Ee", "pass", TRUE},
201 {"Ef", "engine", TRUE},
202 {"Eg", "capath ", TRUE},
203 {"Eh", "pubkey", TRUE},
204 {"Ei", "hostpubmd5", TRUE},
205 {"Ej", "crlfile", TRUE},
206 {"Ek", "tlsuser", TRUE},
207 {"El", "tlspassword", TRUE},
208 {"Em", "tlsauthtype", TRUE},
209 {"En", "ssl-allow-beast", FALSE},
210 {"f", "fail", FALSE},
212 {"Fs", "form-string", TRUE},
213 {"g", "globoff", FALSE},
215 {"h", "help", FALSE},
216 {"H", "header", TRUE},
217 {"i", "include", FALSE},
218 {"I", "head", FALSE},
219 {"j", "junk-session-cookies", FALSE},
220 {"J", "remote-header-name", FALSE},
221 {"k", "insecure", FALSE},
222 {"K", "config", TRUE},
223 {"l", "list-only", FALSE},
224 {"L", "location", FALSE},
225 {"Lt", "location-trusted", FALSE},
226 {"m", "max-time", TRUE},
227 {"M", "manual", FALSE},
228 {"n", "netrc", FALSE},
229 {"no", "netrc-optional", FALSE},
230 {"ne", "netrc-file", TRUE},
231 {"N", "buffer", FALSE},
232 /* 'buffer' listed as --no-buffer in the help */
233 {"o", "output", TRUE},
234 {"O", "remote-name", FALSE},
235 {"Oa", "remote-name-all", FALSE},
236 {"p", "proxytunnel", FALSE},
237 {"P", "ftpport", TRUE},
238 /* 'ftpport' old version */
239 {"P", "ftp-port", TRUE},
240 {"q", "disable", FALSE},
241 {"Q", "quote", TRUE},
242 {"r", "range", TRUE},
243 {"R", "remote-time", FALSE},
244 {"s", "silent", FALSE},
245 {"S", "show-error", FALSE},
246 {"t", "telnet-options", TRUE},
247 /* 'telnet-options' documented as telnet-option */
248 {"T", "upload-file", TRUE},
250 {"U", "proxy-user", TRUE},
251 {"v", "verbose", FALSE},
252 {"V", "version", FALSE},
253 {"w", "write-out", TRUE},
254 {"x", "proxy", TRUE},
255 {"X", "request", TRUE},
256 {"X", "http-request", TRUE},
257 /* 'http-request' OBSOLETE VERSION */
258 {"Y", "speed-limit", TRUE},
259 {"y", "speed-time", TRUE},
260 {"z", "time-cond", TRUE},
261 {"#", "progress-bar", FALSE},
262 {"~", "xattr", FALSE},
270 static const struct feat feats[] = {
271 {"AsynchDNS", CURL_VERSION_ASYNCHDNS},
272 {"Debug", CURL_VERSION_DEBUG},
273 {"TrackMemory", CURL_VERSION_CURLDEBUG},
274 {"GSS-Negotiate", CURL_VERSION_GSSNEGOTIATE},
275 {"IDN", CURL_VERSION_IDN},
276 {"IPv6", CURL_VERSION_IPV6},
277 {"Largefile", CURL_VERSION_LARGEFILE},
278 {"NTLM", CURL_VERSION_NTLM},
279 {"NTLM_WB", CURL_VERSION_NTLM_WB},
280 {"SPNEGO", CURL_VERSION_SPNEGO},
281 {"SSL", CURL_VERSION_SSL},
282 {"SSPI", CURL_VERSION_SSPI},
283 {"krb4", CURL_VERSION_KERBEROS4},
284 {"libz", CURL_VERSION_LIBZ},
285 {"CharConv", CURL_VERSION_CONV},
286 {"TLS-SRP", CURL_VERSION_TLSAUTH_SRP}
289 /* Split the argument of -E to 'certname' and 'passphrase' separated by colon.
290 * We allow ':' and '\' to be escaped by '\' so that we can use certificate
291 * nicknames containing ':'. See <https://sourceforge.net/p/curl/bugs/1196/>
296 void parse_cert_parameter(const char *cert_parameter,
300 size_t param_length = strlen(cert_parameter);
302 const char *param_place = NULL;
303 char *certname_place = NULL;
307 /* most trivial assumption: cert_parameter is empty */
308 if(param_length == 0)
311 /* next less trivial: cert_parameter contains no colon nor backslash; this
312 * means no passphrase was given and no characters escaped */
313 if(!strpbrk(cert_parameter, ":\\")) {
314 *certname = strdup(cert_parameter);
317 /* deal with escaped chars; find unescaped colon if it exists */
318 certname_place = malloc(param_length + 1);
322 *certname = certname_place;
323 param_place = cert_parameter;
324 while(*param_place) {
325 span = strcspn(param_place, ":\\");
326 strncpy(certname_place, param_place, span);
328 certname_place += span;
329 /* we just ate all the non-special chars. now we're on either a special
330 * char or the end of the string. */
331 switch(*param_place) {
336 switch(*param_place) {
338 *certname_place++ = '\\';
341 *certname_place++ = '\\';
345 *certname_place++ = ':';
349 *certname_place++ = '\\';
350 *certname_place++ = *param_place;
356 /* Since we live in a world of weirdness and confusion, the win32
357 dudes can use : when using drive letters and thus c:\file:password
358 needs to work. In order not to break compatibility, we still use : as
359 separator, but we try to detect when it is used for a file name! On
363 (param_place == &cert_parameter[1]) &&
364 (cert_parameter[2] == '\\' || cert_parameter[2] == '/') &&
365 (ISALPHA(cert_parameter[0])) ) {
366 /* colon in the second column, followed by a backslash, and the
367 first character is an alphabetic letter:
369 this is a drive letter colon */
370 *certname_place++ = ':';
375 /* escaped colons and Windows drive letter colons were handled
376 * above; if we're still here, this is a separating colon */
378 if(strlen(param_place) > 0) {
379 *passphrase = strdup(param_place);
385 *certname_place = '\0';
388 ParameterError getparameter(char *flag, /* f or -long-flag */
389 char *nextarg, /* NULL if unset */
390 bool *usedarg, /* set to TRUE if the arg
392 struct Configurable *config)
395 char subletter = '\0'; /* subletters can only occur on long options */
397 const char *parse = NULL;
401 bool longopt = FALSE;
402 bool singleopt = FALSE; /* when true means '-o foo' used '-ofoo' */
404 bool toggle = TRUE; /* how to switch boolean options, on or off. Controlled
405 by using --OPTION or --no-OPTION */
408 if(('-' != flag[0]) ||
409 (('-' == flag[0]) && ('-' == flag[1]))) {
410 /* this should be a long name */
411 char *word = ('-' == flag[0]) ? flag+2 : flag;
412 size_t fnam = strlen(word);
415 if(!strncmp(word, "no-", 3)) {
416 /* disable this option but ignore the "no-" part when looking for it */
421 for(j = 0; j < sizeof(aliases)/sizeof(aliases[0]); j++) {
422 if(curlx_strnequal(aliases[j].lname, word, fnam)) {
425 if(curlx_raw_equal(aliases[j].lname, word)) {
426 parse = aliases[j].letter;
428 numhits = 1; /* a single unique hit */
431 parse = aliases[j].letter;
436 /* this is at least the second match! */
437 return PARAM_OPTION_AMBIGUOUS;
440 return PARAM_OPTION_UNKNOWN;
444 flag++; /* prefixed with one dash, pass it */
450 /* we can loop here if we have multiple single-letters */
454 letter = (char)*parse;
463 subletter = parse[1];
465 *usedarg = FALSE; /* default is that we don't use the arg */
468 for(j = 0; j < sizeof(aliases)/sizeof(aliases[0]); j++) {
469 if(letter == aliases[j].letter[0]) {
475 return PARAM_OPTION_UNKNOWN;
479 if(aliases[hit].extraparam) {
480 /* this option requires an extra parameter */
481 if(!longopt && parse[1]) {
482 nextarg = (char *)&parse[1]; /* this is the actual extra parameter */
483 singleopt = TRUE; /* don't loop anymore after this */
486 return PARAM_REQUIRES_PARAMETER;
488 *usedarg = TRUE; /* mark it as used */
492 case '*': /* options without a short option */
494 case 'a': /* random-file */
495 GetStr(&config->random_file, nextarg);
497 case 'b': /* egd-file */
498 GetStr(&config->egd_file, nextarg);
500 case 'c': /* connect-timeout */
501 err = str2udouble(&config->connecttimeout, nextarg);
505 case 'd': /* ciphers */
506 GetStr(&config->cipher_list, nextarg);
508 case 'e': /* --disable-epsv */
509 config->disable_epsv = toggle;
511 case 'E': /* --epsv */
512 config->disable_epsv = (!toggle)?TRUE:FALSE;
514 #ifdef USE_ENVIRONMENT
516 config->writeenv = toggle;
519 case 'g': /* --trace */
520 GetStr(&config->trace_dump, nextarg);
521 if(config->tracetype && (config->tracetype != TRACE_BIN))
522 warnf(config, "--trace overrides an earlier trace/verbose option\n");
523 config->tracetype = TRACE_BIN;
525 case 'h': /* --trace-ascii */
526 GetStr(&config->trace_dump, nextarg);
527 if(config->tracetype && (config->tracetype != TRACE_ASCII))
529 "--trace-ascii overrides an earlier trace/verbose option\n");
530 config->tracetype = TRACE_ASCII;
532 case 'i': /* --limit-rate */
534 /* We support G, M, K too */
536 curl_off_t value = curlx_strtoofft(nextarg, &unit, 0);
540 else if(strlen(unit) > 1)
541 unit = (char *)"w"; /* unsupported */
546 value *= 1024*1024*1024;
558 /* for plain bytes, leave as-is */
561 warnf(config, "unsupported rate unit. Use G, M, K or B!\n");
562 return PARAM_BAD_USE;
564 config->recvpersecond = value;
565 config->sendpersecond = value;
569 case 'j': /* --compressed */
570 if(toggle && !(curlinfo->features & CURL_VERSION_LIBZ))
571 return PARAM_LIBCURL_DOESNT_SUPPORT;
572 config->encoding = toggle;
575 case 'J': /* --tr-encoding */
576 config->tr_encoding = toggle;
579 case 'k': /* --digest */
581 config->authtype |= CURLAUTH_DIGEST;
583 config->authtype &= ~CURLAUTH_DIGEST;
586 case 'l': /* --negotiate */
588 if(curlinfo->features & CURL_VERSION_GSSNEGOTIATE)
589 config->authtype |= CURLAUTH_GSSNEGOTIATE;
591 return PARAM_LIBCURL_DOESNT_SUPPORT;
594 config->authtype &= ~CURLAUTH_GSSNEGOTIATE;
597 case 'm': /* --ntlm */
599 if(curlinfo->features & CURL_VERSION_NTLM)
600 config->authtype |= CURLAUTH_NTLM;
602 return PARAM_LIBCURL_DOESNT_SUPPORT;
605 config->authtype &= ~CURLAUTH_NTLM;
608 case 'M': /* --ntlm-wb */
610 if(curlinfo->features & CURL_VERSION_NTLM_WB)
611 config->authtype |= CURLAUTH_NTLM_WB;
613 return PARAM_LIBCURL_DOESNT_SUPPORT;
616 config->authtype &= ~CURLAUTH_NTLM_WB;
619 case 'n': /* --basic for completeness */
621 config->authtype |= CURLAUTH_BASIC;
623 config->authtype &= ~CURLAUTH_BASIC;
626 case 'o': /* --anyauth, let libcurl pick it */
628 config->authtype = CURLAUTH_ANY;
629 /* --no-anyauth simply doesn't touch it */
633 case 'p': /* --wdebug */
637 case 'q': /* --ftp-create-dirs */
638 config->ftp_create_dirs = toggle;
641 case 'r': /* --create-dirs */
642 config->create_dirs = TRUE;
645 case 's': /* --max-redirs */
646 /* specified max no of redirects (http(s)), this accepts -1 as a
648 err = str2num(&config->maxredirs, nextarg);
651 if(config->maxredirs < -1)
652 return PARAM_BAD_NUMERIC;
655 case 't': /* --proxy-ntlm */
656 if(curlinfo->features & CURL_VERSION_NTLM)
657 config->proxyntlm = toggle;
659 return PARAM_LIBCURL_DOESNT_SUPPORT;
662 case 'u': /* --crlf */
663 /* LF -> CRLF conversion? */
667 case 'v': /* --stderr */
668 if(strcmp(nextarg, "-")) {
669 FILE *newfile = fopen(nextarg, "wt");
671 warnf(config, "Failed to open %s!\n", nextarg);
673 if(config->errors_fopened)
674 fclose(config->errors);
675 config->errors = newfile;
676 config->errors_fopened = TRUE;
680 config->errors = stdout;
682 case 'w': /* --interface */
684 GetStr(&config->iface, nextarg);
686 case 'x': /* --krb */
687 /* kerberos level string */
688 if(curlinfo->features & (CURL_VERSION_KERBEROS4 |
689 CURL_VERSION_GSSNEGOTIATE))
690 GetStr(&config->krblevel, nextarg);
692 return PARAM_LIBCURL_DOESNT_SUPPORT;
694 case 'y': /* --max-filesize */
695 err = str2offset(&config->max_filesize, nextarg);
699 case 'z': /* --disable-eprt */
700 config->disable_eprt = toggle;
702 case 'Z': /* --eprt */
703 config->disable_eprt = (!toggle)?TRUE:FALSE;
706 default: /* the URL! */
709 if(config->url_get || ((config->url_get = config->url_list) != NULL)) {
710 /* there's a node here, if it already is filled-in continue to find
712 while(config->url_get && (config->url_get->flags & GETOUT_URL))
713 config->url_get = config->url_get->next;
716 /* now there might or might not be an available node to fill in! */
720 url = config->url_get;
722 /* there was no free node, create one! */
723 url = new_getout(config);
728 /* fill in the URL */
729 GetStr(&url->url, nextarg);
730 url->flags |= GETOUT_URL;
735 case '$': /* more options without a short option */
737 case 'a': /* --ftp-ssl */
738 if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
739 return PARAM_LIBCURL_DOESNT_SUPPORT;
740 config->ftp_ssl = toggle;
742 case 'b': /* --ftp-pasv */
743 Curl_safefree(config->ftpport);
745 case 'c': /* --socks5 specifies a socks5 proxy to use, and resolves
746 the name locally and passes on the resolved address */
747 GetStr(&config->socksproxy, nextarg);
748 config->socksver = CURLPROXY_SOCKS5;
750 case 't': /* --socks4 specifies a socks4 proxy to use */
751 GetStr(&config->socksproxy, nextarg);
752 config->socksver = CURLPROXY_SOCKS4;
754 case 'T': /* --socks4a specifies a socks4a proxy to use */
755 GetStr(&config->socksproxy, nextarg);
756 config->socksver = CURLPROXY_SOCKS4A;
758 case '2': /* --socks5-hostname specifies a socks5 proxy and enables name
759 resolving with the proxy */
760 GetStr(&config->socksproxy, nextarg);
761 config->socksver = CURLPROXY_SOCKS5_HOSTNAME;
763 case 'd': /* --tcp-nodelay option */
764 config->tcp_nodelay = toggle;
766 case 'e': /* --proxy-digest */
767 config->proxydigest = toggle;
769 case 'f': /* --proxy-basic */
770 config->proxybasic = toggle;
772 case 'g': /* --retry */
773 err = str2unum(&config->req_retry, nextarg);
777 case 'h': /* --retry-delay */
778 err = str2unum(&config->retry_delay, nextarg);
782 case 'i': /* --retry-max-time */
783 err = str2unum(&config->retry_maxtime, nextarg);
788 case 'k': /* --proxy-negotiate */
789 if(curlinfo->features & CURL_VERSION_GSSNEGOTIATE)
790 config->proxynegotiate = toggle;
792 return PARAM_LIBCURL_DOESNT_SUPPORT;
794 case 'm': /* --ftp-account */
795 GetStr(&config->ftp_account, nextarg);
797 case 'n': /* --proxy-anyauth */
798 config->proxyanyauth = toggle;
800 case 'o': /* --trace-time */
801 config->tracetime = toggle;
803 case 'p': /* --ignore-content-length */
804 config->ignorecl = toggle;
806 case 'q': /* --ftp-skip-pasv-ip */
807 config->ftp_skip_ip = toggle;
809 case 'r': /* --ftp-method (undocumented at this point) */
810 config->ftp_filemethod = ftpfilemethod(config, nextarg);
812 case 's': /* --local-port */
813 rc = sscanf(nextarg, "%d - %d",
815 &config->localportrange);
817 return PARAM_BAD_USE;
819 config->localportrange = 1; /* default number of ports to try */
821 config->localportrange -= config->localport;
822 if(config->localportrange < 1) {
823 warnf(config, "bad range input\n");
824 return PARAM_BAD_USE;
828 case 'u': /* --ftp-alternative-to-user */
829 GetStr(&config->ftp_alternative_to_user, nextarg);
831 case 'v': /* --ftp-ssl-reqd */
832 if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
833 return PARAM_LIBCURL_DOESNT_SUPPORT;
834 config->ftp_ssl_reqd = toggle;
836 case 'w': /* --no-sessionid */
837 config->disable_sessionid = (!toggle)?TRUE:FALSE;
839 case 'x': /* --ftp-ssl-control */
840 if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
841 return PARAM_LIBCURL_DOESNT_SUPPORT;
842 config->ftp_ssl_control = toggle;
844 case 'y': /* --ftp-ssl-ccc */
845 config->ftp_ssl_ccc = toggle;
846 if(!config->ftp_ssl_ccc_mode)
847 config->ftp_ssl_ccc_mode = CURLFTPSSL_CCC_PASSIVE;
849 case 'j': /* --ftp-ssl-ccc-mode */
850 config->ftp_ssl_ccc = TRUE;
851 config->ftp_ssl_ccc_mode = ftpcccmethod(config, nextarg);
853 case 'z': /* --libcurl */
854 #ifdef CURL_DISABLE_LIBCURL_OPTION
856 "--libcurl option was disabled at build-time!\n");
857 return PARAM_OPTION_UNKNOWN;
859 GetStr(&config->libcurl, nextarg);
862 case '#': /* --raw */
863 config->raw = toggle;
865 case '0': /* --post301 */
866 config->post301 = toggle;
868 case '1': /* --no-keepalive */
869 config->nokeepalive = (!toggle)?TRUE:FALSE;
871 case '3': /* --keepalive-time */
872 err = str2unum(&config->alivetime, nextarg);
876 case '4': /* --post302 */
877 config->post302 = toggle;
879 case 'I': /* --post303 */
880 config->post303 = toggle;
882 case '5': /* --noproxy */
883 /* This specifies the noproxy list */
884 GetStr(&config->noproxy, nextarg);
886 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
887 case '6': /* --socks5-gssapi-service */
888 GetStr(&config->socks5_gssapi_service, nextarg);
890 case '7': /* --socks5-gssapi-nec*/
891 config->socks5_gssapi_nec = TRUE;
894 case '8': /* --proxy1.0 */
896 GetStr(&config->proxy, nextarg);
897 config->proxyver = CURLPROXY_HTTP_1_0;
899 case '9': /* --tftp-blksize */
900 err = str2unum(&config->tftp_blksize, nextarg);
904 case 'A': /* --mail-from */
905 GetStr(&config->mail_from, nextarg);
907 case 'B': /* --mail-rcpt */
908 /* append receiver to a list */
909 err = add2list(&config->mail_rcpt, nextarg);
913 case 'C': /* --ftp-pret */
914 config->ftp_pret = toggle;
916 case 'D': /* --proto */
917 config->proto_present = TRUE;
918 if(proto2num(config, &config->proto, nextarg))
919 return PARAM_BAD_USE;
921 case 'E': /* --proto-redir */
922 config->proto_redir_present = TRUE;
923 if(proto2num(config, &config->proto_redir, nextarg))
924 return PARAM_BAD_USE;
926 case 'F': /* --resolve */
927 err = add2list(&config->resolve, nextarg);
931 case 'G': /* --delegation LEVEL */
932 config->gssapi_delegation = delegation(config, nextarg);
934 case 'H': /* --mail-auth */
935 GetStr(&config->mail_auth, nextarg);
937 case 'J': /* --metalink */
940 int mlmaj, mlmin, mlpatch;
941 metalink_get_version(&mlmaj, &mlmin, &mlpatch);
942 if((mlmaj*10000)+(mlmin*100)+mlpatch < CURL_REQ_LIBMETALINK_VERS) {
944 "--metalink option cannot be used because the version of "
945 "the linked libmetalink library is too old. "
946 "Required: %d.%d.%d, found %d.%d.%d\n",
947 CURL_REQ_LIBMETALINK_MAJOR,
948 CURL_REQ_LIBMETALINK_MINOR,
949 CURL_REQ_LIBMETALINK_PATCH,
950 mlmaj, mlmin, mlpatch);
951 return PARAM_BAD_USE;
954 config->use_metalink = toggle;
956 warnf(config, "--metalink option is ignored because the binary is "
957 "built without the Metalink support.\n");
961 case 'K': /* --sasl-ir */
962 config->sasl_ir = TRUE;
966 case '#': /* --progress-bar */
968 config->progressmode = CURL_PROGRESS_BAR;
970 config->progressmode = CURL_PROGRESS_STATS;
972 case '~': /* --xattr */
973 config->xattr = toggle;
976 /* HTTP version 1.0 */
977 config->httpversion = CURL_HTTP_VERSION_1_0;
981 config->ssl_version = CURL_SSLVERSION_TLSv1;
985 config->ssl_version = CURL_SSLVERSION_SSLv2;
989 config->ssl_version = CURL_SSLVERSION_SSLv3;
993 config->ip_version = 4;
997 config->ip_version = 6;
1000 /* This makes the FTP sessions use APPE instead of STOR */
1001 config->ftp_append = toggle;
1004 /* This specifies the User-Agent name */
1005 GetStr(&config->useragent, nextarg);
1007 case 'b': /* cookie string coming up: */
1008 if(nextarg[0] == '@') {
1011 else if(strchr(nextarg, '=')) {
1012 /* A cookie string must have a =-letter */
1013 GetStr(&config->cookie, nextarg);
1016 /* We have a cookie file to read from! */
1017 GetStr(&config->cookiefile, nextarg);
1020 /* use ASCII/text when transferring */
1021 config->use_ascii = toggle;
1024 /* get the file name to dump all cookies in */
1025 GetStr(&config->cookiejar, nextarg);
1028 /* This makes us continue an ftp transfer at given position */
1029 if(!curlx_strequal(nextarg, "-")) {
1030 err = str2offset(&config->resume_from, nextarg);
1033 config->resume_from_current = FALSE;
1036 config->resume_from_current = TRUE;
1037 config->resume_from = 0;
1039 config->use_resume=TRUE;
1042 /* postfield data */
1044 char *postdata = NULL;
1048 if(subletter == 'e') { /* --data-urlencode*/
1049 /* [name]=[content], we encode the content part only
1050 * [name]@[file name]
1052 * Case 2: we first load the file using that name and then encode
1055 const char *p = strchr(nextarg, '=');
1059 /* there was no '=' letter, check for a '@' instead */
1060 p = strchr(nextarg, '@');
1062 nlen = p - nextarg; /* length of the name part */
1063 is_file = *p++; /* pass the separator */
1066 /* neither @ nor =, so no name and it isn't a file */
1070 if('@' == is_file) {
1071 /* a '@' letter, it means that a file name or - (stdin) follows */
1073 if(curlx_strequal("-", p)) {
1078 file = fopen(p, "rb");
1081 "Couldn't read data from file \"%s\", this makes "
1082 "an empty POST.\n", nextarg);
1085 err = file2memory(&postdata, &size, file);
1087 if(file && (file != stdin))
1093 GetStr(&postdata, p);
1095 size = strlen(postdata);
1099 /* no data from the file, point to a zero byte string to make this
1100 get sent as a POST anyway */
1101 postdata = strdup("");
1103 return PARAM_NO_MEM;
1107 char *enc = curl_easy_escape(config->easy, postdata, (int)size);
1108 Curl_safefree(postdata); /* no matter if it worked or not */
1110 /* now make a string with the name from above and append the
1112 size_t outlen = nlen + strlen(enc) + 2;
1113 char *n = malloc(outlen);
1116 return PARAM_NO_MEM;
1118 if(nlen > 0) { /* only append '=' if we have a name */
1119 snprintf(n, outlen, "%.*s=%s", nlen, nextarg, enc);
1124 size = outlen-2; /* since no '=' was inserted */
1130 return PARAM_NO_MEM;
1133 else if('@' == *nextarg) {
1134 /* the data begins with a '@' letter, it means that a file name
1135 or - (stdin) follows */
1136 nextarg++; /* pass the @ */
1138 if(curlx_strequal("-", nextarg)) {
1140 if(subletter == 'b') /* forced data-binary */
1144 file = fopen(nextarg, "rb");
1146 warnf(config, "Couldn't read data from file \"%s\", this makes "
1147 "an empty POST.\n", nextarg);
1150 if(subletter == 'b')
1152 err = file2memory(&postdata, &size, file);
1154 err = file2string(&postdata, file);
1156 size = strlen(postdata);
1159 if(file && (file != stdin))
1165 /* no data from the file, point to a zero byte string to make this
1166 get sent as a POST anyway */
1167 postdata = strdup("");
1169 return PARAM_NO_MEM;
1173 GetStr(&postdata, nextarg);
1175 size = strlen(postdata);
1178 #ifdef CURL_DOES_CONVERSIONS
1179 if(subletter != 'b') {
1180 /* NOT forced binary, convert to ASCII */
1181 if(convert_to_network(postdata, strlen(postdata))) {
1182 Curl_safefree(postdata);
1183 return PARAM_NO_MEM;
1188 if(config->postfields) {
1189 /* we already have a string, we append this one with a separating
1191 char *oldpost = config->postfields;
1192 curl_off_t oldlen = config->postfieldsize;
1193 curl_off_t newlen = oldlen + size + 2;
1194 config->postfields = malloc((size_t)newlen);
1195 if(!config->postfields) {
1196 Curl_safefree(oldpost);
1197 Curl_safefree(postdata);
1198 return PARAM_NO_MEM;
1200 memcpy(config->postfields, oldpost, (size_t)oldlen);
1201 /* use byte value 0x26 for '&' to accommodate non-ASCII platforms */
1202 config->postfields[oldlen] = '\x26';
1203 memcpy(&config->postfields[oldlen+1], postdata, size);
1204 config->postfields[oldlen+1+size] = '\0';
1205 Curl_safefree(oldpost);
1206 Curl_safefree(postdata);
1207 config->postfieldsize += size+1;
1210 config->postfields = postdata;
1211 config->postfieldsize = size;
1215 We can't set the request type here, as this data might be used in
1216 a simple GET if -G is used. Already or soon.
1218 if(SetHTTPrequest(HTTPREQ_SIMPLEPOST, &config->httpreq)) {
1219 Curl_safefree(postdata);
1220 return PARAM_BAD_USE;
1225 /* dump-header to given file name */
1226 GetStr(&config->headerfile, nextarg);
1230 char *ptr = strstr(nextarg, ";auto");
1232 /* Automatic referer requested, this may be combined with a
1234 config->autoreferer = TRUE;
1235 *ptr = 0; /* zero terminate here */
1238 config->autoreferer = FALSE;
1239 GetStr(&config->referer, nextarg);
1244 case 'a': /* CA info PEM file */
1245 /* CA info PEM file */
1246 GetStr(&config->cacert, nextarg);
1248 case 'b': /* cert file type */
1249 GetStr(&config->cert_type, nextarg);
1251 case 'c': /* private key file */
1252 GetStr(&config->key, nextarg);
1254 case 'd': /* private key file type */
1255 GetStr(&config->key_type, nextarg);
1257 case 'e': /* private key passphrase */
1258 GetStr(&config->key_passwd, nextarg);
1261 case 'f': /* crypto engine */
1262 GetStr(&config->engine, nextarg);
1263 if(config->engine && curlx_raw_equal(config->engine,"list"))
1264 config->list_engines = TRUE;
1266 case 'g': /* CA info PEM file */
1267 /* CA cert directory */
1268 GetStr(&config->capath, nextarg);
1270 case 'h': /* --pubkey public key file */
1271 GetStr(&config->pubkey, nextarg);
1273 case 'i': /* --hostpubmd5 md5 of the host public key */
1274 GetStr(&config->hostpubmd5, nextarg);
1275 if(!config->hostpubmd5 || strlen(config->hostpubmd5) != 32)
1276 return PARAM_BAD_USE;
1278 case 'j': /* CRL info PEM file */
1280 GetStr(&config->crlfile, nextarg);
1282 case 'k': /* TLS username */
1283 if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
1284 GetStr(&config->tls_username, nextarg);
1286 return PARAM_LIBCURL_DOESNT_SUPPORT;
1288 case 'l': /* TLS password */
1289 if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
1290 GetStr(&config->tls_password, nextarg);
1292 return PARAM_LIBCURL_DOESNT_SUPPORT;
1294 case 'm': /* TLS authentication type */
1295 if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
1296 GetStr(&config->tls_authtype, nextarg);
1297 if(!strequal(config->tls_authtype, "SRP"))
1298 return PARAM_LIBCURL_DOESNT_SUPPORT; /* only support TLS-SRP */
1301 return PARAM_LIBCURL_DOESNT_SUPPORT;
1303 case 'n': /* no empty SSL fragments */
1304 if(curlinfo->features & CURL_VERSION_SSL)
1305 config->ssl_allow_beast = toggle;
1307 default: /* certificate file */
1309 char *certname, *passphrase;
1310 parse_cert_parameter(nextarg, &certname, &passphrase);
1311 Curl_safefree(config->cert);
1312 config->cert = certname;
1314 Curl_safefree(config->key_passwd);
1315 config->key_passwd = passphrase;
1322 /* fail hard on errors */
1323 config->failonerror = toggle;
1326 /* "form data" simulation, this is a little advanced so lets do our best
1327 to sort this out slowly and carefully */
1328 if(formparse(config,
1332 (subletter=='s')?TRUE:FALSE)) /* 's' means literal string */
1333 return PARAM_BAD_USE;
1334 if(SetHTTPrequest(config, HTTPREQ_POST, &config->httpreq))
1335 return PARAM_BAD_USE;
1338 case 'g': /* g disables URLglobbing */
1339 config->globoff = toggle;
1342 case 'G': /* HTTP GET */
1343 config->use_httpget = TRUE;
1346 case 'h': /* h for help */
1349 return PARAM_HELP_REQUESTED;
1351 /* we now actually support --no-help too! */
1354 /* A custom header to append to a list */
1355 err = add2list(&config->headers, nextarg);
1360 config->include_headers = toggle; /* include the headers as well in the
1361 general output stream */
1364 config->cookiesession = toggle;
1368 * no_body will imply include_headers later on
1370 config->no_body = toggle;
1371 if(SetHTTPrequest(config,
1372 (config->no_body)?HTTPREQ_HEAD:HTTPREQ_GET,
1374 return PARAM_BAD_USE;
1376 case 'J': /* --remote-header-name */
1377 if(config->include_headers) {
1379 "--include and --remote-header-name cannot be combined.\n");
1380 return PARAM_BAD_USE;
1382 config->content_disposition = toggle;
1384 case 'k': /* allow insecure SSL connects */
1385 config->insecure_ok = toggle;
1387 case 'K': /* parse config file */
1388 if(parseconfig(nextarg, config))
1389 warnf(config, "error trying read config from the '%s' file\n",
1393 config->dirlistonly = toggle; /* only list the names of the FTP dir */
1396 config->followlocation = toggle; /* Follow Location: HTTP headers */
1397 switch (subletter) {
1399 /* Continue to send authentication (user+password) when following
1400 * locations, even when hostname changed */
1401 config->unrestricted_auth = toggle;
1406 /* specified max time */
1407 err = str2udouble(&config->timeout, nextarg);
1411 case 'M': /* M for manual, huge help */
1412 if(toggle) { /* --no-manual shows no manual... */
1415 return PARAM_HELP_REQUESTED;
1418 "built-in manual was disabled at build-time!\n");
1419 return PARAM_OPTION_UNKNOWN;
1425 case 'o': /* CA info PEM file */
1426 /* use .netrc or URL */
1427 config->netrc_opt = toggle;
1429 case 'e': /* netrc-file */
1430 GetStr(&config->netrc_file, nextarg);
1433 /* pick info from .netrc, if this is used for http, curl will
1434 automatically enfore user+password with the request */
1435 config->netrc = toggle;
1440 /* disable the output I/O buffering. note that the option is called
1441 --buffer but is mostly used in the negative form: --no-buffer */
1443 config->nobuffer = (!toggle)?TRUE:FALSE;
1445 config->nobuffer = toggle;
1447 case 'O': /* --remote-name */
1448 if(subletter == 'a') { /* --remote-name-all */
1449 config->default_node_flags = toggle?GETOUT_USEREMOTE:0;
1453 case 'o': /* --output */
1457 if(config->url_out || ((config->url_out = config->url_list) != NULL)) {
1458 /* there's a node here, if it already is filled-in continue to find
1460 while(config->url_out && (config->url_out->flags & GETOUT_OUTFILE))
1461 config->url_out = config->url_out->next;
1464 /* now there might or might not be an available node to fill in! */
1468 url = config->url_out;
1470 /* there was no free node, create one! */
1471 url = new_getout(config);
1474 return PARAM_NO_MEM;
1476 /* fill in the outfile */
1478 GetStr(&url->outfile, nextarg);
1479 url->flags &= ~GETOUT_USEREMOTE; /* switch off */
1482 url->outfile = NULL; /* leave it */
1484 url->flags |= GETOUT_USEREMOTE; /* switch on */
1486 url->flags &= ~GETOUT_USEREMOTE; /* switch off */
1488 url->flags |= GETOUT_OUTFILE;
1493 /* This makes the FTP sessions use PORT instead of PASV */
1494 /* use <eth0> or <192.168.10.10> style addresses. Anything except
1495 this will make us try to get the "default" address.
1496 NOTE: this is a changed behaviour since the released 4.1!
1498 GetStr(&config->ftpport, nextarg);
1501 /* proxy tunnel for non-http protocols */
1502 config->proxytunnel = toggle;
1505 case 'q': /* if used first, already taken care of, we do it like
1506 this so we don't cause an error! */
1509 /* QUOTE command to send to FTP server */
1510 switch(nextarg[0]) {
1512 /* prefixed with a dash makes it a POST TRANSFER one */
1514 err = add2list(&config->postquote, nextarg);
1517 /* prefixed with a plus makes it a just-before-transfer one */
1519 err = add2list(&config->prequote, nextarg);
1522 err = add2list(&config->quote, nextarg);
1529 /* Specifying a range WITHOUT A DASH will create an illegal HTTP range
1530 (and won't actually be range by definition). The man page previously
1531 claimed that to be a good way, why this code is added to work-around
1533 if(ISDIGIT(*nextarg) && !strchr(nextarg, '-')) {
1537 "A specified range MUST include at least one dash (-). "
1538 "Appending one for you!\n");
1539 off = curlx_strtoofft(nextarg, NULL, 10);
1540 snprintf(buffer, sizeof(buffer), "%" CURL_FORMAT_CURL_OFF_T "-", off);
1541 Curl_safefree(config->range);
1542 config->range = strdup(buffer);
1544 return PARAM_NO_MEM;
1547 /* byte range requested */
1549 tmp_range = nextarg;
1550 while(*tmp_range != '\0') {
1551 if(!ISDIGIT(*tmp_range) && *tmp_range != '-' && *tmp_range != ',') {
1552 warnf(config,"Invalid character is found in given range. "
1553 "A specified range MUST have only digits in "
1554 "\'start\'-\'stop\'. The server's response to this "
1555 "request is uncertain.\n");
1560 /* byte range requested */
1561 GetStr(&config->range, nextarg);
1565 /* use remote file's time */
1566 config->remote_time = toggle;
1569 /* don't show progress meter, don't show errors : */
1571 config->mute = config->noprogress = TRUE;
1573 config->mute = config->noprogress = FALSE;
1574 if(config->showerror < 0)
1575 /* if still on the default value, set showerror to the reverse of
1576 toggle. This is to allow -S and -s to be used in an independent
1577 order but still have the same effect. */
1578 config->showerror = (!toggle)?TRUE:FALSE; /* toggle off */
1582 config->showerror = toggle?1:0; /* toggle on if used with -s */
1585 /* Telnet options */
1586 err = add2list(&config->telnet_options, nextarg);
1591 /* we are uploading */
1594 if(config->url_out || ((config->url_out = config->url_list) != NULL)) {
1595 /* there's a node here, if it already is filled-in continue to find
1597 while(config->url_out && (config->url_out->flags & GETOUT_UPLOAD))
1598 config->url_out = config->url_out->next;
1601 /* now there might or might not be an available node to fill in! */
1605 url = config->url_out;
1607 /* there was no free node, create one! */
1608 url = new_getout(config);
1611 return PARAM_NO_MEM;
1613 url->flags |= GETOUT_UPLOAD; /* mark -T used */
1615 url->flags |= GETOUT_NOUPLOAD;
1617 /* "-" equals stdin, but keep the string around for now */
1618 GetStr(&url->infile, nextarg);
1625 GetStr(&config->userpwd, nextarg);
1627 err = checkpasswd("host", &config->userpwd);
1632 /* Proxy user:password */
1633 GetStr(&config->proxyuserpwd, nextarg);
1635 err = checkpasswd("proxy", &config->proxyuserpwd);
1641 /* the '%' thing here will cause the trace get sent to stderr */
1642 Curl_safefree(config->trace_dump);
1643 config->trace_dump = strdup("%");
1644 if(!config->trace_dump)
1645 return PARAM_NO_MEM;
1646 if(config->tracetype && (config->tracetype != TRACE_PLAIN))
1648 "-v, --verbose overrides an earlier trace/verbose option\n");
1649 config->tracetype = TRACE_PLAIN;
1652 /* verbose is disabled here */
1653 config->tracetype = TRACE_NONE;
1657 const char *const *proto;
1660 /* --no-version yields no output! */
1663 printf(CURL_ID "%s\n", curl_version());
1664 if(curlinfo->protocols) {
1665 printf("Protocols: ");
1666 for(proto = curlinfo->protocols; *proto; ++proto) {
1667 printf("%s ", *proto);
1669 puts(""); /* newline */
1671 if(curlinfo->features) {
1673 printf("Features: ");
1674 for(i = 0; i < sizeof(feats)/sizeof(feats[0]); i++) {
1675 if(curlinfo->features & feats[i].bitmask)
1676 printf("%s ", feats[i].name);
1679 printf("Metalink ");
1681 puts(""); /* newline */
1684 return PARAM_HELP_REQUESTED;
1686 /* get the output string */
1687 if('@' == *nextarg) {
1688 /* the data begins with a '@' letter, it means that a file name
1689 or - (stdin) follows */
1692 nextarg++; /* pass the @ */
1693 if(curlx_strequal("-", nextarg)) {
1699 file = fopen(nextarg, "r");
1701 err = file2string(&config->writeout, file);
1702 if(file && (file != stdin))
1706 if(!config->writeout)
1707 warnf(config, "Failed to read %s", fname);
1710 GetStr(&config->writeout, nextarg);
1714 GetStr(&config->proxy, nextarg);
1715 config->proxyver = CURLPROXY_HTTP;
1718 /* set custom request */
1719 GetStr(&config->customrequest, nextarg);
1722 /* low speed time */
1723 err = str2unum(&config->low_speed_time, nextarg);
1726 if(!config->low_speed_limit)
1727 config->low_speed_limit = 1;
1730 /* low speed limit */
1731 err = str2unum(&config->low_speed_limit, nextarg);
1734 if(!config->low_speed_time)
1735 config->low_speed_time = 30;
1737 case 'z': /* time condition coming up */
1742 /* If-Modified-Since: (section 14.28 in RFC2068) */
1743 config->timecond = CURL_TIMECOND_IFMODSINCE;
1746 /* If-Unmodified-Since: (section 14.24 in RFC2068) */
1747 config->timecond = CURL_TIMECOND_IFUNMODSINCE;
1751 /* Last-Modified: (section 14.29 in RFC2068) */
1752 config->timecond = CURL_TIMECOND_LASTMOD;
1757 config->condtime=curl_getdate(nextarg, &now);
1758 if(-1 == (int)config->condtime) {
1759 /* now let's see if it is a file name to get the time from instead! */
1760 struct_stat statbuf;
1761 if(-1 == stat(nextarg, &statbuf)) {
1762 /* failed, remove time condition */
1763 config->timecond = CURL_TIMECOND_NONE;
1765 "Illegal date format for -z, --timecond (and not "
1766 "a file name). Disabling time condition. "
1767 "See curl_getdate(3) for valid date syntax.\n");
1770 /* pull the time out from the file */
1771 config->condtime = statbuf.st_mtime;
1775 default: /* unknown flag */
1776 return PARAM_OPTION_UNKNOWN;
1780 } while(!longopt && !singleopt && *++parse && !*usedarg);