source: protocols/jabber/iq.c @ 259edd4

Last change on this file since 259edd4 was 259edd4, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-12T17:48:58Z

Special message when the XMPP session is ended because of a concurrent
login, and now sending proper error responses to IQ packets we can't
handle.

  • Property mode set to 100644
File size: 10.5 KB
RevLine 
[f06894d]1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Jabber module - IQ packets                                               *
5*                                                                           *
6*  Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net>                   *
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
26xt_status jabber_pkt_iq( struct xt_node *node, gpointer data )
27{
[21167d2]28        struct gaim_connection *gc = data;
29        struct jabber_data *jd = gc->proto_data;
[58b5f62]30        struct xt_node *c, *reply = NULL;
[861c199]31        char *type, *s;
[259edd4]32        int st, pack = 1;
[21167d2]33       
[70f6aab8]34        type = xt_find_attr( node, "type" );
[f06894d]35       
[70f6aab8]36        if( !type )
[861c199]37        {
[58b5f62]38                hide_login_progress_error( gc, "Received IQ packet without type." );
[861c199]39                signoff( gc );
40                return XT_ABORT;
41        }
[21167d2]42       
[58b5f62]43        if( strcmp( type, "result" ) == 0 || strcmp( type, "error" ) == 0 )
[861c199]44        {
45                struct jabber_cache_entry *entry;
46               
[58b5f62]47                if( ( s = xt_find_attr( node, "id" ) ) == NULL )
48                {
49                        /* Silently ignore it, without an ID we don't know
50                           how to handle the packet, but it doesn't have
51                           to be a serious problem. */
52                        return XT_HANDLED;
53                }
54               
[861c199]55                entry = g_hash_table_lookup( jd->node_cache, s );
56               
57                if( entry == NULL )
58                        serv_got_crap( gc, "WARNING: Received IQ %s packet with unknown/expired ID %s!", type, s );
59                else if( entry->func )
60                        return entry->func( gc, node, entry->node );
61        }
[58b5f62]62        else if( strcmp( type, "get" ) == 0 )
63        {
64                if( !( c = xt_find_node( node->children, "query" ) ) ||
65                    !( s = xt_find_attr( c, "xmlns" ) ) )
66                {
67                        serv_got_crap( gc, "WARNING: Received incomplete IQ-get packet" );
68                        return XT_HANDLED;
69                }
70               
71                reply = xt_new_node( "query", NULL, NULL );
72                xt_add_attr( reply, "xmlns", s );
73               
74                /* Of course this is a very essential query to support. ;-) */
75                if( strcmp( s, "jabber:iq:version" ) == 0 )
76                {
77                        xt_add_child( reply, xt_new_node( "name", "BitlBee", NULL ) );
78                        xt_add_child( reply, xt_new_node( "version", BITLBEE_VERSION, NULL ) );
79                        xt_add_child( reply, xt_new_node( "os", ARCH, NULL ) );
80                }
81                else if( strcmp( s, "http://jabber.org/protocol/disco#info" ) == 0 )
82                {
83                        c = xt_new_node( "identity", NULL, NULL );
84                        xt_add_attr( c, "category", "client" );
85                        xt_add_attr( c, "type", "pc" );
86                        xt_add_attr( c, "name", "BitlBee" );
87                        xt_add_child( reply, c );
88                       
89                        c = xt_new_node( "feature", NULL, NULL );
90                        xt_add_attr( c, "var", "jabber:iq:version" );
91                        xt_add_child( reply, c );
92                       
93                        c = xt_new_node( "feature", NULL, NULL );
94                        xt_add_attr( c, "var", "http://jabber.org/protocol/chatstates" );
95                        xt_add_child( reply, c );
96                       
97                        /* Later this can be useful to announce things like
98                           MUC support. */
99                }
100                else
101                {
102                        xt_free_node( reply );
[259edd4]103                        reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel" );
104                        pack = 0;
[58b5f62]105                }
[259edd4]106        }
107        else if( strcmp( type, "set" ) == 0 )
108        {
109                xt_free_node( reply );
110                reply = jabber_make_error_packet( node, "feature-not-implemented", "cancel" );
111                pack = 0;
112        }
113       
114        /* If we recognized the xmlns and managed to generate a reply,
115           finish and send it. */
116        if( reply )
117        {
118                /* Normally we still have to pack it into an iq-result
119                   packet, but for errors, for example, we don't. */
120                if( pack )
[58b5f62]121                {
122                        reply = jabber_make_packet( "iq", "result", xt_find_attr( node, "from" ), reply );
123                        if( ( s = xt_find_attr( node, "id" ) ) )
124                                xt_add_attr( reply, "id", s );
125                }
[259edd4]126               
127                st = jabber_write_packet( gc, reply );
128                xt_free_node( reply );
129                if( !st )
130                        return XT_ABORT;
[58b5f62]131        }
[70f6aab8]132       
[861c199]133        return XT_HANDLED;
134}
135
136static xt_status jabber_do_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig );
137static xt_status jabber_finish_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig );
138
139int jabber_init_iq_auth( struct gaim_connection *gc )
140{
141        struct jabber_data *jd = gc->proto_data;
142        struct xt_node *node;
143        int st;
[fe7a554]144       
[861c199]145        node = xt_new_node( "query", NULL, xt_new_node( "username", jd->username, NULL ) );
146        xt_add_attr( node, "xmlns", "jabber:iq:auth" );
147        node = jabber_make_packet( "iq", "get", NULL, node );
148       
149        jabber_cache_add( gc, node, jabber_do_iq_auth );
150        st = jabber_write_packet( gc, node );
151       
152        return st;
153}
154
155static xt_status jabber_do_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig )
156{
157        struct jabber_data *jd = gc->proto_data;
158        struct xt_node *reply, *query;
159        xt_status st;
160        char *s;
161       
162        query = xt_find_node( node->children, "query" );
163       
164        /* Time to authenticate ourselves! */
165        reply = xt_new_node( "query", NULL, NULL );
166        xt_add_attr( reply, "xmlns", "jabber:iq:auth" );
167        xt_add_child( reply, xt_new_node( "username", jd->username, NULL ) );
168        xt_add_child( reply, xt_new_node( "resource", set_getstr( &gc->acc->set, "resource" ), NULL ) );
169       
170        if( xt_find_node( query->children, "digest" ) && ( s = xt_find_attr( jd->xt->root, "id" ) ) )
[21167d2]171        {
[861c199]172                /* We can do digest authentication, it seems, and of
173                   course we prefer that. */
174                SHA_CTX sha;
175                char hash_hex[40];
176                unsigned char hash[20];
177                int i;
[21167d2]178               
[861c199]179                shaInit( &sha );
180                shaUpdate( &sha, (unsigned char*) s, strlen( s ) );
181                shaUpdate( &sha, (unsigned char*) gc->acc->pass, strlen( gc->acc->pass ) );
182                shaFinal( &sha, hash );
[21167d2]183               
[861c199]184                for( i = 0; i < 20; i ++ )
185                        sprintf( hash_hex + i * 2, "%02x", hash[i] );
[21167d2]186               
[861c199]187                xt_add_child( reply, xt_new_node( "digest", hash_hex, NULL ) );
[21167d2]188        }
[861c199]189        else if( xt_find_node( query->children, "password" ) )
[0b4a0db]190        {
[861c199]191                /* We'll have to stick with plaintext. Let's hope we're using SSL/TLS... */
192                xt_add_child( reply, xt_new_node( "password", gc->acc->pass, NULL ) );
[0b4a0db]193        }
[861c199]194        else
[70f6aab8]195        {
[861c199]196                xt_free_node( reply );
[995913b]197               
[861c199]198                hide_login_progress( gc, "Can't find suitable authentication method" );
199                signoff( gc );
200                return XT_ABORT;
[70f6aab8]201        }
[861c199]202       
203        reply = jabber_make_packet( "iq", "set", NULL, reply );
204        jabber_cache_add( gc, reply, jabber_finish_iq_auth );
205        st = jabber_write_packet( gc, reply );
206       
207        return st ? XT_HANDLED : XT_ABORT;
208}
209
210static xt_status jabber_finish_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig )
211{
212        char *type = xt_find_attr( node, "type" );
213        struct jabber_data *jd = gc->proto_data;
214       
215        if( strcmp( type, "error" ) == 0 )
[70f6aab8]216        {
[861c199]217                hide_login_progress( gc, "Authentication failure" );
218                signoff( gc );
219                return XT_ABORT;
220        }
221        else if( strcmp( type, "result" ) == 0 )
222        {
223                /* This happens when we just successfully authenticated the
224                   old (non-SASL) way. */
225                jd->flags |= JFLAG_AUTHENTICATED;
226                if( !jabber_get_roster( gc ) )
[70f6aab8]227                        return XT_ABORT;
228        }
[f06894d]229       
230        return XT_HANDLED;
231}
232
[861c199]233xt_status jabber_pkt_bind_sess( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig )
[21167d2]234{
235        struct jabber_data *jd = gc->proto_data;
[861c199]236        struct xt_node *c;
237        char *s;
[21167d2]238       
[861c199]239        if( ( c = xt_find_node( node->children, "bind" ) ) )
240        {
241                c = xt_find_node( c->children, "jid" );
242                if( c && c->text_len && ( s = strchr( c->text, '/' ) ) &&
243                    strcmp( s + 1, set_getstr( &gc->acc->set, "resource" ) ) != 0 )
244                        serv_got_crap( gc, "Server changed session resource string to `%s'", s + 1 );
245               
246                jd->flags &= ~JFLAG_WAIT_BIND;
247        }
248        else
249        {
250                jd->flags &= ~JFLAG_WAIT_SESSION;
251        }
[21167d2]252       
[861c199]253        if( ( jd->flags & ( JFLAG_WAIT_BIND | JFLAG_WAIT_SESSION ) ) == 0 )
254        {
255                if( !jabber_get_roster( gc ) )
256                        return XT_ABORT;
257        }
[21167d2]258       
[861c199]259        return XT_HANDLED;
[21167d2]260}
[70f6aab8]261
[861c199]262static xt_status jabber_parse_roster( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig );
263
[70f6aab8]264int jabber_get_roster( struct gaim_connection *gc )
265{
266        struct xt_node *node;
267        int st;
268       
269        set_login_progress( gc, 1, "Authenticated, requesting buddy list" );
270       
271        node = xt_new_node( "query", NULL, NULL );
272        xt_add_attr( node, "xmlns", "jabber:iq:roster" );
273        node = jabber_make_packet( "iq", "get", NULL, node );
274       
[861c199]275        jabber_cache_add( gc, node, jabber_parse_roster );
[70f6aab8]276        st = jabber_write_packet( gc, node );
277       
278        return st;
279}
[cfbb3a6]280
[861c199]281static xt_status jabber_parse_roster( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig )
282{
283        struct xt_node *query, *c;
284       
285        query = xt_find_node( node->children, "query" );
286       
287        c = query->children;
288        while( ( c = xt_find_node( c, "item" ) ) )
289        {
290                char *jid = xt_find_attr( c, "jid" );
291                char *name = xt_find_attr( c, "name" );
292                char *sub = xt_find_attr( c, "subscription" );
293               
294                if( jid && sub && ( strcmp( sub, "both" ) == 0 || strcmp( sub, "to" ) == 0 ) )
295                        add_buddy( gc, NULL, jid, name );
296               
297                c = c->next;
298        }
299       
300        account_online( gc );
301       
302        return XT_HANDLED;
303}
304
[cfbb3a6]305int jabber_add_to_roster( struct gaim_connection *gc, char *handle, char *name )
306{
307        struct xt_node *node;
308        int st;
309       
310        /* Build the item entry */
311        node = xt_new_node( "item", NULL, NULL );
312        xt_add_attr( node, "jid", handle );
313        if( name )
314                xt_add_attr( node, "name", name );
315       
316        /* And pack it into a roster-add packet */
317        node = xt_new_node( "query", NULL, node );
318        xt_add_attr( node, "xmlns", "jabber:iq:roster" );
319        node = jabber_make_packet( "iq", "set", NULL, node );
320       
321        st = jabber_write_packet( gc, node );
322       
323        xt_free_node( node );
324        return st;
325}
326
327int jabber_remove_from_roster( struct gaim_connection *gc, char *handle )
328{
329        struct xt_node *node;
330        int st;
331       
332        /* Build the item entry */
333        node = xt_new_node( "item", NULL, NULL );
334        xt_add_attr( node, "jid", handle );
335        xt_add_attr( node, "subscription", "remove" );
336       
337        /* And pack it into a roster-add packet */
338        node = xt_new_node( "query", NULL, node );
339        xt_add_attr( node, "xmlns", "jabber:iq:roster" );
340        node = jabber_make_packet( "iq", "set", NULL, node );
341       
342        st = jabber_write_packet( gc, node );
343       
344        xt_free_node( node );
345        return st;
346}
Note: See TracBrowser for help on using the repository browser.