Mercurial > pidgin
annotate src/protocols/jabber/auth.c @ 7824:1663c076a744
[gaim-migrate @ 8476]
Changes from revo/shx. They're still all #if 0'ed out and what not.
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Wed, 10 Dec 2003 06:34:11 +0000 |
| parents | 8bba34c04c9d |
| children | ad5c8f01882b |
| rev | line source |
|---|---|
| 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 | |
| 57 mechs = xmlnode_get_child(packet, "mechanisms"); | |
| 58 | |
| 59 if(!mechs) { | |
| 60 gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 for(mechnode = mechs->child; mechnode; mechnode = mechnode->next) | |
| 65 { | |
| 66 if(mechnode->type == NODE_TYPE_TAG) { | |
| 67 char *mech_name = xmlnode_get_data(mechnode); | |
| 68 if(mech_name && !strcmp(mech_name, "DIGEST-MD5")) | |
| 69 digest_md5 = TRUE; | |
| 7291 | 70 else if(mech_name && !strcmp(mech_name, "PLAIN")) |
| 71 plain = TRUE; | |
| 7014 | 72 g_free(mech_name); |
| 73 } | |
| 74 } | |
| 75 | |
| 7291 | 76 auth = xmlnode_new("auth"); |
| 77 xmlnode_set_attrib(auth, "xmlns", "urn:ietf:params:xml:ns:xmpp-sasl"); | |
| 7703 | 78 |
| 7645 | 79 if(digest_md5) { |
| 7291 | 80 xmlnode_set_attrib(auth, "mechanism", "DIGEST-MD5"); |
| 81 js->auth_type = JABBER_AUTH_DIGEST_MD5; | |
| 7704 | 82 } else if(plain && js->gsc != NULL) { /* only do plain if we're encrypted */ |
| 7703 | 83 GString *response = g_string_new(""); |
| 84 char *enc_out; | |
| 85 | |
| 86 response = g_string_append_len(response, "\0", 1); | |
| 87 response = g_string_append(response, js->user->node); | |
| 88 response = g_string_append_len(response, "\0", 1); | |
| 89 response = g_string_append(response, | |
| 90 gaim_account_get_password(js->gc->account)); | |
| 91 | |
| 92 enc_out = gaim_base64_encode(response->str, response->len); | |
| 93 | |
| 7642 | 94 xmlnode_set_attrib(auth, "mechanism", "PLAIN"); |
| 7703 | 95 xmlnode_insert_data(auth, enc_out, -1); |
| 96 g_free(enc_out); | |
| 97 | |
| 7291 | 98 js->auth_type = JABBER_AUTH_PLAIN; |
| 7014 | 99 } else { |
| 100 gaim_connection_error(js->gc, | |
| 101 _("Server does not use any supported authentication method")); | |
| 7291 | 102 xmlnode_free(auth); |
| 103 return; | |
| 7014 | 104 } |
| 7703 | 105 |
| 7291 | 106 jabber_send(js, auth); |
| 107 xmlnode_free(auth); | |
| 7014 | 108 } |
| 109 | |
| 7395 | 110 static void auth_old_result_cb(JabberStream *js, xmlnode *packet, gpointer data) |
| 7014 | 111 { |
| 112 const char *type = xmlnode_get_attrib(packet, "type"); | |
| 113 | |
| 7730 | 114 if(type && !strcmp(type, "result")) { |
| 115 jabber_stream_set_state(js, JABBER_STREAM_CONNECTED); | |
| 116 } else { | |
| 7014 | 117 xmlnode *error = xmlnode_get_child(packet, "error"); |
| 7730 | 118 const char *err_code = NULL; |
| 119 char *err_text = NULL; | |
| 7014 | 120 char *buf; |
| 121 | |
| 7730 | 122 if(error) { |
| 123 err_code = xmlnode_get_attrib(error, "code"); | |
| 124 err_text = xmlnode_get_data(error); | |
| 125 } | |
| 7014 | 126 |
| 127 if(!err_code) | |
| 128 err_code = ""; | |
| 129 if(!err_text) | |
| 130 err_text = g_strdup(_("Unknown")); | |
| 131 | |
| 132 if(!strcmp(err_code, "401")) | |
| 133 js->gc->wants_to_die = TRUE; | |
| 134 | |
| 135 buf = g_strdup_printf("Error %s: %s", | |
| 136 err_code, err_text); | |
| 137 | |
| 138 gaim_connection_error(js->gc, buf); | |
| 139 g_free(err_text); | |
| 140 g_free(buf); | |
| 141 } | |
| 142 } | |
| 143 | |
| 7395 | 144 static void auth_old_cb(JabberStream *js, xmlnode *packet, gpointer data) |
| 7014 | 145 { |
| 146 JabberIq *iq; | |
| 147 xmlnode *query, *x; | |
| 148 gboolean digest = FALSE; | |
| 7514 | 149 const char *type = xmlnode_get_attrib(packet, "type"); |
| 7014 | 150 const char *pw = gaim_account_get_password(js->gc->account); |
| 151 | |
| 7514 | 152 if(!type) { |
| 7813 | 153 gaim_connection_error(js->gc, _("Invalid response from server")); |
| 7014 | 154 return; |
| 7515 | 155 } else if(!strcmp(type, "error")) { |
| 7644 | 156 /* XXX: still need to handle XMPP-style errors */ |
| 157 xmlnode *error; | |
| 158 char *buf, *err_txt = NULL; | |
| 159 const char *code = NULL; | |
| 160 if((error = xmlnode_get_child(packet, "error"))) { | |
| 161 code = xmlnode_get_attrib(error, "code"); | |
| 162 err_txt = xmlnode_get_data(error); | |
| 163 } | |
| 164 buf = g_strdup_printf("%s%s%s", code ? code : "", code ? ": " : "", | |
| 165 err_txt ? err_txt : _("Unknown Error")); | |
| 166 gaim_connection_error(js->gc, buf); | |
| 167 if(err_txt) | |
| 168 g_free(err_txt); | |
| 169 g_free(buf); | |
| 7515 | 170 } else if(!strcmp(type, "result")) { |
| 7514 | 171 query = xmlnode_get_child(packet, "query"); |
| 172 if(js->stream_id && xmlnode_get_child(query, "digest")) { | |
| 173 digest = TRUE; | |
| 174 } else if(!xmlnode_get_child(query, "password")) { | |
| 175 gaim_connection_error(js->gc, | |
| 176 _("Server does not use any supported authentication method")); | |
| 177 return; | |
| 178 } | |
| 7014 | 179 |
| 7514 | 180 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:auth"); |
| 181 query = xmlnode_get_child(iq->node, "query"); | |
| 182 x = xmlnode_new_child(query, "username"); | |
| 183 xmlnode_insert_data(x, js->user->node, -1); | |
| 184 x = xmlnode_new_child(query, "resource"); | |
| 185 xmlnode_insert_data(x, js->user->resource, -1); | |
| 7014 | 186 |
| 7514 | 187 if(digest) { |
| 188 unsigned char hashval[20]; | |
| 189 char *s, h[41], *p; | |
| 190 int i; | |
| 7014 | 191 |
| 7514 | 192 x = xmlnode_new_child(query, "digest"); |
| 193 s = g_strdup_printf("%s%s", js->stream_id, pw); | |
| 194 shaBlock((unsigned char *)s, strlen(s), hashval); | |
| 195 p = h; | |
| 196 for(i=0; i<20; i++, p+=2) | |
| 197 snprintf(p, 3, "%02x", hashval[i]); | |
| 198 xmlnode_insert_data(x, h, -1); | |
| 199 g_free(s); | |
| 200 } else { | |
| 201 x = xmlnode_new_child(query, "password"); | |
| 202 xmlnode_insert_data(x, pw, -1); | |
| 203 } | |
| 204 | |
| 205 jabber_iq_set_callback(iq, auth_old_result_cb, NULL); | |
| 206 | |
| 207 jabber_iq_send(iq); | |
| 7014 | 208 } |
| 209 } | |
| 210 | |
| 211 void jabber_auth_start_old(JabberStream *js) | |
| 212 { | |
| 213 JabberIq *iq; | |
| 214 xmlnode *query, *username; | |
| 215 | |
| 216 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:auth"); | |
| 217 | |
| 218 query = xmlnode_get_child(iq->node, "query"); | |
| 219 username = xmlnode_new_child(query, "username"); | |
| 220 xmlnode_insert_data(username, js->user->node, -1); | |
| 221 | |
| 7395 | 222 jabber_iq_set_callback(iq, auth_old_cb, NULL); |
| 7014 | 223 |
| 224 jabber_iq_send(iq); | |
| 225 } | |
| 226 | |
| 227 static GHashTable* parse_challenge(const char *challenge) | |
| 228 { | |
| 229 GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, | |
| 230 g_free, g_free); | |
| 231 char **pairs; | |
| 232 int i; | |
| 233 | |
| 234 pairs = g_strsplit(challenge, ",", -1); | |
| 235 | |
| 236 for(i=0; pairs[i]; i++) { | |
| 237 char **keyval = g_strsplit(pairs[i], "=", 2); | |
| 238 if(keyval[0] && keyval[1]) { | |
| 239 if(keyval[1][0] == '"' && keyval[1][strlen(keyval[1])-1] == '"') | |
| 240 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strndup(keyval[1]+1, strlen(keyval[1])-2)); | |
| 241 else | |
| 242 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strdup(keyval[1])); | |
| 243 } | |
| 244 g_strfreev(keyval); | |
| 245 } | |
| 246 | |
| 247 g_strfreev(pairs); | |
| 248 | |
| 249 return ret; | |
| 250 } | |
| 251 | |
| 252 static unsigned char* | |
| 253 generate_response_value(JabberID *jid, const char *passwd, const char *nonce, | |
| 7267 | 254 const char *cnonce, const char *a2, const char *realm) |
| 7014 | 255 { |
| 256 md5_state_t ctx; | |
| 257 md5_byte_t result[16]; | |
| 258 | |
| 259 char *x, *y, *a1, *ha1, *ha2, *kd, *z; | |
| 260 | |
| 7267 | 261 x = g_strdup_printf("%s:%s:%s", jid->node, realm, passwd); |
| 7014 | 262 md5_init(&ctx); |
| 263 md5_append(&ctx, x, strlen(x)); | |
| 264 md5_finish(&ctx, result); | |
| 265 | |
| 266 y = g_strndup(result, 16); | |
| 267 | |
| 7395 | 268 a1 = g_strdup_printf("%s:%s:%s:%s@%s", y, nonce, cnonce, jid->node, |
| 269 jid->domain); | |
| 7014 | 270 |
| 271 md5_init(&ctx); | |
| 272 md5_append(&ctx, a1, strlen(a1)); | |
| 273 md5_finish(&ctx, result); | |
| 274 | |
|
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
275 ha1 = gaim_base16_encode(result, 16); |
| 7014 | 276 |
| 277 md5_init(&ctx); | |
| 278 md5_append(&ctx, a2, strlen(a2)); | |
| 279 md5_finish(&ctx, result); | |
| 280 | |
|
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
281 ha2 = gaim_base16_encode(result, 16); |
| 7014 | 282 |
| 283 kd = g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1, nonce, cnonce, ha2); | |
| 284 | |
| 285 md5_init(&ctx); | |
| 286 md5_append(&ctx, kd, strlen(kd)); | |
| 287 md5_finish(&ctx, result); | |
| 288 | |
|
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
289 z = gaim_base16_encode(result, 16); |
| 7014 | 290 |
| 291 g_free(x); | |
| 292 g_free(y); | |
| 293 g_free(a1); | |
| 294 g_free(ha1); | |
| 295 g_free(ha2); | |
| 296 g_free(kd); | |
| 297 | |
| 298 return z; | |
| 299 } | |
| 300 | |
| 301 void | |
| 302 jabber_auth_handle_challenge(JabberStream *js, xmlnode *packet) | |
| 303 { | |
| 304 | |
| 7703 | 305 if(js->auth_type == JABBER_AUTH_DIGEST_MD5) { |
| 7291 | 306 char *enc_in = xmlnode_get_data(packet); |
| 307 char *dec_in; | |
| 308 char *enc_out; | |
| 309 GHashTable *parts; | |
| 7014 | 310 |
| 7395 | 311 if(!enc_in) { |
| 312 gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 313 return; | |
| 314 } | |
| 315 | |
| 7291 | 316 gaim_base64_decode(enc_in, &dec_in, NULL); |
| 7395 | 317 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded challenge (%d): %s\n", |
| 318 strlen(dec_in), dec_in); | |
| 7291 | 319 |
| 320 parts = parse_challenge(dec_in); | |
| 7014 | 321 |
| 322 | |
| 7291 | 323 if (g_hash_table_lookup(parts, "rspauth")) { |
| 324 char *rspauth = g_hash_table_lookup(parts, "rspauth"); | |
| 7014 | 325 |
| 326 | |
| 7291 | 327 if(rspauth && js->expected_rspauth && |
| 328 !strcmp(rspauth, js->expected_rspauth)) { | |
| 329 jabber_send_raw(js, | |
| 7642 | 330 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", |
| 331 -1); | |
| 7291 | 332 } else { |
| 333 gaim_connection_error(js->gc, _("Invalid challenge from server")); | |
| 334 } | |
| 335 g_free(js->expected_rspauth); | |
| 336 } else { | |
| 337 /* assemble a response, and send it */ | |
| 338 /* see RFC 2831 */ | |
| 339 GString *response = g_string_new(""); | |
| 340 char *a2; | |
| 341 char *auth_resp; | |
| 342 char *buf; | |
| 343 char *cnonce; | |
| 344 char *realm; | |
| 345 char *nonce; | |
| 7014 | 346 |
| 7291 | 347 /* we're actually supposed to prompt the user for a realm if |
| 348 * the server doesn't send one, but that really complicates things, | |
| 349 * so i'm not gonna worry about it until is poses a problem to | |
| 350 * someone, or I get really bored */ | |
| 351 realm = g_hash_table_lookup(parts, "realm"); | |
| 352 if(!realm) | |
| 353 realm = js->user->domain; | |
| 7014 | 354 |
| 7291 | 355 cnonce = g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL), |
| 356 g_random_int()); | |
| 357 nonce = g_hash_table_lookup(parts, "nonce"); | |
| 7014 | 358 |
| 359 | |
| 7291 | 360 a2 = g_strdup_printf("AUTHENTICATE:xmpp/%s", realm); |
| 361 auth_resp = generate_response_value(js->user, | |
| 362 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
| 363 g_free(a2); | |
| 364 | |
| 365 a2 = g_strdup_printf(":xmpp/%s", realm); | |
| 366 js->expected_rspauth = generate_response_value(js->user, | |
| 367 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
| 368 g_free(a2); | |
| 369 | |
| 370 | |
| 371 g_string_append_printf(response, "username=\"%s\"", js->user->node); | |
| 372 g_string_append_printf(response, ",realm=\"%s\"", realm); | |
| 373 g_string_append_printf(response, ",nonce=\"%s\"", nonce); | |
| 374 g_string_append_printf(response, ",cnonce=\"%s\"", cnonce); | |
| 375 g_string_append_printf(response, ",nc=00000001"); | |
| 376 g_string_append_printf(response, ",qop=auth"); | |
| 377 g_string_append_printf(response, ",digest-uri=\"xmpp/%s\"", realm); | |
| 378 g_string_append_printf(response, ",response=%s", auth_resp); | |
| 379 g_string_append_printf(response, ",charset=utf-8"); | |
| 7395 | 380 g_string_append_printf(response, ",authzid=\"%s@%s\"", |
| 381 js->user->node, js->user->domain); | |
| 7291 | 382 |
| 383 g_free(auth_resp); | |
| 384 g_free(cnonce); | |
| 385 | |
| 386 enc_out = gaim_base64_encode(response->str, response->len); | |
| 387 | |
| 388 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded response (%d): %s\n", response->len, response->str); | |
| 389 | |
| 390 buf = g_strdup_printf("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>", enc_out); | |
| 391 | |
| 7642 | 392 jabber_send_raw(js, buf, -1); |
| 7291 | 393 |
| 394 g_free(buf); | |
| 395 | |
| 396 g_free(enc_out); | |
| 397 | |
| 398 g_string_free(response, TRUE); | |
| 7014 | 399 } |
| 7291 | 400 |
| 401 g_free(enc_in); | |
| 402 g_free(dec_in); | |
| 403 g_hash_table_destroy(parts); | |
| 7014 | 404 } |
| 405 } | |
| 406 | |
| 407 void jabber_auth_handle_success(JabberStream *js, xmlnode *packet) | |
| 408 { | |
| 409 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
| 410 | |
| 411 if(!ns || strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
| 412 gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 413 return; | |
| 414 } | |
| 415 | |
| 416 jabber_stream_set_state(js, JABBER_STREAM_REINITIALIZING); | |
| 417 } | |
| 418 | |
| 419 void jabber_auth_handle_failure(JabberStream *js, xmlnode *packet) | |
| 420 { | |
| 421 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
| 422 | |
| 423 if(!ns) | |
| 424 gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 425 else if(!strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
| 426 if(xmlnode_get_child(packet, "bad-protocol")) { | |
| 427 gaim_connection_error(js->gc, _("Bad Protocol")); | |
| 428 } else if(xmlnode_get_child(packet, "encryption-required")) { | |
| 429 js->gc->wants_to_die = TRUE; | |
| 430 gaim_connection_error(js->gc, _("Encryption Required")); | |
| 431 } else if(xmlnode_get_child(packet, "invalid-authzid")) { | |
| 432 js->gc->wants_to_die = TRUE; | |
| 433 gaim_connection_error(js->gc, _("Invalid authzid")); | |
| 434 } else if(xmlnode_get_child(packet, "invalid-mechanism")) { | |
| 435 js->gc->wants_to_die = TRUE; | |
| 436 gaim_connection_error(js->gc, _("Invalid Mechanism")); | |
| 437 } else if(xmlnode_get_child(packet, "invalid-realm")) { | |
| 438 gaim_connection_error(js->gc, _("Invalid Realm")); | |
| 439 } else if(xmlnode_get_child(packet, "mechanism-too-weak")) { | |
| 440 js->gc->wants_to_die = TRUE; | |
| 441 gaim_connection_error(js->gc, _("Mechanism Too Weak")); | |
| 442 } else if(xmlnode_get_child(packet, "not-authorized")) { | |
| 443 js->gc->wants_to_die = TRUE; | |
| 444 gaim_connection_error(js->gc, _("Not Authorized")); | |
| 445 } else if(xmlnode_get_child(packet, "temporary-auth-failure")) { | |
| 446 gaim_connection_error(js->gc, | |
| 447 _("Temporary Authentication Failure")); | |
| 448 } else { | |
| 449 gaim_connection_error(js->gc, _("Authentication Failure")); | |
| 450 } | |
| 451 } | |
| 452 } |
