source: storage_xml.c @ f277225

Last change on this file since f277225 was f277225, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-06-06T21:58:46Z

Use mkstemp() instead of just a tilde-file when writing new configs to a
temporary file. This solves hard-to-debug issues where for example the
user hand-edited his configs as root and left a root-owned user.xml~ file
behind.

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