1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-03 16:37:45 +02:00

password prompt fixes

This commit is contained in:
Jfreegman 2014-09-15 04:13:12 -04:00
parent f405ae8b42
commit a2a23b3932
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63

View File

@ -474,6 +474,8 @@ static void load_friendlist(Tox *m)
/* return length of password on success, 0 on failure */ /* return length of password on success, 0 on failure */
static int password_prompt(char *buf, int size) static int password_prompt(char *buf, int size)
{ {
buf[0] = '\0';
/* disable terminal echo */ /* disable terminal echo */
struct termios oflags, nflags; struct termios oflags, nflags;
tcgetattr(fileno(stdin), &oflags); tcgetattr(fileno(stdin), &oflags);
@ -484,16 +486,28 @@ static int password_prompt(char *buf, int size)
if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0)
return 0; return 0;
fgets(buf, size, stdin); const char *p = fgets(buf, size, stdin);
int len = strlen(buf); int len = strlen(buf);
if (len > 0)
buf[--len] = '\0'; /* rm newline char */
/* re-enable terminal echo */ /* re-enable terminal echo */
if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) tcsetattr(fileno(stdin), TCSANOW, &oflags);
if (p == NULL)
return 0; return 0;
if (len <= 1)
return 0;
/* eat overflowed stdin and return error */
if (buf[--len] != '\n') {
int ch;
while ((ch = getchar()) != '\n' && ch > 0)
;
return 0;
}
buf[len] = '\0';
return len; return len;
} }
@ -613,7 +627,7 @@ static void load_data(Tox *m, char *path)
if (user_password.data_is_encrypted) { if (user_password.data_is_encrypted) {
system("clear"); system("clear");
printf("Enter password (q to quit)\n"); printf("Enter password (q to quit) ");
while (true) { while (true) {
pwlen = password_prompt(user_password.pass, sizeof(user_password.pass)); pwlen = password_prompt(user_password.pass, sizeof(user_password.pass));
@ -625,13 +639,11 @@ static void load_data(Tox *m, char *path)
if (tox_encrypted_load(m, (uint8_t *) buf, len, (uint8_t *) user_password.pass, pwlen) == 0) { if (tox_encrypted_load(m, (uint8_t *) buf, len, (uint8_t *) user_password.pass, pwlen) == 0) {
break; break;
} else { } else {
sleep(2);
system("clear"); system("clear");
printf("Invalid password. Try again.\n"); sleep(1);
printf("Invalid password. Try again. ");
} }
} }
system("clear");
} else { } else {
tox_load(m, (uint8_t *) buf, len); tox_load(m, (uint8_t *) buf, len);
} }