source: storage_xml.c @ 00ab350

Last change on this file since 00ab350 was 00ab350, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-21T17:14:49Z

Fixed GError memory leak, correctly setting the migrate_storage default.

  • Property mode set to 100644
File size: 12.6 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 "md5.h"
29
30typedef enum
31{
32        XML_PASS_CHECK_ONLY = -1,
33        XML_PASS_UNKNOWN = 0,
34        XML_PASS_OK
35} xml_pass_st;
36
37/* This isn't very clean, probably making a separate error class + code for
38   BitlBee would be a better solution. But this will work for now... */
39#define XML_PASS_ERRORMSG "Wrong username or password"
40
41struct xml_parsedata
42{
43        irc_t *irc;
44        char *current_setting;
45        account_t *current_account;
46        char *given_nick;
47        char *given_pass;
48        xml_pass_st pass_st;
49};
50
51static char *xml_attr( const gchar **attr_names, const gchar **attr_values, const gchar *key )
52{
53        int i;
54       
55        for( i = 0; attr_names[i]; i ++ )
56                if( g_strcasecmp( attr_names[i], key ) == 0 )
57                        return (char*) attr_values[i];
58       
59        return NULL;
60}
61
62static void xml_destroy_xd( gpointer data )
63{
64        struct xml_parsedata *xd = data;
65       
66        g_free( xd->given_nick );
67        g_free( xd->given_pass );
68        g_free( xd );
69}
70
71static void xml_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error )
72{
73        struct xml_parsedata *xd = data;
74        irc_t *irc = xd->irc;
75       
76        if( g_strcasecmp( element_name, "user" ) == 0 )
77        {
78                char *nick = xml_attr( attr_names, attr_values, "nick" );
79                char *pass = xml_attr( attr_names, attr_values, "password" );
80               
81                if( !nick || !pass )
82                {
83                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
84                                     "Missing attributes for %s element", element_name );
85                }
86                else
87                {
88                        md5_byte_t pass_md5[16];
89                        md5_state_t md5_state;
90                        int i, j;
91                       
92                        md5_init( &md5_state );
93                        md5_append( &md5_state, (md5_byte_t*) xd->given_pass, strlen( xd->given_pass ) );
94                        md5_finish( &md5_state, pass_md5 );
95                       
96                        for( i = 0; i < 16; i ++ )
97                        {
98                                if( !isxdigit( pass[i*2] ) || !isxdigit( pass[i*2+1] ) ||
99                                     sscanf( pass + i * 2, "%2x", &j ) != 1 )
100                                {
101                                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
102                                                     "Incorrect password MD5-hash" );
103                                        break;
104                                }
105                                if( j != pass_md5[i] )
106                                {
107                                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
108                                                     XML_PASS_ERRORMSG );
109                                        break;
110                                }
111                        }
112                       
113                        /* If we reached the end of the loop, it was a match! */
114                        if( i == 16 )
115                        {
116                                if( xd->pass_st != XML_PASS_CHECK_ONLY )
117                                        xd->pass_st = XML_PASS_OK;
118                        }
119                }
120        }
121        else if( xd->pass_st < XML_PASS_OK )
122        {
123                /* Let's not parse anything else if we only have to check
124                   the password. */
125        }
126        else if( g_strcasecmp( element_name, "account" ) == 0 )
127        {
128                char *protocol, *handle, *server, *password, *autoconnect;
129                struct prpl *prpl = NULL;
130               
131                handle = xml_attr( attr_names, attr_values, "handle" );
132                password = xml_attr( attr_names, attr_values, "password" );
133                server = xml_attr( attr_names, attr_values, "server" );
134                autoconnect = xml_attr( attr_names, attr_values, "autoconnect" );
135               
136                protocol = xml_attr( attr_names, attr_values, "protocol" );
137                if( protocol )
138                        prpl = find_protocol( protocol );
139               
140                if( !handle || !password || !protocol )
141                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
142                                     "Missing attributes for %s element", element_name );
143                else if( !prpl )
144                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
145                                     "Unknown protocol: %s", protocol );
146                else
147                {
148                        xd->current_account = account_add( irc, prpl, handle, password );
149                        if( server )
150                                xd->current_account->server = g_strdup( server );
151                        if( autoconnect )
152                                /* Return value doesn't matter, since account_add() already sets
153                                   a default! */
154                                sscanf( autoconnect, "%d", &xd->current_account->auto_connect );
155                }
156        }
157        else if( g_strcasecmp( element_name, "setting" ) == 0 )
158        {
159                if( xd->current_account == NULL )
160                {
161                        char *setting;
162                       
163                        if( xd->current_setting )
164                        {
165                                g_free( xd->current_setting );
166                                xd->current_setting = NULL;
167                        }
168                       
169                        if( ( setting = xml_attr( attr_names, attr_values, "name" ) ) )
170                                xd->current_setting = g_strdup( setting );
171                        else
172                                g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
173                                             "Missing attributes for %s element", element_name );
174                }
175        }
176        else if( g_strcasecmp( element_name, "buddy" ) == 0 )
177        {
178                char *handle, *nick;
179               
180                handle = xml_attr( attr_names, attr_values, "handle" );
181                nick = xml_attr( attr_names, attr_values, "nick" );
182               
183                if( xd->current_account && handle && nick )
184                {
185                        nick_set( irc, handle, xd->current_account->prpl, nick );
186                }
187                else
188                {
189                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
190                                     "Missing attributes for %s element", element_name );
191                }
192        }
193        else
194        {
195                g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
196                             "Unkown element: %s", element_name );
197        }
198}
199
200static void xml_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error )
201{
202        struct xml_parsedata *xd = data;
203       
204        if( g_strcasecmp( element_name, "setting" ) == 0 && xd->current_setting )
205        {
206                g_free( xd->current_setting );
207                xd->current_setting = NULL;
208        }
209        else if( g_strcasecmp( element_name, "account" ) == 0 )
210        {
211                xd->current_account = NULL;
212        }
213}
214
215static void xml_text( GMarkupParseContext *ctx, const gchar *text, gsize text_len, gpointer data, GError **error )
216{
217        struct xml_parsedata *xd = data;
218        irc_t *irc = xd->irc;
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 &&
227                 xd->current_setting && xd->current_account == NULL )
228        {
229                set_setstr( irc, 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( access( global.conf->configdir, F_OK ) != 0 )
247                log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG );
248        else if( access( global.conf->configdir, R_OK ) != 0 || 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                        g_markup_parse_context_free( ctx );
286                        close( fd );
287                       
288                        /* Slightly dirty... */
289                        if( gerr && strcmp( gerr->message, XML_PASS_ERRORMSG ) == 0 )
290                        {
291                                g_clear_error( &gerr );
292                                return STORAGE_INVALID_PASSWORD;
293                        }
294                        else
295                        {
296                                if( gerr && irc )
297                                        irc_usermsg( irc, "Error from XML-parser: %s", gerr->message );
298                               
299                                g_clear_error( &gerr );
300                                return STORAGE_OTHER_ERROR;
301                        }
302                }
303        }
304        /* Just to be sure... */
305        g_clear_error( &gerr );
306       
307        g_markup_parse_context_free( ctx );
308        close( fd );
309       
310        if( action == XML_PASS_CHECK_ONLY )
311                return STORAGE_OK;
312       
313        irc->status |= USTATUS_IDENTIFIED;
314       
315        if( set_getint( irc, "auto_connect" ) )
316        {
317                /* Can't do this directly because r_c_s alters the string */
318                strcpy( buf, "account on" );
319                root_command_string( irc, NULL, buf, 0 );
320        }
321       
322        return STORAGE_OK;
323}
324
325static storage_status_t xml_load( const char *my_nick, const char *password, irc_t *irc )
326{
327        return xml_load_real( my_nick, password, irc, XML_PASS_UNKNOWN );
328}
329
330static storage_status_t xml_check_pass( const char *my_nick, const char *password )
331{
332        /* This is a little bit risky because we have to pass NULL for the
333           irc_t argument. This *should* be fine, if I didn't miss anything... */
334        return xml_load_real( my_nick, password, NULL, XML_PASS_CHECK_ONLY );
335}
336
337static int xml_printf( int fd, char *fmt, ... )
338{
339        va_list params;
340        char *out;
341        int len;
342       
343        va_start( params, fmt );
344        out = g_markup_vprintf_escaped( fmt, params );
345        va_end( params );
346       
347        len = strlen( out );
348        len -= write( fd, out, len );
349        g_free( out );
350       
351        return len == 0;
352}
353
354static storage_status_t xml_save( irc_t *irc, int overwrite )
355{
356        char path[512], *path2, md5_buf[33];
357        set_t *set;
358        nick_t *nick;
359        account_t *acc;
360        int fd, i;
361        md5_byte_t pass_md5[16];
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        g_snprintf( path, sizeof( path ) - 2, "%s%s%s", global.conf->configdir, irc->nick, ".xml" );
371       
372        if( !overwrite && access( path, F_OK ) != -1 )
373                return STORAGE_ALREADY_EXISTS;
374       
375        strcat( path, "~" );
376        if( ( fd = open( path, O_WRONLY | O_CREAT, 0600 ) ) < 0 )
377        {
378                irc_usermsg( irc, "Error while opening configuration file." );
379                return STORAGE_OTHER_ERROR;
380        }
381       
382        md5_init( &md5_state );
383        md5_append( &md5_state, (md5_byte_t*) irc->password, strlen( irc->password ) );
384        md5_finish( &md5_state, pass_md5 );
385        for( i = 0; i < 16; i ++ )
386                g_snprintf( md5_buf + i * 2, 3, "%02x", pass_md5[i] );
387       
388        if( !xml_printf( fd, "<user nick=\"%s\" password=\"%s\">\n", irc->nick, md5_buf ) )
389                goto write_error;
390       
391        for( set = irc->set; set; set = set->next )
392                if( set->value && set->def )
393                        if( !xml_printf( fd, "\t<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
394                                goto write_error;
395       
396        for( acc = irc->accounts; acc; acc = acc->next )
397        {
398                if( !xml_printf( fd, "\t<account protocol=\"%s\" handle=\"%s\" password=\"%s\" autoconnect=\"%d\"", acc->prpl->name, acc->user, acc->pass, acc->auto_connect ) )
399                        goto write_error;
400                if( acc->server && acc->server[0] && !xml_printf( fd, " server=\"%s\"", acc->server ) )
401                        goto write_error;
402                if( !xml_printf( fd, ">\n" ) )
403                        goto write_error;
404               
405                for( nick = irc->nicks; nick; nick = nick->next )
406                        if( nick->proto == acc->prpl )
407                                if( !xml_printf( fd, "\t\t<buddy handle=\"%s\" nick=\"%s\" />\n", nick->handle, nick->nick ) )
408                                        goto write_error;
409               
410                if( !xml_printf( fd, "\t</account>\n" ) )
411                        goto write_error;
412        }
413       
414        if( !xml_printf( fd, "</user>\n" ) )
415                goto write_error;
416       
417        close( fd );
418       
419        path2 = g_strndup( path, strlen( path ) - 1 );
420        if( rename( path, path2 ) != 0 )
421        {
422                irc_usermsg( irc, "Error while renaming temporary configuration file." );
423               
424                g_free( path2 );
425                unlink( path );
426               
427                return STORAGE_OTHER_ERROR;
428        }
429       
430        g_free( path2 );
431       
432        return STORAGE_OK;
433
434write_error:
435        irc_usermsg( irc, "Write error. Disk full?" );
436        close( fd );
437       
438        return STORAGE_OTHER_ERROR;
439}
440
441static storage_status_t xml_remove( const char *nick, const char *password )
442{
443        char s[512];
444        storage_status_t status;
445
446        status = xml_check_pass( nick, password );
447        if( status != STORAGE_OK )
448                return status;
449
450        g_snprintf( s, 511, "%s%s%s", global.conf->configdir, nick, ".xml" );
451        if( unlink( s ) == -1 )
452                return STORAGE_OTHER_ERROR;
453       
454        return STORAGE_OK;
455}
456
457storage_t storage_xml = {
458        .name = "xml",
459        .init = xml_init,
460        .check_pass = xml_check_pass,
461        .remove = xml_remove,
462        .load = xml_load,
463        .save = xml_save
464};
Note: See TracBrowser for help on using the repository browser.