[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 | |
---|
| 26 | xt_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; |
---|
[861c199] | 30 | char *type, *s; |
---|
[21167d2] | 31 | |
---|
[70f6aab8] | 32 | type = xt_find_attr( node, "type" ); |
---|
[f06894d] | 33 | |
---|
[70f6aab8] | 34 | if( !type ) |
---|
[861c199] | 35 | { |
---|
| 36 | hide_login_progress_error( gc, "Received IQ packet without type!" ); |
---|
| 37 | signoff( gc ); |
---|
| 38 | return XT_ABORT; |
---|
| 39 | } |
---|
[21167d2] | 40 | |
---|
[861c199] | 41 | if( ( s = xt_find_attr( node, "id" ) ) && |
---|
| 42 | ( strcmp( type, "result" ) == 0 || strcmp( type, "error" ) == 0 ) ) |
---|
| 43 | { |
---|
| 44 | struct jabber_cache_entry *entry; |
---|
| 45 | |
---|
| 46 | entry = g_hash_table_lookup( jd->node_cache, s ); |
---|
| 47 | |
---|
| 48 | if( entry == NULL ) |
---|
| 49 | serv_got_crap( gc, "WARNING: Received IQ %s packet with unknown/expired ID %s!", type, s ); |
---|
| 50 | else if( entry->func ) |
---|
| 51 | return entry->func( gc, node, entry->node ); |
---|
| 52 | } |
---|
[70f6aab8] | 53 | |
---|
[861c199] | 54 | return XT_HANDLED; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | static xt_status jabber_do_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig ); |
---|
| 58 | static xt_status jabber_finish_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig ); |
---|
| 59 | |
---|
| 60 | int jabber_init_iq_auth( struct gaim_connection *gc ) |
---|
| 61 | { |
---|
| 62 | struct jabber_data *jd = gc->proto_data; |
---|
| 63 | struct xt_node *node; |
---|
| 64 | int st; |
---|
[fe7a554] | 65 | |
---|
[861c199] | 66 | node = xt_new_node( "query", NULL, xt_new_node( "username", jd->username, NULL ) ); |
---|
| 67 | xt_add_attr( node, "xmlns", "jabber:iq:auth" ); |
---|
| 68 | node = jabber_make_packet( "iq", "get", NULL, node ); |
---|
| 69 | |
---|
| 70 | jabber_cache_add( gc, node, jabber_do_iq_auth ); |
---|
| 71 | st = jabber_write_packet( gc, node ); |
---|
| 72 | |
---|
| 73 | return st; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | static xt_status jabber_do_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig ) |
---|
| 77 | { |
---|
| 78 | struct jabber_data *jd = gc->proto_data; |
---|
| 79 | struct xt_node *reply, *query; |
---|
| 80 | xt_status st; |
---|
| 81 | char *s; |
---|
| 82 | |
---|
| 83 | query = xt_find_node( node->children, "query" ); |
---|
| 84 | |
---|
| 85 | /* Time to authenticate ourselves! */ |
---|
| 86 | reply = xt_new_node( "query", NULL, NULL ); |
---|
| 87 | xt_add_attr( reply, "xmlns", "jabber:iq:auth" ); |
---|
| 88 | xt_add_child( reply, xt_new_node( "username", jd->username, NULL ) ); |
---|
| 89 | xt_add_child( reply, xt_new_node( "resource", set_getstr( &gc->acc->set, "resource" ), NULL ) ); |
---|
| 90 | |
---|
| 91 | if( xt_find_node( query->children, "digest" ) && ( s = xt_find_attr( jd->xt->root, "id" ) ) ) |
---|
[21167d2] | 92 | { |
---|
[861c199] | 93 | /* We can do digest authentication, it seems, and of |
---|
| 94 | course we prefer that. */ |
---|
| 95 | SHA_CTX sha; |
---|
| 96 | char hash_hex[40]; |
---|
| 97 | unsigned char hash[20]; |
---|
| 98 | int i; |
---|
[21167d2] | 99 | |
---|
[861c199] | 100 | shaInit( &sha ); |
---|
| 101 | shaUpdate( &sha, (unsigned char*) s, strlen( s ) ); |
---|
| 102 | shaUpdate( &sha, (unsigned char*) gc->acc->pass, strlen( gc->acc->pass ) ); |
---|
| 103 | shaFinal( &sha, hash ); |
---|
[21167d2] | 104 | |
---|
[861c199] | 105 | for( i = 0; i < 20; i ++ ) |
---|
| 106 | sprintf( hash_hex + i * 2, "%02x", hash[i] ); |
---|
[21167d2] | 107 | |
---|
[861c199] | 108 | xt_add_child( reply, xt_new_node( "digest", hash_hex, NULL ) ); |
---|
[21167d2] | 109 | } |
---|
[861c199] | 110 | else if( xt_find_node( query->children, "password" ) ) |
---|
[0b4a0db] | 111 | { |
---|
[861c199] | 112 | /* We'll have to stick with plaintext. Let's hope we're using SSL/TLS... */ |
---|
| 113 | xt_add_child( reply, xt_new_node( "password", gc->acc->pass, NULL ) ); |
---|
[0b4a0db] | 114 | } |
---|
[861c199] | 115 | else |
---|
[70f6aab8] | 116 | { |
---|
[861c199] | 117 | xt_free_node( reply ); |
---|
[995913b] | 118 | |
---|
[861c199] | 119 | hide_login_progress( gc, "Can't find suitable authentication method" ); |
---|
| 120 | signoff( gc ); |
---|
| 121 | return XT_ABORT; |
---|
[70f6aab8] | 122 | } |
---|
[861c199] | 123 | |
---|
| 124 | reply = jabber_make_packet( "iq", "set", NULL, reply ); |
---|
| 125 | jabber_cache_add( gc, reply, jabber_finish_iq_auth ); |
---|
| 126 | st = jabber_write_packet( gc, reply ); |
---|
| 127 | |
---|
| 128 | return st ? XT_HANDLED : XT_ABORT; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | static xt_status jabber_finish_iq_auth( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig ) |
---|
| 132 | { |
---|
| 133 | char *type = xt_find_attr( node, "type" ); |
---|
| 134 | struct jabber_data *jd = gc->proto_data; |
---|
| 135 | |
---|
| 136 | if( strcmp( type, "error" ) == 0 ) |
---|
[70f6aab8] | 137 | { |
---|
[861c199] | 138 | hide_login_progress( gc, "Authentication failure" ); |
---|
| 139 | signoff( gc ); |
---|
| 140 | return XT_ABORT; |
---|
| 141 | } |
---|
| 142 | else if( strcmp( type, "result" ) == 0 ) |
---|
| 143 | { |
---|
| 144 | /* This happens when we just successfully authenticated the |
---|
| 145 | old (non-SASL) way. */ |
---|
| 146 | jd->flags |= JFLAG_AUTHENTICATED; |
---|
| 147 | if( !jabber_get_roster( gc ) ) |
---|
[70f6aab8] | 148 | return XT_ABORT; |
---|
| 149 | } |
---|
[f06894d] | 150 | |
---|
| 151 | return XT_HANDLED; |
---|
| 152 | } |
---|
| 153 | |
---|
[861c199] | 154 | xt_status jabber_pkt_bind_sess( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig ) |
---|
[21167d2] | 155 | { |
---|
| 156 | struct jabber_data *jd = gc->proto_data; |
---|
[861c199] | 157 | struct xt_node *c; |
---|
| 158 | char *s; |
---|
[21167d2] | 159 | |
---|
[861c199] | 160 | if( ( c = xt_find_node( node->children, "bind" ) ) ) |
---|
| 161 | { |
---|
| 162 | c = xt_find_node( c->children, "jid" ); |
---|
| 163 | if( c && c->text_len && ( s = strchr( c->text, '/' ) ) && |
---|
| 164 | strcmp( s + 1, set_getstr( &gc->acc->set, "resource" ) ) != 0 ) |
---|
| 165 | serv_got_crap( gc, "Server changed session resource string to `%s'", s + 1 ); |
---|
| 166 | |
---|
| 167 | jd->flags &= ~JFLAG_WAIT_BIND; |
---|
| 168 | } |
---|
| 169 | else |
---|
| 170 | { |
---|
| 171 | jd->flags &= ~JFLAG_WAIT_SESSION; |
---|
| 172 | } |
---|
[21167d2] | 173 | |
---|
[861c199] | 174 | if( ( jd->flags & ( JFLAG_WAIT_BIND | JFLAG_WAIT_SESSION ) ) == 0 ) |
---|
| 175 | { |
---|
| 176 | if( !jabber_get_roster( gc ) ) |
---|
| 177 | return XT_ABORT; |
---|
| 178 | } |
---|
[21167d2] | 179 | |
---|
[861c199] | 180 | return XT_HANDLED; |
---|
[21167d2] | 181 | } |
---|
[70f6aab8] | 182 | |
---|
[861c199] | 183 | static xt_status jabber_parse_roster( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig ); |
---|
| 184 | |
---|
[70f6aab8] | 185 | int jabber_get_roster( struct gaim_connection *gc ) |
---|
| 186 | { |
---|
| 187 | struct xt_node *node; |
---|
| 188 | int st; |
---|
| 189 | |
---|
| 190 | set_login_progress( gc, 1, "Authenticated, requesting buddy list" ); |
---|
| 191 | |
---|
| 192 | node = xt_new_node( "query", NULL, NULL ); |
---|
| 193 | xt_add_attr( node, "xmlns", "jabber:iq:roster" ); |
---|
| 194 | node = jabber_make_packet( "iq", "get", NULL, node ); |
---|
| 195 | |
---|
[861c199] | 196 | jabber_cache_add( gc, node, jabber_parse_roster ); |
---|
[70f6aab8] | 197 | st = jabber_write_packet( gc, node ); |
---|
| 198 | |
---|
| 199 | return st; |
---|
| 200 | } |
---|
[cfbb3a6] | 201 | |
---|
[861c199] | 202 | static xt_status jabber_parse_roster( struct gaim_connection *gc, struct xt_node *node, struct xt_node *orig ) |
---|
| 203 | { |
---|
| 204 | struct xt_node *query, *c; |
---|
| 205 | |
---|
| 206 | query = xt_find_node( node->children, "query" ); |
---|
| 207 | |
---|
| 208 | c = query->children; |
---|
| 209 | while( ( c = xt_find_node( c, "item" ) ) ) |
---|
| 210 | { |
---|
| 211 | char *jid = xt_find_attr( c, "jid" ); |
---|
| 212 | char *name = xt_find_attr( c, "name" ); |
---|
| 213 | char *sub = xt_find_attr( c, "subscription" ); |
---|
| 214 | |
---|
| 215 | if( jid && sub && ( strcmp( sub, "both" ) == 0 || strcmp( sub, "to" ) == 0 ) ) |
---|
| 216 | add_buddy( gc, NULL, jid, name ); |
---|
| 217 | |
---|
| 218 | c = c->next; |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | account_online( gc ); |
---|
| 222 | |
---|
| 223 | return XT_HANDLED; |
---|
| 224 | } |
---|
| 225 | |
---|
[cfbb3a6] | 226 | int jabber_add_to_roster( struct gaim_connection *gc, char *handle, char *name ) |
---|
| 227 | { |
---|
| 228 | struct xt_node *node; |
---|
| 229 | int st; |
---|
| 230 | |
---|
| 231 | /* Build the item entry */ |
---|
| 232 | node = xt_new_node( "item", NULL, NULL ); |
---|
| 233 | xt_add_attr( node, "jid", handle ); |
---|
| 234 | if( name ) |
---|
| 235 | xt_add_attr( node, "name", name ); |
---|
| 236 | |
---|
| 237 | /* And pack it into a roster-add packet */ |
---|
| 238 | node = xt_new_node( "query", NULL, node ); |
---|
| 239 | xt_add_attr( node, "xmlns", "jabber:iq:roster" ); |
---|
| 240 | node = jabber_make_packet( "iq", "set", NULL, node ); |
---|
| 241 | |
---|
| 242 | st = jabber_write_packet( gc, node ); |
---|
| 243 | |
---|
| 244 | xt_free_node( node ); |
---|
| 245 | return st; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | int jabber_remove_from_roster( struct gaim_connection *gc, char *handle ) |
---|
| 249 | { |
---|
| 250 | struct xt_node *node; |
---|
| 251 | int st; |
---|
| 252 | |
---|
| 253 | /* Build the item entry */ |
---|
| 254 | node = xt_new_node( "item", NULL, NULL ); |
---|
| 255 | xt_add_attr( node, "jid", handle ); |
---|
| 256 | xt_add_attr( node, "subscription", "remove" ); |
---|
| 257 | |
---|
| 258 | /* And pack it into a roster-add packet */ |
---|
| 259 | node = xt_new_node( "query", NULL, node ); |
---|
| 260 | xt_add_attr( node, "xmlns", "jabber:iq:roster" ); |
---|
| 261 | node = jabber_make_packet( "iq", "set", NULL, node ); |
---|
| 262 | |
---|
| 263 | st = jabber_write_packet( gc, node ); |
---|
| 264 | |
---|
| 265 | xt_free_node( node ); |
---|
| 266 | return st; |
---|
| 267 | } |
---|