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