Commit 8823679e authored by Daniel Stenberg's avatar Daniel Stenberg
Browse files

made the password prompt nicer

parent 722ece40
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -1051,24 +1051,34 @@ static void cleanarg(char *str)
#endif
}

static void checkpasswd(const char *prompt, char **userpwd)
static void checkpasswd(const char *kind, /* for what purpose */
                        char **userpwd) /* pointer to allocated string */
{
  char *ptr = strchr(*userpwd, ':');
  if(!ptr) {
    /* no password present, prompt for one */
    char passwd[256]="";
    char prompt[256];
    int passwdlen;
    int userlen = strlen(*userpwd);
    char *ptr;

    /* build a nice-looking prompt */
    curl_msnprintf(prompt, sizeof(prompt),
                   "Enter %s password for user '%s':",
                   kind, *userpwd);

    /* get password */
    getpass_r(prompt, passwd, sizeof(passwd));
    passwdlen = strlen(passwd);

    /* extend the allocated memory are to fit the password too */
    ptr = realloc(*userpwd,
                  passwdlen + 1 + /* an extra for the colon */
                  userlen + 1);   /* an extra for the zero */

    if(ptr) {
      /* append the password separated with a colon */
      ptr[userlen]=':';
      memcpy(&ptr[userlen+1], passwd, passwdlen+1);
      *userpwd = ptr;
@@ -1834,13 +1844,13 @@ static ParameterError getparameter(char *flag, /* f or -long-flag */
      /* user:password  */
      GetStr(&config->userpwd, nextarg);
      cleanarg(nextarg);
      checkpasswd("Enter host password:", &config->userpwd);
      checkpasswd("host", &config->userpwd);
      break;
    case 'U':
      /* Proxy user:password  */
      GetStr(&config->proxyuserpwd, nextarg);
      cleanarg(nextarg);
      checkpasswd("Enter proxy password:", &config->proxyuserpwd);
      checkpasswd("proxy", &config->proxyuserpwd);
      break;
    case 'v':
      config->conf ^= CONF_VERBOSE; /* talk a lot */