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