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 | |
---|
26 | /* |
---|
27 | <iq xmlns="jabber:client" id="BeeX00000001" type="result"><query |
---|
28 | xmlns="jabber:iq:auth"><username>wilmer</username><resource/><password/><digest/> |
---|
29 | <sequence>499</sequence><token>450D1FFD</token></query></iq> |
---|
30 | */ |
---|
31 | |
---|
32 | xt_status jabber_pkt_iq( struct xt_node *node, gpointer data ) |
---|
33 | { |
---|
34 | struct gaim_connection *gc = data; |
---|
35 | struct jabber_data *jd = gc->proto_data; |
---|
36 | struct xt_node *query, *reply = NULL; |
---|
37 | char *s; |
---|
38 | int st; |
---|
39 | |
---|
40 | query = xt_find_node( node->children, "query" ); |
---|
41 | |
---|
42 | if( !query ) |
---|
43 | return XT_HANDLED; /* Ignore it for now, don't know what's best... */ |
---|
44 | |
---|
45 | if( ( s = xt_find_attr( query, "xmlns" ) ) && strcmp( s, "jabber:iq:auth" ) == 0 ) |
---|
46 | { |
---|
47 | /* Time to authenticate ourselves! */ |
---|
48 | reply = xt_new_node( "query", NULL, NULL ); |
---|
49 | xt_add_attr( reply, "xmlns", "jabber:iq:auth" ); |
---|
50 | xt_add_child( reply, xt_new_node( "username", jd->username, NULL ) ); |
---|
51 | xt_add_child( reply, xt_new_node( "resource", set_getstr( &gc->acc->set, "resource" ), NULL ) ); |
---|
52 | |
---|
53 | if( xt_find_node( query->children, "digest" ) && ( s = xt_find_attr( jd->xt->root, "id" ) ) ) |
---|
54 | { |
---|
55 | /* We can do digest authentication, it seems, and of |
---|
56 | course we prefer that. */ |
---|
57 | SHA_CTX sha; |
---|
58 | char hash_hex[40]; |
---|
59 | unsigned char hash[20]; |
---|
60 | int i; |
---|
61 | |
---|
62 | shaInit( &sha ); |
---|
63 | shaUpdate( &sha, (unsigned char*) s, strlen( s ) ); |
---|
64 | shaUpdate( &sha, (unsigned char*) gc->acc->pass, strlen( gc->acc->pass ) ); |
---|
65 | shaFinal( &sha, hash ); |
---|
66 | |
---|
67 | for( i = 0; i < 20; i ++ ) |
---|
68 | sprintf( hash_hex + i * 2, "%02x", hash[i] ); |
---|
69 | |
---|
70 | xt_add_child( reply, xt_new_node( "digest", hash_hex, NULL ) ); |
---|
71 | } |
---|
72 | else if( xt_find_node( query->children, "password" ) ) |
---|
73 | { |
---|
74 | /* We'll have to stick with plaintext. Let's hope we're using SSL/TLS... */ |
---|
75 | xt_add_child( reply, xt_new_node( "password", gc->acc->pass, NULL ) ); |
---|
76 | } |
---|
77 | else |
---|
78 | { |
---|
79 | xt_free_node( reply ); |
---|
80 | |
---|
81 | hide_login_progress_error( gc, "Can't find suitable authentication method" ); |
---|
82 | signoff( gc ); |
---|
83 | return XT_ABORT; |
---|
84 | } |
---|
85 | |
---|
86 | reply = jabber_make_packet( "iq", "set", NULL, reply ); |
---|
87 | st = jabber_write_packet( gc, reply ); |
---|
88 | xt_free_node( reply ); |
---|
89 | |
---|
90 | return st ? XT_HANDLED : XT_ABORT; |
---|
91 | } |
---|
92 | |
---|
93 | return XT_HANDLED; |
---|
94 | } |
---|
95 | |
---|
96 | int jabber_start_auth( struct gaim_connection *gc ) |
---|
97 | { |
---|
98 | struct jabber_data *jd = gc->proto_data; |
---|
99 | struct xt_node *node; |
---|
100 | int st; |
---|
101 | |
---|
102 | node = xt_new_node( "query", NULL, xt_new_node( "username", jd->username, NULL ) ); |
---|
103 | xt_add_attr( node, "xmlns", "jabber:iq:auth" ); |
---|
104 | node = jabber_make_packet( "iq", "get", NULL, node ); |
---|
105 | |
---|
106 | st = jabber_write_packet( gc, node ); |
---|
107 | |
---|
108 | xt_free_node( node ); |
---|
109 | return st; |
---|
110 | } |
---|