source: storage_xml.c @ 5ebff60

Last change on this file since 5ebff60 was 5ebff60, checked in by dequis <dx@…>, at 2015-02-20T22:50:54Z

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

  • Property mode set to 100644
File size: 11.5 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                local = protocol_account_islocal(protocol);
106        }
107
108        if (!handle || !pass_b64 || !protocol || !prpl) {
109                return XT_ABORT;
110        } else if ((pass_len = base64_decode(pass_b64, (unsigned char **) &pass_cr)) &&
111                   arc_decode(pass_cr, pass_len, &password, xd->given_pass) >= 0) {
112                acc = account_add(xd->irc->b, prpl, handle, password);
113                if (server) {
114                        set_setstr(&acc->set, "server", server);
115                }
116                if (autoconnect) {
117                        set_setstr(&acc->set, "auto_connect", autoconnect);
118                }
119                if (tag) {
120                        set_setstr(&acc->set, "tag", tag);
121                }
122                if (local) {
123                        acc->flags |= ACC_FLAG_LOCAL;
124                }
125        } else {
126                return XT_ABORT;
127        }
128
129        g_free(pass_cr);
130        g_free(password);
131
132        handle_settings(node, &acc->set);
133
134        for (c = node->children; (c = xt_find_node(c, "buddy")); c = c->next) {
135                char *handle, *nick;
136
137                handle = xt_find_attr(c, "handle");
138                nick = xt_find_attr(c, "nick");
139
140                if (handle && nick) {
141                        nick_set_raw(acc, handle, nick);
142                } else {
143                        return XT_ABORT;
144                }
145        }
146        return XT_HANDLED;
147}
148
149static xt_status handle_channel(struct xt_node *node, gpointer data)
150{
151        struct xml_parsedata *xd = data;
152        irc_channel_t *ic;
153        char *name, *type;
154
155        name = xt_find_attr(node, "name");
156        type = xt_find_attr(node, "type");
157
158        if (!name || !type) {
159                return XT_ABORT;
160        }
161
162        /* The channel may exist already, for example if it's &bitlbee.
163           Also, it's possible that the user just reconnected and the
164           IRC client already rejoined all channels it was in. They
165           should still get the right settings. */
166        if ((ic = irc_channel_by_name(xd->irc, name)) ||
167            (ic = irc_channel_new(xd->irc, name))) {
168                set_setstr(&ic->set, "type", type);
169        }
170
171        handle_settings(node, &ic->set);
172
173        return XT_HANDLED;
174}
175
176static const struct xt_handler_entry handlers[] = {
177        { "account", "user", handle_account, },
178        { "channel", "user", handle_channel, },
179        { NULL,      NULL,   NULL, },
180};
181
182static storage_status_t xml_load_real(irc_t *irc, const char *my_nick, const char *password, xml_pass_st action)
183{
184        struct xml_parsedata xd[1];
185        char *fn, buf[2048];
186        int fd, st;
187        struct xt_parser *xp = NULL;
188        struct xt_node *node;
189        storage_status_t ret = STORAGE_OTHER_ERROR;
190
191        xd->irc = irc;
192        strncpy(xd->given_nick, my_nick, MAX_NICK_LENGTH);
193        xd->given_nick[MAX_NICK_LENGTH] = '\0';
194        nick_lc(NULL, xd->given_nick);
195        xd->given_pass = (char *) password;
196
197        fn = g_strconcat(global.conf->configdir, xd->given_nick, ".xml", NULL);
198        if ((fd = open(fn, O_RDONLY)) < 0) {
199                ret = STORAGE_NO_SUCH_USER;
200                goto error;
201        }
202
203        xp = xt_new(handlers, xd);
204        while ((st = read(fd, buf, sizeof(buf))) > 0) {
205                st = xt_feed(xp, buf, st);
206                if (st != 1) {
207                        break;
208                }
209        }
210        close(fd);
211        if (st != 0) {
212                goto error;
213        }
214
215        node = xp->root;
216        if (node == NULL || node->next != NULL || strcmp(node->name, "user") != 0) {
217                goto error;
218        }
219
220        {
221                char *nick = xt_find_attr(node, "nick");
222                char *pass = xt_find_attr(node, "password");
223
224                if (!nick || !pass) {
225                        goto error;
226                } else if ((st = md5_verify_password(xd->given_pass, pass)) != 0) {
227                        ret = STORAGE_INVALID_PASSWORD;
228                        goto error;
229                }
230        }
231
232        if (action == XML_PASS_CHECK_ONLY) {
233                ret = STORAGE_OK;
234                goto error;
235        }
236
237        /* DO NOT call xt_handle() before verifying the password! */
238        if (xt_handle(xp, NULL, 1) == XT_HANDLED) {
239                ret = STORAGE_OK;
240        }
241
242        handle_settings(node, &xd->irc->b->set);
243
244error:
245        xt_free(xp);
246        g_free(fn);
247        return ret;
248}
249
250static storage_status_t xml_load(irc_t *irc, const char *password)
251{
252        return xml_load_real(irc, irc->user->nick, password, XML_PASS_UNKNOWN);
253}
254
255static storage_status_t xml_check_pass(const char *my_nick, const char *password)
256{
257        return xml_load_real(NULL, my_nick, password, XML_PASS_CHECK_ONLY);
258}
259
260
261static gboolean xml_generate_nick(gpointer key, gpointer value, gpointer data);
262static void xml_generate_settings(struct xt_node *cur, set_t **head);
263
264struct xt_node *xml_generate(irc_t *irc)
265{
266        char *pass_buf = NULL;
267        account_t *acc;
268        md5_byte_t pass_md5[21];
269        md5_state_t md5_state;
270        GSList *l;
271        struct xt_node *root, *cur;
272
273        /* Generate a salted md5sum of the password. Use 5 bytes for the salt
274           (to prevent dictionary lookups of passwords) to end up with a 21-
275           byte password hash, more convenient for base64 encoding. */
276        random_bytes(pass_md5 + 16, 5);
277        md5_init(&md5_state);
278        md5_append(&md5_state, (md5_byte_t *) irc->password, strlen(irc->password));
279        md5_append(&md5_state, pass_md5 + 16, 5);   /* Add the salt. */
280        md5_finish(&md5_state, pass_md5);
281        /* Save the hash in base64-encoded form. */
282        pass_buf = base64_encode(pass_md5, 21);
283
284        root = cur = xt_new_node("user", NULL, NULL);
285        xt_add_attr(cur, "nick", irc->user->nick);
286        xt_add_attr(cur, "password", pass_buf);
287        xt_add_attr(cur, "version", XML_FORMAT_VERSION);
288
289        g_free(pass_buf);
290
291        xml_generate_settings(cur, &irc->b->set);
292
293        for (acc = irc->b->accounts; acc; acc = acc->next) {
294                unsigned char *pass_cr;
295                char *pass_b64;
296                int pass_len;
297
298                pass_len = arc_encode(acc->pass, strlen(acc->pass), (unsigned char **) &pass_cr, irc->password, 12);
299                pass_b64 = base64_encode(pass_cr, pass_len);
300                g_free(pass_cr);
301
302                cur = xt_new_node("account", NULL, NULL);
303                xt_add_attr(cur, "protocol", acc->prpl->name);
304                xt_add_attr(cur, "handle", acc->user);
305                xt_add_attr(cur, "password", pass_b64);
306                xt_add_attr(cur, "autoconnect", acc->auto_connect ? "true" : "false");
307                xt_add_attr(cur, "tag", acc->tag);
308                if (acc->server && acc->server[0]) {
309                        xt_add_attr(cur, "server", acc->server);
310                }
311
312                g_free(pass_b64);
313
314                /* This probably looks pretty strange. g_hash_table_foreach
315                   is quite a PITA already (but it can't get much better in
316                   C without using #define, I'm afraid), and it
317                   doesn't seem to be possible to abort the foreach on write
318                   errors, so instead let's use the _find function and
319                   return TRUE on write errors. Which means, if we found
320                   something, there was an error. :-) */
321                g_hash_table_find(acc->nicks, xml_generate_nick, cur);
322
323                xml_generate_settings(cur, &acc->set);
324
325                xt_add_child(root, cur);
326        }
327
328        for (l = irc->channels; l; l = l->next) {
329                irc_channel_t *ic = l->data;
330
331                if (ic->flags & IRC_CHANNEL_TEMP) {
332                        continue;
333                }
334
335                cur = xt_new_node("channel", NULL, NULL);
336                xt_add_attr(cur, "name", ic->name);
337                xt_add_attr(cur, "type", set_getstr(&ic->set, "type"));
338
339                xml_generate_settings(cur, &ic->set);
340
341                xt_add_child(root, cur);
342        }
343
344        return root;
345}
346
347static gboolean xml_generate_nick(gpointer key, gpointer value, gpointer data)
348{
349        struct xt_node *node = xt_new_node("buddy", NULL, NULL);
350
351        xt_add_attr(node, "handle", key);
352        xt_add_attr(node, "nick", value);
353        xt_add_child((struct xt_node *) data, node);
354
355        return FALSE;
356}
357
358static void xml_generate_settings(struct xt_node *cur, set_t **head)
359{
360        set_t *set;
361
362        for (set = *head; set; set = set->next) {
363                if (set->value && !(set->flags & SET_NOSAVE)) {
364                        struct xt_node *xset;
365                        xt_add_child(cur, xset = xt_new_node("setting", set->value, NULL));
366                        xt_add_attr(xset, "name", set->key);
367                }
368        }
369}
370
371static storage_status_t xml_save(irc_t *irc, int overwrite)
372{
373        storage_status_t ret = STORAGE_OK;
374        char path[512], *path2 = NULL, *xml = NULL;
375        struct xt_node *tree = NULL;
376        size_t len;
377        int fd;
378
379        path2 = g_strdup(irc->user->nick);
380        nick_lc(NULL, path2);
381        g_snprintf(path, sizeof(path) - 20, "%s%s%s", global.conf->configdir, path2, ".xml");
382        g_free(path2);
383
384        if (!overwrite && g_access(path, F_OK) == 0) {
385                return STORAGE_ALREADY_EXISTS;
386        }
387
388        strcat(path, ".XXXXXX");
389        if ((fd = mkstemp(path)) < 0) {
390                irc_rootmsg(irc, "Error while opening configuration file.");
391                return STORAGE_OTHER_ERROR;
392        }
393
394        tree = xml_generate(irc);
395        xml = xt_to_string_i(tree);
396        len = strlen(xml);
397        if (write(fd, xml, len) != len ||
398            fsync(fd) != 0 ||   /* #559 */
399            close(fd) != 0) {
400                goto error;
401        }
402
403        path2 = g_strndup(path, strlen(path) - 7);
404        if (rename(path, path2) != 0) {
405                g_free(path2);
406                goto error;
407        }
408        g_free(path2);
409
410        goto finish;
411
412error:
413        irc_rootmsg(irc, "Write error. Disk full?");
414        ret = STORAGE_OTHER_ERROR;
415
416finish:
417        close(fd);
418        unlink(path);
419        g_free(xml);
420        xt_free_node(tree);
421
422        return ret;
423}
424
425
426static storage_status_t xml_remove(const char *nick, const char *password)
427{
428        char s[512], *lc;
429        storage_status_t status;
430
431        status = xml_check_pass(nick, password);
432        if (status != STORAGE_OK) {
433                return status;
434        }
435
436        lc = g_strdup(nick);
437        nick_lc(NULL, lc);
438        g_snprintf(s, 511, "%s%s%s", global.conf->configdir, lc, ".xml");
439        g_free(lc);
440
441        if (unlink(s) == -1) {
442                return STORAGE_OTHER_ERROR;
443        }
444
445        return STORAGE_OK;
446}
447
448storage_t storage_xml = {
449        .name = "xml",
450        .init = xml_init,
451        .check_pass = xml_check_pass,
452        .remove = xml_remove,
453        .load = xml_load,
454        .save = xml_save
455};
Note: See TracBrowser for help on using the repository browser.