source: storage_xml.c @ 00f1e93

Last change on this file since 00f1e93 was 00f1e93, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-06-05T19:09:14Z

xml_load using xmltree. About half as much code as the old stuff. Likely a
little slower, but IMHO also a little less annoying to work with.

  • Property mode set to 100644
File size: 11.7 KB
RevLine 
[a312b6b]1  /********************************************************************\
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;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#define BITLBEE_CORE
27#include "bitlbee.h"
[6e1fed7]28#include "base64.h"
[a7b5925]29#include "arc.h"
[d28f3b35]30#include "md5.h"
[a338faa]31#include "xmltree.h"
[a312b6b]32
[88d2208]33#include <glib/gstdio.h>
[47b571d]34
[c121f89]35typedef enum
36{
37        XML_PASS_CHECK_ONLY = -1,
38        XML_PASS_UNKNOWN = 0,
[c9f0c79]39        XML_PASS_WRONG,
[c121f89]40        XML_PASS_OK
41} xml_pass_st;
42
[c9f0c79]43/* To make it easier later when extending the format: */
[a338faa]44#define XML_FORMAT_VERSION "1"
[c121f89]45
[a312b6b]46struct xml_parsedata
47{
48        irc_t *irc;
[00f1e93]49        char given_nick[MAX_NICK_LENGTH+1];
[c121f89]50        char *given_pass;
[a312b6b]51};
52
[00f1e93]53static void xml_init( void )
[c121f89]54{
[00f1e93]55        if( g_access( global.conf->configdir, F_OK ) != 0 )
56                log_message( LOGLVL_WARNING, "The configuration directory `%s' does not exist. Configuration won't be saved.", 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'.", global.conf->configdir );
[c121f89]60}
61
[00f1e93]62static void handle_settings( struct xt_node *node, set_t **head )
[a312b6b]63{
[00f1e93]64        struct xt_node *c;
[a312b6b]65       
[00f1e93]66        for( c = node->children; ( c = xt_find_node( c, "setting" ) ); c = c->next )
[a312b6b]67        {
[00f1e93]68                char *name = xt_find_attr( c, "name" );
[a312b6b]69               
[00f1e93]70                if( strcmp( node->name, "account" ) == 0 )
[a312b6b]71                {
[00f1e93]72                        set_t *s = set_find( head, name );
73                        if( s && ( s->flags & ACC_SET_ONLINE_ONLY ) )
74                                continue; /* U can't touch this! */
[1917a1ec]75                }
[00f1e93]76                set_setstr( head, name, c->text );
[a312b6b]77        }
78}
79
[00f1e93]80static xt_status handle_account( struct xt_node *node, gpointer data )
[a312b6b]81{
[5898ef8]82        struct xml_parsedata *xd = data;
[00f1e93]83        char *protocol, *handle, *server, *password = NULL, *autoconnect, *tag;
84        char *pass_b64 = NULL;
85        unsigned char *pass_cr = NULL;
86        int pass_len;
87        struct prpl *prpl = NULL;
88        account_t *acc;
89        struct xt_node *c;
[5898ef8]90       
[00f1e93]91        handle = xt_find_attr( node, "handle" );
92        pass_b64 = xt_find_attr( node, "password" );
93        server = xt_find_attr( node, "server" );
94        autoconnect = xt_find_attr( node, "autoconnect" );
95        tag = xt_find_attr( node, "tag" );
96       
97        protocol = xt_find_attr( node, "protocol" );
98        if( protocol )
99                prpl = find_protocol( protocol );
100       
101        if( !handle || !pass_b64 || !protocol || !prpl )
102                return XT_ABORT;
103        else if( ( pass_len = base64_decode( pass_b64, (unsigned char**) &pass_cr ) ) &&
104                 arc_decode( pass_cr, pass_len, &password, xd->given_pass ) >= 0 )
105        {
106                acc = account_add( xd->irc->b, prpl, handle, password );
107                if( server )
108                        set_setstr( &acc->set, "server", server );
109                if( autoconnect )
110                        set_setstr( &acc->set, "auto_connect", autoconnect );
111                if( tag )
112                        set_setstr( &acc->set, "tag", tag );
[5898ef8]113        }
[00f1e93]114        else
115                return XT_ABORT;
116       
117        g_free( pass_cr );
118        g_free( password );
119       
120        handle_settings( node, &acc->set );
121       
122        for( c = node->children; ( c = xt_find_node( c, "buddy" ) ); c = c->next )
[1917a1ec]123        {
[00f1e93]124                char *handle, *nick;
125               
126                handle = xt_find_attr( c, "handle" );
127                nick = xt_find_attr( c, "nick" );
128               
129                if( handle && nick )
130                        nick_set_raw( acc, handle, nick );
131                else
132                        return XT_ABORT;
133        }       
134        return XT_HANDLED;
[a312b6b]135}
136
[00f1e93]137static xt_status handle_channel( struct xt_node *node, gpointer data )
[a312b6b]138{
139        struct xml_parsedata *xd = data;
[00f1e93]140        irc_channel_t *ic;
141        char *name, *type;
[a312b6b]142       
[00f1e93]143        name = xt_find_attr( node, "name" );
144        type = xt_find_attr( node, "type" );
[8320a7a]145       
[00f1e93]146        if( !name || !type )
147                return XT_ABORT;
148       
149        /* The channel may exist already, for example if it's &bitlbee.
150           Also, it's possible that the user just reconnected and the
151           IRC client already rejoined all channels it was in. They
152           should still get the right settings. */
153        if( ( ic = irc_channel_by_name( xd->irc, name ) ) ||
154            ( ic = irc_channel_new( xd->irc, name ) ) )
155                set_setstr( &ic->set, "type", type );
156       
157        handle_settings( node, &ic->set );
158       
159        return XT_HANDLED;
[a312b6b]160}
161
[00f1e93]162static const struct xt_handler_entry handlers[] = {
163        { "account", "user", handle_account, },
164        { "channel", "user", handle_channel, },
165        { NULL,      NULL,   NULL, },
[a312b6b]166};
167
[3183c21]168static storage_status_t xml_load_real( irc_t *irc, const char *my_nick, const char *password, xml_pass_st action )
[a312b6b]169{
[00f1e93]170        struct xml_parsedata xd[1];
171        char *fn, buf[204];
[c121f89]172        int fd, st;
[00f1e93]173        struct xt_parser *xp;
174        struct xt_node *node;
175        storage_status_t ret = STORAGE_OTHER_ERROR;
176        char *nick;
[a312b6b]177       
[c121f89]178        xd->irc = irc;
[00f1e93]179        strncpy( xd->given_nick, my_nick, MAX_NICK_LENGTH );
180        xd->given_nick[MAX_NICK_LENGTH] = '\0';
[c121f89]181        nick_lc( xd->given_nick );
[00f1e93]182        xd->given_pass = password;
[a312b6b]183       
[00f1e93]184        fn = g_strconcat( global.conf->configdir, xd->given_nick, ".xml", NULL );
[c121f89]185        if( ( fd = open( fn, O_RDONLY ) ) < 0 )
[a312b6b]186        {
[00f1e93]187                ret = STORAGE_NO_SUCH_USER;
188                goto error;
[a312b6b]189        }
190       
[00f1e93]191        xp = xt_new( handlers, xd );
[c121f89]192        while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 )
[a312b6b]193        {
[00f1e93]194                st = xt_feed( xp, buf, st );
195                if( st != 1 )
196                        break;
197        }
198        close( fd );
199        if( st != 0 )
200                goto error;
201       
202        node = xp->root;
203        if( node == NULL || node->next != NULL || strcmp( node->name, "user" ) != 0 )
204                goto error;
205       
206        {
207                char *nick = xt_find_attr( node, "nick" );
208                char *pass = xt_find_attr( node, "password" );
209               
210                if( !nick || !pass )
211                {
212                        goto error;
213                }
214                else if( ( st = md5_verify_password( xd->given_pass, pass ) ) != 0 )
[c121f89]215                {
[00f1e93]216                        ret = STORAGE_INVALID_PASSWORD;
217                        goto error;
[c121f89]218                }
[a312b6b]219        }
[c121f89]220       
[84e9cea]221        if( action == XML_PASS_CHECK_ONLY )
[00f1e93]222        {
223                ret = STORAGE_OK;
224                goto error;
225        }
226       
227        /* DO NOT call xt_handle() before verifying the password! */
228        if( xt_handle( xp, NULL, -1 ) == XT_HANDLED )
229                ret = STORAGE_OK;
[84e9cea]230       
[00f1e93]231        handle_settings( node, &xd->irc->b->set );
232       
233error:
234        return ret;
[a312b6b]235}
236
[3183c21]237static storage_status_t xml_load( irc_t *irc, const char *password )
[84e9cea]238{
[1f92a58]239        return xml_load_real( irc, irc->user->nick, password, XML_PASS_UNKNOWN );
[84e9cea]240}
241
242static storage_status_t xml_check_pass( const char *my_nick, const char *password )
243{
[3183c21]244        return xml_load_real( NULL, my_nick, password, XML_PASS_CHECK_ONLY );
[84e9cea]245}
246
[00f1e93]247
[5b52a48]248static gboolean xml_save_nick( gpointer key, gpointer value, gpointer data );
249
[a338faa]250struct xt_node *xml_generate( irc_t *irc )
[a312b6b]251{
[a338faa]252        char *pass_buf = NULL;
[5898ef8]253        set_t *set;
254        account_t *acc;
[6e1fed7]255        md5_byte_t pass_md5[21];
[ece2cd2]256        md5_state_t md5_state;
[547ea68]257        GSList *l;
[a338faa]258        struct xt_node *root, *cur;
[5898ef8]259       
[6e1fed7]260        /* Generate a salted md5sum of the password. Use 5 bytes for the salt
261           (to prevent dictionary lookups of passwords) to end up with a 21-
262           byte password hash, more convenient for base64 encoding. */
[1719464]263        random_bytes( pass_md5 + 16, 5 );
[ece2cd2]264        md5_init( &md5_state );
265        md5_append( &md5_state, (md5_byte_t*) irc->password, strlen( irc->password ) );
[6e1fed7]266        md5_append( &md5_state, pass_md5 + 16, 5 ); /* Add the salt. */
[ece2cd2]267        md5_finish( &md5_state, pass_md5 );
[6e1fed7]268        /* Save the hash in base64-encoded form. */
[3b6eadc]269        pass_buf = base64_encode( pass_md5, 21 );
[ece2cd2]270       
[a338faa]271        root = cur = xt_new_node( "user", NULL, NULL );
272        xt_add_attr( cur, "nick", irc->user->nick );
273        xt_add_attr( cur, "password", pass_buf );
274        xt_add_attr( cur, "version", XML_FORMAT_VERSION );
[5898ef8]275       
[6e1fed7]276        g_free( pass_buf );
277       
[1f92a58]278        for( set = irc->b->set; set; set = set->next )
[5df65bd]279                if( set->value && !( set->flags & SET_NOSAVE ) )
[a338faa]280                {
281                        struct xt_node *xset;
282                        xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) );
283                        xt_add_attr( xset, "name", set->key );
284                }
[5898ef8]285       
[1f92a58]286        for( acc = irc->b->accounts; acc; acc = acc->next )
[5898ef8]287        {
[a7b5925]288                unsigned char *pass_cr;
[3b6eadc]289                char *pass_b64;
[6e1fed7]290                int pass_len;
291               
[ddcf491f]292                pass_len = arc_encode( acc->pass, strlen( acc->pass ), (unsigned char**) &pass_cr, irc->password, 12 );
[a7b5925]293                pass_b64 = base64_encode( pass_cr, pass_len );
294                g_free( pass_cr );
[6e1fed7]295               
[a338faa]296                cur = xt_new_node( "account", NULL, NULL );
297                xt_add_attr( cur, "protocol", acc->prpl->name );
298                xt_add_attr( cur, "handle", acc->user );
299                xt_add_attr( cur, "password", pass_b64 );
300                xt_add_attr( cur, "autoconnect", acc->auto_connect ? "true" : "false" );
301                xt_add_attr( cur, "tag", acc->tag );
302                if( acc->server && acc->server[0] )
303                        xt_add_attr( cur, "server", acc->server );
[6e1fed7]304               
[a338faa]305                g_free( pass_b64 );
[5898ef8]306               
[5100caa]307                for( set = acc->set; set; set = set->next )
[b84800d]308                        if( set->value && !( set->flags & ACC_SET_NOSAVE ) )
[a338faa]309                        {
310                                struct xt_node *xset;
311                                xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) );
312                                xt_add_attr( xset, "name", set->key );
313                        }
[5100caa]314               
[5b52a48]315                /* This probably looks pretty strange. g_hash_table_foreach
316                   is quite a PITA already (but it can't get much better in
317                   C without using #define, I'm afraid), and since it
318                   doesn't seem to be possible to abort the foreach on write
319                   errors, so instead let's use the _find function and
320                   return TRUE on write errors. Which means, if we found
321                   something, there was an error. :-) */
[a338faa]322                g_hash_table_find( acc->nicks, xml_save_nick, cur );
[5898ef8]323               
[a338faa]324                xt_add_child( root, cur );
[5898ef8]325        }
326       
[547ea68]327        for( l = irc->channels; l; l = l->next )
328        {
329                irc_channel_t *ic = l->data;
330               
[1c40aa7]331                if( ic->flags & IRC_CHANNEL_TEMP )
332                        continue;
333               
[a338faa]334                cur = xt_new_node( "channel", NULL, NULL );
335                xt_add_attr( cur, "name", ic->name );
336                xt_add_attr( cur, "type", set_getstr( &ic->set, "type" ) );
[547ea68]337               
338                for( set = ic->set; set; set = set->next )
339                        if( set->value && strcmp( set->key, "type" ) != 0 )
[a338faa]340                        {
341                                struct xt_node *xset;
342                                xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) );
343                                xt_add_attr( xset, "name", set->key );
344                        }
[547ea68]345               
[a338faa]346                xt_add_child( root, cur );
347        }
348       
349        return root;
350}
351
352static storage_status_t xml_save( irc_t *irc, int overwrite )
353{
354        char path[512], *path2, *xml;
355        struct xt_node *tree;
356        int fd;
357       
358        path2 = g_strdup( irc->user->nick );
359        nick_lc( path2 );
360        g_snprintf( path, sizeof( path ) - 2, "%s%s%s", global.conf->configdir, path2, ".xml" );
361        g_free( path2 );
362       
363        if( !overwrite && g_access( path, F_OK ) == 0 )
364                return STORAGE_ALREADY_EXISTS;
365       
366        strcat( path, ".XXXXXX" );
367        if( ( fd = mkstemp( path ) ) < 0 )
368        {
369                irc_rootmsg( irc, "Error while opening configuration file." );
370                return STORAGE_OTHER_ERROR;
[547ea68]371        }
372       
[a338faa]373        tree = xml_generate( irc );
374        xml = xt_to_string( tree );
375        write( fd, xml, strlen( xml ) );
[5898ef8]376       
[542e44a]377        fsync( fd );
[5898ef8]378        close( fd );
379       
[f277225]380        path2 = g_strndup( path, strlen( path ) - 7 );
[5898ef8]381        if( rename( path, path2 ) != 0 )
382        {
[e67e513]383                irc_rootmsg( irc, "Error while renaming temporary configuration file." );
[5898ef8]384               
385                g_free( path2 );
386                unlink( path );
387               
388                return STORAGE_OTHER_ERROR;
389        }
390       
391        g_free( path2 );
392       
[a312b6b]393        return STORAGE_OK;
[5898ef8]394
395write_error:
[6e1fed7]396       
[e67e513]397        irc_rootmsg( irc, "Write error. Disk full?" );
[5898ef8]398        close( fd );
399       
400        return STORAGE_OTHER_ERROR;
[a312b6b]401}
402
[5b52a48]403static gboolean xml_save_nick( gpointer key, gpointer value, gpointer data )
404{
[a338faa]405        struct xt_node *node = xt_new_node( "buddy", NULL, NULL );
406        xt_add_attr( node, "handle", key );
407        xt_add_attr( node, "nick", value );
408        xt_add_child( (struct xt_node *) data, node );
409       
410        return FALSE;
[5b52a48]411}
412
[84e9cea]413static storage_status_t xml_remove( const char *nick, const char *password )
414{
[e0f9170]415        char s[512], *lc;
[84e9cea]416        storage_status_t status;
417
418        status = xml_check_pass( nick, password );
419        if( status != STORAGE_OK )
420                return status;
421
[e0f9170]422        lc = g_strdup( nick );
423        nick_lc( lc );
424        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, lc, ".xml" );
425        g_free( lc );
426       
[84e9cea]427        if( unlink( s ) == -1 )
428                return STORAGE_OTHER_ERROR;
429       
430        return STORAGE_OK;
431}
432
[a312b6b]433storage_t storage_xml = {
434        .name = "xml",
435        .init = xml_init,
[84e9cea]436        .check_pass = xml_check_pass,
437        .remove = xml_remove,
[a312b6b]438        .load = xml_load,
439        .save = xml_save
440};
Note: See TracBrowser for help on using the repository browser.