Mercurial > pidgin
annotate src/protocols/jabber/auth.c @ 7746:d7fd01f7bdcb
[gaim-migrate @ 8391]
" These patches fix the raw plugin and the gaiminc plugins so that
they compile and function correctly." --Paul A (darkrain)
committer: Tailor Script <tailor@pidgin.im>
| author | Luke Schierer <lschiere@pidgin.im> |
|---|---|
| date | Thu, 04 Dec 2003 12:12:11 +0000 |
| parents | 01eb54bb41dc |
| children | 8bba34c04c9d |
| 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) { |
| 7014 | 153 return; |
| 7515 | 154 } else if(!strcmp(type, "error")) { |
| 7644 | 155 /* XXX: still need to handle XMPP-style errors */ |
| 156 xmlnode *error; | |
| 157 char *buf, *err_txt = NULL; | |
| 158 const char *code = NULL; | |
| 159 if((error = xmlnode_get_child(packet, "error"))) { | |
| 160 code = xmlnode_get_attrib(error, "code"); | |
| 161 err_txt = xmlnode_get_data(error); | |
| 162 } | |
| 163 buf = g_strdup_printf("%s%s%s", code ? code : "", code ? ": " : "", | |
| 164 err_txt ? err_txt : _("Unknown Error")); | |
| 165 gaim_connection_error(js->gc, buf); | |
| 166 if(err_txt) | |
| 167 g_free(err_txt); | |
| 168 g_free(buf); | |
| 7515 | 169 } else if(!strcmp(type, "result")) { |
| 7514 | 170 query = xmlnode_get_child(packet, "query"); |
| 171 if(js->stream_id && xmlnode_get_child(query, "digest")) { | |
| 172 digest = TRUE; | |
| 173 } else if(!xmlnode_get_child(query, "password")) { | |
| 174 gaim_connection_error(js->gc, | |
| 175 _("Server does not use any supported authentication method")); | |
| 176 return; | |
| 177 } | |
| 7014 | 178 |
| 7514 | 179 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:auth"); |
| 180 query = xmlnode_get_child(iq->node, "query"); | |
| 181 x = xmlnode_new_child(query, "username"); | |
| 182 xmlnode_insert_data(x, js->user->node, -1); | |
| 183 x = xmlnode_new_child(query, "resource"); | |
| 184 xmlnode_insert_data(x, js->user->resource, -1); | |
| 7014 | 185 |
| 7514 | 186 if(digest) { |
| 187 unsigned char hashval[20]; | |
| 188 char *s, h[41], *p; | |
| 189 int i; | |
| 7014 | 190 |
| 7514 | 191 x = xmlnode_new_child(query, "digest"); |
| 192 s = g_strdup_printf("%s%s", js->stream_id, pw); | |
| 193 shaBlock((unsigned char *)s, strlen(s), hashval); | |
| 194 p = h; | |
| 195 for(i=0; i<20; i++, p+=2) | |
| 196 snprintf(p, 3, "%02x", hashval[i]); | |
| 197 xmlnode_insert_data(x, h, -1); | |
| 198 g_free(s); | |
| 199 } else { | |
| 200 x = xmlnode_new_child(query, "password"); | |
| 201 xmlnode_insert_data(x, pw, -1); | |
| 202 } | |
| 203 | |
| 204 jabber_iq_set_callback(iq, auth_old_result_cb, NULL); | |
| 205 | |
| 206 jabber_iq_send(iq); | |
| 7014 | 207 } |
| 208 } | |
| 209 | |
| 210 void jabber_auth_start_old(JabberStream *js) | |
| 211 { | |
| 212 JabberIq *iq; | |
| 213 xmlnode *query, *username; | |
| 214 | |
| 215 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:auth"); | |
| 216 | |
| 217 query = xmlnode_get_child(iq->node, "query"); | |
| 218 username = xmlnode_new_child(query, "username"); | |
| 219 xmlnode_insert_data(username, js->user->node, -1); | |
| 220 | |
| 7395 | 221 jabber_iq_set_callback(iq, auth_old_cb, NULL); |
| 7014 | 222 |
| 223 jabber_iq_send(iq); | |
| 224 } | |
| 225 | |
| 226 static GHashTable* parse_challenge(const char *challenge) | |
| 227 { | |
| 228 GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, | |
| 229 g_free, g_free); | |
| 230 char **pairs; | |
| 231 int i; | |
| 232 | |
| 233 pairs = g_strsplit(challenge, ",", -1); | |
| 234 | |
| 235 for(i=0; pairs[i]; i++) { | |
| 236 char **keyval = g_strsplit(pairs[i], "=", 2); | |
| 237 if(keyval[0] && keyval[1]) { | |
| 238 if(keyval[1][0] == '"' && keyval[1][strlen(keyval[1])-1] == '"') | |
| 239 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strndup(keyval[1]+1, strlen(keyval[1])-2)); | |
| 240 else | |
| 241 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strdup(keyval[1])); | |
| 242 } | |
| 243 g_strfreev(keyval); | |
| 244 } | |
| 245 | |
| 246 g_strfreev(pairs); | |
| 247 | |
| 248 return ret; | |
| 249 } | |
| 250 | |
| 251 static unsigned char* | |
| 252 generate_response_value(JabberID *jid, const char *passwd, const char *nonce, | |
| 7267 | 253 const char *cnonce, const char *a2, const char *realm) |
| 7014 | 254 { |
| 255 md5_state_t ctx; | |
| 256 md5_byte_t result[16]; | |
| 257 | |
| 258 char *x, *y, *a1, *ha1, *ha2, *kd, *z; | |
| 259 | |
| 7267 | 260 x = g_strdup_printf("%s:%s:%s", jid->node, realm, passwd); |
| 7014 | 261 md5_init(&ctx); |
| 262 md5_append(&ctx, x, strlen(x)); | |
| 263 md5_finish(&ctx, result); | |
| 264 | |
| 265 y = g_strndup(result, 16); | |
| 266 | |
| 7395 | 267 a1 = g_strdup_printf("%s:%s:%s:%s@%s", y, nonce, cnonce, jid->node, |
| 268 jid->domain); | |
| 7014 | 269 |
| 270 md5_init(&ctx); | |
| 271 md5_append(&ctx, a1, strlen(a1)); | |
| 272 md5_finish(&ctx, result); | |
| 273 | |
|
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
274 ha1 = gaim_base16_encode(result, 16); |
| 7014 | 275 |
| 276 md5_init(&ctx); | |
| 277 md5_append(&ctx, a2, strlen(a2)); | |
| 278 md5_finish(&ctx, result); | |
| 279 | |
|
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
280 ha2 = gaim_base16_encode(result, 16); |
| 7014 | 281 |
| 282 kd = g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1, nonce, cnonce, ha2); | |
| 283 | |
| 284 md5_init(&ctx); | |
| 285 md5_append(&ctx, kd, strlen(kd)); | |
| 286 md5_finish(&ctx, result); | |
| 287 | |
|
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
288 z = gaim_base16_encode(result, 16); |
| 7014 | 289 |
| 290 g_free(x); | |
| 291 g_free(y); | |
| 292 g_free(a1); | |
| 293 g_free(ha1); | |
| 294 g_free(ha2); | |
| 295 g_free(kd); | |
| 296 | |
| 297 return z; | |
| 298 } | |
| 299 | |
| 300 void | |
| 301 jabber_auth_handle_challenge(JabberStream *js, xmlnode *packet) | |
| 302 { | |
| 303 | |
| 7703 | 304 if(js->auth_type == JABBER_AUTH_DIGEST_MD5) { |
| 7291 | 305 char *enc_in = xmlnode_get_data(packet); |
| 306 char *dec_in; | |
| 307 char *enc_out; | |
| 308 GHashTable *parts; | |
| 7014 | 309 |
| 7395 | 310 if(!enc_in) { |
| 311 gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 312 return; | |
| 313 } | |
| 314 | |
| 7291 | 315 gaim_base64_decode(enc_in, &dec_in, NULL); |
| 7395 | 316 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded challenge (%d): %s\n", |
| 317 strlen(dec_in), dec_in); | |
| 7291 | 318 |
| 319 parts = parse_challenge(dec_in); | |
| 7014 | 320 |
| 321 | |
| 7291 | 322 if (g_hash_table_lookup(parts, "rspauth")) { |
| 323 char *rspauth = g_hash_table_lookup(parts, "rspauth"); | |
| 7014 | 324 |
| 325 | |
| 7291 | 326 if(rspauth && js->expected_rspauth && |
| 327 !strcmp(rspauth, js->expected_rspauth)) { | |
| 328 jabber_send_raw(js, | |
| 7642 | 329 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", |
| 330 -1); | |
| 7291 | 331 } else { |
| 332 gaim_connection_error(js->gc, _("Invalid challenge from server")); | |
| 333 } | |
| 334 g_free(js->expected_rspauth); | |
| 335 } else { | |
| 336 /* assemble a response, and send it */ | |
| 337 /* see RFC 2831 */ | |
| 338 GString *response = g_string_new(""); | |
| 339 char *a2; | |
| 340 char *auth_resp; | |
| 341 char *buf; | |
| 342 char *cnonce; | |
| 343 char *realm; | |
| 344 char *nonce; | |
| 7014 | 345 |
| 7291 | 346 /* we're actually supposed to prompt the user for a realm if |
| 347 * the server doesn't send one, but that really complicates things, | |
| 348 * so i'm not gonna worry about it until is poses a problem to | |
| 349 * someone, or I get really bored */ | |
| 350 realm = g_hash_table_lookup(parts, "realm"); | |
| 351 if(!realm) | |
| 352 realm = js->user->domain; | |
| 7014 | 353 |
| 7291 | 354 cnonce = g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL), |
| 355 g_random_int()); | |
| 356 nonce = g_hash_table_lookup(parts, "nonce"); | |
| 7014 | 357 |
| 358 | |
| 7291 | 359 a2 = g_strdup_printf("AUTHENTICATE:xmpp/%s", realm); |
| 360 auth_resp = generate_response_value(js->user, | |
| 361 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
| 362 g_free(a2); | |
| 363 | |
| 364 a2 = g_strdup_printf(":xmpp/%s", realm); | |
| 365 js->expected_rspauth = generate_response_value(js->user, | |
| 366 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
| 367 g_free(a2); | |
| 368 | |
| 369 | |
| 370 g_string_append_printf(response, "username=\"%s\"", js->user->node); | |
| 371 g_string_append_printf(response, ",realm=\"%s\"", realm); | |
| 372 g_string_append_printf(response, ",nonce=\"%s\"", nonce); | |
| 373 g_string_append_printf(response, ",cnonce=\"%s\"", cnonce); | |
| 374 g_string_append_printf(response, ",nc=00000001"); | |
| 375 g_string_append_printf(response, ",qop=auth"); | |
| 376 g_string_append_printf(response, ",digest-uri=\"xmpp/%s\"", realm); | |
| 377 g_string_append_printf(response, ",response=%s", auth_resp); | |
| 378 g_string_append_printf(response, ",charset=utf-8"); | |
| 7395 | 379 g_string_append_printf(response, ",authzid=\"%s@%s\"", |
| 380 js->user->node, js->user->domain); | |
| 7291 | 381 |
| 382 g_free(auth_resp); | |
| 383 g_free(cnonce); | |
| 384 | |
| 385 enc_out = gaim_base64_encode(response->str, response->len); | |
| 386 | |
| 387 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded response (%d): %s\n", response->len, response->str); | |
| 388 | |
| 389 buf = g_strdup_printf("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>", enc_out); | |
| 390 | |
| 7642 | 391 jabber_send_raw(js, buf, -1); |
| 7291 | 392 |
| 393 g_free(buf); | |
| 394 | |
| 395 g_free(enc_out); | |
| 396 | |
| 397 g_string_free(response, TRUE); | |
| 7014 | 398 } |
| 7291 | 399 |
| 400 g_free(enc_in); | |
| 401 g_free(dec_in); | |
| 402 g_hash_table_destroy(parts); | |
| 7014 | 403 } |
| 404 } | |
| 405 | |
| 406 void jabber_auth_handle_success(JabberStream *js, xmlnode *packet) | |
| 407 { | |
| 408 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
| 409 | |
| 410 if(!ns || strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
| 411 gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 412 return; | |
| 413 } | |
| 414 | |
| 415 jabber_stream_set_state(js, JABBER_STREAM_REINITIALIZING); | |
| 416 } | |
| 417 | |
| 418 void jabber_auth_handle_failure(JabberStream *js, xmlnode *packet) | |
| 419 { | |
| 420 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
| 421 | |
| 422 if(!ns) | |
| 423 gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 424 else if(!strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
| 425 if(xmlnode_get_child(packet, "bad-protocol")) { | |
| 426 gaim_connection_error(js->gc, _("Bad Protocol")); | |
| 427 } else if(xmlnode_get_child(packet, "encryption-required")) { | |
| 428 js->gc->wants_to_die = TRUE; | |
| 429 gaim_connection_error(js->gc, _("Encryption Required")); | |
| 430 } else if(xmlnode_get_child(packet, "invalid-authzid")) { | |
| 431 js->gc->wants_to_die = TRUE; | |
| 432 gaim_connection_error(js->gc, _("Invalid authzid")); | |
| 433 } else if(xmlnode_get_child(packet, "invalid-mechanism")) { | |
| 434 js->gc->wants_to_die = TRUE; | |
| 435 gaim_connection_error(js->gc, _("Invalid Mechanism")); | |
| 436 } else if(xmlnode_get_child(packet, "invalid-realm")) { | |
| 437 gaim_connection_error(js->gc, _("Invalid Realm")); | |
| 438 } else if(xmlnode_get_child(packet, "mechanism-too-weak")) { | |
| 439 js->gc->wants_to_die = TRUE; | |
| 440 gaim_connection_error(js->gc, _("Mechanism Too Weak")); | |
| 441 } else if(xmlnode_get_child(packet, "not-authorized")) { | |
| 442 js->gc->wants_to_die = TRUE; | |
| 443 gaim_connection_error(js->gc, _("Not Authorized")); | |
| 444 } else if(xmlnode_get_child(packet, "temporary-auth-failure")) { | |
| 445 gaim_connection_error(js->gc, | |
| 446 _("Temporary Authentication Failure")); | |
| 447 } else { | |
| 448 gaim_connection_error(js->gc, _("Authentication Failure")); | |
| 449 } | |
| 450 } | |
| 451 } |
