Home | History | Annotate | Download | only in src
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel (at) haxx.se>, et al.
      9  *
     10  * This software is licensed as described in the file COPYING, which
     11  * you should have received as part of this distribution. The terms
     12  * are also available at http://curl.haxx.se/docs/copyright.html.
     13  *
     14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     15  * copies of the Software, and permit persons to whom the Software is
     16  * furnished to do so, under the terms of the COPYING file.
     17  *
     18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     19  * KIND, either express or implied.
     20  *
     21  ***************************************************************************/
     22 #include "tool_setup.h"
     23 
     24 #include "rawstr.h"
     25 
     26 #define ENABLE_CURLX_PRINTF
     27 /* use our own printf() functions */
     28 #include "curlx.h"
     29 
     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"
     37 
     38 #include "memdebug.h" /* keep this as LAST include */
     39 
     40 struct getout *new_getout(struct OperationConfig *config)
     41 {
     42   struct getout *node = calloc(1, sizeof(struct getout));
     43   struct getout *last = config->url_last;
     44   if(node) {
     45     /* append this new node last in the list */
     46     if(last)
     47       last->next = node;
     48     else
     49       config->url_list = node; /* first node */
     50 
     51     /* move the last pointer */
     52     config->url_last = node;
     53 
     54     node->flags = config->default_node_flags;
     55   }
     56   return node;
     57 }
     58 
     59 ParameterError file2string(char **bufp, FILE *file)
     60 {
     61   char buffer[256];
     62   char *ptr;
     63   char *string = NULL;
     64   size_t stringlen = 0;
     65   size_t buflen;
     66 
     67   if(file) {
     68     while(fgets(buffer, sizeof(buffer), file)) {
     69       if((ptr = strchr(buffer, '\r')) != NULL)
     70         *ptr = '\0';
     71       if((ptr = strchr(buffer, '\n')) != NULL)
     72         *ptr = '\0';
     73       buflen = strlen(buffer);
     74       if((ptr = realloc(string, stringlen+buflen+1)) == NULL) {
     75         Curl_safefree(string);
     76         return PARAM_NO_MEM;
     77       }
     78       string = ptr;
     79       strcpy(string+stringlen, buffer);
     80       stringlen += buflen;
     81     }
     82   }
     83   *bufp = string;
     84   return PARAM_OK;
     85 }
     86 
     87 ParameterError file2memory(char **bufp, size_t *size, FILE *file)
     88 {
     89   char *newbuf;
     90   char *buffer = NULL;
     91   size_t alloc = 512;
     92   size_t nused = 0;
     93   size_t nread;
     94 
     95   if(file) {
     96     do {
     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);
    101           return PARAM_NO_MEM;
    102         }
    103         alloc *= 2;
    104         /* allocate an extra char, reserved space, for null termination */
    105         if((newbuf = realloc(buffer, alloc+1)) == NULL) {
    106           Curl_safefree(buffer);
    107           return PARAM_NO_MEM;
    108         }
    109         buffer = newbuf;
    110       }
    111       nread = fread(buffer+nused, 1, alloc-nused, file);
    112       nused += nread;
    113     } while(nread);
    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 */
    117     if(alloc != nused) {
    118       if((newbuf = realloc(buffer, nused+1)) == NULL) {
    119         Curl_safefree(buffer);
    120         return PARAM_NO_MEM;
    121       }
    122       buffer = newbuf;
    123     }
    124     /* discard buffer if nothing was read */
    125     if(!nused) {
    126       Curl_safefree(buffer); /* no string */
    127     }
    128   }
    129   *size = nused;
    130   *bufp = buffer;
    131   return PARAM_OK;
    132 }
    133 
    134 void cleanarg(char *str)
    135 {
    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 */
    140   if(str) {
    141     size_t len = strlen(str);
    142     memset(str, ' ', len);
    143   }
    144 #else
    145   (void)str;
    146 #endif
    147 }
    148 
    149 /*
    150  * Parse the string and write the long in the given address. Return PARAM_OK
    151  * on success, otherwise a parameter specific error enum.
    152  *
    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
    155  * data.
    156  */
    157 
    158 ParameterError str2num(long *val, const char *str)
    159 {
    160   if(str) {
    161     char *endptr;
    162     long num = strtol(str, &endptr, 10);
    163     if((endptr != str) && (endptr == str + strlen(str))) {
    164       *val = num;
    165       return PARAM_OK;  /* Ok */
    166     }
    167   }
    168   return PARAM_BAD_NUMERIC; /* badness */
    169 }
    170 
    171 /*
    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!
    174  *
    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
    177  * data.
    178  */
    179 
    180 ParameterError str2unum(long *val, const char *str)
    181 {
    182   ParameterError result = str2num(val, str);
    183   if(result != PARAM_OK)
    184     return result;
    185   if(*val < 0)
    186     return PARAM_NEGATIVE_NUMERIC;
    187 
    188   return PARAM_OK;
    189 }
    190 
    191 /*
    192  * Parse the string and write the double in the given address. Return PARAM_OK
    193  * on success, otherwise a parameter specific error enum.
    194  *
    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
    197  * data.
    198  */
    199 
    200 ParameterError str2double(double *val, const char *str)
    201 {
    202   if(str) {
    203     char *endptr;
    204     double num = strtod(str, &endptr);
    205     if((endptr != str) && (endptr == str + strlen(str))) {
    206       *val = num;
    207       return PARAM_OK;  /* Ok */
    208     }
    209   }
    210   return PARAM_BAD_NUMERIC; /* badness */
    211 }
    212 
    213 /*
    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!
    216  *
    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
    219  * data.
    220  */
    221 
    222 ParameterError str2udouble(double *val, const char *str)
    223 {
    224   ParameterError result = str2double(val, str);
    225   if(result != PARAM_OK)
    226     return result;
    227   if(*val < 0)
    228     return PARAM_NEGATIVE_NUMERIC;
    229 
    230   return PARAM_OK;
    231 }
    232 
    233 /*
    234  * Parse the string and modify the long in the given address. Return
    235  * non-zero on failure, zero on success.
    236  *
    237  * The string is a list of protocols
    238  *
    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
    241  * data.
    242  */
    243 
    244 long proto2num(struct OperationConfig *config, long *val, const char *str)
    245 {
    246   char *buffer;
    247   const char *sep = ",";
    248   char *token;
    249 
    250   static struct sprotos {
    251     const char *name;
    252     long bit;
    253   } const protos[] = {
    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 },
    275     { "smb", CURLPROTO_SMB },
    276     { "smbs", CURLPROTO_SMBS },
    277     { NULL, 0 }
    278   };
    279 
    280   if(!str)
    281     return 1;
    282 
    283   buffer = strdup(str); /* because strtok corrupts it */
    284   if(!buffer)
    285     return 1;
    286 
    287   for(token = strtok(buffer, sep);
    288       token;
    289       token = strtok(NULL, sep)) {
    290     enum e_action { allow, deny, set } action = allow;
    291 
    292     struct sprotos const *pp;
    293 
    294     /* Process token modifiers */
    295     while(!ISALNUM(*token)) { /* may be NULL if token is all modifiers */
    296       switch (*token++) {
    297       case '=':
    298         action = set;
    299         break;
    300       case '-':
    301         action = deny;
    302         break;
    303       case '+':
    304         action = allow;
    305         break;
    306       default: /* Includes case of terminating NULL */
    307         Curl_safefree(buffer);
    308         return 1;
    309       }
    310     }
    311 
    312     for(pp=protos; pp->name; pp++) {
    313       if(curlx_raw_equal(token, pp->name)) {
    314         switch (action) {
    315         case deny:
    316           *val &= ~(pp->bit);
    317           break;
    318         case allow:
    319           *val |= pp->bit;
    320           break;
    321         case set:
    322           *val = pp->bit;
    323           break;
    324         }
    325         break;
    326       }
    327     }
    328 
    329     if(!(pp->name)) { /* unknown protocol */
    330       /* If they have specified only this protocol, we say treat it as
    331          if no protocols are allowed */
    332       if(action == set)
    333         *val = 0;
    334       warnf(config->global, "unrecognized protocol '%s'\n", token);
    335     }
    336   }
    337   Curl_safefree(buffer);
    338   return 0;
    339 }
    340 
    341 /**
    342  * Parses the given string looking for an offset (which may be a
    343  * larger-than-integer value). The offset CANNOT be negative!
    344  *
    345  * @param val  the offset to populate
    346  * @param str  the buffer containing the offset
    347  * @return PARAM_OK if successful, a parameter specific error enum if failure.
    348  */
    349 ParameterError str2offset(curl_off_t *val, const char *str)
    350 {
    351   char *endptr;
    352   if(str[0] == '-')
    353     /* offsets aren't negative, this indicates weird input */
    354     return PARAM_NEGATIVE_NUMERIC;
    355 
    356 #if(CURL_SIZEOF_CURL_OFF_T > CURL_SIZEOF_LONG)
    357   *val = curlx_strtoofft(str, &endptr, 0);
    358   if((*val == CURL_OFF_T_MAX || *val == CURL_OFF_T_MIN) && (ERRNO == ERANGE))
    359     return PARAM_BAD_NUMERIC;
    360 #else
    361   *val = strtol(str, &endptr, 0);
    362   if((*val == LONG_MIN || *val == LONG_MAX) && ERRNO == ERANGE)
    363     return PARAM_BAD_NUMERIC;
    364 #endif
    365   if((endptr != str) && (endptr == str + strlen(str)))
    366     return PARAM_OK;
    367 
    368   return PARAM_BAD_NUMERIC;
    369 }
    370 
    371 static CURLcode checkpasswd(const char *kind, /* for what purpose */
    372                             const size_t i,   /* operation index */
    373                             const bool last,  /* TRUE if last operation */
    374                             char **userpwd)   /* pointer to allocated string */
    375 {
    376   char *psep;
    377   char *osep;
    378 
    379   if(!*userpwd)
    380     return CURLE_OK;
    381 
    382   /* Attempt to find the password separator */
    383   psep = strchr(*userpwd, ':');
    384 
    385   /* Attempt to find the options separator */
    386   osep = strchr(*userpwd, ';');
    387 
    388   if(!psep && **userpwd != ';') {
    389     /* no password present, prompt for one */
    390     char passwd[256] = "";
    391     char prompt[256];
    392     size_t passwdlen;
    393     size_t userlen = strlen(*userpwd);
    394     char *passptr;
    395 
    396     if(osep)
    397       *osep = '\0';
    398 
    399     /* build a nice-looking prompt */
    400     if(!i && last)
    401       curlx_msnprintf(prompt, sizeof(prompt),
    402                       "Enter %s password for user '%s':",
    403                       kind, *userpwd);
    404     else
    405       curlx_msnprintf(prompt, sizeof(prompt),
    406                       "Enter %s password for user '%s' on URL #%"
    407                       CURL_FORMAT_CURL_OFF_TU ":",
    408                       kind, *userpwd, i + 1);
    409 
    410     /* get password */
    411     getpass_r(prompt, passwd, sizeof(passwd));
    412     passwdlen = strlen(passwd);
    413 
    414     if(osep)
    415       *osep = ';';
    416 
    417     /* extend the allocated memory area to fit the password too */
    418     passptr = realloc(*userpwd,
    419                       passwdlen + 1 + /* an extra for the colon */
    420                       userlen + 1);   /* an extra for the zero */
    421     if(!passptr)
    422       return CURLE_OUT_OF_MEMORY;
    423 
    424     /* append the password separated with a colon */
    425     passptr[userlen] = ':';
    426     memcpy(&passptr[userlen+1], passwd, passwdlen+1);
    427     *userpwd = passptr;
    428   }
    429 
    430   return CURLE_OK;
    431 }
    432 
    433 ParameterError add2list(struct curl_slist **list, const char *ptr)
    434 {
    435   struct curl_slist *newlist = curl_slist_append(*list, ptr);
    436   if(newlist)
    437     *list = newlist;
    438   else
    439     return PARAM_NO_MEM;
    440 
    441   return PARAM_OK;
    442 }
    443 
    444 int ftpfilemethod(struct OperationConfig *config, const char *str)
    445 {
    446   if(curlx_raw_equal("singlecwd", str))
    447     return CURLFTPMETHOD_SINGLECWD;
    448   if(curlx_raw_equal("nocwd", str))
    449     return CURLFTPMETHOD_NOCWD;
    450   if(curlx_raw_equal("multicwd", str))
    451     return CURLFTPMETHOD_MULTICWD;
    452 
    453   warnf(config->global, "unrecognized ftp file method '%s', using default\n",
    454         str);
    455 
    456   return CURLFTPMETHOD_MULTICWD;
    457 }
    458 
    459 int ftpcccmethod(struct OperationConfig *config, const char *str)
    460 {
    461   if(curlx_raw_equal("passive", str))
    462     return CURLFTPSSL_CCC_PASSIVE;
    463   if(curlx_raw_equal("active", str))
    464     return CURLFTPSSL_CCC_ACTIVE;
    465 
    466   warnf(config->global, "unrecognized ftp CCC method '%s', using default\n",
    467         str);
    468 
    469   return CURLFTPSSL_CCC_PASSIVE;
    470 }
    471 
    472 long delegation(struct OperationConfig *config, char *str)
    473 {
    474   if(curlx_raw_equal("none", str))
    475     return CURLGSSAPI_DELEGATION_NONE;
    476   if(curlx_raw_equal("policy", str))
    477     return CURLGSSAPI_DELEGATION_POLICY_FLAG;
    478   if(curlx_raw_equal("always", str))
    479     return CURLGSSAPI_DELEGATION_FLAG;
    480 
    481   warnf(config->global, "unrecognized delegation method '%s', using none\n",
    482         str);
    483 
    484   return CURLGSSAPI_DELEGATION_NONE;
    485 }
    486 
    487 /*
    488  * my_useragent: returns allocated string with default user agent
    489  */
    490 static char *my_useragent(void)
    491 {
    492   return strdup(CURL_NAME "/" CURL_VERSION);
    493 }
    494 
    495 CURLcode get_args(struct OperationConfig *config, const size_t i)
    496 {
    497   CURLcode result = CURLE_OK;
    498   bool last = (config->next ? FALSE : TRUE);
    499 
    500   /* Check we have a password for the given host user */
    501   if(config->userpwd && !config->xoauth2_bearer) {
    502     result = checkpasswd("host", i, last, &config->userpwd);
    503     if(result)
    504       return result;
    505   }
    506 
    507   /* Check we have a password for the given proxy user */
    508   if(config->proxyuserpwd) {
    509     result = checkpasswd("proxy", i, last, &config->proxyuserpwd);
    510     if(result)
    511       return result;
    512   }
    513 
    514   /* Check we have a user agent */
    515   if(!config->useragent) {
    516     config->useragent = my_useragent();
    517     if(!config->useragent) {
    518       helpf(config->global->errors, "out of memory\n");
    519       result = CURLE_OUT_OF_MEMORY;
    520     }
    521   }
    522 
    523   return result;
    524 }
    525