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 #ifdef HAVE_FCNTL_H
25 # include <fcntl.h>
26 #endif
27
28 #ifdef HAVE_LOCALE_H
29 # include <locale.h>
30 #endif
31
32 #ifdef HAVE_SYS_SELECT_H
33 # include <sys/select.h>
34 #elif defined(HAVE_UNISTD_H)
35 # include <unistd.h>
36 #endif
37
38 #ifdef __VMS
39 # include <fabdef.h>
40 #endif
41
42 #ifdef __AMIGA__
43 # include <proto/dos.h>
44 #endif
45
46 #include "strcase.h"
47
48 #define ENABLE_CURLX_PRINTF
49 /* use our own printf() functions */
50 #include "curlx.h"
51
52 #include "tool_binmode.h"
53 #include "tool_cfgable.h"
54 #include "tool_cb_dbg.h"
55 #include "tool_cb_hdr.h"
56 #include "tool_cb_prg.h"
57 #include "tool_cb_rea.h"
58 #include "tool_cb_see.h"
59 #include "tool_cb_wrt.h"
60 #include "tool_dirhie.h"
61 #include "tool_doswin.h"
62 #include "tool_easysrc.h"
63 #include "tool_filetime.h"
64 #include "tool_getparam.h"
65 #include "tool_helpers.h"
66 #include "tool_homedir.h"
67 #include "tool_libinfo.h"
68 #include "tool_main.h"
69 #include "tool_metalink.h"
70 #include "tool_msgs.h"
71 #include "tool_operate.h"
72 #include "tool_operhlp.h"
73 #include "tool_paramhlp.h"
74 #include "tool_parsecfg.h"
75 #include "tool_setopt.h"
76 #include "tool_sleep.h"
77 #include "tool_urlglob.h"
78 #include "tool_util.h"
79 #include "tool_writeout.h"
80 #include "tool_xattr.h"
81 #include "tool_vms.h"
82 #include "tool_help.h"
83 #include "tool_hugehelp.h"
84 #include "tool_progress.h"
85
86 #include "memdebug.h" /* keep this as LAST include */
87
88 #ifdef CURLDEBUG
89 /* libcurl's debug builds provide an extra function */
90 CURLcode curl_easy_perform_ev(CURL *easy);
91 #endif
92
93 #define CURLseparator "--_curl_--"
94
95 #ifndef O_BINARY
96 /* since O_BINARY as used in bitmasks, setting it to zero makes it usable in
97 source code but yet it doesn't ruin anything */
98 # define O_BINARY 0
99 #endif
100
101 #define CURL_CA_CERT_ERRORMSG \
102 "More details here: https://curl.haxx.se/docs/sslcerts.html\n\n" \
103 "curl failed to verify the legitimacy of the server and therefore " \
104 "could not\nestablish a secure connection to it. To learn more about " \
105 "this situation and\nhow to fix it, please visit the web page mentioned " \
106 "above.\n"
107
108 static CURLcode single_transfer(struct GlobalConfig *global,
109 struct OperationConfig *config,
110 CURLSH *share,
111 bool capath_from_env,
112 bool *added);
113 static CURLcode create_transfer(struct GlobalConfig *global,
114 CURLSH *share,
115 bool *added);
116
is_fatal_error(CURLcode code)117 static bool is_fatal_error(CURLcode code)
118 {
119 switch(code) {
120 case CURLE_FAILED_INIT:
121 case CURLE_OUT_OF_MEMORY:
122 case CURLE_UNKNOWN_OPTION:
123 case CURLE_FUNCTION_NOT_FOUND:
124 case CURLE_BAD_FUNCTION_ARGUMENT:
125 /* critical error */
126 return TRUE;
127 default:
128 break;
129 }
130
131 /* no error or not critical */
132 return FALSE;
133 }
134
135 /*
136 * Check if a given string is a PKCS#11 URI
137 */
is_pkcs11_uri(const char * string)138 static bool is_pkcs11_uri(const char *string)
139 {
140 if(curl_strnequal(string, "pkcs11:", 7)) {
141 return TRUE;
142 }
143 else {
144 return FALSE;
145 }
146 }
147
148 #ifdef __VMS
149 /*
150 * get_vms_file_size does what it takes to get the real size of the file
151 *
152 * For fixed files, find out the size of the EOF block and adjust.
153 *
154 * For all others, have to read the entire file in, discarding the contents.
155 * Most posted text files will be small, and binary files like zlib archives
156 * and CD/DVD images should be either a STREAM_LF format or a fixed format.
157 *
158 */
vms_realfilesize(const char * name,const struct_stat * stat_buf)159 static curl_off_t vms_realfilesize(const char *name,
160 const struct_stat *stat_buf)
161 {
162 char buffer[8192];
163 curl_off_t count;
164 int ret_stat;
165 FILE * file;
166
167 /* !checksrc! disable FOPENMODE 1 */
168 file = fopen(name, "r"); /* VMS */
169 if(file == NULL) {
170 return 0;
171 }
172 count = 0;
173 ret_stat = 1;
174 while(ret_stat > 0) {
175 ret_stat = fread(buffer, 1, sizeof(buffer), file);
176 if(ret_stat != 0)
177 count += ret_stat;
178 }
179 fclose(file);
180
181 return count;
182 }
183
184 /*
185 *
186 * VmsSpecialSize checks to see if the stat st_size can be trusted and
187 * if not to call a routine to get the correct size.
188 *
189 */
VmsSpecialSize(const char * name,const struct_stat * stat_buf)190 static curl_off_t VmsSpecialSize(const char *name,
191 const struct_stat *stat_buf)
192 {
193 switch(stat_buf->st_fab_rfm) {
194 case FAB$C_VAR:
195 case FAB$C_VFC:
196 return vms_realfilesize(name, stat_buf);
197 break;
198 default:
199 return stat_buf->st_size;
200 }
201 }
202 #endif /* __VMS */
203
204 #define BUFFER_SIZE (100*1024)
205
206 struct per_transfer *transfers; /* first node */
207 static struct per_transfer *transfersl; /* last node */
208
209 /* add_per_transfer creates a new 'per_transfer' node in the linked
210 list of transfers */
add_per_transfer(struct per_transfer ** per)211 static CURLcode add_per_transfer(struct per_transfer **per)
212 {
213 struct per_transfer *p;
214 p = calloc(sizeof(struct per_transfer), 1);
215 if(!p)
216 return CURLE_OUT_OF_MEMORY;
217 if(!transfers)
218 /* first entry */
219 transfersl = transfers = p;
220 else {
221 /* make the last node point to the new node */
222 transfersl->next = p;
223 /* make the new node point back to the formerly last node */
224 p->prev = transfersl;
225 /* move the last node pointer to the new entry */
226 transfersl = p;
227 }
228 *per = p;
229 all_xfers++; /* count total number of transfers added */
230 return CURLE_OK;
231 }
232
233 /* Remove the specified transfer from the list (and free it), return the next
234 in line */
del_per_transfer(struct per_transfer * per)235 static struct per_transfer *del_per_transfer(struct per_transfer *per)
236 {
237 struct per_transfer *n;
238 struct per_transfer *p;
239 DEBUGASSERT(transfers);
240 DEBUGASSERT(transfersl);
241 DEBUGASSERT(per);
242
243 n = per->next;
244 p = per->prev;
245
246 if(p)
247 p->next = n;
248 else
249 transfers = n;
250
251 if(n)
252 n->prev = p;
253 else
254 transfersl = p;
255
256 free(per);
257
258 return n;
259 }
260
pre_transfer(struct GlobalConfig * global,struct per_transfer * per)261 static CURLcode pre_transfer(struct GlobalConfig *global,
262 struct per_transfer *per)
263 {
264 curl_off_t uploadfilesize = -1;
265 struct_stat fileinfo;
266 CURLcode result = CURLE_OK;
267
268 if(per->separator_err)
269 fprintf(global->errors, "%s\n", per->separator_err);
270 if(per->separator)
271 printf("%s\n", per->separator);
272
273 if(per->uploadfile && !stdin_upload(per->uploadfile)) {
274 /* VMS Note:
275 *
276 * Reading binary from files can be a problem... Only FIXED, VAR
277 * etc WITHOUT implied CC will work Others need a \n appended to a
278 * line
279 *
280 * - Stat gives a size but this is UNRELIABLE in VMS As a f.e. a
281 * fixed file with implied CC needs to have a byte added for every
282 * record processed, this can by derived from Filesize & recordsize
283 * for VARiable record files the records need to be counted! for
284 * every record add 1 for linefeed and subtract 2 for the record
285 * header for VARIABLE header files only the bare record data needs
286 * to be considered with one appended if implied CC
287 */
288 #ifdef __VMS
289 /* Calculate the real upload size for VMS */
290 per->infd = -1;
291 if(stat(per->uploadfile, &fileinfo) == 0) {
292 fileinfo.st_size = VmsSpecialSize(uploadfile, &fileinfo);
293 switch(fileinfo.st_fab_rfm) {
294 case FAB$C_VAR:
295 case FAB$C_VFC:
296 case FAB$C_STMCR:
297 per->infd = open(per->uploadfile, O_RDONLY | O_BINARY);
298 break;
299 default:
300 per->infd = open(per->uploadfile, O_RDONLY | O_BINARY,
301 "rfm=stmlf", "ctx=stm");
302 }
303 }
304 if(per->infd == -1)
305 #else
306 per->infd = open(per->uploadfile, O_RDONLY | O_BINARY);
307 if((per->infd == -1) || fstat(per->infd, &fileinfo))
308 #endif
309 {
310 helpf(global->errors, "Can't open '%s'!\n", per->uploadfile);
311 if(per->infd != -1) {
312 close(per->infd);
313 per->infd = STDIN_FILENO;
314 }
315 return CURLE_READ_ERROR;
316 }
317 per->infdopen = TRUE;
318
319 /* we ignore file size for char/block devices, sockets, etc. */
320 if(S_ISREG(fileinfo.st_mode))
321 uploadfilesize = fileinfo.st_size;
322
323 if(uploadfilesize != -1) {
324 struct OperationConfig *config = per->config; /* for the macro below */
325 my_setopt(per->curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
326 }
327 per->input.fd = per->infd;
328 }
329 return result;
330 }
331
332 /*
333 * Call this after a transfer has completed.
334 */
post_per_transfer(struct GlobalConfig * global,struct per_transfer * per,CURLcode result,bool * retryp,long * delay)335 static CURLcode post_per_transfer(struct GlobalConfig *global,
336 struct per_transfer *per,
337 CURLcode result,
338 bool *retryp,
339 long *delay) /* milliseconds! */
340 {
341 struct OutStruct *outs = &per->outs;
342 CURL *curl = per->curl;
343 struct OperationConfig *config = per->config;
344
345 if(!curl || !config)
346 return result;
347
348 *retryp = FALSE;
349 *delay = 0; /* for no retry, keep it zero */
350
351 if(per->infdopen)
352 close(per->infd);
353
354 #ifdef __VMS
355 if(is_vms_shell()) {
356 /* VMS DCL shell behavior */
357 if(!global->showerror)
358 vms_show = VMSSTS_HIDE;
359 }
360 else
361 #endif
362 if(config->synthetic_error) {
363 ;
364 }
365 else if(result && global->showerror) {
366 fprintf(global->errors, "curl: (%d) %s\n", result,
367 (per->errorbuffer[0]) ? per->errorbuffer :
368 curl_easy_strerror(result));
369 if(result == CURLE_PEER_FAILED_VERIFICATION)
370 fputs(CURL_CA_CERT_ERRORMSG, global->errors);
371 }
372
373 /* Set file extended attributes */
374 if(!result && config->xattr && outs->fopened && outs->stream) {
375 int rc = fwrite_xattr(curl, fileno(outs->stream));
376 if(rc)
377 warnf(config->global, "Error setting extended attributes: %s\n",
378 strerror(errno));
379 }
380
381 if(!result && !outs->stream && !outs->bytes) {
382 /* we have received no data despite the transfer was successful
383 ==> force cration of an empty output file (if an output file
384 was specified) */
385 long cond_unmet = 0L;
386 /* do not create (or even overwrite) the file in case we get no
387 data because of unmet condition */
388 curl_easy_getinfo(curl, CURLINFO_CONDITION_UNMET, &cond_unmet);
389 if(!cond_unmet && !tool_create_output_file(outs, config))
390 result = CURLE_WRITE_ERROR;
391 }
392
393 if(!outs->s_isreg && outs->stream) {
394 /* Dump standard stream buffered data */
395 int rc = fflush(outs->stream);
396 if(!result && rc) {
397 /* something went wrong in the writing process */
398 result = CURLE_WRITE_ERROR;
399 fprintf(global->errors, "(%d) Failed writing body\n", result);
400 }
401 }
402
403 #ifdef USE_METALINK
404 if(per->metalink && !per->metalink_next_res)
405 fprintf(global->errors, "Metalink: fetching (%s) from (%s) OK\n",
406 per->mlfile->filename, per->this_url);
407
408 if(!per->metalink && config->use_metalink && result == CURLE_OK) {
409 int rv = parse_metalink(config, outs, per->this_url);
410 if(!rv) {
411 fprintf(config->global->errors, "Metalink: parsing (%s) OK\n",
412 per->this_url);
413 }
414 else if(rv == -1)
415 fprintf(config->global->errors, "Metalink: parsing (%s) FAILED\n",
416 per->this_url);
417 }
418 else if(per->metalink && result == CURLE_OK && !per->metalink_next_res) {
419 int rv;
420 (void)fflush(outs->stream);
421 rv = metalink_check_hash(global, per->mlfile, outs->filename);
422 if(!rv)
423 per->metalink_next_res = 1;
424 }
425 #endif /* USE_METALINK */
426
427 #ifdef USE_METALINK
428 if(outs->metalink_parser)
429 metalink_parser_context_delete(outs->metalink_parser);
430 #endif /* USE_METALINK */
431
432 /* if retry-max-time is non-zero, make sure we haven't exceeded the
433 time */
434 if(per->retry_numretries &&
435 (!config->retry_maxtime ||
436 (tvdiff(tvnow(), per->retrystart) <
437 config->retry_maxtime*1000L)) ) {
438 enum {
439 RETRY_NO,
440 RETRY_ALL_ERRORS,
441 RETRY_TIMEOUT,
442 RETRY_CONNREFUSED,
443 RETRY_HTTP,
444 RETRY_FTP,
445 RETRY_LAST /* not used */
446 } retry = RETRY_NO;
447 long response = 0;
448 if((CURLE_OPERATION_TIMEDOUT == result) ||
449 (CURLE_COULDNT_RESOLVE_HOST == result) ||
450 (CURLE_COULDNT_RESOLVE_PROXY == result) ||
451 (CURLE_FTP_ACCEPT_TIMEOUT == result))
452 /* retry timeout always */
453 retry = RETRY_TIMEOUT;
454 else if(config->retry_connrefused &&
455 (CURLE_COULDNT_CONNECT == result)) {
456 long oserrno = 0;
457 curl_easy_getinfo(curl, CURLINFO_OS_ERRNO, &oserrno);
458 if(ECONNREFUSED == oserrno)
459 retry = RETRY_CONNREFUSED;
460 }
461 else if((CURLE_OK == result) ||
462 (config->failonerror &&
463 (CURLE_HTTP_RETURNED_ERROR == result))) {
464 /* If it returned OK. _or_ failonerror was enabled and it
465 returned due to such an error, check for HTTP transient
466 errors to retry on. */
467 long protocol = 0;
468 curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
469 if((protocol == CURLPROTO_HTTP) || (protocol == CURLPROTO_HTTPS)) {
470 /* This was HTTP(S) */
471 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
472
473 switch(response) {
474 case 429: /* Too Many Requests (RFC6585) */
475 case 500: /* Internal Server Error */
476 case 502: /* Bad Gateway */
477 case 503: /* Service Unavailable */
478 case 504: /* Gateway Timeout */
479 retry = RETRY_HTTP;
480 /*
481 * At this point, we have already written data to the output
482 * file (or terminal). If we write to a file, we must rewind
483 * or close/re-open the file so that the next attempt starts
484 * over from the beginning.
485 *
486 * TODO: similar action for the upload case. We might need
487 * to start over reading from a previous point if we have
488 * uploaded something when this was returned.
489 */
490 break;
491 }
492 }
493 } /* if CURLE_OK */
494 else if(result) {
495 long protocol = 0;
496
497 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
498 curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
499
500 if((protocol == CURLPROTO_FTP || protocol == CURLPROTO_FTPS) &&
501 response / 100 == 4)
502 /*
503 * This is typically when the FTP server only allows a certain
504 * amount of users and we are not one of them. All 4xx codes
505 * are transient.
506 */
507 retry = RETRY_FTP;
508 }
509
510 if(result && !retry && config->retry_all_errors)
511 retry = RETRY_ALL_ERRORS;
512
513 if(retry) {
514 long sleeptime = 0;
515 curl_off_t retry_after = 0;
516 static const char * const m[]={
517 NULL,
518 "(retrying all errors)",
519 ": timeout",
520 ": connection refused",
521 ": HTTP error",
522 ": FTP error"
523 };
524
525 sleeptime = per->retry_sleep;
526 if(RETRY_HTTP == retry) {
527 curl_easy_getinfo(curl, CURLINFO_RETRY_AFTER, &retry_after);
528 if(retry_after) {
529 /* store in a 'long', make sure it doesn't overflow */
530 if(retry_after > LONG_MAX/1000)
531 sleeptime = LONG_MAX;
532 else
533 sleeptime = (long)retry_after * 1000; /* milliseconds */
534 }
535 }
536 warnf(config->global, "Problem %s. "
537 "Will retry in %ld seconds. "
538 "%ld retries left.\n",
539 m[retry], sleeptime/1000L, per->retry_numretries);
540
541 per->retry_numretries--;
542 if(!config->retry_delay) {
543 per->retry_sleep *= 2;
544 if(per->retry_sleep > RETRY_SLEEP_MAX)
545 per->retry_sleep = RETRY_SLEEP_MAX;
546 }
547 if(outs->bytes && outs->filename && outs->stream) {
548 int rc;
549 /* We have written data to a output file, we truncate file
550 */
551 if(!global->mute)
552 fprintf(global->errors, "Throwing away %"
553 CURL_FORMAT_CURL_OFF_T " bytes\n",
554 outs->bytes);
555 fflush(outs->stream);
556 /* truncate file at the position where we started appending */
557 #ifdef HAVE_FTRUNCATE
558 if(ftruncate(fileno(outs->stream), outs->init)) {
559 /* when truncate fails, we can't just append as then we'll
560 create something strange, bail out */
561 if(!global->mute)
562 fprintf(global->errors,
563 "failed to truncate, exiting\n");
564 return CURLE_WRITE_ERROR;
565 }
566 /* now seek to the end of the file, the position where we
567 just truncated the file in a large file-safe way */
568 rc = fseek(outs->stream, 0, SEEK_END);
569 #else
570 /* ftruncate is not available, so just reposition the file
571 to the location we would have truncated it. This won't
572 work properly with large files on 32-bit systems, but
573 most of those will have ftruncate. */
574 rc = fseek(outs->stream, (long)outs->init, SEEK_SET);
575 #endif
576 if(rc) {
577 if(!global->mute)
578 fprintf(global->errors,
579 "failed seeking to end of file, exiting\n");
580 return CURLE_WRITE_ERROR;
581 }
582 outs->bytes = 0; /* clear for next round */
583 }
584 *retryp = TRUE;
585 *delay = sleeptime;
586 return CURLE_OK;
587 }
588 } /* if retry_numretries */
589 else if(per->metalink) {
590 /* Metalink: Decide to try the next resource or not. Try the next resource
591 if download was not successful. */
592 long response = 0;
593 if(CURLE_OK == result) {
594 /* TODO We want to try next resource when download was
595 not successful. How to know that? */
596 char *effective_url = NULL;
597 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective_url);
598 if(effective_url &&
599 curl_strnequal(effective_url, "http", 4)) {
600 /* This was HTTP(S) */
601 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
602 if(response != 200 && response != 206) {
603 per->metalink_next_res = 1;
604 fprintf(global->errors,
605 "Metalink: fetching (%s) from (%s) FAILED "
606 "(HTTP status code %ld)\n",
607 per->mlfile->filename, per->this_url, response);
608 }
609 }
610 }
611 else {
612 per->metalink_next_res = 1;
613 fprintf(global->errors,
614 "Metalink: fetching (%s) from (%s) FAILED (%s)\n",
615 per->mlfile->filename, per->this_url,
616 curl_easy_strerror(result));
617 }
618 }
619
620 if((global->progressmode == CURL_PROGRESS_BAR) &&
621 per->progressbar.calls)
622 /* if the custom progress bar has been displayed, we output a
623 newline here */
624 fputs("\n", per->progressbar.out);
625
626 if(config->writeout)
627 ourWriteOut(per->curl, per, config->writeout);
628
629 /* Close the outs file */
630 if(outs->fopened && outs->stream) {
631 int rc = fclose(outs->stream);
632 if(!result && rc) {
633 /* something went wrong in the writing process */
634 result = CURLE_WRITE_ERROR;
635 fprintf(global->errors, "(%d) Failed writing body\n", result);
636 }
637 }
638
639 /* File time can only be set _after_ the file has been closed */
640 if(!result && config->remote_time && outs->s_isreg && outs->filename) {
641 /* Ask libcurl if we got a remote file time */
642 curl_off_t filetime = -1;
643 curl_easy_getinfo(curl, CURLINFO_FILETIME_T, &filetime);
644 setfiletime(filetime, outs->filename, config->global->errors);
645 }
646
647 /* Close function-local opened file descriptors */
648 if(per->heads.fopened && per->heads.stream)
649 fclose(per->heads.stream);
650
651 if(per->heads.alloc_filename)
652 Curl_safefree(per->heads.filename);
653
654 if(per->etag_save.fopened && per->etag_save.stream)
655 fclose(per->etag_save.stream);
656
657 if(per->etag_save.alloc_filename)
658 Curl_safefree(per->etag_save.filename);
659
660 curl_easy_cleanup(per->curl);
661 if(outs->alloc_filename)
662 free(outs->filename);
663 free(per->this_url);
664 free(per->separator_err);
665 free(per->separator);
666 free(per->outfile);
667 free(per->uploadfile);
668
669 return CURLE_OK;
670 }
671
single_transfer_cleanup(struct OperationConfig * config)672 static void single_transfer_cleanup(struct OperationConfig *config)
673 {
674 if(config) {
675 struct State *state = &config->state;
676 if(state->urls) {
677 /* Free list of remaining URLs */
678 glob_cleanup(state->urls);
679 state->urls = NULL;
680 }
681 Curl_safefree(state->outfiles);
682 Curl_safefree(state->httpgetfields);
683 Curl_safefree(state->uploadfile);
684 if(state->inglob) {
685 /* Free list of globbed upload files */
686 glob_cleanup(state->inglob);
687 state->inglob = NULL;
688 }
689 }
690 }
691
692 /* create the next (singular) transfer */
693
single_transfer(struct GlobalConfig * global,struct OperationConfig * config,CURLSH * share,bool capath_from_env,bool * added)694 static CURLcode single_transfer(struct GlobalConfig *global,
695 struct OperationConfig *config,
696 CURLSH *share,
697 bool capath_from_env,
698 bool *added)
699 {
700 CURLcode result = CURLE_OK;
701 struct getout *urlnode;
702 struct metalinkfile *mlfile_last = NULL;
703 bool orig_noprogress = global->noprogress;
704 bool orig_isatty = global->isatty;
705 struct State *state = &config->state;
706 char *httpgetfields = state->httpgetfields;
707 *added = FALSE; /* not yet */
708
709 if(config->postfields) {
710 if(config->use_httpget) {
711 if(!httpgetfields) {
712 /* Use the postfields data for a http get */
713 httpgetfields = state->httpgetfields = strdup(config->postfields);
714 Curl_safefree(config->postfields);
715 if(!httpgetfields) {
716 errorf(global, "out of memory\n");
717 result = CURLE_OUT_OF_MEMORY;
718 }
719 else if(SetHTTPrequest(config,
720 (config->no_body?HTTPREQ_HEAD:HTTPREQ_GET),
721 &config->httpreq)) {
722 result = CURLE_FAILED_INIT;
723 }
724 }
725 }
726 else {
727 if(SetHTTPrequest(config, HTTPREQ_SIMPLEPOST, &config->httpreq))
728 result = CURLE_FAILED_INIT;
729 }
730 if(result) {
731 single_transfer_cleanup(config);
732 return result;
733 }
734 }
735 if(!state->urlnode) {
736 /* first time caller, setup things */
737 state->urlnode = config->url_list;
738 state->infilenum = 1;
739 }
740
741 while(config->state.urlnode) {
742 char *infiles; /* might be a glob pattern */
743 struct URLGlob *inglob = state->inglob;
744 bool metalink = FALSE; /* metalink download? */
745 struct metalinkfile *mlfile;
746 struct metalink_resource *mlres;
747
748 urlnode = config->state.urlnode;
749 if(urlnode->flags & GETOUT_METALINK) {
750 metalink = 1;
751 if(mlfile_last == NULL) {
752 mlfile_last = config->metalinkfile_list;
753 }
754 mlfile = mlfile_last;
755 mlfile_last = mlfile_last->next;
756 mlres = mlfile->resource;
757 }
758 else {
759 mlfile = NULL;
760 mlres = NULL;
761 }
762
763 /* urlnode->url is the full URL (it might be NULL) */
764
765 if(!urlnode->url) {
766 /* This node has no URL. Free node data without destroying the
767 node itself nor modifying next pointer and continue to next */
768 Curl_safefree(urlnode->outfile);
769 Curl_safefree(urlnode->infile);
770 urlnode->flags = 0;
771 config->state.urlnode = urlnode->next;
772 state->up = 0;
773 continue; /* next URL please */
774 }
775
776 /* save outfile pattern before expansion */
777 if(urlnode->outfile && !state->outfiles) {
778 state->outfiles = strdup(urlnode->outfile);
779 if(!state->outfiles) {
780 errorf(global, "out of memory\n");
781 result = CURLE_OUT_OF_MEMORY;
782 break;
783 }
784 }
785
786 infiles = urlnode->infile;
787
788 if(!config->globoff && infiles && !inglob) {
789 /* Unless explicitly shut off */
790 result = glob_url(&inglob, infiles, &state->infilenum,
791 global->showerror?global->errors:NULL);
792 if(result)
793 break;
794 config->state.inglob = inglob;
795 }
796
797 {
798 int separator;
799 unsigned long urlnum;
800
801 if(!state->up && !infiles)
802 Curl_nop_stmt;
803 else {
804 if(!state->uploadfile) {
805 if(inglob) {
806 result = glob_next_url(&state->uploadfile, inglob);
807 if(result == CURLE_OUT_OF_MEMORY)
808 errorf(global, "out of memory\n");
809 }
810 else if(!state->up) {
811 state->uploadfile = strdup(infiles);
812 if(!state->uploadfile) {
813 errorf(global, "out of memory\n");
814 result = CURLE_OUT_OF_MEMORY;
815 }
816 }
817 }
818 if(result)
819 break;
820 }
821
822 if(!state->urlnum) {
823 if(metalink) {
824 /* For Metalink download, we don't use glob. Instead we use
825 the number of resources as urlnum. */
826 urlnum = count_next_metalink_resource(mlfile);
827 }
828 else if(!config->globoff) {
829 /* Unless explicitly shut off, we expand '{...}' and '[...]'
830 expressions and return total number of URLs in pattern set */
831 result = glob_url(&state->urls, urlnode->url, &state->urlnum,
832 global->showerror?global->errors:NULL);
833 if(result)
834 break;
835 urlnum = state->urlnum;
836 }
837 else
838 urlnum = 1; /* without globbing, this is a single URL */
839 }
840 else
841 urlnum = state->urlnum;
842
843 /* if multiple files extracted to stdout, insert separators! */
844 separator = ((!state->outfiles ||
845 !strcmp(state->outfiles, "-")) && urlnum > 1);
846
847 if(state->up < state->infilenum) {
848 struct per_transfer *per;
849 struct OutStruct *outs;
850 struct InStruct *input;
851 struct OutStruct *heads;
852 struct OutStruct *etag_save;
853 struct HdrCbData *hdrcbdata = NULL;
854 CURL *curl = curl_easy_init();
855 result = add_per_transfer(&per);
856 if(result || !curl) {
857 curl_easy_cleanup(curl);
858 result = CURLE_OUT_OF_MEMORY;
859 break;
860 }
861 if(state->uploadfile) {
862 per->uploadfile = strdup(state->uploadfile);
863 if(!per->uploadfile) {
864 curl_easy_cleanup(curl);
865 result = CURLE_OUT_OF_MEMORY;
866 break;
867 }
868 }
869 *added = TRUE;
870 per->config = config;
871 per->curl = curl;
872
873 /* default headers output stream is stdout */
874 heads = &per->heads;
875 heads->stream = stdout;
876
877 /* Single header file for all URLs */
878 if(config->headerfile) {
879 /* open file for output: */
880 if(strcmp(config->headerfile, "-")) {
881 FILE *newfile;
882 newfile = fopen(config->headerfile, per->prev == NULL?"wb":"ab");
883 if(!newfile) {
884 warnf(config->global, "Failed to open %s\n", config->headerfile);
885 result = CURLE_WRITE_ERROR;
886 break;
887 }
888 else {
889 heads->filename = config->headerfile;
890 heads->s_isreg = TRUE;
891 heads->fopened = TRUE;
892 heads->stream = newfile;
893 }
894 }
895 else {
896 /* always use binary mode for protocol header output */
897 set_binmode(heads->stream);
898 }
899 }
900
901 hdrcbdata = &per->hdrcbdata;
902
903 outs = &per->outs;
904 input = &per->input;
905
906 per->outfile = NULL;
907 per->infdopen = FALSE;
908 per->infd = STDIN_FILENO;
909
910 /* default output stream is stdout */
911 outs->stream = stdout;
912
913 /* --etag-compare */
914 if(config->etag_compare_file) {
915 char *etag_from_file = NULL;
916 char *header = NULL;
917
918 /* open file for reading: */
919 FILE *file = fopen(config->etag_compare_file, FOPEN_READTEXT);
920 if(!file && !config->etag_save_file) {
921 errorf(config->global,
922 "Failed to open %s\n", config->etag_compare_file);
923 result = CURLE_READ_ERROR;
924 break;
925 }
926
927 if((PARAM_OK == file2string(&etag_from_file, file)) &&
928 etag_from_file) {
929 header = aprintf("If-None-Match: %s", etag_from_file);
930 Curl_safefree(etag_from_file);
931 }
932 else
933 header = aprintf("If-None-Match: \"\"");
934
935 if(!header) {
936 if(file)
937 fclose(file);
938 errorf(config->global,
939 "Failed to allocate memory for custom etag header\n");
940 result = CURLE_OUT_OF_MEMORY;
941 break;
942 }
943
944 /* add Etag from file to list of custom headers */
945 add2list(&config->headers, header);
946
947 Curl_safefree(header);
948
949 if(file) {
950 fclose(file);
951 }
952 }
953
954 /* --etag-save */
955 etag_save = &per->etag_save;
956 etag_save->stream = stdout;
957
958 if(config->etag_save_file) {
959 /* open file for output: */
960 if(strcmp(config->etag_save_file, "-")) {
961 FILE *newfile = fopen(config->etag_save_file, "wb");
962 if(!newfile) {
963 warnf(
964 config->global,
965 "Failed to open %s\n", config->etag_save_file);
966
967 result = CURLE_WRITE_ERROR;
968 break;
969 }
970 else {
971 etag_save->filename = config->etag_save_file;
972 etag_save->s_isreg = TRUE;
973 etag_save->fopened = TRUE;
974 etag_save->stream = newfile;
975 }
976 }
977 else {
978 /* always use binary mode for protocol header output */
979 set_binmode(etag_save->stream);
980 }
981 }
982
983 if(metalink) {
984 /* For Metalink download, use name in Metalink file as
985 filename. */
986 per->outfile = strdup(mlfile->filename);
987 if(!per->outfile) {
988 result = CURLE_OUT_OF_MEMORY;
989 break;
990 }
991 per->this_url = strdup(mlres->url);
992 if(!per->this_url) {
993 result = CURLE_OUT_OF_MEMORY;
994 break;
995 }
996 per->mlfile = mlfile;
997 }
998 else {
999 if(state->urls) {
1000 result = glob_next_url(&per->this_url, state->urls);
1001 if(result)
1002 break;
1003 }
1004 else if(!state->li) {
1005 per->this_url = strdup(urlnode->url);
1006 if(!per->this_url) {
1007 result = CURLE_OUT_OF_MEMORY;
1008 break;
1009 }
1010 }
1011 else
1012 per->this_url = NULL;
1013 if(!per->this_url)
1014 break;
1015
1016 if(state->outfiles) {
1017 per->outfile = strdup(state->outfiles);
1018 if(!per->outfile) {
1019 result = CURLE_OUT_OF_MEMORY;
1020 break;
1021 }
1022 }
1023 }
1024
1025 if(((urlnode->flags&GETOUT_USEREMOTE) ||
1026 (per->outfile && strcmp("-", per->outfile))) &&
1027 (metalink || !config->use_metalink)) {
1028
1029 /*
1030 * We have specified a file name to store the result in, or we have
1031 * decided we want to use the remote file name.
1032 */
1033
1034 if(!per->outfile) {
1035 /* extract the file name from the URL */
1036 result = get_url_file_name(&per->outfile, per->this_url);
1037 if(result)
1038 break;
1039 if(!*per->outfile && !config->content_disposition) {
1040 errorf(global, "Remote file name has no length!\n");
1041 result = CURLE_WRITE_ERROR;
1042 break;
1043 }
1044 }
1045 else if(state->urls) {
1046 /* fill '#1' ... '#9' terms from URL pattern */
1047 char *storefile = per->outfile;
1048 result = glob_match_url(&per->outfile, storefile, state->urls);
1049 Curl_safefree(storefile);
1050 if(result) {
1051 /* bad globbing */
1052 warnf(config->global, "bad output glob!\n");
1053 break;
1054 }
1055 }
1056
1057 if(config->output_dir) {
1058 char *d = aprintf("%s/%s", config->output_dir, per->outfile);
1059 if(!d) {
1060 result = CURLE_WRITE_ERROR;
1061 break;
1062 }
1063 free(per->outfile);
1064 per->outfile = d;
1065 }
1066 /* Create the directory hierarchy, if not pre-existent to a multiple
1067 file output call */
1068
1069 if(config->create_dirs || metalink) {
1070 result = create_dir_hierarchy(per->outfile, global->errors);
1071 /* create_dir_hierarchy shows error upon CURLE_WRITE_ERROR */
1072 if(result)
1073 break;
1074 }
1075
1076 if((urlnode->flags & GETOUT_USEREMOTE)
1077 && config->content_disposition) {
1078 /* Our header callback MIGHT set the filename */
1079 DEBUGASSERT(!outs->filename);
1080 }
1081
1082 if(config->resume_from_current) {
1083 /* We're told to continue from where we are now. Get the size
1084 of the file as it is now and open it for append instead */
1085 struct_stat fileinfo;
1086 /* VMS -- Danger, the filesize is only valid for stream files */
1087 if(0 == stat(per->outfile, &fileinfo))
1088 /* set offset to current file size: */
1089 config->resume_from = fileinfo.st_size;
1090 else
1091 /* let offset be 0 */
1092 config->resume_from = 0;
1093 }
1094
1095 if(config->resume_from) {
1096 #ifdef __VMS
1097 /* open file for output, forcing VMS output format into stream
1098 mode which is needed for stat() call above to always work. */
1099 FILE *file = fopen(outfile, "ab",
1100 "ctx=stm", "rfm=stmlf", "rat=cr", "mrs=0");
1101 #else
1102 /* open file for output: */
1103 FILE *file = fopen(per->outfile, "ab");
1104 #endif
1105 if(!file) {
1106 errorf(global, "Can't open '%s'!\n", per->outfile);
1107 result = CURLE_WRITE_ERROR;
1108 break;
1109 }
1110 outs->fopened = TRUE;
1111 outs->stream = file;
1112 outs->init = config->resume_from;
1113 }
1114 else {
1115 outs->stream = NULL; /* open when needed */
1116 }
1117 outs->filename = per->outfile;
1118 outs->s_isreg = TRUE;
1119 }
1120
1121 if(per->uploadfile && !stdin_upload(per->uploadfile)) {
1122 /*
1123 * We have specified a file to upload and it isn't "-".
1124 */
1125 char *nurl = add_file_name_to_url(per->this_url, per->uploadfile);
1126 if(!nurl) {
1127 result = CURLE_OUT_OF_MEMORY;
1128 break;
1129 }
1130 per->this_url = nurl;
1131 }
1132 else if(per->uploadfile && stdin_upload(per->uploadfile)) {
1133 /* count to see if there are more than one auth bit set
1134 in the authtype field */
1135 int authbits = 0;
1136 int bitcheck = 0;
1137 while(bitcheck < 32) {
1138 if(config->authtype & (1UL << bitcheck++)) {
1139 authbits++;
1140 if(authbits > 1) {
1141 /* more than one, we're done! */
1142 break;
1143 }
1144 }
1145 }
1146
1147 /*
1148 * If the user has also selected --anyauth or --proxy-anyauth
1149 * we should warn him/her.
1150 */
1151 if(config->proxyanyauth || (authbits>1)) {
1152 warnf(config->global,
1153 "Using --anyauth or --proxy-anyauth with upload from stdin"
1154 " involves a big risk of it not working. Use a temporary"
1155 " file or a fixed auth type instead!\n");
1156 }
1157
1158 DEBUGASSERT(per->infdopen == FALSE);
1159 DEBUGASSERT(per->infd == STDIN_FILENO);
1160
1161 set_binmode(stdin);
1162 if(!strcmp(per->uploadfile, ".")) {
1163 if(curlx_nonblock((curl_socket_t)per->infd, TRUE) < 0)
1164 warnf(config->global,
1165 "fcntl failed on fd=%d: %s\n", per->infd, strerror(errno));
1166 }
1167 }
1168
1169 if(per->uploadfile && config->resume_from_current)
1170 config->resume_from = -1; /* -1 will then force get-it-yourself */
1171
1172 if(output_expected(per->this_url, per->uploadfile) && outs->stream &&
1173 isatty(fileno(outs->stream)))
1174 /* we send the output to a tty, therefore we switch off the progress
1175 meter */
1176 per->noprogress = global->noprogress = global->isatty = TRUE;
1177 else {
1178 /* progress meter is per download, so restore config
1179 values */
1180 per->noprogress = global->noprogress = orig_noprogress;
1181 global->isatty = orig_isatty;
1182 }
1183
1184 if(urlnum > 1 && !global->mute) {
1185 per->separator_err =
1186 aprintf("\n[%lu/%lu]: %s --> %s",
1187 state->li + 1, urlnum, per->this_url,
1188 per->outfile ? per->outfile : "<stdout>");
1189 if(separator)
1190 per->separator = aprintf("%s%s", CURLseparator, per->this_url);
1191 }
1192 if(httpgetfields) {
1193 char *urlbuffer;
1194 /* Find out whether the url contains a file name */
1195 const char *pc = strstr(per->this_url, "://");
1196 char sep = '?';
1197 if(pc)
1198 pc += 3;
1199 else
1200 pc = per->this_url;
1201
1202 pc = strrchr(pc, '/'); /* check for a slash */
1203
1204 if(pc) {
1205 /* there is a slash present in the URL */
1206
1207 if(strchr(pc, '?'))
1208 /* Ouch, there's already a question mark in the URL string, we
1209 then append the data with an ampersand separator instead! */
1210 sep = '&';
1211 }
1212 /*
1213 * Then append ? followed by the get fields to the url.
1214 */
1215 if(pc)
1216 urlbuffer = aprintf("%s%c%s", per->this_url, sep, httpgetfields);
1217 else
1218 /* Append / before the ? to create a well-formed url
1219 if the url contains a hostname only
1220 */
1221 urlbuffer = aprintf("%s/?%s", per->this_url, httpgetfields);
1222
1223 if(!urlbuffer) {
1224 result = CURLE_OUT_OF_MEMORY;
1225 break;
1226 }
1227
1228 Curl_safefree(per->this_url); /* free previous URL */
1229 per->this_url = urlbuffer; /* use our new URL instead! */
1230 }
1231
1232 if(!global->errors)
1233 global->errors = stderr;
1234
1235 if((!per->outfile || !strcmp(per->outfile, "-")) &&
1236 !config->use_ascii) {
1237 /* We get the output to stdout and we have not got the ASCII/text
1238 flag, then set stdout to be binary */
1239 set_binmode(stdout);
1240 }
1241
1242 /* explicitly passed to stdout means okaying binary gunk */
1243 config->terminal_binary_ok =
1244 (per->outfile && !strcmp(per->outfile, "-"));
1245
1246 /* Avoid having this setopt added to the --libcurl source output. */
1247 result = curl_easy_setopt(curl, CURLOPT_SHARE, share);
1248 if(result)
1249 break;
1250
1251 if(!config->tcp_nodelay)
1252 my_setopt(curl, CURLOPT_TCP_NODELAY, 0L);
1253
1254 if(config->tcp_fastopen)
1255 my_setopt(curl, CURLOPT_TCP_FASTOPEN, 1L);
1256
1257 /* where to store */
1258 my_setopt(curl, CURLOPT_WRITEDATA, per);
1259 my_setopt(curl, CURLOPT_INTERLEAVEDATA, per);
1260
1261 if(metalink || !config->use_metalink)
1262 /* what call to write */
1263 my_setopt(curl, CURLOPT_WRITEFUNCTION, tool_write_cb);
1264 #ifdef USE_METALINK
1265 else
1266 /* Set Metalink specific write callback function to parse
1267 XML data progressively. */
1268 my_setopt(curl, CURLOPT_WRITEFUNCTION, metalink_write_cb);
1269 #endif /* USE_METALINK */
1270
1271 /* for uploads */
1272 input->config = config;
1273 /* Note that if CURLOPT_READFUNCTION is fread (the default), then
1274 * lib/telnet.c will Curl_poll() on the input file descriptor
1275 * rather then calling the READFUNCTION at regular intervals.
1276 * The circumstances in which it is preferable to enable this
1277 * behaviour, by omitting to set the READFUNCTION & READDATA options,
1278 * have not been determined.
1279 */
1280 my_setopt(curl, CURLOPT_READDATA, input);
1281 /* what call to read */
1282 my_setopt(curl, CURLOPT_READFUNCTION, tool_read_cb);
1283
1284 /* in 7.18.0, the CURLOPT_SEEKFUNCTION/DATA pair is taking over what
1285 CURLOPT_IOCTLFUNCTION/DATA pair previously provided for seeking */
1286 my_setopt(curl, CURLOPT_SEEKDATA, input);
1287 my_setopt(curl, CURLOPT_SEEKFUNCTION, tool_seek_cb);
1288
1289 if(config->recvpersecond &&
1290 (config->recvpersecond < BUFFER_SIZE))
1291 /* use a smaller sized buffer for better sleeps */
1292 my_setopt(curl, CURLOPT_BUFFERSIZE, (long)config->recvpersecond);
1293 else
1294 my_setopt(curl, CURLOPT_BUFFERSIZE, (long)BUFFER_SIZE);
1295
1296 my_setopt_str(curl, CURLOPT_URL, per->this_url);
1297 my_setopt(curl, CURLOPT_NOPROGRESS, global->noprogress?1L:0L);
1298 if(config->no_body)
1299 my_setopt(curl, CURLOPT_NOBODY, 1L);
1300
1301 if(config->oauth_bearer)
1302 my_setopt_str(curl, CURLOPT_XOAUTH2_BEARER, config->oauth_bearer);
1303
1304 {
1305 my_setopt_str(curl, CURLOPT_PROXY, config->proxy);
1306 /* new in libcurl 7.5 */
1307 if(config->proxy)
1308 my_setopt_enum(curl, CURLOPT_PROXYTYPE, config->proxyver);
1309
1310 my_setopt_str(curl, CURLOPT_PROXYUSERPWD, config->proxyuserpwd);
1311
1312 /* new in libcurl 7.3 */
1313 my_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, config->proxytunnel?1L:0L);
1314
1315 /* new in libcurl 7.52.0 */
1316 if(config->preproxy)
1317 my_setopt_str(curl, CURLOPT_PRE_PROXY, config->preproxy);
1318
1319 /* new in libcurl 7.10.6 */
1320 if(config->proxyanyauth)
1321 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
1322 (long)CURLAUTH_ANY);
1323 else if(config->proxynegotiate)
1324 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
1325 (long)CURLAUTH_GSSNEGOTIATE);
1326 else if(config->proxyntlm)
1327 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
1328 (long)CURLAUTH_NTLM);
1329 else if(config->proxydigest)
1330 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
1331 (long)CURLAUTH_DIGEST);
1332 else if(config->proxybasic)
1333 my_setopt_bitmask(curl, CURLOPT_PROXYAUTH,
1334 (long)CURLAUTH_BASIC);
1335
1336 /* new in libcurl 7.19.4 */
1337 my_setopt_str(curl, CURLOPT_NOPROXY, config->noproxy);
1338
1339 my_setopt(curl, CURLOPT_SUPPRESS_CONNECT_HEADERS,
1340 config->suppress_connect_headers?1L:0L);
1341 }
1342
1343 my_setopt(curl, CURLOPT_FAILONERROR, config->failonerror?1L:0L);
1344 my_setopt(curl, CURLOPT_REQUEST_TARGET, config->request_target);
1345 my_setopt(curl, CURLOPT_UPLOAD, per->uploadfile?1L:0L);
1346 my_setopt(curl, CURLOPT_DIRLISTONLY, config->dirlistonly?1L:0L);
1347 my_setopt(curl, CURLOPT_APPEND, config->ftp_append?1L:0L);
1348
1349 if(config->netrc_opt)
1350 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_OPTIONAL);
1351 else if(config->netrc || config->netrc_file)
1352 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_REQUIRED);
1353 else
1354 my_setopt_enum(curl, CURLOPT_NETRC, (long)CURL_NETRC_IGNORED);
1355
1356 if(config->netrc_file)
1357 my_setopt_str(curl, CURLOPT_NETRC_FILE, config->netrc_file);
1358
1359 my_setopt(curl, CURLOPT_TRANSFERTEXT, config->use_ascii?1L:0L);
1360 if(config->login_options)
1361 my_setopt_str(curl, CURLOPT_LOGIN_OPTIONS, config->login_options);
1362 my_setopt_str(curl, CURLOPT_USERPWD, config->userpwd);
1363 my_setopt_str(curl, CURLOPT_RANGE, config->range);
1364 my_setopt(curl, CURLOPT_ERRORBUFFER, per->errorbuffer);
1365 my_setopt(curl, CURLOPT_TIMEOUT_MS, (long)(config->timeout * 1000));
1366
1367 switch(config->httpreq) {
1368 case HTTPREQ_SIMPLEPOST:
1369 my_setopt_str(curl, CURLOPT_POSTFIELDS,
1370 config->postfields);
1371 my_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE,
1372 config->postfieldsize);
1373 break;
1374 case HTTPREQ_MIMEPOST:
1375 /* free previous remainders */
1376 curl_mime_free(config->mimepost);
1377 config->mimepost = NULL;
1378 result = tool2curlmime(curl, config->mimeroot, &config->mimepost);
1379 if(result)
1380 break;
1381 my_setopt_mimepost(curl, CURLOPT_MIMEPOST, config->mimepost);
1382 break;
1383 default:
1384 break;
1385 }
1386 if(result)
1387 break;
1388
1389 /* new in libcurl 7.10.6 (default is Basic) */
1390 if(config->authtype)
1391 my_setopt_bitmask(curl, CURLOPT_HTTPAUTH, (long)config->authtype);
1392
1393 my_setopt_slist(curl, CURLOPT_HTTPHEADER, config->headers);
1394
1395 if(built_in_protos & (CURLPROTO_HTTP | CURLPROTO_RTSP)) {
1396 my_setopt_str(curl, CURLOPT_REFERER, config->referer);
1397 my_setopt_str(curl, CURLOPT_USERAGENT, config->useragent);
1398 }
1399
1400 if(built_in_protos & CURLPROTO_HTTP) {
1401
1402 long postRedir = 0;
1403
1404 my_setopt(curl, CURLOPT_FOLLOWLOCATION,
1405 config->followlocation?1L:0L);
1406 my_setopt(curl, CURLOPT_UNRESTRICTED_AUTH,
1407 config->unrestricted_auth?1L:0L);
1408
1409 my_setopt(curl, CURLOPT_AUTOREFERER, config->autoreferer?1L:0L);
1410
1411 /* new in libcurl 7.36.0 */
1412 if(config->proxyheaders) {
1413 my_setopt_slist(curl, CURLOPT_PROXYHEADER, config->proxyheaders);
1414 my_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE);
1415 }
1416
1417 /* new in libcurl 7.5 */
1418 my_setopt(curl, CURLOPT_MAXREDIRS, config->maxredirs);
1419
1420 if(config->httpversion)
1421 my_setopt_enum(curl, CURLOPT_HTTP_VERSION, config->httpversion);
1422 else if(curlinfo->features & CURL_VERSION_HTTP2) {
1423 my_setopt_enum(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
1424 }
1425
1426 /* curl 7.19.1 (the 301 version existed in 7.18.2),
1427 303 was added in 7.26.0 */
1428 if(config->post301)
1429 postRedir |= CURL_REDIR_POST_301;
1430 if(config->post302)
1431 postRedir |= CURL_REDIR_POST_302;
1432 if(config->post303)
1433 postRedir |= CURL_REDIR_POST_303;
1434 my_setopt(curl, CURLOPT_POSTREDIR, postRedir);
1435
1436 /* new in libcurl 7.21.6 */
1437 if(config->encoding)
1438 my_setopt_str(curl, CURLOPT_ACCEPT_ENCODING, "");
1439
1440 /* new in libcurl 7.21.6 */
1441 if(config->tr_encoding)
1442 my_setopt(curl, CURLOPT_TRANSFER_ENCODING, 1L);
1443 /* new in libcurl 7.64.0 */
1444 my_setopt(curl, CURLOPT_HTTP09_ALLOWED,
1445 config->http09_allowed ? 1L : 0L);
1446
1447 } /* (built_in_protos & CURLPROTO_HTTP) */
1448
1449 my_setopt_str(curl, CURLOPT_FTPPORT, config->ftpport);
1450 my_setopt(curl, CURLOPT_LOW_SPEED_LIMIT,
1451 config->low_speed_limit);
1452 my_setopt(curl, CURLOPT_LOW_SPEED_TIME, config->low_speed_time);
1453 my_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE,
1454 config->sendpersecond);
1455 my_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE,
1456 config->recvpersecond);
1457
1458 if(config->use_resume)
1459 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, config->resume_from);
1460 else
1461 my_setopt(curl, CURLOPT_RESUME_FROM_LARGE, CURL_OFF_T_C(0));
1462
1463 my_setopt_str(curl, CURLOPT_KEYPASSWD, config->key_passwd);
1464 my_setopt_str(curl, CURLOPT_PROXY_KEYPASSWD, config->proxy_key_passwd);
1465
1466 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
1467
1468 /* SSH and SSL private key uses same command-line option */
1469 /* new in libcurl 7.16.1 */
1470 my_setopt_str(curl, CURLOPT_SSH_PRIVATE_KEYFILE, config->key);
1471 /* new in libcurl 7.16.1 */
1472 my_setopt_str(curl, CURLOPT_SSH_PUBLIC_KEYFILE, config->pubkey);
1473
1474 /* new in libcurl 7.17.1: SSH host key md5 checking allows us
1475 to fail if we are not talking to who we think we should */
1476 my_setopt_str(curl, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5,
1477 config->hostpubmd5);
1478
1479 /* new in libcurl 7.56.0 */
1480 if(config->ssh_compression)
1481 my_setopt(curl, CURLOPT_SSH_COMPRESSION, 1L);
1482 }
1483
1484 if(config->cacert)
1485 my_setopt_str(curl, CURLOPT_CAINFO, config->cacert);
1486 if(config->proxy_cacert)
1487 my_setopt_str(curl, CURLOPT_PROXY_CAINFO, config->proxy_cacert);
1488
1489 if(config->capath) {
1490 result = res_setopt_str(curl, CURLOPT_CAPATH, config->capath);
1491 if(result == CURLE_NOT_BUILT_IN) {
1492 warnf(config->global, "ignoring %s, not supported by libcurl\n",
1493 capath_from_env?
1494 "SSL_CERT_DIR environment variable":"--capath");
1495 }
1496 else if(result)
1497 break;
1498 }
1499 /* For the time being if --proxy-capath is not set then we use the
1500 --capath value for it, if any. See #1257 */
1501 if((config->proxy_capath || config->capath) &&
1502 !tool_setopt_skip(CURLOPT_PROXY_CAPATH)) {
1503 result = res_setopt_str(curl, CURLOPT_PROXY_CAPATH,
1504 (config->proxy_capath ?
1505 config->proxy_capath :
1506 config->capath));
1507 if(result == CURLE_NOT_BUILT_IN) {
1508 if(config->proxy_capath) {
1509 warnf(config->global,
1510 "ignoring --proxy-capath, not supported by libcurl\n");
1511 }
1512 }
1513 else if(result)
1514 break;
1515 }
1516
1517 if(config->crlfile)
1518 my_setopt_str(curl, CURLOPT_CRLFILE, config->crlfile);
1519 if(config->proxy_crlfile)
1520 my_setopt_str(curl, CURLOPT_PROXY_CRLFILE, config->proxy_crlfile);
1521 else if(config->crlfile) /* CURLOPT_PROXY_CRLFILE default is crlfile */
1522 my_setopt_str(curl, CURLOPT_PROXY_CRLFILE, config->crlfile);
1523
1524 if(config->pinnedpubkey)
1525 my_setopt_str(curl, CURLOPT_PINNEDPUBLICKEY, config->pinnedpubkey);
1526
1527 if(config->ssl_ec_curves)
1528 my_setopt_str(curl, CURLOPT_SSL_EC_CURVES, config->ssl_ec_curves);
1529
1530 if(curlinfo->features & CURL_VERSION_SSL) {
1531 /* Check if config->cert is a PKCS#11 URI and set the
1532 * config->cert_type if necessary */
1533 if(config->cert) {
1534 if(!config->cert_type) {
1535 if(is_pkcs11_uri(config->cert)) {
1536 config->cert_type = strdup("ENG");
1537 }
1538 }
1539 }
1540
1541 /* Check if config->key is a PKCS#11 URI and set the
1542 * config->key_type if necessary */
1543 if(config->key) {
1544 if(!config->key_type) {
1545 if(is_pkcs11_uri(config->key)) {
1546 config->key_type = strdup("ENG");
1547 }
1548 }
1549 }
1550
1551 /* Check if config->proxy_cert is a PKCS#11 URI and set the
1552 * config->proxy_type if necessary */
1553 if(config->proxy_cert) {
1554 if(!config->proxy_cert_type) {
1555 if(is_pkcs11_uri(config->proxy_cert)) {
1556 config->proxy_cert_type = strdup("ENG");
1557 }
1558 }
1559 }
1560
1561 /* Check if config->proxy_key is a PKCS#11 URI and set the
1562 * config->proxy_key_type if necessary */
1563 if(config->proxy_key) {
1564 if(!config->proxy_key_type) {
1565 if(is_pkcs11_uri(config->proxy_key)) {
1566 config->proxy_key_type = strdup("ENG");
1567 }
1568 }
1569 }
1570
1571 /* In debug build of curl tool, using
1572 * --cert loadmem=<filename>:<password> --cert-type p12
1573 * must do the same thing than classic:
1574 * --cert <filename>:<password> --cert-type p12
1575 * but is designed to test blob */
1576 #if defined(CURLDEBUG) || defined(DEBUGBUILD)
1577 if(config->cert && (strlen(config->cert) > 8) &&
1578 (memcmp(config->cert, "loadmem=",8) == 0)) {
1579 FILE *fInCert = fopen(config->cert + 8, "rb");
1580 void *certdata = NULL;
1581 long filesize = 0;
1582 bool continue_reading = fInCert != NULL;
1583 if(continue_reading)
1584 continue_reading = fseek(fInCert, 0, SEEK_END) == 0;
1585 if(continue_reading)
1586 filesize = ftell(fInCert);
1587 if(filesize < 0)
1588 continue_reading = FALSE;
1589 if(continue_reading)
1590 continue_reading = fseek(fInCert, 0, SEEK_SET) == 0;
1591 if(continue_reading)
1592 certdata = malloc(((size_t)filesize) + 1);
1593 if((!certdata) ||
1594 ((int)fread(certdata, (size_t)filesize, 1, fInCert) != 1))
1595 continue_reading = FALSE;
1596 if(fInCert)
1597 fclose(fInCert);
1598 if((filesize > 0) && continue_reading) {
1599 struct curl_blob structblob;
1600 structblob.data = certdata;
1601 structblob.len = (size_t)filesize;
1602 structblob.flags = CURL_BLOB_COPY;
1603 my_setopt_str(curl, CURLOPT_SSLCERT_BLOB, &structblob);
1604 /* if test run well, we are sure we don't reuse
1605 * original mem pointer */
1606 memset(certdata, 0, (size_t)filesize);
1607 }
1608 free(certdata);
1609 }
1610 else
1611 #endif
1612 my_setopt_str(curl, CURLOPT_SSLCERT, config->cert);
1613 my_setopt_str(curl, CURLOPT_PROXY_SSLCERT, config->proxy_cert);
1614 my_setopt_str(curl, CURLOPT_SSLCERTTYPE, config->cert_type);
1615 my_setopt_str(curl, CURLOPT_PROXY_SSLCERTTYPE,
1616 config->proxy_cert_type);
1617
1618
1619 #if defined(CURLDEBUG) || defined(DEBUGBUILD)
1620 if(config->key && (strlen(config->key) > 8) &&
1621 (memcmp(config->key, "loadmem=",8) == 0)) {
1622 FILE *fInCert = fopen(config->key + 8, "rb");
1623 void *certdata = NULL;
1624 long filesize = 0;
1625 bool continue_reading = fInCert != NULL;
1626 if(continue_reading)
1627 continue_reading = fseek(fInCert, 0, SEEK_END) == 0;
1628 if(continue_reading)
1629 filesize = ftell(fInCert);
1630 if(filesize < 0)
1631 continue_reading = FALSE;
1632 if(continue_reading)
1633 continue_reading = fseek(fInCert, 0, SEEK_SET) == 0;
1634 if(continue_reading)
1635 certdata = malloc(((size_t)filesize) + 1);
1636 if((!certdata) ||
1637 ((int)fread(certdata, (size_t)filesize, 1, fInCert) != 1))
1638 continue_reading = FALSE;
1639 if(fInCert)
1640 fclose(fInCert);
1641 if((filesize > 0) && continue_reading) {
1642 struct curl_blob structblob;
1643 structblob.data = certdata;
1644 structblob.len = (size_t)filesize;
1645 structblob.flags = CURL_BLOB_COPY;
1646 my_setopt_str(curl, CURLOPT_SSLKEY_BLOB, &structblob);
1647 /* if test run well, we are sure we don't reuse
1648 * original mem pointer */
1649 memset(certdata, 0, (size_t)filesize);
1650 }
1651 free(certdata);
1652 }
1653 else
1654 #endif
1655 my_setopt_str(curl, CURLOPT_SSLKEY, config->key);
1656 my_setopt_str(curl, CURLOPT_PROXY_SSLKEY, config->proxy_key);
1657 my_setopt_str(curl, CURLOPT_SSLKEYTYPE, config->key_type);
1658 my_setopt_str(curl, CURLOPT_PROXY_SSLKEYTYPE,
1659 config->proxy_key_type);
1660
1661 if(config->insecure_ok) {
1662 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
1663 my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
1664 }
1665 else {
1666 my_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
1667 /* libcurl default is strict verifyhost -> 2L */
1668 /* my_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); */
1669 }
1670 if(config->proxy_insecure_ok) {
1671 my_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 0L);
1672 my_setopt(curl, CURLOPT_PROXY_SSL_VERIFYHOST, 0L);
1673 }
1674 else {
1675 my_setopt(curl, CURLOPT_PROXY_SSL_VERIFYPEER, 1L);
1676 }
1677
1678 if(config->verifystatus)
1679 my_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, 1L);
1680
1681 if(config->falsestart)
1682 my_setopt(curl, CURLOPT_SSL_FALSESTART, 1L);
1683
1684 my_setopt_enum(curl, CURLOPT_SSLVERSION,
1685 config->ssl_version | config->ssl_version_max);
1686 my_setopt_enum(curl, CURLOPT_PROXY_SSLVERSION,
1687 config->proxy_ssl_version);
1688
1689 {
1690 long mask =
1691 (config->ssl_allow_beast ? CURLSSLOPT_ALLOW_BEAST : 0) |
1692 (config->ssl_revoke_best_effort ?
1693 CURLSSLOPT_REVOKE_BEST_EFFORT : 0) |
1694 (config->native_ca_store ?
1695 CURLSSLOPT_NATIVE_CA : 0) |
1696 (config->ssl_no_revoke ? CURLSSLOPT_NO_REVOKE : 0);
1697
1698 if(mask)
1699 my_setopt_bitmask(curl, CURLOPT_SSL_OPTIONS, mask);
1700 }
1701
1702 if(config->proxy_ssl_allow_beast)
1703 my_setopt(curl, CURLOPT_PROXY_SSL_OPTIONS,
1704 (long)CURLSSLOPT_ALLOW_BEAST);
1705 }
1706
1707 if(config->path_as_is)
1708 my_setopt(curl, CURLOPT_PATH_AS_IS, 1L);
1709
1710 if(built_in_protos & (CURLPROTO_SCP|CURLPROTO_SFTP)) {
1711 if(!config->insecure_ok) {
1712 char *home;
1713 char *file;
1714 result = CURLE_FAILED_INIT;
1715 home = homedir(NULL);
1716 if(home) {
1717 file = aprintf("%s/.ssh/known_hosts", home);
1718 if(file) {
1719 /* new in curl 7.19.6 */
1720 result = res_setopt_str(curl, CURLOPT_SSH_KNOWNHOSTS, file);
1721 curl_free(file);
1722 if(result == CURLE_UNKNOWN_OPTION)
1723 /* libssh2 version older than 1.1.1 */
1724 result = CURLE_OK;
1725 }
1726 Curl_safefree(home);
1727 }
1728 else {
1729 errorf(global, "Failed to figure out user's home dir!");
1730 }
1731 if(result)
1732 break;
1733 }
1734 }
1735
1736 if(config->no_body || config->remote_time) {
1737 /* no body or use remote time */
1738 my_setopt(curl, CURLOPT_FILETIME, 1L);
1739 }
1740
1741 my_setopt(curl, CURLOPT_CRLF, config->crlf?1L:0L);
1742 my_setopt_slist(curl, CURLOPT_QUOTE, config->quote);
1743 my_setopt_slist(curl, CURLOPT_POSTQUOTE, config->postquote);
1744 my_setopt_slist(curl, CURLOPT_PREQUOTE, config->prequote);
1745
1746 if(config->cookie)
1747 my_setopt_str(curl, CURLOPT_COOKIE, config->cookie);
1748
1749 if(config->cookiefile)
1750 my_setopt_str(curl, CURLOPT_COOKIEFILE, config->cookiefile);
1751
1752 /* new in libcurl 7.9 */
1753 if(config->cookiejar)
1754 my_setopt_str(curl, CURLOPT_COOKIEJAR, config->cookiejar);
1755
1756 /* new in libcurl 7.9.7 */
1757 my_setopt(curl, CURLOPT_COOKIESESSION, config->cookiesession?1L:0L);
1758
1759 my_setopt_enum(curl, CURLOPT_TIMECONDITION, (long)config->timecond);
1760 my_setopt(curl, CURLOPT_TIMEVALUE_LARGE, config->condtime);
1761 my_setopt_str(curl, CURLOPT_CUSTOMREQUEST, config->customrequest);
1762 customrequest_helper(config, config->httpreq, config->customrequest);
1763 my_setopt(curl, CURLOPT_STDERR, global->errors);
1764
1765 /* three new ones in libcurl 7.3: */
1766 my_setopt_str(curl, CURLOPT_INTERFACE, config->iface);
1767 my_setopt_str(curl, CURLOPT_KRBLEVEL, config->krblevel);
1768 progressbarinit(&per->progressbar, config);
1769
1770 if((global->progressmode == CURL_PROGRESS_BAR) &&
1771 !global->noprogress && !global->mute) {
1772 /* we want the alternative style, then we have to implement it
1773 ourselves! */
1774 my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_progress_cb);
1775 my_setopt(curl, CURLOPT_XFERINFODATA, per);
1776 }
1777 else if(per->uploadfile && !strcmp(per->uploadfile, ".")) {
1778 /* when reading from stdin in non-blocking mode, we use the progress
1779 function to unpause a busy read */
1780 my_setopt(curl, CURLOPT_NOPROGRESS, 0L);
1781 my_setopt(curl, CURLOPT_XFERINFOFUNCTION, tool_readbusy_cb);
1782 my_setopt(curl, CURLOPT_XFERINFODATA, per);
1783 }
1784
1785 /* new in libcurl 7.24.0: */
1786 if(config->dns_servers)
1787 my_setopt_str(curl, CURLOPT_DNS_SERVERS, config->dns_servers);
1788
1789 /* new in libcurl 7.33.0: */
1790 if(config->dns_interface)
1791 my_setopt_str(curl, CURLOPT_DNS_INTERFACE, config->dns_interface);
1792 if(config->dns_ipv4_addr)
1793 my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP4, config->dns_ipv4_addr);
1794 if(config->dns_ipv6_addr)
1795 my_setopt_str(curl, CURLOPT_DNS_LOCAL_IP6, config->dns_ipv6_addr);
1796
1797 /* new in libcurl 7.6.2: */
1798 my_setopt_slist(curl, CURLOPT_TELNETOPTIONS, config->telnet_options);
1799
1800 /* new in libcurl 7.7: */
1801 my_setopt_str(curl, CURLOPT_RANDOM_FILE, config->random_file);
1802 my_setopt_str(curl, CURLOPT_EGDSOCKET, config->egd_file);
1803 my_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
1804 (long)(config->connecttimeout * 1000));
1805
1806 if(config->doh_url)
1807 my_setopt_str(curl, CURLOPT_DOH_URL, config->doh_url);
1808
1809 if(config->cipher_list)
1810 my_setopt_str(curl, CURLOPT_SSL_CIPHER_LIST, config->cipher_list);
1811
1812 if(config->proxy_cipher_list)
1813 my_setopt_str(curl, CURLOPT_PROXY_SSL_CIPHER_LIST,
1814 config->proxy_cipher_list);
1815
1816 if(config->cipher13_list)
1817 my_setopt_str(curl, CURLOPT_TLS13_CIPHERS, config->cipher13_list);
1818
1819 if(config->proxy_cipher13_list)
1820 my_setopt_str(curl, CURLOPT_PROXY_TLS13_CIPHERS,
1821 config->proxy_cipher13_list);
1822
1823 /* new in libcurl 7.9.2: */
1824 if(config->disable_epsv)
1825 /* disable it */
1826 my_setopt(curl, CURLOPT_FTP_USE_EPSV, 0L);
1827
1828 /* new in libcurl 7.10.5 */
1829 if(config->disable_eprt)
1830 /* disable it */
1831 my_setopt(curl, CURLOPT_FTP_USE_EPRT, 0L);
1832
1833 if(global->tracetype != TRACE_NONE) {
1834 my_setopt(curl, CURLOPT_DEBUGFUNCTION, tool_debug_cb);
1835 my_setopt(curl, CURLOPT_DEBUGDATA, config);
1836 my_setopt(curl, CURLOPT_VERBOSE, 1L);
1837 }
1838
1839 /* new in curl 7.9.3 */
1840 if(config->engine) {
1841 result = res_setopt_str(curl, CURLOPT_SSLENGINE, config->engine);
1842 if(result)
1843 break;
1844 }
1845
1846 /* new in curl 7.10.7, extended in 7.19.4. Modified to use
1847 CREATE_DIR_RETRY in 7.49.0 */
1848 my_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS,
1849 (long)(config->ftp_create_dirs?
1850 CURLFTP_CREATE_DIR_RETRY:
1851 CURLFTP_CREATE_DIR_NONE));
1852
1853 /* new in curl 7.10.8 */
1854 if(config->max_filesize)
1855 my_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
1856 config->max_filesize);
1857
1858 my_setopt(curl, CURLOPT_IPRESOLVE, config->ip_version);
1859
1860 /* new in curl 7.15.5 */
1861 if(config->ftp_ssl_reqd)
1862 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
1863
1864 /* new in curl 7.11.0 */
1865 else if(config->ftp_ssl)
1866 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
1867
1868 /* new in curl 7.16.0 */
1869 else if(config->ftp_ssl_control)
1870 my_setopt_enum(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_CONTROL);
1871
1872 /* new in curl 7.16.1 */
1873 if(config->ftp_ssl_ccc)
1874 my_setopt_enum(curl, CURLOPT_FTP_SSL_CCC,
1875 (long)config->ftp_ssl_ccc_mode);
1876
1877 /* new in curl 7.19.4 */
1878 if(config->socks5_gssapi_nec)
1879 my_setopt_str(curl, CURLOPT_SOCKS5_GSSAPI_NEC,
1880 config->socks5_gssapi_nec);
1881
1882 /* new in curl 7.55.0 */
1883 if(config->socks5_auth)
1884 my_setopt_bitmask(curl, CURLOPT_SOCKS5_AUTH,
1885 (long)config->socks5_auth);
1886
1887 /* new in curl 7.43.0 */
1888 if(config->proxy_service_name)
1889 my_setopt_str(curl, CURLOPT_PROXY_SERVICE_NAME,
1890 config->proxy_service_name);
1891
1892 /* new in curl 7.43.0 */
1893 if(config->service_name)
1894 my_setopt_str(curl, CURLOPT_SERVICE_NAME,
1895 config->service_name);
1896
1897 /* curl 7.13.0 */
1898 my_setopt_str(curl, CURLOPT_FTP_ACCOUNT, config->ftp_account);
1899 my_setopt(curl, CURLOPT_IGNORE_CONTENT_LENGTH, config->ignorecl?1L:0L);
1900
1901 /* curl 7.14.2 */
1902 my_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, config->ftp_skip_ip?1L:0L);
1903
1904 /* curl 7.15.1 */
1905 my_setopt(curl, CURLOPT_FTP_FILEMETHOD, (long)config->ftp_filemethod);
1906
1907 /* curl 7.15.2 */
1908 if(config->localport) {
1909 my_setopt(curl, CURLOPT_LOCALPORT, config->localport);
1910 my_setopt_str(curl, CURLOPT_LOCALPORTRANGE, config->localportrange);
1911 }
1912
1913 /* curl 7.15.5 */
1914 my_setopt_str(curl, CURLOPT_FTP_ALTERNATIVE_TO_USER,
1915 config->ftp_alternative_to_user);
1916
1917 /* curl 7.16.0 */
1918 if(config->disable_sessionid)
1919 /* disable it */
1920 my_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, 0L);
1921
1922 /* curl 7.16.2 */
1923 if(config->raw) {
1924 my_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 0L);
1925 my_setopt(curl, CURLOPT_HTTP_TRANSFER_DECODING, 0L);
1926 }
1927
1928 /* curl 7.17.1 */
1929 if(!config->nokeepalive) {
1930 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
1931 if(config->alivetime != 0) {
1932 my_setopt(curl, CURLOPT_TCP_KEEPIDLE, config->alivetime);
1933 my_setopt(curl, CURLOPT_TCP_KEEPINTVL, config->alivetime);
1934 }
1935 }
1936 else
1937 my_setopt(curl, CURLOPT_TCP_KEEPALIVE, 0L);
1938
1939 /* curl 7.20.0 */
1940 if(config->tftp_blksize)
1941 my_setopt(curl, CURLOPT_TFTP_BLKSIZE, config->tftp_blksize);
1942
1943 if(config->mail_from)
1944 my_setopt_str(curl, CURLOPT_MAIL_FROM, config->mail_from);
1945
1946 if(config->mail_rcpt)
1947 my_setopt_slist(curl, CURLOPT_MAIL_RCPT, config->mail_rcpt);
1948
1949 /* curl 7.69.x */
1950 my_setopt(curl, CURLOPT_MAIL_RCPT_ALLLOWFAILS,
1951 config->mail_rcpt_allowfails ? 1L : 0L);
1952
1953 /* curl 7.20.x */
1954 if(config->ftp_pret)
1955 my_setopt(curl, CURLOPT_FTP_USE_PRET, 1L);
1956
1957 if(config->proto_present)
1958 my_setopt_flags(curl, CURLOPT_PROTOCOLS, config->proto);
1959 if(config->proto_redir_present)
1960 my_setopt_flags(curl, CURLOPT_REDIR_PROTOCOLS, config->proto_redir);
1961
1962 if(config->content_disposition
1963 && (urlnode->flags & GETOUT_USEREMOTE))
1964 hdrcbdata->honor_cd_filename = TRUE;
1965 else
1966 hdrcbdata->honor_cd_filename = FALSE;
1967
1968 hdrcbdata->outs = outs;
1969 hdrcbdata->heads = heads;
1970 hdrcbdata->etag_save = etag_save;
1971 hdrcbdata->global = global;
1972 hdrcbdata->config = config;
1973
1974 my_setopt(curl, CURLOPT_HEADERFUNCTION, tool_header_cb);
1975 my_setopt(curl, CURLOPT_HEADERDATA, per);
1976
1977 if(config->resolve)
1978 /* new in 7.21.3 */
1979 my_setopt_slist(curl, CURLOPT_RESOLVE, config->resolve);
1980
1981 if(config->connect_to)
1982 /* new in 7.49.0 */
1983 my_setopt_slist(curl, CURLOPT_CONNECT_TO, config->connect_to);
1984
1985 /* new in 7.21.4 */
1986 if(curlinfo->features & CURL_VERSION_TLSAUTH_SRP) {
1987 if(config->tls_username)
1988 my_setopt_str(curl, CURLOPT_TLSAUTH_USERNAME,
1989 config->tls_username);
1990 if(config->tls_password)
1991 my_setopt_str(curl, CURLOPT_TLSAUTH_PASSWORD,
1992 config->tls_password);
1993 if(config->tls_authtype)
1994 my_setopt_str(curl, CURLOPT_TLSAUTH_TYPE,
1995 config->tls_authtype);
1996 if(config->proxy_tls_username)
1997 my_setopt_str(curl, CURLOPT_PROXY_TLSAUTH_USERNAME,
1998 config->proxy_tls_username);
1999 if(config->proxy_tls_password)
2000 my_setopt_str(curl, CURLOPT_PROXY_TLSAUTH_PASSWORD,
2001 config->proxy_tls_password);
2002 if(config->proxy_tls_authtype)
2003 my_setopt_str(curl, CURLOPT_PROXY_TLSAUTH_TYPE,
2004 config->proxy_tls_authtype);
2005 }
2006
2007 /* new in 7.22.0 */
2008 if(config->gssapi_delegation)
2009 my_setopt_str(curl, CURLOPT_GSSAPI_DELEGATION,
2010 config->gssapi_delegation);
2011
2012 if(config->mail_auth)
2013 my_setopt_str(curl, CURLOPT_MAIL_AUTH, config->mail_auth);
2014
2015 /* new in 7.66.0 */
2016 if(config->sasl_authzid)
2017 my_setopt_str(curl, CURLOPT_SASL_AUTHZID, config->sasl_authzid);
2018
2019 /* new in 7.31.0 */
2020 if(config->sasl_ir)
2021 my_setopt(curl, CURLOPT_SASL_IR, 1L);
2022
2023 if(config->nonpn) {
2024 my_setopt(curl, CURLOPT_SSL_ENABLE_NPN, 0L);
2025 }
2026
2027 if(config->noalpn) {
2028 my_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, 0L);
2029 }
2030
2031 /* new in 7.40.0, abstract support added in 7.53.0 */
2032 if(config->unix_socket_path) {
2033 if(config->abstract_unix_socket) {
2034 my_setopt_str(curl, CURLOPT_ABSTRACT_UNIX_SOCKET,
2035 config->unix_socket_path);
2036 }
2037 else {
2038 my_setopt_str(curl, CURLOPT_UNIX_SOCKET_PATH,
2039 config->unix_socket_path);
2040 }
2041 }
2042
2043 /* new in 7.45.0 */
2044 if(config->proto_default)
2045 my_setopt_str(curl, CURLOPT_DEFAULT_PROTOCOL, config->proto_default);
2046
2047 /* new in 7.47.0 */
2048 if(config->expect100timeout > 0)
2049 my_setopt_str(curl, CURLOPT_EXPECT_100_TIMEOUT_MS,
2050 (long)(config->expect100timeout*1000));
2051
2052 /* new in 7.48.0 */
2053 if(config->tftp_no_options)
2054 my_setopt(curl, CURLOPT_TFTP_NO_OPTIONS, 1L);
2055
2056 /* new in 7.59.0 */
2057 if(config->happy_eyeballs_timeout_ms != CURL_HET_DEFAULT)
2058 my_setopt(curl, CURLOPT_HAPPY_EYEBALLS_TIMEOUT_MS,
2059 config->happy_eyeballs_timeout_ms);
2060
2061 /* new in 7.60.0 */
2062 if(config->haproxy_protocol)
2063 my_setopt(curl, CURLOPT_HAPROXYPROTOCOL, 1L);
2064
2065 if(config->disallow_username_in_url)
2066 my_setopt(curl, CURLOPT_DISALLOW_USERNAME_IN_URL, 1L);
2067
2068 if(config->altsvc)
2069 my_setopt_str(curl, CURLOPT_ALTSVC, config->altsvc);
2070
2071 #ifdef USE_METALINK
2072 if(!metalink && config->use_metalink) {
2073 outs->metalink_parser = metalink_parser_context_new();
2074 if(outs->metalink_parser == NULL) {
2075 result = CURLE_OUT_OF_MEMORY;
2076 break;
2077 }
2078 fprintf(config->global->errors,
2079 "Metalink: parsing (%s) metalink/XML...\n", per->this_url);
2080 }
2081 else if(metalink)
2082 fprintf(config->global->errors,
2083 "Metalink: fetching (%s) from (%s)...\n",
2084 mlfile->filename, per->this_url);
2085 #endif /* USE_METALINK */
2086
2087 per->metalink = metalink;
2088 /* initialize retry vars for loop below */
2089 per->retry_sleep_default = (config->retry_delay) ?
2090 config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
2091 per->retry_numretries = config->req_retry;
2092 per->retry_sleep = per->retry_sleep_default; /* ms */
2093 per->retrystart = tvnow();
2094
2095 state->li++;
2096 /* Here's looping around each globbed URL */
2097 if(state->li >= urlnum) {
2098 state->li = 0;
2099 state->urlnum = 0; /* forced reglob of URLs */
2100 glob_cleanup(state->urls);
2101 state->urls = NULL;
2102 state->up++;
2103 Curl_safefree(state->uploadfile); /* clear it to get the next */
2104 }
2105 }
2106 else {
2107 /* Free this URL node data without destroying the
2108 the node itself nor modifying next pointer. */
2109 Curl_safefree(urlnode->outfile);
2110 Curl_safefree(urlnode->infile);
2111 urlnode->flags = 0;
2112 glob_cleanup(state->urls);
2113 state->urls = NULL;
2114 state->urlnum = 0;
2115
2116 Curl_safefree(state->outfiles);
2117 Curl_safefree(state->uploadfile);
2118 if(state->inglob) {
2119 /* Free list of globbed upload files */
2120 glob_cleanup(state->inglob);
2121 state->inglob = NULL;
2122 }
2123 config->state.urlnode = urlnode->next;
2124 state->up = 0;
2125 continue;
2126 }
2127 }
2128 break;
2129 }
2130
2131 if(!*added || result) {
2132 *added = FALSE;
2133 single_transfer_cleanup(config);
2134 }
2135 return result;
2136 }
2137
2138 static long all_added; /* number of easy handles currently added */
2139
2140 /*
2141 * add_parallel_transfers() sets 'morep' to TRUE if there are more transfers
2142 * to add even after this call returns. sets 'addedp' to TRUE if one or more
2143 * transfers were added.
2144 */
add_parallel_transfers(struct GlobalConfig * global,CURLM * multi,CURLSH * share,bool * morep,bool * addedp)2145 static CURLcode add_parallel_transfers(struct GlobalConfig *global,
2146 CURLM *multi,
2147 CURLSH *share,
2148 bool *morep,
2149 bool *addedp)
2150 {
2151 struct per_transfer *per;
2152 CURLcode result = CURLE_OK;
2153 CURLMcode mcode;
2154 bool sleeping = FALSE;
2155 *addedp = FALSE;
2156 *morep = FALSE;
2157 result = create_transfer(global, share, addedp);
2158 if(result)
2159 return result;
2160 for(per = transfers; per && (all_added < global->parallel_max);
2161 per = per->next) {
2162 bool getadded = FALSE;
2163 if(per->added)
2164 /* already added */
2165 continue;
2166 if(per->startat && (time(NULL) < per->startat)) {
2167 /* this is still delaying */
2168 sleeping = TRUE;
2169 continue;
2170 }
2171
2172 result = pre_transfer(global, per);
2173 if(result)
2174 break;
2175
2176 /* parallel connect means that we don't set PIPEWAIT since pipewait
2177 will make libcurl prefer multiplexing */
2178 (void)curl_easy_setopt(per->curl, CURLOPT_PIPEWAIT,
2179 global->parallel_connect ? 0L : 1L);
2180 (void)curl_easy_setopt(per->curl, CURLOPT_PRIVATE, per);
2181 (void)curl_easy_setopt(per->curl, CURLOPT_XFERINFOFUNCTION, xferinfo_cb);
2182 (void)curl_easy_setopt(per->curl, CURLOPT_XFERINFODATA, per);
2183
2184 mcode = curl_multi_add_handle(multi, per->curl);
2185 if(mcode)
2186 return CURLE_OUT_OF_MEMORY;
2187
2188 result = create_transfer(global, share, &getadded);
2189 if(result)
2190 return result;
2191 per->added = TRUE;
2192 all_added++;
2193 *addedp = TRUE;
2194 }
2195 *morep = (per || sleeping) ? TRUE : FALSE;
2196 return CURLE_OK;
2197 }
2198
parallel_transfers(struct GlobalConfig * global,CURLSH * share)2199 static CURLcode parallel_transfers(struct GlobalConfig *global,
2200 CURLSH *share)
2201 {
2202 CURLM *multi;
2203 CURLMcode mcode = CURLM_OK;
2204 CURLcode result = CURLE_OK;
2205 int still_running = 1;
2206 struct timeval start = tvnow();
2207 bool more_transfers;
2208 bool added_transfers;
2209 time_t tick = time(NULL);
2210
2211 multi = curl_multi_init();
2212 if(!multi)
2213 return CURLE_OUT_OF_MEMORY;
2214
2215 result = add_parallel_transfers(global, multi, share,
2216 &more_transfers, &added_transfers);
2217 if(result) {
2218 curl_multi_cleanup(multi);
2219 return result;
2220 }
2221
2222 while(!mcode && (still_running || more_transfers)) {
2223 mcode = curl_multi_poll(multi, NULL, 0, 1000, NULL);
2224 if(!mcode)
2225 mcode = curl_multi_perform(multi, &still_running);
2226
2227 progress_meter(global, &start, FALSE);
2228
2229 if(!mcode) {
2230 int rc;
2231 CURLMsg *msg;
2232 bool checkmore = FALSE;
2233 do {
2234 msg = curl_multi_info_read(multi, &rc);
2235 if(msg) {
2236 bool retry;
2237 long delay;
2238 struct per_transfer *ended;
2239 CURL *easy = msg->easy_handle;
2240 result = msg->data.result;
2241 curl_easy_getinfo(easy, CURLINFO_PRIVATE, (void *)&ended);
2242 curl_multi_remove_handle(multi, easy);
2243
2244 result = post_per_transfer(global, ended, result, &retry, &delay);
2245 progress_finalize(ended); /* before it goes away */
2246 all_added--; /* one fewer added */
2247 checkmore = TRUE;
2248 if(retry) {
2249 ended->added = FALSE; /* add it again */
2250 /* we delay retries in full integer seconds only */
2251 ended->startat = delay ? time(NULL) + delay/1000 : 0;
2252 }
2253 else
2254 (void)del_per_transfer(ended);
2255 }
2256 } while(msg);
2257 if(!checkmore) {
2258 time_t tock = time(NULL);
2259 if(tick != tock) {
2260 checkmore = TRUE;
2261 tick = tock;
2262 }
2263 }
2264 if(checkmore) {
2265 /* one or more transfers completed, add more! */
2266 (void)add_parallel_transfers(global, multi, share,
2267 &more_transfers,
2268 &added_transfers);
2269 if(added_transfers)
2270 /* we added new ones, make sure the loop doesn't exit yet */
2271 still_running = 1;
2272 }
2273 }
2274 }
2275
2276 (void)progress_meter(global, &start, TRUE);
2277
2278 /* Make sure to return some kind of error if there was a multi problem */
2279 if(mcode) {
2280 result = (mcode == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY :
2281 /* The other multi errors should never happen, so return
2282 something suitably generic */
2283 CURLE_BAD_FUNCTION_ARGUMENT;
2284 }
2285
2286 curl_multi_cleanup(multi);
2287
2288 return result;
2289 }
2290
serial_transfers(struct GlobalConfig * global,CURLSH * share)2291 static CURLcode serial_transfers(struct GlobalConfig *global,
2292 CURLSH *share)
2293 {
2294 CURLcode returncode = CURLE_OK;
2295 CURLcode result = CURLE_OK;
2296 struct per_transfer *per;
2297 bool added = FALSE;
2298
2299 result = create_transfer(global, share, &added);
2300 if(result || !added)
2301 return result;
2302 for(per = transfers; per;) {
2303 bool retry;
2304 long delay;
2305 bool bailout = FALSE;
2306 result = pre_transfer(global, per);
2307 if(result)
2308 break;
2309
2310 #ifndef CURL_DISABLE_LIBCURL_OPTION
2311 if(global->libcurl) {
2312 result = easysrc_perform();
2313 if(result)
2314 break;
2315 }
2316 #endif
2317 #ifdef CURLDEBUG
2318 if(global->test_event_based)
2319 result = curl_easy_perform_ev(per->curl);
2320 else
2321 #endif
2322 result = curl_easy_perform(per->curl);
2323
2324 /* store the result of the actual transfer */
2325 returncode = result;
2326
2327 result = post_per_transfer(global, per, result, &retry, &delay);
2328 if(retry) {
2329 tool_go_sleep(delay);
2330 continue;
2331 }
2332
2333 /* Bail out upon critical errors or --fail-early */
2334 if(result || is_fatal_error(returncode) ||
2335 (returncode && global->fail_early))
2336 bailout = TRUE;
2337 else {
2338 /* setup the next one just before we delete this */
2339 result = create_transfer(global, share, &added);
2340 if(result)
2341 bailout = TRUE;
2342 }
2343
2344 /* Release metalink related resources here */
2345 delete_metalinkfile(per->mlfile);
2346
2347 per = del_per_transfer(per);
2348
2349 if(bailout)
2350 break;
2351 }
2352 if(returncode)
2353 /* returncode errors have priority */
2354 result = returncode;
2355
2356 if(result)
2357 single_transfer_cleanup(global->current);
2358
2359 return result;
2360 }
2361
2362 /* setup a transfer for the given config */
transfer_per_config(struct GlobalConfig * global,struct OperationConfig * config,CURLSH * share,bool * added)2363 static CURLcode transfer_per_config(struct GlobalConfig *global,
2364 struct OperationConfig *config,
2365 CURLSH *share,
2366 bool *added)
2367 {
2368 CURLcode result = CURLE_OK;
2369 bool capath_from_env;
2370 *added = FALSE;
2371
2372 /* Check we have a url */
2373 if(!config->url_list || !config->url_list->url) {
2374 helpf(global->errors, "no URL specified!\n");
2375 return CURLE_FAILED_INIT;
2376 }
2377
2378 /* On WIN32 we can't set the path to curl-ca-bundle.crt
2379 * at compile time. So we look here for the file in two ways:
2380 * 1: look at the environment variable CURL_CA_BUNDLE for a path
2381 * 2: if #1 isn't found, use the windows API function SearchPath()
2382 * to find it along the app's path (includes app's dir and CWD)
2383 *
2384 * We support the environment variable thing for non-Windows platforms
2385 * too. Just for the sake of it.
2386 */
2387 capath_from_env = false;
2388 if(!config->cacert &&
2389 !config->capath &&
2390 !config->insecure_ok) {
2391 CURL *curltls = curl_easy_init();
2392 struct curl_tlssessioninfo *tls_backend_info = NULL;
2393
2394 /* With the addition of CAINFO support for Schannel, this search could find
2395 * a certificate bundle that was previously ignored. To maintain backward
2396 * compatibility, only perform this search if not using Schannel.
2397 */
2398 result = curl_easy_getinfo(curltls, CURLINFO_TLS_SSL_PTR,
2399 &tls_backend_info);
2400 if(result)
2401 return result;
2402
2403 /* Set the CA cert locations specified in the environment. For Windows if
2404 * no environment-specified filename is found then check for CA bundle
2405 * default filename curl-ca-bundle.crt in the user's PATH.
2406 *
2407 * If Schannel is the selected SSL backend then these locations are
2408 * ignored. We allow setting CA location for schannel only when explicitly
2409 * specified by the user via CURLOPT_CAINFO / --cacert.
2410 */
2411 if(tls_backend_info->backend != CURLSSLBACKEND_SCHANNEL) {
2412 char *env;
2413 env = curlx_getenv("CURL_CA_BUNDLE");
2414 if(env) {
2415 config->cacert = strdup(env);
2416 if(!config->cacert) {
2417 curl_free(env);
2418 errorf(global, "out of memory\n");
2419 return CURLE_OUT_OF_MEMORY;
2420 }
2421 }
2422 else {
2423 env = curlx_getenv("SSL_CERT_DIR");
2424 if(env) {
2425 config->capath = strdup(env);
2426 if(!config->capath) {
2427 curl_free(env);
2428 helpf(global->errors, "out of memory\n");
2429 return CURLE_OUT_OF_MEMORY;
2430 }
2431 capath_from_env = true;
2432 }
2433 else {
2434 env = curlx_getenv("SSL_CERT_FILE");
2435 if(env) {
2436 config->cacert = strdup(env);
2437 if(!config->cacert) {
2438 curl_free(env);
2439 errorf(global, "out of memory\n");
2440 return CURLE_OUT_OF_MEMORY;
2441 }
2442 }
2443 }
2444 }
2445
2446 if(env)
2447 curl_free(env);
2448 #ifdef WIN32
2449 else {
2450 result = FindWin32CACert(config, tls_backend_info->backend,
2451 TEXT("curl-ca-bundle.crt"));
2452 }
2453 #endif
2454 }
2455 curl_easy_cleanup(curltls);
2456 }
2457
2458 if(!result)
2459 result = single_transfer(global, config, share, capath_from_env, added);
2460
2461 return result;
2462 }
2463
2464 /*
2465 * 'create_transfer' gets the details and sets up a new transfer if 'added'
2466 * returns TRUE.
2467 */
create_transfer(struct GlobalConfig * global,CURLSH * share,bool * added)2468 static CURLcode create_transfer(struct GlobalConfig *global,
2469 CURLSH *share,
2470 bool *added)
2471 {
2472 CURLcode result = CURLE_OK;
2473 *added = FALSE;
2474 while(global->current) {
2475 result = transfer_per_config(global, global->current, share, added);
2476 if(!result && !*added) {
2477 /* when one set is drained, continue to next */
2478 global->current = global->current->next;
2479 continue;
2480 }
2481 break;
2482 }
2483 return result;
2484 }
2485
run_all_transfers(struct GlobalConfig * global,CURLSH * share,CURLcode result)2486 static CURLcode run_all_transfers(struct GlobalConfig *global,
2487 CURLSH *share,
2488 CURLcode result)
2489 {
2490 /* Save the values of noprogress and isatty to restore them later on */
2491 bool orig_noprogress = global->noprogress;
2492 bool orig_isatty = global->isatty;
2493 struct per_transfer *per;
2494
2495 /* Time to actually do the transfers */
2496 if(!result) {
2497 if(global->parallel)
2498 result = parallel_transfers(global, share);
2499 else
2500 result = serial_transfers(global, share);
2501 }
2502
2503 /* cleanup if there are any left */
2504 for(per = transfers; per;) {
2505 bool retry;
2506 long delay;
2507 CURLcode result2 = post_per_transfer(global, per, result, &retry, &delay);
2508 if(!result)
2509 /* don't overwrite the original error */
2510 result = result2;
2511
2512 /* Free list of given URLs */
2513 clean_getout(per->config);
2514
2515 /* Release metalink related resources here */
2516 clean_metalink(per->config);
2517 per = del_per_transfer(per);
2518 }
2519
2520 /* Reset the global config variables */
2521 global->noprogress = orig_noprogress;
2522 global->isatty = orig_isatty;
2523
2524
2525 return result;
2526 }
2527
operate(struct GlobalConfig * global,int argc,argv_item_t argv[])2528 CURLcode operate(struct GlobalConfig *global, int argc, argv_item_t argv[])
2529 {
2530 CURLcode result = CURLE_OK;
2531 char *first_arg = curlx_convert_tchar_to_UTF8(argv[1]);
2532
2533 /* Setup proper locale from environment */
2534 #ifdef HAVE_SETLOCALE
2535 setlocale(LC_ALL, "");
2536 #endif
2537
2538 /* Parse .curlrc if necessary */
2539 if((argc == 1) ||
2540 (!curl_strequal(first_arg, "-q") &&
2541 !curl_strequal(first_arg, "--disable"))) {
2542 parseconfig(NULL, global); /* ignore possible failure */
2543
2544 /* If we had no arguments then make sure a url was specified in .curlrc */
2545 if((argc < 2) && (!global->first->url_list)) {
2546 helpf(global->errors, NULL);
2547 result = CURLE_FAILED_INIT;
2548 }
2549 }
2550
2551 curlx_unicodefree(first_arg);
2552
2553 if(!result) {
2554 /* Parse the command line arguments */
2555 ParameterError res = parse_args(global, argc, argv);
2556 if(res) {
2557 result = CURLE_OK;
2558
2559 /* Check if we were asked for the help */
2560 if(res == PARAM_HELP_REQUESTED)
2561 tool_help(global->help_category);
2562 /* Check if we were asked for the manual */
2563 else if(res == PARAM_MANUAL_REQUESTED)
2564 hugehelp();
2565 /* Check if we were asked for the version information */
2566 else if(res == PARAM_VERSION_INFO_REQUESTED)
2567 tool_version_info();
2568 /* Check if we were asked to list the SSL engines */
2569 else if(res == PARAM_ENGINES_REQUESTED)
2570 tool_list_engines();
2571 else if(res == PARAM_LIBCURL_UNSUPPORTED_PROTOCOL)
2572 result = CURLE_UNSUPPORTED_PROTOCOL;
2573 else
2574 result = CURLE_FAILED_INIT;
2575 }
2576 else {
2577 #ifndef CURL_DISABLE_LIBCURL_OPTION
2578 if(global->libcurl) {
2579 /* Initialise the libcurl source output */
2580 result = easysrc_init();
2581 }
2582 #endif
2583
2584 /* Perform the main operations */
2585 if(!result) {
2586 size_t count = 0;
2587 struct OperationConfig *operation = global->first;
2588 CURLSH *share = curl_share_init();
2589 if(!share) {
2590 #ifndef CURL_DISABLE_LIBCURL_OPTION
2591 if(global->libcurl) {
2592 /* Cleanup the libcurl source output */
2593 easysrc_cleanup();
2594 }
2595 #endif
2596 return CURLE_OUT_OF_MEMORY;
2597 }
2598
2599 curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
2600 curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
2601 curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
2602 curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
2603 curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_PSL);
2604
2605 /* Get the required arguments for each operation */
2606 do {
2607 result = get_args(operation, count++);
2608
2609 operation = operation->next;
2610 } while(!result && operation);
2611
2612 /* Set the current operation pointer */
2613 global->current = global->first;
2614
2615 /* now run! */
2616 result = run_all_transfers(global, share, result);
2617
2618 curl_share_cleanup(share);
2619 #ifndef CURL_DISABLE_LIBCURL_OPTION
2620 if(global->libcurl) {
2621 /* Cleanup the libcurl source output */
2622 easysrc_cleanup();
2623
2624 /* Dump the libcurl code if previously enabled */
2625 dumpeasysrc(global);
2626 }
2627 #endif
2628 }
2629 else
2630 errorf(global, "out of memory\n");
2631 }
2632 }
2633
2634 return result;
2635 }
2636