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