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