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