Revert "Imported Upstream version 7.53.1"
[platform/upstream/curl.git] / src / tool_setopt.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, 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
34 #include "memdebug.h" /* keep this as LAST include */
35
36 /* Lookup tables for converting setopt values back to symbols */
37 /* For enums, values may be in any order. */
38 /* For bit masks, put combinations first, then single bits, */
39 /* and finally any "NONE" value. */
40
41 #define NV(e) {#e, e}
42 #define NV1(e, v) {#e, (v)}
43 #define NVEND {NULL, 0}         /* sentinel to mark end of list */
44
45 const NameValue setopt_nv_CURLPROXY[] = {
46   NV(CURLPROXY_HTTP),
47   NV(CURLPROXY_HTTP_1_0),
48   NV(CURLPROXY_SOCKS4),
49   NV(CURLPROXY_SOCKS5),
50   NV(CURLPROXY_SOCKS4A),
51   NV(CURLPROXY_SOCKS5_HOSTNAME),
52   NVEND,
53 };
54
55 const NameValueUnsigned setopt_nv_CURLAUTH[] = {
56   NV(CURLAUTH_ANY),             /* combination */
57   NV(CURLAUTH_ANYSAFE),         /* combination */
58   NV(CURLAUTH_BASIC),
59   NV(CURLAUTH_DIGEST),
60   NV(CURLAUTH_GSSNEGOTIATE),
61   NV(CURLAUTH_NTLM),
62   NV(CURLAUTH_DIGEST_IE),
63   NV(CURLAUTH_NTLM_WB),
64   NV(CURLAUTH_ONLY),
65   NV(CURLAUTH_NONE),
66   NVEND,
67 };
68
69 const NameValue setopt_nv_CURL_HTTP_VERSION[] = {
70   NV(CURL_HTTP_VERSION_NONE),
71   NV(CURL_HTTP_VERSION_1_0),
72   NV(CURL_HTTP_VERSION_1_1),
73   NV(CURL_HTTP_VERSION_2_0),
74   NV(CURL_HTTP_VERSION_2TLS),
75   NVEND,
76 };
77
78 const NameValue setopt_nv_CURL_SSLVERSION[] = {
79   NV(CURL_SSLVERSION_DEFAULT),
80   NV(CURL_SSLVERSION_TLSv1),
81   NV(CURL_SSLVERSION_SSLv2),
82   NV(CURL_SSLVERSION_SSLv3),
83   NV(CURL_SSLVERSION_TLSv1_0),
84   NV(CURL_SSLVERSION_TLSv1_1),
85   NV(CURL_SSLVERSION_TLSv1_2),
86   NVEND,
87 };
88
89 const NameValue setopt_nv_CURL_TIMECOND[] = {
90   NV(CURL_TIMECOND_IFMODSINCE),
91   NV(CURL_TIMECOND_IFUNMODSINCE),
92   NV(CURL_TIMECOND_LASTMOD),
93   NV(CURL_TIMECOND_NONE),
94   NVEND,
95 };
96
97 const NameValue setopt_nv_CURLFTPSSL_CCC[] = {
98   NV(CURLFTPSSL_CCC_NONE),
99   NV(CURLFTPSSL_CCC_PASSIVE),
100   NV(CURLFTPSSL_CCC_ACTIVE),
101   NVEND,
102 };
103
104 const NameValue setopt_nv_CURLUSESSL[] = {
105   NV(CURLUSESSL_NONE),
106   NV(CURLUSESSL_TRY),
107   NV(CURLUSESSL_CONTROL),
108   NV(CURLUSESSL_ALL),
109   NVEND,
110 };
111
112 const NameValueUnsigned setopt_nv_CURLSSLOPT[] = {
113   NV(CURLSSLOPT_ALLOW_BEAST),
114   NV(CURLSSLOPT_NO_REVOKE),
115   NVEND,
116 };
117
118 const NameValue setopt_nv_CURL_NETRC[] = {
119   NV(CURL_NETRC_IGNORED),
120   NV(CURL_NETRC_OPTIONAL),
121   NV(CURL_NETRC_REQUIRED),
122   NVEND,
123 };
124
125 /* These mappings essentially triplicated - see
126  * tool_libinfo.c and tool_paramhlp.c */
127 const NameValue setopt_nv_CURLPROTO[] = {
128   NV(CURLPROTO_ALL),            /* combination */
129   NV(CURLPROTO_DICT),
130   NV(CURLPROTO_FILE),
131   NV(CURLPROTO_FTP),
132   NV(CURLPROTO_FTPS),
133   NV(CURLPROTO_GOPHER),
134   NV(CURLPROTO_HTTP),
135   NV(CURLPROTO_HTTPS),
136   NV(CURLPROTO_IMAP),
137   NV(CURLPROTO_IMAPS),
138   NV(CURLPROTO_LDAP),
139   NV(CURLPROTO_LDAPS),
140   NV(CURLPROTO_POP3),
141   NV(CURLPROTO_POP3S),
142   NV(CURLPROTO_RTSP),
143   NV(CURLPROTO_SCP),
144   NV(CURLPROTO_SFTP),
145   NV(CURLPROTO_SMB),
146   NV(CURLPROTO_SMBS),
147   NV(CURLPROTO_SMTP),
148   NV(CURLPROTO_SMTPS),
149   NV(CURLPROTO_TELNET),
150   NV(CURLPROTO_TFTP),
151   NVEND,
152 };
153
154 /* These options have non-zero default values. */
155 static const NameValue setopt_nv_CURLNONZERODEFAULTS[] = {
156   NV1(CURLOPT_SSL_VERIFYPEER, 1),
157   NV1(CURLOPT_SSL_VERIFYHOST, 1),
158   NV1(CURLOPT_SSL_ENABLE_NPN, 1),
159   NV1(CURLOPT_SSL_ENABLE_ALPN, 1),
160   NV1(CURLOPT_TCP_NODELAY, 1),
161   NVEND
162 };
163
164 /* Format and add code; jump to nomem on malloc error */
165 #define ADD(args) do { \
166   ret = easysrc_add args; \
167   if(ret) \
168     goto nomem; \
169 } WHILE_FALSE
170 #define ADDF(args) do { \
171   ret = easysrc_addf args; \
172   if(ret) \
173     goto nomem; \
174 } WHILE_FALSE
175
176 #define DECL0(s) ADD((&easysrc_decl, s))
177 #define DECL1(f,a) ADDF((&easysrc_decl, f,a))
178
179 #define DATA0(s) ADD((&easysrc_data, s))
180 #define DATA1(f,a) ADDF((&easysrc_data, f,a))
181 #define DATA2(f,a,b) ADDF((&easysrc_data, f,a,b))
182 #define DATA3(f,a,b,c) ADDF((&easysrc_data, f,a,b,c))
183
184 #define CODE0(s) ADD((&easysrc_code, s))
185 #define CODE1(f,a) ADDF((&easysrc_code, f,a))
186 #define CODE2(f,a,b) ADDF((&easysrc_code, f,a,b))
187 #define CODE3(f,a,b,c) ADDF((&easysrc_code, f,a,b,c))
188
189 #define CLEAN0(s) ADD((&easysrc_clean, s))
190 #define CLEAN1(f,a) ADDF((&easysrc_clean, f,a))
191
192 #define REM0(s) ADD((&easysrc_toohard, s))
193 #define REM1(f,a) ADDF((&easysrc_toohard, f,a))
194 #define REM2(f,a,b) ADDF((&easysrc_toohard, f,a,b))
195
196 /* Escape string to C string syntax.  Return NULL if out of memory.
197  * Is this correct for those wacky EBCDIC guys? */
198 static char *c_escape(const char *str)
199 {
200   size_t len = 0;
201   const char *s;
202   unsigned char c;
203   char *escaped, *e;
204   /* Allocate space based on worst-case */
205   len = strlen(str);
206   escaped = malloc(4 * len + 1);
207   if(!escaped)
208     return NULL;
209
210   e = escaped;
211   for(s=str; (c=*s) != '\0'; s++) {
212     if(c=='\n') {
213       strcpy(e, "\\n");
214       e += 2;
215     }
216     else if(c=='\r') {
217       strcpy(e, "\\r");
218       e += 2;
219     }
220     else if(c=='\t') {
221       strcpy(e, "\\t");
222       e += 2;
223     }
224     else if(c=='\\') {
225       strcpy(e, "\\\\");
226       e += 2;
227     }
228     else if(c=='"') {
229       strcpy(e, "\\\"");
230       e += 2;
231     }
232     else if(! isprint(c)) {
233       snprintf(e, 5, "\\%03o", (unsigned)c);
234       e += 4;
235     }
236     else
237       *e++ = c;
238   }
239   *e = '\0';
240   return escaped;
241 }
242
243 /* setopt wrapper for enum types */
244 CURLcode tool_setopt_enum(CURL *curl, struct GlobalConfig *config,
245                           const char *name, CURLoption tag,
246                           const NameValue *nvlist, long lval)
247 {
248   CURLcode ret = CURLE_OK;
249   bool skip = FALSE;
250
251   ret = curl_easy_setopt(curl, tag, lval);
252   if(!lval)
253     skip = TRUE;
254
255   if(config->libcurl && !skip && !ret) {
256     /* we only use this for real if --libcurl was used */
257     const NameValue *nv = NULL;
258     for(nv=nvlist; nv->name; nv++) {
259       if(nv->value == lval) break; /* found it */
260     }
261     if(! nv->name) {
262       /* If no definition was found, output an explicit value.
263        * This could happen if new values are defined and used
264        * but the NameValue list is not updated. */
265       CODE2("curl_easy_setopt(hnd, %s, %ldL);", name, lval);
266     }
267     else {
268       CODE2("curl_easy_setopt(hnd, %s, (long)%s);", name, nv->name);
269     }
270   }
271
272  nomem:
273   return ret;
274 }
275
276 /* setopt wrapper for flags */
277 CURLcode tool_setopt_flags(CURL *curl, struct GlobalConfig *config,
278                            const char *name, CURLoption tag,
279                            const NameValue *nvlist, long lval)
280 {
281   CURLcode ret = CURLE_OK;
282   bool skip = FALSE;
283
284   ret = curl_easy_setopt(curl, tag, lval);
285   if(!lval)
286     skip = TRUE;
287
288   if(config->libcurl && !skip && !ret) {
289     /* we only use this for real if --libcurl was used */
290     char preamble[80];          /* should accommodate any symbol name */
291     long rest = lval;           /* bits not handled yet */
292     const NameValue *nv = NULL;
293     snprintf(preamble, sizeof(preamble),
294              "curl_easy_setopt(hnd, %s, ", name);
295     for(nv=nvlist; nv->name; nv++) {
296       if((nv->value & ~ rest) == 0) {
297         /* all value flags contained in rest */
298         rest &= ~ nv->value;    /* remove bits handled here */
299         CODE3("%s(long)%s%s",
300               preamble, nv->name, rest ? " |" : ");");
301         if(!rest)
302           break;                /* handled them all */
303         /* replace with all spaces for continuation line */
304         snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
305       }
306     }
307     /* If any bits have no definition, output an explicit value.
308      * This could happen if new bits are defined and used
309      * but the NameValue list is not updated. */
310     if(rest)
311       CODE2("%s%ldL);", preamble, rest);
312   }
313
314  nomem:
315   return ret;
316 }
317
318 /* setopt wrapper for bitmasks */
319 CURLcode tool_setopt_bitmask(CURL *curl, struct GlobalConfig *config,
320                              const char *name, CURLoption tag,
321                              const NameValueUnsigned *nvlist,
322                              long lval)
323 {
324   CURLcode ret = CURLE_OK;
325   bool skip = FALSE;
326
327   ret = curl_easy_setopt(curl, tag, lval);
328   if(!lval)
329     skip = TRUE;
330
331   if(config->libcurl && !skip && !ret) {
332     /* we only use this for real if --libcurl was used */
333     char preamble[80];
334     unsigned long rest = (unsigned long)lval;
335     const NameValueUnsigned *nv = NULL;
336     snprintf(preamble, sizeof(preamble),
337              "curl_easy_setopt(hnd, %s, ", name);
338     for(nv=nvlist; nv->name; nv++) {
339       if((nv->value & ~ rest) == 0) {
340         /* all value flags contained in rest */
341         rest &= ~ nv->value;    /* remove bits handled here */
342         CODE3("%s(long)%s%s",
343               preamble, nv->name, rest ? " |" : ");");
344         if(!rest)
345           break;                /* handled them all */
346         /* replace with all spaces for continuation line */
347         snprintf(preamble, sizeof(preamble), "%*s", strlen(preamble), "");
348       }
349     }
350     /* If any bits have no definition, output an explicit value.
351      * This could happen if new bits are defined and used
352      * but the NameValue list is not updated. */
353     if(rest)
354       CODE2("%s%luUL);", preamble, rest);
355   }
356
357  nomem:
358   return ret;
359 }
360
361 /* setopt wrapper for CURLOPT_HTTPPOST */
362 CURLcode tool_setopt_httppost(CURL *curl, struct GlobalConfig *config,
363                               const char *name, CURLoption tag,
364                               struct curl_httppost *post)
365 {
366   CURLcode ret = CURLE_OK;
367   char *escaped = NULL;
368   bool skip = FALSE;
369
370   ret = curl_easy_setopt(curl, tag, post);
371   if(!post)
372     skip = TRUE;
373
374   if(config->libcurl && !skip && !ret) {
375     struct curl_httppost *pp, *p;
376     int i;
377     /* May use several httppost lists, if multiple POST actions */
378     i = ++ easysrc_form_count;
379     DECL1("struct curl_httppost *post%d;", i);
380     DATA1("post%d = NULL;", i);
381     CLEAN1("curl_formfree(post%d);", i);
382     CLEAN1("post%d = NULL;", i);
383     if(i == 1)
384       DECL0("struct curl_httppost *postend;");
385     DATA0("postend = NULL;");
386     for(p=post; p; p=p->next) {
387       DATA1("curl_formadd(&post%d, &postend,", i);
388       DATA1("             CURLFORM_COPYNAME, \"%s\",", p->name);
389       for(pp=p; pp; pp=pp->more) {
390         /* May be several files uploaded for one name;
391          * these are linked through the 'more' pointer */
392         Curl_safefree(escaped);
393         escaped = c_escape(pp->contents);
394         if(!escaped) {
395           ret = CURLE_OUT_OF_MEMORY;
396           goto nomem;
397         }
398         if(pp->flags & CURL_HTTPPOST_FILENAME) {
399           /* file upload as for -F @filename */
400           DATA1("             CURLFORM_FILE, \"%s\",", escaped);
401         }
402         else if(pp->flags & CURL_HTTPPOST_READFILE) {
403           /* content from file as for -F <filename */
404           DATA1("             CURLFORM_FILECONTENT, \"%s\",", escaped);
405         }
406         else
407           DATA1("             CURLFORM_COPYCONTENTS, \"%s\",", escaped);
408         if(pp->showfilename) {
409           Curl_safefree(escaped);
410           escaped = c_escape(pp->showfilename);
411           if(!escaped) {
412             ret = CURLE_OUT_OF_MEMORY;
413             goto nomem;
414           }
415           DATA1("             CURLFORM_FILENAME, \"%s\",", escaped);
416         }
417         if(pp->contenttype) {
418           Curl_safefree(escaped);
419           escaped = c_escape(pp->contenttype);
420           if(!escaped) {
421             ret = CURLE_OUT_OF_MEMORY;
422             goto nomem;
423           }
424           DATA1("             CURLFORM_CONTENTTYPE, \"%s\",", escaped);
425         }
426       }
427       DATA0("             CURLFORM_END);");
428     }
429     CODE2("curl_easy_setopt(hnd, %s, post%d);", name, i);
430   }
431
432  nomem:
433   Curl_safefree(escaped);
434   return ret;
435 }
436
437 /* setopt wrapper for curl_slist options */
438 CURLcode tool_setopt_slist(CURL *curl, struct GlobalConfig *config,
439                            const char *name, CURLoption tag,
440                            struct curl_slist *list)
441 {
442   CURLcode ret = CURLE_OK;
443   char *escaped = NULL;
444   bool skip = FALSE;
445
446   ret = curl_easy_setopt(curl, tag, list);
447   if(!list)
448     skip = TRUE;
449
450   if(config->libcurl && !skip && !ret) {
451     struct curl_slist *s;
452     int i;
453     /* May need several slist variables, so invent name */
454     i = ++ easysrc_slist_count;
455     DECL1("struct curl_slist *slist%d;", i);
456     DATA1("slist%d = NULL;", i);
457     CLEAN1("curl_slist_free_all(slist%d);", i);
458     CLEAN1("slist%d = NULL;", i);
459     for(s=list; s; s=s->next) {
460       Curl_safefree(escaped);
461       escaped = c_escape(s->data);
462       if(!escaped) {
463         ret = CURLE_OUT_OF_MEMORY;
464         goto nomem;
465       }
466       DATA3("slist%d = curl_slist_append(slist%d, \"%s\");", i, i, escaped);
467     }
468     CODE2("curl_easy_setopt(hnd, %s, slist%d);", name, i);
469   }
470
471  nomem:
472   Curl_safefree(escaped);
473   return ret;
474 }
475
476 /* generic setopt wrapper for all other options.
477  * Some type information is encoded in the tag value. */
478 CURLcode tool_setopt(CURL *curl, bool str, struct GlobalConfig *config,
479                      const char *name, CURLoption tag, ...)
480 {
481   va_list arg;
482   char buf[256];
483   const char *value = NULL;
484   bool remark = FALSE;
485   bool skip = FALSE;
486   bool escape = FALSE;
487   char *escaped = NULL;
488   CURLcode ret = CURLE_OK;
489
490   va_start(arg, tag);
491
492   if(tag < CURLOPTTYPE_OBJECTPOINT) {
493     /* Value is expected to be a long */
494     long lval = va_arg(arg, long);
495     long defval = 0L;
496     const NameValue *nv = NULL;
497     for(nv=setopt_nv_CURLNONZERODEFAULTS; nv->name; nv++) {
498       if(!strcmp(name, nv->name)) {
499         defval = nv->value;
500         break; /* found it */
501       }
502     }
503
504     snprintf(buf, sizeof(buf), "%ldL", lval);
505     value = buf;
506     ret = curl_easy_setopt(curl, tag, lval);
507     if(lval == defval)
508       skip = TRUE;
509   }
510   else if(tag < CURLOPTTYPE_OFF_T) {
511     /* Value is some sort of object pointer */
512     void *pval = va_arg(arg, void *);
513
514     /* function pointers are never printable */
515     if(tag >= CURLOPTTYPE_FUNCTIONPOINT) {
516       if(pval) {
517         value = "functionpointer";
518         remark = TRUE;
519       }
520       else
521         skip = TRUE;
522     }
523
524     else if(pval && str) {
525       value = (char *)pval;
526       escape = TRUE;
527     }
528     else if(pval) {
529       value = "objectpointer";
530       remark = TRUE;
531     }
532     else
533       skip = TRUE;
534
535     ret = curl_easy_setopt(curl, tag, pval);
536
537   }
538   else {
539     /* Value is expected to be curl_off_t */
540     curl_off_t oval = va_arg(arg, curl_off_t);
541     snprintf(buf, sizeof(buf),
542              "(curl_off_t)%" CURL_FORMAT_CURL_OFF_T, oval);
543     value = buf;
544     ret = curl_easy_setopt(curl, tag, oval);
545
546     if(!oval)
547       skip = TRUE;
548   }
549
550   va_end(arg);
551
552   if(config->libcurl && !skip && !ret) {
553     /* we only use this for real if --libcurl was used */
554
555     if(remark)
556       REM2("%s set to a %s", name, value);
557     else {
558       if(escape) {
559         escaped = c_escape(value);
560         if(!escaped) {
561           ret = CURLE_OUT_OF_MEMORY;
562           goto nomem;
563         }
564         CODE2("curl_easy_setopt(hnd, %s, \"%s\");", name, escaped);
565       }
566       else
567         CODE2("curl_easy_setopt(hnd, %s, %s);", name, value);
568     }
569   }
570
571  nomem:
572   Curl_safefree(escaped);
573   return ret;
574 }
575
576 #endif /* CURL_DISABLE_LIBCURL_OPTION */