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