1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 #include "tool_setup.h"
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
30 #include "tool_cfgable.h"
31 #include "tool_getparam.h"
32 #include "tool_getpass.h"
33 #include "tool_homedir.h"
34 #include "tool_msgs.h"
35 #include "tool_paramhlp.h"
36 #include "tool_version.h"
38 #include "memdebug.h" /* keep this as LAST include */
40 struct getout *new_getout(struct OperationConfig *config)
42 struct getout *node = calloc(1, sizeof(struct getout));
43 struct getout *last = config->url_last;
45 /* append this new node last in the list */
49 config->url_list = node; /* first node */
51 /* move the last pointer */
52 config->url_last = node;
54 node->flags = config->default_node_flags;
59 ParameterError file2string(char **bufp, FILE *file)
68 while(fgets(buffer, sizeof(buffer), file)) {
69 ptr = strchr(buffer, '\r');
72 ptr = strchr(buffer, '\n');
75 buflen = strlen(buffer);
76 ptr = realloc(string, stringlen+buflen+1);
78 Curl_safefree(string);
82 strcpy(string+stringlen, buffer);
90 ParameterError file2memory(char **bufp, size_t *size, FILE *file)
100 if(!buffer || (alloc == nused)) {
101 /* size_t overflow detection for huge files */
102 if(alloc+1 > ((size_t)-1)/2) {
103 Curl_safefree(buffer);
107 /* allocate an extra char, reserved space, for null termination */
108 newbuf = realloc(buffer, alloc+1);
110 Curl_safefree(buffer);
115 nread = fread(buffer+nused, 1, alloc-nused, file);
118 /* null terminate the buffer in case it's used as a string later */
119 buffer[nused] = '\0';
120 /* free trailing slack space, if possible */
122 newbuf = realloc(buffer, nused+1);
124 Curl_safefree(buffer);
129 /* discard buffer if nothing was read */
131 Curl_safefree(buffer); /* no string */
139 void cleanarg(char *str)
141 #ifdef HAVE_WRITABLE_ARGV
142 /* now that GetStr has copied the contents of nextarg, wipe the next
143 * argument out so that the username:password isn't displayed in the
144 * system process list */
146 size_t len = strlen(str);
147 memset(str, ' ', len);
155 * Parse the string and write the long in the given address. Return PARAM_OK
156 * on success, otherwise a parameter specific error enum.
158 * Since this function gets called with the 'nextarg' pointer from within the
159 * getparameter a lot, we must check it for NULL before accessing the str
163 ParameterError str2num(long *val, const char *str)
167 long num = strtol(str, &endptr, 10);
168 if((endptr != str) && (endptr == str + strlen(str))) {
170 return PARAM_OK; /* Ok */
173 return PARAM_BAD_NUMERIC; /* badness */
177 * Parse the string and write the long in the given address. Return PARAM_OK
178 * on success, otherwise a parameter error enum. ONLY ACCEPTS POSITIVE NUMBERS!
180 * Since this function gets called with the 'nextarg' pointer from within the
181 * getparameter a lot, we must check it for NULL before accessing the str
185 ParameterError str2unum(long *val, const char *str)
187 ParameterError result = str2num(val, str);
188 if(result != PARAM_OK)
191 return PARAM_NEGATIVE_NUMERIC;
197 * Parse the string and write the double in the given address. Return PARAM_OK
198 * on success, otherwise a parameter specific error enum.
200 * Since this function gets called with the 'nextarg' pointer from within the
201 * getparameter a lot, we must check it for NULL before accessing the str
205 ParameterError str2double(double *val, const char *str)
209 double num = strtod(str, &endptr);
210 if((endptr != str) && (endptr == str + strlen(str))) {
212 return PARAM_OK; /* Ok */
215 return PARAM_BAD_NUMERIC; /* badness */
219 * Parse the string and write the double in the given address. Return PARAM_OK
220 * on success, otherwise a parameter error enum. ONLY ACCEPTS POSITIVE NUMBERS!
222 * Since this function gets called with the 'nextarg' pointer from within the
223 * getparameter a lot, we must check it for NULL before accessing the str
227 ParameterError str2udouble(double *val, const char *str)
229 ParameterError result = str2double(val, str);
230 if(result != PARAM_OK)
233 return PARAM_NEGATIVE_NUMERIC;
239 * Parse the string and modify the long in the given address. Return
240 * non-zero on failure, zero on success.
242 * The string is a list of protocols
244 * Since this function gets called with the 'nextarg' pointer from within the
245 * getparameter a lot, we must check it for NULL before accessing the str
249 long proto2num(struct OperationConfig *config, long *val, const char *str)
252 const char *sep = ",";
255 static struct sprotos {
259 { "all", CURLPROTO_ALL },
260 { "http", CURLPROTO_HTTP },
261 { "https", CURLPROTO_HTTPS },
262 { "ftp", CURLPROTO_FTP },
263 { "ftps", CURLPROTO_FTPS },
264 { "scp", CURLPROTO_SCP },
265 { "sftp", CURLPROTO_SFTP },
266 { "telnet", CURLPROTO_TELNET },
267 { "ldap", CURLPROTO_LDAP },
268 { "ldaps", CURLPROTO_LDAPS },
269 { "dict", CURLPROTO_DICT },
270 { "file", CURLPROTO_FILE },
271 { "tftp", CURLPROTO_TFTP },
272 { "imap", CURLPROTO_IMAP },
273 { "imaps", CURLPROTO_IMAPS },
274 { "pop3", CURLPROTO_POP3 },
275 { "pop3s", CURLPROTO_POP3S },
276 { "smtp", CURLPROTO_SMTP },
277 { "smtps", CURLPROTO_SMTPS },
278 { "rtsp", CURLPROTO_RTSP },
279 { "gopher", CURLPROTO_GOPHER },
280 { "smb", CURLPROTO_SMB },
281 { "smbs", CURLPROTO_SMBS },
288 buffer = strdup(str); /* because strtok corrupts it */
292 /* Allow strtok() here since this isn't used threaded */
293 /* !checksrc! disable BANNEDFUNC 2 */
294 for(token = strtok(buffer, sep);
296 token = strtok(NULL, sep)) {
297 enum e_action { allow, deny, set } action = allow;
299 struct sprotos const *pp;
301 /* Process token modifiers */
302 while(!ISALNUM(*token)) { /* may be NULL if token is all modifiers */
313 default: /* Includes case of terminating NULL */
314 Curl_safefree(buffer);
319 for(pp=protos; pp->name; pp++) {
320 if(curl_strequal(token, pp->name)) {
336 if(!(pp->name)) { /* unknown protocol */
337 /* If they have specified only this protocol, we say treat it as
338 if no protocols are allowed */
341 warnf(config->global, "unrecognized protocol '%s'\n", token);
344 Curl_safefree(buffer);
349 * Check if the given string is a protocol supported by libcurl
351 * @param str the protocol name
352 * @return PARAM_OK protocol supported
353 * @return PARAM_LIBCURL_UNSUPPORTED_PROTOCOL protocol not supported
354 * @return PARAM_REQUIRES_PARAMETER missing parameter
356 int check_protocol(const char *str)
358 const char * const *pp;
359 const curl_version_info_data *curlinfo = curl_version_info(CURLVERSION_NOW);
361 return PARAM_REQUIRES_PARAMETER;
362 for(pp = curlinfo->protocols; *pp; pp++) {
363 if(curl_strequal(*pp, str))
366 return PARAM_LIBCURL_UNSUPPORTED_PROTOCOL;
370 * Parses the given string looking for an offset (which may be a
371 * larger-than-integer value). The offset CANNOT be negative!
373 * @param val the offset to populate
374 * @param str the buffer containing the offset
375 * @return PARAM_OK if successful, a parameter specific error enum if failure.
377 ParameterError str2offset(curl_off_t *val, const char *str)
381 /* offsets aren't negative, this indicates weird input */
382 return PARAM_NEGATIVE_NUMERIC;
384 #if(CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
385 *val = curlx_strtoofft(str, &endptr, 0);
386 if((*val == CURL_OFF_T_MAX || *val == CURL_OFF_T_MIN) && (ERRNO == ERANGE))
387 return PARAM_BAD_NUMERIC;
389 *val = strtol(str, &endptr, 0);
390 if((*val == LONG_MIN || *val == LONG_MAX) && ERRNO == ERANGE)
391 return PARAM_BAD_NUMERIC;
393 if((endptr != str) && (endptr == str + strlen(str)))
396 return PARAM_BAD_NUMERIC;
399 static CURLcode checkpasswd(const char *kind, /* for what purpose */
400 const size_t i, /* operation index */
401 const bool last, /* TRUE if last operation */
402 char **userpwd) /* pointer to allocated string */
410 /* Attempt to find the password separator */
411 psep = strchr(*userpwd, ':');
413 /* Attempt to find the options separator */
414 osep = strchr(*userpwd, ';');
416 if(!psep && **userpwd != ';') {
417 /* no password present, prompt for one */
418 char passwd[256] = "";
421 size_t userlen = strlen(*userpwd);
427 /* build a nice-looking prompt */
429 curlx_msnprintf(prompt, sizeof(prompt),
430 "Enter %s password for user '%s':",
433 curlx_msnprintf(prompt, sizeof(prompt),
434 "Enter %s password for user '%s' on URL #%"
435 CURL_FORMAT_CURL_OFF_TU ":",
436 kind, *userpwd, (curl_off_t) (i + 1));
439 getpass_r(prompt, passwd, sizeof(passwd));
440 passwdlen = strlen(passwd);
445 /* extend the allocated memory area to fit the password too */
446 passptr = realloc(*userpwd,
447 passwdlen + 1 + /* an extra for the colon */
448 userlen + 1); /* an extra for the zero */
450 return CURLE_OUT_OF_MEMORY;
452 /* append the password separated with a colon */
453 passptr[userlen] = ':';
454 memcpy(&passptr[userlen+1], passwd, passwdlen+1);
461 ParameterError add2list(struct curl_slist **list, const char *ptr)
463 struct curl_slist *newlist = curl_slist_append(*list, ptr);
472 int ftpfilemethod(struct OperationConfig *config, const char *str)
474 if(curl_strequal("singlecwd", str))
475 return CURLFTPMETHOD_SINGLECWD;
476 if(curl_strequal("nocwd", str))
477 return CURLFTPMETHOD_NOCWD;
478 if(curl_strequal("multicwd", str))
479 return CURLFTPMETHOD_MULTICWD;
481 warnf(config->global, "unrecognized ftp file method '%s', using default\n",
484 return CURLFTPMETHOD_MULTICWD;
487 int ftpcccmethod(struct OperationConfig *config, const char *str)
489 if(curl_strequal("passive", str))
490 return CURLFTPSSL_CCC_PASSIVE;
491 if(curl_strequal("active", str))
492 return CURLFTPSSL_CCC_ACTIVE;
494 warnf(config->global, "unrecognized ftp CCC method '%s', using default\n",
497 return CURLFTPSSL_CCC_PASSIVE;
500 long delegation(struct OperationConfig *config, char *str)
502 if(curl_strequal("none", str))
503 return CURLGSSAPI_DELEGATION_NONE;
504 if(curl_strequal("policy", str))
505 return CURLGSSAPI_DELEGATION_POLICY_FLAG;
506 if(curl_strequal("always", str))
507 return CURLGSSAPI_DELEGATION_FLAG;
509 warnf(config->global, "unrecognized delegation method '%s', using none\n",
512 return CURLGSSAPI_DELEGATION_NONE;
516 * my_useragent: returns allocated string with default user agent
518 static char *my_useragent(void)
520 return strdup(CURL_NAME "/" CURL_VERSION);
523 CURLcode get_args(struct OperationConfig *config, const size_t i)
525 CURLcode result = CURLE_OK;
526 bool last = (config->next ? FALSE : TRUE);
528 /* Check we have a password for the given host user */
529 if(config->userpwd && !config->oauth_bearer) {
530 result = checkpasswd("host", i, last, &config->userpwd);
535 /* Check we have a password for the given proxy user */
536 if(config->proxyuserpwd) {
537 result = checkpasswd("proxy", i, last, &config->proxyuserpwd);
542 /* Check we have a user agent */
543 if(!config->useragent) {
544 config->useragent = my_useragent();
545 if(!config->useragent) {
546 helpf(config->global->errors, "out of memory\n");
547 result = CURLE_OUT_OF_MEMORY;