[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" |
---|
[d28f3b35] | 28 | #include "md5.h" |
---|
[a312b6b] | 29 | |
---|
[c121f89] | 30 | typedef enum |
---|
| 31 | { |
---|
| 32 | XML_PASS_CHECK_ONLY = -1, |
---|
| 33 | XML_PASS_UNKNOWN = 0, |
---|
| 34 | XML_PASS_OK |
---|
| 35 | } xml_pass_st; |
---|
| 36 | |
---|
| 37 | #define XML_PASS_ERRORMSG "Wrong username or password" |
---|
| 38 | |
---|
[a312b6b] | 39 | struct xml_parsedata |
---|
| 40 | { |
---|
| 41 | irc_t *irc; |
---|
| 42 | char *current_setting; |
---|
| 43 | account_t *current_account; |
---|
[c121f89] | 44 | char *given_nick; |
---|
| 45 | char *given_pass; |
---|
| 46 | xml_pass_st pass_st; |
---|
[a312b6b] | 47 | }; |
---|
| 48 | |
---|
| 49 | static char *xml_attr( const gchar **attr_names, const gchar **attr_values, const gchar *key ) |
---|
| 50 | { |
---|
| 51 | int i; |
---|
| 52 | |
---|
| 53 | for( i = 0; attr_names[i]; i ++ ) |
---|
| 54 | if( g_strcasecmp( attr_names[i], key ) == 0 ) |
---|
[c121f89] | 55 | return (char*) attr_values[i]; |
---|
[a312b6b] | 56 | |
---|
| 57 | return NULL; |
---|
| 58 | } |
---|
| 59 | |
---|
[c121f89] | 60 | static void xml_destroy_xd( gpointer data ) |
---|
| 61 | { |
---|
| 62 | struct xml_parsedata *xd = data; |
---|
| 63 | |
---|
| 64 | g_free( xd->given_nick ); |
---|
| 65 | g_free( xd->given_pass ); |
---|
| 66 | g_free( xd ); |
---|
| 67 | } |
---|
| 68 | |
---|
[a312b6b] | 69 | static void xml_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error ) |
---|
| 70 | { |
---|
| 71 | struct xml_parsedata *xd = data; |
---|
[c121f89] | 72 | irc_t *irc = xd->irc; |
---|
[a312b6b] | 73 | |
---|
| 74 | if( g_strcasecmp( element_name, "user" ) == 0 ) |
---|
| 75 | { |
---|
| 76 | char *nick = xml_attr( attr_names, attr_values, "nick" ); |
---|
[c121f89] | 77 | char *pass = xml_attr( attr_names, attr_values, "password" ); |
---|
[a312b6b] | 78 | |
---|
[c121f89] | 79 | if( !nick || !pass ) |
---|
[a312b6b] | 80 | { |
---|
[c121f89] | 81 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
| 82 | "Missing attributes for %s element", element_name ); |
---|
[a312b6b] | 83 | } |
---|
[c121f89] | 84 | else |
---|
| 85 | { |
---|
[d28f3b35] | 86 | md5_byte_t pass_md5[16]; |
---|
| 87 | md5_state_t md5_state; |
---|
[d028a77] | 88 | int i, j; |
---|
[d28f3b35] | 89 | |
---|
| 90 | md5_init( &md5_state ); |
---|
[d028a77] | 91 | md5_append( &md5_state, (md5_byte_t*) xd->given_pass, strlen( xd->given_pass ) ); |
---|
[d28f3b35] | 92 | md5_finish( &md5_state, pass_md5 ); |
---|
| 93 | |
---|
[d028a77] | 94 | for( i = 0; i < 16; i ++ ) |
---|
[d28f3b35] | 95 | { |
---|
[d028a77] | 96 | if( !isxdigit( pass[i*2] ) || !isxdigit( pass[i*2+1] ) || |
---|
| 97 | sscanf( pass + i * 2, "%2x", &j ) != 1 ) |
---|
| 98 | { |
---|
| 99 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
| 100 | "Incorrect password MD5-hash" ); |
---|
| 101 | break; |
---|
| 102 | } |
---|
[d28f3b35] | 103 | if( j != pass_md5[i] ) |
---|
[d028a77] | 104 | { |
---|
| 105 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
| 106 | XML_PASS_ERRORMSG ); |
---|
[d28f3b35] | 107 | break; |
---|
[d028a77] | 108 | } |
---|
[d28f3b35] | 109 | } |
---|
| 110 | |
---|
[d028a77] | 111 | /* If we reached the end of the loop, it was a match! */ |
---|
| 112 | if( i == 16 ) |
---|
[d28f3b35] | 113 | { |
---|
| 114 | if( xd->pass_st != XML_PASS_CHECK_ONLY ) |
---|
| 115 | xd->pass_st = XML_PASS_OK; |
---|
| 116 | } |
---|
[c121f89] | 117 | } |
---|
| 118 | } |
---|
| 119 | else if( xd->pass_st < XML_PASS_OK ) |
---|
| 120 | { |
---|
| 121 | /* Let's not parse anything else if we only have to check |
---|
| 122 | the password. */ |
---|
[a312b6b] | 123 | } |
---|
| 124 | else if( g_strcasecmp( element_name, "account" ) == 0 ) |
---|
| 125 | { |
---|
[c121f89] | 126 | char *protocol, *handle, *server, *password; |
---|
[a312b6b] | 127 | struct prpl *prpl = NULL; |
---|
| 128 | |
---|
| 129 | handle = xml_attr( attr_names, attr_values, "handle" ); |
---|
| 130 | password = xml_attr( attr_names, attr_values, "password" ); |
---|
[c121f89] | 131 | server = xml_attr( attr_names, attr_values, "server" ); |
---|
[a312b6b] | 132 | |
---|
| 133 | protocol = xml_attr( attr_names, attr_values, "protocol" ); |
---|
| 134 | if( protocol ) |
---|
| 135 | prpl = find_protocol( protocol ); |
---|
| 136 | |
---|
[c121f89] | 137 | if( !handle || !password ) |
---|
| 138 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
| 139 | "Missing attributes for %s element", element_name ); |
---|
| 140 | else if( !prpl ) |
---|
| 141 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
| 142 | "Missing or unknown protocol %s element", element_name ); |
---|
| 143 | else |
---|
[a312b6b] | 144 | { |
---|
[c121f89] | 145 | xd->current_account = account_add( irc, prpl, handle, password ); |
---|
| 146 | if( server ) |
---|
| 147 | xd->current_account->server = g_strdup( server ); |
---|
[a312b6b] | 148 | } |
---|
| 149 | } |
---|
| 150 | else if( g_strcasecmp( element_name, "setting" ) == 0 ) |
---|
| 151 | { |
---|
| 152 | if( xd->current_account == NULL ) |
---|
| 153 | { |
---|
[c121f89] | 154 | char *setting; |
---|
| 155 | |
---|
| 156 | if( xd->current_setting ) |
---|
| 157 | { |
---|
| 158 | g_free( xd->current_setting ); |
---|
| 159 | xd->current_setting = NULL; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | if( ( setting = xml_attr( attr_names, attr_values, "name" ) ) ) |
---|
| 163 | xd->current_setting = g_strdup( setting ); |
---|
| 164 | else |
---|
| 165 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
| 166 | "Missing attributes for %s element", element_name ); |
---|
[a312b6b] | 167 | } |
---|
| 168 | } |
---|
| 169 | else if( g_strcasecmp( element_name, "buddy" ) == 0 ) |
---|
| 170 | { |
---|
[c121f89] | 171 | char *handle, *nick; |
---|
| 172 | |
---|
| 173 | handle = xml_attr( attr_names, attr_values, "handle" ); |
---|
| 174 | nick = xml_attr( attr_names, attr_values, "nick" ); |
---|
| 175 | |
---|
| 176 | if( xd->current_account && handle && nick ) |
---|
| 177 | { |
---|
| 178 | nick_set( irc, handle, xd->current_account->prpl, nick ); |
---|
| 179 | } |
---|
| 180 | else |
---|
| 181 | { |
---|
| 182 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
| 183 | "Missing attributes for %s element", element_name ); |
---|
| 184 | } |
---|
[a312b6b] | 185 | } |
---|
| 186 | else |
---|
| 187 | { |
---|
[c121f89] | 188 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT, |
---|
| 189 | "Unkown element: %s", element_name ); |
---|
[a312b6b] | 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | static void xml_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error ) |
---|
| 194 | { |
---|
[5898ef8] | 195 | struct xml_parsedata *xd = data; |
---|
| 196 | // irc_t *irc = xd->irc; |
---|
| 197 | |
---|
| 198 | if( g_strcasecmp( element_name, "setting" ) == 0 && xd->current_setting ) |
---|
| 199 | { |
---|
| 200 | g_free( xd->current_setting ); |
---|
| 201 | xd->current_setting = NULL; |
---|
| 202 | } |
---|
| 203 | else if( g_strcasecmp( element_name, "account" ) == 0 ) |
---|
| 204 | { |
---|
| 205 | xd->current_account = NULL; |
---|
| 206 | } |
---|
[a312b6b] | 207 | } |
---|
| 208 | |
---|
| 209 | static void xml_text( GMarkupParseContext *ctx, const gchar *text, gsize text_len, gpointer data, GError **error ) |
---|
| 210 | { |
---|
| 211 | struct xml_parsedata *xd = data; |
---|
[c121f89] | 212 | irc_t *irc = xd->irc; |
---|
[a312b6b] | 213 | |
---|
[c121f89] | 214 | if( xd->pass_st < XML_PASS_OK ) |
---|
| 215 | { |
---|
| 216 | /* Let's not parse anything else if we only have to check |
---|
| 217 | the password. */ |
---|
| 218 | } |
---|
| 219 | else if( g_strcasecmp( g_markup_parse_context_get_element( ctx ), "setting" ) == 0 && |
---|
| 220 | xd->current_setting && xd->current_account == NULL ) |
---|
[a312b6b] | 221 | { |
---|
[c121f89] | 222 | set_setstr( irc, xd->current_setting, (char*) text ); |
---|
| 223 | g_free( xd->current_setting ); |
---|
| 224 | xd->current_setting = NULL; |
---|
[a312b6b] | 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | GMarkupParser xml_parser = |
---|
| 229 | { |
---|
| 230 | xml_start_element, |
---|
| 231 | xml_end_element, |
---|
| 232 | xml_text, |
---|
| 233 | NULL, |
---|
[5898ef8] | 234 | NULL |
---|
[a312b6b] | 235 | }; |
---|
| 236 | |
---|
| 237 | static void xml_init( void ) |
---|
| 238 | { |
---|
| 239 | if( access( global.conf->configdir, F_OK ) != 0 ) |
---|
| 240 | log_message( LOGLVL_WARNING, "The configuration directory %s does not exist. Configuration won't be saved.", CONFIG ); |
---|
| 241 | else if( access( global.conf->configdir, R_OK ) != 0 || access( global.conf->configdir, W_OK ) != 0 ) |
---|
| 242 | log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to %s.", global.conf->configdir ); |
---|
| 243 | } |
---|
| 244 | |
---|
[c121f89] | 245 | static storage_status_t xml_load( const char *my_nick, const char *password, irc_t *irc ) |
---|
[a312b6b] | 246 | { |
---|
| 247 | GMarkupParseContext *ctx; |
---|
[c121f89] | 248 | struct xml_parsedata *xd; |
---|
| 249 | char *fn, buf[512]; |
---|
| 250 | GError *gerr = NULL; |
---|
| 251 | int fd, st; |
---|
[a312b6b] | 252 | |
---|
[5898ef8] | 253 | if( irc->status & USTATUS_IDENTIFIED ) |
---|
[a312b6b] | 254 | return( 1 ); |
---|
| 255 | |
---|
[c121f89] | 256 | xd = g_new0( struct xml_parsedata, 1 ); |
---|
| 257 | xd->irc = irc; |
---|
| 258 | xd->given_nick = g_strdup( my_nick ); |
---|
| 259 | xd->given_pass = g_strdup( password ); |
---|
| 260 | nick_lc( xd->given_nick ); |
---|
[a312b6b] | 261 | |
---|
[c121f89] | 262 | fn = g_strdup_printf( "%s%s%s", global.conf->configdir, xd->given_nick, ".xml" ); |
---|
| 263 | if( ( fd = open( fn, O_RDONLY ) ) < 0 ) |
---|
[a312b6b] | 264 | { |
---|
[c121f89] | 265 | xml_destroy_xd( xd ); |
---|
| 266 | g_free( fn ); |
---|
| 267 | return STORAGE_NO_SUCH_USER; |
---|
[a312b6b] | 268 | } |
---|
[c121f89] | 269 | g_free( fn ); |
---|
[a312b6b] | 270 | |
---|
[c121f89] | 271 | ctx = g_markup_parse_context_new( &xml_parser, 0, xd, xml_destroy_xd ); |
---|
[a312b6b] | 272 | |
---|
[c121f89] | 273 | while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 ) |
---|
[a312b6b] | 274 | { |
---|
[c121f89] | 275 | if( !g_markup_parse_context_parse( ctx, buf, st, &gerr ) || gerr ) |
---|
| 276 | { |
---|
| 277 | g_markup_parse_context_free( ctx ); |
---|
[5898ef8] | 278 | close( fd ); |
---|
[c121f89] | 279 | |
---|
[5898ef8] | 280 | /* Slightly dirty... */ |
---|
[c121f89] | 281 | if( gerr && strcmp( gerr->message, XML_PASS_ERRORMSG ) == 0 ) |
---|
| 282 | return STORAGE_INVALID_PASSWORD; |
---|
| 283 | else |
---|
[5898ef8] | 284 | { |
---|
| 285 | if( gerr ) |
---|
| 286 | irc_usermsg( irc, "Error from XML-parser: %s", gerr->message ); |
---|
| 287 | |
---|
[c121f89] | 288 | return STORAGE_OTHER_ERROR; |
---|
[5898ef8] | 289 | } |
---|
[c121f89] | 290 | } |
---|
[a312b6b] | 291 | } |
---|
| 292 | |
---|
[c121f89] | 293 | g_markup_parse_context_free( ctx ); |
---|
[5898ef8] | 294 | close( fd ); |
---|
[c121f89] | 295 | |
---|
[5898ef8] | 296 | irc->status |= USTATUS_IDENTIFIED; |
---|
[a312b6b] | 297 | |
---|
| 298 | if( set_getint( irc, "auto_connect" ) ) |
---|
| 299 | { |
---|
[c121f89] | 300 | /* Can't do this directly because r_c_s alters the string */ |
---|
| 301 | strcpy( buf, "account on" ); |
---|
| 302 | root_command_string( irc, NULL, buf, 0 ); |
---|
[a312b6b] | 303 | } |
---|
| 304 | |
---|
| 305 | return STORAGE_OK; |
---|
| 306 | } |
---|
| 307 | |
---|
[5898ef8] | 308 | static int xml_printf( int fd, char *fmt, ... ) |
---|
| 309 | { |
---|
| 310 | va_list params; |
---|
| 311 | char *out; |
---|
| 312 | int len; |
---|
| 313 | |
---|
| 314 | va_start( params, fmt ); |
---|
| 315 | out = g_markup_vprintf_escaped( fmt, params ); |
---|
| 316 | va_end( params ); |
---|
| 317 | |
---|
| 318 | len = strlen( out ); |
---|
| 319 | len -= write( fd, out, len ); |
---|
| 320 | g_free( out ); |
---|
| 321 | |
---|
| 322 | return len == 0; |
---|
| 323 | } |
---|
| 324 | |
---|
[c121f89] | 325 | static storage_status_t xml_save( irc_t *irc, int overwrite ) |
---|
[a312b6b] | 326 | { |
---|
[5898ef8] | 327 | char path[512], *path2; |
---|
| 328 | set_t *set; |
---|
| 329 | nick_t *nick; |
---|
| 330 | account_t *acc; |
---|
| 331 | int fd; |
---|
[a312b6b] | 332 | |
---|
[5898ef8] | 333 | g_snprintf( path, sizeof( path ) - 2, "%s%s%s", global.conf->configdir, irc->nick, ".xml" ); |
---|
| 334 | |
---|
| 335 | if( !overwrite && access( path, F_OK ) != -1 ) |
---|
| 336 | return STORAGE_ALREADY_EXISTS; |
---|
| 337 | |
---|
| 338 | strcat( path, "~" ); |
---|
| 339 | if( ( fd = open( path, O_WRONLY | O_CREAT, 0600 ) ) < 0 ) |
---|
| 340 | { |
---|
| 341 | irc_usermsg( irc, "Error while opening configuration file." ); |
---|
| 342 | return STORAGE_OTHER_ERROR; |
---|
[a312b6b] | 343 | } |
---|
[5898ef8] | 344 | |
---|
| 345 | if( !xml_printf( fd, "<user nick=\"%s\" password=\"%s\">\n", irc->nick, irc->password ) ) |
---|
| 346 | goto write_error; |
---|
| 347 | |
---|
| 348 | for( set = irc->set; set; set = set->next ) |
---|
| 349 | if( set->value && set->def ) |
---|
| 350 | if( !xml_printf( fd, "\t<setting name=\"%s\">%s</setting>\n", set->key, set->value ) ) |
---|
| 351 | goto write_error; |
---|
| 352 | |
---|
| 353 | for( acc = irc->accounts; acc; acc = acc->next ) |
---|
| 354 | { |
---|
| 355 | if( !xml_printf( fd, "\t<account protocol=\"%s\" handle=\"%s\" password=\"%s\" autoconnect=\"%s\"", acc->prpl->name, acc->user, acc->pass, "yes" ) ) |
---|
| 356 | goto write_error; |
---|
| 357 | if( acc->server && acc->server[0] && !xml_printf( fd, " server=\"%s\"", acc->server ) ) |
---|
| 358 | goto write_error; |
---|
| 359 | if( !xml_printf( fd, ">\n" ) ) |
---|
| 360 | goto write_error; |
---|
| 361 | |
---|
| 362 | for( nick = irc->nicks; nick; nick = nick->next ) |
---|
| 363 | if( nick->proto == acc->prpl ) |
---|
| 364 | if( !xml_printf( fd, "\t\t<buddy handle=\"%s\" nick=\"%s\" />\n", nick->handle, nick->nick ) ) |
---|
| 365 | goto write_error; |
---|
| 366 | |
---|
| 367 | if( !xml_printf( fd, "\t</account>\n" ) ) |
---|
| 368 | goto write_error; |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | if( !xml_printf( fd, "</user>\n" ) ) |
---|
| 372 | goto write_error; |
---|
| 373 | |
---|
| 374 | close( fd ); |
---|
| 375 | |
---|
| 376 | path2 = g_strndup( path, strlen( path ) - 1 ); |
---|
| 377 | if( rename( path, path2 ) != 0 ) |
---|
| 378 | { |
---|
| 379 | irc_usermsg( irc, "Error while renaming temporary configuration file." ); |
---|
| 380 | |
---|
| 381 | g_free( path2 ); |
---|
| 382 | unlink( path ); |
---|
| 383 | |
---|
| 384 | return STORAGE_OTHER_ERROR; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | g_free( path2 ); |
---|
| 388 | |
---|
[a312b6b] | 389 | return STORAGE_OK; |
---|
[5898ef8] | 390 | |
---|
| 391 | write_error: |
---|
| 392 | irc_usermsg( irc, "Write error. Disk full?" ); |
---|
| 393 | close( fd ); |
---|
| 394 | |
---|
| 395 | return STORAGE_OTHER_ERROR; |
---|
[a312b6b] | 396 | } |
---|
| 397 | |
---|
| 398 | storage_t storage_xml = { |
---|
| 399 | .name = "xml", |
---|
| 400 | .init = xml_init, |
---|
[c121f89] | 401 | // .check_pass = xml_check_pass, |
---|
| 402 | // .remove = xml_remove, |
---|
[a312b6b] | 403 | .load = xml_load, |
---|
| 404 | .save = xml_save |
---|
| 405 | }; |
---|