source: storage_xml.c @ 6a48992

Last change on this file since 6a48992 was 7320610, checked in by dequis <dx@…>, at 2015-03-10T07:33:54Z

Various user experience/error reporting improvements

  • Show version as part of the initial message of &bitlbee
  • Use g_strerror() to show actual errors when saving xml configs
  • Only show "The nick is (probably) not registered" for ENOENT, use g_strerror() for the rest of OS errors when loading xml configs
  • Show "Protocol not found: <name>" when find_protocol() returns null, useful when the user uninstalls a plugin accidentally.
  • Suggest the user to check the system clock when getting error 401 from the twitter stream (other REST endpoints show a better error message)
  • Property mode set to 100644
File size: 11.2 KB
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* Storage backend that uses an XMLish format for all data. */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "base64.h"
29#include "arc.h"
30#include "md5.h"
31#include "xmltree.h"
32
33#include <glib/gstdio.h>
34
35typedef enum {
36        XML_PASS_CHECK_ONLY = -1,
37        XML_PASS_UNKNOWN = 0,
38        XML_PASS_WRONG,
39        XML_PASS_OK
40} xml_pass_st;
41
42/* To make it easier later when extending the format: */
43#define XML_FORMAT_VERSION "1"
44
45struct xml_parsedata {
46        irc_t *irc;
47        char given_nick[MAX_NICK_LENGTH + 1];
48        char *given_pass;
49};
50
51static void xml_init(void)
52{
53        if (g_access(global.conf->configdir, F_OK) != 0) {
54                log_message(LOGLVL_WARNING,
55                            "The configuration directory `%s' does not exist. Configuration won't be saved.",
56                            global.conf->configdir);
57        } else if (g_access(global.conf->configdir, F_OK) != 0 ||
58                   g_access(global.conf->configdir, W_OK) != 0) {
59                log_message(LOGLVL_WARNING, "Permission problem: Can't read/write from/to `%s'.",
60                            global.conf->configdir);
61        }
62}
63
64static void handle_settings(struct xt_node *node, set_t **head)
65{
66        struct xt_node *c;
67
68        for (c = node->children; (c = xt_find_node(c, "setting")); c = c->next) {
69                char *name = xt_find_attr(c, "name");
70
71                if (!name) {
72                        continue;
73                }
74
75                if (strcmp(node->name, "account") == 0) {
76                        set_t *s = set_find(head, name);
77                        if (s && (s->flags & ACC_SET_ONLINE_ONLY)) {
78                                continue; /* U can't touch this! */
79                        }
80                }
81                set_setstr(head, name, c->text);
82        }
83}
84
85static xt_status handle_account(struct xt_node *node, gpointer data)
86{
87        struct xml_parsedata *xd = data;
88        char *protocol, *handle, *server, *password = NULL, *autoconnect, *tag;
89        char *pass_b64 = NULL;
90        unsigned char *pass_cr = NULL;
91        int pass_len, local = 0;
92        struct prpl *prpl = NULL;
93        account_t *acc;
94        struct xt_node *c;
95
96        handle = xt_find_attr(node, "handle");
97        pass_b64 = xt_find_attr(node, "password");
98        server = xt_find_attr(node, "server");
99        autoconnect = xt_find_attr(node, "autoconnect");
100        tag = xt_find_attr(node, "tag");
101
102        protocol = xt_find_attr(node, "protocol");
103        if (protocol) {
104                prpl = find_protocol(protocol);
105                if (!prpl) {
106                        irc_rootmsg(xd->irc, "Error loading user config: Protocol not found: `%s'", protocol);
107                        return XT_ABORT;
108                }
109                local = protocol_account_islocal(protocol);
110        }
111
112        if (!handle || !pass_b64 || !protocol || !prpl) {
113                return XT_ABORT;
114        } else if ((pass_len = base64_decode(pass_b64, (unsigned char **) &pass_cr)) &&
115                   arc_decode(pass_cr, pass_len, &password, xd->given_pass) >= 0) {
116                acc = account_add(xd->irc->b, prpl, handle, password);
117                if (server) {
118                        set_setstr(&acc->set, "server", server);
119                }
120                if (autoconnect) {
121                        set_setstr(&acc->set, "auto_connect", autoconnect);
122                }
123                if (tag) {
124                        set_setstr(&acc->set, "tag", tag);
125                }
126                if (local) {
127                        acc->flags |= ACC_FLAG_LOCAL;
128                }
129        } else {
130                return XT_ABORT;
131        }
132
133        g_free(pass_cr);
134        g_free(password);
135
136        handle_settings(node, &acc->set);
137
138        for (c = node->children; (c = xt_find_node(c, "buddy")); c = c->next) {
139                char *handle, *nick;
140
141                handle = xt_find_attr(c, "handle");
142                nick = xt_find_attr(c, "nick");
143
144                if (handle && nick) {
145                        nick_set_raw(acc, handle, nick);
146                } else {
147                        return XT_ABORT;
148                }
149        }
150        return XT_HANDLED;
151}
152
153static xt_status handle_channel(struct xt_node *node, gpointer data)
154{
155        struct xml_parsedata *xd = data;
156        irc_channel_t *ic;
157        char *name, *type;
158
159        name = xt_find_attr(node, "name");
160        type = xt_find_attr(node, "type");
161
162        if (!name || !type) {
163                return XT_ABORT;
164        }
165
166        /* The channel may exist already, for example if it's &bitlbee.
167           Also, it's possible that the user just reconnected and the
168           IRC client already rejoined all channels it was in. They
169           should still get the right settings. */
170        if ((ic = irc_channel_by_name(xd->irc, name)) ||
171            (ic = irc_channel_new(xd->irc, name))) {
172                set_setstr(&ic->set, "type", type);
173        }
174
175        handle_settings(node, &ic->set);
176
177        return XT_HANDLED;
178}
179
180static const struct xt_handler_entry handlers[] = {
181        { "account", "user", handle_account, },
182        { "channel", "user", handle_channel, },
183        { NULL,      NULL,   NULL, },
184};
185
186static storage_status_t xml_load_real(irc_t *irc, const char *my_nick, const char *password, xml_pass_st action)
187{
188        struct xml_parsedata xd[1];
189        char *fn, buf[2048];
190        int fd, st;
191        struct xt_parser *xp = NULL;
192        struct xt_node *node;
193        storage_status_t ret = STORAGE_OTHER_ERROR;
194
195        xd->irc = irc;
196        strncpy(xd->given_nick, my_nick, MAX_NICK_LENGTH);
197        xd->given_nick[MAX_NICK_LENGTH] = '\0';
198        nick_lc(NULL, xd->given_nick);
199        xd->given_pass = (char *) password;
200
201        fn = g_strconcat(global.conf->configdir, xd->given_nick, ".xml", NULL);
202        if ((fd = open(fn, O_RDONLY)) < 0) {
203                if (errno == ENOENT) {
204                        ret = STORAGE_NO_SUCH_USER;
205                } else {
206                        irc_rootmsg(irc, "Error loading user config: %s", g_strerror(errno));
207                }
208                goto error;
209        }
210
211        xp = xt_new(handlers, xd);
212        while ((st = read(fd, buf, sizeof(buf))) > 0) {
213                st = xt_feed(xp, buf, st);
214                if (st != 1) {
215                        break;
216                }
217        }
218        close(fd);
219        if (st != 0) {
220                goto error;
221        }
222
223        node = xp->root;
224        if (node == NULL || node->next != NULL || strcmp(node->name, "user") != 0) {
225                goto error;
226        }
227
228        {
229                char *nick = xt_find_attr(node, "nick");
230                char *pass = xt_find_attr(node, "password");
231
232                if (!nick || !pass) {
233                        goto error;
234                } else if ((st = md5_verify_password(xd->given_pass, pass)) != 0) {
235                        ret = STORAGE_INVALID_PASSWORD;
236                        goto error;
237                }
238        }
239
240        if (action == XML_PASS_CHECK_ONLY) {
241                ret = STORAGE_OK;
242                goto error;
243        }
244
245        /* DO NOT call xt_handle() before verifying the password! */
246        if (xt_handle(xp, NULL, 1) == XT_HANDLED) {
247                ret = STORAGE_OK;
248        }
249
250        handle_settings(node, &xd->irc->b->set);
251
252error:
253        xt_free(xp);
254        g_free(fn);
255        return ret;
256}
257
258static storage_status_t xml_load(irc_t *irc, const char *password)
259{
260        return xml_load_real(irc, irc->user->nick, password, XML_PASS_UNKNOWN);
261}
262
263static storage_status_t xml_check_pass(const char *my_nick, const char *password)
264{
265        return xml_load_real(NULL, my_nick, password, XML_PASS_CHECK_ONLY);
266}
267
268
269static void xml_generate_settings(struct xt_node *cur, set_t **head);
270
271struct xt_node *xml_generate(irc_t *irc)
272{
273        char *pass_buf = NULL;
274        account_t *acc;
275        md5_byte_t pass_md5[21];
276        md5_state_t md5_state;
277        GSList *l;
278        struct xt_node *root, *cur;
279
280        /* Generate a salted md5sum of the password. Use 5 bytes for the salt
281           (to prevent dictionary lookups of passwords) to end up with a 21-
282           byte password hash, more convenient for base64 encoding. */
283        random_bytes(pass_md5 + 16, 5);
284        md5_init(&md5_state);
285        md5_append(&md5_state, (md5_byte_t *) irc->password, strlen(irc->password));
286        md5_append(&md5_state, pass_md5 + 16, 5);   /* Add the salt. */
287        md5_finish(&md5_state, pass_md5);
288        /* Save the hash in base64-encoded form. */
289        pass_buf = base64_encode(pass_md5, 21);
290
291        root = cur = xt_new_node("user", NULL, NULL);
292        xt_add_attr(cur, "nick", irc->user->nick);
293        xt_add_attr(cur, "password", pass_buf);
294        xt_add_attr(cur, "version", XML_FORMAT_VERSION);
295
296        g_free(pass_buf);
297
298        xml_generate_settings(cur, &irc->b->set);
299
300        for (acc = irc->b->accounts; acc; acc = acc->next) {
301                GHashTableIter iter;
302                gpointer key, value;
303                unsigned char *pass_cr;
304                char *pass_b64;
305                int pass_len;
306
307                pass_len = arc_encode(acc->pass, strlen(acc->pass), (unsigned char **) &pass_cr, irc->password, 12);
308                pass_b64 = base64_encode(pass_cr, pass_len);
309                g_free(pass_cr);
310
311                cur = xt_new_node("account", NULL, NULL);
312                xt_add_attr(cur, "protocol", acc->prpl->name);
313                xt_add_attr(cur, "handle", acc->user);
314                xt_add_attr(cur, "password", pass_b64);
315                xt_add_attr(cur, "autoconnect", acc->auto_connect ? "true" : "false");
316                xt_add_attr(cur, "tag", acc->tag);
317                if (acc->server && acc->server[0]) {
318                        xt_add_attr(cur, "server", acc->server);
319                }
320
321                g_free(pass_b64);
322
323                g_hash_table_iter_init(&iter, acc->nicks);
324                while (g_hash_table_iter_next(&iter, &key, &value)) {
325                        struct xt_node *node = xt_new_node("buddy", NULL, NULL);
326                        xt_add_attr(node, "handle", key);
327                        xt_add_attr(node, "nick", value);
328                        xt_add_child(cur, node);
329                }
330
331                xml_generate_settings(cur, &acc->set);
332
333                xt_add_child(root, cur);
334        }
335
336        for (l = irc->channels; l; l = l->next) {
337                irc_channel_t *ic = l->data;
338
339                if (ic->flags & IRC_CHANNEL_TEMP) {
340                        continue;
341                }
342
343                cur = xt_new_node("channel", NULL, NULL);
344                xt_add_attr(cur, "name", ic->name);
345                xt_add_attr(cur, "type", set_getstr(&ic->set, "type"));
346
347                xml_generate_settings(cur, &ic->set);
348
349                xt_add_child(root, cur);
350        }
351
352        return root;
353}
354
355static void xml_generate_settings(struct xt_node *cur, set_t **head)
356{
357        set_t *set;
358
359        for (set = *head; set; set = set->next) {
360                if (set->value && !(set->flags & SET_NOSAVE)) {
361                        struct xt_node *xset;
362                        xt_add_child(cur, xset = xt_new_node("setting", set->value, NULL));
363                        xt_add_attr(xset, "name", set->key);
364                }
365        }
366}
367
368static storage_status_t xml_save(irc_t *irc, int overwrite)
369{
370        storage_status_t ret = STORAGE_OK;
371        char path[512], *path2 = NULL, *xml = NULL;
372        struct xt_node *tree = NULL;
373        size_t len;
374        int fd;
375
376        path2 = g_strdup(irc->user->nick);
377        nick_lc(NULL, path2);
378        g_snprintf(path, sizeof(path) - 20, "%s%s%s", global.conf->configdir, path2, ".xml");
379        g_free(path2);
380
381        if (!overwrite && g_access(path, F_OK) == 0) {
382                return STORAGE_ALREADY_EXISTS;
383        }
384
385        strcat(path, ".XXXXXX");
386        if ((fd = mkstemp(path)) < 0) {
387                goto error;
388        }
389
390        tree = xml_generate(irc);
391        xml = xt_to_string_i(tree);
392        len = strlen(xml);
393        if (write(fd, xml, len) != len ||
394            fsync(fd) != 0 ||   /* #559 */
395            close(fd) != 0) {
396                goto error;
397        }
398
399        path2 = g_strndup(path, strlen(path) - 7);
400        if (rename(path, path2) != 0) {
401                g_free(path2);
402                goto error;
403        }
404        g_free(path2);
405
406        goto finish;
407
408error:
409        irc_rootmsg(irc, "Write error: %s", g_strerror(errno));
410        ret = STORAGE_OTHER_ERROR;
411
412finish:
413        close(fd);
414        unlink(path);
415        g_free(xml);
416        xt_free_node(tree);
417
418        return ret;
419}
420
421
422static storage_status_t xml_remove(const char *nick, const char *password)
423{
424        char s[512], *lc;
425        storage_status_t status;
426
427        status = xml_check_pass(nick, password);
428        if (status != STORAGE_OK) {
429                return status;
430        }
431
432        lc = g_strdup(nick);
433        nick_lc(NULL, lc);
434        g_snprintf(s, 511, "%s%s%s", global.conf->configdir, lc, ".xml");
435        g_free(lc);
436
437        if (unlink(s) == -1) {
438                return STORAGE_OTHER_ERROR;
439        }
440
441        return STORAGE_OK;
442}
443
444storage_t storage_xml = {
445        .name = "xml",
446        .init = xml_init,
447        .check_pass = xml_check_pass,
448        .remove = xml_remove,
449        .load = xml_load,
450        .save = xml_save
451};
Note: See TracBrowser for help on using the repository browser.