source: storage_xml.c @ 8eb0b76

Last change on this file since 8eb0b76 was c351434, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-07-03T21:32:10Z

Skip unsupported tags in user configs. (This should make downgrades from
ui-fix for whatever reason less painful.)

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