source: storage_xml.c @ b9e4ab5

Last change on this file since b9e4ab5 was 3b6eadc, checked in by Jelmer Vernooij <jelmer@…>, at 2007-07-07T17:19:28Z

Fix some warnings in storage.c.

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