[1ee6c18] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* Support for multiple storage backends */ |
---|
| 8 | |
---|
[d9d36fc] | 9 | /* Copyright (C) 2005 Jelmer Vernooij <jelmer@samba.org> */ |
---|
| 10 | |
---|
[1ee6c18] | 11 | /* |
---|
| 12 | This program is free software; you can redistribute it and/or modify |
---|
| 13 | it under the terms of the GNU General Public License as published by |
---|
| 14 | the Free Software Foundation; either version 2 of the License, or |
---|
| 15 | (at your option) any later version. |
---|
| 16 | |
---|
| 17 | This program is distributed in the hope that it will be useful, |
---|
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 20 | GNU General Public License for more details. |
---|
| 21 | |
---|
| 22 | You should have received a copy of the GNU General Public License with |
---|
| 23 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
| 24 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
| 25 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | #define BITLBEE_CORE |
---|
| 29 | #include "bitlbee.h" |
---|
| 30 | #include "crypting.h" |
---|
[764c7d1] | 31 | #include "otr.h" |
---|
[1ee6c18] | 32 | |
---|
| 33 | extern storage_t storage_text; |
---|
[c121f89] | 34 | extern storage_t storage_xml; |
---|
[1ee6c18] | 35 | |
---|
[c121f89] | 36 | static GList *storage_backends = NULL; |
---|
[1ee6c18] | 37 | |
---|
| 38 | void register_storage_backend(storage_t *backend) |
---|
| 39 | { |
---|
| 40 | storage_backends = g_list_append(storage_backends, backend); |
---|
| 41 | } |
---|
| 42 | |
---|
[b73ac9c] | 43 | static storage_t *storage_init_single(const char *name) |
---|
[1ee6c18] | 44 | { |
---|
| 45 | GList *gl; |
---|
[b3c467b] | 46 | storage_t *st = NULL; |
---|
[1ee6c18] | 47 | |
---|
| 48 | for (gl = storage_backends; gl; gl = gl->next) { |
---|
| 49 | st = gl->data; |
---|
| 50 | if (strcmp(st->name, name) == 0) |
---|
| 51 | break; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | if (gl == NULL) |
---|
| 55 | return NULL; |
---|
| 56 | |
---|
| 57 | if (st->init) |
---|
| 58 | st->init(); |
---|
| 59 | |
---|
| 60 | return st; |
---|
| 61 | } |
---|
[ab49fdc] | 62 | |
---|
[b73ac9c] | 63 | GList *storage_init(const char *primary, char **migrate) |
---|
| 64 | { |
---|
| 65 | GList *ret = NULL; |
---|
| 66 | int i; |
---|
| 67 | storage_t *storage; |
---|
[c121f89] | 68 | |
---|
| 69 | register_storage_backend(&storage_text); |
---|
| 70 | register_storage_backend(&storage_xml); |
---|
| 71 | |
---|
[b73ac9c] | 72 | storage = storage_init_single(primary); |
---|
[7e3592e] | 73 | if (storage == NULL && storage->save == NULL) |
---|
[b73ac9c] | 74 | return NULL; |
---|
| 75 | |
---|
| 76 | ret = g_list_append(ret, storage); |
---|
| 77 | |
---|
| 78 | for (i = 0; migrate && migrate[i]; i++) { |
---|
| 79 | storage = storage_init_single(migrate[i]); |
---|
| 80 | |
---|
| 81 | if (storage) |
---|
| 82 | ret = g_list_append(ret, storage); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | return ret; |
---|
| 86 | } |
---|
| 87 | |
---|
[ab49fdc] | 88 | storage_status_t storage_check_pass (const char *nick, const char *password) |
---|
| 89 | { |
---|
[b73ac9c] | 90 | GList *gl; |
---|
| 91 | |
---|
| 92 | /* Loop until we don't get NO_SUCH_USER */ |
---|
| 93 | |
---|
| 94 | for (gl = global.storage; gl; gl = gl->next) { |
---|
| 95 | storage_t *st = gl->data; |
---|
| 96 | storage_status_t status; |
---|
| 97 | |
---|
| 98 | status = st->check_pass(nick, password); |
---|
| 99 | if (status != STORAGE_NO_SUCH_USER) |
---|
| 100 | return status; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | return STORAGE_NO_SUCH_USER; |
---|
[ab49fdc] | 104 | } |
---|
| 105 | |
---|
| 106 | storage_status_t storage_load (const char *nick, const char *password, irc_t * irc) |
---|
| 107 | { |
---|
[b73ac9c] | 108 | GList *gl; |
---|
| 109 | |
---|
| 110 | /* Loop until we don't get NO_SUCH_USER */ |
---|
| 111 | for (gl = global.storage; gl; gl = gl->next) { |
---|
| 112 | storage_t *st = gl->data; |
---|
| 113 | storage_status_t status; |
---|
| 114 | |
---|
| 115 | status = st->load(nick, password, irc); |
---|
| 116 | if (status == STORAGE_OK) { |
---|
| 117 | irc_setpass(irc, password); |
---|
[764c7d1] | 118 | otr_load(irc); /* load our OTR userstate */ |
---|
[b73ac9c] | 119 | return status; |
---|
| 120 | } |
---|
| 121 | |
---|
[764c7d1] | 122 | if (status != STORAGE_NO_SUCH_USER) { |
---|
[b73ac9c] | 123 | return status; |
---|
[764c7d1] | 124 | } |
---|
[b73ac9c] | 125 | } |
---|
| 126 | |
---|
| 127 | return STORAGE_NO_SUCH_USER; |
---|
[ab49fdc] | 128 | } |
---|
| 129 | |
---|
| 130 | storage_status_t storage_save (irc_t *irc, int overwrite) |
---|
| 131 | { |
---|
[764c7d1] | 132 | storage_status_t st; |
---|
| 133 | |
---|
| 134 | otr_save(irc); |
---|
| 135 | st = ((storage_t *)global.storage->data)->save(irc, overwrite); |
---|
| 136 | return st; |
---|
[ab49fdc] | 137 | } |
---|
| 138 | |
---|
| 139 | storage_status_t storage_remove (const char *nick, const char *password) |
---|
| 140 | { |
---|
[b73ac9c] | 141 | GList *gl; |
---|
| 142 | storage_status_t ret = STORAGE_OK; |
---|
| 143 | |
---|
| 144 | /* Remove this account from all storage backends. If this isn't |
---|
| 145 | * done, the account will still be usable, it'd just be |
---|
| 146 | * loaded from a different backend. */ |
---|
| 147 | for (gl = global.storage; gl; gl = gl->next) { |
---|
| 148 | storage_t *st = gl->data; |
---|
| 149 | storage_status_t status; |
---|
| 150 | |
---|
| 151 | status = st->remove(nick, password); |
---|
| 152 | if (status != STORAGE_NO_SUCH_USER && |
---|
| 153 | status != STORAGE_OK) |
---|
| 154 | ret = status; |
---|
| 155 | } |
---|
[764c7d1] | 156 | if (ret == STORAGE_OK) { |
---|
| 157 | otr_remove(nick); |
---|
| 158 | } |
---|
[b73ac9c] | 159 | |
---|
| 160 | return ret; |
---|
[ab49fdc] | 161 | } |
---|
| 162 | |
---|
| 163 | storage_status_t storage_rename (const char *onick, const char *nnick, const char *password) |
---|
| 164 | { |
---|
[b73ac9c] | 165 | storage_status_t status; |
---|
| 166 | GList *gl = global.storage; |
---|
| 167 | storage_t *primary_storage = gl->data; |
---|
| 168 | irc_t *irc; |
---|
[764c7d1] | 169 | |
---|
[b73ac9c] | 170 | /* First, try to rename in the current write backend, assuming onick |
---|
| 171 | * is stored there */ |
---|
| 172 | status = primary_storage->rename(onick, nnick, password); |
---|
[764c7d1] | 173 | if (status != STORAGE_NO_SUCH_USER) { |
---|
| 174 | otr_rename(onick, nnick); |
---|
[b73ac9c] | 175 | return status; |
---|
[764c7d1] | 176 | } |
---|
[b73ac9c] | 177 | |
---|
| 178 | /* Try to load from a migration backend and save to the current backend. |
---|
| 179 | * Explicitly remove the account from the migration backend as otherwise |
---|
| 180 | * it'd still be usable under the old name */ |
---|
| 181 | |
---|
| 182 | irc = g_new0(irc_t, 1); |
---|
| 183 | status = storage_load(onick, password, irc); |
---|
| 184 | if (status != STORAGE_OK) { |
---|
| 185 | irc_free(irc); |
---|
| 186 | return status; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | g_free(irc->nick); |
---|
| 190 | irc->nick = g_strdup(nnick); |
---|
| 191 | |
---|
| 192 | status = storage_save(irc, FALSE); |
---|
| 193 | if (status != STORAGE_OK) { |
---|
| 194 | irc_free(irc); |
---|
| 195 | return status; |
---|
| 196 | } |
---|
| 197 | irc_free(irc); |
---|
| 198 | |
---|
| 199 | storage_remove(onick, password); |
---|
[764c7d1] | 200 | otr_rename(onick, nnick); |
---|
[b73ac9c] | 201 | |
---|
| 202 | return STORAGE_OK; |
---|
[ab49fdc] | 203 | } |
---|