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