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