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 "md5.h" |
---|
29 | |
---|
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 | |
---|
39 | struct xml_parsedata |
---|
40 | { |
---|
41 | irc_t *irc; |
---|
42 | char *current_setting; |
---|
43 | account_t *current_account; |
---|
44 | char *given_nick; |
---|
45 | char *given_pass; |
---|
46 | xml_pass_st pass_st; |
---|
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 ) |
---|
55 | return (char*) attr_values[i]; |
---|
56 | |
---|
57 | return NULL; |
---|
58 | } |
---|
59 | |
---|
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 | |
---|
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; |
---|
72 | irc_t *irc = xd->irc; |
---|
73 | |
---|
74 | if( g_strcasecmp( element_name, "user" ) == 0 ) |
---|
75 | { |
---|
76 | char *nick = xml_attr( attr_names, attr_values, "nick" ); |
---|
77 | char *pass = xml_attr( attr_names, attr_values, "password" ); |
---|
78 | |
---|
79 | if( !nick || !pass ) |
---|
80 | { |
---|
81 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
82 | "Missing attributes for %s element", element_name ); |
---|
83 | } |
---|
84 | else |
---|
85 | { |
---|
86 | md5_byte_t pass_md5[16]; |
---|
87 | md5_state_t md5_state; |
---|
88 | int i, j; |
---|
89 | |
---|
90 | md5_init( &md5_state ); |
---|
91 | md5_append( &md5_state, (md5_byte_t*) xd->given_pass, strlen( xd->given_pass ) ); |
---|
92 | md5_finish( &md5_state, pass_md5 ); |
---|
93 | |
---|
94 | for( i = 0; i < 16; i ++ ) |
---|
95 | { |
---|
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 | } |
---|
103 | if( j != pass_md5[i] ) |
---|
104 | { |
---|
105 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT, |
---|
106 | XML_PASS_ERRORMSG ); |
---|
107 | break; |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | /* If we reached the end of the loop, it was a match! */ |
---|
112 | if( i == 16 ) |
---|
113 | { |
---|
114 | if( xd->pass_st != XML_PASS_CHECK_ONLY ) |
---|
115 | xd->pass_st = XML_PASS_OK; |
---|
116 | } |
---|
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. */ |
---|
123 | } |
---|
124 | else if( g_strcasecmp( element_name, "account" ) == 0 ) |
---|
125 | { |
---|
126 | char *protocol, *handle, *server, *password; |
---|
127 | struct prpl *prpl = NULL; |
---|
128 | |
---|
129 | handle = xml_attr( attr_names, attr_values, "handle" ); |
---|
130 | password = xml_attr( attr_names, attr_values, "password" ); |
---|
131 | server = xml_attr( attr_names, attr_values, "server" ); |
---|
132 | |
---|
133 | protocol = xml_attr( attr_names, attr_values, "protocol" ); |
---|
134 | if( protocol ) |
---|
135 | prpl = find_protocol( protocol ); |
---|
136 | |
---|
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 |
---|
144 | { |
---|
145 | xd->current_account = account_add( irc, prpl, handle, password ); |
---|
146 | if( server ) |
---|
147 | xd->current_account->server = g_strdup( server ); |
---|
148 | } |
---|
149 | } |
---|
150 | else if( g_strcasecmp( element_name, "setting" ) == 0 ) |
---|
151 | { |
---|
152 | if( xd->current_account == NULL ) |
---|
153 | { |
---|
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 ); |
---|
167 | } |
---|
168 | } |
---|
169 | else if( g_strcasecmp( element_name, "buddy" ) == 0 ) |
---|
170 | { |
---|
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 | } |
---|
185 | } |
---|
186 | else |
---|
187 | { |
---|
188 | g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT, |
---|
189 | "Unkown element: %s", element_name ); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | static void xml_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error ) |
---|
194 | { |
---|
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 | } |
---|
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; |
---|
212 | irc_t *irc = xd->irc; |
---|
213 | |
---|
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 ) |
---|
221 | { |
---|
222 | set_setstr( irc, xd->current_setting, (char*) text ); |
---|
223 | g_free( xd->current_setting ); |
---|
224 | xd->current_setting = NULL; |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | GMarkupParser xml_parser = |
---|
229 | { |
---|
230 | xml_start_element, |
---|
231 | xml_end_element, |
---|
232 | xml_text, |
---|
233 | NULL, |
---|
234 | NULL |
---|
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 | |
---|
245 | static storage_status_t xml_load( const char *my_nick, const char *password, irc_t *irc ) |
---|
246 | { |
---|
247 | GMarkupParseContext *ctx; |
---|
248 | struct xml_parsedata *xd; |
---|
249 | char *fn, buf[512]; |
---|
250 | GError *gerr = NULL; |
---|
251 | int fd, st; |
---|
252 | |
---|
253 | if( irc->status & USTATUS_IDENTIFIED ) |
---|
254 | return( 1 ); |
---|
255 | |
---|
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 ); |
---|
261 | |
---|
262 | fn = g_strdup_printf( "%s%s%s", global.conf->configdir, xd->given_nick, ".xml" ); |
---|
263 | if( ( fd = open( fn, O_RDONLY ) ) < 0 ) |
---|
264 | { |
---|
265 | xml_destroy_xd( xd ); |
---|
266 | g_free( fn ); |
---|
267 | return STORAGE_NO_SUCH_USER; |
---|
268 | } |
---|
269 | g_free( fn ); |
---|
270 | |
---|
271 | ctx = g_markup_parse_context_new( &xml_parser, 0, xd, xml_destroy_xd ); |
---|
272 | |
---|
273 | while( ( st = read( fd, buf, sizeof( buf ) ) ) > 0 ) |
---|
274 | { |
---|
275 | if( !g_markup_parse_context_parse( ctx, buf, st, &gerr ) || gerr ) |
---|
276 | { |
---|
277 | g_markup_parse_context_free( ctx ); |
---|
278 | close( fd ); |
---|
279 | |
---|
280 | /* Slightly dirty... */ |
---|
281 | if( gerr && strcmp( gerr->message, XML_PASS_ERRORMSG ) == 0 ) |
---|
282 | return STORAGE_INVALID_PASSWORD; |
---|
283 | else |
---|
284 | { |
---|
285 | if( gerr ) |
---|
286 | irc_usermsg( irc, "Error from XML-parser: %s", gerr->message ); |
---|
287 | |
---|
288 | return STORAGE_OTHER_ERROR; |
---|
289 | } |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | g_markup_parse_context_free( ctx ); |
---|
294 | close( fd ); |
---|
295 | |
---|
296 | irc->status |= USTATUS_IDENTIFIED; |
---|
297 | |
---|
298 | if( set_getint( irc, "auto_connect" ) ) |
---|
299 | { |
---|
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 ); |
---|
303 | } |
---|
304 | |
---|
305 | return STORAGE_OK; |
---|
306 | } |
---|
307 | |
---|
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 | |
---|
325 | static storage_status_t xml_save( irc_t *irc, int overwrite ) |
---|
326 | { |
---|
327 | char path[512], *path2; |
---|
328 | set_t *set; |
---|
329 | nick_t *nick; |
---|
330 | account_t *acc; |
---|
331 | int fd; |
---|
332 | |
---|
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; |
---|
343 | } |
---|
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 | |
---|
389 | return STORAGE_OK; |
---|
390 | |
---|
391 | write_error: |
---|
392 | irc_usermsg( irc, "Write error. Disk full?" ); |
---|
393 | close( fd ); |
---|
394 | |
---|
395 | return STORAGE_OTHER_ERROR; |
---|
396 | } |
---|
397 | |
---|
398 | storage_t storage_xml = { |
---|
399 | .name = "xml", |
---|
400 | .init = xml_init, |
---|
401 | // .check_pass = xml_check_pass, |
---|
402 | // .remove = xml_remove, |
---|
403 | .load = xml_load, |
---|
404 | .save = xml_save |
---|
405 | }; |
---|