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