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