source: storage_xml.c @ 84c3a72

Last change on this file since 84c3a72 was 84c3a72, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-27T12:39:07Z

Import chatrooms configured in older BitlBee versions. Settings are currently
ignored though. Also removing the old chat.[ch] files since they're really not
important anymore.

  • Property mode set to 100644
File size: 15.4 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2006 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
32#if GLIB_CHECK_VERSION(2,8,0)
33#include <glib/gstdio.h>
34#else
35/* GLib < 2.8.0 doesn't have g_access, so just use the system access(). */
36#include <unistd.h>
37#define g_access access
38#endif
39
40typedef enum
41{
42        XML_PASS_CHECK_ONLY = -1,
43        XML_PASS_UNKNOWN = 0,
44        XML_PASS_WRONG,
45        XML_PASS_OK
46} xml_pass_st;
47
48/* To make it easier later when extending the format: */
49#define XML_FORMAT_VERSION 1
50
51struct xml_parsedata
52{
53        irc_t *irc;
54        char *current_setting;
55        account_t *current_account;
56        irc_channel_t *current_channel;
57        set_t **current_set_head;
58        char *given_nick;
59        char *given_pass;
60        xml_pass_st pass_st;
61};
62
63static char *xml_attr( const gchar **attr_names, const gchar **attr_values, const gchar *key )
64{
65        int i;
66       
67        for( i = 0; attr_names[i]; i ++ )
68                if( g_strcasecmp( attr_names[i], key ) == 0 )
69                        return (char*) attr_values[i];
70       
71        return NULL;
72}
73
74static void xml_destroy_xd( gpointer data )
75{
76        struct xml_parsedata *xd = data;
77       
78        g_free( xd->given_nick );
79        g_free( xd->given_pass );
80        g_free( xd );
81}
82
83static void xml_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error )
84{
85        struct xml_parsedata *xd = data;
86        irc_t *irc = xd->irc;
87       
88        if( g_strcasecmp( element_name, "user" ) == 0 )
89        {
90                char *nick = xml_attr( attr_names, attr_values, "nick" );
91                char *pass = xml_attr( attr_names, attr_values, "password" );
92                int st;
93               
94                if( !nick || !pass )
95                {
96                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
97                                     "Missing attributes for %s element", element_name );
98                }
99                else if( ( st = md5_verify_password( xd->given_pass, pass ) ) == -1 )
100                {
101                        xd->pass_st = XML_PASS_WRONG;
102                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
103                                     "Error while decoding password attribute" );
104                }
105                else if( st == 0 )
106                {
107                        if( xd->pass_st != XML_PASS_CHECK_ONLY )
108                                xd->pass_st = XML_PASS_OK;
109                }
110                else
111                {
112                        xd->pass_st = XML_PASS_WRONG;
113                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
114                                     "Password mismatch" );
115                }
116        }
117        else if( xd->pass_st < XML_PASS_OK )
118        {
119                /* Let's not parse anything else if we only have to check
120                   the password. */
121        }
122        else if( g_strcasecmp( element_name, "account" ) == 0 )
123        {
124                char *protocol, *handle, *server, *password = NULL, *autoconnect;
125                char *pass_b64 = NULL;
126                unsigned char *pass_cr = NULL;
127                int pass_len;
128                struct prpl *prpl = NULL;
129               
130                handle = xml_attr( attr_names, attr_values, "handle" );
131                pass_b64 = xml_attr( attr_names, attr_values, "password" );
132                server = xml_attr( attr_names, attr_values, "server" );
133                autoconnect = xml_attr( attr_names, attr_values, "autoconnect" );
134               
135                protocol = xml_attr( attr_names, attr_values, "protocol" );
136                if( protocol )
137                        prpl = find_protocol( protocol );
138               
139                if( !handle || !pass_b64 || !protocol )
140                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
141                                     "Missing attributes for %s element", element_name );
142                else if( !prpl )
143                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
144                                     "Unknown protocol: %s", protocol );
145                else if( ( pass_len = base64_decode( pass_b64, (unsigned char**) &pass_cr ) ) &&
146                                         arc_decode( pass_cr, pass_len, &password, xd->given_pass ) )
147                {
148                        xd->current_account = account_add( irc->b, prpl, handle, password );
149                        if( server )
150                                set_setstr( &xd->current_account->set, "server", server );
151                        if( autoconnect )
152                                set_setstr( &xd->current_account->set, "auto_connect", autoconnect );
153                }
154                else
155                {
156                        /* Actually the _decode functions don't even return error codes,
157                           but maybe they will later... */
158                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
159                                     "Error while decrypting account password" );
160                }
161               
162                g_free( pass_cr );
163                g_free( password );
164        }
165        else if( g_strcasecmp( element_name, "setting" ) == 0 )
166        {
167                char *setting;
168               
169                if( xd->current_setting )
170                {
171                        g_free( xd->current_setting );
172                        xd->current_setting = NULL;
173                }
174               
175                if( ( setting = xml_attr( attr_names, attr_values, "name" ) ) )
176                {
177                        /*
178                        if( xd->current_chat != NULL )
179                                xd->current_set_head = &xd->current_chat->set;
180                        else
181                        */
182                        if( xd->current_account != NULL )
183                                xd->current_set_head = &xd->current_account->set;
184                        else
185                                xd->current_set_head = &xd->irc->b->set;
186                       
187                        xd->current_setting = g_strdup( setting );
188                }
189                else
190                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
191                                     "Missing attributes for %s element", element_name );
192        }
193        else if( g_strcasecmp( element_name, "buddy" ) == 0 )
194        {
195                char *handle, *nick;
196               
197                handle = xml_attr( attr_names, attr_values, "handle" );
198                nick = xml_attr( attr_names, attr_values, "nick" );
199               
200                if( xd->current_account && handle && nick )
201                {
202                        nick_set( xd->current_account, handle, nick );
203                }
204                else
205                {
206                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
207                                     "Missing attributes for %s element", element_name );
208                }
209        }
210        else if( g_strcasecmp( element_name, "chat" ) == 0 )
211        {
212                char *handle, *channel;
213               
214                handle = xml_attr( attr_names, attr_values, "handle" );
215                channel = xml_attr( attr_names, attr_values, "channel" );
216               
217                if( xd->current_account && handle && channel )
218                {
219                        irc_channel_t *ic;
220                        char *acc;
221                       
222                        acc = g_strdup_printf( "%s(%s)",
223                                               xd->current_account->prpl->name,
224                                               xd->current_account->user );
225                       
226                        if( ( ic = irc_channel_new( irc, channel ) ) &&
227                            set_setstr( &ic->set, "chat_type", "room" ) &&
228                            set_setstr( &ic->set, "account", acc ) &&
229                            set_setstr( &ic->set, "room", handle ) )
230                        {
231                                /* Nothing else to do for now, really. */
232                        }
233                        else if( ic )
234                                irc_channel_free( ic );
235                       
236                        g_free( acc );
237                }
238                else
239                {
240                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
241                                     "Missing attributes for %s element", element_name );
242                }
243        }
244        else
245        {
246                g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
247                             "Unkown element: %s", element_name );
248        }
249}
250
251static void xml_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error )
252{
253        struct xml_parsedata *xd = data;
254       
255        if( g_strcasecmp( element_name, "setting" ) == 0 && xd->current_setting )
256        {
257                g_free( xd->current_setting );
258                xd->current_setting = NULL;
259        }
260        else if( g_strcasecmp( element_name, "account" ) == 0 )
261        {
262                xd->current_account = NULL;
263        }
264        else if( g_strcasecmp( element_name, "chat" ) == 0 )
265        {
266                /* xd->current_chat = NULL; */
267        }
268}
269
270static void xml_text( GMarkupParseContext *ctx, const gchar *text_orig, gsize text_len, gpointer data, GError **error )
271{
272        char text[text_len+1];
273        struct xml_parsedata *xd = data;
274       
275        strncpy( text, text_orig, text_len );
276        text[text_len] = 0;
277       
278        if( xd->pass_st < XML_PASS_OK )
279        {
280                /* Let's not parse anything else if we only have to check
281                   the password, or if we didn't get the chance to check it
282                   yet. */
283        }
284        else if( g_strcasecmp( g_markup_parse_context_get_element( ctx ), "setting" ) == 0 && xd->current_setting )
285        {
286                set_setstr( xd->current_set_head, xd->current_setting, (char*) text );
287                g_free( xd->current_setting );
288                xd->current_setting = NULL;
289        }
290}
291
292GMarkupParser xml_parser =
293{
294        xml_start_element,
295        xml_end_element,
296        xml_text,
297        NULL,
298        NULL
299};
300
301static void xml_init( void )
302{
303        if( g_access( global.conf->configdir, F_OK ) != 0 )
304                log_message( LOGLVL_WARNING, "The configuration directory `%s' does not exist. Configuration won't be saved.", global.conf->configdir );
305        else if( g_access( global.conf->configdir, F_OK ) != 0 || 
306                 g_access( global.conf->configdir, W_OK ) != 0 )
307                log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to `%s'.", global.conf->configdir );
308}
309
310static storage_status_t xml_load_real( irc_t *irc, const char *my_nick, const char *password, xml_pass_st action )
311{
312        GMarkupParseContext *ctx;
313        struct xml_parsedata *xd;
314        char *fn, buf[512];
315        GError *gerr = NULL;
316        int fd, st;
317       
318        xd = g_new0( struct xml_parsedata, 1 );
319        xd->irc = irc;
320        xd->given_nick = g_strdup( my_nick );
321        xd->given_pass = g_strdup( password );
322        xd->pass_st = action;
323        nick_lc( xd->given_nick );
324       
325        fn = g_strdup_printf( "%s%s%s", global.conf->configdir, xd->given_nick, ".xml" );
326        if( ( fd = open( fn, O_RDONLY ) ) < 0 )
327        {
328                xml_destroy_xd( xd );
329                g_free( fn );
330                return STORAGE_NO_SUCH_USER;
331        }
332        g_free( fn );
333       
334        ctx = g_markup_parse_context_new( &xml_parser, 0, xd, xml_destroy_xd );
335       
336        while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 )
337        {
338                if( !g_markup_parse_context_parse( ctx, buf, st, &gerr ) || gerr )
339                {
340                        xml_pass_st pass_st = xd->pass_st;
341                       
342                        g_markup_parse_context_free( ctx );
343                        close( fd );
344                       
345                        if( pass_st == XML_PASS_WRONG )
346                        {
347                                g_clear_error( &gerr );
348                                return STORAGE_INVALID_PASSWORD;
349                        }
350                        else
351                        {
352                                if( gerr && irc )
353                                        irc_usermsg( irc, "Error from XML-parser: %s", gerr->message );
354                               
355                                g_clear_error( &gerr );
356                                return STORAGE_OTHER_ERROR;
357                        }
358                }
359        }
360        /* Just to be sure... */
361        g_clear_error( &gerr );
362       
363        g_markup_parse_context_free( ctx );
364        close( fd );
365       
366        if( action == XML_PASS_CHECK_ONLY )
367                return STORAGE_OK;
368       
369        return STORAGE_OK;
370}
371
372static storage_status_t xml_load( irc_t *irc, const char *password )
373{
374        return xml_load_real( irc, irc->user->nick, password, XML_PASS_UNKNOWN );
375}
376
377static storage_status_t xml_check_pass( const char *my_nick, const char *password )
378{
379        /* This is a little bit risky because we have to pass NULL for the
380           irc_t argument. This *should* be fine, if I didn't miss anything... */
381        return xml_load_real( NULL, my_nick, password, XML_PASS_CHECK_ONLY );
382}
383
384static int xml_printf( int fd, int indent, char *fmt, ... )
385{
386        va_list params;
387        char *out;
388        char tabs[9] = "\t\t\t\t\t\t\t\t";
389        int len;
390       
391        /* Maybe not very clean, but who needs more than 8 levels of indentation anyway? */
392        if( write( fd, tabs, indent <= 8 ? indent : 8 ) != indent )
393                return 0;
394       
395        va_start( params, fmt );
396        out = g_markup_vprintf_escaped( fmt, params );
397        va_end( params );
398       
399        len = strlen( out );
400        len -= write( fd, out, len );
401        g_free( out );
402       
403        return len == 0;
404}
405
406static gboolean xml_save_nick( gpointer key, gpointer value, gpointer data );
407
408static storage_status_t xml_save( irc_t *irc, int overwrite )
409{
410        char path[512], *path2, *pass_buf = NULL;
411        set_t *set;
412        account_t *acc;
413        int fd;
414        md5_byte_t pass_md5[21];
415        md5_state_t md5_state;
416       
417        path2 = g_strdup( irc->user->nick );
418        nick_lc( path2 );
419        g_snprintf( path, sizeof( path ) - 2, "%s%s%s", global.conf->configdir, path2, ".xml" );
420        g_free( path2 );
421       
422        if( !overwrite && g_access( path, F_OK ) == 0 )
423                return STORAGE_ALREADY_EXISTS;
424       
425        strcat( path, ".XXXXXX" );
426        if( ( fd = mkstemp( path ) ) < 0 )
427        {
428                irc_usermsg( irc, "Error while opening configuration file." );
429                return STORAGE_OTHER_ERROR;
430        }
431       
432        /* Generate a salted md5sum of the password. Use 5 bytes for the salt
433           (to prevent dictionary lookups of passwords) to end up with a 21-
434           byte password hash, more convenient for base64 encoding. */
435        random_bytes( pass_md5 + 16, 5 );
436        md5_init( &md5_state );
437        md5_append( &md5_state, (md5_byte_t*) irc->password, strlen( irc->password ) );
438        md5_append( &md5_state, pass_md5 + 16, 5 ); /* Add the salt. */
439        md5_finish( &md5_state, pass_md5 );
440        /* Save the hash in base64-encoded form. */
441        pass_buf = base64_encode( pass_md5, 21 );
442       
443        if( !xml_printf( fd, 0, "<user nick=\"%s\" password=\"%s\" version=\"%d\">\n", irc->user->nick, pass_buf, XML_FORMAT_VERSION ) )
444                goto write_error;
445       
446        g_free( pass_buf );
447       
448        for( set = irc->b->set; set; set = set->next )
449                if( set->value )
450                        if( !xml_printf( fd, 1, "<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
451                                goto write_error;
452       
453        for( acc = irc->b->accounts; acc; acc = acc->next )
454        {
455                unsigned char *pass_cr;
456                char *pass_b64;
457                int pass_len;
458               
459                pass_len = arc_encode( acc->pass, strlen( acc->pass ), (unsigned char**) &pass_cr, irc->password, 12 );
460                pass_b64 = base64_encode( pass_cr, pass_len );
461                g_free( pass_cr );
462               
463                if( !xml_printf( fd, 1, "<account protocol=\"%s\" handle=\"%s\" password=\"%s\" autoconnect=\"%d\"", acc->prpl->name, acc->user, pass_b64, acc->auto_connect ) )
464                {
465                        g_free( pass_b64 );
466                        goto write_error;
467                }
468                g_free( pass_b64 );
469               
470                if( acc->server && acc->server[0] && !xml_printf( fd, 0, " server=\"%s\"", acc->server ) )
471                        goto write_error;
472                if( !xml_printf( fd, 0, ">\n" ) )
473                        goto write_error;
474               
475                for( set = acc->set; set; set = set->next )
476                        if( set->value && !( set->flags & ACC_SET_NOSAVE ) )
477                                if( !xml_printf( fd, 2, "<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
478                                        goto write_error;
479               
480                /* This probably looks pretty strange. g_hash_table_foreach
481                   is quite a PITA already (but it can't get much better in
482                   C without using #define, I'm afraid), and since it
483                   doesn't seem to be possible to abort the foreach on write
484                   errors, so instead let's use the _find function and
485                   return TRUE on write errors. Which means, if we found
486                   something, there was an error. :-) */
487                if( g_hash_table_find( acc->nicks, xml_save_nick, & fd ) )
488                        goto write_error;
489               
490                if( !xml_printf( fd, 1, "</account>\n" ) )
491                        goto write_error;
492        }
493       
494        if( !xml_printf( fd, 0, "</user>\n" ) )
495                goto write_error;
496       
497        fsync( fd );
498        close( fd );
499       
500        path2 = g_strndup( path, strlen( path ) - 7 );
501        if( rename( path, path2 ) != 0 )
502        {
503                irc_usermsg( irc, "Error while renaming temporary configuration file." );
504               
505                g_free( path2 );
506                unlink( path );
507               
508                return STORAGE_OTHER_ERROR;
509        }
510       
511        g_free( path2 );
512       
513        return STORAGE_OK;
514
515write_error:
516        g_free( pass_buf );
517       
518        irc_usermsg( irc, "Write error. Disk full?" );
519        close( fd );
520       
521        return STORAGE_OTHER_ERROR;
522}
523
524static gboolean xml_save_nick( gpointer key, gpointer value, gpointer data )
525{
526        return !xml_printf( *( (int*) data ), 2, "<buddy handle=\"%s\" nick=\"%s\" />\n", key, value );
527}
528
529static storage_status_t xml_remove( const char *nick, const char *password )
530{
531        char s[512], *lc;
532        storage_status_t status;
533
534        status = xml_check_pass( nick, password );
535        if( status != STORAGE_OK )
536                return status;
537
538        lc = g_strdup( nick );
539        nick_lc( lc );
540        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, lc, ".xml" );
541        g_free( lc );
542       
543        if( unlink( s ) == -1 )
544                return STORAGE_OTHER_ERROR;
545       
546        return STORAGE_OK;
547}
548
549storage_t storage_xml = {
550        .name = "xml",
551        .init = xml_init,
552        .check_pass = xml_check_pass,
553        .remove = xml_remove,
554        .load = xml_load,
555        .save = xml_save
556};
Note: See TracBrowser for help on using the repository browser.