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

PGP: Show key IDs for assigned public keys

This commit is contained in:
James Booth 2015-08-25 22:45:51 +01:00
parent 592a3695a5
commit fc1ee79190
5 changed files with 73 additions and 73 deletions

View File

@ -1146,7 +1146,7 @@ static struct cmd_t command_defs[] =
CMD_SYN( CMD_SYN(
"/pgp libver", "/pgp libver",
"/pgp keys", "/pgp keys",
"/pgp fps", "/pgp contacts",
"/pgp setkey <contact> <keyid>", "/pgp setkey <contact> <keyid>",
"/pgp start [<contact>]", "/pgp start [<contact>]",
"/pgp end", "/pgp end",
@ -1156,8 +1156,8 @@ static struct cmd_t command_defs[] =
"See the /account command to set your own PGP key.") "See the /account command to set your own PGP key.")
CMD_ARGS( CMD_ARGS(
{ "libver", "Show which version of the libgpgme library is being used." }, { "libver", "Show which version of the libgpgme library is being used." },
{ "keys", "List all keys." }, { "keys", "List all keys known to the system." },
{ "fps", "Show known fingerprints." }, { "contacts", "Show contacts with assigned public keys." },
{ "setkey <contact> <keyid>", "Manually associate a key ID with a JID." }, { "setkey <contact> <keyid>", "Manually associate a key ID with a JID." },
{ "start [<contact>]", "Start PGP encrypted chat, current contact will be used if not specified." }, { "start [<contact>]", "Start PGP encrypted chat, current contact will be used if not specified." },
{ "end", "End PGP encrypted chat with the current recipient." }, { "end", "End PGP encrypted chat with the current recipient." },
@ -2053,7 +2053,7 @@ cmd_init(void)
pgp_ac = autocomplete_new(); pgp_ac = autocomplete_new();
autocomplete_add(pgp_ac, "keys"); autocomplete_add(pgp_ac, "keys");
autocomplete_add(pgp_ac, "fps"); autocomplete_add(pgp_ac, "contacts");
autocomplete_add(pgp_ac, "setkey"); autocomplete_add(pgp_ac, "setkey");
autocomplete_add(pgp_ac, "libver"); autocomplete_add(pgp_ac, "libver");
autocomplete_add(pgp_ac, "start"); autocomplete_add(pgp_ac, "start");

View File

@ -4270,25 +4270,25 @@ cmd_pgp(ProfWin *window, const char * const command, gchar **args)
return TRUE; return TRUE;
} }
if (g_strcmp0(args[0], "fps") == 0) { if (g_strcmp0(args[0], "contacts") == 0) {
jabber_conn_status_t conn_status = jabber_get_connection_status(); jabber_conn_status_t conn_status = jabber_get_connection_status();
if (conn_status != JABBER_CONNECTED) { if (conn_status != JABBER_CONNECTED) {
cons_show("You are not currently connected."); cons_show("You are not currently connected.");
return TRUE; return TRUE;
} }
GHashTable *fingerprints = p_gpg_fingerprints(); GHashTable *pubkeys = p_gpg_pubkeys();
GList *jids = g_hash_table_get_keys(fingerprints); GList *jids = g_hash_table_get_keys(pubkeys);
if (!jids) { if (!jids) {
cons_show("No PGP fingerprints available."); cons_show("No contacts found with PGP public keys assigned.");
return TRUE; return TRUE;
} }
cons_show("Known PGP fingerprints:"); cons_show("Assigned PGP public keys:");
GList *curr = jids; GList *curr = jids;
while (curr) { while (curr) {
char *jid = curr->data; char *jid = curr->data;
char *fingerprint = g_hash_table_lookup(fingerprints, jid); char *pubkey = g_hash_table_lookup(pubkeys, jid);
cons_show(" %s: %s", jid, fingerprint); cons_show(" %s: %s", jid, pubkey);
curr = g_list_next(curr); curr = g_list_next(curr);
} }
g_list_free(jids); g_list_free(jids);

View File

@ -54,14 +54,14 @@
#define PGP_MESSAGE_FOOTER "-----END PGP MESSAGE-----" #define PGP_MESSAGE_FOOTER "-----END PGP MESSAGE-----"
static const char *libversion; static const char *libversion;
static GHashTable *fingerprints; static GHashTable *pubkeys;
static gchar *fpsloc; static gchar *pubsloc;
static GKeyFile *fpskeyfile; static GKeyFile *pubkeyfile;
static char* _remove_header_footer(char *str, const char * const footer); static char* _remove_header_footer(char *str, const char * const footer);
static char* _add_header_footer(const char * const str, const char * const header, const char * const footer); static char* _add_header_footer(const char * const str, const char * const header, const char * const footer);
static void _save_fps(void); static void _save_pubkeys(void);
void void
p_gpg_init(void) p_gpg_init(void)
@ -70,65 +70,65 @@ p_gpg_init(void)
log_debug("GPG: Found gpgme version: %s", libversion); log_debug("GPG: Found gpgme version: %s", libversion);
gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
fingerprints = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
} }
void void
p_gpg_close(void) p_gpg_close(void)
{ {
if (fingerprints) { if (pubkeys) {
g_hash_table_destroy(fingerprints); g_hash_table_destroy(pubkeys);
fingerprints = NULL; pubkeys = NULL;
} }
if (fpskeyfile) { if (pubkeyfile) {
g_key_file_free(fpskeyfile); g_key_file_free(pubkeyfile);
fpskeyfile = NULL; pubkeyfile = NULL;
} }
free(fpsloc); free(pubsloc);
fpsloc = NULL; pubsloc = NULL;
} }
void void
p_gpg_on_connect(const char * const barejid) p_gpg_on_connect(const char * const barejid)
{ {
gchar *data_home = xdg_get_data_home(); gchar *data_home = xdg_get_data_home();
GString *fpsfile = g_string_new(data_home); GString *pubsfile = g_string_new(data_home);
free(data_home); free(data_home);
gchar *account_dir = str_replace(barejid, "@", "_at_"); gchar *account_dir = str_replace(barejid, "@", "_at_");
g_string_append(fpsfile, "/profanity/pgp/"); g_string_append(pubsfile, "/profanity/pgp/");
g_string_append(fpsfile, account_dir); g_string_append(pubsfile, account_dir);
free(account_dir); free(account_dir);
// mkdir if doesn't exist for account // mkdir if doesn't exist for account
errno = 0; errno = 0;
int res = g_mkdir_with_parents(fpsfile->str, S_IRWXU); int res = g_mkdir_with_parents(pubsfile->str, S_IRWXU);
if (res == -1) { if (res == -1) {
char *errmsg = strerror(errno); char *errmsg = strerror(errno);
if (errmsg) { if (errmsg) {
log_error("Error creating directory: %s, %s", fpsfile->str, errmsg); log_error("Error creating directory: %s, %s", pubsfile->str, errmsg);
} else { } else {
log_error("Error creating directory: %s", fpsfile->str); log_error("Error creating directory: %s", pubsfile->str);
} }
} }
// create or read fingerprints keyfile // create or read publickeys
g_string_append(fpsfile, "/fingerprints"); g_string_append(pubsfile, "/pubkeys");
fpsloc = fpsfile->str; pubsloc = pubsfile->str;
g_string_free(fpsfile, FALSE); g_string_free(pubsfile, FALSE);
if (g_file_test(fpsloc, G_FILE_TEST_EXISTS)) { if (g_file_test(pubsloc, G_FILE_TEST_EXISTS)) {
g_chmod(fpsloc, S_IRUSR | S_IWUSR); g_chmod(pubsloc, S_IRUSR | S_IWUSR);
} }
fpskeyfile = g_key_file_new(); pubkeyfile = g_key_file_new();
g_key_file_load_from_file(fpskeyfile, fpsloc, G_KEY_FILE_KEEP_COMMENTS, NULL); g_key_file_load_from_file(pubkeyfile, pubsloc, G_KEY_FILE_KEEP_COMMENTS, NULL);
// load each keyid // load each keyid
gsize len = 0; gsize len = 0;
gchar **jids = g_key_file_get_groups(fpskeyfile, &len); gchar **jids = g_key_file_get_groups(pubkeyfile, &len);
gpgme_ctx_t ctx; gpgme_ctx_t ctx;
gpgme_error_t error = gpgme_new(&ctx); gpgme_error_t error = gpgme_new(&ctx);
@ -143,21 +143,21 @@ p_gpg_on_connect(const char * const barejid)
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
GError *gerr = NULL; GError *gerr = NULL;
gchar *jid = jids[i]; gchar *jid = jids[i];
gchar *keyid = g_key_file_get_string(fpskeyfile, jid, "keyid", &gerr); gchar *keyid = g_key_file_get_string(pubkeyfile, jid, "keyid", &gerr);
if (gerr) { if (gerr) {
log_error("Error loading PGP key id for %s", jid); log_error("Error loading PGP key id for %s", jid);
g_error_free(gerr); g_error_free(gerr);
g_free(keyid); g_free(keyid);
} else { } else {
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, 0);
g_free(keyid);
if (error || key == NULL) { if (error || key == NULL) {
log_warning("GPG: Failed to get key for %s: %s %s", jid, gpgme_strsource(error), gpgme_strerror(error)); log_warning("GPG: Failed to get key for %s: %s %s", jid, gpgme_strsource(error), gpgme_strerror(error));
continue; continue;
} }
g_hash_table_replace(fingerprints, strdup(jid), strdup(key->subkeys->fpr)); g_hash_table_replace(pubkeys, strdup(jid), strdup(keyid));
g_free(keyid);
gpgme_key_unref(key); gpgme_key_unref(key);
} }
} }
@ -165,24 +165,24 @@ p_gpg_on_connect(const char * const barejid)
gpgme_release(ctx); gpgme_release(ctx);
g_strfreev(jids); g_strfreev(jids);
_save_fps(); _save_pubkeys();
} }
void void
p_gpg_on_disconnect(void) p_gpg_on_disconnect(void)
{ {
if (fingerprints) { if (pubkeys) {
g_hash_table_destroy(fingerprints); g_hash_table_destroy(pubkeys);
fingerprints = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
} }
if (fpskeyfile) { if (pubkeyfile) {
g_key_file_free(fpskeyfile); g_key_file_free(pubkeyfile);
fpskeyfile = NULL; pubkeyfile = NULL;
} }
free(fpsloc); free(pubsloc);
fpsloc = NULL; pubsloc = NULL;
} }
gboolean gboolean
@ -204,12 +204,12 @@ p_gpg_addkey(const char * const jid, const char * const keyid)
return FALSE; return FALSE;
} }
// save to ID keyfile // save to public key file
g_key_file_set_string(fpskeyfile, jid, "keyid", keyid); g_key_file_set_string(pubkeyfile, jid, "keyid", keyid);
_save_fps(); _save_pubkeys();
// update in memory fingerprint list // update in memory pubkeys list
g_hash_table_replace(fingerprints, strdup(jid), strdup(key->subkeys->fpr)); g_hash_table_replace(pubkeys, strdup(jid), strdup(keyid));
gpgme_key_unref(key); gpgme_key_unref(key);
return TRUE; return TRUE;
@ -323,9 +323,9 @@ p_gpg_free_keys(GHashTable *keys)
GHashTable * GHashTable *
p_gpg_fingerprints(void) p_gpg_pubkeys(void)
{ {
return fingerprints; return pubkeys;
} }
const char* const char*
@ -366,8 +366,8 @@ p_gpg_valid_key(const char * const keyid)
gboolean gboolean
p_gpg_available(const char * const barejid) p_gpg_available(const char * const barejid)
{ {
char *fp = g_hash_table_lookup(fingerprints, barejid); char *pubkey = g_hash_table_lookup(pubkeys, barejid);
return (fp != NULL); return (pubkey != NULL);
} }
void void
@ -412,7 +412,7 @@ p_gpg_verify(const char * const barejid, const char *const sign)
log_debug("Could not find PGP key with ID %s for %s", result->signatures->fpr, barejid); log_debug("Could not find PGP key with ID %s for %s", result->signatures->fpr, barejid);
} else { } else {
log_debug("Fingerprint found for %s: %s ", barejid, key->subkeys->fpr); log_debug("Fingerprint found for %s: %s ", barejid, key->subkeys->fpr);
g_hash_table_replace(fingerprints, strdup(barejid), strdup(key->subkeys->fpr)); g_hash_table_replace(pubkeys, strdup(barejid), strdup(key->subkeys->keyid));
} }
gpgme_key_unref(key); gpgme_key_unref(key);
@ -493,9 +493,9 @@ p_gpg_sign(const char * const str, const char * const fp)
char * char *
p_gpg_encrypt(const char * const barejid, const char * const message) p_gpg_encrypt(const char * const barejid, const char * const message)
{ {
char *fp = g_hash_table_lookup(fingerprints, barejid); char *keyid = g_hash_table_lookup(pubkeys, barejid);
if (!fp) { if (!keyid) {
return NULL; return NULL;
} }
@ -512,7 +512,7 @@ p_gpg_encrypt(const char * const barejid, const char * const message)
} }
gpgme_key_t key; gpgme_key_t key;
error = gpgme_get_key(ctx, fp, &key, 0); error = gpgme_get_key(ctx, keyid, &key, 0);
if (error || key == NULL) { if (error || key == NULL) {
log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error)); log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
@ -588,7 +588,7 @@ p_gpg_decrypt(const char * const cipher)
gpgme_recipient_t recipient = res->recipients; gpgme_recipient_t recipient = res->recipients;
if (recipient) { if (recipient) {
gpgme_key_t key; gpgme_key_t key;
error = gpgme_get_key(ctx, recipient->keyid, &key, 0); error = gpgme_get_key(ctx, recipient->keyid, &key, 1);
if (!error && key) { if (!error && key) {
const char *addr = gpgme_key_get_string_attr(key, GPGME_ATTR_EMAIL, NULL, 0); const char *addr = gpgme_key_get_string_attr(key, GPGME_ATTR_EMAIL, NULL, 0);
@ -661,11 +661,11 @@ _add_header_footer(const char * const str, const char * const header, const char
} }
static void static void
_save_fps(void) _save_pubkeys(void)
{ {
gsize g_data_size; gsize g_data_size;
gchar *g_fps_data = g_key_file_to_data(fpskeyfile, &g_data_size, NULL); gchar *g_pubkeys_data = g_key_file_to_data(pubkeyfile, &g_data_size, NULL);
g_file_set_contents(fpsloc, g_fps_data, g_data_size, NULL); g_file_set_contents(pubsloc, g_pubkeys_data, g_data_size, NULL);
g_chmod(fpsloc, S_IRUSR | S_IWUSR); g_chmod(pubsloc, S_IRUSR | S_IWUSR);
g_free(g_fps_data); g_free(g_pubkeys_data);
} }

View File

@ -53,7 +53,7 @@ void p_gpg_on_disconnect(void);
GHashTable* p_gpg_list_keys(void); GHashTable* p_gpg_list_keys(void);
void p_gpg_free_keys(GHashTable *keys); void p_gpg_free_keys(GHashTable *keys);
gboolean p_gpg_addkey(const char * const jid, const char * const keyid); gboolean p_gpg_addkey(const char * const jid, const char * const keyid);
GHashTable* p_gpg_fingerprints(void); GHashTable* p_gpg_pubkeys(void);
gboolean p_gpg_valid_key(const char * const keyid); gboolean p_gpg_valid_key(const char * const keyid);
gboolean p_gpg_available(const char * const barejid); gboolean p_gpg_available(const char * const barejid);
const char* p_gpg_libver(void); const char* p_gpg_libver(void);

View File

@ -11,7 +11,7 @@ GHashTable* p_gpg_list_keys(void)
} }
GHashTable* GHashTable*
p_gpg_fingerprints(void) p_gpg_pubkeys(void)
{ {
return NULL; return NULL;
} }