tool_getparam.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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. #include "tool_setup.h"
  23. #include "strcase.h"
  24. #define ENABLE_CURLX_PRINTF
  25. /* use our own printf() functions */
  26. #include "curlx.h"
  27. #include "tool_binmode.h"
  28. #include "tool_cfgable.h"
  29. #include "tool_cb_prg.h"
  30. #include "tool_convert.h"
  31. #include "tool_filetime.h"
  32. #include "tool_formparse.h"
  33. #include "tool_getparam.h"
  34. #include "tool_helpers.h"
  35. #include "tool_libinfo.h"
  36. #include "tool_metalink.h"
  37. #include "tool_msgs.h"
  38. #include "tool_paramhlp.h"
  39. #include "tool_parsecfg.h"
  40. #include "tool_main.h"
  41. #include "memdebug.h" /* keep this as LAST include */
  42. #ifdef MSDOS
  43. # define USE_WATT32
  44. #endif
  45. #define GetStr(str,val) do { \
  46. if(*(str)) { \
  47. free(*(str)); \
  48. *(str) = NULL; \
  49. } \
  50. if((val)) { \
  51. *(str) = strdup((val)); \
  52. if(!(*(str))) \
  53. return PARAM_NO_MEM; \
  54. } \
  55. } WHILE_FALSE
  56. struct LongShort {
  57. const char *letter; /* short name option */
  58. const char *lname; /* long name option */
  59. enum {
  60. ARG_NONE, /* stand-alone but not a boolean */
  61. ARG_BOOL, /* accepts a --no-[name] prefix */
  62. ARG_STRING, /* requires an argument */
  63. ARG_FILENAME /* requires an argument, usually a file name */
  64. } desc;
  65. };
  66. static const struct LongShort aliases[]= {
  67. /* 'letter' strings with more than one character have *no* short option to
  68. mention. */
  69. {"*@", "url", ARG_STRING},
  70. {"*4", "dns-ipv4-addr", ARG_STRING},
  71. {"*6", "dns-ipv6-addr", ARG_STRING},
  72. {"*a", "random-file", ARG_FILENAME},
  73. {"*b", "egd-file", ARG_STRING},
  74. {"*B", "oauth2-bearer", ARG_STRING},
  75. {"*c", "connect-timeout", ARG_STRING},
  76. {"*C", "doh-url" , ARG_STRING},
  77. {"*d", "ciphers", ARG_STRING},
  78. {"*D", "dns-interface", ARG_STRING},
  79. {"*e", "disable-epsv", ARG_BOOL},
  80. {"*f", "disallow-username-in-url", ARG_BOOL},
  81. {"*E", "epsv", ARG_BOOL},
  82. /* 'epsv' made like this to make --no-epsv and --epsv to work
  83. although --disable-epsv is the documented option */
  84. {"*F", "dns-servers", ARG_STRING},
  85. {"*g", "trace", ARG_FILENAME},
  86. {"*G", "npn", ARG_BOOL},
  87. {"*h", "trace-ascii", ARG_FILENAME},
  88. {"*H", "alpn", ARG_BOOL},
  89. {"*i", "limit-rate", ARG_STRING},
  90. {"*j", "compressed", ARG_BOOL},
  91. {"*J", "tr-encoding", ARG_BOOL},
  92. {"*k", "digest", ARG_BOOL},
  93. {"*l", "negotiate", ARG_BOOL},
  94. {"*m", "ntlm", ARG_BOOL},
  95. {"*M", "ntlm-wb", ARG_BOOL},
  96. {"*n", "basic", ARG_BOOL},
  97. {"*o", "anyauth", ARG_BOOL},
  98. #ifdef USE_WATT32
  99. {"*p", "wdebug", ARG_BOOL},
  100. #endif
  101. {"*q", "ftp-create-dirs", ARG_BOOL},
  102. {"*r", "create-dirs", ARG_BOOL},
  103. {"*s", "max-redirs", ARG_STRING},
  104. {"*t", "proxy-ntlm", ARG_BOOL},
  105. {"*u", "crlf", ARG_BOOL},
  106. {"*v", "stderr", ARG_FILENAME},
  107. {"*w", "interface", ARG_STRING},
  108. {"*x", "krb", ARG_STRING},
  109. {"*x", "krb4", ARG_STRING},
  110. /* 'krb4' is the previous name */
  111. {"*X", "haproxy-protocol", ARG_BOOL},
  112. {"*y", "max-filesize", ARG_STRING},
  113. {"*z", "disable-eprt", ARG_BOOL},
  114. {"*Z", "eprt", ARG_BOOL},
  115. /* 'eprt' made like this to make --no-eprt and --eprt to work
  116. although --disable-eprt is the documented option */
  117. {"*~", "xattr", ARG_BOOL},
  118. {"$a", "ftp-ssl", ARG_BOOL},
  119. /* 'ftp-ssl' deprecated name since 7.20.0 */
  120. {"$a", "ssl", ARG_BOOL},
  121. /* 'ssl' new option name in 7.20.0, previously this was ftp-ssl */
  122. {"$b", "ftp-pasv", ARG_BOOL},
  123. {"$c", "socks5", ARG_STRING},
  124. {"$d", "tcp-nodelay", ARG_BOOL},
  125. {"$e", "proxy-digest", ARG_BOOL},
  126. {"$f", "proxy-basic", ARG_BOOL},
  127. {"$g", "retry", ARG_STRING},
  128. {"$V", "retry-connrefused", ARG_BOOL},
  129. {"$h", "retry-delay", ARG_STRING},
  130. {"$i", "retry-max-time", ARG_STRING},
  131. {"$k", "proxy-negotiate", ARG_BOOL},
  132. {"$m", "ftp-account", ARG_STRING},
  133. {"$n", "proxy-anyauth", ARG_BOOL},
  134. {"$o", "trace-time", ARG_BOOL},
  135. {"$p", "ignore-content-length", ARG_BOOL},
  136. {"$q", "ftp-skip-pasv-ip", ARG_BOOL},
  137. {"$r", "ftp-method", ARG_STRING},
  138. {"$s", "local-port", ARG_STRING},
  139. {"$t", "socks4", ARG_STRING},
  140. {"$T", "socks4a", ARG_STRING},
  141. {"$u", "ftp-alternative-to-user", ARG_STRING},
  142. {"$v", "ftp-ssl-reqd", ARG_BOOL},
  143. /* 'ftp-ssl-reqd' deprecated name since 7.20.0 */
  144. {"$v", "ssl-reqd", ARG_BOOL},
  145. /* 'ssl-reqd' new in 7.20.0, previously this was ftp-ssl-reqd */
  146. {"$w", "sessionid", ARG_BOOL},
  147. /* 'sessionid' listed as --no-sessionid in the help */
  148. {"$x", "ftp-ssl-control", ARG_BOOL},
  149. {"$y", "ftp-ssl-ccc", ARG_BOOL},
  150. {"$j", "ftp-ssl-ccc-mode", ARG_STRING},
  151. {"$z", "libcurl", ARG_STRING},
  152. {"$#", "raw", ARG_BOOL},
  153. {"$0", "post301", ARG_BOOL},
  154. {"$1", "keepalive", ARG_BOOL},
  155. /* 'keepalive' listed as --no-keepalive in the help */
  156. {"$2", "socks5-hostname", ARG_STRING},
  157. {"$3", "keepalive-time", ARG_STRING},
  158. {"$4", "post302", ARG_BOOL},
  159. {"$5", "noproxy", ARG_STRING},
  160. {"$7", "socks5-gssapi-nec", ARG_BOOL},
  161. {"$8", "proxy1.0", ARG_STRING},
  162. {"$9", "tftp-blksize", ARG_STRING},
  163. {"$A", "mail-from", ARG_STRING},
  164. {"$B", "mail-rcpt", ARG_STRING},
  165. {"$C", "ftp-pret", ARG_BOOL},
  166. {"$D", "proto", ARG_STRING},
  167. {"$E", "proto-redir", ARG_STRING},
  168. {"$F", "resolve", ARG_STRING},
  169. {"$G", "delegation", ARG_STRING},
  170. {"$H", "mail-auth", ARG_STRING},
  171. {"$I", "post303", ARG_BOOL},
  172. {"$J", "metalink", ARG_BOOL},
  173. {"$K", "sasl-ir", ARG_BOOL},
  174. {"$L", "test-event", ARG_BOOL},
  175. {"$M", "unix-socket", ARG_FILENAME},
  176. {"$N", "path-as-is", ARG_BOOL},
  177. {"$O", "socks5-gssapi-service", ARG_STRING},
  178. /* 'socks5-gssapi-service' merged with'proxy-service-name' and
  179. deprecated since 7.49.0 */
  180. {"$O", "proxy-service-name", ARG_STRING},
  181. {"$P", "service-name", ARG_STRING},
  182. {"$Q", "proto-default", ARG_STRING},
  183. {"$R", "expect100-timeout", ARG_STRING},
  184. {"$S", "tftp-no-options", ARG_BOOL},
  185. {"$U", "connect-to", ARG_STRING},
  186. {"$W", "abstract-unix-socket", ARG_FILENAME},
  187. {"$X", "tls-max", ARG_STRING},
  188. {"$Y", "suppress-connect-headers", ARG_BOOL},
  189. {"$Z", "compressed-ssh", ARG_BOOL},
  190. {"$~", "happy-eyeballs-timeout-ms", ARG_STRING},
  191. {"0", "http1.0", ARG_NONE},
  192. {"01", "http1.1", ARG_NONE},
  193. {"02", "http2", ARG_NONE},
  194. {"03", "http2-prior-knowledge", ARG_NONE},
  195. {"04", "http3-direct", ARG_NONE},
  196. {"09", "http0.9", ARG_BOOL},
  197. {"1", "tlsv1", ARG_NONE},
  198. {"10", "tlsv1.0", ARG_NONE},
  199. {"11", "tlsv1.1", ARG_NONE},
  200. {"12", "tlsv1.2", ARG_NONE},
  201. {"13", "tlsv1.3", ARG_NONE},
  202. {"1A", "tls13-ciphers", ARG_STRING},
  203. {"1B", "proxy-tls13-ciphers", ARG_STRING},
  204. {"2", "sslv2", ARG_NONE},
  205. {"3", "sslv3", ARG_NONE},
  206. {"4", "ipv4", ARG_NONE},
  207. {"6", "ipv6", ARG_NONE},
  208. {"a", "append", ARG_BOOL},
  209. {"A", "user-agent", ARG_STRING},
  210. {"b", "cookie", ARG_STRING},
  211. {"ba", "alt-svc", ARG_STRING},
  212. {"B", "use-ascii", ARG_BOOL},
  213. {"c", "cookie-jar", ARG_STRING},
  214. {"C", "continue-at", ARG_STRING},
  215. {"d", "data", ARG_STRING},
  216. {"dr", "data-raw", ARG_STRING},
  217. {"da", "data-ascii", ARG_STRING},
  218. {"db", "data-binary", ARG_STRING},
  219. {"de", "data-urlencode", ARG_STRING},
  220. {"D", "dump-header", ARG_FILENAME},
  221. {"e", "referer", ARG_STRING},
  222. {"E", "cert", ARG_FILENAME},
  223. {"Ea", "cacert", ARG_FILENAME},
  224. {"Eb", "cert-type", ARG_STRING},
  225. {"Ec", "key", ARG_FILENAME},
  226. {"Ed", "key-type", ARG_STRING},
  227. {"Ee", "pass", ARG_STRING},
  228. {"Ef", "engine", ARG_STRING},
  229. {"Eg", "capath", ARG_FILENAME},
  230. {"Eh", "pubkey", ARG_STRING},
  231. {"Ei", "hostpubmd5", ARG_STRING},
  232. {"Ej", "crlfile", ARG_FILENAME},
  233. {"Ek", "tlsuser", ARG_STRING},
  234. {"El", "tlspassword", ARG_STRING},
  235. {"Em", "tlsauthtype", ARG_STRING},
  236. {"En", "ssl-allow-beast", ARG_BOOL},
  237. {"Eo", "login-options", ARG_STRING},
  238. {"Ep", "pinnedpubkey", ARG_STRING},
  239. {"EP", "proxy-pinnedpubkey", ARG_STRING},
  240. {"Eq", "cert-status", ARG_BOOL},
  241. {"Er", "false-start", ARG_BOOL},
  242. {"Es", "ssl-no-revoke", ARG_BOOL},
  243. {"Et", "tcp-fastopen", ARG_BOOL},
  244. {"Eu", "proxy-tlsuser", ARG_STRING},
  245. {"Ev", "proxy-tlspassword", ARG_STRING},
  246. {"Ew", "proxy-tlsauthtype", ARG_STRING},
  247. {"Ex", "proxy-cert", ARG_FILENAME},
  248. {"Ey", "proxy-cert-type", ARG_STRING},
  249. {"Ez", "proxy-key", ARG_FILENAME},
  250. {"E0", "proxy-key-type", ARG_STRING},
  251. {"E1", "proxy-pass", ARG_STRING},
  252. {"E2", "proxy-ciphers", ARG_STRING},
  253. {"E3", "proxy-crlfile", ARG_FILENAME},
  254. {"E4", "proxy-ssl-allow-beast", ARG_BOOL},
  255. {"E5", "login-options", ARG_STRING},
  256. {"E6", "proxy-cacert", ARG_FILENAME},
  257. {"E7", "proxy-capath", ARG_FILENAME},
  258. {"E8", "proxy-insecure", ARG_BOOL},
  259. {"E9", "proxy-tlsv1", ARG_NONE},
  260. {"EA", "socks5-basic", ARG_BOOL},
  261. {"EB", "socks5-gssapi", ARG_BOOL},
  262. {"f", "fail", ARG_BOOL},
  263. {"fa", "fail-early", ARG_BOOL},
  264. {"fb", "styled-output", ARG_BOOL},
  265. {"F", "form", ARG_STRING},
  266. {"Fs", "form-string", ARG_STRING},
  267. {"g", "globoff", ARG_BOOL},
  268. {"G", "get", ARG_NONE},
  269. {"Ga", "request-target", ARG_STRING},
  270. {"h", "help", ARG_BOOL},
  271. {"H", "header", ARG_STRING},
  272. {"Hp", "proxy-header", ARG_STRING},
  273. {"i", "include", ARG_BOOL},
  274. {"I", "head", ARG_BOOL},
  275. {"j", "junk-session-cookies", ARG_BOOL},
  276. {"J", "remote-header-name", ARG_BOOL},
  277. {"k", "insecure", ARG_BOOL},
  278. {"K", "config", ARG_FILENAME},
  279. {"l", "list-only", ARG_BOOL},
  280. {"L", "location", ARG_BOOL},
  281. {"Lt", "location-trusted", ARG_BOOL},
  282. {"m", "max-time", ARG_STRING},
  283. {"M", "manual", ARG_BOOL},
  284. {"n", "netrc", ARG_BOOL},
  285. {"no", "netrc-optional", ARG_BOOL},
  286. {"ne", "netrc-file", ARG_FILENAME},
  287. {"N", "buffer", ARG_BOOL},
  288. /* 'buffer' listed as --no-buffer in the help */
  289. {"o", "output", ARG_FILENAME},
  290. {"O", "remote-name", ARG_NONE},
  291. {"Oa", "remote-name-all", ARG_BOOL},
  292. {"p", "proxytunnel", ARG_BOOL},
  293. {"P", "ftp-port", ARG_STRING},
  294. {"q", "disable", ARG_BOOL},
  295. {"Q", "quote", ARG_STRING},
  296. {"r", "range", ARG_STRING},
  297. {"R", "remote-time", ARG_BOOL},
  298. {"s", "silent", ARG_BOOL},
  299. {"S", "show-error", ARG_BOOL},
  300. {"t", "telnet-option", ARG_STRING},
  301. {"T", "upload-file", ARG_FILENAME},
  302. {"u", "user", ARG_STRING},
  303. {"U", "proxy-user", ARG_STRING},
  304. {"v", "verbose", ARG_BOOL},
  305. {"V", "version", ARG_BOOL},
  306. {"w", "write-out", ARG_STRING},
  307. {"x", "proxy", ARG_STRING},
  308. {"xa", "preproxy", ARG_STRING},
  309. {"X", "request", ARG_STRING},
  310. {"Y", "speed-limit", ARG_STRING},
  311. {"y", "speed-time", ARG_STRING},
  312. {"z", "time-cond", ARG_STRING},
  313. {"Z", "parallel", ARG_BOOL},
  314. {"Zb", "parallel-max", ARG_STRING},
  315. {"#", "progress-bar", ARG_BOOL},
  316. {":", "next", ARG_NONE},
  317. };
  318. /* Split the argument of -E to 'certname' and 'passphrase' separated by colon.
  319. * We allow ':' and '\' to be escaped by '\' so that we can use certificate
  320. * nicknames containing ':'. See <https://sourceforge.net/p/curl/bugs/1196/>
  321. * for details. */
  322. #ifndef UNITTESTS
  323. static
  324. #endif
  325. void parse_cert_parameter(const char *cert_parameter,
  326. char **certname,
  327. char **passphrase)
  328. {
  329. size_t param_length = strlen(cert_parameter);
  330. size_t span;
  331. const char *param_place = NULL;
  332. char *certname_place = NULL;
  333. *certname = NULL;
  334. *passphrase = NULL;
  335. /* most trivial assumption: cert_parameter is empty */
  336. if(param_length == 0)
  337. return;
  338. /* next less trivial: cert_parameter starts 'pkcs11:' and thus
  339. * looks like a RFC7512 PKCS#11 URI which can be used as-is.
  340. * Also if cert_parameter contains no colon nor backslash, this
  341. * means no passphrase was given and no characters escaped */
  342. if(curl_strnequal(cert_parameter, "pkcs11:", 7) ||
  343. !strpbrk(cert_parameter, ":\\")) {
  344. *certname = strdup(cert_parameter);
  345. return;
  346. }
  347. /* deal with escaped chars; find unescaped colon if it exists */
  348. certname_place = malloc(param_length + 1);
  349. if(!certname_place)
  350. return;
  351. *certname = certname_place;
  352. param_place = cert_parameter;
  353. while(*param_place) {
  354. span = strcspn(param_place, ":\\");
  355. strncpy(certname_place, param_place, span);
  356. param_place += span;
  357. certname_place += span;
  358. /* we just ate all the non-special chars. now we're on either a special
  359. * char or the end of the string. */
  360. switch(*param_place) {
  361. case '\0':
  362. break;
  363. case '\\':
  364. param_place++;
  365. switch(*param_place) {
  366. case '\0':
  367. *certname_place++ = '\\';
  368. break;
  369. case '\\':
  370. *certname_place++ = '\\';
  371. param_place++;
  372. break;
  373. case ':':
  374. *certname_place++ = ':';
  375. param_place++;
  376. break;
  377. default:
  378. *certname_place++ = '\\';
  379. *certname_place++ = *param_place;
  380. param_place++;
  381. break;
  382. }
  383. break;
  384. case ':':
  385. /* Since we live in a world of weirdness and confusion, the win32
  386. dudes can use : when using drive letters and thus c:\file:password
  387. needs to work. In order not to break compatibility, we still use : as
  388. separator, but we try to detect when it is used for a file name! On
  389. windows. */
  390. #ifdef WIN32
  391. if(param_place &&
  392. (param_place == &cert_parameter[1]) &&
  393. (cert_parameter[2] == '\\' || cert_parameter[2] == '/') &&
  394. (ISALPHA(cert_parameter[0])) ) {
  395. /* colon in the second column, followed by a backslash, and the
  396. first character is an alphabetic letter:
  397. this is a drive letter colon */
  398. *certname_place++ = ':';
  399. param_place++;
  400. break;
  401. }
  402. #endif
  403. /* escaped colons and Windows drive letter colons were handled
  404. * above; if we're still here, this is a separating colon */
  405. param_place++;
  406. if(strlen(param_place) > 0) {
  407. *passphrase = strdup(param_place);
  408. }
  409. goto done;
  410. }
  411. }
  412. done:
  413. *certname_place = '\0';
  414. }
  415. static void
  416. GetFileAndPassword(char *nextarg, char **file, char **password)
  417. {
  418. char *certname, *passphrase;
  419. parse_cert_parameter(nextarg, &certname, &passphrase);
  420. Curl_safefree(*file);
  421. *file = certname;
  422. if(passphrase) {
  423. Curl_safefree(*password);
  424. *password = passphrase;
  425. }
  426. cleanarg(nextarg);
  427. }
  428. /* Get a size parameter for '--limit-rate' or '--max-filesize'.
  429. * We support a 'G', 'M' or 'K' suffix too.
  430. */
  431. static ParameterError GetSizeParameter(struct GlobalConfig *global,
  432. const char *arg,
  433. const char *which,
  434. curl_off_t *value_out)
  435. {
  436. char *unit;
  437. curl_off_t value;
  438. if(curlx_strtoofft(arg, &unit, 0, &value)) {
  439. warnf(global, "invalid number specified for %s\n", which);
  440. return PARAM_BAD_USE;
  441. }
  442. if(!*unit)
  443. unit = (char *)"b";
  444. else if(strlen(unit) > 1)
  445. unit = (char *)"w"; /* unsupported */
  446. switch(*unit) {
  447. case 'G':
  448. case 'g':
  449. if(value > (CURL_OFF_T_MAX / (1024*1024*1024)))
  450. return PARAM_NUMBER_TOO_LARGE;
  451. value *= 1024*1024*1024;
  452. break;
  453. case 'M':
  454. case 'm':
  455. if(value > (CURL_OFF_T_MAX / (1024*1024)))
  456. return PARAM_NUMBER_TOO_LARGE;
  457. value *= 1024*1024;
  458. break;
  459. case 'K':
  460. case 'k':
  461. if(value > (CURL_OFF_T_MAX / 1024))
  462. return PARAM_NUMBER_TOO_LARGE;
  463. value *= 1024;
  464. break;
  465. case 'b':
  466. case 'B':
  467. /* for plain bytes, leave as-is */
  468. break;
  469. default:
  470. warnf(global, "unsupported %s unit. Use G, M, K or B!\n", which);
  471. return PARAM_BAD_USE;
  472. }
  473. *value_out = value;
  474. return PARAM_OK;
  475. }
  476. ParameterError getparameter(const char *flag, /* f or -long-flag */
  477. char *nextarg, /* NULL if unset */
  478. bool *usedarg, /* set to TRUE if the arg
  479. has been used */
  480. struct GlobalConfig *global,
  481. struct OperationConfig *config)
  482. {
  483. char letter;
  484. char subletter = '\0'; /* subletters can only occur on long options */
  485. int rc;
  486. const char *parse = NULL;
  487. unsigned int j;
  488. time_t now;
  489. int hit = -1;
  490. bool longopt = FALSE;
  491. bool singleopt = FALSE; /* when true means '-o foo' used '-ofoo' */
  492. ParameterError err;
  493. bool toggle = TRUE; /* how to switch boolean options, on or off. Controlled
  494. by using --OPTION or --no-OPTION */
  495. *usedarg = FALSE; /* default is that we don't use the arg */
  496. if(('-' != flag[0]) || ('-' == flag[1])) {
  497. /* this should be a long name */
  498. const char *word = ('-' == flag[0]) ? flag + 2 : flag;
  499. size_t fnam = strlen(word);
  500. int numhits = 0;
  501. bool noflagged = FALSE;
  502. if(!strncmp(word, "no-", 3)) {
  503. /* disable this option but ignore the "no-" part when looking for it */
  504. word += 3;
  505. toggle = FALSE;
  506. noflagged = TRUE;
  507. }
  508. for(j = 0; j < sizeof(aliases)/sizeof(aliases[0]); j++) {
  509. if(curl_strnequal(aliases[j].lname, word, fnam)) {
  510. longopt = TRUE;
  511. numhits++;
  512. if(curl_strequal(aliases[j].lname, word)) {
  513. parse = aliases[j].letter;
  514. hit = j;
  515. numhits = 1; /* a single unique hit */
  516. break;
  517. }
  518. parse = aliases[j].letter;
  519. hit = j;
  520. }
  521. }
  522. if(numhits > 1) {
  523. /* this is at least the second match! */
  524. return PARAM_OPTION_AMBIGUOUS;
  525. }
  526. if(hit < 0) {
  527. return PARAM_OPTION_UNKNOWN;
  528. }
  529. if(noflagged && (aliases[hit].desc != ARG_BOOL))
  530. /* --no- prefixed an option that isn't boolean! */
  531. return PARAM_NO_NOT_BOOLEAN;
  532. }
  533. else {
  534. flag++; /* prefixed with one dash, pass it */
  535. hit = -1;
  536. parse = flag;
  537. }
  538. do {
  539. /* we can loop here if we have multiple single-letters */
  540. if(!longopt) {
  541. letter = (char)*parse;
  542. subletter = '\0';
  543. }
  544. else {
  545. letter = parse[0];
  546. subletter = parse[1];
  547. }
  548. if(hit < 0) {
  549. for(j = 0; j < sizeof(aliases)/sizeof(aliases[0]); j++) {
  550. if(letter == aliases[j].letter[0]) {
  551. hit = j;
  552. break;
  553. }
  554. }
  555. if(hit < 0) {
  556. return PARAM_OPTION_UNKNOWN;
  557. }
  558. }
  559. if(aliases[hit].desc >= ARG_STRING) {
  560. /* this option requires an extra parameter */
  561. if(!longopt && parse[1]) {
  562. nextarg = (char *)&parse[1]; /* this is the actual extra parameter */
  563. singleopt = TRUE; /* don't loop anymore after this */
  564. }
  565. else if(!nextarg)
  566. return PARAM_REQUIRES_PARAMETER;
  567. else
  568. *usedarg = TRUE; /* mark it as used */
  569. if((aliases[hit].desc == ARG_FILENAME) &&
  570. (nextarg[0] == '-') && nextarg[1]) {
  571. /* if the file name looks like a command line option */
  572. warnf(global, "The file name argument '%s' looks like a flag.\n",
  573. nextarg);
  574. }
  575. }
  576. else if((aliases[hit].desc == ARG_NONE) && !toggle)
  577. return PARAM_NO_PREFIX;
  578. switch(letter) {
  579. case '*': /* options without a short option */
  580. switch(subletter) {
  581. case '4': /* --dns-ipv4-addr */
  582. /* addr in dot notation */
  583. GetStr(&config->dns_ipv4_addr, nextarg);
  584. break;
  585. case '6': /* --dns-ipv6-addr */
  586. /* addr in dot notation */
  587. GetStr(&config->dns_ipv6_addr, nextarg);
  588. break;
  589. case 'a': /* random-file */
  590. GetStr(&config->random_file, nextarg);
  591. break;
  592. case 'b': /* egd-file */
  593. GetStr(&config->egd_file, nextarg);
  594. break;
  595. case 'B': /* OAuth 2.0 bearer token */
  596. GetStr(&config->oauth_bearer, nextarg);
  597. config->authtype |= CURLAUTH_BEARER;
  598. break;
  599. case 'c': /* connect-timeout */
  600. err = str2udouble(&config->connecttimeout, nextarg,
  601. LONG_MAX/1000);
  602. if(err)
  603. return err;
  604. break;
  605. case 'C': /* doh-url */
  606. GetStr(&config->doh_url, nextarg);
  607. break;
  608. case 'd': /* ciphers */
  609. GetStr(&config->cipher_list, nextarg);
  610. break;
  611. case 'D': /* --dns-interface */
  612. /* interface name */
  613. GetStr(&config->dns_interface, nextarg);
  614. break;
  615. case 'e': /* --disable-epsv */
  616. config->disable_epsv = toggle;
  617. break;
  618. case 'f': /* --disallow-username-in-url */
  619. config->disallow_username_in_url = toggle;
  620. break;
  621. case 'E': /* --epsv */
  622. config->disable_epsv = (!toggle)?TRUE:FALSE;
  623. break;
  624. case 'F': /* --dns-servers */
  625. /* IP addrs of DNS servers */
  626. GetStr(&config->dns_servers, nextarg);
  627. break;
  628. case 'g': /* --trace */
  629. GetStr(&global->trace_dump, nextarg);
  630. if(global->tracetype && (global->tracetype != TRACE_BIN))
  631. warnf(global, "--trace overrides an earlier trace/verbose option\n");
  632. global->tracetype = TRACE_BIN;
  633. break;
  634. case 'G': /* --npn */
  635. config->nonpn = (!toggle)?TRUE:FALSE;
  636. break;
  637. case 'h': /* --trace-ascii */
  638. GetStr(&global->trace_dump, nextarg);
  639. if(global->tracetype && (global->tracetype != TRACE_ASCII))
  640. warnf(global,
  641. "--trace-ascii overrides an earlier trace/verbose option\n");
  642. global->tracetype = TRACE_ASCII;
  643. break;
  644. case 'H': /* --alpn */
  645. config->noalpn = (!toggle)?TRUE:FALSE;
  646. break;
  647. case 'i': /* --limit-rate */
  648. {
  649. curl_off_t value;
  650. ParameterError pe = GetSizeParameter(global, nextarg, "rate", &value);
  651. if(pe != PARAM_OK)
  652. return pe;
  653. config->recvpersecond = value;
  654. config->sendpersecond = value;
  655. }
  656. break;
  657. case 'j': /* --compressed */
  658. if(toggle &&
  659. !(curlinfo->features & (CURL_VERSION_LIBZ | CURL_VERSION_BROTLI)))
  660. return PARAM_LIBCURL_DOESNT_SUPPORT;
  661. config->encoding = toggle;
  662. break;
  663. case 'J': /* --tr-encoding */
  664. config->tr_encoding = toggle;
  665. break;
  666. case 'k': /* --digest */
  667. if(toggle)
  668. config->authtype |= CURLAUTH_DIGEST;
  669. else
  670. config->authtype &= ~CURLAUTH_DIGEST;
  671. break;
  672. case 'l': /* --negotiate */
  673. if(toggle) {
  674. if(curlinfo->features & CURL_VERSION_SPNEGO)
  675. config->authtype |= CURLAUTH_NEGOTIATE;
  676. else
  677. return PARAM_LIBCURL_DOESNT_SUPPORT;
  678. }
  679. else
  680. config->authtype &= ~CURLAUTH_NEGOTIATE;
  681. break;
  682. case 'm': /* --ntlm */
  683. if(toggle) {
  684. if(curlinfo->features & CURL_VERSION_NTLM)
  685. config->authtype |= CURLAUTH_NTLM;
  686. else
  687. return PARAM_LIBCURL_DOESNT_SUPPORT;
  688. }
  689. else
  690. config->authtype &= ~CURLAUTH_NTLM;
  691. break;
  692. case 'M': /* --ntlm-wb */
  693. if(toggle) {
  694. if(curlinfo->features & CURL_VERSION_NTLM_WB)
  695. config->authtype |= CURLAUTH_NTLM_WB;
  696. else
  697. return PARAM_LIBCURL_DOESNT_SUPPORT;
  698. }
  699. else
  700. config->authtype &= ~CURLAUTH_NTLM_WB;
  701. break;
  702. case 'n': /* --basic for completeness */
  703. if(toggle)
  704. config->authtype |= CURLAUTH_BASIC;
  705. else
  706. config->authtype &= ~CURLAUTH_BASIC;
  707. break;
  708. case 'o': /* --anyauth, let libcurl pick it */
  709. if(toggle)
  710. config->authtype = CURLAUTH_ANY;
  711. /* --no-anyauth simply doesn't touch it */
  712. break;
  713. #ifdef USE_WATT32
  714. case 'p': /* --wdebug */
  715. dbug_init();
  716. break;
  717. #endif
  718. case 'q': /* --ftp-create-dirs */
  719. config->ftp_create_dirs = toggle;
  720. break;
  721. case 'r': /* --create-dirs */
  722. config->create_dirs = toggle;
  723. break;
  724. case 's': /* --max-redirs */
  725. /* specified max no of redirects (http(s)), this accepts -1 as a
  726. special condition */
  727. err = str2num(&config->maxredirs, nextarg);
  728. if(err)
  729. return err;
  730. if(config->maxredirs < -1)
  731. return PARAM_BAD_NUMERIC;
  732. break;
  733. case 't': /* --proxy-ntlm */
  734. if(curlinfo->features & CURL_VERSION_NTLM)
  735. config->proxyntlm = toggle;
  736. else
  737. return PARAM_LIBCURL_DOESNT_SUPPORT;
  738. break;
  739. case 'u': /* --crlf */
  740. /* LF -> CRLF conversion? */
  741. config->crlf = toggle;
  742. break;
  743. case 'v': /* --stderr */
  744. if(strcmp(nextarg, "-")) {
  745. FILE *newfile = fopen(nextarg, FOPEN_WRITETEXT);
  746. if(!newfile)
  747. warnf(global, "Failed to open %s!\n", nextarg);
  748. else {
  749. if(global->errors_fopened)
  750. fclose(global->errors);
  751. global->errors = newfile;
  752. global->errors_fopened = TRUE;
  753. }
  754. }
  755. else
  756. global->errors = stdout;
  757. break;
  758. case 'w': /* --interface */
  759. /* interface */
  760. GetStr(&config->iface, nextarg);
  761. break;
  762. case 'x': /* --krb */
  763. /* kerberos level string */
  764. if(curlinfo->features & CURL_VERSION_KERBEROS4)
  765. GetStr(&config->krblevel, nextarg);
  766. else
  767. return PARAM_LIBCURL_DOESNT_SUPPORT;
  768. break;
  769. case 'X': /* --haproxy-protocol */
  770. config->haproxy_protocol = toggle;
  771. break;
  772. case 'y': /* --max-filesize */
  773. {
  774. curl_off_t value;
  775. ParameterError pe =
  776. GetSizeParameter(global, nextarg, "max-filesize", &value);
  777. if(pe != PARAM_OK)
  778. return pe;
  779. config->max_filesize = value;
  780. }
  781. break;
  782. case 'z': /* --disable-eprt */
  783. config->disable_eprt = toggle;
  784. break;
  785. case 'Z': /* --eprt */
  786. config->disable_eprt = (!toggle)?TRUE:FALSE;
  787. break;
  788. case '~': /* --xattr */
  789. config->xattr = toggle;
  790. break;
  791. case '@': /* the URL! */
  792. {
  793. struct getout *url;
  794. if(!config->url_get)
  795. config->url_get = config->url_list;
  796. if(config->url_get) {
  797. /* there's a node here, if it already is filled-in continue to find
  798. an "empty" node */
  799. while(config->url_get && (config->url_get->flags & GETOUT_URL))
  800. config->url_get = config->url_get->next;
  801. }
  802. /* now there might or might not be an available node to fill in! */
  803. if(config->url_get)
  804. /* existing node */
  805. url = config->url_get;
  806. else
  807. /* there was no free node, create one! */
  808. config->url_get = url = new_getout(config);
  809. if(!url)
  810. return PARAM_NO_MEM;
  811. /* fill in the URL */
  812. GetStr(&url->url, nextarg);
  813. url->flags |= GETOUT_URL;
  814. }
  815. }
  816. break;
  817. case '$': /* more options without a short option */
  818. switch(subletter) {
  819. case 'a': /* --ssl */
  820. if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
  821. return PARAM_LIBCURL_DOESNT_SUPPORT;
  822. config->ftp_ssl = toggle;
  823. break;
  824. case 'b': /* --ftp-pasv */
  825. Curl_safefree(config->ftpport);
  826. break;
  827. case 'c': /* --socks5 specifies a socks5 proxy to use, and resolves
  828. the name locally and passes on the resolved address */
  829. GetStr(&config->proxy, nextarg);
  830. config->proxyver = CURLPROXY_SOCKS5;
  831. break;
  832. case 't': /* --socks4 specifies a socks4 proxy to use */
  833. GetStr(&config->proxy, nextarg);
  834. config->proxyver = CURLPROXY_SOCKS4;
  835. break;
  836. case 'T': /* --socks4a specifies a socks4a proxy to use */
  837. GetStr(&config->proxy, nextarg);
  838. config->proxyver = CURLPROXY_SOCKS4A;
  839. break;
  840. case '2': /* --socks5-hostname specifies a socks5 proxy and enables name
  841. resolving with the proxy */
  842. GetStr(&config->proxy, nextarg);
  843. config->proxyver = CURLPROXY_SOCKS5_HOSTNAME;
  844. break;
  845. case 'd': /* --tcp-nodelay option */
  846. config->tcp_nodelay = toggle;
  847. break;
  848. case 'e': /* --proxy-digest */
  849. config->proxydigest = toggle;
  850. break;
  851. case 'f': /* --proxy-basic */
  852. config->proxybasic = toggle;
  853. break;
  854. case 'g': /* --retry */
  855. err = str2unum(&config->req_retry, nextarg);
  856. if(err)
  857. return err;
  858. break;
  859. case 'V': /* --retry-connrefused */
  860. config->retry_connrefused = toggle;
  861. break;
  862. case 'h': /* --retry-delay */
  863. err = str2unummax(&config->retry_delay, nextarg, LONG_MAX/1000);
  864. if(err)
  865. return err;
  866. break;
  867. case 'i': /* --retry-max-time */
  868. err = str2unummax(&config->retry_maxtime, nextarg, LONG_MAX/1000);
  869. if(err)
  870. return err;
  871. break;
  872. case 'k': /* --proxy-negotiate */
  873. if(curlinfo->features & CURL_VERSION_SPNEGO)
  874. config->proxynegotiate = toggle;
  875. else
  876. return PARAM_LIBCURL_DOESNT_SUPPORT;
  877. break;
  878. case 'm': /* --ftp-account */
  879. GetStr(&config->ftp_account, nextarg);
  880. break;
  881. case 'n': /* --proxy-anyauth */
  882. config->proxyanyauth = toggle;
  883. break;
  884. case 'o': /* --trace-time */
  885. global->tracetime = toggle;
  886. break;
  887. case 'p': /* --ignore-content-length */
  888. config->ignorecl = toggle;
  889. break;
  890. case 'q': /* --ftp-skip-pasv-ip */
  891. config->ftp_skip_ip = toggle;
  892. break;
  893. case 'r': /* --ftp-method (undocumented at this point) */
  894. config->ftp_filemethod = ftpfilemethod(config, nextarg);
  895. break;
  896. case 's': { /* --local-port */
  897. char lrange[7]; /* 16bit base 10 is 5 digits, but we allow 6 so that
  898. this catches overflows, not just truncates */
  899. char *p = nextarg;
  900. while(ISDIGIT(*p))
  901. p++;
  902. if(*p) {
  903. /* if there's anything more than a plain decimal number */
  904. rc = sscanf(p, " - %6s", lrange);
  905. *p = 0; /* zero terminate to make str2unum() work below */
  906. }
  907. else
  908. rc = 0;
  909. err = str2unum(&config->localport, nextarg);
  910. if(err || (config->localport > 65535))
  911. return PARAM_BAD_USE;
  912. if(!rc)
  913. config->localportrange = 1; /* default number of ports to try */
  914. else {
  915. err = str2unum(&config->localportrange, lrange);
  916. if(err || (config->localportrange > 65535))
  917. return PARAM_BAD_USE;
  918. config->localportrange -= (config->localport-1);
  919. if(config->localportrange < 1)
  920. return PARAM_BAD_USE;
  921. }
  922. break;
  923. }
  924. case 'u': /* --ftp-alternative-to-user */
  925. GetStr(&config->ftp_alternative_to_user, nextarg);
  926. break;
  927. case 'v': /* --ssl-reqd */
  928. if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
  929. return PARAM_LIBCURL_DOESNT_SUPPORT;
  930. config->ftp_ssl_reqd = toggle;
  931. break;
  932. case 'w': /* --no-sessionid */
  933. config->disable_sessionid = (!toggle)?TRUE:FALSE;
  934. break;
  935. case 'x': /* --ftp-ssl-control */
  936. if(toggle && !(curlinfo->features & CURL_VERSION_SSL))
  937. return PARAM_LIBCURL_DOESNT_SUPPORT;
  938. config->ftp_ssl_control = toggle;
  939. break;
  940. case 'y': /* --ftp-ssl-ccc */
  941. config->ftp_ssl_ccc = toggle;
  942. if(!config->ftp_ssl_ccc_mode)
  943. config->ftp_ssl_ccc_mode = CURLFTPSSL_CCC_PASSIVE;
  944. break;
  945. case 'j': /* --ftp-ssl-ccc-mode */
  946. config->ftp_ssl_ccc = TRUE;
  947. config->ftp_ssl_ccc_mode = ftpcccmethod(config, nextarg);
  948. break;
  949. case 'z': /* --libcurl */
  950. #ifdef CURL_DISABLE_LIBCURL_OPTION
  951. warnf(global,
  952. "--libcurl option was disabled at build-time!\n");
  953. return PARAM_OPTION_UNKNOWN;
  954. #else
  955. GetStr(&global->libcurl, nextarg);
  956. break;
  957. #endif
  958. case '#': /* --raw */
  959. config->raw = toggle;
  960. break;
  961. case '0': /* --post301 */
  962. config->post301 = toggle;
  963. break;
  964. case '1': /* --no-keepalive */
  965. config->nokeepalive = (!toggle)?TRUE:FALSE;
  966. break;
  967. case '3': /* --keepalive-time */
  968. err = str2unum(&config->alivetime, nextarg);
  969. if(err)
  970. return err;
  971. break;
  972. case '4': /* --post302 */
  973. config->post302 = toggle;
  974. break;
  975. case 'I': /* --post303 */
  976. config->post303 = toggle;
  977. break;
  978. case '5': /* --noproxy */
  979. /* This specifies the noproxy list */
  980. GetStr(&config->noproxy, nextarg);
  981. break;
  982. case '7': /* --socks5-gssapi-nec*/
  983. config->socks5_gssapi_nec = toggle;
  984. break;
  985. case '8': /* --proxy1.0 */
  986. /* http 1.0 proxy */
  987. GetStr(&config->proxy, nextarg);
  988. config->proxyver = CURLPROXY_HTTP_1_0;
  989. break;
  990. case '9': /* --tftp-blksize */
  991. err = str2unum(&config->tftp_blksize, nextarg);
  992. if(err)
  993. return err;
  994. break;
  995. case 'A': /* --mail-from */
  996. GetStr(&config->mail_from, nextarg);
  997. break;
  998. case 'B': /* --mail-rcpt */
  999. /* append receiver to a list */
  1000. err = add2list(&config->mail_rcpt, nextarg);
  1001. if(err)
  1002. return err;
  1003. break;
  1004. case 'C': /* --ftp-pret */
  1005. config->ftp_pret = toggle;
  1006. break;
  1007. case 'D': /* --proto */
  1008. config->proto_present = TRUE;
  1009. if(proto2num(config, &config->proto, nextarg))
  1010. return PARAM_BAD_USE;
  1011. break;
  1012. case 'E': /* --proto-redir */
  1013. config->proto_redir_present = TRUE;
  1014. if(proto2num(config, &config->proto_redir, nextarg))
  1015. return PARAM_BAD_USE;
  1016. break;
  1017. case 'F': /* --resolve */
  1018. err = add2list(&config->resolve, nextarg);
  1019. if(err)
  1020. return err;
  1021. break;
  1022. case 'G': /* --delegation LEVEL */
  1023. config->gssapi_delegation = delegation(config, nextarg);
  1024. break;
  1025. case 'H': /* --mail-auth */
  1026. GetStr(&config->mail_auth, nextarg);
  1027. break;
  1028. case 'J': /* --metalink */
  1029. {
  1030. #ifdef USE_METALINK
  1031. int mlmaj, mlmin, mlpatch;
  1032. metalink_get_version(&mlmaj, &mlmin, &mlpatch);
  1033. if((mlmaj*10000)+(mlmin*100) + mlpatch < CURL_REQ_LIBMETALINK_VERS) {
  1034. warnf(global,
  1035. "--metalink option cannot be used because the version of "
  1036. "the linked libmetalink library is too old. "
  1037. "Required: %d.%d.%d, found %d.%d.%d\n",
  1038. CURL_REQ_LIBMETALINK_MAJOR,
  1039. CURL_REQ_LIBMETALINK_MINOR,
  1040. CURL_REQ_LIBMETALINK_PATCH,
  1041. mlmaj, mlmin, mlpatch);
  1042. return PARAM_BAD_USE;
  1043. }
  1044. else
  1045. config->use_metalink = toggle;
  1046. #else
  1047. warnf(global, "--metalink option is ignored because the binary is "
  1048. "built without the Metalink support.\n");
  1049. #endif
  1050. break;
  1051. }
  1052. case 'K': /* --sasl-ir */
  1053. config->sasl_ir = toggle;
  1054. break;
  1055. case 'L': /* --test-event */
  1056. #ifdef CURLDEBUG
  1057. global->test_event_based = toggle;
  1058. #else
  1059. warnf(global, "--test-event is ignored unless a debug build!\n");
  1060. #endif
  1061. break;
  1062. case 'M': /* --unix-socket */
  1063. config->abstract_unix_socket = FALSE;
  1064. GetStr(&config->unix_socket_path, nextarg);
  1065. break;
  1066. case 'N': /* --path-as-is */
  1067. config->path_as_is = toggle;
  1068. break;
  1069. case 'O': /* --proxy-service-name */
  1070. GetStr(&config->proxy_service_name, nextarg);
  1071. break;
  1072. case 'P': /* --service-name */
  1073. GetStr(&config->service_name, nextarg);
  1074. break;
  1075. case 'Q': /* --proto-default */
  1076. GetStr(&config->proto_default, nextarg);
  1077. err = check_protocol(config->proto_default);
  1078. if(err)
  1079. return err;
  1080. break;
  1081. case 'R': /* --expect100-timeout */
  1082. err = str2udouble(&config->expect100timeout, nextarg, LONG_MAX/1000);
  1083. if(err)
  1084. return err;
  1085. break;
  1086. case 'S': /* --tftp-no-options */
  1087. config->tftp_no_options = toggle;
  1088. break;
  1089. case 'U': /* --connect-to */
  1090. err = add2list(&config->connect_to, nextarg);
  1091. if(err)
  1092. return err;
  1093. break;
  1094. case 'W': /* --abstract-unix-socket */
  1095. config->abstract_unix_socket = TRUE;
  1096. GetStr(&config->unix_socket_path, nextarg);
  1097. break;
  1098. case 'X': /* --tls-max */
  1099. err = str2tls_max(&config->ssl_version_max, nextarg);
  1100. if(err)
  1101. return err;
  1102. break;
  1103. case 'Y': /* --suppress-connect-headers */
  1104. config->suppress_connect_headers = toggle;
  1105. break;
  1106. case 'Z': /* --compressed-ssh */
  1107. config->ssh_compression = toggle;
  1108. break;
  1109. case '~': /* --happy-eyeballs-timeout-ms */
  1110. err = str2unum(&config->happy_eyeballs_timeout_ms, nextarg);
  1111. if(err)
  1112. return err;
  1113. /* 0 is a valid value for this timeout */
  1114. break;
  1115. }
  1116. break;
  1117. case '#': /* --progress-bar */
  1118. if(toggle)
  1119. global->progressmode = CURL_PROGRESS_BAR;
  1120. else
  1121. global->progressmode = CURL_PROGRESS_STATS;
  1122. break;
  1123. case ':': /* --next */
  1124. return PARAM_NEXT_OPERATION;
  1125. case '0': /* --http* options */
  1126. switch(subletter) {
  1127. case '\0':
  1128. /* HTTP version 1.0 */
  1129. config->httpversion = CURL_HTTP_VERSION_1_0;
  1130. break;
  1131. case '1':
  1132. /* HTTP version 1.1 */
  1133. config->httpversion = CURL_HTTP_VERSION_1_1;
  1134. break;
  1135. case '2':
  1136. /* HTTP version 2.0 */
  1137. config->httpversion = CURL_HTTP_VERSION_2_0;
  1138. break;
  1139. case '3': /* --http2-prior-knowledge */
  1140. /* HTTP version 2.0 over clean TCP*/
  1141. config->httpversion = CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE;
  1142. break;
  1143. case '4': /* --http3-direct */
  1144. /* HTTP version 3 over QUIC - at once */
  1145. config->h3direct = toggle;
  1146. break;
  1147. case '9':
  1148. /* Allow HTTP/0.9 responses! */
  1149. config->http09_allowed = toggle;
  1150. break;
  1151. }
  1152. break;
  1153. case '1': /* --tlsv1* options */
  1154. switch(subletter) {
  1155. case '\0':
  1156. /* TLS version 1.x */
  1157. config->ssl_version = CURL_SSLVERSION_TLSv1;
  1158. break;
  1159. case '0':
  1160. /* TLS version 1.0 */
  1161. config->ssl_version = CURL_SSLVERSION_TLSv1_0;
  1162. break;
  1163. case '1':
  1164. /* TLS version 1.1 */
  1165. config->ssl_version = CURL_SSLVERSION_TLSv1_1;
  1166. break;
  1167. case '2':
  1168. /* TLS version 1.2 */
  1169. config->ssl_version = CURL_SSLVERSION_TLSv1_2;
  1170. break;
  1171. case '3':
  1172. /* TLS version 1.3 */
  1173. config->ssl_version = CURL_SSLVERSION_TLSv1_3;
  1174. break;
  1175. case 'A': /* --tls13-ciphers */
  1176. GetStr(&config->cipher13_list, nextarg);
  1177. break;
  1178. case 'B': /* --proxy-tls13-ciphers */
  1179. GetStr(&config->proxy_cipher13_list, nextarg);
  1180. break;
  1181. }
  1182. break;
  1183. case '2':
  1184. /* SSL version 2 */
  1185. config->ssl_version = CURL_SSLVERSION_SSLv2;
  1186. break;
  1187. case '3':
  1188. /* SSL version 3 */
  1189. config->ssl_version = CURL_SSLVERSION_SSLv3;
  1190. break;
  1191. case '4':
  1192. /* IPv4 */
  1193. config->ip_version = 4;
  1194. break;
  1195. case '6':
  1196. /* IPv6 */
  1197. config->ip_version = 6;
  1198. break;
  1199. case 'a':
  1200. /* This makes the FTP sessions use APPE instead of STOR */
  1201. config->ftp_append = toggle;
  1202. break;
  1203. case 'A':
  1204. /* This specifies the User-Agent name */
  1205. GetStr(&config->useragent, nextarg);
  1206. break;
  1207. case 'b':
  1208. switch(subletter) {
  1209. case 'a': /* --alt-svc */
  1210. GetStr(&config->altsvc, nextarg);
  1211. break;
  1212. default: /* --cookie string coming up: */
  1213. if(nextarg[0] == '@') {
  1214. nextarg++;
  1215. }
  1216. else if(strchr(nextarg, '=')) {
  1217. /* A cookie string must have a =-letter */
  1218. GetStr(&config->cookie, nextarg);
  1219. break;
  1220. }
  1221. /* We have a cookie file to read from! */
  1222. GetStr(&config->cookiefile, nextarg);
  1223. }
  1224. break;
  1225. case 'B':
  1226. /* use ASCII/text when transferring */
  1227. config->use_ascii = toggle;
  1228. break;
  1229. case 'c':
  1230. /* get the file name to dump all cookies in */
  1231. GetStr(&config->cookiejar, nextarg);
  1232. break;
  1233. case 'C':
  1234. /* This makes us continue an ftp transfer at given position */
  1235. if(strcmp(nextarg, "-")) {
  1236. err = str2offset(&config->resume_from, nextarg);
  1237. if(err)
  1238. return err;
  1239. config->resume_from_current = FALSE;
  1240. }
  1241. else {
  1242. config->resume_from_current = TRUE;
  1243. config->resume_from = 0;
  1244. }
  1245. config->use_resume = TRUE;
  1246. break;
  1247. case 'd':
  1248. /* postfield data */
  1249. {
  1250. char *postdata = NULL;
  1251. FILE *file;
  1252. size_t size = 0;
  1253. bool raw_mode = (subletter == 'r');
  1254. if(subletter == 'e') { /* --data-urlencode*/
  1255. /* [name]=[content], we encode the content part only
  1256. * [name]@[file name]
  1257. *
  1258. * Case 2: we first load the file using that name and then encode
  1259. * the content.
  1260. */
  1261. const char *p = strchr(nextarg, '=');
  1262. size_t nlen;
  1263. char is_file;
  1264. if(!p)
  1265. /* there was no '=' letter, check for a '@' instead */
  1266. p = strchr(nextarg, '@');
  1267. if(p) {
  1268. nlen = p - nextarg; /* length of the name part */
  1269. is_file = *p++; /* pass the separator */
  1270. }
  1271. else {
  1272. /* neither @ nor =, so no name and it isn't a file */
  1273. nlen = is_file = 0;
  1274. p = nextarg;
  1275. }
  1276. if('@' == is_file) {
  1277. /* a '@' letter, it means that a file name or - (stdin) follows */
  1278. if(!strcmp("-", p)) {
  1279. file = stdin;
  1280. set_binmode(stdin);
  1281. }
  1282. else {
  1283. file = fopen(p, "rb");
  1284. if(!file)
  1285. warnf(global,
  1286. "Couldn't read data from file \"%s\", this makes "
  1287. "an empty POST.\n", nextarg);
  1288. }
  1289. err = file2memory(&postdata, &size, file);
  1290. if(file && (file != stdin))
  1291. fclose(file);
  1292. if(err)
  1293. return err;
  1294. }
  1295. else {
  1296. GetStr(&postdata, p);
  1297. if(postdata)
  1298. size = strlen(postdata);
  1299. }
  1300. if(!postdata) {
  1301. /* no data from the file, point to a zero byte string to make this
  1302. get sent as a POST anyway */
  1303. postdata = strdup("");
  1304. if(!postdata)
  1305. return PARAM_NO_MEM;
  1306. size = 0;
  1307. }
  1308. else {
  1309. char *enc = curl_easy_escape(NULL, postdata, (int)size);
  1310. Curl_safefree(postdata); /* no matter if it worked or not */
  1311. if(enc) {
  1312. /* now make a string with the name from above and append the
  1313. encoded string */
  1314. size_t outlen = nlen + strlen(enc) + 2;
  1315. char *n = malloc(outlen);
  1316. if(!n) {
  1317. curl_free(enc);
  1318. return PARAM_NO_MEM;
  1319. }
  1320. if(nlen > 0) { /* only append '=' if we have a name */
  1321. msnprintf(n, outlen, "%.*s=%s", nlen, nextarg, enc);
  1322. size = outlen-1;
  1323. }
  1324. else {
  1325. strcpy(n, enc);
  1326. size = outlen-2; /* since no '=' was inserted */
  1327. }
  1328. curl_free(enc);
  1329. postdata = n;
  1330. }
  1331. else
  1332. return PARAM_NO_MEM;
  1333. }
  1334. }
  1335. else if('@' == *nextarg && !raw_mode) {
  1336. /* the data begins with a '@' letter, it means that a file name
  1337. or - (stdin) follows */
  1338. nextarg++; /* pass the @ */
  1339. if(!strcmp("-", nextarg)) {
  1340. file = stdin;
  1341. if(subletter == 'b') /* forced data-binary */
  1342. set_binmode(stdin);
  1343. }
  1344. else {
  1345. file = fopen(nextarg, "rb");
  1346. if(!file)
  1347. warnf(global, "Couldn't read data from file \"%s\", this makes "
  1348. "an empty POST.\n", nextarg);
  1349. }
  1350. if(subletter == 'b')
  1351. /* forced binary */
  1352. err = file2memory(&postdata, &size, file);
  1353. else {
  1354. err = file2string(&postdata, file);
  1355. if(postdata)
  1356. size = strlen(postdata);
  1357. }
  1358. if(file && (file != stdin))
  1359. fclose(file);
  1360. if(err)
  1361. return err;
  1362. if(!postdata) {
  1363. /* no data from the file, point to a zero byte string to make this
  1364. get sent as a POST anyway */
  1365. postdata = strdup("");
  1366. if(!postdata)
  1367. return PARAM_NO_MEM;
  1368. }
  1369. }
  1370. else {
  1371. GetStr(&postdata, nextarg);
  1372. if(postdata)
  1373. size = strlen(postdata);
  1374. }
  1375. #ifdef CURL_DOES_CONVERSIONS
  1376. if(subletter != 'b') {
  1377. /* NOT forced binary, convert to ASCII */
  1378. if(convert_to_network(postdata, strlen(postdata))) {
  1379. Curl_safefree(postdata);
  1380. return PARAM_NO_MEM;
  1381. }
  1382. }
  1383. #endif
  1384. if(config->postfields) {
  1385. /* we already have a string, we append this one with a separating
  1386. &-letter */
  1387. char *oldpost = config->postfields;
  1388. curl_off_t oldlen = config->postfieldsize;
  1389. curl_off_t newlen = oldlen + curlx_uztoso(size) + 2;
  1390. config->postfields = malloc((size_t)newlen);
  1391. if(!config->postfields) {
  1392. Curl_safefree(oldpost);
  1393. Curl_safefree(postdata);
  1394. return PARAM_NO_MEM;
  1395. }
  1396. memcpy(config->postfields, oldpost, (size_t)oldlen);
  1397. /* use byte value 0x26 for '&' to accommodate non-ASCII platforms */
  1398. config->postfields[oldlen] = '\x26';
  1399. memcpy(&config->postfields[oldlen + 1], postdata, size);
  1400. config->postfields[oldlen + 1 + size] = '\0';
  1401. Curl_safefree(oldpost);
  1402. Curl_safefree(postdata);
  1403. config->postfieldsize += size + 1;
  1404. }
  1405. else {
  1406. config->postfields = postdata;
  1407. config->postfieldsize = curlx_uztoso(size);
  1408. }
  1409. }
  1410. /*
  1411. We can't set the request type here, as this data might be used in
  1412. a simple GET if -G is used. Already or soon.
  1413. if(SetHTTPrequest(HTTPREQ_SIMPLEPOST, &config->httpreq)) {
  1414. Curl_safefree(postdata);
  1415. return PARAM_BAD_USE;
  1416. }
  1417. */
  1418. break;
  1419. case 'D':
  1420. /* dump-header to given file name */
  1421. GetStr(&config->headerfile, nextarg);
  1422. break;
  1423. case 'e':
  1424. {
  1425. char *ptr = strstr(nextarg, ";auto");
  1426. if(ptr) {
  1427. /* Automatic referer requested, this may be combined with a
  1428. set initial one */
  1429. config->autoreferer = TRUE;
  1430. *ptr = 0; /* zero terminate here */
  1431. }
  1432. else
  1433. config->autoreferer = FALSE;
  1434. GetStr(&config->referer, nextarg);
  1435. }
  1436. break;
  1437. case 'E':
  1438. switch(subletter) {
  1439. case '\0': /* certificate file */
  1440. GetFileAndPassword(nextarg, &config->cert, &config->key_passwd);
  1441. break;
  1442. case 'a': /* CA info PEM file */
  1443. GetStr(&config->cacert, nextarg);
  1444. break;
  1445. case 'b': /* cert file type */
  1446. GetStr(&config->cert_type, nextarg);
  1447. break;
  1448. case 'c': /* private key file */
  1449. GetStr(&config->key, nextarg);
  1450. break;
  1451. case 'd': /* private key file type */
  1452. GetStr(&config->key_type, nextarg);
  1453. break;
  1454. case 'e': /* private key passphrase */
  1455. GetStr(&config->key_passwd, nextarg);
  1456. cleanarg(nextarg);
  1457. break;
  1458. case 'f': /* crypto engine */
  1459. GetStr(&config->engine, nextarg);
  1460. if(config->engine && curl_strequal(config->engine, "list"))
  1461. return PARAM_ENGINES_REQUESTED;
  1462. break;
  1463. case 'g': /* CA cert directory */
  1464. GetStr(&config->capath, nextarg);
  1465. break;
  1466. case 'h': /* --pubkey public key file */
  1467. GetStr(&config->pubkey, nextarg);
  1468. break;
  1469. case 'i': /* --hostpubmd5 md5 of the host public key */
  1470. GetStr(&config->hostpubmd5, nextarg);
  1471. if(!config->hostpubmd5 || strlen(config->hostpubmd5) != 32)
  1472. return PARAM_BAD_USE;
  1473. break;
  1474. case 'j': /* CRL file */
  1475. GetStr(&config->crlfile, nextarg);
  1476. break;
  1477. case 'k': /* TLS username */
  1478. if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
  1479. GetStr(&config->tls_username, nextarg);
  1480. else
  1481. return PARAM_LIBCURL_DOESNT_SUPPORT;
  1482. break;
  1483. case 'l': /* TLS password */
  1484. if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
  1485. GetStr(&config->tls_password, nextarg);
  1486. else
  1487. return PARAM_LIBCURL_DOESNT_SUPPORT;
  1488. break;
  1489. case 'm': /* TLS authentication type */
  1490. if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
  1491. GetStr(&config->tls_authtype, nextarg);
  1492. if(!curl_strequal(config->tls_authtype, "SRP"))
  1493. return PARAM_LIBCURL_DOESNT_SUPPORT; /* only support TLS-SRP */
  1494. }
  1495. else
  1496. return PARAM_LIBCURL_DOESNT_SUPPORT;
  1497. break;
  1498. case 'n': /* no empty SSL fragments, --ssl-allow-beast */
  1499. if(curlinfo->features & CURL_VERSION_SSL)
  1500. config->ssl_allow_beast = toggle;
  1501. break;
  1502. case 'o': /* --login-options */
  1503. GetStr(&config->login_options, nextarg);
  1504. break;
  1505. case 'p': /* Pinned public key DER file */
  1506. GetStr(&config->pinnedpubkey, nextarg);
  1507. break;
  1508. case 'P': /* proxy pinned public key */
  1509. GetStr(&config->proxy_pinnedpubkey, nextarg);
  1510. break;
  1511. case 'q': /* --cert-status */
  1512. config->verifystatus = TRUE;
  1513. break;
  1514. case 'r': /* --false-start */
  1515. config->falsestart = TRUE;
  1516. break;
  1517. case 's': /* --ssl-no-revoke */
  1518. if(curlinfo->features & CURL_VERSION_SSL)
  1519. config->ssl_no_revoke = TRUE;
  1520. break;
  1521. case 't': /* --tcp-fastopen */
  1522. config->tcp_fastopen = TRUE;
  1523. break;
  1524. case 'u': /* TLS username for proxy */
  1525. if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
  1526. GetStr(&config->proxy_tls_username, nextarg);
  1527. else
  1528. return PARAM_LIBCURL_DOESNT_SUPPORT;
  1529. break;
  1530. case 'v': /* TLS password for proxy */
  1531. if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP)
  1532. GetStr(&config->proxy_tls_password, nextarg);
  1533. else
  1534. return PARAM_LIBCURL_DOESNT_SUPPORT;
  1535. break;
  1536. case 'w': /* TLS authentication type for proxy */
  1537. if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
  1538. GetStr(&config->proxy_tls_authtype, nextarg);
  1539. if(!curl_strequal(config->proxy_tls_authtype, "SRP"))
  1540. return PARAM_LIBCURL_DOESNT_SUPPORT; /* only support TLS-SRP */
  1541. }
  1542. else
  1543. return PARAM_LIBCURL_DOESNT_SUPPORT;
  1544. break;
  1545. case 'x': /* certificate file for proxy */
  1546. GetFileAndPassword(nextarg, &config->proxy_cert,
  1547. &config->proxy_key_passwd);
  1548. break;
  1549. case 'y': /* cert file type for proxy */
  1550. GetStr(&config->proxy_cert_type, nextarg);
  1551. break;
  1552. case 'z': /* private key file for proxy */
  1553. GetStr(&config->proxy_key, nextarg);
  1554. break;
  1555. case '0': /* private key file type for proxy */
  1556. GetStr(&config->proxy_key_type, nextarg);
  1557. break;
  1558. case '1': /* private key passphrase for proxy */
  1559. GetStr(&config->proxy_key_passwd, nextarg);
  1560. cleanarg(nextarg);
  1561. break;
  1562. case '2': /* ciphers for proxy */
  1563. GetStr(&config->proxy_cipher_list, nextarg);
  1564. break;
  1565. case '3': /* CRL file for proxy */
  1566. GetStr(&config->proxy_crlfile, nextarg);
  1567. break;
  1568. case '4': /* no empty SSL fragments for proxy */
  1569. if(curlinfo->features & CURL_VERSION_SSL)
  1570. config->proxy_ssl_allow_beast = toggle;
  1571. break;
  1572. case '5': /* --login-options */
  1573. GetStr(&config->login_options, nextarg);
  1574. break;
  1575. case '6': /* CA info PEM file for proxy */
  1576. GetStr(&config->proxy_cacert, nextarg);
  1577. break;
  1578. case '7': /* CA cert directory for proxy */
  1579. GetStr(&config->proxy_capath, nextarg);
  1580. break;
  1581. case '8': /* allow insecure SSL connects for proxy */
  1582. config->proxy_insecure_ok = toggle;
  1583. break;
  1584. case '9': /* --proxy-tlsv1 */
  1585. /* TLS version 1 for proxy */
  1586. config->proxy_ssl_version = CURL_SSLVERSION_TLSv1;
  1587. break;
  1588. case 'A':
  1589. /* --socks5-basic */
  1590. if(toggle)
  1591. config->socks5_auth |= CURLAUTH_BASIC;
  1592. else
  1593. config->socks5_auth &= ~CURLAUTH_BASIC;
  1594. break;
  1595. case 'B':
  1596. /* --socks5-gssapi */
  1597. if(toggle)
  1598. config->socks5_auth |= CURLAUTH_GSSAPI;
  1599. else
  1600. config->socks5_auth &= ~CURLAUTH_GSSAPI;
  1601. break;
  1602. default: /* unknown flag */
  1603. return PARAM_OPTION_UNKNOWN;
  1604. }
  1605. break;
  1606. case 'f':
  1607. switch(subletter) {
  1608. case 'a': /* --fail-early */
  1609. global->fail_early = toggle;
  1610. break;
  1611. case 'b': /* --styled-output */
  1612. global->styled_output = toggle;
  1613. break;
  1614. default: /* --fail (hard on errors) */
  1615. config->failonerror = toggle;
  1616. }
  1617. break;
  1618. case 'F':
  1619. /* "form data" simulation, this is a little advanced so lets do our best
  1620. to sort this out slowly and carefully */
  1621. if(formparse(config,
  1622. nextarg,
  1623. &config->mimeroot,
  1624. &config->mimecurrent,
  1625. (subletter == 's')?TRUE:FALSE)) /* 's' is literal string */
  1626. return PARAM_BAD_USE;
  1627. if(SetHTTPrequest(config, HTTPREQ_MIMEPOST, &config->httpreq))
  1628. return PARAM_BAD_USE;
  1629. break;
  1630. case 'g': /* g disables URLglobbing */
  1631. config->globoff = toggle;
  1632. break;
  1633. case 'G': /* HTTP GET */
  1634. if(subletter == 'a') { /* --request-target */
  1635. GetStr(&config->request_target, nextarg);
  1636. }
  1637. else
  1638. config->use_httpget = TRUE;
  1639. break;
  1640. case 'h': /* h for help */
  1641. if(toggle) {
  1642. return PARAM_HELP_REQUESTED;
  1643. }
  1644. /* we now actually support --no-help too! */
  1645. break;
  1646. case 'H':
  1647. /* A custom header to append to a list */
  1648. if(nextarg[0] == '@') {
  1649. /* read many headers from a file or stdin */
  1650. char *string;
  1651. size_t len;
  1652. bool use_stdin = !strcmp(&nextarg[1], "-");
  1653. FILE *file = use_stdin?stdin:fopen(&nextarg[1], FOPEN_READTEXT);
  1654. if(!file)
  1655. warnf(global, "Failed to open %s!\n", &nextarg[1]);
  1656. else {
  1657. err = file2memory(&string, &len, file);
  1658. if(!err && string) {
  1659. /* Allow strtok() here since this isn't used threaded */
  1660. /* !checksrc! disable BANNEDFUNC 2 */
  1661. char *h = strtok(string, "\r\n");
  1662. while(h) {
  1663. if(subletter == 'p') /* --proxy-header */
  1664. err = add2list(&config->proxyheaders, h);
  1665. else
  1666. err = add2list(&config->headers, h);
  1667. if(err)
  1668. break;
  1669. h = strtok(NULL, "\r\n");
  1670. }
  1671. free(string);
  1672. }
  1673. if(!use_stdin)
  1674. fclose(file);
  1675. if(err)
  1676. return err;
  1677. }
  1678. }
  1679. else {
  1680. if(subletter == 'p') /* --proxy-header */
  1681. err = add2list(&config->proxyheaders, nextarg);
  1682. else
  1683. err = add2list(&config->headers, nextarg);
  1684. if(err)
  1685. return err;
  1686. }
  1687. break;
  1688. case 'i':
  1689. config->show_headers = toggle; /* show the headers as well in the
  1690. general output stream */
  1691. break;
  1692. case 'j':
  1693. config->cookiesession = toggle;
  1694. break;
  1695. case 'I': /* --head */
  1696. config->no_body = toggle;
  1697. config->show_headers = toggle;
  1698. if(SetHTTPrequest(config,
  1699. (config->no_body)?HTTPREQ_HEAD:HTTPREQ_GET,
  1700. &config->httpreq))
  1701. return PARAM_BAD_USE;
  1702. break;
  1703. case 'J': /* --remote-header-name */
  1704. if(config->show_headers) {
  1705. warnf(global,
  1706. "--include and --remote-header-name cannot be combined.\n");
  1707. return PARAM_BAD_USE;
  1708. }
  1709. config->content_disposition = toggle;
  1710. break;
  1711. case 'k': /* allow insecure SSL connects */
  1712. config->insecure_ok = toggle;
  1713. break;
  1714. case 'K': /* parse config file */
  1715. if(parseconfig(nextarg, global))
  1716. warnf(global, "error trying read config from the '%s' file\n",
  1717. nextarg);
  1718. break;
  1719. case 'l':
  1720. config->dirlistonly = toggle; /* only list the names of the FTP dir */
  1721. break;
  1722. case 'L':
  1723. config->followlocation = toggle; /* Follow Location: HTTP headers */
  1724. switch(subletter) {
  1725. case 't':
  1726. /* Continue to send authentication (user+password) when following
  1727. * locations, even when hostname changed */
  1728. config->unrestricted_auth = toggle;
  1729. break;
  1730. }
  1731. break;
  1732. case 'm':
  1733. /* specified max time */
  1734. err = str2udouble(&config->timeout, nextarg, LONG_MAX/1000);
  1735. if(err)
  1736. return err;
  1737. break;
  1738. case 'M': /* M for manual, huge help */
  1739. if(toggle) { /* --no-manual shows no manual... */
  1740. #ifdef USE_MANUAL
  1741. return PARAM_MANUAL_REQUESTED;
  1742. #else
  1743. warnf(global,
  1744. "built-in manual was disabled at build-time!\n");
  1745. return PARAM_OPTION_UNKNOWN;
  1746. #endif
  1747. }
  1748. break;
  1749. case 'n':
  1750. switch(subletter) {
  1751. case 'o': /* use .netrc or URL */
  1752. config->netrc_opt = toggle;
  1753. break;
  1754. case 'e': /* netrc-file */
  1755. GetStr(&config->netrc_file, nextarg);
  1756. break;
  1757. default:
  1758. /* pick info from .netrc, if this is used for http, curl will
  1759. automatically enfore user+password with the request */
  1760. config->netrc = toggle;
  1761. break;
  1762. }
  1763. break;
  1764. case 'N':
  1765. /* disable the output I/O buffering. note that the option is called
  1766. --buffer but is mostly used in the negative form: --no-buffer */
  1767. if(longopt)
  1768. config->nobuffer = (!toggle)?TRUE:FALSE;
  1769. else
  1770. config->nobuffer = toggle;
  1771. break;
  1772. case 'O': /* --remote-name */
  1773. if(subletter == 'a') { /* --remote-name-all */
  1774. config->default_node_flags = toggle?GETOUT_USEREMOTE:0;
  1775. break;
  1776. }
  1777. /* FALLTHROUGH */
  1778. case 'o': /* --output */
  1779. /* output file */
  1780. {
  1781. struct getout *url;
  1782. if(!config->url_out)
  1783. config->url_out = config->url_list;
  1784. if(config->url_out) {
  1785. /* there's a node here, if it already is filled-in continue to find
  1786. an "empty" node */
  1787. while(config->url_out && (config->url_out->flags & GETOUT_OUTFILE))
  1788. config->url_out = config->url_out->next;
  1789. }
  1790. /* now there might or might not be an available node to fill in! */
  1791. if(config->url_out)
  1792. /* existing node */
  1793. url = config->url_out;
  1794. else
  1795. /* there was no free node, create one! */
  1796. config->url_out = url = new_getout(config);
  1797. if(!url)
  1798. return PARAM_NO_MEM;
  1799. /* fill in the outfile */
  1800. if('o' == letter) {
  1801. GetStr(&url->outfile, nextarg);
  1802. url->flags &= ~GETOUT_USEREMOTE; /* switch off */
  1803. }
  1804. else {
  1805. url->outfile = NULL; /* leave it */
  1806. if(toggle)
  1807. url->flags |= GETOUT_USEREMOTE; /* switch on */
  1808. else
  1809. url->flags &= ~GETOUT_USEREMOTE; /* switch off */
  1810. }
  1811. url->flags |= GETOUT_OUTFILE;
  1812. }
  1813. break;
  1814. case 'P':
  1815. /* This makes the FTP sessions use PORT instead of PASV */
  1816. /* use <eth0> or <192.168.10.10> style addresses. Anything except
  1817. this will make us try to get the "default" address.
  1818. NOTE: this is a changed behaviour since the released 4.1!
  1819. */
  1820. GetStr(&config->ftpport, nextarg);
  1821. break;
  1822. case 'p':
  1823. /* proxy tunnel for non-http protocols */
  1824. config->proxytunnel = toggle;
  1825. break;
  1826. case 'q': /* if used first, already taken care of, we do it like
  1827. this so we don't cause an error! */
  1828. break;
  1829. case 'Q':
  1830. /* QUOTE command to send to FTP server */
  1831. switch(nextarg[0]) {
  1832. case '-':
  1833. /* prefixed with a dash makes it a POST TRANSFER one */
  1834. nextarg++;
  1835. err = add2list(&config->postquote, nextarg);
  1836. break;
  1837. case '+':
  1838. /* prefixed with a plus makes it a just-before-transfer one */
  1839. nextarg++;
  1840. err = add2list(&config->prequote, nextarg);
  1841. break;
  1842. default:
  1843. err = add2list(&config->quote, nextarg);
  1844. break;
  1845. }
  1846. if(err)
  1847. return err;
  1848. break;
  1849. case 'r':
  1850. /* Specifying a range WITHOUT A DASH will create an illegal HTTP range
  1851. (and won't actually be range by definition). The man page previously
  1852. claimed that to be a good way, why this code is added to work-around
  1853. it. */
  1854. if(ISDIGIT(*nextarg) && !strchr(nextarg, '-')) {
  1855. char buffer[32];
  1856. curl_off_t off;
  1857. if(curlx_strtoofft(nextarg, NULL, 10, &off)) {
  1858. warnf(global, "unsupported range point\n");
  1859. return PARAM_BAD_USE;
  1860. }
  1861. warnf(global,
  1862. "A specified range MUST include at least one dash (-). "
  1863. "Appending one for you!\n");
  1864. msnprintf(buffer, sizeof(buffer), "%" CURL_FORMAT_CURL_OFF_T "-", off);
  1865. Curl_safefree(config->range);
  1866. config->range = strdup(buffer);
  1867. if(!config->range)
  1868. return PARAM_NO_MEM;
  1869. }
  1870. {
  1871. /* byte range requested */
  1872. char *tmp_range;
  1873. tmp_range = nextarg;
  1874. while(*tmp_range != '\0') {
  1875. if(!ISDIGIT(*tmp_range) && *tmp_range != '-' && *tmp_range != ',') {
  1876. warnf(global, "Invalid character is found in given range. "
  1877. "A specified range MUST have only digits in "
  1878. "\'start\'-\'stop\'. The server's response to this "
  1879. "request is uncertain.\n");
  1880. break;
  1881. }
  1882. tmp_range++;
  1883. }
  1884. /* byte range requested */
  1885. GetStr(&config->range, nextarg);
  1886. }
  1887. break;
  1888. case 'R':
  1889. /* use remote file's time */
  1890. config->remote_time = toggle;
  1891. break;
  1892. case 's':
  1893. /* don't show progress meter, don't show errors : */
  1894. if(toggle)
  1895. global->mute = global->noprogress = TRUE;
  1896. else
  1897. global->mute = global->noprogress = FALSE;
  1898. if(global->showerror < 0)
  1899. /* if still on the default value, set showerror to the reverse of
  1900. toggle. This is to allow -S and -s to be used in an independent
  1901. order but still have the same effect. */
  1902. global->showerror = (!toggle)?TRUE:FALSE; /* toggle off */
  1903. break;
  1904. case 'S':
  1905. /* show errors */
  1906. global->showerror = toggle?1:0; /* toggle on if used with -s */
  1907. break;
  1908. case 't':
  1909. /* Telnet options */
  1910. err = add2list(&config->telnet_options, nextarg);
  1911. if(err)
  1912. return err;
  1913. break;
  1914. case 'T':
  1915. /* we are uploading */
  1916. {
  1917. struct getout *url;
  1918. if(!config->url_ul)
  1919. config->url_ul = config->url_list;
  1920. if(config->url_ul) {
  1921. /* there's a node here, if it already is filled-in continue to find
  1922. an "empty" node */
  1923. while(config->url_ul && (config->url_ul->flags & GETOUT_UPLOAD))
  1924. config->url_ul = config->url_ul->next;
  1925. }
  1926. /* now there might or might not be an available node to fill in! */
  1927. if(config->url_ul)
  1928. /* existing node */
  1929. url = config->url_ul;
  1930. else
  1931. /* there was no free node, create one! */
  1932. config->url_ul = url = new_getout(config);
  1933. if(!url)
  1934. return PARAM_NO_MEM;
  1935. url->flags |= GETOUT_UPLOAD; /* mark -T used */
  1936. if(!*nextarg)
  1937. url->flags |= GETOUT_NOUPLOAD;
  1938. else {
  1939. /* "-" equals stdin, but keep the string around for now */
  1940. GetStr(&url->infile, nextarg);
  1941. }
  1942. }
  1943. break;
  1944. case 'u':
  1945. /* user:password */
  1946. GetStr(&config->userpwd, nextarg);
  1947. cleanarg(nextarg);
  1948. break;
  1949. case 'U':
  1950. /* Proxy user:password */
  1951. GetStr(&config->proxyuserpwd, nextarg);
  1952. cleanarg(nextarg);
  1953. break;
  1954. case 'v':
  1955. if(toggle) {
  1956. /* the '%' thing here will cause the trace get sent to stderr */
  1957. Curl_safefree(global->trace_dump);
  1958. global->trace_dump = strdup("%");
  1959. if(!global->trace_dump)
  1960. return PARAM_NO_MEM;
  1961. if(global->tracetype && (global->tracetype != TRACE_PLAIN))
  1962. warnf(global,
  1963. "-v, --verbose overrides an earlier trace/verbose option\n");
  1964. global->tracetype = TRACE_PLAIN;
  1965. }
  1966. else
  1967. /* verbose is disabled here */
  1968. global->tracetype = TRACE_NONE;
  1969. break;
  1970. case 'V':
  1971. if(toggle) /* --no-version yields no output! */
  1972. return PARAM_VERSION_INFO_REQUESTED;
  1973. break;
  1974. case 'w':
  1975. /* get the output string */
  1976. if('@' == *nextarg) {
  1977. /* the data begins with a '@' letter, it means that a file name
  1978. or - (stdin) follows */
  1979. FILE *file;
  1980. const char *fname;
  1981. nextarg++; /* pass the @ */
  1982. if(!strcmp("-", nextarg)) {
  1983. fname = "<stdin>";
  1984. file = stdin;
  1985. }
  1986. else {
  1987. fname = nextarg;
  1988. file = fopen(nextarg, FOPEN_READTEXT);
  1989. }
  1990. Curl_safefree(config->writeout);
  1991. err = file2string(&config->writeout, file);
  1992. if(file && (file != stdin))
  1993. fclose(file);
  1994. if(err)
  1995. return err;
  1996. if(!config->writeout)
  1997. warnf(global, "Failed to read %s", fname);
  1998. }
  1999. else
  2000. GetStr(&config->writeout, nextarg);
  2001. break;
  2002. case 'x':
  2003. switch(subletter) {
  2004. case 'a': /* --preproxy */
  2005. GetStr(&config->preproxy, nextarg);
  2006. break;
  2007. default:
  2008. /* --proxy */
  2009. GetStr(&config->proxy, nextarg);
  2010. config->proxyver = CURLPROXY_HTTP;
  2011. break;
  2012. }
  2013. break;
  2014. case 'X':
  2015. /* set custom request */
  2016. GetStr(&config->customrequest, nextarg);
  2017. break;
  2018. case 'y':
  2019. /* low speed time */
  2020. err = str2unum(&config->low_speed_time, nextarg);
  2021. if(err)
  2022. return err;
  2023. if(!config->low_speed_limit)
  2024. config->low_speed_limit = 1;
  2025. break;
  2026. case 'Y':
  2027. /* low speed limit */
  2028. err = str2unum(&config->low_speed_limit, nextarg);
  2029. if(err)
  2030. return err;
  2031. if(!config->low_speed_time)
  2032. config->low_speed_time = 30;
  2033. break;
  2034. case 'Z':
  2035. switch(subletter) {
  2036. case '\0': /* --parallel */
  2037. global->parallel = toggle;
  2038. break;
  2039. case 'b': /* --parallel-max */
  2040. err = str2unum(&global->parallel_max, nextarg);
  2041. if(err)
  2042. return err;
  2043. if((global->parallel_max > MAX_PARALLEL) ||
  2044. (global->parallel_max < 1))
  2045. global->parallel_max = PARALLEL_DEFAULT;
  2046. break;
  2047. }
  2048. break;
  2049. case 'z': /* time condition coming up */
  2050. switch(*nextarg) {
  2051. case '+':
  2052. nextarg++;
  2053. /* FALLTHROUGH */
  2054. default:
  2055. /* If-Modified-Since: (section 14.28 in RFC2068) */
  2056. config->timecond = CURL_TIMECOND_IFMODSINCE;
  2057. break;
  2058. case '-':
  2059. /* If-Unmodified-Since: (section 14.24 in RFC2068) */
  2060. config->timecond = CURL_TIMECOND_IFUNMODSINCE;
  2061. nextarg++;
  2062. break;
  2063. case '=':
  2064. /* Last-Modified: (section 14.29 in RFC2068) */
  2065. config->timecond = CURL_TIMECOND_LASTMOD;
  2066. nextarg++;
  2067. break;
  2068. }
  2069. now = time(NULL);
  2070. config->condtime = (curl_off_t)curl_getdate(nextarg, &now);
  2071. if(-1 == config->condtime) {
  2072. /* now let's see if it is a file name to get the time from instead! */
  2073. curl_off_t filetime = getfiletime(nextarg, config->global->errors);
  2074. if(filetime >= 0) {
  2075. /* pull the time out from the file */
  2076. config->condtime = filetime;
  2077. }
  2078. else {
  2079. /* failed, remove time condition */
  2080. config->timecond = CURL_TIMECOND_NONE;
  2081. warnf(global,
  2082. "Illegal date format for -z, --time-cond (and not "
  2083. "a file name). Disabling time condition. "
  2084. "See curl_getdate(3) for valid date syntax.\n");
  2085. }
  2086. }
  2087. break;
  2088. default: /* unknown flag */
  2089. return PARAM_OPTION_UNKNOWN;
  2090. }
  2091. hit = -1;
  2092. } while(!longopt && !singleopt && *++parse && !*usedarg);
  2093. return PARAM_OK;
  2094. }
  2095. ParameterError parse_args(struct GlobalConfig *global, int argc,
  2096. argv_item_t argv[])
  2097. {
  2098. int i;
  2099. bool stillflags;
  2100. char *orig_opt = NULL;
  2101. ParameterError result = PARAM_OK;
  2102. struct OperationConfig *config = global->first;
  2103. for(i = 1, stillflags = TRUE; i < argc && !result; i++) {
  2104. orig_opt = argv[i];
  2105. if(stillflags && ('-' == argv[i][0])) {
  2106. bool passarg;
  2107. char *flag = argv[i];
  2108. if(!strcmp("--", argv[i]))
  2109. /* This indicates the end of the flags and thus enables the
  2110. following (URL) argument to start with -. */
  2111. stillflags = FALSE;
  2112. else {
  2113. char *nextarg = (i < (argc - 1)) ? argv[i + 1] : NULL;
  2114. result = getparameter(flag, nextarg, &passarg, global, config);
  2115. if(result == PARAM_NEXT_OPERATION) {
  2116. /* Reset result as PARAM_NEXT_OPERATION is only used here and not
  2117. returned from this function */
  2118. result = PARAM_OK;
  2119. if(config->url_list && config->url_list->url) {
  2120. /* Allocate the next config */
  2121. config->next = malloc(sizeof(struct OperationConfig));
  2122. if(config->next) {
  2123. /* Initialise the newly created config */
  2124. config_init(config->next);
  2125. /* Set the global config pointer */
  2126. config->next->global = global;
  2127. /* Update the last config pointer */
  2128. global->last = config->next;
  2129. /* Move onto the new config */
  2130. config->next->prev = config;
  2131. config = config->next;
  2132. }
  2133. else
  2134. result = PARAM_NO_MEM;
  2135. }
  2136. }
  2137. else if(!result && passarg)
  2138. i++; /* we're supposed to skip this */
  2139. }
  2140. }
  2141. else {
  2142. bool used;
  2143. /* Just add the URL please */
  2144. result = getparameter((char *)"--url", argv[i], &used, global,
  2145. config);
  2146. }
  2147. }
  2148. if(result && result != PARAM_HELP_REQUESTED &&
  2149. result != PARAM_MANUAL_REQUESTED &&
  2150. result != PARAM_VERSION_INFO_REQUESTED &&
  2151. result != PARAM_ENGINES_REQUESTED) {
  2152. const char *reason = param2text(result);
  2153. if(orig_opt && strcmp(":", orig_opt))
  2154. helpf(global->errors, "option %s: %s\n", orig_opt, reason);
  2155. else
  2156. helpf(global->errors, "%s\n", reason);
  2157. }
  2158. return result;
  2159. }