1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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 if((ptr = strchr(buffer, '\r')) != NULL)
71 if((ptr = strchr(buffer, '\n')) != NULL)
73 buflen = strlen(buffer);
74 if((ptr = realloc(string, stringlen+buflen+1)) == NULL) {
75 Curl_safefree(string);
79 strcpy(string+stringlen, buffer);
87 ParameterError file2memory(char **bufp, size_t *size, FILE *file)
97 if(!buffer || (alloc == nused)) {
98 /* size_t overflow detection for huge files */
99 if(alloc+1 > ((size_t)-1)/2) {
100 Curl_safefree(buffer);
104 /* allocate an extra char, reserved space, for null termination */
105 if((newbuf = realloc(buffer, alloc+1)) == NULL) {
106 Curl_safefree(buffer);
111 nread = fread(buffer+nused, 1, alloc-nused, file);
114 /* null terminate the buffer in case it's used as a string later */
115 buffer[nused] = '\0';
116 /* free trailing slack space, if possible */
118 if((newbuf = realloc(buffer, nused+1)) == NULL) {
119 Curl_safefree(buffer);
124 /* discard buffer if nothing was read */
126 Curl_safefree(buffer); /* no string */
134 void cleanarg(char *str)
136 #ifdef HAVE_WRITABLE_ARGV
137 /* now that GetStr has copied the contents of nextarg, wipe the next
138 * argument out so that the username:password isn't displayed in the
139 * system process list */
141 size_t len = strlen(str);
142 memset(str, ' ', len);
150 * Parse the string and write the long in the given address. Return PARAM_OK
151 * on success, otherwise a parameter specific error enum.
153 * Since this function gets called with the 'nextarg' pointer from within the
154 * getparameter a lot, we must check it for NULL before accessing the str
158 ParameterError str2num(long *val, const char *str)
162 long num = strtol(str, &endptr, 10);
163 if((endptr != str) && (endptr == str + strlen(str))) {
165 return PARAM_OK; /* Ok */
168 return PARAM_BAD_NUMERIC; /* badness */
172 * Parse the string and write the long in the given address. Return PARAM_OK
173 * on success, otherwise a parameter error enum. ONLY ACCEPTS POSITIVE NUMBERS!
175 * Since this function gets called with the 'nextarg' pointer from within the
176 * getparameter a lot, we must check it for NULL before accessing the str
180 ParameterError str2unum(long *val, const char *str)
182 ParameterError result = str2num(val, str);
183 if(result != PARAM_OK)
186 return PARAM_NEGATIVE_NUMERIC;
192 * Parse the string and write the double in the given address. Return PARAM_OK
193 * on success, otherwise a parameter specific error enum.
195 * Since this function gets called with the 'nextarg' pointer from within the
196 * getparameter a lot, we must check it for NULL before accessing the str
200 ParameterError str2double(double *val, const char *str)
204 double num = strtod(str, &endptr);
205 if((endptr != str) && (endptr == str + strlen(str))) {
207 return PARAM_OK; /* Ok */
210 return PARAM_BAD_NUMERIC; /* badness */
214 * Parse the string and write the double in the given address. Return PARAM_OK
215 * on success, otherwise a parameter error enum. ONLY ACCEPTS POSITIVE NUMBERS!
217 * Since this function gets called with the 'nextarg' pointer from within the
218 * getparameter a lot, we must check it for NULL before accessing the str
222 ParameterError str2udouble(double *val, const char *str)
224 ParameterError result = str2double(val, str);
225 if(result != PARAM_OK)
228 return PARAM_NEGATIVE_NUMERIC;
234 * Parse the string and modify the long in the given address. Return
235 * non-zero on failure, zero on success.
237 * The string is a list of protocols
239 * Since this function gets called with the 'nextarg' pointer from within the
240 * getparameter a lot, we must check it for NULL before accessing the str
244 long proto2num(struct OperationConfig *config, long *val, const char *str)
247 const char *sep = ",";
250 static struct sprotos {
254 { "all", CURLPROTO_ALL },
255 { "http", CURLPROTO_HTTP },
256 { "https", CURLPROTO_HTTPS },
257 { "ftp", CURLPROTO_FTP },
258 { "ftps", CURLPROTO_FTPS },
259 { "scp", CURLPROTO_SCP },
260 { "sftp", CURLPROTO_SFTP },
261 { "telnet", CURLPROTO_TELNET },
262 { "ldap", CURLPROTO_LDAP },
263 { "ldaps", CURLPROTO_LDAPS },
264 { "dict", CURLPROTO_DICT },
265 { "file", CURLPROTO_FILE },
266 { "tftp", CURLPROTO_TFTP },
267 { "imap", CURLPROTO_IMAP },
268 { "imaps", CURLPROTO_IMAPS },
269 { "pop3", CURLPROTO_POP3 },
270 { "pop3s", CURLPROTO_POP3S },
271 { "smtp", CURLPROTO_SMTP },
272 { "smtps", CURLPROTO_SMTPS },
273 { "rtsp", CURLPROTO_RTSP },
274 { "gopher", CURLPROTO_GOPHER },
281 buffer = strdup(str); /* because strtok corrupts it */
285 for(token = strtok(buffer, sep);
287 token = strtok(NULL, sep)) {
288 enum e_action { allow, deny, set } action = allow;
290 struct sprotos const *pp;
292 /* Process token modifiers */
293 while(!ISALNUM(*token)) { /* may be NULL if token is all modifiers */
304 default: /* Includes case of terminating NULL */
305 Curl_safefree(buffer);
310 for(pp=protos; pp->name; pp++) {
311 if(curlx_raw_equal(token, pp->name)) {
327 if(!(pp->name)) { /* unknown protocol */
328 /* If they have specified only this protocol, we say treat it as
329 if no protocols are allowed */
332 warnf(config, "unrecognized protocol '%s'\n", token);
335 Curl_safefree(buffer);
340 * Parses the given string looking for an offset (which may be a
341 * larger-than-integer value). The offset CANNOT be negative!
343 * @param val the offset to populate
344 * @param str the buffer containing the offset
345 * @return PARAM_OK if successful, a parameter specific error enum if failure.
347 ParameterError str2offset(curl_off_t *val, const char *str)
351 /* offsets aren't negative, this indicates weird input */
352 return PARAM_NEGATIVE_NUMERIC;
354 #if(CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
355 *val = curlx_strtoofft(str, &endptr, 0);
356 if((*val == CURL_OFF_T_MAX || *val == CURL_OFF_T_MIN) && (ERRNO == ERANGE))
357 return PARAM_BAD_NUMERIC;
359 *val = strtol(str, &endptr, 0);
360 if((*val == LONG_MIN || *val == LONG_MAX) && ERRNO == ERANGE)
361 return PARAM_BAD_NUMERIC;
363 if((endptr != str) && (endptr == str + strlen(str)))
366 return PARAM_BAD_NUMERIC;
369 static CURLcode checkpasswd(const char *kind, /* for what purpose */
370 const size_t i, /* operation index */
371 const bool last, /* TRUE if last operation */
372 char **userpwd) /* pointer to allocated string */
380 /* Attempt to find the password separator */
381 psep = strchr(*userpwd, ':');
383 /* Attempt to find the options separator */
384 osep = strchr(*userpwd, ';');
386 if(!psep && **userpwd != ';') {
387 /* no password present, prompt for one */
388 char passwd[256] = "";
391 size_t userlen = strlen(*userpwd);
397 /* build a nice-looking prompt */
399 curlx_msnprintf(prompt, sizeof(prompt),
400 "Enter %s password for user '%s':",
403 curlx_msnprintf(prompt, sizeof(prompt),
404 "Enter %s password for user '%s' on URL #%"
405 CURL_FORMAT_CURL_OFF_TU ":",
406 kind, *userpwd, i + 1);
409 getpass_r(prompt, passwd, sizeof(passwd));
410 passwdlen = strlen(passwd);
415 /* extend the allocated memory area to fit the password too */
416 passptr = realloc(*userpwd,
417 passwdlen + 1 + /* an extra for the colon */
418 userlen + 1); /* an extra for the zero */
420 return CURLE_OUT_OF_MEMORY;
422 /* append the password separated with a colon */
423 passptr[userlen] = ':';
424 memcpy(&passptr[userlen+1], passwd, passwdlen+1);
431 ParameterError add2list(struct curl_slist **list, const char *ptr)
433 struct curl_slist *newlist = curl_slist_append(*list, ptr);
442 int ftpfilemethod(struct OperationConfig *config, const char *str)
444 if(curlx_raw_equal("singlecwd", str))
445 return CURLFTPMETHOD_SINGLECWD;
446 if(curlx_raw_equal("nocwd", str))
447 return CURLFTPMETHOD_NOCWD;
448 if(curlx_raw_equal("multicwd", str))
449 return CURLFTPMETHOD_MULTICWD;
450 warnf(config, "unrecognized ftp file method '%s', using default\n", str);
451 return CURLFTPMETHOD_MULTICWD;
454 int ftpcccmethod(struct OperationConfig *config, const char *str)
456 if(curlx_raw_equal("passive", str))
457 return CURLFTPSSL_CCC_PASSIVE;
458 if(curlx_raw_equal("active", str))
459 return CURLFTPSSL_CCC_ACTIVE;
460 warnf(config, "unrecognized ftp CCC method '%s', using default\n", str);
461 return CURLFTPSSL_CCC_PASSIVE;
464 long delegation(struct OperationConfig *config, char *str)
466 if(curlx_raw_equal("none", str))
467 return CURLGSSAPI_DELEGATION_NONE;
468 if(curlx_raw_equal("policy", str))
469 return CURLGSSAPI_DELEGATION_POLICY_FLAG;
470 if(curlx_raw_equal("always", str))
471 return CURLGSSAPI_DELEGATION_FLAG;
472 warnf(config, "unrecognized delegation method '%s', using none\n", str);
473 return CURLGSSAPI_DELEGATION_NONE;
477 * my_useragent: returns allocated string with default user agent
479 static char *my_useragent(void)
481 return strdup(CURL_NAME "/" CURL_VERSION);
484 CURLcode get_args(struct OperationConfig *config, const size_t i)
486 CURLcode result = CURLE_OK;
487 bool last = (config->next ? FALSE : TRUE);
489 /* Check we have a password for the given host user */
490 if(config->userpwd && !config->xoauth2_bearer) {
491 result = checkpasswd("host", i, last, &config->userpwd);
496 /* Check we have a password for the given proxy user */
497 if(config->proxyuserpwd) {
498 result = checkpasswd("proxy", i, last, &config->proxyuserpwd);
503 /* Check we have a user agent */
504 if(!config->useragent) {
505 config->useragent = my_useragent();
506 if(!config->useragent) {
507 helpf(config->global->errors, "out of memory\n");
508 result = CURLE_OUT_OF_MEMORY;