1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-12-22 22:13:25 +01:00

Add password_eval option to skip password prompt

Runs a command and uses its output as the password. This can be used for
getting the password from a password manager such as pass.
This commit is contained in:
Antoine Kaufmann 2016-01-19 13:45:52 -08:00
parent 74416b4b58
commit ac6d8ff89c
4 changed files with 70 additions and 3 deletions

View File

@ -123,6 +123,10 @@ OPTIONS
*chatlogs_path*;;
Default path for chatlogs. String value. Absolute path for chatlog files.
*password_eval*;;
Replace password prompt by running this command and using its output as
the password.
*sounds*::
Configuration related to notification sounds.
Special value "silent" can be used to disable a specific notification. +

View File

@ -158,11 +158,13 @@ static const struct tox_strings {
const char* download_path;
const char* chatlogs_path;
const char* avatar_path;
const char* password_eval;
} tox_strings = {
"tox",
"download_path",
"chatlogs_path",
"avatar_path",
"password_eval",
};
static void tox_defaults(struct user_settings* settings)
@ -170,6 +172,7 @@ static void tox_defaults(struct user_settings* settings)
strcpy(settings->download_path, "");
strcpy(settings->chatlogs_path, "");
strcpy(settings->avatar_path, "");
strcpy(settings->password_eval, "");
}
#ifdef AUDIO
@ -359,6 +362,14 @@ int settings_load(struct user_settings *s, const char *patharg)
if (len >= sizeof(s->avatar_path))
s->avatar_path[0] = '\0';
}
if ( config_setting_lookup_string(setting, tox_strings.password_eval, &str) ) {
snprintf(s->password_eval, sizeof(s->password_eval), "%s", str);
int len = strlen(str);
if (len >= sizeof(s->password_eval))
s->password_eval[0] = '\0';
}
}
/* keys */

View File

@ -30,6 +30,8 @@
/* Represents line_* hints max strlen */
#define LINE_HINT_MAX 3
#define PASSWORD_EVAL_MAX 512
/* holds user setting values */
struct user_settings {
int autolog; /* boolean */
@ -53,6 +55,7 @@ struct user_settings {
char download_path[PATH_MAX];
char chatlogs_path[PATH_MAX];
char avatar_path[PATH_MAX];
char password_eval[PASSWORD_EVAL_MAX];
int key_next_tab;
int key_prev_tab;

View File

@ -39,6 +39,7 @@
#include <unistd.h>
#include <limits.h>
#include <termios.h>
#include <ctype.h>
#include <tox/tox.h>
#include <tox/toxencryptsave.h>
@ -472,6 +473,45 @@ static int password_prompt(char *buf, int size)
return len;
}
/* Get the password from the eval command.
* return length of password on success, 0 on failure
*/
static int password_eval(char *buf, int size)
{
buf[0] = '\0';
/* Run password_eval command */
FILE *f = popen(user_settings->password_eval, "r");
if (f == NULL) {
fprintf(stderr, "Executing password_eval failed\n");
return 0;
}
/* Get output from command */
char *ret = fgets(buf, size, f);
if (ret == NULL) {
fprintf(stderr, "Reading password from password_eval command failed\n");
pclose(f);
return 0;
}
/* Get exit status */
int status = pclose(f);
if (status != 0) {
fprintf(stderr, "password_eval command returned error %d\n", status);
return 0;
}
/* Removez whitespace or \n at end */
int i, len = strlen(buf);
for (i = len - 1; i > 0 && isspace(buf[i]); i--) {
buf[i] = 0;
len--;
}
return len;
}
/* Ask user if they would like to encrypt the data file and set password */
static void first_time_encrypt(const char *msg)
{
@ -693,14 +733,21 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, TOX_ERR_NEW
user_password.data_is_encrypted = true;
size_t pwlen = 0;
system("clear"); // TODO: is this portable?
printf("Enter password (q to quit) ");
int pweval = user_settings->password_eval[0];
if (!pweval) {
system("clear"); // TODO: is this portable?
printf("Enter password (q to quit) ");
}
size_t plain_len = len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
char plain[plain_len];
while (true) {
pwlen = password_prompt(user_password.pass, sizeof(user_password.pass));
if (pweval) {
pwlen = password_eval(user_password.pass, sizeof(user_password.pass));
} else {
pwlen = password_prompt(user_password.pass, sizeof(user_password.pass));
}
user_password.len = pwlen;
if (strcasecmp(user_password.pass, "q") == 0) {
@ -712,6 +759,7 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, TOX_ERR_NEW
system("clear");
sleep(1);
printf("Invalid password. Try again. ");
pweval = 0;
continue;
}
@ -736,6 +784,7 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, TOX_ERR_NEW
system("clear");
sleep(1);
printf("Invalid password. Try again. ");
pweval = 0;
} else {
fclose(fp);
exit_toxic_err("tox_pass_decrypt() failed", pwerr);