source: storage_xml.c @ 88d2208

Last change on this file since 88d2208 was 88d2208, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-08-24T16:45:40Z

Now really fixing #429 by including unistd.h (which defines F_OK).

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