xref: /curl/src/tool_setopt.c (revision 50def7c8)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #include "tool_setup.h"
25 
26 #ifndef CURL_DISABLE_LIBCURL_OPTION
27 
28 #define ENABLE_CURLX_PRINTF
29 /* use our own printf() functions */
30 #include "curlx.h"
31 
32 #include "tool_cfgable.h"
33 #include "tool_easysrc.h"
34 #include "tool_setopt.h"
35 #include "tool_msgs.h"
36 #include "dynbuf.h"
37 
38 #include "memdebug.h" /* keep this as LAST include */
39 
40 /* Lookup tables for converting setopt values back to symbols */
41 /* For enums, values may be in any order. */
42 /* For bit masks, put combinations first, then single bits, */
43 /* and finally any "NONE" value. */
44 
45 #define NV(e) {#e, e}
46 #define NV1(e, v) {#e, (v)}
47 #define NVEND {NULL, 0}         /* sentinel to mark end of list */
48 
49 const struct NameValue setopt_nv_CURLPROXY[] = {
50   NV(CURLPROXY_HTTP),
51   NV(CURLPROXY_HTTP_1_0),
52   NV(CURLPROXY_HTTPS),
53   NV(CURLPROXY_SOCKS4),
54   NV(CURLPROXY_SOCKS5),
55   NV(CURLPROXY_SOCKS4A),
56   NV(CURLPROXY_SOCKS5_HOSTNAME),
57   NVEND,
58 };
59 
60 const struct NameValue setopt_nv_CURL_SOCKS_PROXY[] = {
61   NV(CURLPROXY_SOCKS4),
62   NV(CURLPROXY_SOCKS5),
63   NV(CURLPROXY_SOCKS4A),
64   NV(CURLPROXY_SOCKS5_HOSTNAME),
65   NVEND,
66 };
67 
68 const struct NameValueUnsigned setopt_nv_CURLHSTS[] = {
69   NV(CURLHSTS_ENABLE),
70   NVEND,
71 };
72 
73 const struct NameValueUnsigned setopt_nv_CURLAUTH[] = {
74   NV(CURLAUTH_ANY),             /* combination */
75   NV(CURLAUTH_ANYSAFE),         /* combination */
76   NV(CURLAUTH_BASIC),
77   NV(CURLAUTH_DIGEST),
78   NV(CURLAUTH_GSSNEGOTIATE),
79   NV(CURLAUTH_NTLM),
80   NV(CURLAUTH_DIGEST_IE),
81   NV(CURLAUTH_ONLY),
82   NV(CURLAUTH_NONE),
83   NVEND,
84 };
85 
86 const struct NameValue setopt_nv_CURL_HTTP_VERSION[] = {
87   NV(CURL_HTTP_VERSION_NONE),
88   NV(CURL_HTTP_VERSION_1_0),
89   NV(CURL_HTTP_VERSION_1_1),
90   NV(CURL_HTTP_VERSION_2_0),
91   NV(CURL_HTTP_VERSION_2TLS),
92   NV(CURL_HTTP_VERSION_3),
93   NV(CURL_HTTP_VERSION_3ONLY),
94   NVEND,
95 };
96 
97 const struct NameValue setopt_nv_CURL_SSLVERSION[] = {
98   NV(CURL_SSLVERSION_DEFAULT),
99   NV(CURL_SSLVERSION_TLSv1),
100   NV(CURL_SSLVERSION_SSLv2),
101   NV(CURL_SSLVERSION_SSLv3),
102   NV(CURL_SSLVERSION_TLSv1_0),
103   NV(CURL_SSLVERSION_TLSv1_1),
104   NV(CURL_SSLVERSION_TLSv1_2),
105   NV(CURL_SSLVERSION_TLSv1_3),
106   NVEND,
107 };
108 
109 const struct NameValue setopt_nv_CURL_SSLVERSION_MAX[] = {
110   NV(CURL_SSLVERSION_MAX_NONE),
111   NV(CURL_SSLVERSION_MAX_DEFAULT),
112   NV(CURL_SSLVERSION_MAX_TLSv1_0),
113   NV(CURL_SSLVERSION_MAX_TLSv1_1),
114   NV(CURL_SSLVERSION_MAX_TLSv1_2),
115   NV(CURL_SSLVERSION_MAX_TLSv1_3),
116   NVEND,
117 };
118 
119 const struct NameValue setopt_nv_CURL_TIMECOND[] = {
120   NV(CURL_TIMECOND_IFMODSINCE),
121   NV(CURL_TIMECOND_IFUNMODSINCE),
122   NV(CURL_TIMECOND_LASTMOD),
123   NV(CURL_TIMECOND_NONE),
124   NVEND,
125 };
126 
127 const struct NameValue setopt_nv_CURLFTPSSL_CCC[] = {
128   NV(CURLFTPSSL_CCC_NONE),
129   NV(CURLFTPSSL_CCC_PASSIVE),
130   NV(CURLFTPSSL_CCC_ACTIVE),
131   NVEND,
132 };
133 
134 const struct NameValue setopt_nv_CURLUSESSL[] = {
135   NV(CURLUSESSL_NONE),
136   NV(CURLUSESSL_TRY),
137   NV(CURLUSESSL_CONTROL),
138   NV(CURLUSESSL_ALL),
139   NVEND,
140 };
141 
142 const struct NameValueUnsigned setopt_nv_CURLSSLOPT[] = {
143   NV(CURLSSLOPT_ALLOW_BEAST),
144   NV(CURLSSLOPT_NO_REVOKE),
145   NV(CURLSSLOPT_NO_PARTIALCHAIN),
146   NV(CURLSSLOPT_REVOKE_BEST_EFFORT),
147   NV(CURLSSLOPT_NATIVE_CA),
148   NV(CURLSSLOPT_AUTO_CLIENT_CERT),
149   NVEND,
150 };
151 
152 const struct NameValue setopt_nv_CURL_NETRC[] = {
153   NV(CURL_NETRC_IGNORED),
154   NV(CURL_NETRC_OPTIONAL),
155   NV(CURL_NETRC_REQUIRED),
156   NVEND,
157 };
158 
159 /* These options have non-zero default values. */
160 static const struct NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
161   NV1(CURLOPT_SSL_VERIFYPEER, 1),
162   NV1(CURLOPT_SSL_VERIFYHOST, 1),
163   NV1(CURLOPT_SSL_ENABLE_NPN, 1),
164   NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
165   NV1(CURLOPT_TCP_NODELAY, 1),
166   NV1(CURLOPT_PROXY_SSL_VERIFYPEER, 1),
167   NV1(CURLOPT_PROXY_SSL_VERIFYHOST, 1),
168   NV1(CURLOPT_SOCKS5_AUTH, 1),
169   NVEND
170 };
171 
172 /* Format and add code; jump to nomem on malloc error */
173 #define ADD(args) do { \
174   ret = easysrc_add args; \
175   if(ret) \
176     goto nomem; \
177 } while(0)
178 #define ADDF(args) do { \
179   ret = easysrc_addf args; \
180   if(ret) \
181     goto nomem; \
182 } while(0)
183 #define NULL_CHECK(p) do { \
184   if(!p) { \
185     ret = CURLE_OUT_OF_MEMORY; \
186     goto nomem; \
187   } \
188 } while(0)
189 
190 #define DECL0(s) ADD((&easysrc_decl, s))
191 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
192 
193 #define DATA0(s) ADD((&easysrc_data, s))
194 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
195 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
196 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
197 
198 #define CODE0(s) ADD((&easysrc_code, s))
199 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
200 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
201 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
202 
203 #define CLEAN0(s) ADD((&easysrc_clean, s))
204 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
205 
206 #define REM0(s) ADD((&easysrc_toohard, s))
207 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
208 #define REM3(f,a,b,c) ADDF((&easysrc_toohard, f,a,b,c))
209 
210 /* Escape string to C string syntax.  Return NULL if out of memory.
211  * Is this correct for those wacky EBCDIC guys? */
212 
213 #define MAX_STRING_LENGTH_OUTPUT 2000
214 #define ZERO_TERMINATED -1
215 
c_escape(const char * str,curl_off_t len)216 static char *c_escape(const char *str, curl_off_t len)
217 {
218   const char *s;
219   unsigned int cutoff = 0;
220   CURLcode result;
221   struct curlx_dynbuf escaped;
222 
223   curlx_dyn_init(&escaped, 4 * MAX_STRING_LENGTH_OUTPUT + 3);
224 
225   if(len == ZERO_TERMINATED)
226     len = strlen(str);
227 
228   if(len > MAX_STRING_LENGTH_OUTPUT) {
229     /* cap ridiculously long strings */
230     len = MAX_STRING_LENGTH_OUTPUT;
231     cutoff = 3;
232   }
233 
234   result = curlx_dyn_addn(&escaped, STRCONST(""));
235   for(s = str; !result && len; s++, len--) {
236     /* escape question marks as well, to prevent generating accidental
237        trigraphs */
238     static const char from[] = "\t\r\n?\"\\";
239     static const char to[] = "\\t\\r\\n\\?\\\"\\\\";
240     const char *p = strchr(from, *s);
241 
242     if(!p && ISPRINT(*s))
243       continue;
244 
245     result = curlx_dyn_addn(&escaped, str, s - str);
246     str = s + 1;
247 
248     if(!result) {
249       if(p && *p)
250         result = curlx_dyn_addn(&escaped, to + 2 * (p - from), 2);
251       else {
252         result = curlx_dyn_addf(&escaped,
253                                 /* Octal escape to avoid >2 digit hex. */
254                                 (len > 1 && ISXDIGIT(s[1])) ?
255                                   "\\%03o" : "\\x%02x",
256                                 (unsigned int) *(unsigned char *) s);
257       }
258     }
259   }
260 
261   if(!result)
262     result = curlx_dyn_addn(&escaped, str, s - str);
263 
264   if(!result)
265     (void) !curlx_dyn_addn(&escaped, "...", cutoff);
266 
267   return curlx_dyn_ptr(&escaped);
268 }
269 
270 /* setopt wrapper for enum types */
tool_setopt_enum(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,const struct NameValue * nvlist,long lval)271 CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
272                           const char *name, CURLoption tag,
273                           const struct NameValue *nvlist, long lval)
274 {
275   CURLcode ret = CURLE_OK;
276   bool skip = FALSE;
277 
278   ret = curl_easy_setopt(curl, tag, lval);
279   if(!lval)
280     skip = TRUE;
281 
282   if(config->libcurl && !skip && !ret) {
283     /* we only use this for real if --libcurl was used */
284     const struct NameValue *nv = NULL;
285     for(nv = nvlist; nv->name; nv++) {
286       if(nv->value == lval)
287         break; /* found it */
288     }
289     if(!nv->name) {
290       /* If no definition was found, output an explicit value.
291        * This could happen if new values are defined and used
292        * but the NameValue list is not updated. */
293       CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
294     }
295     else {
296       CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
297     }
298   }
299 
300 #ifdef DEBUGBUILD
301   if(ret)
302     warnf(config, "option %s returned error (%d)", name, (int)ret);
303 #endif
304 nomem:
305   return ret;
306 }
307 
308 /* setopt wrapper for CURLOPT_SSLVERSION */
tool_setopt_SSLVERSION(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,long lval)309 CURLcode tool_setopt_SSLVERSION(CURL *curl, struct GlobalConfig *config,
310                                 const char *name, CURLoption tag,
311                                 long lval)
312 {
313   CURLcode ret = CURLE_OK;
314   bool skip = FALSE;
315 
316   ret = curl_easy_setopt(curl, tag, lval);
317   if(!lval)
318     skip = TRUE;
319 
320   if(config->libcurl && !skip && !ret) {
321     /* we only use this for real if --libcurl was used */
322     const struct NameValue *nv = NULL;
323     const struct NameValue *nv2 = NULL;
324     for(nv = setopt_nv_CURL_SSLVERSION; nv->name; nv++) {
325       if(nv->value == (lval & 0xffff))
326         break; /* found it */
327     }
328     for(nv2 = setopt_nv_CURL_SSLVERSION_MAX; nv2->name; nv2++) {
329       if(nv2->value == (lval & ~0xffff))
330         break; /* found it */
331     }
332     if(!nv->name) {
333       /* If no definition was found, output an explicit value.
334        * This could happen if new values are defined and used
335        * but the NameValue list is not updated. */
336       CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
337     }
338     else {
339       CODE3("curl_easy_setopt(hnd, %s, (long)(%s | %s));",
340             name, nv->name, nv2->name);
341     }
342   }
343 
344 #ifdef DEBUGBUILD
345   if(ret)
346     warnf(config, "option %s returned error (%d)", name, (int)ret);
347 #endif
348 nomem:
349   return ret;
350 }
351 
352 /* setopt wrapper for bitmasks */
tool_setopt_bitmask(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,const struct NameValueUnsigned * nvlist,long lval)353 CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
354                              const char *name, CURLoption tag,
355                              const struct NameValueUnsigned *nvlist,
356                              long lval)
357 {
358   CURLcode ret = CURLE_OK;
359   bool skip = FALSE;
360 
361   ret = curl_easy_setopt(curl, tag, lval);
362   if(!lval)
363     skip = TRUE;
364 
365   if(config->libcurl && !skip && !ret) {
366     /* we only use this for real if --libcurl was used */
367     char preamble[80];
368     unsigned long rest = (unsigned long)lval;
369     const struct NameValueUnsigned *nv = NULL;
370     msnprintf(preamble, sizeof(preamble),
371               "curl_easy_setopt(hnd, %s, ", name);
372     for(nv = nvlist; nv->name; nv++) {
373       if((nv->value & ~ rest) == 0) {
374         /* all value flags contained in rest */
375         rest &= ~ nv->value;    /* remove bits handled here */
376         CODE3("%s(long)%s%s",
377               preamble, nv->name, rest ? " |" : ");");
378         if(!rest)
379           break;                /* handled them all */
380         /* replace with all spaces for continuation line */
381         msnprintf(preamble, sizeof(preamble), "%*s", (int)strlen(preamble),
382                   "");
383       }
384     }
385     /* If any bits have no definition, output an explicit value.
386      * This could happen if new bits are defined and used
387      * but the NameValue list is not updated. */
388     if(rest)
389       CODE2("%s%luUL);", preamble, rest);
390   }
391 
392 nomem:
393   return ret;
394 }
395 
396 /* Generate code for a struct curl_slist. */
libcurl_generate_slist(struct curl_slist * slist,int * slistno)397 static CURLcode libcurl_generate_slist(struct curl_slist *slist, int *slistno)
398 {
399   CURLcode ret = CURLE_OK;
400   char *escaped = NULL;
401 
402   /* May need several slist variables, so invent name */
403   *slistno = ++easysrc_slist_count;
404 
405   DECL1("struct curl_slist *slist%d;", *slistno);
406   DATA1("slist%d = NULL;", *slistno);
407   CLEAN1("curl_slist_free_all(slist%d);", *slistno);
408   CLEAN1("slist%d = NULL;", *slistno);
409   for(; slist; slist = slist->next) {
410     Curl_safefree(escaped);
411     escaped = c_escape(slist->data, ZERO_TERMINATED);
412     if(!escaped)
413       return CURLE_OUT_OF_MEMORY;
414     DATA3("slist%d = curl_slist_append(slist%d, \"%s\");",
415                                        *slistno, *slistno, escaped);
416   }
417 
418 nomem:
419   Curl_safefree(escaped);
420   return ret;
421 }
422 
423 static CURLcode libcurl_generate_mime(CURL *curl,
424                                       struct GlobalConfig *config,
425                                       struct tool_mime *toolmime,
426                                       int *mimeno);     /* Forward. */
427 
428 /* Wrapper to generate source code for a mime part. */
libcurl_generate_mime_part(CURL * curl,struct GlobalConfig * config,struct tool_mime * part,int mimeno)429 static CURLcode libcurl_generate_mime_part(CURL *curl,
430                                            struct GlobalConfig *config,
431                                            struct tool_mime *part,
432                                            int mimeno)
433 {
434   CURLcode ret = CURLE_OK;
435   int submimeno = 0;
436   char *escaped = NULL;
437   const char *data = NULL;
438   const char *filename = part->filename;
439 
440   /* Parts are linked in reverse order. */
441   if(part->prev) {
442     ret = libcurl_generate_mime_part(curl, config, part->prev, mimeno);
443     if(ret)
444       return ret;
445   }
446 
447   /* Create the part. */
448   CODE2("part%d = curl_mime_addpart(mime%d);", mimeno, mimeno);
449 
450   switch(part->kind) {
451   case TOOLMIME_PARTS:
452     ret = libcurl_generate_mime(curl, config, part, &submimeno);
453     if(!ret) {
454       CODE2("curl_mime_subparts(part%d, mime%d);", mimeno, submimeno);
455       CODE1("mime%d = NULL;", submimeno);   /* Avoid freeing in CLEAN. */
456     }
457     break;
458 
459   case TOOLMIME_DATA:
460     data = part->data;
461     if(!ret) {
462       Curl_safefree(escaped);
463       escaped = c_escape(data, ZERO_TERMINATED);
464       NULL_CHECK(escaped);
465       CODE2("curl_mime_data(part%d, \"%s\", CURL_ZERO_TERMINATED);",
466                             mimeno, escaped);
467     }
468     break;
469 
470   case TOOLMIME_FILE:
471   case TOOLMIME_FILEDATA:
472     escaped = c_escape(part->data, ZERO_TERMINATED);
473     NULL_CHECK(escaped);
474     CODE2("curl_mime_filedata(part%d, \"%s\");", mimeno, escaped);
475     if(part->kind == TOOLMIME_FILEDATA && !filename) {
476       CODE1("curl_mime_filename(part%d, NULL);", mimeno);
477     }
478     break;
479 
480   case TOOLMIME_STDIN:
481     if(!filename)
482       filename = "-";
483     FALLTHROUGH();
484   case TOOLMIME_STDINDATA:
485     /* Can only be reading stdin in the current context. */
486     CODE1("curl_mime_data_cb(part%d, -1, (curl_read_callback) fread, \\",
487           mimeno);
488     CODE0("                  (curl_seek_callback) fseek, NULL, stdin);");
489     break;
490   default:
491     /* Other cases not possible in this context. */
492     break;
493   }
494 
495   if(!ret && part->encoder) {
496     Curl_safefree(escaped);
497     escaped = c_escape(part->encoder, ZERO_TERMINATED);
498     NULL_CHECK(escaped);
499     CODE2("curl_mime_encoder(part%d, \"%s\");", mimeno, escaped);
500   }
501 
502   if(!ret && filename) {
503     Curl_safefree(escaped);
504     escaped = c_escape(filename, ZERO_TERMINATED);
505     NULL_CHECK(escaped);
506     CODE2("curl_mime_filename(part%d, \"%s\");", mimeno, escaped);
507   }
508 
509   if(!ret && part->name) {
510     Curl_safefree(escaped);
511     escaped = c_escape(part->name, ZERO_TERMINATED);
512     NULL_CHECK(escaped);
513     CODE2("curl_mime_name(part%d, \"%s\");", mimeno, escaped);
514   }
515 
516   if(!ret && part->type) {
517     Curl_safefree(escaped);
518     escaped = c_escape(part->type, ZERO_TERMINATED);
519     NULL_CHECK(escaped);
520     CODE2("curl_mime_type(part%d, \"%s\");", mimeno, escaped);
521   }
522 
523   if(!ret && part->headers) {
524     int slistno;
525 
526     ret = libcurl_generate_slist(part->headers, &slistno);
527     if(!ret) {
528       CODE2("curl_mime_headers(part%d, slist%d, 1);", mimeno, slistno);
529       CODE1("slist%d = NULL;", slistno); /* Prevent CLEANing. */
530     }
531   }
532 
533 nomem:
534   Curl_safefree(escaped);
535   return ret;
536 }
537 
538 /* Wrapper to generate source code for a mime structure. */
libcurl_generate_mime(CURL * curl,struct GlobalConfig * config,struct tool_mime * toolmime,int * mimeno)539 static CURLcode libcurl_generate_mime(CURL *curl,
540                                       struct GlobalConfig *config,
541                                       struct tool_mime *toolmime,
542                                       int *mimeno)
543 {
544   CURLcode ret = CURLE_OK;
545 
546   /* May need several mime variables, so invent name. */
547   *mimeno = ++easysrc_mime_count;
548   DECL1("curl_mime *mime%d;", *mimeno);
549   DATA1("mime%d = NULL;", *mimeno);
550   CODE1("mime%d = curl_mime_init(hnd);", *mimeno);
551   CLEAN1("curl_mime_free(mime%d);", *mimeno);
552   CLEAN1("mime%d = NULL;", *mimeno);
553 
554   if(toolmime->subparts) {
555     DECL1("curl_mimepart *part%d;", *mimeno);
556     ret = libcurl_generate_mime_part(curl, config,
557                                      toolmime->subparts, *mimeno);
558   }
559 
560 nomem:
561   return ret;
562 }
563 
564 /* setopt wrapper for CURLOPT_MIMEPOST */
tool_setopt_mimepost(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,curl_mime * mimepost)565 CURLcode tool_setopt_mimepost(CURL *curl, struct GlobalConfig *config,
566                               const char *name, CURLoption tag,
567                               curl_mime *mimepost)
568 {
569   CURLcode ret = curl_easy_setopt(curl, tag, mimepost);
570   int mimeno = 0;
571 
572   if(!ret && config->libcurl) {
573     ret = libcurl_generate_mime(curl, config,
574                                 config->current->mimeroot, &mimeno);
575 
576     if(!ret)
577       CODE2("curl_easy_setopt(hnd, %s, mime%d);", name, mimeno);
578   }
579 
580 nomem:
581   return ret;
582 }
583 
584 /* setopt wrapper for curl_slist options */
tool_setopt_slist(CURL * curl,struct GlobalConfig * config,const char * name,CURLoption tag,struct curl_slist * list)585 CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
586                            const char *name, CURLoption tag,
587                            struct curl_slist *list)
588 {
589   CURLcode ret = CURLE_OK;
590 
591   ret = curl_easy_setopt(curl, tag, list);
592 
593   if(config->libcurl && list && !ret) {
594     int i;
595 
596     ret = libcurl_generate_slist(list, &i);
597     if(!ret)
598       CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
599   }
600 
601 nomem:
602   return ret;
603 }
604 
605 /* generic setopt wrapper for all other options.
606  * 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,...)607 CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *global,
608                      struct OperationConfig *config,
609                      const char *name, CURLoption tag, ...)
610 {
611   va_list arg;
612   char buf[256];
613   const char *value = NULL;
614   bool remark = FALSE;
615   bool skip = FALSE;
616   bool escape = FALSE;
617   char *escaped = NULL;
618   CURLcode ret = CURLE_OK;
619 
620   va_start(arg, tag);
621 
622   if(tag < CURLOPTTYPE_OBJECTPOINT) {
623     /* Value is expected to be a long */
624     long lval = va_arg(arg, long);
625     long defval = 0L;
626     const struct NameValue *nv = NULL;
627     for(nv = setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
628       if(!strcmp(name, nv->name)) {
629         defval = nv->value;
630         break; /* found it */
631       }
632     }
633 
634     msnprintf(buf, sizeof(buf), "%ldL", lval);
635     value = buf;
636     ret = curl_easy_setopt(curl, tag, lval);
637     if(lval == defval)
638       skip = TRUE;
639   }
640   else if(tag < CURLOPTTYPE_OFF_T) {
641     /* Value is some sort of object pointer */
642     void *pval = va_arg(arg, void *);
643 
644     /* function pointers are never printable */
645     if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
646       if(pval) {
647         value = "function pointer";
648         remark = TRUE;
649       }
650       else
651         skip = TRUE;
652     }
653 
654     else if(pval && str) {
655       value = (char *)pval;
656       escape = TRUE;
657     }
658     else if(pval) {
659       value = "object pointer";
660       remark = TRUE;
661     }
662     else
663       skip = TRUE;
664 
665     ret = curl_easy_setopt(curl, tag, pval);
666 
667   }
668   else if(tag < CURLOPTTYPE_BLOB) {
669     /* Value is expected to be curl_off_t */
670     curl_off_t oval = va_arg(arg, curl_off_t);
671     msnprintf(buf, sizeof(buf),
672               "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
673     value = buf;
674     ret = curl_easy_setopt(curl, tag, oval);
675 
676     if(!oval)
677       skip = TRUE;
678   }
679   else {
680     /* Value is a blob */
681     void *pblob = va_arg(arg, void *);
682 
683     /* blobs are never printable */
684     if(pblob) {
685       value = "blob pointer";
686       remark = TRUE;
687     }
688     else
689       skip = TRUE;
690 
691     ret = curl_easy_setopt(curl, tag, pblob);
692   }
693 
694   va_end(arg);
695 
696   if(global->libcurl && !skip && !ret) {
697     /* we only use this for real if --libcurl was used */
698 
699     if(remark)
700       REM3("%s was set to a%s %s", name, (*value == 'o' ? "n" : ""), value);
701     else {
702       if(escape) {
703         curl_off_t len = ZERO_TERMINATED;
704         if(tag == CURLOPT_POSTFIELDS)
705           len = curlx_dyn_len(&config->postdata);
706         escaped = c_escape(value, len);
707         NULL_CHECK(escaped);
708         CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
709       }
710       else
711         CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
712     }
713   }
714 
715 nomem:
716   Curl_safefree(escaped);
717   return ret;
718 }
719 
720 #else /* CURL_DISABLE_LIBCURL_OPTION */
721 
722 #endif /* CURL_DISABLE_LIBCURL_OPTION */
723