1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Jabber module - SI packets * |
---|
5 | * * |
---|
6 | * Copyright 2007 Uli Meis <a.sporto+bee@gmail.com> * |
---|
7 | * * |
---|
8 | * This program is free software; you can redistribute it and/or modify * |
---|
9 | * it under the terms of the GNU General Public License as published by * |
---|
10 | * the Free Software Foundation; either version 2 of the License, or * |
---|
11 | * (at your option) any later version. * |
---|
12 | * * |
---|
13 | * This program is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
---|
16 | * GNU General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU General Public License along * |
---|
19 | * with this program; if not, write to the Free Software Foundation, Inc., * |
---|
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
---|
21 | * * |
---|
22 | \***************************************************************************/ |
---|
23 | |
---|
24 | #include "jabber.h" |
---|
25 | #include "sha1.h" |
---|
26 | |
---|
27 | void jabber_si_answer_request( file_transfer_t *ft ); |
---|
28 | int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf ); |
---|
29 | |
---|
30 | /* file_transfer free() callback */ |
---|
31 | void jabber_si_free_transfer( file_transfer_t *ft) |
---|
32 | { |
---|
33 | struct jabber_transfer *tf = ft->data; |
---|
34 | struct jabber_data *jd = tf->ic->proto_data; |
---|
35 | |
---|
36 | if ( tf->watch_in ) |
---|
37 | b_event_remove( tf->watch_in ); |
---|
38 | |
---|
39 | jd->filetransfers = g_slist_remove( jd->filetransfers, tf ); |
---|
40 | |
---|
41 | if( tf->fd ) |
---|
42 | { |
---|
43 | close( tf->fd ); |
---|
44 | tf->fd = 0; |
---|
45 | } |
---|
46 | |
---|
47 | g_free( tf->ini_jid ); |
---|
48 | g_free( tf->tgt_jid ); |
---|
49 | g_free( tf->iq_id ); |
---|
50 | g_free( tf->sid ); |
---|
51 | } |
---|
52 | |
---|
53 | /* file_transfer finished() callback */ |
---|
54 | void jabber_si_finished( file_transfer_t *ft ) |
---|
55 | { |
---|
56 | struct jabber_transfer *tf = ft->data; |
---|
57 | |
---|
58 | imcb_log( tf->ic, "File %s transferred successfully!" , ft->file_name ); |
---|
59 | } |
---|
60 | |
---|
61 | /* file_transfer canceled() callback */ |
---|
62 | void jabber_si_canceled( file_transfer_t *ft, char *reason ) |
---|
63 | { |
---|
64 | struct jabber_transfer *tf = ft->data; |
---|
65 | struct xt_node *reply, *iqnode; |
---|
66 | |
---|
67 | if( tf->accepted ) |
---|
68 | return; |
---|
69 | |
---|
70 | iqnode = jabber_make_packet( "iq", "error", tf->ini_jid, NULL ); |
---|
71 | xt_add_attr( iqnode, "id", tf->iq_id ); |
---|
72 | reply = jabber_make_error_packet( iqnode, "forbidden", "cancel", "403" ); |
---|
73 | xt_free_node( iqnode ); |
---|
74 | |
---|
75 | if( !jabber_write_packet( tf->ic, reply ) ) |
---|
76 | imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" ); |
---|
77 | xt_free_node( reply ); |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | void jabber_si_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who ) |
---|
82 | { |
---|
83 | struct jabber_transfer *tf; |
---|
84 | struct jabber_data *jd = ic->proto_data; |
---|
85 | char *server = jd->server; |
---|
86 | |
---|
87 | imcb_log( ic, "Trying to send %s(%zd bytes) to %s", ft->file_name, ft->file_size, who ); |
---|
88 | |
---|
89 | tf = g_new0( struct jabber_transfer, 1 ); |
---|
90 | |
---|
91 | tf->ic = ic; |
---|
92 | tf->ft = ft; |
---|
93 | tf->ft->data = tf; |
---|
94 | tf->ft->free = jabber_si_free_transfer; |
---|
95 | tf->ft->finished = jabber_si_finished; |
---|
96 | ft->write = jabber_bs_send_write; |
---|
97 | |
---|
98 | jd->filetransfers = g_slist_prepend( jd->filetransfers, tf ); |
---|
99 | |
---|
100 | /* query the buddy's features */ |
---|
101 | jabber_iq_query_features( ic, who ); |
---|
102 | |
---|
103 | /* query proxies from the server */ |
---|
104 | if( !jd->have_streamhosts ) |
---|
105 | jabber_iq_query_server( ic, server, XMLNS_DISCO_ITEMS ); |
---|
106 | |
---|
107 | /* send the request to our buddy */ |
---|
108 | jabber_si_send_request( ic, who, tf ); |
---|
109 | |
---|
110 | /* and start the receive logic */ |
---|
111 | imcb_file_recv_start( ft ); |
---|
112 | } |
---|
113 | |
---|
114 | /* |
---|
115 | * First function that gets called when a file transfer request comes in. |
---|
116 | * A lot to parse. |
---|
117 | * |
---|
118 | * We choose a stream type from the options given by the initiator. |
---|
119 | * Then we wait for imcb to call the accept or cancel callbacks. |
---|
120 | */ |
---|
121 | int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode) |
---|
122 | { |
---|
123 | struct xt_node *c, *d, *reply; |
---|
124 | char *sid, *ini_jid, *tgt_jid, *iq_id, *s, *ext_jid; |
---|
125 | struct jabber_buddy *bud; |
---|
126 | int requestok = FALSE; |
---|
127 | char *name; |
---|
128 | size_t size; |
---|
129 | struct jabber_transfer *tf; |
---|
130 | struct jabber_data *jd = ic->proto_data; |
---|
131 | file_transfer_t *ft; |
---|
132 | |
---|
133 | /* All this means we expect something like this: ( I think ) |
---|
134 | * <iq from=... to=... id=...> |
---|
135 | * <si id=id xmlns=si profile=ft> |
---|
136 | * <file xmlns=ft/> |
---|
137 | * <feature xmlns=feature> |
---|
138 | * <x xmlns=xdata type=submit> |
---|
139 | * <field var=stream-method> |
---|
140 | * |
---|
141 | */ |
---|
142 | if( !( ini_jid = xt_find_attr( node, "from" ) ) || |
---|
143 | !( tgt_jid = xt_find_attr( node, "to" ) ) || |
---|
144 | !( iq_id = xt_find_attr( node, "id" ) ) || |
---|
145 | !( sid = xt_find_attr( sinode, "id" ) ) || |
---|
146 | !( strcmp( xt_find_attr( sinode, "profile" ), XMLNS_FILETRANSFER ) == 0 ) || |
---|
147 | !( d = xt_find_node( sinode->children, "file" ) ) || |
---|
148 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FILETRANSFER ) == 0 ) || |
---|
149 | !( name = xt_find_attr( d, "name" ) ) || |
---|
150 | !( size = (size_t) atoll( xt_find_attr( d, "size" ) ) ) || |
---|
151 | !( d = xt_find_node( sinode->children, "feature" ) ) || |
---|
152 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FEATURE ) == 0 ) || |
---|
153 | !( d = xt_find_node( d->children, "x" ) ) || |
---|
154 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_XDATA ) == 0 ) || |
---|
155 | !( strcmp( xt_find_attr( d, "type" ), "form" ) == 0 ) || |
---|
156 | !( d = xt_find_node( d->children, "field" ) ) || |
---|
157 | !( strcmp( xt_find_attr( d, "var" ), "stream-method" ) == 0 ) ) |
---|
158 | { |
---|
159 | imcb_log( ic, "WARNING: Received incomplete Stream Initiation request" ); |
---|
160 | } else |
---|
161 | { |
---|
162 | /* Check if we support one of the options */ |
---|
163 | |
---|
164 | c = d->children; |
---|
165 | while( ( c = xt_find_node( c, "option" ) ) ) |
---|
166 | if( ( d = xt_find_node( c->children, "value" ) ) && |
---|
167 | ( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) |
---|
168 | { |
---|
169 | requestok = TRUE; |
---|
170 | break; |
---|
171 | } |
---|
172 | |
---|
173 | if ( !requestok ) |
---|
174 | imcb_log( ic, "WARNING: Unsupported file transfer request from %s", ini_jid); |
---|
175 | } |
---|
176 | |
---|
177 | if ( requestok ) |
---|
178 | { |
---|
179 | /* Figure out who the transfer should come frome... */ |
---|
180 | |
---|
181 | if( ( s = strchr( ini_jid, '/' ) ) ) |
---|
182 | { |
---|
183 | if( ( bud = jabber_buddy_by_jid( ic, ini_jid, GET_BUDDY_EXACT ) ) ) |
---|
184 | { |
---|
185 | bud->last_act = time( NULL ); |
---|
186 | ext_jid = bud->ext_jid ? : bud->bare_jid; |
---|
187 | } |
---|
188 | else |
---|
189 | *s = 0; /* We need to generate a bare JID now. */ |
---|
190 | } |
---|
191 | |
---|
192 | if( !( ft = imcb_file_send_start( ic, ext_jid, name, size ) ) ) |
---|
193 | { |
---|
194 | imcb_log( ic, "WARNING: Error handling transfer request from %s", ini_jid); |
---|
195 | requestok = FALSE; |
---|
196 | } |
---|
197 | |
---|
198 | *s = '/'; |
---|
199 | } |
---|
200 | |
---|
201 | if ( !requestok ) |
---|
202 | { |
---|
203 | reply = jabber_make_error_packet( node, "item-not-found", "cancel", NULL ); |
---|
204 | if (!jabber_write_packet( ic, reply )) |
---|
205 | imcb_log( ic, "WARNING: Error generating reply to file transfer request" ); |
---|
206 | xt_free_node( reply ); |
---|
207 | return XT_HANDLED; |
---|
208 | } |
---|
209 | |
---|
210 | /* Request is fine. */ |
---|
211 | |
---|
212 | imcb_log( ic, "File transfer request from %s for %s (%zd kb). ", xt_find_attr( node, "from" ), name, size/1024 ); |
---|
213 | |
---|
214 | imcb_log( ic, "Accept the DCC transfer if you'd like the file. If you don't, issue the 'transfers reject' command."); |
---|
215 | |
---|
216 | tf = g_new0( struct jabber_transfer, 1 ); |
---|
217 | |
---|
218 | tf->ini_jid = g_strdup( ini_jid ); |
---|
219 | tf->tgt_jid = g_strdup( tgt_jid ); |
---|
220 | tf->iq_id = g_strdup( iq_id ); |
---|
221 | tf->sid = g_strdup( sid ); |
---|
222 | tf->ic = ic; |
---|
223 | tf->ft = ft; |
---|
224 | tf->ft->data = tf; |
---|
225 | tf->ft->accept = jabber_si_answer_request; |
---|
226 | tf->ft->free = jabber_si_free_transfer; |
---|
227 | tf->ft->finished = jabber_si_finished; |
---|
228 | tf->ft->canceled = jabber_si_canceled; |
---|
229 | |
---|
230 | jd->filetransfers = g_slist_prepend( jd->filetransfers, tf ); |
---|
231 | |
---|
232 | return XT_HANDLED; |
---|
233 | } |
---|
234 | |
---|
235 | /* |
---|
236 | * imc called the accept callback which probably means that the user accepted this file transfer. |
---|
237 | * We send our response to the initiator. |
---|
238 | * In the next step, the initiator will send us a request for the given stream type. |
---|
239 | * (currently that can only be a SOCKS5 bytestream) |
---|
240 | */ |
---|
241 | void jabber_si_answer_request( file_transfer_t *ft ) { |
---|
242 | struct jabber_transfer *tf = ft->data; |
---|
243 | struct xt_node *node, *sinode, *reply; |
---|
244 | |
---|
245 | /* generate response, start with the SI tag */ |
---|
246 | sinode = xt_new_node( "si", NULL, NULL ); |
---|
247 | xt_add_attr( sinode, "xmlns", XMLNS_SI ); |
---|
248 | xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER ); |
---|
249 | xt_add_attr( sinode, "id", tf->sid ); |
---|
250 | |
---|
251 | /* now the file tag */ |
---|
252 | node = xt_new_node( "file", NULL, NULL ); |
---|
253 | xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER ); |
---|
254 | |
---|
255 | xt_add_child( sinode, node ); |
---|
256 | |
---|
257 | /* and finally the feature tag */ |
---|
258 | node = xt_new_node( "field", NULL, NULL ); |
---|
259 | xt_add_attr( node, "var", "stream-method" ); |
---|
260 | xt_add_attr( node, "type", "list-single" ); |
---|
261 | |
---|
262 | /* Currently all we can do. One could also implement in-band (IBB) */ |
---|
263 | xt_add_child( node, xt_new_node( "value", XMLNS_BYTESTREAMS, NULL ) ); |
---|
264 | |
---|
265 | node = xt_new_node( "x", NULL, node ); |
---|
266 | xt_add_attr( node, "xmlns", XMLNS_XDATA ); |
---|
267 | xt_add_attr( node, "type", "submit" ); |
---|
268 | |
---|
269 | node = xt_new_node( "feature", NULL, node ); |
---|
270 | xt_add_attr( node, "xmlns", XMLNS_FEATURE ); |
---|
271 | |
---|
272 | xt_add_child( sinode, node ); |
---|
273 | |
---|
274 | reply = jabber_make_packet( "iq", "result", tf->ini_jid, sinode ); |
---|
275 | xt_add_attr( reply, "id", tf->iq_id ); |
---|
276 | |
---|
277 | if( !jabber_write_packet( tf->ic, reply ) ) |
---|
278 | imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" ); |
---|
279 | else |
---|
280 | tf->accepted = TRUE; |
---|
281 | xt_free_node( reply ); |
---|
282 | } |
---|
283 | |
---|
284 | static xt_status jabber_si_handle_response(struct im_connection *ic, struct xt_node *node, struct xt_node *orig ) |
---|
285 | { |
---|
286 | struct xt_node *c, *d; |
---|
287 | char *ini_jid, *tgt_jid, *iq_id; |
---|
288 | GSList *tflist; |
---|
289 | struct jabber_transfer *tf=NULL; |
---|
290 | struct jabber_data *jd = ic->proto_data; |
---|
291 | |
---|
292 | if( !( tgt_jid = xt_find_attr( node, "from" ) ) || |
---|
293 | !( ini_jid = xt_find_attr( node, "to" ) ) ) |
---|
294 | { |
---|
295 | imcb_log( ic, "Invalid SI response from=%s to=%s", tgt_jid, ini_jid ); |
---|
296 | return XT_HANDLED; |
---|
297 | } |
---|
298 | |
---|
299 | /* All this means we expect something like this: ( I think ) |
---|
300 | * <iq from=... to=... id=...> |
---|
301 | * <si xmlns=si> |
---|
302 | * [ <file xmlns=ft/> ] <-- not neccessary |
---|
303 | * <feature xmlns=feature> |
---|
304 | * <x xmlns=xdata type=submit> |
---|
305 | * <field var=stream-method> |
---|
306 | * <value> |
---|
307 | */ |
---|
308 | if( !( tgt_jid = xt_find_attr( node, "from" ) ) || |
---|
309 | !( ini_jid = xt_find_attr( node, "to" ) ) || |
---|
310 | !( iq_id = xt_find_attr( node, "id" ) ) || |
---|
311 | !( c = xt_find_node( node->children, "si" ) ) || |
---|
312 | !( strcmp( xt_find_attr( c, "xmlns" ), XMLNS_SI ) == 0 ) || |
---|
313 | /* !( d = xt_find_node( c->children, "file" ) ) || |
---|
314 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FILETRANSFER ) == 0 ) || */ |
---|
315 | !( d = xt_find_node( c->children, "feature" ) ) || |
---|
316 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FEATURE ) == 0 ) || |
---|
317 | !( d = xt_find_node( d->children, "x" ) ) || |
---|
318 | !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_XDATA ) == 0 ) || |
---|
319 | !( strcmp( xt_find_attr( d, "type" ), "submit" ) == 0 ) || |
---|
320 | !( d = xt_find_node( d->children, "field" ) ) || |
---|
321 | !( strcmp( xt_find_attr( d, "var" ), "stream-method" ) == 0 ) || |
---|
322 | !( d = xt_find_node( d->children, "value" ) ) ) |
---|
323 | { |
---|
324 | imcb_log( ic, "WARNING: Received incomplete Stream Initiation response" ); |
---|
325 | return XT_HANDLED; |
---|
326 | } |
---|
327 | |
---|
328 | if( !( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) { |
---|
329 | /* since we should only have advertised what we can do and the peer should |
---|
330 | * only have chosen what we offered, this should never happen */ |
---|
331 | imcb_log( ic, "WARNING: Received invalid Stream Initiation response, method %s", d->text ); |
---|
332 | |
---|
333 | return XT_HANDLED; |
---|
334 | } |
---|
335 | |
---|
336 | /* Let's see if we can find out what this bytestream should be for... */ |
---|
337 | |
---|
338 | for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) ) |
---|
339 | { |
---|
340 | struct jabber_transfer *tft = tflist->data; |
---|
341 | if( ( strcmp( tft->iq_id, iq_id ) == 0 ) ) |
---|
342 | { |
---|
343 | tf = tft; |
---|
344 | break; |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | if (!tf) |
---|
349 | { |
---|
350 | imcb_log( ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid ); |
---|
351 | return XT_HANDLED; |
---|
352 | } |
---|
353 | |
---|
354 | tf->ini_jid = g_strdup( ini_jid ); |
---|
355 | tf->tgt_jid = g_strdup( tgt_jid ); |
---|
356 | |
---|
357 | imcb_log( ic, "File %s: %s accepted the transfer!", tf->ft->file_name, tgt_jid ); |
---|
358 | |
---|
359 | jabber_bs_send_start( tf ); |
---|
360 | |
---|
361 | return XT_HANDLED; |
---|
362 | } |
---|
363 | |
---|
364 | int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf ) |
---|
365 | { |
---|
366 | struct xt_node *node, *sinode; |
---|
367 | struct jabber_buddy *bud; |
---|
368 | |
---|
369 | /* who knows how many bits the future holds :) */ |
---|
370 | char filesizestr[ 1 + ( int ) ( 0.301029995663981198f * sizeof( size_t ) * 8 ) ]; |
---|
371 | |
---|
372 | const char *methods[] = |
---|
373 | { |
---|
374 | XMLNS_BYTESTREAMS, |
---|
375 | //XMLNS_IBB, |
---|
376 | NULL |
---|
377 | }; |
---|
378 | const char **m; |
---|
379 | char *s; |
---|
380 | |
---|
381 | /* Maybe we should hash this? */ |
---|
382 | tf->sid = g_strdup_printf( "BitlBeeJabberSID%d", tf->ft->local_id ); |
---|
383 | |
---|
384 | if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) ) |
---|
385 | bud = jabber_buddy_by_ext_jid( ic, who, 0 ); |
---|
386 | else |
---|
387 | bud = jabber_buddy_by_jid( ic, who, 0 ); |
---|
388 | |
---|
389 | /* start with the SI tag */ |
---|
390 | sinode = xt_new_node( "si", NULL, NULL ); |
---|
391 | xt_add_attr( sinode, "xmlns", XMLNS_SI ); |
---|
392 | xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER ); |
---|
393 | xt_add_attr( sinode, "id", tf->sid ); |
---|
394 | |
---|
395 | /* if( mimetype ) |
---|
396 | xt_add_attr( node, "mime-type", mimetype ); */ |
---|
397 | |
---|
398 | /* now the file tag */ |
---|
399 | /* if( desc ) |
---|
400 | node = xt_new_node( "desc", descr, NULL ); */ |
---|
401 | node = xt_new_node( "range", NULL, NULL ); |
---|
402 | |
---|
403 | sprintf( filesizestr, "%zd", tf->ft->file_size ); |
---|
404 | node = xt_new_node( "file", NULL, node ); |
---|
405 | xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER ); |
---|
406 | xt_add_attr( node, "name", tf->ft->file_name ); |
---|
407 | xt_add_attr( node, "size", filesizestr ); |
---|
408 | /* if (hash) |
---|
409 | xt_add_attr( node, "hash", hash ); |
---|
410 | if (date) |
---|
411 | xt_add_attr( node, "date", date ); */ |
---|
412 | |
---|
413 | xt_add_child( sinode, node ); |
---|
414 | |
---|
415 | /* and finally the feature tag */ |
---|
416 | node = xt_new_node( "field", NULL, NULL ); |
---|
417 | xt_add_attr( node, "var", "stream-method" ); |
---|
418 | xt_add_attr( node, "type", "list-single" ); |
---|
419 | |
---|
420 | for ( m = methods ; *m ; m ++ ) |
---|
421 | xt_add_child( node, xt_new_node( "option", NULL, xt_new_node( "value", (char *)*m, NULL ) ) ); |
---|
422 | |
---|
423 | node = xt_new_node( "x", NULL, node ); |
---|
424 | xt_add_attr( node, "xmlns", XMLNS_XDATA ); |
---|
425 | xt_add_attr( node, "type", "form" ); |
---|
426 | |
---|
427 | node = xt_new_node( "feature", NULL, node ); |
---|
428 | xt_add_attr( node, "xmlns", XMLNS_FEATURE ); |
---|
429 | |
---|
430 | xt_add_child( sinode, node ); |
---|
431 | |
---|
432 | /* and we are there... */ |
---|
433 | node = jabber_make_packet( "iq", "set", bud ? bud->full_jid : who, sinode ); |
---|
434 | jabber_cache_add( ic, node, jabber_si_handle_response ); |
---|
435 | tf->iq_id = g_strdup( xt_find_attr( node, "id" ) ); |
---|
436 | |
---|
437 | return jabber_write_packet( ic, node ); |
---|
438 | } |
---|