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