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
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., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  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{
37        XML_PASS_CHECK_ONLY = -1,
38        XML_PASS_UNKNOWN = 0,
39        XML_PASS_WRONG,
40        XML_PASS_OK
41} xml_pass_st;
42
43/* To make it easier later when extending the format: */
44#define XML_FORMAT_VERSION "1"
45
46struct xml_parsedata
47{
48        irc_t *irc;
49        char given_nick[MAX_NICK_LENGTH+1];
50        char *given_pass;
51};
52
53static void xml_init( void )
54{
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 );
60}
61
62static void handle_settings( struct xt_node *node, set_t **head )
63{
64        struct xt_node *c;
65       
66        for( c = node->children; ( c = xt_find_node( c, "setting" ) ); c = c->next )
67        {
68                char *name = xt_find_attr( c, "name" );
69               
70                if( strcmp( node->name, "account" ) == 0 )
71                {
72                        set_t *s = set_find( head, name );
73                        if( s && ( s->flags & ACC_SET_ONLINE_ONLY ) )
74                                continue; /* U can't touch this! */
75                }
76                set_setstr( head, name, c->text );
77        }
78}
79
80static xt_status handle_account( struct xt_node *node, gpointer data )
81{
82        struct xml_parsedata *xd = data;
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;
90       
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 );
113        }
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 )
123        {
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;
135}
136
137static xt_status handle_channel( struct xt_node *node, gpointer data )
138{
139        struct xml_parsedata *xd = data;
140        irc_channel_t *ic;
141        char *name, *type;
142       
143        name = xt_find_attr( node, "name" );
144        type = xt_find_attr( node, "type" );
145       
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;
160}
161
162static const struct xt_handler_entry handlers[] = {
163        { "account", "user", handle_account, },
164        { "channel", "user", handle_channel, },
165        { NULL,      NULL,   NULL, },
166};
167
168static storage_status_t xml_load_real( irc_t *irc, const char *my_nick, const char *password, xml_pass_st action )
169{
170        struct xml_parsedata xd[1];
171        char *fn, buf[204];
172        int fd, st;
173        struct xt_parser *xp;
174        struct xt_node *node;
175        storage_status_t ret = STORAGE_OTHER_ERROR;
176        char *nick;
177       
178        xd->irc = irc;
179        strncpy( xd->given_nick, my_nick, MAX_NICK_LENGTH );
180        xd->given_nick[MAX_NICK_LENGTH] = '\0';
181        nick_lc( xd->given_nick );
182        xd->given_pass = password;
183       
184        fn = g_strconcat( global.conf->configdir, xd->given_nick, ".xml", NULL );
185        if( ( fd = open( fn, O_RDONLY ) ) < 0 )
186        {
187                ret = STORAGE_NO_SUCH_USER;
188                goto error;
189        }
190       
191        xp = xt_new( handlers, xd );
192        while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 )
193        {
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 )
215                {
216                        ret = STORAGE_INVALID_PASSWORD;
217                        goto error;
218                }
219        }
220       
221        if( action == XML_PASS_CHECK_ONLY )
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;
230       
231        handle_settings( node, &xd->irc->b->set );
232       
233error:
234        return ret;
235}
236
237static storage_status_t xml_load( irc_t *irc, const char *password )
238{
239        return xml_load_real( irc, irc->user->nick, password, XML_PASS_UNKNOWN );
240}
241
242static storage_status_t xml_check_pass( const char *my_nick, const char *password )
243{
244        return xml_load_real( NULL, my_nick, password, XML_PASS_CHECK_ONLY );
245}
246
247
248static gboolean xml_save_nick( gpointer key, gpointer value, gpointer data );
249
250struct xt_node *xml_generate( irc_t *irc )
251{
252        char *pass_buf = NULL;
253        set_t *set;
254        account_t *acc;
255        md5_byte_t pass_md5[21];
256        md5_state_t md5_state;
257        GSList *l;
258        struct xt_node *root, *cur;
259       
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. */
263        random_bytes( pass_md5 + 16, 5 );
264        md5_init( &md5_state );
265        md5_append( &md5_state, (md5_byte_t*) irc->password, strlen( irc->password ) );
266        md5_append( &md5_state, pass_md5 + 16, 5 ); /* Add the salt. */
267        md5_finish( &md5_state, pass_md5 );
268        /* Save the hash in base64-encoded form. */
269        pass_buf = base64_encode( pass_md5, 21 );
270       
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 );
275       
276        g_free( pass_buf );
277       
278        for( set = irc->b->set; set; set = set->next )
279                if( set->value && !( set->flags & SET_NOSAVE ) )
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                }
285       
286        for( acc = irc->b->accounts; acc; acc = acc->next )
287        {
288                unsigned char *pass_cr;
289                char *pass_b64;
290                int pass_len;
291               
292                pass_len = arc_encode( acc->pass, strlen( acc->pass ), (unsigned char**) &pass_cr, irc->password, 12 );
293                pass_b64 = base64_encode( pass_cr, pass_len );
294                g_free( pass_cr );
295               
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 );
304               
305                g_free( pass_b64 );
306               
307                for( set = acc->set; set; set = set->next )
308                        if( set->value && !( set->flags & ACC_SET_NOSAVE ) )
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                        }
314               
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. :-) */
322                g_hash_table_find( acc->nicks, xml_save_nick, cur );
323               
324                xt_add_child( root, cur );
325        }
326       
327        for( l = irc->channels; l; l = l->next )
328        {
329                irc_channel_t *ic = l->data;
330               
331                if( ic->flags & IRC_CHANNEL_TEMP )
332                        continue;
333               
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" ) );
337               
338                for( set = ic->set; set; set = set->next )
339                        if( set->value && strcmp( set->key, "type" ) != 0 )
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                        }
345               
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;
371        }
372       
373        tree = xml_generate( irc );
374        xml = xt_to_string( tree );
375        write( fd, xml, strlen( xml ) );
376       
377        fsync( fd );
378        close( fd );
379       
380        path2 = g_strndup( path, strlen( path ) - 7 );
381        if( rename( path, path2 ) != 0 )
382        {
383                irc_rootmsg( irc, "Error while renaming temporary configuration file." );
384               
385                g_free( path2 );
386                unlink( path );
387               
388                return STORAGE_OTHER_ERROR;
389        }
390       
391        g_free( path2 );
392       
393        return STORAGE_OK;
394
395write_error:
396       
397        irc_rootmsg( irc, "Write error. Disk full?" );
398        close( fd );
399       
400        return STORAGE_OTHER_ERROR;
401}
402
403static gboolean xml_save_nick( gpointer key, gpointer value, gpointer data )
404{
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;
411}
412
413static storage_status_t xml_remove( const char *nick, const char *password )
414{
415        char s[512], *lc;
416        storage_status_t status;
417
418        status = xml_check_pass( nick, password );
419        if( status != STORAGE_OK )
420                return status;
421
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       
427        if( unlink( s ) == -1 )
428                return STORAGE_OTHER_ERROR;
429       
430        return STORAGE_OK;
431}
432
433storage_t storage_xml = {
434        .name = "xml",
435        .init = xml_init,
436        .check_pass = xml_check_pass,
437        .remove = xml_remove,
438        .load = xml_load,
439        .save = xml_save
440};
Note: See TracBrowser for help on using the repository browser.