Mon, 05 Jan 2004 22:37:07 +0000
[gaim-migrate @ 8696]
fix jabber registrations for XMPP servers
| 7014 | 1 | /* |
| 2 | * gaim - Jabber Protocol Plugin | |
| 3 | * | |
| 4 | * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | #include "internal.h" | |
| 22 | ||
| 23 | #include "jutil.h" | |
| 24 | #include "auth.h" | |
| 25 | #include "xmlnode.h" | |
| 26 | #include "jabber.h" | |
| 27 | #include "iq.h" | |
| 28 | #include "sha.h" | |
| 29 | ||
| 30 | #include "debug.h" | |
| 31 | #include "md5.h" | |
| 32 | #include "util.h" | |
| 33 | #include "sslconn.h" | |
| 34 | ||
| 35 | ||
| 36 | void | |
| 37 | jabber_auth_start(JabberStream *js, xmlnode *packet) | |
| 38 | { | |
| 39 | xmlnode *mechs, *mechnode; | |
| 40 | xmlnode *starttls; | |
| 7291 | 41 | xmlnode *auth; |
| 7014 | 42 | |
| 7291 | 43 | gboolean digest_md5 = FALSE, plain=FALSE; |
| 7014 | 44 | |
| 7157 | 45 | if((starttls = xmlnode_get_child(packet, "starttls"))) { |
| 7630 | 46 | if(gaim_account_get_bool(js->gc->account, "use_tls", TRUE) && |
| 47 | gaim_ssl_is_supported()) { | |
| 7157 | 48 | jabber_send_raw(js, |
| 7642 | 49 | "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>", -1); |
| 7157 | 50 | return; |
| 51 | } else if(xmlnode_get_child(starttls, "required")) { | |
| 52 | gaim_connection_error(js->gc, _("Server requires SSL for login")); | |
| 53 | return; | |
| 54 | } | |
| 7014 | 55 | } |
| 56 | ||
| 8016 | 57 | if(js->registration) { |
| 58 | jabber_register_start(js); | |
| 59 | return; | |
| 60 | } | |
| 61 | ||
| 7014 | 62 | mechs = xmlnode_get_child(packet, "mechanisms"); |
| 63 | ||
| 64 | if(!mechs) { | |
| 7981 | 65 | gaim_connection_error(js->gc, _("Invalid response from server.")); |
| 7014 | 66 | return; |
| 67 | } | |
| 68 | ||
| 69 | for(mechnode = mechs->child; mechnode; mechnode = mechnode->next) | |
| 70 | { | |
| 71 | if(mechnode->type == NODE_TYPE_TAG) { | |
| 72 | char *mech_name = xmlnode_get_data(mechnode); | |
| 73 | if(mech_name && !strcmp(mech_name, "DIGEST-MD5")) | |
| 74 | digest_md5 = TRUE; | |
| 7291 | 75 | else if(mech_name && !strcmp(mech_name, "PLAIN")) |
| 76 | plain = TRUE; | |
| 7014 | 77 | g_free(mech_name); |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 7291 | 81 | auth = xmlnode_new("auth"); |
| 82 | xmlnode_set_attrib(auth, "xmlns", "urn:ietf:params:xml:ns:xmpp-sasl"); | |
| 7703 | 83 | |
| 7645 | 84 | if(digest_md5) { |
| 7291 | 85 | xmlnode_set_attrib(auth, "mechanism", "DIGEST-MD5"); |
| 86 | js->auth_type = JABBER_AUTH_DIGEST_MD5; | |
| 7704 | 87 | } else if(plain && js->gsc != NULL) { /* only do plain if we're encrypted */ |
| 7703 | 88 | GString *response = g_string_new(""); |
| 89 | char *enc_out; | |
| 90 | ||
| 91 | response = g_string_append_len(response, "\0", 1); | |
| 92 | response = g_string_append(response, js->user->node); | |
| 93 | response = g_string_append_len(response, "\0", 1); | |
| 94 | response = g_string_append(response, | |
| 95 | gaim_account_get_password(js->gc->account)); | |
| 96 | ||
| 97 | enc_out = gaim_base64_encode(response->str, response->len); | |
| 98 | ||
| 7642 | 99 | xmlnode_set_attrib(auth, "mechanism", "PLAIN"); |
| 7703 | 100 | xmlnode_insert_data(auth, enc_out, -1); |
| 101 | g_free(enc_out); | |
| 102 | ||
| 7291 | 103 | js->auth_type = JABBER_AUTH_PLAIN; |
| 7014 | 104 | } else { |
| 105 | gaim_connection_error(js->gc, | |
| 106 | _("Server does not use any supported authentication method")); | |
| 7291 | 107 | xmlnode_free(auth); |
| 108 | return; | |
| 7014 | 109 | } |
| 7703 | 110 | |
| 7291 | 111 | jabber_send(js, auth); |
| 112 | xmlnode_free(auth); | |
| 7014 | 113 | } |
| 114 | ||
| 7395 | 115 | static void auth_old_result_cb(JabberStream *js, xmlnode *packet, gpointer data) |
| 7014 | 116 | { |
| 117 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 118 | ||
| 7730 | 119 | if(type && !strcmp(type, "result")) { |
| 120 | jabber_stream_set_state(js, JABBER_STREAM_CONNECTED); | |
| 121 | } else { | |
| 7014 | 122 | xmlnode *error = xmlnode_get_child(packet, "error"); |
| 7730 | 123 | const char *err_code = NULL; |
| 124 | char *err_text = NULL; | |
| 7014 | 125 | char *buf; |
| 126 | ||
| 7730 | 127 | if(error) { |
| 128 | err_code = xmlnode_get_attrib(error, "code"); | |
| 129 | err_text = xmlnode_get_data(error); | |
| 130 | } | |
| 7014 | 131 | |
| 132 | if(!err_code) | |
| 133 | err_code = ""; | |
| 134 | if(!err_text) | |
| 135 | err_text = g_strdup(_("Unknown")); | |
| 136 | ||
| 137 | if(!strcmp(err_code, "401")) | |
| 138 | js->gc->wants_to_die = TRUE; | |
| 139 | ||
| 140 | buf = g_strdup_printf("Error %s: %s", | |
| 141 | err_code, err_text); | |
| 142 | ||
| 143 | gaim_connection_error(js->gc, buf); | |
| 144 | g_free(err_text); | |
| 145 | g_free(buf); | |
| 146 | } | |
| 147 | } | |
| 148 | ||
| 7395 | 149 | static void auth_old_cb(JabberStream *js, xmlnode *packet, gpointer data) |
| 7014 | 150 | { |
| 151 | JabberIq *iq; | |
| 152 | xmlnode *query, *x; | |
| 153 | gboolean digest = FALSE; | |
| 7514 | 154 | const char *type = xmlnode_get_attrib(packet, "type"); |
| 7014 | 155 | const char *pw = gaim_account_get_password(js->gc->account); |
| 156 | ||
| 7514 | 157 | if(!type) { |
| 7981 | 158 | gaim_connection_error(js->gc, _("Invalid response from server.")); |
| 7014 | 159 | return; |
| 7515 | 160 | } else if(!strcmp(type, "error")) { |
| 7644 | 161 | /* XXX: still need to handle XMPP-style errors */ |
| 162 | xmlnode *error; | |
| 163 | char *buf, *err_txt = NULL; | |
| 164 | const char *code = NULL; | |
| 165 | if((error = xmlnode_get_child(packet, "error"))) { | |
| 166 | code = xmlnode_get_attrib(error, "code"); | |
| 167 | err_txt = xmlnode_get_data(error); | |
| 168 | } | |
| 169 | buf = g_strdup_printf("%s%s%s", code ? code : "", code ? ": " : "", | |
| 170 | err_txt ? err_txt : _("Unknown Error")); | |
| 171 | gaim_connection_error(js->gc, buf); | |
| 172 | if(err_txt) | |
| 173 | g_free(err_txt); | |
| 174 | g_free(buf); | |
| 7515 | 175 | } else if(!strcmp(type, "result")) { |
| 7514 | 176 | query = xmlnode_get_child(packet, "query"); |
| 177 | if(js->stream_id && xmlnode_get_child(query, "digest")) { | |
| 178 | digest = TRUE; | |
| 179 | } else if(!xmlnode_get_child(query, "password")) { | |
| 180 | gaim_connection_error(js->gc, | |
| 181 | _("Server does not use any supported authentication method")); | |
| 182 | return; | |
| 183 | } | |
| 7014 | 184 | |
| 7514 | 185 | iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:auth"); |
| 186 | query = xmlnode_get_child(iq->node, "query"); | |
| 187 | x = xmlnode_new_child(query, "username"); | |
| 188 | xmlnode_insert_data(x, js->user->node, -1); | |
| 189 | x = xmlnode_new_child(query, "resource"); | |
| 190 | xmlnode_insert_data(x, js->user->resource, -1); | |
| 7014 | 191 | |
| 7514 | 192 | if(digest) { |
| 193 | unsigned char hashval[20]; | |
| 194 | char *s, h[41], *p; | |
| 195 | int i; | |
| 7014 | 196 | |
| 7514 | 197 | x = xmlnode_new_child(query, "digest"); |
| 198 | s = g_strdup_printf("%s%s", js->stream_id, pw); | |
| 199 | shaBlock((unsigned char *)s, strlen(s), hashval); | |
| 200 | p = h; | |
| 201 | for(i=0; i<20; i++, p+=2) | |
| 202 | snprintf(p, 3, "%02x", hashval[i]); | |
| 203 | xmlnode_insert_data(x, h, -1); | |
| 204 | g_free(s); | |
| 205 | } else { | |
| 206 | x = xmlnode_new_child(query, "password"); | |
| 207 | xmlnode_insert_data(x, pw, -1); | |
| 208 | } | |
| 209 | ||
| 210 | jabber_iq_set_callback(iq, auth_old_result_cb, NULL); | |
| 211 | ||
| 212 | jabber_iq_send(iq); | |
| 7014 | 213 | } |
| 214 | } | |
| 215 | ||
| 216 | void jabber_auth_start_old(JabberStream *js) | |
| 217 | { | |
| 218 | JabberIq *iq; | |
| 219 | xmlnode *query, *username; | |
| 220 | ||
| 221 | iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:auth"); | |
| 222 | ||
| 223 | query = xmlnode_get_child(iq->node, "query"); | |
| 224 | username = xmlnode_new_child(query, "username"); | |
| 225 | xmlnode_insert_data(username, js->user->node, -1); | |
| 226 | ||
| 7395 | 227 | jabber_iq_set_callback(iq, auth_old_cb, NULL); |
| 7014 | 228 | |
| 229 | jabber_iq_send(iq); | |
| 230 | } | |
| 231 | ||
| 232 | static GHashTable* parse_challenge(const char *challenge) | |
| 233 | { | |
| 234 | GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, | |
| 235 | g_free, g_free); | |
| 236 | char **pairs; | |
| 237 | int i; | |
| 238 | ||
| 239 | pairs = g_strsplit(challenge, ",", -1); | |
| 240 | ||
| 241 | for(i=0; pairs[i]; i++) { | |
| 242 | char **keyval = g_strsplit(pairs[i], "=", 2); | |
| 243 | if(keyval[0] && keyval[1]) { | |
| 244 | if(keyval[1][0] == '"' && keyval[1][strlen(keyval[1])-1] == '"') | |
| 245 | g_hash_table_replace(ret, g_strdup(keyval[0]), g_strndup(keyval[1]+1, strlen(keyval[1])-2)); | |
| 246 | else | |
| 247 | g_hash_table_replace(ret, g_strdup(keyval[0]), g_strdup(keyval[1])); | |
| 248 | } | |
| 249 | g_strfreev(keyval); | |
| 250 | } | |
| 251 | ||
| 252 | g_strfreev(pairs); | |
| 253 | ||
| 254 | return ret; | |
| 255 | } | |
| 256 | ||
| 257 | static unsigned char* | |
| 258 | generate_response_value(JabberID *jid, const char *passwd, const char *nonce, | |
| 7267 | 259 | const char *cnonce, const char *a2, const char *realm) |
| 7014 | 260 | { |
| 261 | md5_state_t ctx; | |
| 262 | md5_byte_t result[16]; | |
| 263 | ||
| 264 | char *x, *y, *a1, *ha1, *ha2, *kd, *z; | |
| 265 | ||
| 7267 | 266 | x = g_strdup_printf("%s:%s:%s", jid->node, realm, passwd); |
| 7014 | 267 | md5_init(&ctx); |
| 268 | md5_append(&ctx, x, strlen(x)); | |
| 269 | md5_finish(&ctx, result); | |
| 270 | ||
| 271 | y = g_strndup(result, 16); | |
| 272 | ||
| 7395 | 273 | a1 = g_strdup_printf("%s:%s:%s:%s@%s", y, nonce, cnonce, jid->node, |
| 274 | jid->domain); | |
| 7014 | 275 | |
| 276 | md5_init(&ctx); | |
| 277 | md5_append(&ctx, a1, strlen(a1)); | |
| 278 | md5_finish(&ctx, result); | |
| 279 | ||
|
7106
eaeff5775818
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
280 | ha1 = gaim_base16_encode(result, 16); |
| 7014 | 281 | |
| 282 | md5_init(&ctx); | |
| 283 | md5_append(&ctx, a2, strlen(a2)); | |
| 284 | md5_finish(&ctx, result); | |
| 285 | ||
|
7106
eaeff5775818
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
286 | ha2 = gaim_base16_encode(result, 16); |
| 7014 | 287 | |
| 288 | kd = g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1, nonce, cnonce, ha2); | |
| 289 | ||
| 290 | md5_init(&ctx); | |
| 291 | md5_append(&ctx, kd, strlen(kd)); | |
| 292 | md5_finish(&ctx, result); | |
| 293 | ||
|
7106
eaeff5775818
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
294 | z = gaim_base16_encode(result, 16); |
| 7014 | 295 | |
| 296 | g_free(x); | |
| 297 | g_free(y); | |
| 298 | g_free(a1); | |
| 299 | g_free(ha1); | |
| 300 | g_free(ha2); | |
| 301 | g_free(kd); | |
| 302 | ||
| 303 | return z; | |
| 304 | } | |
| 305 | ||
| 306 | void | |
| 307 | jabber_auth_handle_challenge(JabberStream *js, xmlnode *packet) | |
| 308 | { | |
| 309 | ||
| 7703 | 310 | if(js->auth_type == JABBER_AUTH_DIGEST_MD5) { |
| 7291 | 311 | char *enc_in = xmlnode_get_data(packet); |
| 312 | char *dec_in; | |
| 313 | char *enc_out; | |
| 314 | GHashTable *parts; | |
| 7014 | 315 | |
| 7395 | 316 | if(!enc_in) { |
| 7981 | 317 | gaim_connection_error(js->gc, _("Invalid response from server.")); |
| 7395 | 318 | return; |
| 319 | } | |
| 320 | ||
| 7291 | 321 | gaim_base64_decode(enc_in, &dec_in, NULL); |
| 7395 | 322 | gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded challenge (%d): %s\n", |
| 323 | strlen(dec_in), dec_in); | |
| 7291 | 324 | |
| 325 | parts = parse_challenge(dec_in); | |
| 7014 | 326 | |
| 327 | ||
| 7291 | 328 | if (g_hash_table_lookup(parts, "rspauth")) { |
| 329 | char *rspauth = g_hash_table_lookup(parts, "rspauth"); | |
| 7014 | 330 | |
| 331 | ||
| 7291 | 332 | if(rspauth && js->expected_rspauth && |
| 333 | !strcmp(rspauth, js->expected_rspauth)) { | |
| 334 | jabber_send_raw(js, | |
| 7642 | 335 | "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", |
| 336 | -1); | |
| 7291 | 337 | } else { |
| 338 | gaim_connection_error(js->gc, _("Invalid challenge from server")); | |
| 339 | } | |
| 340 | g_free(js->expected_rspauth); | |
| 341 | } else { | |
| 342 | /* assemble a response, and send it */ | |
| 343 | /* see RFC 2831 */ | |
| 344 | GString *response = g_string_new(""); | |
| 345 | char *a2; | |
| 346 | char *auth_resp; | |
| 347 | char *buf; | |
| 348 | char *cnonce; | |
| 349 | char *realm; | |
| 350 | char *nonce; | |
| 7014 | 351 | |
| 7291 | 352 | /* we're actually supposed to prompt the user for a realm if |
| 353 | * the server doesn't send one, but that really complicates things, | |
| 354 | * so i'm not gonna worry about it until is poses a problem to | |
| 355 | * someone, or I get really bored */ | |
| 356 | realm = g_hash_table_lookup(parts, "realm"); | |
| 357 | if(!realm) | |
| 358 | realm = js->user->domain; | |
| 7014 | 359 | |
| 7291 | 360 | cnonce = g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL), |
| 361 | g_random_int()); | |
| 362 | nonce = g_hash_table_lookup(parts, "nonce"); | |
| 7014 | 363 | |
| 364 | ||
| 7291 | 365 | a2 = g_strdup_printf("AUTHENTICATE:xmpp/%s", realm); |
| 366 | auth_resp = generate_response_value(js->user, | |
| 367 | gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
| 368 | g_free(a2); | |
| 369 | ||
| 370 | a2 = g_strdup_printf(":xmpp/%s", realm); | |
| 371 | js->expected_rspauth = generate_response_value(js->user, | |
| 372 | gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
| 373 | g_free(a2); | |
| 374 | ||
| 375 | ||
| 376 | g_string_append_printf(response, "username=\"%s\"", js->user->node); | |
| 377 | g_string_append_printf(response, ",realm=\"%s\"", realm); | |
| 378 | g_string_append_printf(response, ",nonce=\"%s\"", nonce); | |
| 379 | g_string_append_printf(response, ",cnonce=\"%s\"", cnonce); | |
| 380 | g_string_append_printf(response, ",nc=00000001"); | |
| 381 | g_string_append_printf(response, ",qop=auth"); | |
| 382 | g_string_append_printf(response, ",digest-uri=\"xmpp/%s\"", realm); | |
| 383 | g_string_append_printf(response, ",response=%s", auth_resp); | |
| 384 | g_string_append_printf(response, ",charset=utf-8"); | |
| 7395 | 385 | g_string_append_printf(response, ",authzid=\"%s@%s\"", |
| 386 | js->user->node, js->user->domain); | |
| 7291 | 387 | |
| 388 | g_free(auth_resp); | |
| 389 | g_free(cnonce); | |
| 390 | ||
| 391 | enc_out = gaim_base64_encode(response->str, response->len); | |
| 392 | ||
| 393 | gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded response (%d): %s\n", response->len, response->str); | |
| 394 | ||
| 395 | buf = g_strdup_printf("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>", enc_out); | |
| 396 | ||
| 7642 | 397 | jabber_send_raw(js, buf, -1); |
| 7291 | 398 | |
| 399 | g_free(buf); | |
| 400 | ||
| 401 | g_free(enc_out); | |
| 402 | ||
| 403 | g_string_free(response, TRUE); | |
| 7014 | 404 | } |
| 7291 | 405 | |
| 406 | g_free(enc_in); | |
| 407 | g_free(dec_in); | |
| 408 | g_hash_table_destroy(parts); | |
| 7014 | 409 | } |
| 410 | } | |
| 411 | ||
| 412 | void jabber_auth_handle_success(JabberStream *js, xmlnode *packet) | |
| 413 | { | |
| 414 | const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
| 415 | ||
| 416 | if(!ns || strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
| 7981 | 417 | gaim_connection_error(js->gc, _("Invalid response from server.")); |
| 7014 | 418 | return; |
| 419 | } | |
| 420 | ||
| 421 | jabber_stream_set_state(js, JABBER_STREAM_REINITIALIZING); | |
| 422 | } | |
| 423 | ||
| 424 | void jabber_auth_handle_failure(JabberStream *js, xmlnode *packet) | |
| 425 | { | |
| 426 | const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
| 427 | ||
| 428 | if(!ns) | |
| 7981 | 429 | gaim_connection_error(js->gc, _("Invalid response from server.")); |
| 7014 | 430 | else if(!strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { |
| 431 | if(xmlnode_get_child(packet, "bad-protocol")) { | |
| 432 | gaim_connection_error(js->gc, _("Bad Protocol")); | |
| 433 | } else if(xmlnode_get_child(packet, "encryption-required")) { | |
| 434 | js->gc->wants_to_die = TRUE; | |
| 435 | gaim_connection_error(js->gc, _("Encryption Required")); | |
| 436 | } else if(xmlnode_get_child(packet, "invalid-authzid")) { | |
| 437 | js->gc->wants_to_die = TRUE; | |
| 438 | gaim_connection_error(js->gc, _("Invalid authzid")); | |
| 439 | } else if(xmlnode_get_child(packet, "invalid-mechanism")) { | |
| 440 | js->gc->wants_to_die = TRUE; | |
| 441 | gaim_connection_error(js->gc, _("Invalid Mechanism")); | |
| 442 | } else if(xmlnode_get_child(packet, "invalid-realm")) { | |
| 443 | gaim_connection_error(js->gc, _("Invalid Realm")); | |
| 444 | } else if(xmlnode_get_child(packet, "mechanism-too-weak")) { | |
| 445 | js->gc->wants_to_die = TRUE; | |
| 446 | gaim_connection_error(js->gc, _("Mechanism Too Weak")); | |
| 447 | } else if(xmlnode_get_child(packet, "not-authorized")) { | |
| 448 | js->gc->wants_to_die = TRUE; | |
| 449 | gaim_connection_error(js->gc, _("Not Authorized")); | |
| 450 | } else if(xmlnode_get_child(packet, "temporary-auth-failure")) { | |
| 451 | gaim_connection_error(js->gc, | |
| 452 | _("Temporary Authentication Failure")); | |
| 453 | } else { | |
| 454 | gaim_connection_error(js->gc, _("Authentication Failure")); | |
| 455 | } | |
| 456 | } | |
| 457 | } |