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