1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 13:53:26 +01:00

Merge pull request #379 from FreakyPenguin/password_eval

Add password_eval option to skip password prompt
This commit is contained in:
JFreegman 2016-01-19 18:31:04 -05:00
commit 93835f0455
4 changed files with 70 additions and 3 deletions

View File

@ -123,6 +123,10 @@ OPTIONS
*chatlogs_path*;; *chatlogs_path*;;
Default path for chatlogs. String value. Absolute path for chatlog files. 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*:: *sounds*::
Configuration related to notification sounds. Configuration related to notification sounds.
Special value "silent" can be used to disable a specific notification. + 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* download_path;
const char* chatlogs_path; const char* chatlogs_path;
const char* avatar_path; const char* avatar_path;
const char* password_eval;
} tox_strings = { } tox_strings = {
"tox", "tox",
"download_path", "download_path",
"chatlogs_path", "chatlogs_path",
"avatar_path", "avatar_path",
"password_eval",
}; };
static void tox_defaults(struct user_settings* settings) 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->download_path, "");
strcpy(settings->chatlogs_path, ""); strcpy(settings->chatlogs_path, "");
strcpy(settings->avatar_path, ""); strcpy(settings->avatar_path, "");
strcpy(settings->password_eval, "");
} }
#ifdef AUDIO #ifdef AUDIO
@ -359,6 +362,14 @@ int settings_load(struct user_settings *s, const char *patharg)
if (len >= sizeof(s->avatar_path)) if (len >= sizeof(s->avatar_path))
s->avatar_path[0] = '\0'; 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 */ /* keys */

View File

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

View File

@ -39,6 +39,7 @@
#include <unistd.h> #include <unistd.h>
#include <limits.h> #include <limits.h>
#include <termios.h> #include <termios.h>
#include <ctype.h>
#include <tox/tox.h> #include <tox/tox.h>
#include <tox/toxencryptsave.h> #include <tox/toxencryptsave.h>
@ -472,6 +473,45 @@ static int password_prompt(char *buf, int size)
return len; 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 */ /* Ask user if they would like to encrypt the data file and set password */
static void first_time_encrypt(const char *msg) 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; user_password.data_is_encrypted = true;
size_t pwlen = 0; size_t pwlen = 0;
int pweval = user_settings->password_eval[0];
if (!pweval) {
system("clear"); // TODO: is this portable? system("clear"); // TODO: is this portable?
printf("Enter password (q to quit) "); printf("Enter password (q to quit) ");
}
size_t plain_len = len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH; size_t plain_len = len - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
char plain[plain_len]; char plain[plain_len];
while (true) { while (true) {
if (pweval) {
pwlen = password_eval(user_password.pass, sizeof(user_password.pass));
} else {
pwlen = password_prompt(user_password.pass, sizeof(user_password.pass)); pwlen = password_prompt(user_password.pass, sizeof(user_password.pass));
}
user_password.len = pwlen; user_password.len = pwlen;
if (strcasecmp(user_password.pass, "q") == 0) { 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"); system("clear");
sleep(1); sleep(1);
printf("Invalid password. Try again. "); printf("Invalid password. Try again. ");
pweval = 0;
continue; continue;
} }
@ -736,6 +784,7 @@ static Tox *load_tox(char *data_path, struct Tox_Options *tox_opts, TOX_ERR_NEW
system("clear"); system("clear");
sleep(1); sleep(1);
printf("Invalid password. Try again. "); printf("Invalid password. Try again. ");
pweval = 0;
} else { } else {
fclose(fp); fclose(fp);
exit_toxic_err("tox_pass_decrypt() failed", pwerr); exit_toxic_err("tox_pass_decrypt() failed", pwerr);