1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Save PGP passphrase when correct

This commit is contained in:
James Booth 2015-09-01 21:05:17 +01:00
parent 7dc1b9d428
commit 53035f5e4c

View File

@ -61,6 +61,9 @@ static GHashTable *pubkeys;
static gchar *pubsloc; static gchar *pubsloc;
static GKeyFile *pubkeyfile; static GKeyFile *pubkeyfile;
static char *passphrase;
static char *passphrase_attempt;
static Autocomplete key_ac; static Autocomplete key_ac;
static char* _remove_header_footer(char *str, const char * const footer); static char* _remove_header_footer(char *str, const char * const footer);
@ -79,20 +82,27 @@ _p_gpg_free_pubkeyid(ProfPGPPubKeyId *pubkeyid)
static gpgme_error_t * static gpgme_error_t *
_p_gpg_passphrase_cb(void *hook, const char *uid_hint, const char *passphrase_info, int prev_was_bad, int fd) _p_gpg_passphrase_cb(void *hook, const char *uid_hint, const char *passphrase_info, int prev_was_bad, int fd)
{ {
GString *pass_term = g_string_new(""); if (passphrase) {
gpgme_io_writen(fd, passphrase, strlen(passphrase));
} else {
GString *pass_term = g_string_new("");
char *password = ui_ask_pgp_passphrase(uid_hint, prev_was_bad); char *password = ui_ask_pgp_passphrase(uid_hint, prev_was_bad);
if (password) { if (password) {
g_string_append(pass_term, password); g_string_append(pass_term, password);
free(password); free(password);
}
g_string_append(pass_term, "\n");
if (passphrase_attempt) {
free(passphrase_attempt);
}
passphrase_attempt = pass_term->str;
g_string_free(pass_term, FALSE);
gpgme_io_writen(fd, passphrase_attempt, strlen(passphrase_attempt));
} }
g_string_append(pass_term, "\n");
gpgme_io_writen(fd, pass_term->str, pass_term->len);
g_string_free(pass_term, TRUE);
return 0; return 0;
} }
@ -108,6 +118,9 @@ p_gpg_init(void)
key_ac = autocomplete_new(); key_ac = autocomplete_new();
GHashTable *keys = p_gpg_list_keys(); GHashTable *keys = p_gpg_list_keys();
p_gpg_free_keys(keys); p_gpg_free_keys(keys);
passphrase = NULL;
passphrase_attempt = NULL;
} }
void void
@ -128,6 +141,16 @@ p_gpg_close(void)
autocomplete_free(key_ac); autocomplete_free(key_ac);
key_ac = NULL; key_ac = NULL;
if (passphrase) {
free(passphrase);
passphrase = NULL;
}
if (passphrase_attempt) {
free(passphrase_attempt);
passphrase_attempt = NULL;
}
} }
void void
@ -179,8 +202,6 @@ p_gpg_on_connect(const char * const barejid)
return; return;
} }
gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
int i = 0; int i = 0;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
GError *gerr = NULL; GError *gerr = NULL;
@ -228,6 +249,16 @@ p_gpg_on_disconnect(void)
free(pubsloc); free(pubsloc);
pubsloc = NULL; pubsloc = NULL;
if (passphrase) {
free(passphrase);
passphrase = NULL;
}
if (passphrase_attempt) {
free(passphrase_attempt);
passphrase_attempt = NULL;
}
} }
gboolean gboolean
@ -240,8 +271,6 @@ p_gpg_addkey(const char * const jid, const char * const keyid)
return FALSE; return FALSE;
} }
gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
gpgme_key_t key = NULL; gpgme_key_t key = NULL;
error = gpgme_get_key(ctx, keyid, &key, 0); error = gpgme_get_key(ctx, keyid, &key, 0);
gpgme_release(ctx); gpgme_release(ctx);
@ -306,8 +335,6 @@ p_gpg_list_keys(void)
return NULL; return NULL;
} }
gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
error = gpgme_op_keylist_start(ctx, NULL, 0); error = gpgme_op_keylist_start(ctx, NULL, 0);
if (error == GPG_ERR_NO_ERROR) { if (error == GPG_ERR_NO_ERROR) {
gpgme_key_t key; gpgme_key_t key;
@ -406,8 +433,6 @@ p_gpg_valid_key(const char * const keyid)
return FALSE; return FALSE;
} }
gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
gpgme_key_t key = NULL; gpgme_key_t key = NULL;
error = gpgme_get_key(ctx, keyid, &key, 1); error = gpgme_get_key(ctx, keyid, &key, 1);
@ -449,8 +474,6 @@ p_gpg_verify(const char * const barejid, const char *const sign)
return; return;
} }
gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
char *sign_with_header_footer = _add_header_footer(sign, PGP_SIGNATURE_HEADER, PGP_SIGNATURE_FOOTER); char *sign_with_header_footer = _add_header_footer(sign, PGP_SIGNATURE_HEADER, PGP_SIGNATURE_FOOTER);
gpgme_data_t sign_data; gpgme_data_t sign_data;
gpgme_data_new_from_mem(&sign_data, sign_with_header_footer, strlen(sign_with_header_footer), 1); gpgme_data_new_from_mem(&sign_data, sign_with_header_footer, strlen(sign_with_header_footer), 1);
@ -558,6 +581,8 @@ p_gpg_sign(const char * const str, const char * const fp)
gpgme_free(signed_str); gpgme_free(signed_str);
} }
passphrase = strdup(passphrase_attempt);
return result; return result;
} }
@ -584,8 +609,6 @@ p_gpg_encrypt(const char * const barejid, const char * const message)
return NULL; return NULL;
} }
gpgme_set_passphrase_cb(ctx, (gpgme_passphrase_cb_t)_p_gpg_passphrase_cb, NULL);
gpgme_key_t key; gpgme_key_t key;
error = gpgme_get_key(ctx, pubkeyid->id, &key, 0); error = gpgme_get_key(ctx, pubkeyid->id, &key, 0);
@ -687,6 +710,8 @@ p_gpg_decrypt(const char * const cipher)
} }
gpgme_free(plain_str); gpgme_free(plain_str);
passphrase = strdup(passphrase_attempt);
return result; return result;
} }