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