source: storage_xml.c @ 00ab350

Last change on this file since 00ab350 was 00ab350, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-21T17:14:49Z

Fixed GError memory leak, correctly setting the migrate_storage default.

  • Property mode set to 100644
File size: 12.6 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"
[d28f3b35]28#include "md5.h"
[a312b6b]29
[c121f89]30typedef enum
31{
32        XML_PASS_CHECK_ONLY = -1,
33        XML_PASS_UNKNOWN = 0,
34        XML_PASS_OK
35} xml_pass_st;
36
[84e9cea]37/* This isn't very clean, probably making a separate error class + code for
38   BitlBee would be a better solution. But this will work for now... */
[c121f89]39#define XML_PASS_ERRORMSG "Wrong username or password"
40
[a312b6b]41struct xml_parsedata
42{
43        irc_t *irc;
44        char *current_setting;
45        account_t *current_account;
[c121f89]46        char *given_nick;
47        char *given_pass;
48        xml_pass_st pass_st;
[a312b6b]49};
50
51static char *xml_attr( const gchar **attr_names, const gchar **attr_values, const gchar *key )
52{
53        int i;
54       
55        for( i = 0; attr_names[i]; i ++ )
56                if( g_strcasecmp( attr_names[i], key ) == 0 )
[c121f89]57                        return (char*) attr_values[i];
[a312b6b]58       
59        return NULL;
60}
61
[c121f89]62static void xml_destroy_xd( gpointer data )
63{
64        struct xml_parsedata *xd = data;
65       
66        g_free( xd->given_nick );
67        g_free( xd->given_pass );
68        g_free( xd );
69}
70
[a312b6b]71static void xml_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error )
72{
73        struct xml_parsedata *xd = data;
[c121f89]74        irc_t *irc = xd->irc;
[a312b6b]75       
76        if( g_strcasecmp( element_name, "user" ) == 0 )
77        {
78                char *nick = xml_attr( attr_names, attr_values, "nick" );
[c121f89]79                char *pass = xml_attr( attr_names, attr_values, "password" );
[a312b6b]80               
[c121f89]81                if( !nick || !pass )
[a312b6b]82                {
[c121f89]83                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
84                                     "Missing attributes for %s element", element_name );
[a312b6b]85                }
[c121f89]86                else
87                {
[d28f3b35]88                        md5_byte_t pass_md5[16];
89                        md5_state_t md5_state;
[d028a77]90                        int i, j;
[d28f3b35]91                       
92                        md5_init( &md5_state );
[d028a77]93                        md5_append( &md5_state, (md5_byte_t*) xd->given_pass, strlen( xd->given_pass ) );
[d28f3b35]94                        md5_finish( &md5_state, pass_md5 );
95                       
[d028a77]96                        for( i = 0; i < 16; i ++ )
[d28f3b35]97                        {
[d028a77]98                                if( !isxdigit( pass[i*2] ) || !isxdigit( pass[i*2+1] ) ||
99                                     sscanf( pass + i * 2, "%2x", &j ) != 1 )
100                                {
101                                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
102                                                     "Incorrect password MD5-hash" );
103                                        break;
104                                }
[d28f3b35]105                                if( j != pass_md5[i] )
[d028a77]106                                {
107                                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
108                                                     XML_PASS_ERRORMSG );
[d28f3b35]109                                        break;
[d028a77]110                                }
[d28f3b35]111                        }
112                       
[d028a77]113                        /* If we reached the end of the loop, it was a match! */
114                        if( i == 16 )
[d28f3b35]115                        {
116                                if( xd->pass_st != XML_PASS_CHECK_ONLY )
117                                        xd->pass_st = XML_PASS_OK;
118                        }
[c121f89]119                }
120        }
121        else if( xd->pass_st < XML_PASS_OK )
122        {
123                /* Let's not parse anything else if we only have to check
124                   the password. */
[a312b6b]125        }
126        else if( g_strcasecmp( element_name, "account" ) == 0 )
127        {
[2b14eef]128                char *protocol, *handle, *server, *password, *autoconnect;
[a312b6b]129                struct prpl *prpl = NULL;
130               
131                handle = xml_attr( attr_names, attr_values, "handle" );
132                password = xml_attr( attr_names, attr_values, "password" );
[c121f89]133                server = xml_attr( attr_names, attr_values, "server" );
[2b14eef]134                autoconnect = xml_attr( attr_names, attr_values, "autoconnect" );
[a312b6b]135               
136                protocol = xml_attr( attr_names, attr_values, "protocol" );
137                if( protocol )
138                        prpl = find_protocol( protocol );
139               
[9b46b64]140                if( !handle || !password || !protocol )
[c121f89]141                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
142                                     "Missing attributes for %s element", element_name );
143                else if( !prpl )
144                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
[9b46b64]145                                     "Unknown protocol: %s", protocol );
[c121f89]146                else
[a312b6b]147                {
[c121f89]148                        xd->current_account = account_add( irc, prpl, handle, password );
149                        if( server )
150                                xd->current_account->server = g_strdup( server );
[2b14eef]151                        if( autoconnect )
152                                /* Return value doesn't matter, since account_add() already sets
153                                   a default! */
154                                sscanf( autoconnect, "%d", &xd->current_account->auto_connect );
[a312b6b]155                }
156        }
157        else if( g_strcasecmp( element_name, "setting" ) == 0 )
158        {
159                if( xd->current_account == NULL )
160                {
[c121f89]161                        char *setting;
162                       
163                        if( xd->current_setting )
164                        {
165                                g_free( xd->current_setting );
166                                xd->current_setting = NULL;
167                        }
168                       
169                        if( ( setting = xml_attr( attr_names, attr_values, "name" ) ) )
170                                xd->current_setting = g_strdup( setting );
171                        else
172                                g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
173                                             "Missing attributes for %s element", element_name );
[a312b6b]174                }
175        }
176        else if( g_strcasecmp( element_name, "buddy" ) == 0 )
177        {
[c121f89]178                char *handle, *nick;
179               
180                handle = xml_attr( attr_names, attr_values, "handle" );
181                nick = xml_attr( attr_names, attr_values, "nick" );
182               
183                if( xd->current_account && handle && nick )
184                {
185                        nick_set( irc, handle, xd->current_account->prpl, nick );
186                }
187                else
188                {
189                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
190                                     "Missing attributes for %s element", element_name );
191                }
[a312b6b]192        }
193        else
194        {
[c121f89]195                g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
196                             "Unkown element: %s", element_name );
[a312b6b]197        }
198}
199
200static void xml_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error )
201{
[5898ef8]202        struct xml_parsedata *xd = data;
203       
204        if( g_strcasecmp( element_name, "setting" ) == 0 && xd->current_setting )
205        {
206                g_free( xd->current_setting );
207                xd->current_setting = NULL;
208        }
209        else if( g_strcasecmp( element_name, "account" ) == 0 )
210        {
211                xd->current_account = NULL;
212        }
[a312b6b]213}
214
215static void xml_text( GMarkupParseContext *ctx, const gchar *text, gsize text_len, gpointer data, GError **error )
216{
217        struct xml_parsedata *xd = data;
[c121f89]218        irc_t *irc = xd->irc;
[a312b6b]219       
[c121f89]220        if( xd->pass_st < XML_PASS_OK )
221        {
222                /* Let's not parse anything else if we only have to check
[84e9cea]223                   the password, or if we didn't get the chance to check it
224                   yet. */
[c121f89]225        }
226        else if( g_strcasecmp( g_markup_parse_context_get_element( ctx ), "setting" ) == 0 &&
227                 xd->current_setting && xd->current_account == NULL )
[a312b6b]228        {
[c121f89]229                set_setstr( irc, xd->current_setting, (char*) text );
230                g_free( xd->current_setting );
231                xd->current_setting = NULL;
[a312b6b]232        }
233}
234
235GMarkupParser xml_parser =
236{
237        xml_start_element,
238        xml_end_element,
239        xml_text,
240        NULL,
[5898ef8]241        NULL
[a312b6b]242};
243
244static void xml_init( void )
245{
246        if( access( global.conf->configdir, F_OK ) != 0 )
247                log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG );
248        else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 )
249                log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir );
250}
251
[84e9cea]252static storage_status_t xml_load_real( const char *my_nick, const char *password, irc_t *irc, xml_pass_st action )
[a312b6b]253{
254        GMarkupParseContext *ctx;
[c121f89]255        struct xml_parsedata *xd;
256        char *fn, buf[512];
257        GError *gerr = NULL;
258        int fd, st;
[a312b6b]259       
[84e9cea]260        if( irc && irc->status & USTATUS_IDENTIFIED )
[a312b6b]261                return( 1 );
262       
[c121f89]263        xd = g_new0( struct xml_parsedata, 1 );
264        xd->irc = irc;
265        xd->given_nick = g_strdup( my_nick );
266        xd->given_pass = g_strdup( password );
[84e9cea]267        xd->pass_st = action;
[c121f89]268        nick_lc( xd->given_nick );
[a312b6b]269       
[c121f89]270        fn = g_strdup_printf( "%s%s%s", global.conf->configdir, xd->given_nick, ".xml" );
271        if( ( fd = open( fn, O_RDONLY ) ) < 0 )
[a312b6b]272        {
[c121f89]273                xml_destroy_xd( xd );
274                g_free( fn );
275                return STORAGE_NO_SUCH_USER;
[a312b6b]276        }
[c121f89]277        g_free( fn );
[a312b6b]278       
[c121f89]279        ctx = g_markup_parse_context_new( &xml_parser, 0, xd, xml_destroy_xd );
[a312b6b]280       
[c121f89]281        while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 )
[a312b6b]282        {
[c121f89]283                if( !g_markup_parse_context_parse( ctx, buf, st, &gerr ) || gerr )
284                {
285                        g_markup_parse_context_free( ctx );
[5898ef8]286                        close( fd );
[c121f89]287                       
[5898ef8]288                        /* Slightly dirty... */
[c121f89]289                        if( gerr && strcmp( gerr->message, XML_PASS_ERRORMSG ) == 0 )
[00ab350]290                        {
291                                g_clear_error( &gerr );
[c121f89]292                                return STORAGE_INVALID_PASSWORD;
[00ab350]293                        }
[c121f89]294                        else
[5898ef8]295                        {
[84e9cea]296                                if( gerr && irc )
[5898ef8]297                                        irc_usermsg( irc, "Error from XML-parser: %s", gerr->message );
298                               
[00ab350]299                                g_clear_error( &gerr );
[c121f89]300                                return STORAGE_OTHER_ERROR;
[5898ef8]301                        }
[c121f89]302                }
[a312b6b]303        }
[00ab350]304        /* Just to be sure... */
305        g_clear_error( &gerr );
[a312b6b]306       
[c121f89]307        g_markup_parse_context_free( ctx );
[5898ef8]308        close( fd );
[c121f89]309       
[84e9cea]310        if( action == XML_PASS_CHECK_ONLY )
311                return STORAGE_OK;
312       
[5898ef8]313        irc->status |= USTATUS_IDENTIFIED;
[a312b6b]314       
315        if( set_getint( irc, "auto_connect" ) )
316        {
[c121f89]317                /* Can't do this directly because r_c_s alters the string */
318                strcpy( buf, "account on" );
319                root_command_string( irc, NULL, buf, 0 );
[a312b6b]320        }
321       
322        return STORAGE_OK;
323}
324
[84e9cea]325static storage_status_t xml_load( const char *my_nick, const char *password, irc_t *irc )
326{
327        return xml_load_real( my_nick, password, irc, XML_PASS_UNKNOWN );
328}
329
330static storage_status_t xml_check_pass( const char *my_nick, const char *password )
331{
332        /* This is a little bit risky because we have to pass NULL for the
333           irc_t argument. This *should* be fine, if I didn't miss anything... */
334        return xml_load_real( my_nick, password, NULL, XML_PASS_CHECK_ONLY );
335}
336
[5898ef8]337static int xml_printf( int fd, char *fmt, ... )
338{
339        va_list params;
340        char *out;
341        int len;
342       
343        va_start( params, fmt );
344        out = g_markup_vprintf_escaped( fmt, params );
345        va_end( params );
346       
347        len = strlen( out );
348        len -= write( fd, out, len );
349        g_free( out );
350       
351        return len == 0;
352}
353
[c121f89]354static storage_status_t xml_save( irc_t *irc, int overwrite )
[a312b6b]355{
[ece2cd2]356        char path[512], *path2, md5_buf[33];
[5898ef8]357        set_t *set;
358        nick_t *nick;
359        account_t *acc;
[ece2cd2]360        int fd, i;
361        md5_byte_t pass_md5[16];
362        md5_state_t md5_state;
363       
364        if( irc->password == NULL )
365        {
366                irc_usermsg( irc, "Please register yourself if you want to save your settings." );
367                return STORAGE_OTHER_ERROR;
368        }
[a312b6b]369       
[5898ef8]370        g_snprintf( path, sizeof( path ) - 2, "%s%s%s", global.conf->configdir, irc->nick, ".xml" );
371       
372        if( !overwrite && access( path, F_OK ) != -1 )
373                return STORAGE_ALREADY_EXISTS;
374       
375        strcat( path, "~" );
376        if( ( fd = open( path, O_WRONLY | O_CREAT, 0600 ) ) < 0 )
377        {
378                irc_usermsg( irc, "Error while opening configuration file." );
379                return STORAGE_OTHER_ERROR;
[a312b6b]380        }
[5898ef8]381       
[ece2cd2]382        md5_init( &md5_state );
383        md5_append( &md5_state, (md5_byte_t*) irc->password, strlen( irc->password ) );
384        md5_finish( &md5_state, pass_md5 );
385        for( i = 0; i < 16; i ++ )
386                g_snprintf( md5_buf + i * 2, 3, "%02x", pass_md5[i] );
387       
388        if( !xml_printf( fd, "<user nick=\"%s\" password=\"%s\">\n", irc->nick, md5_buf ) )
[5898ef8]389                goto write_error;
390       
391        for( set = irc->set; set; set = set->next )
392                if( set->value && set->def )
393                        if( !xml_printf( fd, "\t<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
394                                goto write_error;
395       
396        for( acc = irc->accounts; acc; acc = acc->next )
397        {
[2b14eef]398                if( !xml_printf( fd, "\t<account protocol=\"%s\" handle=\"%s\" password=\"%s\" autoconnect=\"%d\"", acc->prpl->name, acc->user, acc->pass, acc->auto_connect ) )
[5898ef8]399                        goto write_error;
400                if( acc->server && acc->server[0] && !xml_printf( fd, " server=\"%s\"", acc->server ) )
401                        goto write_error;
402                if( !xml_printf( fd, ">\n" ) )
403                        goto write_error;
404               
405                for( nick = irc->nicks; nick; nick = nick->next )
406                        if( nick->proto == acc->prpl )
407                                if( !xml_printf( fd, "\t\t<buddy handle=\"%s\" nick=\"%s\" />\n", nick->handle, nick->nick ) )
408                                        goto write_error;
409               
410                if( !xml_printf( fd, "\t</account>\n" ) )
411                        goto write_error;
412        }
413       
414        if( !xml_printf( fd, "</user>\n" ) )
415                goto write_error;
416       
417        close( fd );
418       
419        path2 = g_strndup( path, strlen( path ) - 1 );
420        if( rename( path, path2 ) != 0 )
421        {
422                irc_usermsg( irc, "Error while renaming temporary configuration file." );
423               
424                g_free( path2 );
425                unlink( path );
426               
427                return STORAGE_OTHER_ERROR;
428        }
429       
430        g_free( path2 );
431       
[a312b6b]432        return STORAGE_OK;
[5898ef8]433
434write_error:
435        irc_usermsg( irc, "Write error. Disk full?" );
436        close( fd );
437       
438        return STORAGE_OTHER_ERROR;
[a312b6b]439}
440
[84e9cea]441static storage_status_t xml_remove( const char *nick, const char *password )
442{
443        char s[512];
444        storage_status_t status;
445
446        status = xml_check_pass( nick, password );
447        if( status != STORAGE_OK )
448                return status;
449
450        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".xml" );
451        if( unlink( s ) == -1 )
452                return STORAGE_OTHER_ERROR;
453       
454        return STORAGE_OK;
455}
456
[a312b6b]457storage_t storage_xml = {
458        .name = "xml",
459        .init = xml_init,
[84e9cea]460        .check_pass = xml_check_pass,
461        .remove = xml_remove,
[a312b6b]462        .load = xml_load,
463        .save = xml_save
464};
Note: See TracBrowser for help on using the repository browser.