source: storage_xml.c @ 1917a1ec

Last change on this file since 1917a1ec was 1917a1ec, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-08-30T19:43:38Z

Added support for loading chatroom data from user .xml files.

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