source: storage_xml.c @ 1bdc669

Last change on this file since 1bdc669 was 1bdc669, checked in by GitHub <noreply@…>, at 2023-02-23T23:48:10Z

Migrate internal users of md5.h to using GChecksum directly (#169)

  • Use GChecksum directly rather than md5 wrapper
  • Mark md5 functions as deprecated.
  • Migrate more users of md5.h to GChecksum
  • Property mode set to 100644
File size: 13.1 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 "xmltree.h"
31
32#include <glib/gstdio.h>
33
34typedef enum {
35        XML_PASS_CHECK = 0,
36        XML_LOAD
37} xml_action;
38
39/* To make it easier later when extending the format: */
40#define XML_FORMAT_VERSION "1"
41
42struct xml_parsedata {
43        irc_t *irc;
44        char given_nick[MAX_NICK_LENGTH + 1];
45        char *given_pass;
46};
47
48static void xml_init(void)
49{
50        if (g_access(global.conf->configdir, F_OK) != 0) {
51                log_message(LOGLVL_WARNING,
52                            "The configuration directory `%s' does not exist. Configuration won't be saved.",
53                            global.conf->configdir);
54        } else if (g_access(global.conf->configdir, F_OK) != 0 ||
55                   g_access(global.conf->configdir, W_OK) != 0) {
56                log_message(LOGLVL_WARNING, "Permission problem: Can't read/write from/to `%s'.",
57                            global.conf->configdir);
58        }
59}
60
61static void handle_settings(struct xt_node *node, set_t **head)
62{
63        struct xt_node *c;
64        struct set *s;
65
66        for (c = node->children; (c = xt_find_node(c, "setting")); c = c->next) {
67                char *name = xt_find_attr(c, "name");
68                char *locked = xt_find_attr(c, "locked");
69
70                if (!name) {
71                        continue;
72                }
73
74                if (strcmp(node->name, "account") == 0) {
75                        set_t *s = set_find(head, name);
76                        if (s && (s->flags & ACC_SET_ONLINE_ONLY)) {
77                                continue; /* U can't touch this! */
78                        }
79                }
80                set_setstr(head, name, c->text);
81                if (locked && !g_strcasecmp(locked, "true")) {
82                        s = set_find(head, name);
83                        if (s) {
84                                s->flags |= SET_LOCKED;
85                        }
86                }
87        }
88}
89
90/* Use for unsupported/not-found protocols. Save settings as-is but don't allow changes. */
91static void handle_settings_raw(struct xt_node *node, set_t **head)
92{
93        struct xt_node *c;
94
95        for (c = node->children; (c = xt_find_node(c, "setting")); c = c->next) {
96                char *name = xt_find_attr(c, "name");
97
98                if (!name) {
99                        continue;
100                }
101
102                set_t *s = set_add(head, name, NULL, NULL, NULL);
103                set_setstr(head, name, c->text);
104                s->flags |= SET_HIDDEN |
105                            ACC_SET_OFFLINE_ONLY | ACC_SET_ONLINE_ONLY;
106        }
107}
108
109static xt_status handle_account(struct xt_node *node, gpointer data)
110{
111        struct xml_parsedata *xd = data;
112        char *protocol, *handle, *server, *password = NULL, *autoconnect, *tag, *locked;
113        char *pass_b64 = NULL;
114        unsigned char *pass_cr = NULL;
115        int pass_len, local = 0;
116        struct prpl *prpl = NULL;
117        account_t *acc;
118        struct xt_node *c;
119
120        handle = xt_find_attr(node, "handle");
121        pass_b64 = xt_find_attr(node, "password");
122        server = xt_find_attr(node, "server");
123        autoconnect = xt_find_attr(node, "autoconnect");
124        tag = xt_find_attr(node, "tag");
125        locked = xt_find_attr(node, "locked");
126
127        protocol = xt_find_attr(node, "protocol");
128        if (protocol) {
129                prpl = find_protocol(protocol);
130                if (!prpl) {
131                        irc_rootmsg(xd->irc, "Warning: Protocol not found: `%s'", protocol);
132                        prpl = (struct prpl*) &protocol_missing;
133                }
134                local = protocol_account_islocal(protocol);
135        }
136
137        if (!handle || !pass_b64 || !protocol || !prpl) {
138                return XT_ABORT;
139        }
140
141        pass_len = base64_decode(pass_b64, (unsigned char **) &pass_cr);
142        if (xd->irc->auth_backend) {
143                password = g_strdup((char *)pass_cr);
144        } else {
145                pass_len = arc_decode(pass_cr, pass_len, &password, xd->given_pass);
146                if (pass_len < 0) {
147                        g_free(pass_cr);
148                        g_free(password);
149                        return XT_ABORT;
150                }
151        }
152
153        acc = account_add(xd->irc->b, prpl, handle, password);
154        if (server) {
155                set_setstr(&acc->set, "server", server);
156        }
157        if (autoconnect) {
158                set_setstr(&acc->set, "auto_connect", autoconnect);
159        }
160        if (tag) {
161                set_setstr(&acc->set, "tag", tag);
162        }
163        if (local) {
164                acc->flags |= ACC_FLAG_LOCAL;
165        }
166        if (locked && !g_strcasecmp(locked, "true")) {
167                acc->flags |= ACC_FLAG_LOCKED;
168        }
169        if (prpl == &protocol_missing) {
170                set_t *s = set_add(&acc->set, "_protocol_name", protocol, NULL, NULL);
171                s->flags |= SET_HIDDEN | SET_NOSAVE |
172                            ACC_SET_OFFLINE_ONLY | ACC_SET_ONLINE_ONLY;
173        }
174
175        g_free(pass_cr);
176        g_free(password);
177
178        if (prpl == &protocol_missing) {
179                handle_settings_raw(node, &acc->set);
180        } else {
181                handle_settings(node, &acc->set);
182        }
183
184        for (c = node->children; (c = xt_find_node(c, "buddy")); c = c->next) {
185                char *handle, *nick;
186
187                handle = xt_find_attr(c, "handle");
188                nick = xt_find_attr(c, "nick");
189
190                if (handle && nick) {
191                        nick_set_raw(acc, handle, nick);
192                } else {
193                        return XT_ABORT;
194                }
195        }
196        return XT_HANDLED;
197}
198
199static xt_status handle_channel(struct xt_node *node, gpointer data)
200{
201        struct xml_parsedata *xd = data;
202        irc_channel_t *ic;
203        char *name, *type;
204
205        name = xt_find_attr(node, "name");
206        type = xt_find_attr(node, "type");
207
208        if (!name || !type) {
209                return XT_ABORT;
210        }
211
212        /* The channel may exist already, for example if it's &bitlbee.
213           Also, it's possible that the user just reconnected and the
214           IRC client already rejoined all channels it was in. They
215           should still get the right settings. */
216        if ((ic = irc_channel_by_name(xd->irc, name)) ||
217            (ic = irc_channel_new(xd->irc, name))) {
218                set_setstr(&ic->set, "type", type);
219        }
220
221        handle_settings(node, &ic->set);
222
223        return XT_HANDLED;
224}
225
226static const struct xt_handler_entry handlers[] = {
227        { "account", "user", handle_account, },
228        { "channel", "user", handle_channel, },
229        { NULL,      NULL,   NULL, },
230};
231
232static storage_status_t xml_load_real(irc_t *irc, const char *my_nick, const char *password, xml_action action)
233{
234        struct xml_parsedata xd[1];
235        char *fn, buf[2048];
236        int fd, st;
237        struct xt_parser *xp = NULL;
238        struct xt_node *node;
239        storage_status_t ret = STORAGE_OTHER_ERROR;
240
241        xd->irc = irc;
242        strncpy(xd->given_nick, my_nick, MAX_NICK_LENGTH);
243        xd->given_nick[MAX_NICK_LENGTH] = '\0';
244        nick_lc(NULL, xd->given_nick);
245        xd->given_pass = (char *) password;
246
247        fn = g_strconcat(global.conf->configdir, xd->given_nick, ".xml", NULL);
248        if ((fd = open(fn, O_RDONLY)) < 0) {
249                if (errno == ENOENT) {
250                        ret = STORAGE_NO_SUCH_USER;
251                } else {
252                        irc_rootmsg(irc, "Error loading user config: %s", g_strerror(errno));
253                }
254                goto error;
255        }
256
257        xp = xt_new(handlers, xd);
258        while ((st = read(fd, buf, sizeof(buf))) > 0) {
259                st = xt_feed(xp, buf, st);
260                if (st != 1) {
261                        break;
262                }
263        }
264        close(fd);
265        if (st != 0) {
266                goto error;
267        }
268
269        node = xp->root;
270        if (node == NULL || node->next != NULL || strcmp(node->name, "user") != 0) {
271                goto error;
272        }
273
274        if (action == XML_PASS_CHECK) {
275                char *nick = xt_find_attr(node, "nick");
276                char *pass = xt_find_attr(node, "password");
277                char *backend = xt_find_attr(node, "auth_backend");
278
279                if (!nick || !(pass || backend)) {
280                        goto error;
281                }
282
283                if (backend) {
284                        g_free(xd->irc->auth_backend);
285                        xd->irc->auth_backend = g_strdup(backend);
286                        ret = STORAGE_CHECK_BACKEND;
287                } else if ((st = md5_verify_password(xd->given_pass, pass)) != 0) {
288                        ret = STORAGE_INVALID_PASSWORD;
289                } else {
290                        ret = STORAGE_OK;
291                }
292                goto error;
293        }
294
295        handle_settings(node, &xd->irc->b->set);
296
297        if (xt_handle(xp, NULL, 1) == XT_HANDLED) {
298                ret = STORAGE_OK;
299        }
300
301error:
302        xt_free(xp);
303        g_free(fn);
304        return ret;
305}
306
307static storage_status_t xml_load(irc_t *irc, const char *password)
308{
309        return xml_load_real(irc, irc->user->nick, password, XML_LOAD);
310}
311
312static storage_status_t xml_check_pass(irc_t *irc, const char *my_nick, const char *password)
313{
314        return xml_load_real(irc, my_nick, password, XML_PASS_CHECK);
315}
316
317
318static void xml_generate_settings(struct xt_node *cur, set_t **head);
319
320struct xt_node *xml_generate(irc_t *irc)
321{
322        char *pass_buf = NULL;
323        account_t *acc;
324        guint8 pass_md5[21];
325        GChecksum *md5_state;
326        GSList *l;
327        struct xt_node *root, *cur;
328        gsize digest_len = MD5_HASH_SIZE;
329
330        root = cur = xt_new_node("user", NULL, NULL);
331        if (irc->auth_backend) {
332                xt_add_attr(cur, "auth_backend", irc->auth_backend);
333        } else {
334                /* Generate a salted md5sum of the password. Use 5 bytes for the salt
335                   (to prevent dictionary lookups of passwords) to end up with a 21-
336                   byte password hash, more convenient for base64 encoding. */
337                random_bytes(pass_md5 + 16, 5);
338                md5_state = g_checksum_new(G_CHECKSUM_MD5);
339                g_checksum_update(md5_state, (guint8 *) irc->password, strlen(irc->password));
340                g_checksum_update(md5_state, pass_md5 + 16, 5);   /* Add the salt. */
341                g_checksum_get_digest(md5_state, pass_md5, &digest_len);
342                g_checksum_free(md5_state);
343                /* Save the hash in base64-encoded form. */
344                pass_buf = base64_encode(pass_md5, 21);
345                xt_add_attr(cur, "password", pass_buf);
346                g_free(pass_buf);
347        }
348
349        xt_add_attr(cur, "nick", irc->user->nick);
350        xt_add_attr(cur, "version", XML_FORMAT_VERSION);
351
352        xml_generate_settings(cur, &irc->b->set);
353
354        for (acc = irc->b->accounts; acc; acc = acc->next) {
355                GHashTableIter iter;
356                gpointer key, value;
357                unsigned char *pass_cr;
358                char *pass_b64;
359                int pass_len;
360
361                if(irc->auth_backend) {
362                        /* If we don't "own" the password, it may change without us
363                         * knowing, so we cannot encrypt the data, as we then may not be
364                         * able to decrypt it */
365                        pass_b64 = base64_encode((unsigned char *)acc->pass, strlen(acc->pass));
366                } else {
367                        pass_len = arc_encode(acc->pass, strlen(acc->pass), (unsigned char **) &pass_cr, irc->password, 12);
368                        pass_b64 = base64_encode(pass_cr, pass_len);
369                        g_free(pass_cr);
370                }
371
372                cur = xt_new_node("account", NULL, NULL);
373                if (acc->prpl == &protocol_missing) {
374                        xt_add_attr(cur, "protocol", set_getstr(&acc->set, "_protocol_name"));
375                } else {
376                        xt_add_attr(cur, "protocol", acc->prpl->name);
377                }
378                xt_add_attr(cur, "handle", acc->user);
379                xt_add_attr(cur, "password", pass_b64);
380                xt_add_attr(cur, "autoconnect", acc->auto_connect ? "true" : "false");
381                xt_add_attr(cur, "tag", acc->tag);
382                if (acc->server && acc->server[0]) {
383                        xt_add_attr(cur, "server", acc->server);
384                }
385                if (acc->flags & ACC_FLAG_LOCKED) {
386                        xt_add_attr(cur, "locked", "true");
387                }
388
389                g_free(pass_b64);
390
391                g_hash_table_iter_init(&iter, acc->nicks);
392                while (g_hash_table_iter_next(&iter, &key, &value)) {
393                        struct xt_node *node = xt_new_node("buddy", NULL, NULL);
394                        xt_add_attr(node, "handle", key);
395                        xt_add_attr(node, "nick", value);
396                        xt_add_child(cur, node);
397                }
398
399                xml_generate_settings(cur, &acc->set);
400
401                xt_add_child(root, cur);
402        }
403
404        for (l = irc->channels; l; l = l->next) {
405                irc_channel_t *ic = l->data;
406
407                if (ic->flags & IRC_CHANNEL_TEMP) {
408                        continue;
409                }
410
411                cur = xt_new_node("channel", NULL, NULL);
412                xt_add_attr(cur, "name", ic->name);
413                xt_add_attr(cur, "type", set_getstr(&ic->set, "type"));
414
415                xml_generate_settings(cur, &ic->set);
416
417                xt_add_child(root, cur);
418        }
419
420        return root;
421}
422
423static void xml_generate_settings(struct xt_node *cur, set_t **head)
424{
425        set_t *set;
426
427        for (set = *head; set; set = set->next) {
428                if (set->value && !(set->flags & SET_NOSAVE)) {
429                        struct xt_node *xset;
430                        xt_add_child(cur, xset = xt_new_node("setting", set->value, NULL));
431                        xt_add_attr(xset, "name", set->key);
432                        if (set->flags & SET_LOCKED) {
433                                xt_add_attr(xset, "locked", "true");
434                        }
435                }
436        }
437}
438
439static storage_status_t xml_save(irc_t *irc, int overwrite)
440{
441        storage_status_t ret = STORAGE_OK;
442        char path[512], *path2 = NULL, *xml = NULL;
443        struct xt_node *tree = NULL;
444        size_t len;
445        int fd;
446
447        path2 = g_strdup(irc->user->nick);
448        nick_lc(NULL, path2);
449        g_snprintf(path, sizeof(path) - 20, "%s%s%s", global.conf->configdir, path2, ".xml");
450        g_free(path2);
451
452        if (!overwrite && g_access(path, F_OK) == 0) {
453                return STORAGE_ALREADY_EXISTS;
454        }
455
456        strcat(path, ".XXXXXX");
457        if ((fd = mkstemp(path)) < 0) {
458                goto error;
459        }
460
461        tree = xml_generate(irc);
462        xml = xt_to_string_i(tree);
463        len = strlen(xml);
464        if (write(fd, xml, len) != len ||
465            fsync(fd) != 0 ||   /* #559 */
466            close(fd) != 0) {
467                goto error;
468        }
469
470        path2 = g_strndup(path, strlen(path) - 7);
471        if (rename(path, path2) != 0) {
472                g_free(path2);
473                goto error;
474        }
475        g_free(path2);
476
477        goto finish;
478
479error:
480        irc_rootmsg(irc, "Write error: %s", g_strerror(errno));
481        ret = STORAGE_OTHER_ERROR;
482
483finish:
484        close(fd);
485        unlink(path);
486        g_free(xml);
487        xt_free_node(tree);
488
489        return ret;
490}
491
492
493static storage_status_t xml_remove(const char *nick)
494{
495        char s[512], *lc;
496
497        lc = g_strdup(nick);
498        nick_lc(NULL, lc);
499        g_snprintf(s, 511, "%s%s%s", global.conf->configdir, lc, ".xml");
500        g_free(lc);
501
502        if (unlink(s) == -1) {
503                return STORAGE_OTHER_ERROR;
504        }
505
506        return STORAGE_OK;
507}
508
509storage_t storage_xml = {
510        .name = "xml",
511        .init = xml_init,
512        .check_pass = xml_check_pass,
513        .remove = xml_remove,
514        .load = xml_load,
515        .save = xml_save
516};
Note: See TracBrowser for help on using the repository browser.