source: storage_xml.c @ d28f3b35

Last change on this file since d28f3b35 was d28f3b35, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-06-18T23:07:28Z

Now saving the password's md5sum instead of the plaintext version.

  • Property mode set to 100644
File size: 10.6 KB
RevLine 
[a312b6b]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"
[d28f3b35]28#include "md5.h"
[a312b6b]29
[c121f89]30typedef enum
31{
32        XML_PASS_CHECK_ONLY = -1,
33        XML_PASS_UNKNOWN = 0,
34        XML_PASS_OK
35} xml_pass_st;
36
37#define XML_PASS_ERRORMSG "Wrong username or password"
38
[a312b6b]39struct xml_parsedata
40{
41        irc_t *irc;
42        char *current_setting;
43        account_t *current_account;
[c121f89]44        char *given_nick;
45        char *given_pass;
46        xml_pass_st pass_st;
[a312b6b]47};
48
49static char *xml_attr( const gchar **attr_names, const gchar **attr_values, const gchar *key )
50{
51        int i;
52       
53        for( i = 0; attr_names[i]; i ++ )
54                if( g_strcasecmp( attr_names[i], key ) == 0 )
[c121f89]55                        return (char*) attr_values[i];
[a312b6b]56       
57        return NULL;
58}
59
[c121f89]60static void xml_destroy_xd( gpointer data )
61{
62        struct xml_parsedata *xd = data;
63       
64        g_free( xd->given_nick );
65        g_free( xd->given_pass );
66        g_free( xd );
67}
68
[a312b6b]69static void xml_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error )
70{
71        struct xml_parsedata *xd = data;
[c121f89]72        irc_t *irc = xd->irc;
[a312b6b]73       
74        if( g_strcasecmp( element_name, "user" ) == 0 )
75        {
76                char *nick = xml_attr( attr_names, attr_values, "nick" );
[c121f89]77                char *pass = xml_attr( attr_names, attr_values, "password" );
[a312b6b]78               
[c121f89]79                if( !nick || !pass )
[a312b6b]80                {
[c121f89]81                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
82                                     "Missing attributes for %s element", element_name );
[a312b6b]83                }
[c121f89]84                else
85                {
[d28f3b35]86                        md5_byte_t pass_md5[16];
87                        md5_state_t md5_state;
88                        int pass_match, i, j;
89                       
90                        md5_init( &md5_state );
91                        md5_append( &md5_state, xd->given_pass, strlen( xd->given_pass ) );
92                        md5_finish( &md5_state, pass_md5 );
93                       
94                        for( i = 0; i < 16 && pass[i*2] && pass[i*2+1]; i ++ )
95                        {
96                                sscanf( pass + i * 2, "%2x", &j );
97                                if( j != pass_md5[i] )
98                                        break;
99                        }
100                        /* If we reached the end of the loop, it was a match! */
101                        pass_match = i == 16;
102                       
103                        if( strcmp( nick, xd->given_nick ) == 0 && pass_match )
104                        {
105                                if( xd->pass_st != XML_PASS_CHECK_ONLY )
106                                        xd->pass_st = XML_PASS_OK;
107                        }
108                        else
109                        {
110                                g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
111                                             XML_PASS_ERRORMSG );
112                        }
[c121f89]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. */
[a312b6b]119        }
120        else if( g_strcasecmp( element_name, "account" ) == 0 )
121        {
[c121f89]122                char *protocol, *handle, *server, *password;
[a312b6b]123                struct prpl *prpl = NULL;
124               
125                handle = xml_attr( attr_names, attr_values, "handle" );
126                password = xml_attr( attr_names, attr_values, "password" );
[c121f89]127                server = xml_attr( attr_names, attr_values, "server" );
[a312b6b]128               
129                protocol = xml_attr( attr_names, attr_values, "protocol" );
130                if( protocol )
131                        prpl = find_protocol( protocol );
132               
[c121f89]133                if( !handle || !password )
134                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
135                                     "Missing attributes for %s element", element_name );
136                else if( !prpl )
137                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
138                                     "Missing or unknown protocol %s element", element_name );
139                else
[a312b6b]140                {
[c121f89]141                        xd->current_account = account_add( irc, prpl, handle, password );
142                        if( server )
143                                xd->current_account->server = g_strdup( server );
[a312b6b]144                }
145        }
146        else if( g_strcasecmp( element_name, "setting" ) == 0 )
147        {
148                if( xd->current_account == NULL )
149                {
[c121f89]150                        char *setting;
151                       
152                        if( xd->current_setting )
153                        {
154                                g_free( xd->current_setting );
155                                xd->current_setting = NULL;
156                        }
157                       
158                        if( ( setting = xml_attr( attr_names, attr_values, "name" ) ) )
159                                xd->current_setting = g_strdup( setting );
160                        else
161                                g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
162                                             "Missing attributes for %s element", element_name );
[a312b6b]163                }
164        }
165        else if( g_strcasecmp( element_name, "buddy" ) == 0 )
166        {
[c121f89]167                char *handle, *nick;
168               
169                handle = xml_attr( attr_names, attr_values, "handle" );
170                nick = xml_attr( attr_names, attr_values, "nick" );
171               
172                if( xd->current_account && handle && nick )
173                {
174                        nick_set( irc, handle, xd->current_account->prpl, nick );
175                }
176                else
177                {
178                        g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
179                                     "Missing attributes for %s element", element_name );
180                }
[a312b6b]181        }
182        else
183        {
[c121f89]184                g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
185                             "Unkown element: %s", element_name );
[a312b6b]186        }
187}
188
189static void xml_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error )
190{
[5898ef8]191        struct xml_parsedata *xd = data;
192        // irc_t *irc = xd->irc;
193       
194        if( g_strcasecmp( element_name, "setting" ) == 0 && xd->current_setting )
195        {
196                g_free( xd->current_setting );
197                xd->current_setting = NULL;
198        }
199        else if( g_strcasecmp( element_name, "account" ) == 0 )
200        {
201                xd->current_account = NULL;
202        }
[a312b6b]203}
204
205static void xml_text( GMarkupParseContext *ctx, const gchar *text, gsize text_len, gpointer data, GError **error )
206{
207        struct xml_parsedata *xd = data;
[c121f89]208        irc_t *irc = xd->irc;
[a312b6b]209       
[c121f89]210        if( xd->pass_st < XML_PASS_OK )
211        {
212                /* Let's not parse anything else if we only have to check
213                   the password. */
214        }
215        else if( g_strcasecmp( g_markup_parse_context_get_element( ctx ), "setting" ) == 0 &&
216                 xd->current_setting && xd->current_account == NULL )
[a312b6b]217        {
[c121f89]218                set_setstr( irc, xd->current_setting, (char*) text );
219                g_free( xd->current_setting );
220                xd->current_setting = NULL;
[a312b6b]221        }
222}
223
224GMarkupParser xml_parser =
225{
226        xml_start_element,
227        xml_end_element,
228        xml_text,
229        NULL,
[5898ef8]230        NULL
[a312b6b]231};
232
233static void xml_init( void )
234{
235        if( access( global.conf->configdir, F_OK ) != 0 )
236                log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG );
237        else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 )
238                log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir );
239}
240
[c121f89]241static storage_status_t xml_load( const char *my_nick, const char *password, irc_t *irc )
[a312b6b]242{
243        GMarkupParseContext *ctx;
[c121f89]244        struct xml_parsedata *xd;
245        char *fn, buf[512];
246        GError *gerr = NULL;
247        int fd, st;
[a312b6b]248       
[5898ef8]249        if( irc->status & USTATUS_IDENTIFIED )
[a312b6b]250                return( 1 );
251       
[c121f89]252        xd = g_new0( struct xml_parsedata, 1 );
253        xd->irc = irc;
254        xd->given_nick = g_strdup( my_nick );
255        xd->given_pass = g_strdup( password );
256        nick_lc( xd->given_nick );
[a312b6b]257       
[c121f89]258        fn = g_strdup_printf( "%s%s%s", global.conf->configdir, xd->given_nick, ".xml" );
259        if( ( fd = open( fn, O_RDONLY ) ) < 0 )
[a312b6b]260        {
[c121f89]261                xml_destroy_xd( xd );
262                g_free( fn );
263                return STORAGE_NO_SUCH_USER;
[a312b6b]264        }
[c121f89]265        g_free( fn );
[a312b6b]266       
[c121f89]267        ctx = g_markup_parse_context_new( &xml_parser, 0, xd, xml_destroy_xd );
[a312b6b]268       
[c121f89]269        while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 )
[a312b6b]270        {
[c121f89]271                if( !g_markup_parse_context_parse( ctx, buf, st, &gerr ) || gerr )
272                {
273                        g_markup_parse_context_free( ctx );
[5898ef8]274                        close( fd );
[c121f89]275                       
[5898ef8]276                        /* Slightly dirty... */
[c121f89]277                        if( gerr && strcmp( gerr->message, XML_PASS_ERRORMSG ) == 0 )
278                                return STORAGE_INVALID_PASSWORD;
279                        else
[5898ef8]280                        {
281                                if( gerr )
282                                        irc_usermsg( irc, "Error from XML-parser: %s", gerr->message );
283                               
[c121f89]284                                return STORAGE_OTHER_ERROR;
[5898ef8]285                        }
[c121f89]286                }
[a312b6b]287        }
288       
[c121f89]289        g_markup_parse_context_free( ctx );
[5898ef8]290        close( fd );
[c121f89]291       
[5898ef8]292        irc->status |= USTATUS_IDENTIFIED;
[a312b6b]293       
294        if( set_getint( irc, "auto_connect" ) )
295        {
[c121f89]296                /* Can't do this directly because r_c_s alters the string */
297                strcpy( buf, "account on" );
298                root_command_string( irc, NULL, buf, 0 );
[a312b6b]299        }
300       
301        return STORAGE_OK;
302}
303
[5898ef8]304static int xml_printf( int fd, char *fmt, ... )
305{
306        va_list params;
307        char *out;
308        int len;
309       
310        va_start( params, fmt );
311        out = g_markup_vprintf_escaped( fmt, params );
312        va_end( params );
313       
314        len = strlen( out );
315        len -= write( fd, out, len );
316        g_free( out );
317       
318        return len == 0;
319}
320
[c121f89]321static storage_status_t xml_save( irc_t *irc, int overwrite )
[a312b6b]322{
[5898ef8]323        char path[512], *path2;
324        set_t *set;
325        nick_t *nick;
326        account_t *acc;
327        int fd;
[a312b6b]328       
[5898ef8]329        g_snprintf( path, sizeof( path ) - 2, "%s%s%s", global.conf->configdir, irc->nick, ".xml" );
330       
331        if( !overwrite && access( path, F_OK ) != -1 )
332                return STORAGE_ALREADY_EXISTS;
333       
334        strcat( path, "~" );
335        if( ( fd = open( path, O_WRONLY | O_CREAT, 0600 ) ) < 0 )
336        {
337                irc_usermsg( irc, "Error while opening configuration file." );
338                return STORAGE_OTHER_ERROR;
[a312b6b]339        }
[5898ef8]340       
341        if( !xml_printf( fd, "<user nick=\"%s\" password=\"%s\">\n", irc->nick, irc->password ) )
342                goto write_error;
343       
344        for( set = irc->set; set; set = set->next )
345                if( set->value && set->def )
346                        if( !xml_printf( fd, "\t<setting name=\"%s\">%s</setting>\n", set->key, set->value ) )
347                                goto write_error;
348       
349        for( acc = irc->accounts; acc; acc = acc->next )
350        {
351                if( !xml_printf( fd, "\t<account protocol=\"%s\" handle=\"%s\" password=\"%s\" autoconnect=\"%s\"", acc->prpl->name, acc->user, acc->pass, "yes" ) )
352                        goto write_error;
353                if( acc->server && acc->server[0] && !xml_printf( fd, " server=\"%s\"", acc->server ) )
354                        goto write_error;
355                if( !xml_printf( fd, ">\n" ) )
356                        goto write_error;
357               
358                for( nick = irc->nicks; nick; nick = nick->next )
359                        if( nick->proto == acc->prpl )
360                                if( !xml_printf( fd, "\t\t<buddy handle=\"%s\" nick=\"%s\" />\n", nick->handle, nick->nick ) )
361                                        goto write_error;
362               
363                if( !xml_printf( fd, "\t</account>\n" ) )
364                        goto write_error;
365        }
366       
367        if( !xml_printf( fd, "</user>\n" ) )
368                goto write_error;
369       
370        close( fd );
371       
372        path2 = g_strndup( path, strlen( path ) - 1 );
373        if( rename( path, path2 ) != 0 )
374        {
375                irc_usermsg( irc, "Error while renaming temporary configuration file." );
376               
377                g_free( path2 );
378                unlink( path );
379               
380                return STORAGE_OTHER_ERROR;
381        }
382       
383        g_free( path2 );
384       
[a312b6b]385        return STORAGE_OK;
[5898ef8]386
387write_error:
388        irc_usermsg( irc, "Write error. Disk full?" );
389        close( fd );
390       
391        return STORAGE_OTHER_ERROR;
[a312b6b]392}
393
394storage_t storage_xml = {
395        .name = "xml",
396        .init = xml_init,
[c121f89]397//      .check_pass = xml_check_pass,
398//      .remove = xml_remove,
[a312b6b]399        .load = xml_load,
400        .save = xml_save
401};
Note: See TracBrowser for help on using the repository browser.