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