source: storage_xml.c @ 5d3b4e8

1.2.1-2
Last change on this file since 5d3b4e8 was 5f5d433, checked in by Jelmer Vernooij <jelmer@…>, at 2008-04-02T14:17:37Z

Use GLib functions to check whether files exist, for extra portability.

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