1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22 #include "tool_setup.h"
23
24 #ifndef CURL_DISABLE_LIBCURL_OPTION
25
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
28 #include "curlx.h"
29
30 #include "tool_cfgable.h"
31 #include "tool_easysrc.h"
32 #include "tool_setopt.h"
33 #include "tool_convert.h"
34
35 #include "memdebug.h" /* keep this as LAST include */
36
37 /* Lookup tables for converting setopt values back to symbols */
38 /* For enums, values may be in any order. */
39 /* For bit masks, put combinations first, then single bits, */
40 /* and finally any "NONE" value. */
41
42 #define NV(e) {#e, e}
43 #define NV1(e, v) {#e, (v)}
44 #define NVEND {NULL, 0} /* sentinel to mark end of list */
45
46 const struct NameValue setopt_nv_CURLPROXY[] = {
47 NV(CURLPROXY_HTTP),
48 NV(CURLPROXY_HTTP_1_0),
49 NV(CURLPROXY_HTTPS),
50 NV(CURLPROXY_SOCKS4),
51 NV(CURLPROXY_SOCKS5),
52 NV(CURLPROXY_SOCKS4A),
53 NV(CURLPROXY_SOCKS5_HOSTNAME),
54 NVEND,
55 };
56
57 const struct NameValue setopt_nv_CURL_SOCKS_PROXY[] = {
58 NV(CURLPROXY_SOCKS4),
59 NV(CURLPROXY_SOCKS5),
60 NV(CURLPROXY_SOCKS4A),
61 NV(CURLPROXY_SOCKS5_HOSTNAME),
62 NVEND,
63 };
64
65 const struct NameValueUnsigned setopt_nv_CURLAUTH[] = {
66 NV(CURLAUTH_ANY), /* combination */
67 NV(CURLAUTH_ANYSAFE), /* combination */
68 NV(CURLAUTH_BASIC),
69 NV(CURLAUTH_DIGEST),
70 NV(CURLAUTH_GSSNEGOTIATE),
71 NV(CURLAUTH_NTLM),
72 NV(CURLAUTH_DIGEST_IE),
73 NV(CURLAUTH_NTLM_WB),
74 NV(CURLAUTH_ONLY),
75 NV(CURLAUTH_NONE),
76 NVEND,
77 };
78
79 const struct NameValue setopt_nv_CURL_HTTP_VERSION[] = {
80 NV(CURL_HTTP_VERSION_NONE),
81 NV(CURL_HTTP_VERSION_1_0),
82 NV(CURL_HTTP_VERSION_1_1),
83 NV(CURL_HTTP_VERSION_2_0),
84 NV(CURL_HTTP_VERSION_2TLS),
85 NV(CURL_HTTP_VERSION_3),
86 NVEND,
87 };
88
89 const struct NameValue setopt_nv_CURL_SSLVERSION[] = {
90 NV(CURL_SSLVERSION_DEFAULT),
91 NV(CURL_SSLVERSION_TLSv1),
92 NV(CURL_SSLVERSION_SSLv2),
93 NV(CURL_SSLVERSION_SSLv3),
94 NV(CURL_SSLVERSION_TLSv1_0),
95 NV(CURL_SSLVERSION_TLSv1_1),
96 NV(CURL_SSLVERSION_TLSv1_2),
97 NV(CURL_SSLVERSION_TLSv1_3),
98 NVEND,
99 };
100
101 const struct NameValue setopt_nv_CURL_TIMECOND[] = {
102 NV(CURL_TIMECOND_IFMODSINCE),
103 NV(CURL_TIMECOND_IFUNMODSINCE),
104 NV(CURL_TIMECOND_LASTMOD),
105 NV(CURL_TIMECOND_NONE),
106 NVEND,
107 };
108
109 const struct NameValue setopt_nv_CURLFTPSSL_CCC[] = {
110 NV(CURLFTPSSL_CCC_NONE),
111 NV(CURLFTPSSL_CCC_PASSIVE),
112 NV(CURLFTPSSL_CCC_ACTIVE),
113 NVEND,
114 };
115
116 const struct NameValue setopt_nv_CURLUSESSL[] = {
117 NV(CURLUSESSL_NONE),
118 NV(CURLUSESSL_TRY),
119 NV(CURLUSESSL_CONTROL),
120 NV(CURLUSESSL_ALL),
121 NVEND,
122 };
123
124 const struct NameValueUnsigned setopt_nv_CURLSSLOPT[] = {
125 NV(CURLSSLOPT_ALLOW_BEAST),
126 NV(CURLSSLOPT_NO_REVOKE),
127 NV(CURLSSLOPT_NO_PARTIALCHAIN),
128 NV(CURLSSLOPT_REVOKE_BEST_EFFORT),
129 NV(CURLSSLOPT_NATIVE_CA),
130 NVEND,
131 };
132
133 const struct NameValue setopt_nv_CURL_NETRC[] = {
134 NV(CURL_NETRC_IGNORED),
135 NV(CURL_NETRC_OPTIONAL),
136 NV(CURL_NETRC_REQUIRED),
137 NVEND,
138 };
139
140 /* These mappings essentially triplicated - see
141 * tool_libinfo.c and tool_paramhlp.c */
142 const struct NameValue setopt_nv_CURLPROTO[] = {
143 NV(CURLPROTO_ALL), /* combination */
144 NV(CURLPROTO_DICT),
145 NV(CURLPROTO_FILE),
146 NV(CURLPROTO_FTP),
147 NV(CURLPROTO_FTPS),
148 NV(CURLPROTO_GOPHER),
149 NV(CURLPROTO_HTTP),
150 NV(CURLPROTO_HTTPS),
151 NV(CURLPROTO_IMAP),
152 NV(CURLPROTO_IMAPS),
153 NV(CURLPROTO_LDAP),
154 NV(CURLPROTO_LDAPS),
155 NV(CURLPROTO_POP3),
156 NV(CURLPROTO_POP3S),
157 NV(CURLPROTO_RTSP),
158 NV(CURLPROTO_SCP),
159 NV(CURLPROTO_SFTP),
160 NV(CURLPROTO_SMB),
161 NV(CURLPROTO_SMBS),
162 NV(CURLPROTO_SMTP),
163 NV(CURLPROTO_SMTPS),
164 NV(CURLPROTO_TELNET),
165 NV(CURLPROTO_TFTP),
166 NVEND,
167 };
168
169 /* These options have non-zero default values. */
170 static const struct NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
171 NV1(CURLOPT_SSL_VERIFYPEER, 1),
172 NV1(CURLOPT_SSL_VERIFYHOST, 1),
173 NV1(CURLOPT_SSL_ENABLE_NPN, 1),
174 NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
175 NV1(CURLOPT_TCP_NODELAY, 1),
176 NV1(CURLOPT_PROXY_SSL_VERIFYPEER, 1),
177 NV1(CURLOPT_PROXY_SSL_VERIFYHOST, 1),
178 NV1(CURLOPT_SOCKS5_AUTH, 1),
179 NVEND
180 };
181
182 /* Format and add code; jump to nomem on malloc error */
183 #define ADD(args) do { \
184 ret = easysrc_add args; \
185 if(ret) \
186 goto nomem; \
187 } while(0)
188 #define ADDF(args) do { \
189 ret = easysrc_addf args; \
190 if(ret) \
191 goto nomem; \
192 } while(0)
193 #define NULL_CHECK(p) do { \
194 if(!p) { \
195 ret = CURLE_OUT_OF_MEMORY; \
196 goto nomem; \
197 } \
198 } while(0)
199
200 #define DECL0(s) ADD((&easysrc_decl, s))
201 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
202
203 #define DATA0(s) ADD((&easysrc_data, s))
204 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
205 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
206 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
207
208 #define CODE0(s) ADD((&easysrc_code, s))
209 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
210 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
211 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
212
213 #define CLEAN0(s) ADD((&easysrc_clean, s))
214 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
215
216 #define REM0(s) ADD((&easysrc_toohard, s))
217 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
218 #define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
219
220 /* Escape string to C string syntax. Return NULL if out of memory.
221 * Is this correct for those wacky EBCDIC guys? */
222
223 #define MAX_STRING_LENGTH_OUTPUT 2000
224 #define ZERO_TERMINATED -1
225
c_escape(const char * str,curl_off_t len)226 static char *c_escape(const char *str, curl_off_t len)
227 {
228 const char *s;
229 unsigned char c;
230 char *escaped, *e;
231 unsigned int cutoff = 0;
232
233 if(len == ZERO_TERMINATED)
234 len = strlen(str);
235
236 if(len > MAX_STRING_LENGTH_OUTPUT) {
237 /* cap ridiculously long strings */
238 len = MAX_STRING_LENGTH_OUTPUT;
239 cutoff = 3;
240 }
241
242 /* Allocate space based on worst-case */
243 escaped = malloc(4 * (size_t)len + 1 + cutoff);
244 if(!escaped)
245 return NULL;
246
247 e = escaped;
248 for(s = str; len; s++, len--) {
249 c = *s;
250 if(c == '\n') {
251 strcpy(e, "\\n");
252 e += 2;
253 }
254 else if(c == '\r') {
255 strcpy(e, "\\r");
256 e += 2;
257 }
258 else if(c == '\t') {
259 strcpy(e, "\\t");
260 e += 2;
261 }
262 else if(c == '\\') {
263 strcpy(e, "\\\\");
264 e += 2;
265 }
266 else if(c == '"') {
267 strcpy(e, "\\\"");
268 e += 2;
269 }
270 else if(!isprint(c)) {
271 msnprintf(e, 5, "\\x%02x", (unsigned)c);
272 e += 4;
273 }
274 else
275 *e++ = c;
276 }
277 while(cutoff--)
278 *e++ = '.';
279 *e = '\0';
280 return escaped;
281 }
282
283 /* setopt wrapper for enum types */
tool_setopt_enum(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,const struct NameValue * nvlist,long lval)284 CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
285 const char *name, CURLoption tag,
286 const struct NameValue *nvlist, long lval)
287 {
288 CURLcode ret = CURLE_OK;
289 bool skip = FALSE;
290
291 ret = curl_easy_setopt(curl, tag, lval);
292 if(!lval)
293 skip = TRUE;
294
295 if(config->libcurl && !skip && !ret) {
296 /* we only use this for real if --libcurl was used */
297 const struct NameValue *nv = NULL;
298 for(nv = nvlist; nv->name; nv++) {
299 if(nv->value == lval)
300 break; /* found it */
301 }
302 if(!nv->name) {
303 /* If no definition was found, output an explicit value.
304 * This could happen if new values are defined and used
305 * but the NameValue list is not updated. */
306 CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
307 }
308 else {
309 CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
310 }
311 }
312
313 nomem:
314 return ret;
315 }
316
317 /* setopt wrapper for flags */
tool_setopt_flags(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,const struct NameValue * nvlist,long lval)318 CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config,
319 const char *name, CURLoption tag,
320 const struct NameValue *nvlist, long lval)
321 {
322 CURLcode ret = CURLE_OK;
323 bool skip = FALSE;
324
325 ret = curl_easy_setopt(curl, tag, lval);
326 if(!lval)
327 skip = TRUE;
328
329 if(config->libcurl && !skip && !ret) {
330 /* we only use this for real if --libcurl was used */
331 char preamble[80]; /* should accommodate any symbol name */
332 long rest = lval; /* bits not handled yet */
333 const struct NameValue *nv = NULL;
334 msnprintf(preamble, sizeof(preamble),
335 "curl_easy_setopt(hnd, %s, ", name);
336 for(nv = nvlist; nv->name; nv++) {
337 if((nv->value & ~ rest) == 0) {
338 /* all value flags contained in rest */
339 rest &= ~ nv->value; /* remove bits handled here */
340 CODE3("%s(long)%s%s",
341 preamble, nv->name, rest ? " |" : ");");
342 if(!rest)
343 break; /* handled them all */
344 /* replace with all spaces for continuation line */
345 msnprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
346 }
347 }
348 /* If any bits have no definition, output an explicit value.
349 * This could happen if new bits are defined and used
350 * but the NameValue list is not updated. */
351 if(rest)
352 CODE2("%s%ldL);", preamble, rest);
353 }
354
355 nomem:
356 return ret;
357 }
358
359 /* setopt wrapper for bitmasks */
tool_setopt_bitmask(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,const struct NameValueUnsigned * nvlist,long lval)360 CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
361 const char *name, CURLoption tag,
362 const struct NameValueUnsigned *nvlist,
363 long lval)
364 {
365 CURLcode ret = CURLE_OK;
366 bool skip = FALSE;
367
368 ret = curl_easy_setopt(curl, tag, lval);
369 if(!lval)
370 skip = TRUE;
371
372 if(config->libcurl && !skip && !ret) {
373 /* we only use this for real if --libcurl was used */
374 char preamble[80];
375 unsigned long rest = (unsigned long)lval;
376 const struct NameValueUnsigned *nv = NULL;
377 msnprintf(preamble, sizeof(preamble),
378 "curl_easy_setopt(hnd, %s, ", name);
379 for(nv = nvlist; nv->name; nv++) {
380 if((nv->value & ~ rest) == 0) {
381 /* all value flags contained in rest */
382 rest &= ~ nv->value; /* remove bits handled here */
383 CODE3("%s(long)%s%s",
384 preamble, nv->name, rest ? " |" : ");");
385 if(!rest)
386 break; /* handled them all */
387 /* replace with all spaces for continuation line */
388 msnprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
389 }
390 }
391 /* If any bits have no definition, output an explicit value.
392 * This could happen if new bits are defined and used
393 * but the NameValue list is not updated. */
394 if(rest)
395 CODE2("%s%luUL);", preamble, rest);
396 }
397
398 nomem:
399 return ret;
400 }
401
402 /* Generate code for a struct curl_slist. */
libcurl_generate_slist(struct curl_slist * slist,int * slistno)403 static CURLcode libcurl_generate_slist(struct curl_slist *slist, int *slistno)
404 {
405 CURLcode ret = CURLE_OK;
406 char *escaped = NULL;
407
408 /* May need several slist variables, so invent name */
409 *slistno = ++easysrc_slist_count;
410
411 DECL1("struct curl_slist *slist%d;", *slistno);
412 DATA1("slist%d = NULL;", *slistno);
413 CLEAN1("curl_slist_free_all(slist%d);", *slistno);
414 CLEAN1("slist%d = NULL;", *slistno);
415 for(; slist; slist = slist->next) {
416 Curl_safefree(escaped);
417 escaped = c_escape(slist->data, ZERO_TERMINATED);
418 if(!escaped)
419 return CURLE_OUT_OF_MEMORY;
420 DATA3("slist%d = curl_slist_append(slist%d, \"%s\");",
421 *slistno, *slistno, escaped);
422 }
423
424 nomem:
425 Curl_safefree(escaped);
426 return ret;
427 }
428
429 static CURLcode libcurl_generate_mime(CURL *curl,
430 struct GlobalConfig *config,
431 struct tool_mime *toolmime,
432 int *mimeno); /* Forward. */
433
434 /* Wrapper to generate source code for a mime part. */
libcurl_generate_mime_part(CURL * curl,struct GlobalConfig * config,struct tool_mime * part,int mimeno)435 static CURLcode libcurl_generate_mime_part(CURL *curl,
436 struct GlobalConfig *config,
437 struct tool_mime *part,
438 int mimeno)
439 {
440 CURLcode ret = CURLE_OK;
441 int submimeno = 0;
442 char *escaped = NULL;
443 const char *data = NULL;
444 const char *filename = part->filename;
445
446 /* Parts are linked in reverse order. */
447 if(part->prev) {
448 ret = libcurl_generate_mime_part(curl, config, part->prev, mimeno);
449 if(ret)
450 return ret;
451 }
452
453 /* Create the part. */
454 CODE2("part%d = curl_mime_addpart(mime%d);", mimeno, mimeno);
455
456 switch(part->kind) {
457 case TOOLMIME_PARTS:
458 ret = libcurl_generate_mime(curl, config, part, &submimeno);
459 if(!ret) {
460 CODE2("curl_mime_subparts(part%d, mime%d);", mimeno, submimeno);
461 CODE1("mime%d = NULL;", submimeno); /* Avoid freeing in CLEAN. */
462 }
463 break;
464
465 case TOOLMIME_DATA:
466 #ifdef CURL_DOES_CONVERSIONS
467 /* Data will be set in ASCII, thus issue a comment with clear text. */
468 escaped = c_escape(part->data, ZERO_TERMINATED);
469 NULL_CHECK(escaped);
470 CODE1("/* \"%s\" */", escaped);
471
472 /* Our data is always textual: convert it to ASCII. */
473 {
474 size_t size = strlen(part->data);
475 char *cp = malloc(size + 1);
476
477 NULL_CHECK(cp);
478 memcpy(cp, part->data, size + 1);
479 ret = convert_to_network(cp, size);
480 data = cp;
481 }
482 #else
483 data = part->data;
484 #endif
485 if(!ret) {
486 Curl_safefree(escaped);
487 escaped = c_escape(data, ZERO_TERMINATED);
488 NULL_CHECK(escaped);
489 CODE2("curl_mime_data(part%d, \"%s\", CURL_ZERO_TERMINATED);",
490 mimeno, escaped);
491 }
492 break;
493
494 case TOOLMIME_FILE:
495 case TOOLMIME_FILEDATA:
496 escaped = c_escape(part->data, ZERO_TERMINATED);
497 NULL_CHECK(escaped);
498 CODE2("curl_mime_filedata(part%d, \"%s\");", mimeno, escaped);
499 if(part->kind == TOOLMIME_FILEDATA && !filename) {
500 CODE1("curl_mime_filename(part%d, NULL);", mimeno);
501 }
502 break;
503
504 case TOOLMIME_STDIN:
505 if(!filename)
506 filename = "-";
507 /* FALLTHROUGH */
508 case TOOLMIME_STDINDATA:
509 /* Can only be reading stdin in the current context. */
510 CODE1("curl_mime_data_cb(part%d, -1, (curl_read_callback) fread, \\",
511 mimeno);
512 CODE0(" (curl_seek_callback) fseek, NULL, stdin);");
513 break;
514 default:
515 /* Other cases not possible in this context. */
516 break;
517 }
518
519 if(!ret && part->encoder) {
520 Curl_safefree(escaped);
521 escaped = c_escape(part->encoder, ZERO_TERMINATED);
522 NULL_CHECK(escaped);
523 CODE2("curl_mime_encoder(part%d, \"%s\");", mimeno, escaped);
524 }
525
526 if(!ret && filename) {
527 Curl_safefree(escaped);
528 escaped = c_escape(filename, ZERO_TERMINATED);
529 NULL_CHECK(escaped);
530 CODE2("curl_mime_filename(part%d, \"%s\");", mimeno, escaped);
531 }
532
533 if(!ret && part->name) {
534 Curl_safefree(escaped);
535 escaped = c_escape(part->name, ZERO_TERMINATED);
536 NULL_CHECK(escaped);
537 CODE2("curl_mime_name(part%d, \"%s\");", mimeno, escaped);
538 }
539
540 if(!ret && part->type) {
541 Curl_safefree(escaped);
542 escaped = c_escape(part->type, ZERO_TERMINATED);
543 NULL_CHECK(escaped);
544 CODE2("curl_mime_type(part%d, \"%s\");", mimeno, escaped);
545 }
546
547 if(!ret && part->headers) {
548 int slistno;
549
550 ret = libcurl_generate_slist(part->headers, &slistno);
551 if(!ret) {
552 CODE2("curl_mime_headers(part%d, slist%d, 1);", mimeno, slistno);
553 CODE1("slist%d = NULL;", slistno); /* Prevent CLEANing. */
554 }
555 }
556
557 nomem:
558 #ifdef CURL_DOES_CONVERSIONS
559 if(data)
560 free((char *) data);
561 #endif
562
563 Curl_safefree(escaped);
564 return ret;
565 }
566
567 /* Wrapper to generate source code for a mime structure. */
libcurl_generate_mime(CURL * curl,struct GlobalConfig * config,struct tool_mime * toolmime,int * mimeno)568 static CURLcode libcurl_generate_mime(CURL *curl,
569 struct GlobalConfig *config,
570 struct tool_mime *toolmime,
571 int *mimeno)
572 {
573 CURLcode ret = CURLE_OK;
574
575 /* May need several mime variables, so invent name. */
576 *mimeno = ++easysrc_mime_count;
577 DECL1("curl_mime *mime%d;", *mimeno);
578 DATA1("mime%d = NULL;", *mimeno);
579 CODE1("mime%d = curl_mime_init(hnd);", *mimeno);
580 CLEAN1("curl_mime_free(mime%d);", *mimeno);
581 CLEAN1("mime%d = NULL;", *mimeno);
582
583 if(toolmime->subparts) {
584 DECL1("curl_mimepart *part%d;", *mimeno);
585 ret = libcurl_generate_mime_part(curl, config,
586 toolmime->subparts, *mimeno);
587 }
588
589 nomem:
590 return ret;
591 }
592
593 /* setopt wrapper for CURLOPT_MIMEPOST */
tool_setopt_mimepost(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,curl_mime * mimepost)594 CURLcode tool_setopt_mimepost(CURL *curl, struct GlobalConfig *config,
595 const char *name, CURLoption tag,
596 curl_mime *mimepost)
597 {
598 CURLcode ret = curl_easy_setopt(curl, tag, mimepost);
599 int mimeno = 0;
600
601 if(!ret && config->libcurl) {
602 ret = libcurl_generate_mime(curl, config,
603 config->current->mimeroot, &mimeno);
604
605 if(!ret)
606 CODE2("curl_easy_setopt(hnd, %s, mime%d);", name, mimeno);
607 }
608
609 nomem:
610 return ret;
611 }
612
613 /* setopt wrapper for curl_slist options */
tool_setopt_slist(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,struct curl_slist * list)614 CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
615 const char *name, CURLoption tag,
616 struct curl_slist *list)
617 {
618 CURLcode ret = CURLE_OK;
619
620 ret = curl_easy_setopt(curl, tag, list);
621
622 if(config->libcurl && list && !ret) {
623 int i;
624
625 ret = libcurl_generate_slist(list, &i);
626 if(!ret)
627 CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
628 }
629
630 nomem:
631 return ret;
632 }
633
634 /* generic setopt wrapper for all other options.
635 * Some type information is encoded in the tag value. */
tool_setopt(CURL * curl,bool str,struct GlobalConfig * global,struct OperationConfig * config,const char * name,CURLoption tag,...)636 CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *global,
637 struct OperationConfig *config,
638 const char *name, CURLoption tag, ...)
639 {
640 va_list arg;
641 char buf[256];
642 const char *value = NULL;
643 bool remark = FALSE;
644 bool skip = FALSE;
645 bool escape = FALSE;
646 char *escaped = NULL;
647 CURLcode ret = CURLE_OK;
648
649 va_start(arg, tag);
650
651 if(tag < CURLOPTTYPE_OBJECTPOINT) {
652 /* Value is expected to be a long */
653 long lval = va_arg(arg, long);
654 long defval = 0L;
655 const struct NameValue *nv = NULL;
656 for(nv = setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
657 if(!strcmp(name, nv->name)) {
658 defval = nv->value;
659 break; /* found it */
660 }
661 }
662
663 msnprintf(buf, sizeof(buf), "%ldL", lval);
664 value = buf;
665 ret = curl_easy_setopt(curl, tag, lval);
666 if(lval == defval)
667 skip = TRUE;
668 }
669 else if(tag < CURLOPTTYPE_OFF_T) {
670 /* Value is some sort of object pointer */
671 void *pval = va_arg(arg, void *);
672
673 /* function pointers are never printable */
674 if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
675 if(pval) {
676 value = "functionpointer";
677 remark = TRUE;
678 }
679 else
680 skip = TRUE;
681 }
682
683 else if(pval && str) {
684 value = (char *)pval;
685 escape = TRUE;
686 }
687 else if(pval) {
688 value = "objectpointer";
689 remark = TRUE;
690 }
691 else
692 skip = TRUE;
693
694 ret = curl_easy_setopt(curl, tag, pval);
695
696 }
697 else if(tag < CURLOPTTYPE_BLOB) {
698 /* Value is expected to be curl_off_t */
699 curl_off_t oval = va_arg(arg, curl_off_t);
700 msnprintf(buf, sizeof(buf),
701 "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
702 value = buf;
703 ret = curl_easy_setopt(curl, tag, oval);
704
705 if(!oval)
706 skip = TRUE;
707 }
708 else {
709 /* Value is a blob */
710 void *pblob = va_arg(arg, void *);
711
712 /* blobs are never printable */
713 if(pblob) {
714 value = "blobpointer";
715 remark = TRUE;
716 }
717 else
718 skip = TRUE;
719
720 ret = curl_easy_setopt(curl, tag, pblob);
721 }
722
723 va_end(arg);
724
725 if(global->libcurl && !skip && !ret) {
726 /* we only use this for real if --libcurl was used */
727
728 if(remark)
729 REM2("%s set to a %s", name, value);
730 else {
731 if(escape) {
732 curl_off_t len = ZERO_TERMINATED;
733 if(tag == CURLOPT_POSTFIELDS)
734 len = config->postfieldsize;
735 escaped = c_escape(value, len);
736 NULL_CHECK(escaped);
737 CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
738 }
739 else
740 CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
741 }
742 }
743
744 nomem:
745 Curl_safefree(escaped);
746 return ret;
747 }
748
749 #else /* CURL_DISABLE_LIBCURL_OPTION */
750
751 #include "tool_cfgable.h"
752 #include "tool_setopt.h"
753
754 #endif /* CURL_DISABLE_LIBCURL_OPTION */
755
756 /*
757 * tool_setopt_skip() allows the curl tool code to avoid setopt options that
758 * are explicitly disabled in the build.
759 */
tool_setopt_skip(CURLoption tag)760 bool tool_setopt_skip(CURLoption tag)
761 {
762 #ifdef CURL_DISABLE_PROXY
763 #define USED_TAG
764 switch(tag) {
765 case CURLOPT_HAPROXYPROTOCOL:
766 case CURLOPT_HTTPPROXYTUNNEL:
767 case CURLOPT_NOPROXY:
768 case CURLOPT_PRE_PROXY:
769 case CURLOPT_PROXY:
770 case CURLOPT_PROXYAUTH:
771 case CURLOPT_PROXY_CAINFO:
772 case CURLOPT_PROXY_CAPATH:
773 case CURLOPT_PROXY_CRLFILE:
774 case CURLOPT_PROXYHEADER:
775 case CURLOPT_PROXY_KEYPASSWD:
776 case CURLOPT_PROXYPASSWORD:
777 case CURLOPT_PROXY_PINNEDPUBLICKEY:
778 case CURLOPT_PROXYPORT:
779 case CURLOPT_PROXY_SERVICE_NAME:
780 case CURLOPT_PROXY_SSLCERT:
781 case CURLOPT_PROXY_SSLCERTTYPE:
782 case CURLOPT_PROXY_SSL_CIPHER_LIST:
783 case CURLOPT_PROXY_SSLKEY:
784 case CURLOPT_PROXY_SSLKEYTYPE:
785 case CURLOPT_PROXY_SSL_OPTIONS:
786 case CURLOPT_PROXY_SSL_VERIFYHOST:
787 case CURLOPT_PROXY_SSL_VERIFYPEER:
788 case CURLOPT_PROXY_SSLVERSION:
789 case CURLOPT_PROXY_TLS13_CIPHERS:
790 case CURLOPT_PROXY_TLSAUTH_PASSWORD:
791 case CURLOPT_PROXY_TLSAUTH_TYPE:
792 case CURLOPT_PROXY_TLSAUTH_USERNAME:
793 case CURLOPT_PROXY_TRANSFER_MODE:
794 case CURLOPT_PROXYTYPE:
795 case CURLOPT_PROXYUSERNAME:
796 case CURLOPT_PROXYUSERPWD:
797 return TRUE;
798 default:
799 break;
800 }
801 #endif
802 #ifdef CURL_DISABLE_FTP
803 #define USED_TAG
804 switch(tag) {
805 case CURLOPT_FTPPORT:
806 case CURLOPT_FTP_ACCOUNT:
807 case CURLOPT_FTP_ALTERNATIVE_TO_USER:
808 case CURLOPT_FTP_FILEMETHOD:
809 case CURLOPT_FTP_SKIP_PASV_IP:
810 case CURLOPT_FTP_USE_EPRT:
811 case CURLOPT_FTP_USE_EPSV:
812 case CURLOPT_FTP_USE_PRET:
813 case CURLOPT_KRBLEVEL:
814 return TRUE;
815 default:
816 break;
817 }
818 #endif
819 #ifdef CURL_DISABLE_RTSP
820 #define USED_TAG
821 switch(tag) {
822 case CURLOPT_INTERLEAVEDATA:
823 return TRUE;
824 default:
825 break;
826 }
827 #endif
828 #if defined(CURL_DISABLE_HTTP) || defined(CURL_DISABLE_COOKIES)
829 #define USED_TAG
830 switch(tag) {
831 case CURLOPT_COOKIE:
832 case CURLOPT_COOKIEFILE:
833 case CURLOPT_COOKIEJAR:
834 case CURLOPT_COOKIESESSION:
835 return TRUE;
836 default:
837 break;
838 }
839 #endif
840 #if defined(CURL_DISABLE_TELNET)
841 #define USED_TAG
842 switch(tag) {
843 case CURLOPT_TELNETOPTIONS:
844 return TRUE;
845 default:
846 break;
847 }
848 #endif
849 #ifdef CURL_DISABLE_TFTP
850 #define USED_TAG
851 switch(tag) {
852 case CURLOPT_TFTP_BLKSIZE:
853 case CURLOPT_TFTP_NO_OPTIONS:
854 return TRUE;
855 default:
856 break;
857 }
858 #endif
859 #ifdef CURL_DISABLE_NETRC
860 #define USED_TAG
861 switch(tag) {
862 case CURLOPT_NETRC:
863 case CURLOPT_NETRC_FILE:
864 return TRUE;
865 default:
866 break;
867 }
868 #endif
869
870 #ifndef USED_TAG
871 (void)tag;
872 #endif
873 return FALSE;
874 }
875