source: storage_xml.c @ c43f488

Last change on this file since c43f488 was c43f488, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-06-04T10:58:51Z

Remove two hacks for some glib<2.14 versions that are no longer supported.

  • Property mode set to 100644
File size: 17.6 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#include <glib/gstdio.h>
33
34typedef enum
35{
36        XML_PASS_CHECK_ONLY = -1,
37        XML_PASS_UNKNOWN = 0,
38        XML_PASS_WRONG,
39        XML_PASS_OK
40} xml_pass_st;
41
42/* To make it easier later when extending the format: */
43#define XML_FORMAT_VERSION 1
44
45struct xml_parsedata
46{
47        irc_t *irc;
48        char *current_setting;
49        account_t *current_account;
50        irc_channel_t *current_channel;
51        set_t **current_set_head;
52        char *given_nick;
53        char *given_pass;
54        xml_pass_st pass_st;
55        int unknown_tag;
56};
57
58static char *xml_attr( const gchar **attr_names, const gchar **attr_values, const gchar *key )
59{
60        int i;
61       
62        for( i = 0; attr_names[i]; i ++ )
63                if( g_strcasecmp( attr_names[i], key ) == 0 )
64                        return (char*) attr_values[i];
65       
66        return NULL;
67}
68
69static void xml_destroy_xd( gpointer data )
70{
71        struct xml_parsedata *xd = data;
72       
73        g_free( xd->given_nick );
74        g_free( xd->given_pass );
75        g_free( xd );
76}
77
78static void xml_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error )
79{
80        struct xml_parsedata *xd = data;
81        irc_t *irc = xd->irc;
82       
83        if( xd->unknown_tag > 0 )
84        {
85                xd->unknown_tag ++;
86        }
87        else if( g_strcasecmp( element_name, "user" ) == 0 )
88        {
89                char *nick = xml_attr( attr_names, attr_values, "nick" );
90                char *pass = xml_attr( attr_names, attr_values, "password" );
91                int st;
92               
93                if( !nick || !pass )
94                {
95                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
96                                     "Missing attributes for %s element", element_name );
97                }
98                else if( ( st = md5_verify_password( xd->given_pass, pass ) ) == -1 )
99                {
100                        xd->pass_st = XML_PASS_WRONG;
101                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
102                                     "Error while decoding password attribute" );
103                }
104                else if( st == 0 )
105                {
106                        if( xd->pass_st != XML_PASS_CHECK_ONLY )
107                                xd->pass_st = XML_PASS_OK;
108                }
109                else
110                {
111                        xd->pass_st = XML_PASS_WRONG;
112                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
113                                     "Password mismatch" );
114                }
115        }
116        else if( xd->pass_st < XML_PASS_OK )
117        {
118                /* Let's not parse anything else if we only have to check
119                   the password. */
120        }
121        else if( g_strcasecmp( element_name, "account" ) == 0 )
122        {
123                char *protocol, *handle, *server, *password = NULL, *autoconnect, *tag;
124                char *pass_b64 = NULL;
125                unsigned char *pass_cr = NULL;
126                int pass_len;
127                struct prpl *prpl = NULL;
128               
129                handle = xml_attr( attr_names, attr_values, "handle" );
130                pass_b64 = xml_attr( attr_names, attr_values, "password" );
131                server = xml_attr( attr_names, attr_values, "server" );
132                autoconnect = xml_attr( attr_names, attr_values, "autoconnect" );
133                tag = xml_attr( attr_names, attr_values, "tag" );
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 ) >= 0 )
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                        if( tag )
154                                set_setstr( &xd->current_account->set, "tag", tag );
155                }
156                else
157                {
158                        /* Actually the _decode functions don't even return error codes,
159                           but maybe they will later... */
160                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
161                                     "Error while decrypting account password" );
162                }
163               
164                g_free( pass_cr );
165                g_free( password );
166        }
167        else if( g_strcasecmp( element_name, "setting" ) == 0 )
168        {
169                char *setting;
170               
171                if( xd->current_setting )
172                {
173                        g_free( xd->current_setting );
174                        xd->current_setting = NULL;
175                }
176               
177                if( ( setting = xml_attr( attr_names, attr_values, "name" ) ) )
178                {
179                        if( xd->current_channel != NULL )
180                                xd->current_set_head = &xd->current_channel->set;
181                        else if( xd->current_account != NULL )
182                                xd->current_set_head = &xd->current_account->set;
183                        else
184                                xd->current_set_head = &xd->irc->b->set;
185                       
186                        xd->current_setting = g_strdup( setting );
187                }
188                else
189                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
190                                     "Missing attributes for %s element", element_name );
191        }
192        else if( g_strcasecmp( element_name, "buddy" ) == 0 )
193        {
194                char *handle, *nick;
195               
196                handle = xml_attr( attr_names, attr_values, "handle" );
197                nick = xml_attr( attr_names, attr_values, "nick" );
198               
199                if( xd->current_account && handle && nick )
200                {
201                        nick_set_raw( xd->current_account, handle, nick );
202                }
203                else
204                {
205                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
206                                     "Missing attributes for %s element", element_name );
207                }
208        }
209        else if( g_strcasecmp( element_name, "channel" ) == 0 )
210        {
211                char *name, *type;
212               
213                name = xml_attr( attr_names, attr_values, "name" );
214                type = xml_attr( attr_names, attr_values, "type" );
215               
216                if( !name || !type )
217                {
218                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
219                                     "Missing attributes for %s element", element_name );
220                        return;
221                }
222               
223                /* The channel may exist already, for example if it's &bitlbee.
224                   Also, it's possible that the user just reconnected and the
225                   IRC client already rejoined all channels it was in. They
226                   should still get the right settings. */
227                if( ( xd->current_channel = irc_channel_by_name( irc, name ) ) ||
228                    ( xd->current_channel = irc_channel_new( irc, name ) ) )
229                        set_setstr( &xd->current_channel->set, "type", type );
230        }
231        /* Backward compatibility: Keep this around for a while for people
232           switching from BitlBee 1.2.4+. */
233        else if( g_strcasecmp( element_name, "chat" ) == 0 )
234        {
235                char *handle, *channel;
236               
237                handle = xml_attr( attr_names, attr_values, "handle" );
238                channel = xml_attr( attr_names, attr_values, "channel" );
239               
240                if( xd->current_account && handle && channel )
241                {
242                        irc_channel_t *ic;
243                       
244                        if( ( ic = irc_channel_new( irc, channel ) ) &&
245                            set_setstr( &ic->set, "type", "chat" ) &&
246                            set_setstr( &ic->set, "chat_type", "room" ) &&
247                            set_setstr( &ic->set, "account", xd->current_account->tag ) &&
248                            set_setstr( &ic->set, "room", handle ) )
249                        {
250                                /* Try to pick up some settings where possible. */
251                                xd->current_channel = ic;
252                        }
253                        else if( ic )
254                                irc_channel_free( ic );
255                }
256                else
257                {
258                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
259                                     "Missing attributes for %s element", element_name );
260                }
261        }
262        else
263        {
264                xd->unknown_tag ++;
265                irc_rootmsg( irc, "Warning: Unknown XML tag found in configuration file (%s). "
266                                  "This may happen when downgrading BitlBee versions. "
267                                  "This tag will be skipped and the information will be lost "
268                                  "once you save your settings.", element_name );
269                /*
270                g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
271                             "Unkown element: %s", element_name );
272                */
273        }
274}
275
276static void xml_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error )
277{
278        struct xml_parsedata *xd = data;
279       
280        if( xd->unknown_tag > 0 )
281        {
282                xd->unknown_tag --;
283        }
284        else if( g_strcasecmp( element_name, "setting" ) == 0 && xd->current_setting )
285        {
286                g_free( xd->current_setting );
287                xd->current_setting = NULL;
288        }
289        else if( g_strcasecmp( element_name, "account" ) == 0 )
290        {
291                xd->current_account = NULL;
292        }
293        else if( g_strcasecmp( element_name, "channel" ) == 0 ||
294                 g_strcasecmp( element_name, "chat" ) == 0 )
295        {
296                xd->current_channel = NULL;
297        }
298}
299
300static void xml_text( GMarkupParseContext *ctx, const gchar *text_orig, gsize text_len, gpointer data, GError **error )
301{
302        char text[text_len+1];
303        struct xml_parsedata *xd = data;
304       
305        strncpy( text, text_orig, text_len );
306        text[text_len] = 0;
307       
308        if( xd->pass_st < XML_PASS_OK )
309        {
310                /* Let's not parse anything else if we only have to check
311                   the password, or if we didn't get the chance to check it
312                   yet. */
313        }
314        else if( g_strcasecmp( g_markup_parse_context_get_element( ctx ), "setting" ) == 0 && xd->current_setting )
315        {
316                if( xd->current_account )
317                {
318                        set_t *s = set_find( xd->current_set_head, xd->current_setting );
319                        if( s && ( s->flags & ACC_SET_ONLINE_ONLY ) )
320                        {
321                                g_free( xd->current_setting );
322                                xd->current_setting = NULL;
323                                return;
324                        }
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_rootmsg( 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_rootmsg( 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 && !( set->flags & SET_NOSAVE ) )
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\" "
505                                        "autoconnect=\"%d\" tag=\"%s\"", acc->prpl->name, acc->user,
506                                        pass_b64, acc->auto_connect, acc->tag ) )
507                {
508                        g_free( pass_b64 );
509                        goto write_error;
510                }
511                g_free( pass_b64 );
512               
513                if( acc->server && acc->server[0] && !xml_printf( fd, 0, " server=\"%s\"", acc->server ) )
514                        goto write_error;
515                if( !xml_printf( fd, 0, ">\n" ) )
516                        goto write_error;
517               
518                for( set = acc->set; set; set = set->next )
519                        if( set->value && !( set->flags & ACC_SET_NOSAVE ) )
520                                if( !xml_printf( fd, 2, "<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
521                                        goto write_error;
522               
523                /* This probably looks pretty strange. g_hash_table_foreach
524                   is quite a PITA already (but it can't get much better in
525                   C without using #define, I'm afraid), and since it
526                   doesn't seem to be possible to abort the foreach on write
527                   errors, so instead let's use the _find function and
528                   return TRUE on write errors. Which means, if we found
529                   something, there was an error. :-) */
530                if( g_hash_table_find( acc->nicks, xml_save_nick, & fd ) )
531                        goto write_error;
532               
533                if( !xml_printf( fd, 1, "</account>\n" ) )
534                        goto write_error;
535        }
536       
537        for( l = irc->channels; l; l = l->next )
538        {
539                irc_channel_t *ic = l->data;
540               
541                if( ic->flags & IRC_CHANNEL_TEMP )
542                        continue;
543               
544                if( !xml_printf( fd, 1, "<channel name=\"%s\" type=\"%s\">\n",
545                                 ic->name, set_getstr( &ic->set, "type" ) ) )
546                        goto write_error;
547               
548                for( set = ic->set; set; set = set->next )
549                        if( set->value && strcmp( set->key, "type" ) != 0 )
550                                if( !xml_printf( fd, 2, "<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
551                                        goto write_error;
552               
553                if( !xml_printf( fd, 1, "</channel>\n" ) )
554                        goto write_error;
555        }
556       
557        if( !xml_printf( fd, 0, "</user>\n" ) )
558                goto write_error;
559       
560        fsync( fd );
561        close( fd );
562       
563        path2 = g_strndup( path, strlen( path ) - 7 );
564        if( rename( path, path2 ) != 0 )
565        {
566                irc_rootmsg( irc, "Error while renaming temporary configuration file." );
567               
568                g_free( path2 );
569                unlink( path );
570               
571                return STORAGE_OTHER_ERROR;
572        }
573       
574        g_free( path2 );
575       
576        return STORAGE_OK;
577
578write_error:
579        g_free( pass_buf );
580       
581        irc_rootmsg( irc, "Write error. Disk full?" );
582        close( fd );
583       
584        return STORAGE_OTHER_ERROR;
585}
586
587static gboolean xml_save_nick( gpointer key, gpointer value, gpointer data )
588{
589        return !xml_printf( *( (int*) data ), 2, "<buddy handle=\"%s\" nick=\"%s\" />\n", key, value );
590}
591
592static storage_status_t xml_remove( const char *nick, const char *password )
593{
594        char s[512], *lc;
595        storage_status_t status;
596
597        status = xml_check_pass( nick, password );
598        if( status != STORAGE_OK )
599                return status;
600
601        lc = g_strdup( nick );
602        nick_lc( lc );
603        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, lc, ".xml" );
604        g_free( lc );
605       
606        if( unlink( s ) == -1 )
607                return STORAGE_OTHER_ERROR;
608       
609        return STORAGE_OK;
610}
611
612storage_t storage_xml = {
613        .name = "xml",
614        .init = xml_init,
615        .check_pass = xml_check_pass,
616        .remove = xml_remove,
617        .load = xml_load,
618        .save = xml_save
619};
Note: See TracBrowser for help on using the repository browser.