source: storage_xml.c @ d219296

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

Small cleanup of xml_save().

  • Property mode set to 100644
File size: 11.8 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       
177        xd->irc = irc;
178        strncpy( xd->given_nick, my_nick, MAX_NICK_LENGTH );
179        xd->given_nick[MAX_NICK_LENGTH] = '\0';
180        nick_lc( xd->given_nick );
181        xd->given_pass = password;
182       
183        fn = g_strconcat( global.conf->configdir, xd->given_nick, ".xml", NULL );
184        if( ( fd = open( fn, O_RDONLY ) ) < 0 )
185        {
186                ret = STORAGE_NO_SUCH_USER;
187                goto error;
188        }
189       
190        xp = xt_new( handlers, xd );
191        while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 )
192        {
193                st = xt_feed( xp, buf, st );
194                if( st != 1 )
195                        break;
196        }
197        close( fd );
198        if( st != 0 )
199                goto error;
200       
201        node = xp->root;
202        if( node == NULL || node->next != NULL || strcmp( node->name, "user" ) != 0 )
203                goto error;
204       
205        {
206                char *nick = xt_find_attr( node, "nick" );
207                char *pass = xt_find_attr( node, "password" );
208               
209                if( !nick || !pass )
210                {
211                        goto error;
212                }
213                else if( ( st = md5_verify_password( xd->given_pass, pass ) ) != 0 )
214                {
215                        ret = STORAGE_INVALID_PASSWORD;
216                        goto error;
217                }
218        }
219       
220        if( action == XML_PASS_CHECK_ONLY )
221        {
222                ret = STORAGE_OK;
223                goto error;
224        }
225       
226        /* DO NOT call xt_handle() before verifying the password! */
227        if( xt_handle( xp, NULL, -1 ) == XT_HANDLED )
228                ret = STORAGE_OK;
229       
230        handle_settings( node, &xd->irc->b->set );
231       
232error:
233        return ret;
234}
235
236static storage_status_t xml_load( irc_t *irc, const char *password )
237{
238        return xml_load_real( irc, irc->user->nick, password, XML_PASS_UNKNOWN );
239}
240
241static storage_status_t xml_check_pass( const char *my_nick, const char *password )
242{
243        return xml_load_real( NULL, my_nick, password, XML_PASS_CHECK_ONLY );
244}
245
246
247static gboolean xml_generate_nick( gpointer key, gpointer value, gpointer data );
248
249struct xt_node *xml_generate( irc_t *irc )
250{
251        char *pass_buf = NULL;
252        set_t *set;
253        account_t *acc;
254        md5_byte_t pass_md5[21];
255        md5_state_t md5_state;
256        GSList *l;
257        struct xt_node *root, *cur;
258       
259        /* Generate a salted md5sum of the password. Use 5 bytes for the salt
260           (to prevent dictionary lookups of passwords) to end up with a 21-
261           byte password hash, more convenient for base64 encoding. */
262        random_bytes( pass_md5 + 16, 5 );
263        md5_init( &md5_state );
264        md5_append( &md5_state, (md5_byte_t*) irc->password, strlen( irc->password ) );
265        md5_append( &md5_state, pass_md5 + 16, 5 ); /* Add the salt. */
266        md5_finish( &md5_state, pass_md5 );
267        /* Save the hash in base64-encoded form. */
268        pass_buf = base64_encode( pass_md5, 21 );
269       
270        root = cur = xt_new_node( "user", NULL, NULL );
271        xt_add_attr( cur, "nick", irc->user->nick );
272        xt_add_attr( cur, "password", pass_buf );
273        xt_add_attr( cur, "version", XML_FORMAT_VERSION );
274       
275        g_free( pass_buf );
276       
277        for( set = irc->b->set; set; set = set->next )
278                if( set->value && !( set->flags & SET_NOSAVE ) )
279                {
280                        struct xt_node *xset;
281                        xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) );
282                        xt_add_attr( xset, "name", set->key );
283                }
284       
285        for( acc = irc->b->accounts; acc; acc = acc->next )
286        {
287                unsigned char *pass_cr;
288                char *pass_b64;
289                int pass_len;
290               
291                pass_len = arc_encode( acc->pass, strlen( acc->pass ), (unsigned char**) &pass_cr, irc->password, 12 );
292                pass_b64 = base64_encode( pass_cr, pass_len );
293                g_free( pass_cr );
294               
295                cur = xt_new_node( "account", NULL, NULL );
296                xt_add_attr( cur, "protocol", acc->prpl->name );
297                xt_add_attr( cur, "handle", acc->user );
298                xt_add_attr( cur, "password", pass_b64 );
299                xt_add_attr( cur, "autoconnect", acc->auto_connect ? "true" : "false" );
300                xt_add_attr( cur, "tag", acc->tag );
301                if( acc->server && acc->server[0] )
302                        xt_add_attr( cur, "server", acc->server );
303               
304                g_free( pass_b64 );
305               
306                for( set = acc->set; set; set = set->next )
307                        if( set->value && !( set->flags & ACC_SET_NOSAVE ) )
308                        {
309                                struct xt_node *xset;
310                                xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) );
311                                xt_add_attr( xset, "name", set->key );
312                        }
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 since 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                xt_add_child( root, cur );
324        }
325       
326        for( l = irc->channels; l; l = l->next )
327        {
328                irc_channel_t *ic = l->data;
329               
330                if( ic->flags & IRC_CHANNEL_TEMP )
331                        continue;
332               
333                cur = xt_new_node( "channel", NULL, NULL );
334                xt_add_attr( cur, "name", ic->name );
335                xt_add_attr( cur, "type", set_getstr( &ic->set, "type" ) );
336               
337                for( set = ic->set; set; set = set->next )
338                        if( set->value && strcmp( set->key, "type" ) != 0 )
339                        {
340                                struct xt_node *xset;
341                                xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) );
342                                xt_add_attr( xset, "name", set->key );
343                        }
344               
345                xt_add_child( root, cur );
346        }
347       
348        return root;
349}
350
351static storage_status_t xml_save( irc_t *irc, int overwrite )
352{
353        storage_status_t ret = STORAGE_OK;
354        char path[512], *path2 = NULL, *xml = NULL;
355        struct xt_node *tree = NULL;
356        size_t len;
357        int fd;
358       
359        path2 = g_strdup( irc->user->nick );
360        nick_lc( path2 );
361        g_snprintf( path, sizeof( path ) - 20, "%s%s%s", global.conf->configdir, path2, ".xml" );
362        g_free( path2 );
363       
364        if( !overwrite && g_access( path, F_OK ) == 0 )
365                return STORAGE_ALREADY_EXISTS;
366       
367        strcat( path, ".XXXXXX" );
368        if( ( fd = mkstemp( path ) ) < 0 )
369        {
370                irc_rootmsg( irc, "Error while opening configuration file." );
371                return STORAGE_OTHER_ERROR;
372        }
373       
374        tree = xml_generate( irc );
375        xml = xt_to_string( tree );
376        len = strlen( xml );
377        if( write( fd, xml, len ) != len ||
378            fsync( fd ) != 0 || /* #559 */
379            close( fd ) != 0 )
380                goto error;
381       
382        path2 = g_strndup( path, strlen( path ) - 7 );
383        if( rename( path, path2 ) != 0 )
384        {
385                g_free( path2 );
386                goto error;
387        }
388        g_free( path2 );
389       
390        goto finish;
391
392error:
393        irc_rootmsg( irc, "Write error. Disk full?" );
394        ret = STORAGE_OTHER_ERROR;
395
396finish: 
397        close( fd );
398        unlink( path );
399        g_free( xml );
400        xt_free_node( tree );
401       
402        return ret;
403}
404
405static gboolean xml_generate_nick( gpointer key, gpointer value, gpointer data )
406{
407        struct xt_node *node = xt_new_node( "buddy", NULL, NULL );
408        xt_add_attr( node, "handle", key );
409        xt_add_attr( node, "nick", value );
410        xt_add_child( (struct xt_node *) data, node );
411       
412        return FALSE;
413}
414
415
416static storage_status_t xml_remove( const char *nick, const char *password )
417{
418        char s[512], *lc;
419        storage_status_t status;
420
421        status = xml_check_pass( nick, password );
422        if( status != STORAGE_OK )
423                return status;
424
425        lc = g_strdup( nick );
426        nick_lc( lc );
427        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, lc, ".xml" );
428        g_free( lc );
429       
430        if( unlink( s ) == -1 )
431                return STORAGE_OTHER_ERROR;
432       
433        return STORAGE_OK;
434}
435
436storage_t storage_xml = {
437        .name = "xml",
438        .init = xml_init,
439        .check_pass = xml_check_pass,
440        .remove = xml_remove,
441        .load = xml_load,
442        .save = xml_save
443};
Note: See TracBrowser for help on using the repository browser.