source: protocols/jabber/iq.c @ 58b5f62

Last change on this file since 58b5f62 was 58b5f62, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-11T08:45:45Z

Handling of some basic IQ-get packets.

  • Property mode set to 100644
File size: 10.1 KB
Line 
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{
28        struct gaim_connection *gc = data;
29        struct jabber_data *jd = gc->proto_data;
30        struct xt_node *c, *reply = NULL;
31        char *type, *s;
32        int st;
33       
34        type = xt_find_attr( node, "type" );
35       
36        if( !type )
37        {
38                hide_login_progress_error( gc, "Received IQ packet without type." );
39                signoff( gc );
40                return XT_ABORT;
41        }
42       
43        if( strcmp( type, "result" ) == 0 || strcmp( type, "error" ) == 0 )
44        {
45                struct jabber_cache_entry *entry;
46               
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               
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        }
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 );
103                        reply = NULL;
104                }
105               
106                /* If we recognized the xmlns and managed to generate a reply,
107                   finish and send it. */
108                if( reply )
109                {
110                        reply = jabber_make_packet( "iq", "result", xt_find_attr( node, "from" ), reply );
111                        if( ( s = xt_find_attr( node, "id" ) ) )
112                                xt_add_attr( reply, "id", s );
113                       
114                        st = jabber_write_packet( gc, reply );
115                        xt_free_node( reply );
116                        if( !st )
117                                return XT_ABORT;
118                }
119        }
120       
121        return XT_HANDLED;
122}
123
124static xt_status jabber_do_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig );
125static xt_status jabber_finish_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig );
126
127int jabber_init_iq_auth( struct gaim_connection *gc )
128{
129        struct jabber_data *jd = gc->proto_data;
130        struct xt_node *node;
131        int st;
132       
133        node = xt_new_node( "query", NULL, xt_new_node( "username", jd->username, NULL ) );
134        xt_add_attr( node, "xmlns", "jabber:iq:auth" );
135        node = jabber_make_packet( "iq", "get", NULL, node );
136       
137        jabber_cache_add( gc, node, jabber_do_iq_auth );
138        st = jabber_write_packet( gc, node );
139       
140        return st;
141}
142
143static xt_status jabber_do_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig )
144{
145        struct jabber_data *jd = gc->proto_data;
146        struct xt_node *reply, *query;
147        xt_status st;
148        char *s;
149       
150        query = xt_find_node( node->children, "query" );
151       
152        /* Time to authenticate ourselves! */
153        reply = xt_new_node( "query", NULL, NULL );
154        xt_add_attr( reply, "xmlns", "jabber:iq:auth" );
155        xt_add_child( reply, xt_new_node( "username", jd->username, NULL ) );
156        xt_add_child( reply, xt_new_node( "resource", set_getstr( &gc->acc->set, "resource" ), NULL ) );
157       
158        if( xt_find_node( query->children, "digest" ) && ( s = xt_find_attr( jd->xt->root, "id" ) ) )
159        {
160                /* We can do digest authentication, it seems, and of
161                   course we prefer that. */
162                SHA_CTX sha;
163                char hash_hex[40];
164                unsigned char hash[20];
165                int i;
166               
167                shaInit( &sha );
168                shaUpdate( &sha, (unsigned char*) s, strlen( s ) );
169                shaUpdate( &sha, (unsigned char*) gc->acc->pass, strlen( gc->acc->pass ) );
170                shaFinal( &sha, hash );
171               
172                for( i = 0; i < 20; i ++ )
173                        sprintf( hash_hex + i * 2, "%02x", hash[i] );
174               
175                xt_add_child( reply, xt_new_node( "digest", hash_hex, NULL ) );
176        }
177        else if( xt_find_node( query->children, "password" ) )
178        {
179                /* We'll have to stick with plaintext. Let's hope we're using SSL/TLS... */
180                xt_add_child( reply, xt_new_node( "password", gc->acc->pass, NULL ) );
181        }
182        else
183        {
184                xt_free_node( reply );
185               
186                hide_login_progress( gc, "Can't find suitable authentication method" );
187                signoff( gc );
188                return XT_ABORT;
189        }
190       
191        reply = jabber_make_packet( "iq", "set", NULL, reply );
192        jabber_cache_add( gc, reply, jabber_finish_iq_auth );
193        st = jabber_write_packet( gc, reply );
194       
195        return st ? XT_HANDLED : XT_ABORT;
196}
197
198static xt_status jabber_finish_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig )
199{
200        char *type = xt_find_attr( node, "type" );
201        struct jabber_data *jd = gc->proto_data;
202       
203        if( strcmp( type, "error" ) == 0 )
204        {
205                hide_login_progress( gc, "Authentication failure" );
206                signoff( gc );
207                return XT_ABORT;
208        }
209        else if( strcmp( type, "result" ) == 0 )
210        {
211                /* This happens when we just successfully authenticated the
212                   old (non-SASL) way. */
213                jd->flags |= JFLAG_AUTHENTICATED;
214                if( !jabber_get_roster( gc ) )
215                        return XT_ABORT;
216        }
217       
218        return XT_HANDLED;
219}
220
221xt_status jabber_pkt_bind_sess( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig )
222{
223        struct jabber_data *jd = gc->proto_data;
224        struct xt_node *c;
225        char *s;
226       
227        if( ( c = xt_find_node( node->children, "bind" ) ) )
228        {
229                c = xt_find_node( c->children, "jid" );
230                if( c && c->text_len && ( s = strchr( c->text, '/' ) ) &&
231                    strcmp( s + 1, set_getstr( &gc->acc->set, "resource" ) ) != 0 )
232                        serv_got_crap( gc, "Server changed session resource string to `%s'", s + 1 );
233               
234                jd->flags &= ~JFLAG_WAIT_BIND;
235        }
236        else
237        {
238                jd->flags &= ~JFLAG_WAIT_SESSION;
239        }
240       
241        if( ( jd->flags & ( JFLAG_WAIT_BIND | JFLAG_WAIT_SESSION ) ) == 0 )
242        {
243                if( !jabber_get_roster( gc ) )
244                        return XT_ABORT;
245        }
246       
247        return XT_HANDLED;
248}
249
250static xt_status jabber_parse_roster( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig );
251
252int jabber_get_roster( struct gaim_connection *gc )
253{
254        struct xt_node *node;
255        int st;
256       
257        set_login_progress( gc, 1, "Authenticated, requesting buddy list" );
258       
259        node = xt_new_node( "query", NULL, NULL );
260        xt_add_attr( node, "xmlns", "jabber:iq:roster" );
261        node = jabber_make_packet( "iq", "get", NULL, node );
262       
263        jabber_cache_add( gc, node, jabber_parse_roster );
264        st = jabber_write_packet( gc, node );
265       
266        return st;
267}
268
269static xt_status jabber_parse_roster( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig )
270{
271        struct xt_node *query, *c;
272       
273        query = xt_find_node( node->children, "query" );
274       
275        c = query->children;
276        while( ( c = xt_find_node( c, "item" ) ) )
277        {
278                char *jid = xt_find_attr( c, "jid" );
279                char *name = xt_find_attr( c, "name" );
280                char *sub = xt_find_attr( c, "subscription" );
281               
282                if( jid && sub && ( strcmp( sub, "both" ) == 0 || strcmp( sub, "to" ) == 0 ) )
283                        add_buddy( gc, NULL, jid, name );
284               
285                c = c->next;
286        }
287       
288        account_online( gc );
289       
290        return XT_HANDLED;
291}
292
293int jabber_add_to_roster( struct gaim_connection *gc, char *handle, char *name )
294{
295        struct xt_node *node;
296        int st;
297       
298        /* Build the item entry */
299        node = xt_new_node( "item", NULL, NULL );
300        xt_add_attr( node, "jid", handle );
301        if( name )
302                xt_add_attr( node, "name", name );
303       
304        /* And pack it into a roster-add packet */
305        node = xt_new_node( "query", NULL, node );
306        xt_add_attr( node, "xmlns", "jabber:iq:roster" );
307        node = jabber_make_packet( "iq", "set", NULL, node );
308       
309        st = jabber_write_packet( gc, node );
310       
311        xt_free_node( node );
312        return st;
313}
314
315int jabber_remove_from_roster( struct gaim_connection *gc, char *handle )
316{
317        struct xt_node *node;
318        int st;
319       
320        /* Build the item entry */
321        node = xt_new_node( "item", NULL, NULL );
322        xt_add_attr( node, "jid", handle );
323        xt_add_attr( node, "subscription", "remove" );
324       
325        /* And pack it into a roster-add packet */
326        node = xt_new_node( "query", NULL, node );
327        xt_add_attr( node, "xmlns", "jabber:iq:roster" );
328        node = jabber_make_packet( "iq", "set", NULL, node );
329       
330        st = jabber_write_packet( gc, node );
331       
332        xt_free_node( node );
333        return st;
334}
Note: See TracBrowser for help on using the repository browser.