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