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
RevLine 
[5ebff60]1/********************************************************************\
[a312b6b]2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[a338faa]4  * Copyright 2002-2012 Wilmer van der Gaast and others                *
[a312b6b]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;
[6f10697]22  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
23  Fifth Floor, Boston, MA  02110-1301  USA
[a312b6b]24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
[6e1fed7]28#include "base64.h"
[a7b5925]29#include "arc.h"
[a338faa]30#include "xmltree.h"
[a312b6b]31
[88d2208]32#include <glib/gstdio.h>
[47b571d]33
[5ebff60]34typedef enum {
[8e6ecfe]35        XML_PASS_CHECK = 0,
36        XML_LOAD
37} xml_action;
[c121f89]38
[c9f0c79]39/* To make it easier later when extending the format: */
[a338faa]40#define XML_FORMAT_VERSION "1"
[c121f89]41
[5ebff60]42struct xml_parsedata {
[a312b6b]43        irc_t *irc;
[5ebff60]44        char given_nick[MAX_NICK_LENGTH + 1];
[c121f89]45        char *given_pass;
[a312b6b]46};
47
[5ebff60]48static void xml_init(void)
[c121f89]49{
[5ebff60]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        }
[c121f89]59}
60
[5a8afc3]61static void handle_settings(struct xt_node *node, set_t **head)
[a312b6b]62{
[00f1e93]63        struct xt_node *c;
[e41ba05]64        struct set *s;
[5ebff60]65
66        for (c = node->children; (c = xt_find_node(c, "setting")); c = c->next) {
67                char *name = xt_find_attr(c, "name");
[e41ba05]68                char *locked = xt_find_attr(c, "locked");
[5ebff60]69
70                if (!name) {
[15581c1]71                        continue;
[5ebff60]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)) {
[00f1e93]77                                continue; /* U can't touch this! */
[5ebff60]78                        }
[6e1fed7]79                }
[5ebff60]80                set_setstr(head, name, c->text);
[e41ba05]81                if (locked && !g_strcasecmp(locked, "true")) {
82                        s = set_find(head, name);
83                        if (s) {
84                                s->flags |= SET_LOCKED;
85                        }
86                }
[a312b6b]87        }
88}
89
[5a8afc3]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
[5ebff60]109static xt_status handle_account(struct xt_node *node, gpointer data)
[a312b6b]110{
[5898ef8]111        struct xml_parsedata *xd = data;
[3ac6d9f]112        char *protocol, *handle, *server, *password = NULL, *autoconnect, *tag, *locked;
[00f1e93]113        char *pass_b64 = NULL;
114        unsigned char *pass_cr = NULL;
[1783ab6]115        int pass_len, local = 0;
[00f1e93]116        struct prpl *prpl = NULL;
117        account_t *acc;
118        struct xt_node *c;
[5ebff60]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");
[3ac6d9f]125        locked = xt_find_attr(node, "locked");
[5ebff60]126
127        protocol = xt_find_attr(node, "protocol");
128        if (protocol) {
129                prpl = find_protocol(protocol);
[7320610]130                if (!prpl) {
[b4f496e]131                        irc_rootmsg(xd->irc, "Warning: Protocol not found: `%s'", protocol);
[5a8afc3]132                        prpl = (struct prpl*) &protocol_missing;
[7320610]133                }
[5ebff60]134                local = protocol_account_islocal(protocol);
[1783ab6]135        }
[5ebff60]136
137        if (!handle || !pass_b64 || !protocol || !prpl) {
[00f1e93]138                return XT_ABORT;
[8e6ecfe]139        }
140
[64b4263]141        pass_len = base64_decode(pass_b64, (unsigned char **) &pass_cr);
[8e6ecfe]142        if (xd->irc->auth_backend) {
143                password = g_strdup((char *)pass_cr);
[5ebff60]144        } else {
[8e6ecfe]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;
[5ebff60]168        }
[5a8afc3]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        }
[5ebff60]174
175        g_free(pass_cr);
176        g_free(password);
177
[5a8afc3]178        if (prpl == &protocol_missing) {
179                handle_settings_raw(node, &acc->set);
180        } else {
181                handle_settings(node, &acc->set);
182        }
[5ebff60]183
184        for (c = node->children; (c = xt_find_node(c, "buddy")); c = c->next) {
[c121f89]185                char *handle, *nick;
[5ebff60]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 {
[00f1e93]193                        return XT_ABORT;
[5ebff60]194                }
195        }
[00f1e93]196        return XT_HANDLED;
[a312b6b]197}
198
[5ebff60]199static xt_status handle_channel(struct xt_node *node, gpointer data)
[a312b6b]200{
[5898ef8]201        struct xml_parsedata *xd = data;
[00f1e93]202        irc_channel_t *ic;
203        char *name, *type;
[5ebff60]204
205        name = xt_find_attr(node, "name");
206        type = xt_find_attr(node, "type");
207
208        if (!name || !type) {
[00f1e93]209                return XT_ABORT;
[5ebff60]210        }
211
[00f1e93]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. */
[5ebff60]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
[5a8afc3]221        handle_settings(node, &ic->set);
[5ebff60]222
[00f1e93]223        return XT_HANDLED;
[a312b6b]224}
225
[00f1e93]226static const struct xt_handler_entry handlers[] = {
227        { "account", "user", handle_account, },
228        { "channel", "user", handle_channel, },
229        { NULL,      NULL,   NULL, },
[a312b6b]230};
231
[8e6ecfe]232static storage_status_t xml_load_real(irc_t *irc, const char *my_nick, const char *password, xml_action action)
[a312b6b]233{
[00f1e93]234        struct xml_parsedata xd[1];
[15581c1]235        char *fn, buf[2048];
[c121f89]236        int fd, st;
[aed00f8]237        struct xt_parser *xp = NULL;
[00f1e93]238        struct xt_node *node;
239        storage_status_t ret = STORAGE_OTHER_ERROR;
[5ebff60]240
[c121f89]241        xd->irc = irc;
[5ebff60]242        strncpy(xd->given_nick, my_nick, MAX_NICK_LENGTH);
[00f1e93]243        xd->given_nick[MAX_NICK_LENGTH] = '\0';
[5ebff60]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) {
[7320610]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                }
[00f1e93]254                goto error;
[a312b6b]255        }
[5ebff60]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) {
[00f1e93]261                        break;
[5ebff60]262                }
[00f1e93]263        }
[5ebff60]264        close(fd);
265        if (st != 0) {
[00f1e93]266                goto error;
[5ebff60]267        }
268
[00f1e93]269        node = xp->root;
[5ebff60]270        if (node == NULL || node->next != NULL || strcmp(node->name, "user") != 0) {
[00f1e93]271                goto error;
[5ebff60]272        }
273
[8e6ecfe]274        if (action == XML_PASS_CHECK) {
[5ebff60]275                char *nick = xt_find_attr(node, "nick");
276                char *pass = xt_find_attr(node, "password");
[8e6ecfe]277                char *backend = xt_find_attr(node, "auth_backend");
[5ebff60]278
[8e6ecfe]279                if (!nick || !(pass || backend)) {
[00f1e93]280                        goto error;
[8e6ecfe]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;
[5ebff60]287                } else if ((st = md5_verify_password(xd->given_pass, pass)) != 0) {
[00f1e93]288                        ret = STORAGE_INVALID_PASSWORD;
[8e6ecfe]289                } else {
290                        ret = STORAGE_OK;
[c121f89]291                }
[00f1e93]292                goto error;
293        }
[5ebff60]294
[17a58df]295        handle_settings(node, &xd->irc->b->set);
296
[5ebff60]297        if (xt_handle(xp, NULL, 1) == XT_HANDLED) {
[00f1e93]298                ret = STORAGE_OK;
[5ebff60]299        }
300
[00f1e93]301error:
[5ebff60]302        xt_free(xp);
303        g_free(fn);
[00f1e93]304        return ret;
[a312b6b]305}
306
[5ebff60]307static storage_status_t xml_load(irc_t *irc, const char *password)
[84e9cea]308{
[8e6ecfe]309        return xml_load_real(irc, irc->user->nick, password, XML_LOAD);
[84e9cea]310}
311
[8e6ecfe]312static storage_status_t xml_check_pass(irc_t *irc, const char *my_nick, const char *password)
[84e9cea]313{
[8e6ecfe]314        return xml_load_real(irc, my_nick, password, XML_PASS_CHECK);
[84e9cea]315}
316
[5898ef8]317
[5ebff60]318static void xml_generate_settings(struct xt_node *cur, set_t **head);
[5b52a48]319
[5ebff60]320struct xt_node *xml_generate(irc_t *irc)
[a312b6b]321{
[a338faa]322        char *pass_buf = NULL;
[5898ef8]323        account_t *acc;
[1bdc669]324        guint8 pass_md5[21];
325        GChecksum *md5_state;
[547ea68]326        GSList *l;
[a338faa]327        struct xt_node *root, *cur;
[1bdc669]328        gsize digest_len = MD5_HASH_SIZE;
[5ebff60]329
330        root = cur = xt_new_node("user", NULL, NULL);
[8e6ecfe]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);
[1bdc669]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);
[8e6ecfe]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
[5ebff60]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) {
[d5c55ac]355                GHashTableIter iter;
356                gpointer key, value;
[a7b5925]357                unsigned char *pass_cr;
[3b6eadc]358                char *pass_b64;
[6e1fed7]359                int pass_len;
[5ebff60]360
[8e6ecfe]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                }
[5ebff60]371
372                cur = xt_new_node("account", NULL, NULL);
[5a8afc3]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                }
[5ebff60]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                }
[3ac6d9f]385                if (acc->flags & ACC_FLAG_LOCKED) {
386                        xt_add_attr(cur, "locked", "true");
387                }
[5ebff60]388
389                g_free(pass_b64);
390
[d5c55ac]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                }
[5ebff60]398
399                xml_generate_settings(cur, &acc->set);
400
401                xt_add_child(root, cur);
[5898ef8]402        }
[5ebff60]403
404        for (l = irc->channels; l; l = l->next) {
[547ea68]405                irc_channel_t *ic = l->data;
[5ebff60]406
407                if (ic->flags & IRC_CHANNEL_TEMP) {
[1c40aa7]408                        continue;
[5ebff60]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);
[547ea68]418        }
[5ebff60]419
[a338faa]420        return root;
421}
422
[5ebff60]423static void xml_generate_settings(struct xt_node *cur, set_t **head)
[e222c36]424{
425        set_t *set;
[5ebff60]426
427        for (set = *head; set; set = set->next) {
428                if (set->value && !(set->flags & SET_NOSAVE)) {
[e222c36]429                        struct xt_node *xset;
[5ebff60]430                        xt_add_child(cur, xset = xt_new_node("setting", set->value, NULL));
431                        xt_add_attr(xset, "name", set->key);
[e41ba05]432                        if (set->flags & SET_LOCKED) {
433                                xt_add_attr(xset, "locked", "true");
434                        }
[e222c36]435                }
[5ebff60]436        }
[e222c36]437}
438
[5ebff60]439static storage_status_t xml_save(irc_t *irc, int overwrite)
[a338faa]440{
[d219296]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;
[a338faa]445        int fd;
[5ebff60]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) {
[a338faa]453                return STORAGE_ALREADY_EXISTS;
[5ebff60]454        }
455
456        strcat(path, ".XXXXXX");
457        if ((fd = mkstemp(path)) < 0) {
[7320610]458                goto error;
[547ea68]459        }
[5ebff60]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) {
[d219296]467                goto error;
[5ebff60]468        }
469
470        path2 = g_strndup(path, strlen(path) - 7);
471        if (rename(path, path2) != 0) {
472                g_free(path2);
[d219296]473                goto error;
[5898ef8]474        }
[5ebff60]475        g_free(path2);
476
[d219296]477        goto finish;
[5898ef8]478
[d219296]479error:
[7320610]480        irc_rootmsg(irc, "Write error: %s", g_strerror(errno));
[d219296]481        ret = STORAGE_OTHER_ERROR;
482
[5ebff60]483finish:
484        close(fd);
485        unlink(path);
486        g_free(xml);
487        xt_free_node(tree);
488
[d219296]489        return ret;
[a312b6b]490}
491
[5b52a48]492
[8e6ecfe]493static storage_status_t xml_remove(const char *nick)
[84e9cea]494{
[e0f9170]495        char s[512], *lc;
[5ebff60]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);
[84e9cea]501
[5ebff60]502        if (unlink(s) == -1) {
[84e9cea]503                return STORAGE_OTHER_ERROR;
[5ebff60]504        }
505
[84e9cea]506        return STORAGE_OK;
507}
508
[a312b6b]509storage_t storage_xml = {
510        .name = "xml",
511        .init = xml_init,
[84e9cea]512        .check_pass = xml_check_pass,
513        .remove = xml_remove,
[a312b6b]514        .load = xml_load,
515        .save = xml_save
516};
Note: See TracBrowser for help on using the repository browser.