source: storage_xml.c @ 15d1469

Last change on this file since 15d1469 was 15d1469, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-09-06T23:10:47Z

Really removing the gstdio.h include now to fully fix GLib 2.4
compatibility.

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