source: storage_xml.c @ 3183c21

Last change on this file since 3183c21 was 3183c21, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-09-06T22:59:32Z

Completely reviewed all uses of irc->password, irc_setpass() and
USTATUS_IDENTIFIED after another account overwriting vulnerability was
found by Tero Marttila.

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