source: storage_xml.c @ 40e6dac

Last change on this file since 40e6dac was 4346c3f4, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-16T23:31:55Z

Merging mainline.

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