c.sexchat/plugins/fishlim/keystore.c

248 lines
6.4 KiB
C
Raw Normal View History

2011-11-24 23:17:31 +01:00
/*
2012-05-04 19:29:02 +02:00
Copyright (c) 2010 Samuel Lidén Borell <samuel@kodafritt.se>
2011-11-24 23:17:31 +01:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
2014-12-16 04:57:27 +01:00
#include "config.h"
2011-11-24 23:17:31 +01:00
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include "irc.h"
#include "fish.h"
#include "keystore.h"
2012-10-24 21:33:02 +02:00
#include "plugin_hexchat.h"
2011-11-24 23:17:31 +01:00
static char *keystore_password = NULL;
/**
* Opens the key store file: ~/.config/hexchat/addon_fishlim.conf
2011-11-24 23:17:31 +01:00
*/
2016-02-18 14:57:22 +01:00
static GKeyFile *getConfigFile(void) {
2011-11-24 23:17:31 +01:00
gchar *filename = get_config_filename();
GKeyFile *keyfile = g_key_file_new();
g_key_file_load_from_file(keyfile, filename,
G_KEY_FILE_KEEP_COMMENTS |
G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
g_free(filename);
return keyfile;
}
/**
* Returns the key store password, or the default.
*/
2016-02-18 14:57:22 +01:00
static const char *get_keystore_password(void) {
2011-11-24 23:17:31 +01:00
return (keystore_password != NULL ?
keystore_password :
2014-12-18 00:49:59 +01:00
/* Silly default value... */
2011-11-24 23:17:31 +01:00
"blowinikey");
}
static char *escape_nickname(const char *nick) {
char *escaped = g_strdup(nick);
char *p = escaped;
while (*p) {
if (*p == '[')
*p = '~';
else if (*p == ']')
*p = '!';
++p;
}
return escaped;
}
2011-11-24 23:17:31 +01:00
/**
* Gets a value for a nick/channel from addon_fishlim.conf. Unlike
2011-11-24 23:17:31 +01:00
* g_key_file_get_string, this function is case insensitive.
*/
static gchar *get_nick_value(GKeyFile *keyfile, const char *nick, const char *item) {
gchar **group;
gchar **groups = g_key_file_get_groups(keyfile, NULL);
gchar *result = NULL;
for (group = groups; *group != NULL; group++) {
if (!irc_nick_cmp(*group, nick)) {
result = g_key_file_get_string(keyfile, *group, item, NULL);
break;
}
}
g_strfreev(groups);
return result;
}
/**
* Extracts a key from the key store file.
*/
char *keystore_get_key(const char *nick) {
2014-12-18 00:49:59 +01:00
/* Get the key */
2011-11-24 23:17:31 +01:00
GKeyFile *keyfile = getConfigFile();
char *escaped_nick = escape_nickname(nick);
gchar *value = get_nick_value(keyfile, escaped_nick, "key");
2011-11-24 23:17:31 +01:00
g_key_file_free(keyfile);
g_free(escaped_nick);
if (!value)
return NULL;
2011-11-24 23:17:31 +01:00
if (strncmp(value, "+OK ", 4) != 0) {
2014-12-18 00:49:59 +01:00
/* Key is stored in plaintext */
return value;
2011-11-24 23:17:31 +01:00
} else {
2014-12-18 00:49:59 +01:00
/* Key is encrypted */
2011-11-24 23:17:31 +01:00
const char *encrypted = value+4;
const char *password = get_keystore_password();
char *decrypted = fish_decrypt(password, strlen(password), encrypted);
g_free(value);
return decrypted;
}
}
/**
* Deletes a nick and the associated key in the key store file.
*/
2014-12-18 00:49:59 +01:00
static gboolean delete_nick(GKeyFile *keyfile, const char *nick) {
2011-11-24 23:17:31 +01:00
gchar **group;
gchar **groups = g_key_file_get_groups(keyfile, NULL);
2014-12-18 00:49:59 +01:00
gboolean ok = FALSE;
2011-11-24 23:17:31 +01:00
for (group = groups; *group != NULL; group++) {
if (!irc_nick_cmp(*group, nick)) {
ok = g_key_file_remove_group(keyfile, *group, NULL);
break;
}
}
g_strfreev(groups);
return ok;
}
2014-12-15 21:33:10 +01:00
#if !GLIB_CHECK_VERSION(2,40,0)
/**
* Writes the key store file to disk.
*/
static gboolean keyfile_save_to_file (GKeyFile *keyfile, char *filename) {
gboolean ok;
/* Serialize */
gsize file_length;
gchar *file_data = g_key_file_to_data(keyfile, &file_length, NULL);
if (!file_data)
return FALSE;
/* Write to file */
ok = g_file_set_contents (filename, file_data, file_length, NULL);
g_free(file_data);
return ok;
}
#endif
2011-11-24 23:17:31 +01:00
/**
* Writes the key store file to disk.
*/
2014-12-18 00:49:59 +01:00
static gboolean save_keystore(GKeyFile *keyfile) {
2011-11-24 23:17:31 +01:00
char *filename;
2014-12-18 00:49:59 +01:00
gboolean ok;
2011-11-24 23:17:31 +01:00
filename = get_config_filename();
2014-12-15 21:33:10 +01:00
#if !GLIB_CHECK_VERSION(2,40,0)
ok = keyfile_save_to_file (keyfile, filename);
#else
2017-06-15 06:14:30 +02:00
G_GNUC_BEGIN_IGNORE_DEPRECATIONS /* Hide false positive */
ok = g_key_file_save_to_file (keyfile, filename, NULL);
2017-06-15 06:14:30 +02:00
G_GNUC_END_IGNORE_DEPRECATIONS
2014-12-15 21:33:10 +01:00
#endif
g_free (filename);
2011-11-24 23:17:31 +01:00
return ok;
}
/**
* Sets a key in the key store file.
*/
2014-12-18 00:49:59 +01:00
gboolean keystore_store_key(const char *nick, const char *key) {
2011-11-24 23:17:31 +01:00
const char *password;
char *encrypted;
char *wrapped;
2014-12-18 00:49:59 +01:00
gboolean ok = FALSE;
2011-11-24 23:17:31 +01:00
GKeyFile *keyfile = getConfigFile();
char *escaped_nick = escape_nickname(nick);
2014-12-18 00:49:59 +01:00
/* Remove old key */
delete_nick(keyfile, escaped_nick);
2011-11-24 23:17:31 +01:00
2014-12-18 00:49:59 +01:00
/* Add new key */
2011-11-24 23:17:31 +01:00
password = get_keystore_password();
if (password) {
2014-12-18 00:49:59 +01:00
/* Encrypt the password */
2011-11-24 23:17:31 +01:00
encrypted = fish_encrypt(password, strlen(password), key);
if (!encrypted) goto end;
2014-12-18 00:49:59 +01:00
/* Prepend "+OK " */
2012-01-03 16:38:40 +01:00
wrapped = g_strconcat("+OK ", encrypted, NULL);
2011-11-24 23:17:31 +01:00
g_free(encrypted);
2014-12-18 00:49:59 +01:00
/* Store encrypted in file */
g_key_file_set_string(keyfile, escaped_nick, "key", wrapped);
g_free(wrapped);
2011-11-24 23:17:31 +01:00
} else {
2014-12-18 00:49:59 +01:00
/* Store unencrypted in file */
g_key_file_set_string(keyfile, escaped_nick, "key", key);
2011-11-24 23:17:31 +01:00
}
2014-12-18 00:49:59 +01:00
/* Save key store file */
2011-11-24 23:17:31 +01:00
ok = save_keystore(keyfile);
end:
g_key_file_free(keyfile);
g_free(escaped_nick);
2011-11-24 23:17:31 +01:00
return ok;
}
/**
* Deletes a nick from the key store.
*/
2014-12-18 00:49:59 +01:00
gboolean keystore_delete_nick(const char *nick) {
2011-11-24 23:17:31 +01:00
GKeyFile *keyfile = getConfigFile();
char *escaped_nick = escape_nickname(nick);
2011-11-24 23:17:31 +01:00
2014-12-18 00:49:59 +01:00
/* Delete entry */
gboolean ok = delete_nick(keyfile, escaped_nick);
2011-11-24 23:17:31 +01:00
2014-12-18 00:49:59 +01:00
/* Save */
2011-11-24 23:17:31 +01:00
if (ok) save_keystore(keyfile);
g_key_file_free(keyfile);
g_free(escaped_nick);
2011-11-24 23:17:31 +01:00
return ok;
}