source: storage_xml.c @ 15d1469

Last change on this file since 15d1469 was 15d1469, checked in by Wilmer van der Gaast <wilmer@…>, at 2008-09-06T23:10:47Z

Really removing the gstdio.h include now to fully fix GLib 2.4
compatibility.

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