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