1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2012 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., 51 Franklin St., |
---|
23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #define BITLBEE_CORE |
---|
27 | #include "bitlbee.h" |
---|
28 | #include "base64.h" |
---|
29 | #include "arc.h" |
---|
30 | #include "md5.h" |
---|
31 | #include "xmltree.h" |
---|
32 | |
---|
33 | #include <glib/gstdio.h> |
---|
34 | |
---|
35 | typedef enum |
---|
36 | { |
---|
37 | XML_PASS_CHECK_ONLY = -1, |
---|
38 | XML_PASS_UNKNOWN = 0, |
---|
39 | XML_PASS_WRONG, |
---|
40 | XML_PASS_OK |
---|
41 | } xml_pass_st; |
---|
42 | |
---|
43 | /* To make it easier later when extending the format: */ |
---|
44 | #define XML_FORMAT_VERSION "1" |
---|
45 | |
---|
46 | struct xml_parsedata |
---|
47 | { |
---|
48 | irc_t *irc; |
---|
49 | char given_nick[MAX_NICK_LENGTH+1]; |
---|
50 | char *given_pass; |
---|
51 | }; |
---|
52 | |
---|
53 | static void xml_init( void ) |
---|
54 | { |
---|
55 | if( g_access( global.conf->configdir, F_OK ) != 0 ) |
---|
56 | log_message( LOGLVL_WARNING, "The configuration directory `%s' does not exist. Configuration won't be saved.", global.conf->configdir ); |
---|
57 | else if( g_access( global.conf->configdir, F_OK ) != 0 || |
---|
58 | g_access( global.conf->configdir, W_OK ) != 0 ) |
---|
59 | log_message( LOGLVL_WARNING, "Permission problem: Can't read/write from/to `%s'.", global.conf->configdir ); |
---|
60 | } |
---|
61 | |
---|
62 | static void handle_settings( struct xt_node *node, set_t **head ) |
---|
63 | { |
---|
64 | struct xt_node *c; |
---|
65 | |
---|
66 | for( c = node->children; ( c = xt_find_node( c, "setting" ) ); c = c->next ) |
---|
67 | { |
---|
68 | char *name = xt_find_attr( c, "name" ); |
---|
69 | |
---|
70 | if( !name ) |
---|
71 | continue; |
---|
72 | |
---|
73 | if( strcmp( node->name, "account" ) == 0 ) |
---|
74 | { |
---|
75 | set_t *s = set_find( head, name ); |
---|
76 | if( s && ( s->flags & ACC_SET_ONLINE_ONLY ) ) |
---|
77 | continue; /* U can't touch this! */ |
---|
78 | } |
---|
79 | set_setstr( head, name, c->text ); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | static xt_status handle_account( struct xt_node *node, gpointer data ) |
---|
84 | { |
---|
85 | struct xml_parsedata *xd = data; |
---|
86 | char *protocol, *handle, *server, *password = NULL, *autoconnect, *tag; |
---|
87 | char *pass_b64 = NULL; |
---|
88 | unsigned char *pass_cr = NULL; |
---|
89 | int pass_len, local = 0; |
---|
90 | struct prpl *prpl = NULL; |
---|
91 | account_t *acc; |
---|
92 | struct xt_node *c; |
---|
93 | |
---|
94 | handle = xt_find_attr( node, "handle" ); |
---|
95 | pass_b64 = xt_find_attr( node, "password" ); |
---|
96 | server = xt_find_attr( node, "server" ); |
---|
97 | autoconnect = xt_find_attr( node, "autoconnect" ); |
---|
98 | tag = xt_find_attr( node, "tag" ); |
---|
99 | |
---|
100 | protocol = xt_find_attr( node, "protocol" ); |
---|
101 | if( protocol ) |
---|
102 | { |
---|
103 | prpl = find_protocol( protocol ); |
---|
104 | local = protocol_account_islocal( protocol ); |
---|
105 | } |
---|
106 | |
---|
107 | if( !handle || !pass_b64 || !protocol || !prpl ) |
---|
108 | return XT_ABORT; |
---|
109 | else if( ( pass_len = base64_decode( pass_b64, (unsigned char**) &pass_cr ) ) && |
---|
110 | arc_decode( pass_cr, pass_len, &password, xd->given_pass ) >= 0 ) |
---|
111 | { |
---|
112 | acc = account_add( xd->irc->b, prpl, handle, password ); |
---|
113 | if( server ) |
---|
114 | set_setstr( &acc->set, "server", server ); |
---|
115 | if( autoconnect ) |
---|
116 | set_setstr( &acc->set, "auto_connect", autoconnect ); |
---|
117 | if( tag ) |
---|
118 | set_setstr( &acc->set, "tag", tag ); |
---|
119 | if( local ) |
---|
120 | acc->flags |= ACC_FLAG_LOCAL; |
---|
121 | } |
---|
122 | else |
---|
123 | return XT_ABORT; |
---|
124 | |
---|
125 | g_free( pass_cr ); |
---|
126 | g_free( password ); |
---|
127 | |
---|
128 | handle_settings( node, &acc->set ); |
---|
129 | |
---|
130 | for( c = node->children; ( c = xt_find_node( c, "buddy" ) ); c = c->next ) |
---|
131 | { |
---|
132 | char *handle, *nick; |
---|
133 | |
---|
134 | handle = xt_find_attr( c, "handle" ); |
---|
135 | nick = xt_find_attr( c, "nick" ); |
---|
136 | |
---|
137 | if( handle && nick ) |
---|
138 | nick_set_raw( acc, handle, nick ); |
---|
139 | else |
---|
140 | return XT_ABORT; |
---|
141 | } |
---|
142 | return XT_HANDLED; |
---|
143 | } |
---|
144 | |
---|
145 | static xt_status handle_channel( struct xt_node *node, gpointer data ) |
---|
146 | { |
---|
147 | struct xml_parsedata *xd = data; |
---|
148 | irc_channel_t *ic; |
---|
149 | char *name, *type; |
---|
150 | |
---|
151 | name = xt_find_attr( node, "name" ); |
---|
152 | type = xt_find_attr( node, "type" ); |
---|
153 | |
---|
154 | if( !name || !type ) |
---|
155 | return XT_ABORT; |
---|
156 | |
---|
157 | /* The channel may exist already, for example if it's &bitlbee. |
---|
158 | Also, it's possible that the user just reconnected and the |
---|
159 | IRC client already rejoined all channels it was in. They |
---|
160 | should still get the right settings. */ |
---|
161 | if( ( ic = irc_channel_by_name( xd->irc, name ) ) || |
---|
162 | ( ic = irc_channel_new( xd->irc, name ) ) ) |
---|
163 | set_setstr( &ic->set, "type", type ); |
---|
164 | |
---|
165 | handle_settings( node, &ic->set ); |
---|
166 | |
---|
167 | return XT_HANDLED; |
---|
168 | } |
---|
169 | |
---|
170 | static const struct xt_handler_entry handlers[] = { |
---|
171 | { "account", "user", handle_account, }, |
---|
172 | { "channel", "user", handle_channel, }, |
---|
173 | { NULL, NULL, NULL, }, |
---|
174 | }; |
---|
175 | |
---|
176 | static storage_status_t xml_load_real( irc_t *irc, const char *my_nick, const char *password, xml_pass_st action ) |
---|
177 | { |
---|
178 | struct xml_parsedata xd[1]; |
---|
179 | char *fn, buf[2048]; |
---|
180 | int fd, st; |
---|
181 | struct xt_parser *xp = NULL; |
---|
182 | struct xt_node *node; |
---|
183 | storage_status_t ret = STORAGE_OTHER_ERROR; |
---|
184 | |
---|
185 | xd->irc = irc; |
---|
186 | strncpy( xd->given_nick, my_nick, MAX_NICK_LENGTH ); |
---|
187 | xd->given_nick[MAX_NICK_LENGTH] = '\0'; |
---|
188 | nick_lc( NULL, xd->given_nick ); |
---|
189 | xd->given_pass = (char*) password; |
---|
190 | |
---|
191 | fn = g_strconcat( global.conf->configdir, xd->given_nick, ".xml", NULL ); |
---|
192 | if( ( fd = open( fn, O_RDONLY ) ) < 0 ) |
---|
193 | { |
---|
194 | ret = STORAGE_NO_SUCH_USER; |
---|
195 | goto error; |
---|
196 | } |
---|
197 | |
---|
198 | xp = xt_new( handlers, xd ); |
---|
199 | while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 ) |
---|
200 | { |
---|
201 | st = xt_feed( xp, buf, st ); |
---|
202 | if( st != 1 ) |
---|
203 | break; |
---|
204 | } |
---|
205 | close( fd ); |
---|
206 | if( st != 0 ) |
---|
207 | goto error; |
---|
208 | |
---|
209 | node = xp->root; |
---|
210 | if( node == NULL || node->next != NULL || strcmp( node->name, "user" ) != 0 ) |
---|
211 | goto error; |
---|
212 | |
---|
213 | { |
---|
214 | char *nick = xt_find_attr( node, "nick" ); |
---|
215 | char *pass = xt_find_attr( node, "password" ); |
---|
216 | |
---|
217 | if( !nick || !pass ) |
---|
218 | { |
---|
219 | goto error; |
---|
220 | } |
---|
221 | else if( ( st = md5_verify_password( xd->given_pass, pass ) ) != 0 ) |
---|
222 | { |
---|
223 | ret = STORAGE_INVALID_PASSWORD; |
---|
224 | goto error; |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | if( action == XML_PASS_CHECK_ONLY ) |
---|
229 | { |
---|
230 | ret = STORAGE_OK; |
---|
231 | goto error; |
---|
232 | } |
---|
233 | |
---|
234 | /* DO NOT call xt_handle() before verifying the password! */ |
---|
235 | if( xt_handle( xp, NULL, 1 ) == XT_HANDLED ) |
---|
236 | ret = STORAGE_OK; |
---|
237 | |
---|
238 | handle_settings( node, &xd->irc->b->set ); |
---|
239 | |
---|
240 | error: |
---|
241 | xt_free( xp ); |
---|
242 | g_free( fn ); |
---|
243 | return ret; |
---|
244 | } |
---|
245 | |
---|
246 | static storage_status_t xml_load( irc_t *irc, const char *password ) |
---|
247 | { |
---|
248 | return xml_load_real( irc, irc->user->nick, password, XML_PASS_UNKNOWN ); |
---|
249 | } |
---|
250 | |
---|
251 | static storage_status_t xml_check_pass( const char *my_nick, const char *password ) |
---|
252 | { |
---|
253 | return xml_load_real( NULL, my_nick, password, XML_PASS_CHECK_ONLY ); |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | static gboolean xml_generate_nick( gpointer key, gpointer value, gpointer data ); |
---|
258 | static void xml_generate_settings( struct xt_node *cur, set_t **head ); |
---|
259 | |
---|
260 | struct xt_node *xml_generate( irc_t *irc ) |
---|
261 | { |
---|
262 | char *pass_buf = NULL; |
---|
263 | account_t *acc; |
---|
264 | md5_byte_t pass_md5[21]; |
---|
265 | md5_state_t md5_state; |
---|
266 | GSList *l; |
---|
267 | struct xt_node *root, *cur; |
---|
268 | |
---|
269 | /* Generate a salted md5sum of the password. Use 5 bytes for the salt |
---|
270 | (to prevent dictionary lookups of passwords) to end up with a 21- |
---|
271 | byte password hash, more convenient for base64 encoding. */ |
---|
272 | random_bytes( pass_md5 + 16, 5 ); |
---|
273 | md5_init( &md5_state ); |
---|
274 | md5_append( &md5_state, (md5_byte_t*) irc->password, strlen( irc->password ) ); |
---|
275 | md5_append( &md5_state, pass_md5 + 16, 5 ); /* Add the salt. */ |
---|
276 | md5_finish( &md5_state, pass_md5 ); |
---|
277 | /* Save the hash in base64-encoded form. */ |
---|
278 | pass_buf = base64_encode( pass_md5, 21 ); |
---|
279 | |
---|
280 | root = cur = xt_new_node( "user", NULL, NULL ); |
---|
281 | xt_add_attr( cur, "nick", irc->user->nick ); |
---|
282 | xt_add_attr( cur, "password", pass_buf ); |
---|
283 | xt_add_attr( cur, "version", XML_FORMAT_VERSION ); |
---|
284 | |
---|
285 | g_free( pass_buf ); |
---|
286 | |
---|
287 | xml_generate_settings( cur, &irc->b->set ); |
---|
288 | |
---|
289 | for( acc = irc->b->accounts; acc; acc = acc->next ) |
---|
290 | { |
---|
291 | unsigned char *pass_cr; |
---|
292 | char *pass_b64; |
---|
293 | int pass_len; |
---|
294 | |
---|
295 | pass_len = arc_encode( acc->pass, strlen( acc->pass ), (unsigned char**) &pass_cr, irc->password, 12 ); |
---|
296 | pass_b64 = base64_encode( pass_cr, pass_len ); |
---|
297 | g_free( pass_cr ); |
---|
298 | |
---|
299 | cur = xt_new_node( "account", NULL, NULL ); |
---|
300 | xt_add_attr( cur, "protocol", acc->prpl->name ); |
---|
301 | xt_add_attr( cur, "handle", acc->user ); |
---|
302 | xt_add_attr( cur, "password", pass_b64 ); |
---|
303 | xt_add_attr( cur, "autoconnect", acc->auto_connect ? "true" : "false" ); |
---|
304 | xt_add_attr( cur, "tag", acc->tag ); |
---|
305 | if( acc->server && acc->server[0] ) |
---|
306 | xt_add_attr( cur, "server", acc->server ); |
---|
307 | |
---|
308 | g_free( pass_b64 ); |
---|
309 | |
---|
310 | /* This probably looks pretty strange. g_hash_table_foreach |
---|
311 | is quite a PITA already (but it can't get much better in |
---|
312 | C without using #define, I'm afraid), and it |
---|
313 | doesn't seem to be possible to abort the foreach on write |
---|
314 | errors, so instead let's use the _find function and |
---|
315 | return TRUE on write errors. Which means, if we found |
---|
316 | something, there was an error. :-) */ |
---|
317 | g_hash_table_find( acc->nicks, xml_generate_nick, cur ); |
---|
318 | |
---|
319 | xml_generate_settings( cur, &acc->set ); |
---|
320 | |
---|
321 | xt_add_child( root, cur ); |
---|
322 | } |
---|
323 | |
---|
324 | for( l = irc->channels; l; l = l->next ) |
---|
325 | { |
---|
326 | irc_channel_t *ic = l->data; |
---|
327 | |
---|
328 | if( ic->flags & IRC_CHANNEL_TEMP ) |
---|
329 | continue; |
---|
330 | |
---|
331 | cur = xt_new_node( "channel", NULL, NULL ); |
---|
332 | xt_add_attr( cur, "name", ic->name ); |
---|
333 | xt_add_attr( cur, "type", set_getstr( &ic->set, "type" ) ); |
---|
334 | |
---|
335 | xml_generate_settings( cur, &ic->set ); |
---|
336 | |
---|
337 | xt_add_child( root, cur ); |
---|
338 | } |
---|
339 | |
---|
340 | return root; |
---|
341 | } |
---|
342 | |
---|
343 | static gboolean xml_generate_nick( gpointer key, gpointer value, gpointer data ) |
---|
344 | { |
---|
345 | struct xt_node *node = xt_new_node( "buddy", NULL, NULL ); |
---|
346 | xt_add_attr( node, "handle", key ); |
---|
347 | xt_add_attr( node, "nick", value ); |
---|
348 | xt_add_child( (struct xt_node *) data, node ); |
---|
349 | |
---|
350 | return FALSE; |
---|
351 | } |
---|
352 | |
---|
353 | static void xml_generate_settings( struct xt_node *cur, set_t **head ) |
---|
354 | { |
---|
355 | set_t *set; |
---|
356 | |
---|
357 | for( set = *head; set; set = set->next ) |
---|
358 | if( set->value && !( set->flags & SET_NOSAVE ) ) |
---|
359 | { |
---|
360 | struct xt_node *xset; |
---|
361 | xt_add_child( cur, xset = xt_new_node( "setting", set->value, NULL ) ); |
---|
362 | xt_add_attr( xset, "name", set->key ); |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | static storage_status_t xml_save( irc_t *irc, int overwrite ) |
---|
367 | { |
---|
368 | storage_status_t ret = STORAGE_OK; |
---|
369 | char path[512], *path2 = NULL, *xml = NULL; |
---|
370 | struct xt_node *tree = NULL; |
---|
371 | size_t len; |
---|
372 | int fd; |
---|
373 | |
---|
374 | path2 = g_strdup( irc->user->nick ); |
---|
375 | nick_lc( NULL, path2 ); |
---|
376 | g_snprintf( path, sizeof( path ) - 20, "%s%s%s", global.conf->configdir, path2, ".xml" ); |
---|
377 | g_free( path2 ); |
---|
378 | |
---|
379 | if( !overwrite && g_access( path, F_OK ) == 0 ) |
---|
380 | return STORAGE_ALREADY_EXISTS; |
---|
381 | |
---|
382 | strcat( path, ".XXXXXX" ); |
---|
383 | if( ( fd = mkstemp( path ) ) < 0 ) |
---|
384 | { |
---|
385 | irc_rootmsg( irc, "Error while opening configuration file." ); |
---|
386 | return STORAGE_OTHER_ERROR; |
---|
387 | } |
---|
388 | |
---|
389 | tree = xml_generate( irc ); |
---|
390 | xml = xt_to_string_i( tree ); |
---|
391 | len = strlen( xml ); |
---|
392 | if( write( fd, xml, len ) != len || |
---|
393 | fsync( fd ) != 0 || /* #559 */ |
---|
394 | close( fd ) != 0 ) |
---|
395 | goto error; |
---|
396 | |
---|
397 | path2 = g_strndup( path, strlen( path ) - 7 ); |
---|
398 | if( rename( path, path2 ) != 0 ) |
---|
399 | { |
---|
400 | g_free( path2 ); |
---|
401 | goto error; |
---|
402 | } |
---|
403 | g_free( path2 ); |
---|
404 | |
---|
405 | goto finish; |
---|
406 | |
---|
407 | error: |
---|
408 | irc_rootmsg( irc, "Write error. Disk full?" ); |
---|
409 | ret = STORAGE_OTHER_ERROR; |
---|
410 | |
---|
411 | finish: |
---|
412 | close( fd ); |
---|
413 | unlink( path ); |
---|
414 | g_free( xml ); |
---|
415 | xt_free_node( tree ); |
---|
416 | |
---|
417 | return ret; |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | static storage_status_t xml_remove( const char *nick, const char *password ) |
---|
422 | { |
---|
423 | char s[512], *lc; |
---|
424 | storage_status_t status; |
---|
425 | |
---|
426 | status = xml_check_pass( nick, password ); |
---|
427 | if( status != STORAGE_OK ) |
---|
428 | return status; |
---|
429 | |
---|
430 | lc = g_strdup( nick ); |
---|
431 | nick_lc( NULL, lc ); |
---|
432 | g_snprintf( s, 511, "%s%s%s", global.conf->configdir, lc, ".xml" ); |
---|
433 | g_free( lc ); |
---|
434 | |
---|
435 | if( unlink( s ) == -1 ) |
---|
436 | return STORAGE_OTHER_ERROR; |
---|
437 | |
---|
438 | return STORAGE_OK; |
---|
439 | } |
---|
440 | |
---|
441 | storage_t storage_xml = { |
---|
442 | .name = "xml", |
---|
443 | .init = xml_init, |
---|
444 | .check_pass = xml_check_pass, |
---|
445 | .remove = xml_remove, |
---|
446 | .load = xml_load, |
---|
447 | .save = xml_save |
---|
448 | }; |
---|