source: protocols/jabber/si.c @ 78d254f1

Last change on this file since 78d254f1 was 78d254f1, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-03-18T00:07:16Z

More small fixes. (NULL derefs and s/close/disconnect/)

  • Property mode set to 100644
File size: 16.5 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                disconnect( 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->ft, "Buddy's client doesn't feature file transfers" );
94        else if( !foundbt )
95                imcb_file_canceled( tf->ft, "Buddy's client doesn't feature byte streams (required)" );
96        else if( !foundsi )
97                imcb_file_canceled( 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->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( tf->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;
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            !( strcmp( xt_find_attr( sinode, "profile" ), XMLNS_FILETRANSFER ) == 0     ) ||
230            !( d                = xt_find_node( sinode->children, "file" )              ) ||
231            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FILETRANSFER ) == 0            ) ||
232            !( name             = xt_find_attr( d, "name" )                             ) ||
233            !( size_s           = xt_find_attr( d, "size" )                             ) ||
234            !( 1               == sscanf( size_s, "%lld", &size )                       ) ||
235            !( d                = xt_find_node( sinode->children, "feature" )           ) ||
236            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FEATURE ) == 0                 ) ||
237            !( d                = xt_find_node( d->children, "x" )                      ) ||
238            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_XDATA ) == 0                   ) ||
239            !( strcmp( xt_find_attr( d, "type" ), "form" ) == 0                         ) ||
240            !( d                = xt_find_node( d->children, "field" )                  ) ||
241            !( strcmp( xt_find_attr( d, "var" ), "stream-method" ) == 0                 ) )
242        {
243                imcb_log( ic, "WARNING: Received incomplete Stream Initiation request" );
244        } else
245        {
246                /* Check if we support one of the options */
247
248                c = d->children;
249                while( ( c = xt_find_node( c, "option" ) ) )
250                        if(     ( d = xt_find_node( c->children, "value" ) ) &&
251                                ( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) )
252                        {
253                                requestok = TRUE;
254                                break;
255                        }
256
257                if ( !requestok )
258                        imcb_log( ic, "WARNING: Unsupported file transfer request from %s", ini_jid);
259        }
260       
261        if ( requestok )
262        {
263                /* Figure out who the transfer should come frome... */
264
265                if( ( s = strchr( ini_jid, '/' ) ) )
266                {
267                        if( ( bud = jabber_buddy_by_jid( ic, ini_jid, GET_BUDDY_EXACT ) ) )
268                        {
269                                bud->last_act = time( NULL );
270                                ext_jid = bud->ext_jid ? : bud->bare_jid;
271                        }
272                        else
273                                *s = 0; /* We need to generate a bare JID now. */
274                }
275
276                if( !( ft = imcb_file_send_start( ic, ext_jid, name, size ) ) )
277                { 
278                        imcb_log( ic, "WARNING: Error handling transfer request from %s", ini_jid);
279                        requestok = FALSE;
280                }
281
282                *s = '/';
283        }
284
285        if ( !requestok )
286        { 
287                reply = jabber_make_error_packet( node, "item-not-found", "cancel", NULL );
288                if (!jabber_write_packet( ic, reply ))
289                        imcb_log( ic, "WARNING: Error generating reply to file transfer request" );
290                xt_free_node( reply );
291                return XT_HANDLED;
292        }
293
294        /* Request is fine. */
295
296        tf = g_new0( struct jabber_transfer, 1 );
297
298        tf->ini_jid = g_strdup( ini_jid );
299        tf->tgt_jid = g_strdup( tgt_jid );
300        tf->iq_id = g_strdup( iq_id );
301        tf->sid = g_strdup( sid );
302        tf->ic = ic;
303        tf->ft = ft;
304        tf->fd = -1;
305        tf->ft->data = tf;
306        tf->ft->accept = jabber_si_answer_request;
307        tf->ft->free = jabber_si_free_transfer;
308        tf->ft->canceled = jabber_si_canceled;
309
310        jd->filetransfers = g_slist_prepend( jd->filetransfers, tf );
311
312        return XT_HANDLED;
313}
314
315/*
316 * imc called the accept callback which probably means that the user accepted this file transfer.
317 * We send our response to the initiator.
318 * In the next step, the initiator will send us a request for the given stream type.
319 * (currently that can only be a SOCKS5 bytestream)
320 */
321void jabber_si_answer_request( file_transfer_t *ft ) {
322        struct jabber_transfer *tf = ft->data;
323        struct xt_node *node, *sinode, *reply;
324
325        /* generate response, start with the SI tag */
326        sinode = xt_new_node( "si", NULL, NULL );
327        xt_add_attr( sinode, "xmlns", XMLNS_SI );
328        xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER );
329        xt_add_attr( sinode, "id", tf->sid );
330
331        /* now the file tag */
332        node = xt_new_node( "file", NULL, NULL );
333        xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER );
334
335        xt_add_child( sinode, node );
336
337        /* and finally the feature tag */
338        node = xt_new_node( "field", NULL, NULL );
339        xt_add_attr( node, "var", "stream-method" );
340        xt_add_attr( node, "type", "list-single" );
341
342        /* Currently all we can do. One could also implement in-band (IBB) */
343        xt_add_child( node, xt_new_node( "value", XMLNS_BYTESTREAMS, NULL ) );
344
345        node = xt_new_node( "x", NULL, node );
346        xt_add_attr( node, "xmlns", XMLNS_XDATA );
347        xt_add_attr( node, "type", "submit" );
348
349        node = xt_new_node( "feature", NULL, node );
350        xt_add_attr( node, "xmlns", XMLNS_FEATURE );
351
352        xt_add_child( sinode, node );
353
354        reply = jabber_make_packet( "iq", "result", tf->ini_jid, sinode );
355        xt_add_attr( reply, "id", tf->iq_id );
356       
357        if( !jabber_write_packet( tf->ic, reply ) )
358                imcb_log( tf->ic, "WARNING: Error generating reply to file transfer request" );
359        else
360                tf->accepted = TRUE;
361        xt_free_node( reply );
362}
363
364static xt_status jabber_si_handle_response(struct im_connection *ic, struct xt_node *node, struct xt_node *orig )
365{
366        struct xt_node *c, *d;
367        char *ini_jid, *tgt_jid, *iq_id;
368        GSList *tflist;
369        struct jabber_transfer *tf=NULL;
370        struct jabber_data *jd = ic->proto_data;
371
372        if( !( tgt_jid = xt_find_attr( node, "from" ) ) ||
373            !( ini_jid = xt_find_attr( node, "to" ) ) )
374        {
375                imcb_log( ic, "Invalid SI response from=%s to=%s", tgt_jid, ini_jid );
376                return XT_HANDLED;
377        }
378       
379        /* All this means we expect something like this: ( I think )
380         * <iq from=... to=... id=...>
381         *      <si xmlns=si>
382         *      [       <file xmlns=ft/>    ] <-- not neccessary
383         *              <feature xmlns=feature>
384         *                      <x xmlns=xdata type=submit>
385         *                              <field var=stream-method>
386         *                                      <value>
387         */
388        if( !( tgt_jid = xt_find_attr( node, "from" ) ) ||
389            !( ini_jid = xt_find_attr( node, "to" ) ) ||
390            !( iq_id   = xt_find_attr( node, "id" ) ) ||
391            !( c = xt_find_node( node->children, "si" ) ) ||
392            !( strcmp( xt_find_attr( c, "xmlns" ), XMLNS_SI ) == 0 ) ||
393/*          !( d = xt_find_node( c->children, "file" ) ) ||
394            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FILETRANSFER ) == 0 ) || */
395            !( d = xt_find_node( c->children, "feature" ) ) ||
396            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_FEATURE ) == 0 ) ||
397            !( d = xt_find_node( d->children, "x" ) ) ||
398            !( strcmp( xt_find_attr( d, "xmlns" ), XMLNS_XDATA ) == 0 ) ||
399            !( strcmp( xt_find_attr( d, "type" ), "submit" ) == 0 ) ||
400            !( d = xt_find_node( d->children, "field" ) ) ||
401            !( strcmp( xt_find_attr( d, "var" ), "stream-method" ) == 0 ) ||
402            !( d = xt_find_node( d->children, "value" ) ) )
403        {
404                imcb_log( ic, "WARNING: Received incomplete Stream Initiation response" );
405                return XT_HANDLED;
406        }
407
408        if( !( strcmp( d->text, XMLNS_BYTESTREAMS ) == 0 ) ) { 
409                /* since we should only have advertised what we can do and the peer should
410                 * only have chosen what we offered, this should never happen */
411                imcb_log( ic, "WARNING: Received invalid Stream Initiation response, method %s", d->text );
412                       
413                return XT_HANDLED;
414        }
415       
416        /* Let's see if we can find out what this bytestream should be for... */
417
418        for( tflist = jd->filetransfers ; tflist; tflist = g_slist_next(tflist) )
419        {
420                struct jabber_transfer *tft = tflist->data;
421                if( ( strcmp( tft->iq_id, iq_id ) == 0 ) )
422                {
423                        tf = tft;
424                        break;
425                }
426        }
427
428        if (!tf) 
429        {
430                imcb_log( ic, "WARNING: Received bytestream request from %s that doesn't match an SI request", ini_jid );
431                return XT_HANDLED;
432        }
433
434        tf->ini_jid = g_strdup( ini_jid );
435        tf->tgt_jid = g_strdup( tgt_jid );
436
437        imcb_log( ic, "File %s: %s accepted the transfer!", tf->ft->file_name, tgt_jid );
438
439        jabber_bs_send_start( tf );
440
441        return XT_HANDLED;
442}
443
444int jabber_si_send_request(struct im_connection *ic, char *who, struct jabber_transfer *tf )
445{
446        struct xt_node *node, *sinode;
447        struct jabber_buddy *bud;
448
449        /* who knows how many bits the future holds :) */
450        char filesizestr[ 1 + ( int ) ( 0.301029995663981198f * sizeof( size_t ) * 8 ) ];
451
452        const char *methods[] = 
453        {       
454                XMLNS_BYTESTREAMS,
455                //XMLNS_IBB,
456                NULL 
457        };
458        const char **m;
459        char *s;
460
461        /* Maybe we should hash this? */
462        tf->sid = g_strdup_printf( "BitlBeeJabberSID%d", tf->ft->local_id );
463       
464        if( ( s = strchr( who, '=' ) ) && jabber_chat_by_jid( ic, s + 1 ) )
465                bud = jabber_buddy_by_ext_jid( ic, who, 0 );
466        else
467                bud = jabber_buddy_by_jid( ic, who, 0 );
468
469        /* start with the SI tag */
470        sinode = xt_new_node( "si", NULL, NULL );
471        xt_add_attr( sinode, "xmlns", XMLNS_SI );
472        xt_add_attr( sinode, "profile", XMLNS_FILETRANSFER );
473        xt_add_attr( sinode, "id", tf->sid );
474
475/*      if( mimetype )
476                xt_add_attr( node, "mime-type", mimetype ); */
477
478        /* now the file tag */
479/*      if( desc )
480                node = xt_new_node( "desc", descr, NULL ); */
481        node = xt_new_node( "range", NULL, NULL );
482
483        sprintf( filesizestr, "%zd", tf->ft->file_size );
484        node = xt_new_node( "file", NULL, node );
485        xt_add_attr( node, "xmlns", XMLNS_FILETRANSFER );
486        xt_add_attr( node, "name", tf->ft->file_name );
487        xt_add_attr( node, "size", filesizestr );
488/*      if (hash)
489                xt_add_attr( node, "hash", hash );
490        if (date)
491                xt_add_attr( node, "date", date ); */
492
493        xt_add_child( sinode, node );
494
495        /* and finally the feature tag */
496        node = xt_new_node( "field", NULL, NULL );
497        xt_add_attr( node, "var", "stream-method" );
498        xt_add_attr( node, "type", "list-single" );
499
500        for ( m = methods ; *m ; m ++ )
501                xt_add_child( node, xt_new_node( "option", NULL, xt_new_node( "value", (char *)*m, NULL ) ) );
502
503        node = xt_new_node( "x", NULL, node );
504        xt_add_attr( node, "xmlns", XMLNS_XDATA );
505        xt_add_attr( node, "type", "form" );
506
507        node = xt_new_node( "feature", NULL, node );
508        xt_add_attr( node, "xmlns", XMLNS_FEATURE );
509
510        xt_add_child( sinode, node );
511
512        /* and we are there... */
513        node = jabber_make_packet( "iq", "set", bud ? bud->full_jid : who, sinode );
514        jabber_cache_add( ic, node, jabber_si_handle_response );
515        tf->iq_id = g_strdup( xt_find_attr( node, "id" ) );
516       
517        return jabber_write_packet( ic, node );
518}
Note: See TracBrowser for help on using the repository browser.