source: protocols/jabber/si.c @ 6f360a0

Last change on this file since 6f360a0 was 92d3044, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-12-18T01:24:33Z

Improved failure handling in jabber_si_handle_request(). If the imcb
callback fails, we shouldn't try to continue the ft and dereference the NULL
ptr it returned.

  • Property mode set to 100644
File size: 17.0 KB
Line 
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
27void jabber_si_answer_request( file_transfer_t *ft );
28int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf );
29
30/* file_transfer free() callback */
31void 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 != -1 )
42        {
43                closesocket( tf->fd );
44                tf->fd = -1;
45        }
46
47        if( tf->disco_timeout )
48                b_event_remove( tf->disco_timeout );
49       
50        g_free( tf->ini_jid );
51        g_free( tf->tgt_jid );
52        g_free( tf->iq_id );
53        g_free( tf->sid );
54        g_free( tf );
55}
56
57/* file_transfer canceled() callback */
58void jabber_si_canceled( file_transfer_t *ft, char *reason )
59{
60        struct jabber_transfer *tf = ft->data;
61        struct xt_node *reply, *iqnode;
62
63        if( tf->accepted )
64                return;
65       
66        iqnode = jabber_make_packet( "iq", "error", tf->ini_jid, NULL );
67        xt_add_attr( iqnode, "id", tf->iq_id );
68        reply = jabber_make_error_packet( iqnode, "forbidden", "cancel", "403" );
69        xt_free_node( iqnode );
70       
71        if( !jabber_write_packet( tf->ic, reply ) )
72                imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" );
73        xt_free_node( reply );
74
75}
76
77int jabber_si_check_features( struct jabber_transfer *tf, GSList *features ) {
78        int foundft = FALSE, foundbt = FALSE, foundsi = FALSE;
79
80        while ( features )
81        {
82                if( !strcmp( features->data, XMLNS_FILETRANSFER ) )
83                        foundft = TRUE;
84                if( !strcmp( features->data, XMLNS_BYTESTREAMS ) )
85                        foundbt = TRUE;
86                if( !strcmp( features->data, XMLNS_SI ) )
87                        foundsi = TRUE;
88
89                features = g_slist_next(features);
90        }
91
92        if( !foundft )
93                imcb_file_canceled( tf->ic, tf->ft, "Buddy's client doesn't feature file transfers" );
94        else if( !foundbt )
95                imcb_file_canceled( tf->ic, tf->ft, "Buddy's client doesn't feature byte streams (required)" );
96        else if( !foundsi )
97                imcb_file_canceled( tf->ic, tf->ft, "Buddy's client doesn't feature stream initiation (required)" );
98               
99        return foundft && foundbt && foundsi;
100}
101
102void jabber_si_transfer_start( struct jabber_transfer *tf ) {
103
104        if( !jabber_si_check_features( tf, tf->bud->features ) )
105                return;
106               
107        /* send the request to our buddy */
108        jabber_si_send_request( tf->ic, tf->bud->full_jid, tf );
109
110        /* and start the receive logic */
111        imcb_file_recv_start( tf->ic, tf->ft );
112
113}
114
115gboolean jabber_si_waitfor_disco( gpointer data, gint fd, b_input_condition cond )
116{
117        struct jabber_transfer *tf = data;
118        struct jabber_data *jd = tf->ic->proto_data;
119
120        tf->disco_timeout_fired++;
121
122        if( tf->bud->features && jd->have_streamhosts==1 ) {
123                tf->disco_timeout = 0;
124                jabber_si_transfer_start( tf );
125                return FALSE;
126        }
127
128        /* 8 seconds should be enough for server and buddy to respond */
129        if ( tf->disco_timeout_fired < 16 )
130                return TRUE;
131       
132        if( !tf->bud->features && jd->have_streamhosts!=1 )
133                imcb_log( tf->ic, "Couldn't get buddy's features nor discover all services of the server" );
134        else if( !tf->bud->features )
135                imcb_log( tf->ic, "Couldn't get buddy's features" );
136        else
137                imcb_log( tf->ic, "Couldn't discover some of the server's services" );
138       
139        tf->disco_timeout = 0;
140        jabber_si_transfer_start( tf );
141        return FALSE;
142}
143
144void jabber_si_transfer_request( struct im_connection *ic, file_transfer_t *ft, char *who ) 
145{
146        struct jabber_transfer *tf;
147        struct jabber_data *jd = ic->proto_data;
148        struct jabber_buddy *bud;
149        char *server = jd->server, *s;
150
151        if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
152                bud = jabber_buddy_by_ext_jid( ic, who, 0 );
153        else
154                bud = jabber_buddy_by_jid( ic, who, 0 );
155
156        if( bud == NULL )
157        {
158                imcb_file_canceled( ic, ft, "Couldn't find buddy (BUG?)" );
159                return;
160        }
161       
162        imcb_log( ic, "Trying to send %s(%zd bytes) to %s", ft->file_name, ft->file_size, who );
163
164        tf = g_new0( struct jabber_transfer, 1 );
165
166        tf->ic = ic;
167        tf->ft = ft;
168        tf->fd = -1;
169        tf->ft->data = tf;
170        tf->ft->free = jabber_si_free_transfer;
171        tf->bud = bud;
172        ft->write = jabber_bs_send_write;
173
174        jd->filetransfers = g_slist_prepend( jd->filetransfers, tf );
175
176        /* query buddy's features and server's streaming proxies if neccessary */
177
178        if( !tf->bud->features )
179                jabber_iq_query_features( ic, bud->full_jid );
180
181        /* If <auto> is not set don't check for proxies */
182        if( ( jd->have_streamhosts!=1 ) && ( jd->streamhosts==NULL ) &&
183            ( strstr( set_getstr( &ic->acc->set, "proxy" ), "<auto>" ) != NULL ) ) {
184                jd->have_streamhosts = 0;
185                jabber_iq_query_server( ic, server, XMLNS_DISCO_ITEMS );
186        } else if ( jd->streamhosts!=NULL )
187                jd->have_streamhosts = 1;
188
189        /* if we had to do a query, wait for the result.
190         * Otherwise fire away. */
191        if( !tf->bud->features || jd->have_streamhosts!=1 )
192                tf->disco_timeout = b_timeout_add( 500, jabber_si_waitfor_disco, tf );
193        else
194                jabber_si_transfer_start( tf );
195}
196
197/*
198 * First function that gets called when a file transfer request comes in.
199 * A lot to parse.
200 *
201 * We choose a stream type from the options given by the initiator.
202 * Then we wait for imcb to call the accept or cancel callbacks.
203 */
204int jabber_si_handle_request( struct im_connection *ic, struct xt_node *node, struct xt_node *sinode)
205{
206        struct xt_node *c, *d, *reply;
207        char *sid, *ini_jid, *tgt_jid, *iq_id, *s, *ext_jid, *size_s;
208        struct jabber_buddy *bud;
209        int requestok = FALSE;
210        char *name, *cmp;
211        size_t size;
212        struct jabber_transfer *tf;
213        struct jabber_data *jd = ic->proto_data;
214        file_transfer_t *ft;
215       
216        /* All this means we expect something like this: ( I think )
217         * <iq from=... to=... id=...>
218         *      <si id=id xmlns=si profile=ft>
219         *              <file xmlns=ft/>
220         *              <feature xmlns=feature>
221         *                      <x xmlns=xdata type=submit>
222         *                              <field var=stream-method>
223         *
224         */
225        if( !( ini_jid          = xt_find_attr(   node, "from" )                        ) ||
226            !( tgt_jid          = xt_find_attr(   node, "to" )                          ) ||
227            !( iq_id            = xt_find_attr(   node, "id" )                          ) ||
228            !( sid              = xt_find_attr( sinode, "id" )                          ) ||
229            !( cmp              = xt_find_attr( sinode, "profile" )                     ) ||
230            !( 0               == strcmp( cmp, XMLNS_FILETRANSFER )                     ) ||
231            !( d                = xt_find_node( sinode->children, "file" )              ) ||
232            !( cmp = xt_find_attr( d, "xmlns" )                                         ) ||
233            !( 0               == strcmp( cmp, XMLNS_FILETRANSFER )                     ) ||
234            !( name             = xt_find_attr( d, "name" )                             ) ||
235            !( size_s           = xt_find_attr( d, "size" )                             ) ||
236            !( 1               == sscanf( size_s, "%zd", &size )                        ) ||
237            !( d                = xt_find_node( sinode->children, "feature" )           ) ||
238            !( cmp              = xt_find_attr( d, "xmlns" )                            ) ||
239            !( 0               == strcmp( cmp, XMLNS_FEATURE )                          ) ||
240            !( d                = xt_find_node( d->children, "x" )                      ) ||
241            !( cmp              = xt_find_attr( d, "xmlns" )                            ) ||
242            !( 0               == strcmp( cmp, XMLNS_XDATA )                            ) ||
243            !( cmp              = xt_find_attr( d, "type" )                             ) ||
244            !( 0               == strcmp( cmp, "form" )                                 ) ||
245            !( d                = xt_find_node( d->children, "field" )                  ) ||
246            !( cmp              = xt_find_attr( d, "var" )                              ) ||
247            !( 0               == strcmp( cmp, "stream-method" )                        ) )
248        {
249                imcb_log( ic, "WARNING: Received incomplete Stream Initiation request" );
250        }
251        else
252        {
253                /* Check if we support one of the options */
254
255                c = d->children;
256                while( ( c = xt_find_node( c, "option" ) ) )
257                        if( ( d = xt_find_node( c->children, "value" ) ) &&
258                            ( d->text != NULL ) &&
259                            ( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) )
260                        {
261                                requestok = TRUE;
262                                break;
263                        }
264                        else
265                        {
266                                c = c->next;
267                        }
268
269                if ( !requestok )
270                        imcb_log( ic, "WARNING: Unsupported file transfer request from %s", ini_jid);
271        }
272       
273        if( requestok )
274        {
275                /* Figure out who the transfer should come frome... */
276
277                ext_jid = ini_jid;
278                if( ( s = strchr( ini_jid, '/' ) ) )
279                {
280                        if( ( bud = jabber_buddy_by_jid( ic, ini_jid, GET_BUDDY_EXACT ) ) )
281                        {
282                                bud->last_msg = time( NULL );
283                                ext_jid = bud->ext_jid ? : bud->bare_jid;
284                        }
285                        else
286                                *s = 0; /* We need to generate a bare JID now. */
287                }
288
289                if( !( ft = imcb_file_send_start( ic, ext_jid, name, size ) ) )
290                { 
291                        imcb_log( ic, "WARNING: Error handling transfer request from %s", ini_jid);
292                        requestok = FALSE;
293                }
294
295                if( s )
296                        *s = '/';
297        }
298       
299        if( !requestok )
300        { 
301                reply = jabber_make_error_packet( node, "item-not-found", "cancel", NULL );
302                if (!jabber_write_packet( ic, reply ))
303                        imcb_log( ic, "WARNING: Error generating reply to file transfer request" );
304                xt_free_node( reply );
305                return XT_HANDLED;
306        }
307
308        /* Request is fine. */
309
310        tf = g_new0( struct jabber_transfer, 1 );
311
312        tf->ini_jid = g_strdup( ini_jid );
313        tf->tgt_jid = g_strdup( tgt_jid );
314        tf->iq_id = g_strdup( iq_id );
315        tf->sid = g_strdup( sid );
316        tf->ic = ic;
317        tf->ft = ft;
318        tf->fd = -1;
319        tf->ft->data = tf;
320        tf->ft->accept = jabber_si_answer_request;
321        tf->ft->free = jabber_si_free_transfer;
322        tf->ft->canceled = jabber_si_canceled;
323
324        jd->filetransfers = g_slist_prepend( jd->filetransfers, tf );
325
326        return XT_HANDLED;
327}
328
329/*
330 * imc called the accept callback which probably means that the user accepted this file transfer.
331 * We send our response to the initiator.
332 * In the next step, the initiator will send us a request for the given stream type.
333 * (currently that can only be a SOCKS5 bytestream)
334 */
335void jabber_si_answer_request( file_transfer_t *ft ) {
336        struct jabber_transfer *tf = ft->data;
337        struct xt_node *node, *sinode, *reply;
338
339        /* generate response, start with the SI tag */
340        sinode = xt_new_node( "si", NULL, NULL );
341        xt_add_attr( sinode, "xmlns", XMLNS_SI );
342        xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER );
343        xt_add_attr( sinode, "id", tf->sid );
344
345        /* now the file tag */
346        node = xt_new_node( "file", NULL, NULL );
347        xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER );
348
349        xt_add_child( sinode, node );
350
351        /* and finally the feature tag */
352        node = xt_new_node( "field", NULL, NULL );
353        xt_add_attr( node, "var", "stream-method" );
354        xt_add_attr( node, "type", "list-single" );
355
356        /* Currently all we can do. One could also implement in-band (IBB) */
357        xt_add_child( node, xt_new_node( "value", XMLNS_BYTESTREAMS, NULL ) );
358
359        node = xt_new_node( "x", NULL, node );
360        xt_add_attr( node, "xmlns", XMLNS_XDATA );
361        xt_add_attr( node, "type", "submit" );
362
363        node = xt_new_node( "feature", NULL, node );
364        xt_add_attr( node, "xmlns", XMLNS_FEATURE );
365
366        xt_add_child( sinode, node );
367
368        reply = jabber_make_packet( "iq", "result", tf->ini_jid, sinode );
369        xt_add_attr( reply, "id", tf->iq_id );
370       
371        if( !jabber_write_packet( tf->ic, reply ) )
372                imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" );
373        else
374                tf->accepted = TRUE;
375        xt_free_node( reply );
376}
377
378static xt_status jabber_si_handle_response(struct im_connection *ic, struct xt_node *node, struct xt_node *orig )
379{
380        struct xt_node *c, *d;
381        char *ini_jid = NULL, *tgt_jid, *iq_id, *cmp;
382        GSList *tflist;
383        struct jabber_transfer *tf=NULL;
384        struct jabber_data *jd = ic->proto_data;
385
386        if( !( tgt_jid = xt_find_attr( node, "from" ) ) ||
387            !( ini_jid = xt_find_attr( node, "to" ) ) )
388        {
389                imcb_log( ic, "Invalid SI response from=%s to=%s", tgt_jid, ini_jid );
390                return XT_HANDLED;
391        }
392       
393        /* All this means we expect something like this: ( I think )
394         * <iq from=... to=... id=...>
395         *      <si xmlns=si>
396         *      [       <file xmlns=ft/>    ] <-- not neccessary
397         *              <feature xmlns=feature>
398         *                      <x xmlns=xdata type=submit>
399         *                              <field var=stream-method>
400         *                                      <value>
401         */
402        if( !( tgt_jid = xt_find_attr( node, "from" ) ) ||
403            !( ini_jid = xt_find_attr( node, "to" ) ) ||
404            !( iq_id   = xt_find_attr( node, "id" ) ) ||
405            !( c = xt_find_node( node->children, "si" ) ) ||
406            !( cmp = xt_find_attr( c, "xmlns" ) ) ||
407            !( strcmp( cmp, XMLNS_SI ) == 0 ) ||
408            !( d = xt_find_node( c->children, "feature" ) ) ||
409            !( cmp = xt_find_attr( d, "xmlns" ) ) ||
410            !( strcmp( cmp, XMLNS_FEATURE ) == 0 ) ||
411            !( d = xt_find_node( d->children, "x" ) ) ||
412            !( cmp = xt_find_attr( d, "xmlns" ) ) ||
413            !( strcmp( cmp, XMLNS_XDATA ) == 0 ) ||
414            !( cmp = xt_find_attr( d, "type" ) ) ||
415            !( strcmp( cmp, "submit" ) == 0 ) ||
416            !( d = xt_find_node( d->children, "field" ) ) ||
417            !( cmp = xt_find_attr( d, "var" ) ) ||
418            !( strcmp( cmp, "stream-method" ) == 0 ) ||
419            !( d = xt_find_node( d->children, "value" ) ) )
420        {
421                imcb_log( ic, "WARNING: Received incomplete Stream Initiation response" );
422                return XT_HANDLED;
423        }
424
425        if( !( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) { 
426                /* since we should only have advertised what we can do and the peer should
427                 * only have chosen what we offered, this should never happen */
428                imcb_log( ic, "WARNING: Received invalid Stream Initiation response, method %s", d->text );
429                       
430                return XT_HANDLED;
431        }
432       
433        /* Let's see if we can find out what this bytestream should be for... */
434
435        for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) )
436        {
437                struct jabber_transfer *tft = tflist->data;
438                if( ( strcmp( tft->iq_id, iq_id ) == 0 ) )
439                {
440                        tf = tft;
441                        break;
442                }
443        }
444
445        if (!tf) 
446        {
447                imcb_log( ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid );
448                return XT_HANDLED;
449        }
450
451        tf->ini_jid = g_strdup( ini_jid );
452        tf->tgt_jid = g_strdup( tgt_jid );
453
454        imcb_log( ic, "File %s: %s accepted the transfer!", tf->ft->file_name, tgt_jid );
455
456        jabber_bs_send_start( tf );
457
458        return XT_HANDLED;
459}
460
461int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf )
462{
463        struct xt_node *node, *sinode;
464        struct jabber_buddy *bud;
465
466        /* who knows how many bits the future holds :) */
467        char filesizestr[ 1 + ( int ) ( 0.301029995663981198f * sizeof( size_t ) * 8 ) ];
468
469        const char *methods[] = 
470        {       
471                XMLNS_BYTESTREAMS,
472                //XMLNS_IBB,
473                NULL 
474        };
475        const char **m;
476        char *s;
477
478        /* Maybe we should hash this? */
479        tf->sid = g_strdup_printf( "BitlBeeJabberSID%d", tf->ft->local_id );
480       
481        if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
482                bud = jabber_buddy_by_ext_jid( ic, who, 0 );
483        else
484                bud = jabber_buddy_by_jid( ic, who, 0 );
485
486        /* start with the SI tag */
487        sinode = xt_new_node( "si", NULL, NULL );
488        xt_add_attr( sinode, "xmlns", XMLNS_SI );
489        xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER );
490        xt_add_attr( sinode, "id", tf->sid );
491
492/*      if( mimetype )
493                xt_add_attr( node, "mime-type", mimetype ); */
494
495        /* now the file tag */
496/*      if( desc )
497                node = xt_new_node( "desc", descr, NULL ); */
498        node = xt_new_node( "range", NULL, NULL );
499
500        sprintf( filesizestr, "%zd", tf->ft->file_size );
501        node = xt_new_node( "file", NULL, node );
502        xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER );
503        xt_add_attr( node, "name", tf->ft->file_name );
504        xt_add_attr( node, "size", filesizestr );
505/*      if (hash)
506                xt_add_attr( node, "hash", hash );
507        if (date)
508                xt_add_attr( node, "date", date ); */
509
510        xt_add_child( sinode, node );
511
512        /* and finally the feature tag */
513        node = xt_new_node( "field", NULL, NULL );
514        xt_add_attr( node, "var", "stream-method" );
515        xt_add_attr( node, "type", "list-single" );
516
517        for ( m = methods ; *m ; m ++ )
518                xt_add_child( node, xt_new_node( "option", NULL, xt_new_node( "value", (char *)*m, NULL ) ) );
519
520        node = xt_new_node( "x", NULL, node );
521        xt_add_attr( node, "xmlns", XMLNS_XDATA );
522        xt_add_attr( node, "type", "form" );
523
524        node = xt_new_node( "feature", NULL, node );
525        xt_add_attr( node, "xmlns", XMLNS_FEATURE );
526
527        xt_add_child( sinode, node );
528
529        /* and we are there... */
530        node = jabber_make_packet( "iq", "set", bud ? bud->full_jid : who, sinode );
531        jabber_cache_add( ic, node, jabber_si_handle_response );
532        tf->iq_id = g_strdup( xt_find_attr( node, "id" ) );
533       
534        return jabber_write_packet( ic, node );
535}
Note: See TracBrowser for help on using the repository browser.