source: storage_xml.c @ c9f0c79

Last change on this file since c9f0c79 was c9f0c79, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-26T12:04:21Z

Implemented cleaner way of passing the "Incorrect password" error.

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