summary refs log blame commit diff stats
path: root/plugins/fishlim/keystore.c
blob: c54b5dca8cfe00be5d1b5a04f2ee265583742ea9 (plain) (tree)
1
2
3

  
                                                               



























                                                                               
                         





                                      
                                                                 

























                                                                  
                                                                  



































































































                                                                                     

                                                       








































                                                             
/*

  Copyright (c) 2010 Samuel Lidén Borell <samuel@kodafritt.se>

  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.

*/

#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include "irc.h"
#include "fish.h"
#include "misc.h"
#include "keystore.h"
#include "plugin_xchat.h"


static char *keystore_password = NULL;


/**
 * Opens the key store file: ~/.config/hexchat/addon_fishlim.conf
 */
static GKeyFile *getConfigFile() {
    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.
 */
static const char *get_keystore_password() {
    return (keystore_password != NULL ?
        keystore_password :
        // Silly default value...
        "blowinikey");
}


/**
 * Gets a value for a nick/channel from addon_fishlim.conf. Unlike
 * 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) {
    // Get the key
    GKeyFile *keyfile = getConfigFile();
    gchar *value = get_nick_value(keyfile, nick, "key");
    g_key_file_free(keyfile);
    if (!value) return NULL;
    
    if (strncmp(value, "+OK ", 4) != 0) {
        // Key is stored in plaintext
        return import_glib_string(value);
    } else {
        // Key is encrypted
        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.
 */
static bool delete_nick(GKeyFile *keyfile, const char *nick) {
    gchar **group;
    gchar **groups = g_key_file_get_groups(keyfile, NULL);
    bool ok = false;
    
    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;
}

/**
 * Writes the key store file to disk.
 */
static bool save_keystore(GKeyFile *keyfile) {
    char *filename;
    bool 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
    filename = get_config_filename();
    ok = g_file_set_contents(filename, file_data, file_length, NULL);
    g_free(filename);
    g_free(file_data);
    return ok;
}

/**
 * Sets a key in the key store file.
 */
bool keystore_store_key(const char *nick, const char *key) {
    const char *password;
    char *encrypted;
    char *wrapped;
    bool ok = false;
    GKeyFile *keyfile = getConfigFile();
    
    // Remove old key
    delete_nick(keyfile, nick);
    
    // Add new key
    password = get_keystore_password();
    if (password) {
        // Encrypt the password
        encrypted = fish_encrypt(password, strlen(password), key);
        if (!encrypted) goto end;
        
        // Prepend "+OK "
        wrapped = g_strconcat("+OK ", encrypted, NULL);
        g_free(encrypted);
        
        // Store encrypted in file
        g_key_file_set_string(keyfile, nick, "key", wrapped);
        free(wrapped);
    } else {
        // Store unencrypted in file
        g_key_file_set_string(keyfile, nick, "key", key);
    }
    
    // Save key store file
    ok = save_keystore(keyfile);
    
  end:
    g_key_file_free(keyfile);
    return ok;
}

/**
 * Deletes a nick from the key store.
 */
bool keystore_delete_nick(const char *nick) {
    GKeyFile *keyfile = getConfigFile();
    
    // Delete entry
    bool ok = delete_nick(keyfile, nick);
    
    // Save
    if (ok) save_keystore(keyfile);
    
    g_key_file_free(keyfile);
    return ok;
}


void keystore_secure_free(void *ptr, size_t size) {
    secure_erase(ptr, size);
    free(ptr);
}
n class="se">\glib-2.0.lib \ $(libdir)\libenchant.lib ENCHANT_OBJECTS = \ $(objdir)\enchant-ispell.obj $(objdir)\enchant-ispell.obj : $(srcdir)\enchant-ispell.c $(CC_OBJ) $** $(ENCHANT_DEFINES) enchant: $(ENCHANT_EXE) $(ENCHANT_EXE): $(ENCHANT_OBJECTS) $(LINK_EXE) $(ENCHANT_OBJECTS) $(ENCHANT_LIBS) -OUT:$(ENCHANT_EXE) $(EMBED_MANIFEST:exe_name=enchant.exe) $(CLEAN_MANIFEST:exe_name=enchant.exe) enchant_clean: $(RM) $(ENCHANT_OBJECTS) $(ENCHANT_EXE) $(ENCHANT_PDB) ################################################################################ #### ENCHANT-LSMOD #### srcdir = $(toptestdir) ENCHANT_LSMOD_EXE=$(outdir)\enchant-lsmod.exe ENCHANT_LSMOD_PDB=$(pdbdir)\enchant-lsmod.pdb ENCHANT_LSMOD_DEFINES = \ -D_CONSOLE \ -DVERSION=\"$(ENCHANT_VERSION)\" ENCHANT_LSMOD_LIBS = \ $(otherlibdir)\glib-2.0.lib \ $(libdir)\libenchant.lib ENCHANT_LSMOD_OBJECTS = \ $(objdir)\enchant-lsmod.obj $(objdir)\enchant-lsmod.obj : $(srcdir)\enchant-lsmod.c $(CC_OBJ) $** $(ENCHANT_LSMOD_DEFINES) enchant_lsmod: $(ENCHANT_LSMOD_EXE) $(ENCHANT_LSMOD_EXE): $(ENCHANT_LSMOD_OBJECTS) $(LINK_EXE) $(ENCHANT_LSMOD_OBJECTS) $(ENCHANT_LSMOD_LIBS) -OUT:$(ENCHANT_LSMOD_EXE) $(EMBED_MANIFEST:exe_name=enchant-lsmod.exe) $(CLEAN_MANIFEST:exe_name=enchant-lsmod.exe) enchant_lsmod_clean: $(RM) $(ENCHANT_LSMOD_OBJECTS) $(ENCHANT_LSMOD_EXE) $(ENCHANT_LSMOD_PDB) ################################################################################ #### TEST-ENCHANT #### srcdir = $(toptestdir) TEST_ENCHANT_EXE=$(outdir)\test-enchant.exe TEST_ENCHANT_PDB=$(pdbdir)\test-enchant.pdb TEST_ENCHANT_DEFINES = \ -D_CONSOLE \ -DVERSION=\"$(ENCHANT_VERSION)\" TEST_ENCHANT_LIBS = \ $(otherlibdir)\glib-2.0.lib \ $(libdir)\libenchant.lib TEST_ENCHANT_OBJECTS = \ $(objdir)\test-enchant.obj $(objdir)\test-enchant.obj : $(srcdir)\test-enchant.c $(CC_OBJ) $** $(TEST_ENCHANT_DEFINES) test-enchant: $(TEST_ENCHANT_EXE) $(TEST_ENCHANT_EXE): $(TEST_ENCHANT_OBJECTS) $(LINK_EXE) $(TEST_ENCHANT_OBJECTS) $(TEST_ENCHANT_LIBS) -OUT:$(TEST_ENCHANT_EXE) $(EMBED_MANIFEST:exe_name=test-enchant.exe) $(CLEAN_MANIFEST:exe_name=test-enchant.exe) test-enchant_clean: $(RM) $(TEST_ENCHANT_OBJECTS) $(TEST_ENCHANT_EXE) $(TEST_ENCHANT_PDB) ################################################################################ #### TEST-ENCHANTXX #### srcdir = $(toptestdir) TEST_ENCHANTXX_EXE=$(outdir)\test-enchantxx.exe TEST_ENCHANTXX_PDB=$(pdbdir)\test-enchantxx.pdb TEST_ENCHANTXX_DEFINES = \ -D_CONSOLE \ -DVERSION=\"$(ENCHANT_VERSION)\" TEST_ENCHANTXX_LIBS = \ $(otherlibdir)\glib-2.0.lib \ $(libdir)\libenchant.lib TEST_ENCHANTXX_OBJECTS = \ $(objdir)\test-enchantxx.obj $(objdir)\test-enchantxx.obj : $(srcdir)\test-enchantxx.cpp $(CC_OBJ) $** $(TEST_ENCHANTXX_DEFINES) test-enchantxx: $(TEST_ENCHANTXX_EXE) $(TEST_ENCHANTXX_EXE): $(TEST_ENCHANTXX_OBJECTS) $(LINK_EXE) $(TEST_ENCHANTXX_OBJECTS) $(TEST_ENCHANTXX_LIBS) -OUT:$(TEST_ENCHANTXX_EXE) $(EMBED_MANIFEST:exe_name=test-enchantxx.exe) $(CLEAN_MANIFEST:exe_name=test-enchantxx.exe) test-enchantxx_clean: $(RM) $(TEST_ENCHANTXX_OBJECTS) $(TEST_ENCHANTXX_EXE) $(TEST_ENCHANTXX_PDB) ################################################################################ #### LIBENCHANT #### srcdir = $(topsrcdir) LIBENCHANT_DEFINES = \ -D_ENCHANT_BUILD=1 \ -DWINDLL LIBENCHANT_DLL=$(outdir)\libenchant.dll LIBENCHANT_LIB=$(outdir)\libenchant.lib LIBENCHANT_PDB=$(pdbdir)\libenchant.pdb LIBENCHANT_RES=$(srcdir)\libenchant.res LIBENCHANT_RC=$(srcdir)\libenchant.rc LIBENCHANT_LIBS = \ $(otherlibdir)\glib-2.0.lib \ $(otherlibdir)\gmodule-2.0.lib \ advapi32.lib LIBENCHANT_OBJECTS = \ $(objdir)\enchant.obj \ $(objdir)\prefix.obj \ $(objdir)\pwl.obj $(objdir)\pwl.obj : $(srcdir)\pwl.c $(CC_OBJ) $** $(LIBENCHANT_DEFINES) $(objdir)\prefix.obj : $(srcdir)\prefix.c $(CC_OBJ) $** $(LIBENCHANT_DEFINES) $(objdir)\enchant.obj : $(srcdir)\enchant.c $(CC_OBJ) $** $(LIBENCHANT_DEFINES) $(LIBENCHANT_RES) : $(LIBENCHANT_RC) rc -Fo $(LIBENCHANT_RES) $(LIBENCHANT_RC) !if !$(DLL) libenchant: LIBENCHANT_LIB libenchant_clean: libenchant_lib_clean !else libenchant: LIBENCHANT_DLL libenchant_clean: libenchant_dll_clean !endif LIBENCHANT_LIB : $(LIBENCHANT_OBJECTS) -$(RM) libenchant.lib $(AR) $(AR_FLAGS)$(LIBENCHANT_LIB) $(LIBENCHANT_OBJECTS) # libenchant.dll and libenchant.lib are created together. LIBENCHANT_DLL : $(LIBENCHANT_OBJECTS) $(LIBENCHANT_RES) $(LINK_DLL) $(LIBENCHANT_OBJECTS) $(LIBENCHANT_LIBS) $(LIBENCHANT_RES) -OUT:$(LIBENCHANT_DLL) $(EMBED_MANIFEST:exe_name=libenchant.dll) $(CLEAN_MANIFEST:exe_name=libenchant.dll) libenchant_lib_clean: $(RM) $(LIBENCHANT_OBJECTS) $(LIBENCHANT_LIB) libenchant_dll_clean: $(RM) $(LIBENCHANT_OBJECTS) $(LIBENCHANT_LIB) $(LIBENCHANT_DLL) $(LIBENCHANT_PDB) $(LIBENCHANT_RES) $(LIBENCHANT_RC) ################################################################################ #### LIBENCHANT_ISPELL #### srcdir = $(topsrcdir)\ispell LIBENCHANT_ISPELL_DEFINES = \ -D "_ENCHANT_BUILD=1" \ -D "ISPELL_PROVIDER_EXPORTS" \ -D_USRDLL \ -D_WINDLL LIBENCHANT_ISPELL_DLL=$(outdir)\libenchant_ispell.dll LIBENCHANT_ISPELL_LIB=$(libdir)\libenchant_ispell.lib LIBENCHANT_ISPELL_PDB=$(pdbdir)\libenchant_ispell.pdb LIBENCHANT_ISPELL_LIBS = \ $(otherlibdir)\glib-2.0.lib \ $(otherlibdir)\gmodule-2.0.lib \ $(libdir)\libenchant.lib \ advapi32.lib ntstc_msvcrt.lib LIBENCHANT_ISPELL_OBJECTS = \ $(objdir)\correct.obj \ $(objdir)\good.obj \ $(objdir)\hash.obj \ $(objdir)\ispell_checker.obj \ $(objdir)\lookup.obj \ $(objdir)\makedent.obj \ $(objdir)\tgood.obj !if !$(DLL) libenchant_ispell: LIBENCHANT_ISPELL_LIB libenchant_ispell_clean: libenchant_ispell_lib_clean !else libenchant_ispell: LIBENCHANT_ISPELL_DLL libenchant_ispell_clean: libenchant_ispell_dll_clean !endif $(objdir)\correct.obj : $(srcdir)\correct.cpp $(CC_OBJ) $** $(LIBENCHANT_ISPELL_DEFINES) $(objdir)\good.obj : $(srcdir)\good.cpp $(CC_OBJ) $** $(LIBENCHANT_ISPELL_DEFINES) $(objdir)\hash.obj : $(srcdir)\hash.cpp $(CC_OBJ) $** $(LIBENCHANT_ISPELL_DEFINES) $(objdir)\ispell_checker.obj : $(srcdir)\ispell_checker.cpp $(CC_OBJ) $** $(LIBENCHANT_ISPELL_DEFINES) $(objdir)\lookup.obj : $(srcdir)\lookup.cpp $(CC_OBJ) $** $(LIBENCHANT_ISPELL_DEFINES) $(objdir)\makedent.obj : $(srcdir)\makedent.cpp $(CC_OBJ) $** $(LIBENCHANT_ISPELL_DEFINES) $(objdir)\tgood.obj : $(srcdir)\tgood.cpp $(CC_OBJ) $** $(LIBENCHANT_ISPELL_DEFINES) LIBENCHANT_ISPELL_LIB : $(LIBENCHANT_ISPELL_OBJECTS) -$(RM) $(LIBENCHANT_ISPELL_LIB) $(AR) $(AR_FLAGS)$(LIBENCHANT_ISPELL_LIB) $(LIBENCHANT_ISPELL_OBJECTS) # libenchant_ispell.dll and libenchant_ispell.lib are created together. LIBENCHANT_ISPELL_DLL : $(LIBENCHANT_ISPELL_OBJECTS) $(LINK_DLL) $(LIBENCHANT_ISPELL_OBJECTS) $(LIBENCHANT_ISPELL_LIBS) -OUT:$(LIBENCHANT_ISPELL_DLL) $(EMBED_MANIFEST:exe_name=libenchant_ispell.dll) $(CLEAN_MANIFEST:exe_name=libenchant_ispell.dll) libenchant_ispell_lib_clean: $(RM) $(LIBENCHANT_ISPELL_OBJECTS) $(LIBENCHANT_ISPELL_LIB) libenchant_ispell_dll_clean: $(RM) $(LIBENCHANT_ISPELL_OBJECTS) $(LIBENCHANT_ISPELL_LIB) $(LIBENCHANT_ISPELL_DLL) $(LIBENCHANT_ISPELL_PDB) ################################################################################ #### LIBENCHANT_MYSPELL #### srcdir = $(topsrcdir)\myspell LIBENCHANT_MYSPELL_DEFINES = \ -D "HUNSPELL_STATIC" \ -D "_ENCHANT_BUILD=1" \ -D "_USRDLL" \ -D "MYSPELL_PROVIDER_EXPORTS" \ -D "_WINDLL" LIBENCHANT_MYSPELL_DLL=$(outdir)\libenchant_myspell.dll LIBENCHANT_MYSPELL_LIB=$(libdir)\libenchant_myspell.lib LIBENCHANT_MYSPELL_PDB=$(pdbdir)\libenchant_myspell.pdb LIBENCHANT_MYSPELL_LIBS = \ $(otherlibdir)\glib-2.0.lib \ $(otherlibdir)\gmodule-2.0.lib \ $(libdir)\libenchant.lib \ advapi32.lib ntstc_msvcrt.lib LIBENCHANT_MYSPELL_OBJECTS = \ $(objdir)\affentry.obj \ $(objdir)\affixmgr.obj \ $(objdir)\dictmgr.obj \ $(objdir)\csutil.obj \ $(objdir)\utf_info.obj \ $(objdir)\hashmgr.obj \ $(objdir)\suggestmgr.obj \ $(objdir)\hunspell.obj \ $(objdir)\filemgr.obj \ $(objdir)\phonet.obj \ $(objdir)\hunzip.obj \ $(objdir)\myspell_checker.obj !if !$(DLL) libenchant_myspell: LIBENCHANT_MYSPELL_LIB libenchant_myspell_clean: libenchant_myspell_lib_clean !else libenchant_myspell: LIBENCHANT_MYSPELL_DLL libenchant_myspell_clean: libenchant_myspell_dll_clean !endif $(objdir)\affentry.obj : $(srcdir)\affentry.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\affixmgr.obj : $(srcdir)\affixmgr.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\dictmgr.obj : $(srcdir)\dictmgr.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\csutil.obj : $(srcdir)\csutil.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\utf_info.obj : $(srcdir)\utf_info.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\hashmgr.obj : $(srcdir)\hashmgr.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\suggestmgr.obj : $(srcdir)\suggestmgr.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\hunspell.obj : $(srcdir)\hunspell.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\filemgr.obj : $(srcdir)\filemgr.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\phonet.obj : $(srcdir)\phonet.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\hunzip.obj : $(srcdir)\hunzip.cxx $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) $(objdir)\myspell_checker.obj : $(srcdir)\myspell_checker.cpp $(CC_OBJ) $** $(LIBENCHANT_MYSPELL_DEFINES) LIBENCHANT_MYSPELL_LIB : $(LIBENCHANT_MYSPELL_OBJECTS) -$(RM) $(LIBENCHANT_MYSPELL_LIB) $(AR) $(AR_FLAGS)$(LIBENCHANT_MYSPELL_LIB) $(LIBENCHANT_MYSPELL_OBJECTS) # libenchant_myspell.dll and libenchant_myspell.lib are created together. LIBENCHANT_MYSPELL_DLL : $(LIBENCHANT_MYSPELL_OBJECTS) $(LINK_DLL) $(LIBENCHANT_MYSPELL_OBJECTS) $(LIBENCHANT_MYSPELL_LIBS) -OUT:$(LIBENCHANT_MYSPELL_DLL) $(EMBED_MANIFEST:exe_name=libenchant_myspell.dll) $(CLEAN_MANIFEST:exe_name=libenchant_myspell.dll) libenchant_myspell_lib_clean: $(RM) $(LIBENCHANT_MYSPELL_OBJECTS) $(LIBENCHANT_MYSPELL_LIB) libenchant_myspell_dll_clean: $(RM) $(LIBENCHANT_MYSPELL_OBJECTS) $(LIBENCHANT_MYSPELL_LIB) $(LIBENCHANT_MYSPELL_DLL) $(LIBENCHANT_MYSPELL_PDB) ################################################################################ check : all mostlyclean : clean clean: libenchant_clean libenchant_ispell_clean libenchant_myspell_clean enchant_clean enchant_lsmod_clean test-enchant_clean test-enchantxx_clean distclean : clean maintainer-clean : distclean force :