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