[21167d2] | 1 | /***************************************************************************\ |
---|
| 2 | * * |
---|
| 3 | * BitlBee - An IRC to IM gateway * |
---|
| 4 | * Jabber module - I/O stuff (plain, SSL), queues, etc * |
---|
| 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" |
---|
[42127dc] | 25 | #include "ssl_client.h" |
---|
[21167d2] | 26 | |
---|
| 27 | static gboolean jabber_write_callback( gpointer data, gint fd, b_input_condition cond ); |
---|
[0da65d5] | 28 | static gboolean jabber_write_queue( struct im_connection *ic ); |
---|
[21167d2] | 29 | |
---|
[0da65d5] | 30 | int jabber_write_packet( struct im_connection *ic, struct xt_node *node ) |
---|
[21167d2] | 31 | { |
---|
| 32 | char *buf; |
---|
| 33 | int st; |
---|
| 34 | |
---|
| 35 | buf = xt_to_string( node ); |
---|
[0da65d5] | 36 | st = jabber_write( ic, buf, strlen( buf ) ); |
---|
[21167d2] | 37 | g_free( buf ); |
---|
| 38 | |
---|
| 39 | return st; |
---|
| 40 | } |
---|
| 41 | |
---|
[0da65d5] | 42 | int jabber_write( struct im_connection *ic, char *buf, int len ) |
---|
[21167d2] | 43 | { |
---|
[0da65d5] | 44 | struct jabber_data *jd = ic->proto_data; |
---|
[cc2cb2d] | 45 | gboolean ret; |
---|
[21167d2] | 46 | |
---|
[8e3890b] | 47 | if( jd->flags & JFLAG_XMLCONSOLE && !( ic->flags & OPT_LOGGING_OUT ) ) |
---|
[bb95d43] | 48 | { |
---|
[fe79f7a7] | 49 | char *msg, *s; |
---|
[bb95d43] | 50 | |
---|
| 51 | msg = g_strdup_printf( "TX: %s", buf ); |
---|
[fe79f7a7] | 52 | /* Don't include auth info in XML logs. */ |
---|
| 53 | if( strncmp( msg, "TX: <auth ", 10 ) == 0 && ( s = strchr( msg, '>' ) ) ) |
---|
| 54 | { |
---|
| 55 | s++; |
---|
| 56 | while( *s && *s != '<' ) |
---|
| 57 | *(s++) = '*'; |
---|
| 58 | } |
---|
[bb95d43] | 59 | imcb_buddy_msg( ic, JABBER_XMLCONSOLE_HANDLE, msg, 0, 0 ); |
---|
| 60 | g_free( msg ); |
---|
| 61 | } |
---|
| 62 | |
---|
[21167d2] | 63 | if( jd->tx_len == 0 ) |
---|
| 64 | { |
---|
| 65 | /* If the queue is empty, allocate a new buffer. */ |
---|
| 66 | jd->tx_len = len; |
---|
| 67 | jd->txq = g_memdup( buf, len ); |
---|
| 68 | |
---|
| 69 | /* Try if we can write it immediately so we don't have to do |
---|
| 70 | it via the event handler. If not, add the handler. (In |
---|
| 71 | most cases it probably won't be necessary.) */ |
---|
[0da65d5] | 72 | if( ( ret = jabber_write_queue( ic ) ) && jd->tx_len > 0 ) |
---|
[e046390] | 73 | jd->w_inpa = b_input_add( jd->fd, B_EV_IO_WRITE, jabber_write_callback, ic ); |
---|
[21167d2] | 74 | } |
---|
| 75 | else |
---|
| 76 | { |
---|
| 77 | /* Just add it to the buffer if it's already filled. The |
---|
| 78 | event handler is already set. */ |
---|
| 79 | jd->txq = g_renew( char, jd->txq, jd->tx_len + len ); |
---|
| 80 | memcpy( jd->txq + jd->tx_len, buf, len ); |
---|
| 81 | jd->tx_len += len; |
---|
[cc2cb2d] | 82 | |
---|
| 83 | /* The return value for write() doesn't necessarily mean |
---|
| 84 | that everything got sent, it mainly means that the |
---|
| 85 | connection (officially) still exists and can still |
---|
| 86 | be accessed without hitting SIGSEGV. IOW: */ |
---|
| 87 | ret = TRUE; |
---|
[21167d2] | 88 | } |
---|
| 89 | |
---|
[cc2cb2d] | 90 | return ret; |
---|
[21167d2] | 91 | } |
---|
| 92 | |
---|
[cc2cb2d] | 93 | /* Splitting up in two separate functions: One to use as a callback and one |
---|
| 94 | to use in the function above to escape from having to wait for the event |
---|
| 95 | handler to call us, if possible. |
---|
| 96 | |
---|
| 97 | Two different functions are necessary because of the return values: The |
---|
| 98 | callback should only return TRUE if the write was successful AND if the |
---|
| 99 | buffer is not empty yet (ie. if the handler has to be called again when |
---|
| 100 | the socket is ready for more data). */ |
---|
[21167d2] | 101 | static gboolean jabber_write_callback( gpointer data, gint fd, b_input_condition cond ) |
---|
| 102 | { |
---|
[0da65d5] | 103 | struct jabber_data *jd = ((struct im_connection *)data)->proto_data; |
---|
[cc2cb2d] | 104 | |
---|
| 105 | return jd->fd != -1 && |
---|
| 106 | jabber_write_queue( data ) && |
---|
| 107 | jd->tx_len > 0; |
---|
| 108 | } |
---|
| 109 | |
---|
[0da65d5] | 110 | static gboolean jabber_write_queue( struct im_connection *ic ) |
---|
[cc2cb2d] | 111 | { |
---|
[0da65d5] | 112 | struct jabber_data *jd = ic->proto_data; |
---|
[21167d2] | 113 | int st; |
---|
| 114 | |
---|
[42127dc] | 115 | if( jd->ssl ) |
---|
| 116 | st = ssl_write( jd->ssl, jd->txq, jd->tx_len ); |
---|
| 117 | else |
---|
| 118 | st = write( jd->fd, jd->txq, jd->tx_len ); |
---|
| 119 | |
---|
[21167d2] | 120 | if( st == jd->tx_len ) |
---|
| 121 | { |
---|
| 122 | /* We wrote everything, clear the buffer. */ |
---|
| 123 | g_free( jd->txq ); |
---|
| 124 | jd->txq = NULL; |
---|
| 125 | jd->tx_len = 0; |
---|
| 126 | |
---|
[cc2cb2d] | 127 | return TRUE; |
---|
[21167d2] | 128 | } |
---|
[d52111a] | 129 | else if( st == 0 || ( st < 0 && !ssl_sockerr_again( jd->ssl ) ) ) |
---|
[21167d2] | 130 | { |
---|
[8d74291] | 131 | /* Set fd to -1 to make sure we won't write to it anymore. */ |
---|
| 132 | closesocket( jd->fd ); /* Shouldn't be necessary after errors? */ |
---|
| 133 | jd->fd = -1; |
---|
| 134 | |
---|
[84b045d] | 135 | imcb_error( ic, "Short write() to server" ); |
---|
[c2fb3809] | 136 | imc_logout( ic, TRUE ); |
---|
[21167d2] | 137 | return FALSE; |
---|
| 138 | } |
---|
| 139 | else if( st > 0 ) |
---|
| 140 | { |
---|
| 141 | char *s; |
---|
| 142 | |
---|
| 143 | s = g_memdup( jd->txq + st, jd->tx_len - st ); |
---|
| 144 | jd->tx_len -= st; |
---|
| 145 | g_free( jd->txq ); |
---|
| 146 | jd->txq = s; |
---|
| 147 | |
---|
[8d74291] | 148 | return TRUE; |
---|
[21167d2] | 149 | } |
---|
| 150 | else |
---|
| 151 | { |
---|
| 152 | /* Just in case we had EINPROGRESS/EAGAIN: */ |
---|
| 153 | |
---|
| 154 | return TRUE; |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | static gboolean jabber_read_callback( gpointer data, gint fd, b_input_condition cond ) |
---|
| 159 | { |
---|
[0da65d5] | 160 | struct im_connection *ic = data; |
---|
| 161 | struct jabber_data *jd = ic->proto_data; |
---|
[21167d2] | 162 | char buf[512]; |
---|
| 163 | int st; |
---|
| 164 | |
---|
[8d74291] | 165 | if( jd->fd == -1 ) |
---|
| 166 | return FALSE; |
---|
| 167 | |
---|
[42127dc] | 168 | if( jd->ssl ) |
---|
| 169 | st = ssl_read( jd->ssl, buf, sizeof( buf ) ); |
---|
| 170 | else |
---|
| 171 | st = read( jd->fd, buf, sizeof( buf ) ); |
---|
| 172 | |
---|
[21167d2] | 173 | if( st > 0 ) |
---|
| 174 | { |
---|
| 175 | /* Parse. */ |
---|
[b56b220] | 176 | if( xt_feed( jd->xt, buf, st ) < 0 ) |
---|
[21167d2] | 177 | { |
---|
[84b045d] | 178 | imcb_error( ic, "XML stream error" ); |
---|
[c2fb3809] | 179 | imc_logout( ic, TRUE ); |
---|
[21167d2] | 180 | return FALSE; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /* Execute all handlers. */ |
---|
[101d84f] | 184 | if( !xt_handle( jd->xt, NULL, 1 ) ) |
---|
[21167d2] | 185 | { |
---|
| 186 | /* Don't do anything, the handlers should have |
---|
[25984f2] | 187 | aborted the connection already. */ |
---|
[21167d2] | 188 | return FALSE; |
---|
| 189 | } |
---|
| 190 | |
---|
[5997488] | 191 | if( jd->flags & JFLAG_STREAM_RESTART ) |
---|
| 192 | { |
---|
| 193 | jd->flags &= ~JFLAG_STREAM_RESTART; |
---|
[0da65d5] | 194 | jabber_start_stream( ic ); |
---|
[5997488] | 195 | } |
---|
| 196 | |
---|
[21167d2] | 197 | /* Garbage collection. */ |
---|
[101d84f] | 198 | xt_cleanup( jd->xt, NULL, 1 ); |
---|
[21167d2] | 199 | |
---|
| 200 | /* This is a bit hackish, unfortunately. Although xmltree |
---|
| 201 | has nifty event handler stuff, it only calls handlers |
---|
| 202 | when nodes are complete. Since the server should only |
---|
| 203 | send an opening <stream:stream> tag, we have to check |
---|
| 204 | this by hand. :-( */ |
---|
| 205 | if( !( jd->flags & JFLAG_STREAM_STARTED ) && jd->xt && jd->xt->root ) |
---|
| 206 | { |
---|
| 207 | if( g_strcasecmp( jd->xt->root->name, "stream:stream" ) == 0 ) |
---|
| 208 | { |
---|
| 209 | jd->flags |= JFLAG_STREAM_STARTED; |
---|
[8d74291] | 210 | |
---|
| 211 | /* If there's no version attribute, assume |
---|
| 212 | this is an old server that can't do SASL |
---|
| 213 | authentication. */ |
---|
[06b5893] | 214 | if( !set_getbool( &ic->acc->set, "sasl") || !sasl_supported( ic ) ) |
---|
[cc2cb2d] | 215 | { |
---|
| 216 | /* If there's no version= tag, we suppose |
---|
| 217 | this server does NOT implement: XMPP 1.0, |
---|
| 218 | SASL and TLS. */ |
---|
[0da65d5] | 219 | if( set_getbool( &ic->acc->set, "tls" ) ) |
---|
[cc2cb2d] | 220 | { |
---|
[84b045d] | 221 | imcb_error( ic, "TLS is turned on for this " |
---|
[cc2cb2d] | 222 | "account, but is not supported by this server" ); |
---|
[c2fb3809] | 223 | imc_logout( ic, FALSE ); |
---|
[cc2cb2d] | 224 | return FALSE; |
---|
| 225 | } |
---|
| 226 | else |
---|
| 227 | { |
---|
[0da65d5] | 228 | return jabber_init_iq_auth( ic ); |
---|
[cc2cb2d] | 229 | } |
---|
| 230 | } |
---|
[21167d2] | 231 | } |
---|
| 232 | else |
---|
| 233 | { |
---|
[84b045d] | 234 | imcb_error( ic, "XML stream error" ); |
---|
[c2fb3809] | 235 | imc_logout( ic, TRUE ); |
---|
[21167d2] | 236 | return FALSE; |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | } |
---|
[d52111a] | 240 | else if( st == 0 || ( st < 0 && !ssl_sockerr_again( jd->ssl ) ) ) |
---|
[21167d2] | 241 | { |
---|
[8d74291] | 242 | closesocket( jd->fd ); |
---|
| 243 | jd->fd = -1; |
---|
| 244 | |
---|
[84b045d] | 245 | imcb_error( ic, "Error while reading from server" ); |
---|
[c2fb3809] | 246 | imc_logout( ic, TRUE ); |
---|
[21167d2] | 247 | return FALSE; |
---|
| 248 | } |
---|
| 249 | |
---|
[8a2221a7] | 250 | if( ssl_pending( jd->ssl ) ) |
---|
| 251 | /* OpenSSL empties the TCP buffers completely but may keep some |
---|
| 252 | data in its internap buffers. select() won't see that, but |
---|
| 253 | ssl_pending() does. */ |
---|
| 254 | return jabber_read_callback( data, fd, cond ); |
---|
| 255 | else |
---|
| 256 | return TRUE; |
---|
[21167d2] | 257 | } |
---|
| 258 | |
---|
| 259 | gboolean jabber_connected_plain( gpointer data, gint source, b_input_condition cond ) |
---|
| 260 | { |
---|
[0da65d5] | 261 | struct im_connection *ic = data; |
---|
[21167d2] | 262 | |
---|
[b5c8a34] | 263 | if( g_slist_find( jabber_connections, ic ) == NULL ) |
---|
| 264 | return FALSE; |
---|
| 265 | |
---|
[21167d2] | 266 | if( source == -1 ) |
---|
| 267 | { |
---|
[84b045d] | 268 | imcb_error( ic, "Could not connect to server" ); |
---|
[c2fb3809] | 269 | imc_logout( ic, TRUE ); |
---|
[21167d2] | 270 | return FALSE; |
---|
| 271 | } |
---|
| 272 | |
---|
[84b045d] | 273 | imcb_log( ic, "Connected to server, logging in" ); |
---|
[21167d2] | 274 | |
---|
[0da65d5] | 275 | return jabber_start_stream( ic ); |
---|
[21167d2] | 276 | } |
---|
| 277 | |
---|
[486ddb5] | 278 | gboolean jabber_connected_ssl( gpointer data, int returncode, void *source, b_input_condition cond ) |
---|
[42127dc] | 279 | { |
---|
[0da65d5] | 280 | struct im_connection *ic = data; |
---|
[b5c8a34] | 281 | struct jabber_data *jd; |
---|
| 282 | |
---|
| 283 | if( g_slist_find( jabber_connections, ic ) == NULL ) |
---|
| 284 | return FALSE; |
---|
| 285 | |
---|
| 286 | jd = ic->proto_data; |
---|
[42127dc] | 287 | |
---|
| 288 | if( source == NULL ) |
---|
| 289 | { |
---|
[c1ed6527] | 290 | /* The SSL connection will be cleaned up by the SSL lib |
---|
| 291 | already, set it to NULL here to prevent a double cleanup: */ |
---|
| 292 | jd->ssl = NULL; |
---|
| 293 | |
---|
[41658da] | 294 | if( returncode != 0 ) |
---|
[486ddb5] | 295 | { |
---|
[78b8401] | 296 | char *err = ssl_verify_strerror( returncode ); |
---|
| 297 | imcb_error( ic, "Certificate verification problem 0x%x: %s", |
---|
| 298 | returncode, err ? err : "Unknown" ); |
---|
| 299 | g_free( err ); |
---|
[486ddb5] | 300 | imc_logout( ic, FALSE ); |
---|
| 301 | } |
---|
[78b8401] | 302 | else |
---|
[486ddb5] | 303 | { |
---|
[78b8401] | 304 | imcb_error( ic, "Could not connect to server" ); |
---|
| 305 | imc_logout( ic, TRUE ); |
---|
[486ddb5] | 306 | } |
---|
[78b8401] | 307 | |
---|
[42127dc] | 308 | return FALSE; |
---|
| 309 | } |
---|
| 310 | |
---|
[84b045d] | 311 | imcb_log( ic, "Connected to server, logging in" ); |
---|
[42127dc] | 312 | |
---|
[0da65d5] | 313 | return jabber_start_stream( ic ); |
---|
[42127dc] | 314 | } |
---|
| 315 | |
---|
[21167d2] | 316 | static xt_status jabber_end_of_stream( struct xt_node *node, gpointer data ) |
---|
| 317 | { |
---|
[c2fb3809] | 318 | imc_logout( data, TRUE ); |
---|
[21167d2] | 319 | return XT_ABORT; |
---|
| 320 | } |
---|
| 321 | |
---|
[8d74291] | 322 | static xt_status jabber_pkt_features( struct xt_node *node, gpointer data ) |
---|
| 323 | { |
---|
[0da65d5] | 324 | struct im_connection *ic = data; |
---|
| 325 | struct jabber_data *jd = ic->proto_data; |
---|
[fe7a554] | 326 | struct xt_node *c, *reply; |
---|
[e101506] | 327 | int trytls; |
---|
[8d74291] | 328 | |
---|
[0da65d5] | 329 | trytls = g_strcasecmp( set_getstr( &ic->acc->set, "tls" ), "try" ) == 0; |
---|
[8d74291] | 330 | c = xt_find_node( node->children, "starttls" ); |
---|
[42127dc] | 331 | if( c && !jd->ssl ) |
---|
[8d74291] | 332 | { |
---|
[42127dc] | 333 | /* If the server advertises the STARTTLS feature and if we're |
---|
| 334 | not in a secure connection already: */ |
---|
| 335 | |
---|
| 336 | c = xt_find_node( c->children, "required" ); |
---|
| 337 | |
---|
[0da65d5] | 338 | if( c && ( !trytls && !set_getbool( &ic->acc->set, "tls" ) ) ) |
---|
[e101506] | 339 | { |
---|
[84b045d] | 340 | imcb_error( ic, "Server requires TLS connections, but TLS is turned off for this account" ); |
---|
[c2fb3809] | 341 | imc_logout( ic, FALSE ); |
---|
[e101506] | 342 | |
---|
| 343 | return XT_ABORT; |
---|
| 344 | } |
---|
| 345 | |
---|
[42127dc] | 346 | /* Only run this if the tls setting is set to true or try: */ |
---|
[0da65d5] | 347 | if( ( trytls || set_getbool( &ic->acc->set, "tls" ) ) ) |
---|
[42127dc] | 348 | { |
---|
| 349 | reply = xt_new_node( "starttls", NULL, NULL ); |
---|
[47d3ac4] | 350 | xt_add_attr( reply, "xmlns", XMLNS_TLS ); |
---|
[0da65d5] | 351 | if( !jabber_write_packet( ic, reply ) ) |
---|
[42127dc] | 352 | { |
---|
| 353 | xt_free_node( reply ); |
---|
| 354 | return XT_ABORT; |
---|
| 355 | } |
---|
| 356 | xt_free_node( reply ); |
---|
| 357 | |
---|
| 358 | return XT_HANDLED; |
---|
| 359 | } |
---|
| 360 | } |
---|
[e101506] | 361 | else if( !c && !jd->ssl ) |
---|
[42127dc] | 362 | { |
---|
[e101506] | 363 | /* If the server does not advertise the STARTTLS feature and |
---|
| 364 | we're not in a secure connection already: (Servers have a |
---|
| 365 | habit of not advertising <starttls/> anymore when already |
---|
| 366 | using SSL/TLS. */ |
---|
| 367 | |
---|
[0da65d5] | 368 | if( !trytls && set_getbool( &ic->acc->set, "tls" ) ) |
---|
[e101506] | 369 | { |
---|
[84b045d] | 370 | imcb_error( ic, "TLS is turned on for this account, but is not supported by this server" ); |
---|
[c2fb3809] | 371 | imc_logout( ic, FALSE ); |
---|
[e101506] | 372 | |
---|
| 373 | return XT_ABORT; |
---|
| 374 | } |
---|
[8d74291] | 375 | } |
---|
| 376 | |
---|
[42127dc] | 377 | /* This one used to be in jabber_handlers[], but it has to be done |
---|
| 378 | from here to make sure the TLS session will be initialized |
---|
| 379 | properly before we attempt SASL authentication. */ |
---|
| 380 | if( ( c = xt_find_node( node->children, "mechanisms" ) ) ) |
---|
[0e2d97f] | 381 | { |
---|
[42127dc] | 382 | if( sasl_pkt_mechanisms( c, data ) == XT_ABORT ) |
---|
| 383 | return XT_ABORT; |
---|
[0e2d97f] | 384 | } |
---|
[88591fd] | 385 | /* If the server *SEEMS* to support SASL authentication but doesn't |
---|
| 386 | support it after all, we should try to do authentication the |
---|
| 387 | other way. jabber.com doesn't seem to do SASL while it pretends |
---|
| 388 | to be XMPP 1.0 compliant! */ |
---|
[06b5893] | 389 | else if( !( jd->flags & JFLAG_AUTHENTICATED ) && set_getbool( &ic->acc->set, "sasl") && sasl_supported( ic ) ) |
---|
[0e2d97f] | 390 | { |
---|
[0da65d5] | 391 | if( !jabber_init_iq_auth( ic ) ) |
---|
[88591fd] | 392 | return XT_ABORT; |
---|
[0e2d97f] | 393 | } |
---|
[42127dc] | 394 | |
---|
[fe7a554] | 395 | if( ( c = xt_find_node( node->children, "bind" ) ) ) |
---|
[8fb1263] | 396 | jd->flags |= JFLAG_WANT_BIND; |
---|
[fe7a554] | 397 | |
---|
| 398 | if( ( c = xt_find_node( node->children, "session" ) ) ) |
---|
[8fb1263] | 399 | jd->flags |= JFLAG_WANT_SESSION; |
---|
[fe7a554] | 400 | |
---|
[315dd4c] | 401 | if( jd->flags & JFLAG_AUTHENTICATED ) |
---|
[8fb1263] | 402 | return jabber_pkt_bind_sess( ic, NULL, NULL ); |
---|
[8d74291] | 403 | |
---|
| 404 | return XT_HANDLED; |
---|
| 405 | } |
---|
| 406 | |
---|
[42127dc] | 407 | static xt_status jabber_pkt_proceed_tls( struct xt_node *node, gpointer data ) |
---|
| 408 | { |
---|
[0da65d5] | 409 | struct im_connection *ic = data; |
---|
| 410 | struct jabber_data *jd = ic->proto_data; |
---|
[486ddb5] | 411 | char *xmlns, *tlsname; |
---|
[42127dc] | 412 | |
---|
| 413 | xmlns = xt_find_attr( node, "xmlns" ); |
---|
| 414 | |
---|
| 415 | /* Just ignore it when it doesn't seem to be TLS-related (is that at |
---|
| 416 | all possible??). */ |
---|
[47d3ac4] | 417 | if( !xmlns || strcmp( xmlns, XMLNS_TLS ) != 0 ) |
---|
[42127dc] | 418 | return XT_HANDLED; |
---|
| 419 | |
---|
| 420 | /* We don't want event handlers to touch our TLS session while it's |
---|
| 421 | still initializing! */ |
---|
| 422 | b_event_remove( jd->r_inpa ); |
---|
| 423 | if( jd->tx_len > 0 ) |
---|
| 424 | { |
---|
| 425 | /* Actually the write queue should be empty here, but just |
---|
| 426 | to be sure... */ |
---|
| 427 | b_event_remove( jd->w_inpa ); |
---|
| 428 | g_free( jd->txq ); |
---|
| 429 | jd->txq = NULL; |
---|
| 430 | jd->tx_len = 0; |
---|
| 431 | } |
---|
| 432 | jd->w_inpa = jd->r_inpa = 0; |
---|
| 433 | |
---|
[84b045d] | 434 | imcb_log( ic, "Converting stream to TLS" ); |
---|
[42127dc] | 435 | |
---|
[af7f046] | 436 | jd->flags |= JFLAG_STARTTLS_DONE; |
---|
[486ddb5] | 437 | |
---|
| 438 | /* If the user specified a server for the account, use this server as the |
---|
| 439 | * hostname in the certificate verification. Else we use the domain from |
---|
| 440 | * the username. */ |
---|
| 441 | if( ic->acc->server && *ic->acc->server ) |
---|
| 442 | tlsname = ic->acc->server; |
---|
| 443 | else |
---|
| 444 | tlsname = jd->server; |
---|
| 445 | |
---|
| 446 | jd->ssl = ssl_starttls( jd->fd, tlsname, set_getbool( &ic->acc->set, "tls_verify" ), |
---|
| 447 | jabber_connected_ssl, ic ); |
---|
[42127dc] | 448 | |
---|
| 449 | return XT_HANDLED; |
---|
| 450 | } |
---|
| 451 | |
---|
[b56b220] | 452 | static xt_status jabber_pkt_stream_error( struct xt_node *node, gpointer data ) |
---|
| 453 | { |
---|
[0da65d5] | 454 | struct im_connection *ic = data; |
---|
[daf544a] | 455 | struct jabber_data *jd = ic->proto_data; |
---|
[c2fb3809] | 456 | int allow_reconnect = TRUE; |
---|
[1baaef8] | 457 | struct jabber_error *err; |
---|
[daf544a] | 458 | struct xt_node *host; |
---|
| 459 | |
---|
| 460 | if( !( ic->flags & OPT_LOGGED_IN ) && |
---|
| 461 | ( host = xt_find_node( node->children, "see-other-host" ) ) && |
---|
| 462 | host->text ) |
---|
| 463 | { |
---|
| 464 | char *s; |
---|
| 465 | int port = set_getint( &ic->acc->set, "port" ); |
---|
| 466 | |
---|
| 467 | /* Let's try to obey this request, if we're not logged |
---|
| 468 | in yet (i.e. not have too much state yet). */ |
---|
| 469 | if( jd->ssl ) |
---|
| 470 | ssl_disconnect( jd->ssl ); |
---|
| 471 | closesocket( jd->fd ); |
---|
| 472 | b_event_remove( jd->r_inpa ); |
---|
| 473 | b_event_remove( jd->w_inpa ); |
---|
| 474 | |
---|
| 475 | jd->ssl = NULL; |
---|
| 476 | jd->r_inpa = jd->w_inpa = 0; |
---|
| 477 | jd->flags &= JFLAG_XMLCONSOLE; |
---|
| 478 | |
---|
| 479 | s = strchr( host->text, ':' ); |
---|
| 480 | if( s != NULL ) |
---|
| 481 | sscanf( s + 1, "%d", &port ); |
---|
| 482 | |
---|
| 483 | imcb_log( ic, "Redirected to %s", host->text ); |
---|
| 484 | jd->fd = proxy_connect( host->text, port, jabber_connected_plain, ic ); |
---|
| 485 | |
---|
| 486 | return XT_ABORT; |
---|
| 487 | } |
---|
[b56b220] | 488 | |
---|
[1baaef8] | 489 | err = jabber_error_parse( node, XMLNS_STREAM_ERROR ); |
---|
[b56b220] | 490 | |
---|
| 491 | /* Tssk... */ |
---|
[1baaef8] | 492 | if( err->code == NULL ) |
---|
[b56b220] | 493 | { |
---|
[84b045d] | 494 | imcb_error( ic, "Unknown stream error reported by server" ); |
---|
[c2fb3809] | 495 | imc_logout( ic, allow_reconnect ); |
---|
[1baaef8] | 496 | jabber_error_free( err ); |
---|
[b56b220] | 497 | return XT_ABORT; |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | /* We know that this is a fatal error. If it's a "conflict" error, we |
---|
| 501 | should turn off auto-reconnect to make sure we won't get some nasty |
---|
| 502 | infinite loop! */ |
---|
[1baaef8] | 503 | if( strcmp( err->code, "conflict" ) == 0 ) |
---|
[259edd4] | 504 | { |
---|
[84b045d] | 505 | imcb_error( ic, "Account and resource used from a different location" ); |
---|
[c2fb3809] | 506 | allow_reconnect = FALSE; |
---|
[259edd4] | 507 | } |
---|
| 508 | else |
---|
| 509 | { |
---|
[1baaef8] | 510 | imcb_error( ic, "Stream error: %s%s%s", err->code, err->text ? ": " : "", |
---|
| 511 | err->text ? err->text : "" ); |
---|
[259edd4] | 512 | } |
---|
[b56b220] | 513 | |
---|
[1baaef8] | 514 | jabber_error_free( err ); |
---|
[c2fb3809] | 515 | imc_logout( ic, allow_reconnect ); |
---|
[b56b220] | 516 | |
---|
| 517 | return XT_ABORT; |
---|
| 518 | } |
---|
| 519 | |
---|
[bb95d43] | 520 | static xt_status jabber_xmlconsole( struct xt_node *node, gpointer data ) |
---|
| 521 | { |
---|
| 522 | struct im_connection *ic = data; |
---|
| 523 | struct jabber_data *jd = ic->proto_data; |
---|
| 524 | |
---|
| 525 | if( jd->flags & JFLAG_XMLCONSOLE ) |
---|
| 526 | { |
---|
| 527 | char *msg, *pkt; |
---|
| 528 | |
---|
| 529 | pkt = xt_to_string( node ); |
---|
| 530 | msg = g_strdup_printf( "RX: %s", pkt ); |
---|
| 531 | imcb_buddy_msg( ic, JABBER_XMLCONSOLE_HANDLE, msg, 0, 0 ); |
---|
| 532 | g_free( msg ); |
---|
| 533 | g_free( pkt ); |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | return XT_NEXT; |
---|
| 537 | } |
---|
| 538 | |
---|
[21167d2] | 539 | static const struct xt_handler_entry jabber_handlers[] = { |
---|
[bb95d43] | 540 | { NULL, "stream:stream", jabber_xmlconsole }, |
---|
[21167d2] | 541 | { "stream:stream", "<root>", jabber_end_of_stream }, |
---|
| 542 | { "message", "stream:stream", jabber_pkt_message }, |
---|
| 543 | { "presence", "stream:stream", jabber_pkt_presence }, |
---|
[8d74291] | 544 | { "iq", "stream:stream", jabber_pkt_iq }, |
---|
| 545 | { "stream:features", "stream:stream", jabber_pkt_features }, |
---|
[b56b220] | 546 | { "stream:error", "stream:stream", jabber_pkt_stream_error }, |
---|
[42127dc] | 547 | { "proceed", "stream:stream", jabber_pkt_proceed_tls }, |
---|
[5997488] | 548 | { "challenge", "stream:stream", sasl_pkt_challenge }, |
---|
| 549 | { "success", "stream:stream", sasl_pkt_result }, |
---|
| 550 | { "failure", "stream:stream", sasl_pkt_result }, |
---|
[21167d2] | 551 | { NULL, NULL, NULL } |
---|
| 552 | }; |
---|
| 553 | |
---|
[0da65d5] | 554 | gboolean jabber_start_stream( struct im_connection *ic ) |
---|
[21167d2] | 555 | { |
---|
[0da65d5] | 556 | struct jabber_data *jd = ic->proto_data; |
---|
[21167d2] | 557 | int st; |
---|
| 558 | char *greet; |
---|
| 559 | |
---|
| 560 | /* We'll start our stream now, so prepare everything to receive one |
---|
| 561 | from the server too. */ |
---|
| 562 | xt_free( jd->xt ); /* In case we're RE-starting. */ |
---|
[4bbcba3] | 563 | jd->xt = xt_new( jabber_handlers, ic ); |
---|
[21167d2] | 564 | |
---|
[8d74291] | 565 | if( jd->r_inpa <= 0 ) |
---|
[e046390] | 566 | jd->r_inpa = b_input_add( jd->fd, B_EV_IO_READ, jabber_read_callback, ic ); |
---|
[21167d2] | 567 | |
---|
[af7f046] | 568 | greet = g_strdup_printf( "%s<stream:stream to=\"%s\" xmlns=\"jabber:client\" " |
---|
| 569 | "xmlns:stream=\"http://etherx.jabber.org/streams\" version=\"1.0\">", |
---|
| 570 | ( jd->flags & JFLAG_STARTTLS_DONE ) ? "" : "<?xml version='1.0' ?>", |
---|
| 571 | jd->server ); |
---|
[21167d2] | 572 | |
---|
[0da65d5] | 573 | st = jabber_write( ic, greet, strlen( greet ) ); |
---|
[21167d2] | 574 | |
---|
| 575 | g_free( greet ); |
---|
| 576 | |
---|
| 577 | return st; |
---|
| 578 | } |
---|
[4a0614e] | 579 | |
---|
[0da65d5] | 580 | void jabber_end_stream( struct im_connection *ic ) |
---|
[4a0614e] | 581 | { |
---|
[0da65d5] | 582 | struct jabber_data *jd = ic->proto_data; |
---|
[4a0614e] | 583 | |
---|
| 584 | /* Let's only do this if the queue is currently empty, otherwise it'd |
---|
| 585 | take too long anyway. */ |
---|
[5bcf70a] | 586 | if( jd->tx_len == 0 ) |
---|
| 587 | { |
---|
| 588 | char eos[] = "</stream:stream>"; |
---|
| 589 | struct xt_node *node; |
---|
[8d74291] | 590 | int st = 1; |
---|
[5bcf70a] | 591 | |
---|
[0da65d5] | 592 | if( ic->flags & OPT_LOGGED_IN ) |
---|
[8d74291] | 593 | { |
---|
| 594 | node = jabber_make_packet( "presence", "unavailable", NULL, NULL ); |
---|
[0da65d5] | 595 | st = jabber_write_packet( ic, node ); |
---|
[8d74291] | 596 | xt_free_node( node ); |
---|
| 597 | } |
---|
[5bcf70a] | 598 | |
---|
| 599 | if( st ) |
---|
[0da65d5] | 600 | jabber_write( ic, eos, strlen( eos ) ); |
---|
[5bcf70a] | 601 | } |
---|
[4a0614e] | 602 | } |
---|