source: storage_xml.c @ 823de9d

Last change on this file since 823de9d was 823de9d, checked in by Sven Moritz Hallberg <pesco@…>, at 2009-03-12T19:10:06Z

commit updates by ashish shukla <wahjava@…>

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