Mercurial > pidgin.yaz
annotate src/protocols/jabber/auth.c @ 12543:09cae5fdd5b6
[gaim-migrate @ 14860]
cyrus sasl works for PLAIN now too, and it even logs!
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sat, 17 Dec 2005 22:12:15 +0000 |
parents | b0ba132271a9 |
children | 1e0caf8f40dc |
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 | |
29 #include "debug.h" | |
30 #include "util.h" | |
10684
72a5babfa8b4
[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 | 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 | 77 gaim_connection_get_password(js->gc)); |
8397 | 78 |
11137 | 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 | 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 | |
12508 | 117 #ifdef HAVE_CYRUS_SASL |
118 | |
119 static void jabber_auth_start_cyrus(JabberStream *); | |
120 | |
121 /* Callbacks for Cyrus SASL */ | |
122 | |
123 static int jabber_sasl_cb_realm(void *ctx, int id, const char **avail, const char **result) | |
124 { | |
125 JabberStream *js = (JabberStream *)ctx; | |
126 | |
127 if (id != SASL_CB_GETREALM || !result) return SASL_BADPARAM; | |
128 | |
129 *result = js->user->domain; | |
130 | |
131 return SASL_OK; | |
132 } | |
133 | |
134 static int jabber_sasl_cb_simple(void *ctx, int id, const char **res, unsigned *len) | |
135 { | |
136 JabberStream *js = (JabberStream *)ctx; | |
137 | |
138 switch(id) { | |
139 case SASL_CB_AUTHNAME: | |
140 *res = js->user->node; | |
141 break; | |
142 case SASL_CB_USER: | |
12543 | 143 *res = ""; |
12508 | 144 break; |
145 default: | |
146 return SASL_BADPARAM; | |
147 } | |
148 if (len) *len = strlen((char *)*res); | |
149 return SASL_OK; | |
150 } | |
151 | |
152 static int jabber_sasl_cb_secret(sasl_conn_t *conn, void *ctx, int id, sasl_secret_t **secret) | |
153 { | |
154 JabberStream *js = (JabberStream *)ctx; | |
155 const char *pw = gaim_account_get_password(js->gc->account); | |
156 size_t len; | |
157 static sasl_secret_t *x = NULL; | |
158 | |
159 if (!conn || !secret || id != SASL_CB_PASS) | |
160 return SASL_BADPARAM; | |
161 | |
162 len = strlen(pw); | |
163 x = (sasl_secret_t *) realloc(x, sizeof(sasl_secret_t) + len); | |
164 | |
165 if (!x) | |
166 return SASL_NOMEM; | |
167 | |
168 x->len = len; | |
169 strcpy((char*)x->data, pw); | |
170 | |
171 *secret = x; | |
172 return SASL_OK; | |
173 } | |
174 | |
175 static void allow_cyrus_plaintext_auth(GaimAccount *account) | |
176 { | |
177 gaim_account_set_bool(account, "auth_plain_in_clear", TRUE); | |
178 | |
179 jabber_auth_start_cyrus(account->gc->proto_data); | |
180 } | |
181 | |
182 static void jabber_auth_start_cyrus(JabberStream *js) | |
183 { | |
184 const char *clientout, *mech; | |
185 char *enc_out; | |
186 unsigned coutlen; | |
187 xmlnode *auth; | |
188 sasl_security_properties_t secprops; | |
189 gboolean again; | |
190 gboolean plaintext = TRUE; | |
191 | |
192 /* Set up security properties and options */ | |
193 secprops.min_ssf = 0; | |
194 secprops.security_flags = SASL_SEC_NOANONYMOUS; | |
12543 | 195 secprops.max_ssf = -1; |
196 secprops.maxbufsize = -1; | |
12508 | 197 |
198 if (!js->gsc) { | |
199 plaintext = gaim_account_get_bool(js->gc->account, "auth_plain_in_clear", FALSE); | |
200 if (!plaintext) | |
201 secprops.security_flags |= SASL_SEC_NOPLAINTEXT; | |
202 } else { | |
12540 | 203 plaintext = TRUE; |
12508 | 204 } |
205 secprops.property_names = 0; | |
206 secprops.property_values = 0; | |
207 | |
208 do { | |
209 again = FALSE; | |
210 /* Use the user's domain for compatibility with the old | |
211 * DIGESTMD5 code. Note that this may cause problems where | |
212 * the user's domain doesn't match the FQDN of the jabber | |
213 * service | |
214 */ | |
215 | |
216 js->sasl_state = sasl_client_new("xmpp", js->user->domain, NULL, NULL, js->sasl_cb, 0, &js->sasl); | |
217 if (js->sasl_state==SASL_OK) { | |
218 sasl_setprop(js->sasl, SASL_SEC_PROPS, &secprops); | |
12543 | 219 gaim_debug_info("sasl", "Mechs found: %s\n", js->sasl_mechs->str); |
12508 | 220 js->sasl_state = sasl_client_start(js->sasl, js->sasl_mechs->str, NULL, &clientout, &coutlen, &mech); |
221 } | |
222 switch (js->sasl_state) { | |
223 /* Success */ | |
12543 | 224 case SASL_OK: |
12508 | 225 case SASL_CONTINUE: |
226 break; | |
227 case SASL_NOMECH: | |
228 /* No mechanisms do what we want. See if we can add | |
229 * plaintext ones to the list. */ | |
230 | |
231 if (!gaim_account_get_password(js->gc->account)) { | |
232 gaim_connection_error(js->gc, _("Server couldn't authenticate you without a password")); | |
233 return; | |
234 } else if (!plaintext) { | |
235 gaim_request_yes_no(js->gc, _("Plaintext Authentication"), | |
236 _("Plaintext Authentication"), | |
237 _("This server requires plaintext authentication over an unencrypted connection. Allow this and continue authentication?"), | |
238 2, js->gc->account, | |
239 allow_cyrus_plaintext_auth, | |
240 disallow_plaintext_auth); | |
241 return; | |
242 } else { | |
243 gaim_connection_error(js->gc, _("Server does not use any supported authentication method")); | |
244 return; | |
245 } | |
246 /* not reached */ | |
247 break; | |
248 | |
249 /* Fatal errors. Give up and go home */ | |
250 case SASL_BADPARAM: | |
251 case SASL_NOMEM: | |
252 break; | |
253 | |
254 /* For everything else, fail the mechanism and try again */ | |
255 default: | |
12543 | 256 gaim_debug_info("sasl", "sasl_state is %d, failing the mech and trying again\n", js->sasl_state); |
12508 | 257 if (strlen(mech)>0) { |
258 char *pos; | |
259 pos = strstr(js->sasl_mechs->str,mech); | |
260 g_assert(pos!=NULL); | |
261 g_string_erase(js->sasl_mechs, pos-js->sasl_mechs->str,strlen(mech)); | |
262 } | |
263 sasl_dispose(&js->sasl); | |
264 again=TRUE; | |
265 } | |
266 } while (again); | |
267 | |
12543 | 268 if (js->sasl_state == SASL_CONTINUE || js->sasl_state == SASL_OK) { |
12508 | 269 auth = xmlnode_new("auth"); |
270 xmlnode_set_attrib(auth, "xmlns", "urn:ietf:params:xml:ns:xmpp-sasl"); | |
271 xmlnode_set_attrib(auth,"mechanism", mech); | |
272 if (clientout) { | |
273 if (coutlen == 0) { | |
274 xmlnode_insert_data(auth, "=", -1); | |
275 } else { | |
276 enc_out = gaim_base64_encode((unsigned char*)clientout, coutlen); | |
277 xmlnode_insert_data(auth, enc_out, -1); | |
278 g_free(enc_out); | |
279 } | |
280 } | |
281 jabber_send(js, auth); | |
282 xmlnode_free(auth); | |
283 } else { | |
284 gaim_connection_error(js->gc, "SASL authentication failed\n"); | |
285 } | |
286 } | |
287 | |
12543 | 288 static int |
289 jabber_sasl_cb_log(void *context, int level, const char *message) | |
290 { | |
291 if(level <= SASL_LOG_TRACE) | |
292 gaim_debug_info("sasl", "%s\n", message); | |
293 | |
294 return SASL_OK; | |
295 } | |
296 | |
12508 | 297 #endif |
298 | |
8296 | 299 void |
300 jabber_auth_start(JabberStream *js, xmlnode *packet) | |
301 { | |
12508 | 302 #ifdef HAVE_CYRUS_SASL |
303 int id; | |
304 #else | |
305 gboolean digest_md5 = FALSE, plain=FALSE; | |
306 #endif | |
8296 | 307 |
12508 | 308 xmlnode *mechs, *mechnode; |
8296 | 309 |
310 | |
8016 | 311 if(js->registration) { |
312 jabber_register_start(js); | |
313 return; | |
314 } | |
315 | |
7014 | 316 mechs = xmlnode_get_child(packet, "mechanisms"); |
317 | |
318 if(!mechs) { | |
7981 | 319 gaim_connection_error(js->gc, _("Invalid response from server.")); |
7014 | 320 return; |
321 } | |
322 | |
12508 | 323 #ifdef HAVE_CYRUS_SASL |
324 js->sasl_mechs = g_string_new(""); | |
325 #endif | |
326 | |
8135 | 327 for(mechnode = xmlnode_get_child(mechs, "mechanism"); mechnode; |
328 mechnode = xmlnode_get_next_twin(mechnode)) | |
7014 | 329 { |
8135 | 330 char *mech_name = xmlnode_get_data(mechnode); |
12508 | 331 #ifdef HAVE_CYRUS_SASL |
332 g_string_append(js->sasl_mechs, mech_name); | |
333 g_string_append_c(js->sasl_mechs,' '); | |
334 #else | |
8135 | 335 if(mech_name && !strcmp(mech_name, "DIGEST-MD5")) |
336 digest_md5 = TRUE; | |
337 else if(mech_name && !strcmp(mech_name, "PLAIN")) | |
338 plain = TRUE; | |
12508 | 339 #endif |
8135 | 340 g_free(mech_name); |
7014 | 341 } |
342 | |
12508 | 343 #ifdef HAVE_CYRUS_SASL |
344 js->auth_type = JABBER_AUTH_CYRUS; | |
345 | |
346 /* Set up our callbacks structure */ | |
12543 | 347 js->sasl_cb = g_new0(sasl_callback_t,6); |
12508 | 348 |
349 id = 0; | |
350 js->sasl_cb[id].id = SASL_CB_GETREALM; | |
351 js->sasl_cb[id].proc = jabber_sasl_cb_realm; | |
352 js->sasl_cb[id].context = (void *)js; | |
353 id++; | |
354 | |
355 js->sasl_cb[id].id = SASL_CB_AUTHNAME; | |
356 js->sasl_cb[id].proc = jabber_sasl_cb_simple; | |
357 js->sasl_cb[id].context = (void *)js; | |
358 id++; | |
359 | |
360 js->sasl_cb[id].id = SASL_CB_USER; | |
361 js->sasl_cb[id].proc = jabber_sasl_cb_simple; | |
362 js->sasl_cb[id].context = (void *)js; | |
363 id++; | |
364 | |
365 if (gaim_account_get_password(js->gc->account)) { | |
366 js->sasl_cb[id].id = SASL_CB_PASS; | |
367 js->sasl_cb[id].proc = jabber_sasl_cb_secret; | |
368 js->sasl_cb[id].context = (void *)js; | |
369 id++; | |
370 } | |
371 | |
12543 | 372 js->sasl_cb[id].id = SASL_CB_LOG; |
373 js->sasl_cb[id].proc = jabber_sasl_cb_log; | |
374 js->sasl_cb[id].context = (void*)js; | |
375 id++; | |
376 | |
12508 | 377 js->sasl_cb[id].id = SASL_CB_LIST_END; |
378 | |
379 jabber_auth_start_cyrus(js); | |
380 #else | |
7703 | 381 |
7645 | 382 if(digest_md5) { |
8397 | 383 xmlnode *auth; |
384 | |
385 js->auth_type = JABBER_AUTH_DIGEST_MD5; | |
386 auth = xmlnode_new("auth"); | |
387 xmlnode_set_attrib(auth, "xmlns", "urn:ietf:params:xml:ns:xmpp-sasl"); | |
7291 | 388 xmlnode_set_attrib(auth, "mechanism", "DIGEST-MD5"); |
8397 | 389 |
390 jabber_send(js, auth); | |
391 xmlnode_free(auth); | |
8086 | 392 } else if(plain) { |
8397 | 393 js->auth_type = JABBER_AUTH_PLAIN; |
7703 | 394 |
8086 | 395 if(js->gsc == NULL && !gaim_account_get_bool(js->gc->account, "auth_plain_in_clear", FALSE)) { |
8397 | 396 gaim_request_yes_no(js->gc, _("Plaintext Authentication"), |
397 _("Plaintext Authentication"), | |
398 _("This server requires plaintext authentication over an unencrypted connection. Allow this and continue authentication?"), | |
399 2, js->gc->account, allow_plaintext_auth, | |
400 disallow_plaintext_auth); | |
8086 | 401 return; |
402 } | |
8397 | 403 finish_plaintext_authentication(js); |
7014 | 404 } else { |
405 gaim_connection_error(js->gc, | |
406 _("Server does not use any supported authentication method")); | |
407 } | |
12508 | 408 #endif |
7014 | 409 } |
410 | |
7395 | 411 static void auth_old_result_cb(JabberStream *js, xmlnode *packet, gpointer data) |
7014 | 412 { |
413 const char *type = xmlnode_get_attrib(packet, "type"); | |
414 | |
7730 | 415 if(type && !strcmp(type, "result")) { |
416 jabber_stream_set_state(js, JABBER_STREAM_CONNECTED); | |
417 } else { | |
8401 | 418 char *msg = jabber_parse_error(js, packet); |
419 xmlnode *error; | |
420 const char *err_code; | |
7014 | 421 |
8401 | 422 if((error = xmlnode_get_child(packet, "error")) && |
423 (err_code = xmlnode_get_attrib(error, "code")) && | |
424 !strcmp(err_code, "401")) { | |
425 js->gc->wants_to_die = TRUE; | |
7730 | 426 } |
7014 | 427 |
8401 | 428 gaim_connection_error(js->gc, msg); |
429 g_free(msg); | |
7014 | 430 } |
431 } | |
432 | |
7395 | 433 static void auth_old_cb(JabberStream *js, xmlnode *packet, gpointer data) |
7014 | 434 { |
435 JabberIq *iq; | |
436 xmlnode *query, *x; | |
7514 | 437 const char *type = xmlnode_get_attrib(packet, "type"); |
10740 | 438 const char *pw = gaim_connection_get_password(js->gc); |
7014 | 439 |
7514 | 440 if(!type) { |
7981 | 441 gaim_connection_error(js->gc, _("Invalid response from server.")); |
7014 | 442 return; |
7515 | 443 } else if(!strcmp(type, "error")) { |
8401 | 444 char *msg = jabber_parse_error(js, packet); |
445 gaim_connection_error(js->gc, msg); | |
446 g_free(msg); | |
7515 | 447 } else if(!strcmp(type, "result")) { |
7514 | 448 query = xmlnode_get_child(packet, "query"); |
449 if(js->stream_id && xmlnode_get_child(query, "digest")) { | |
450 unsigned char hashval[20]; | |
451 char *s, h[41], *p; | |
452 int i; | |
7014 | 453 |
8397 | 454 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:auth"); |
455 query = xmlnode_get_child(iq->node, "query"); | |
456 x = xmlnode_new_child(query, "username"); | |
457 xmlnode_insert_data(x, js->user->node, -1); | |
458 x = xmlnode_new_child(query, "resource"); | |
459 xmlnode_insert_data(x, js->user->resource, -1); | |
460 | |
7514 | 461 x = xmlnode_new_child(query, "digest"); |
462 s = g_strdup_printf("%s%s", js->stream_id, pw); | |
10684
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
463 |
11183 | 464 gaim_cipher_digest_region("sha1", (guchar *)s, strlen(s), |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
465 sizeof(hashval), hashval, NULL); |
10684
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
466 |
7514 | 467 p = h; |
468 for(i=0; i<20; i++, p+=2) | |
469 snprintf(p, 3, "%02x", hashval[i]); | |
470 xmlnode_insert_data(x, h, -1); | |
471 g_free(s); | |
8397 | 472 jabber_iq_set_callback(iq, auth_old_result_cb, NULL); |
473 jabber_iq_send(iq); | |
474 | |
475 } else if(xmlnode_get_child(query, "password")) { | |
476 if(js->gsc == NULL && !gaim_account_get_bool(js->gc->account, | |
477 "auth_plain_in_clear", FALSE)) { | |
478 gaim_request_yes_no(js->gc, _("Plaintext Authentication"), | |
479 _("Plaintext Authentication"), | |
480 _("This server requires plaintext authentication over an unencrypted connection. Allow this and continue authentication?"), | |
481 2, js->gc->account, allow_plaintext_auth, | |
482 disallow_plaintext_auth); | |
483 return; | |
484 } | |
485 finish_plaintext_authentication(js); | |
7514 | 486 } else { |
8397 | 487 gaim_connection_error(js->gc, |
488 _("Server does not use any supported authentication method")); | |
489 return; | |
7514 | 490 } |
7014 | 491 } |
492 } | |
493 | |
494 void jabber_auth_start_old(JabberStream *js) | |
495 { | |
496 JabberIq *iq; | |
497 xmlnode *query, *username; | |
498 | |
499 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:auth"); | |
500 | |
501 query = xmlnode_get_child(iq->node, "query"); | |
502 username = xmlnode_new_child(query, "username"); | |
503 xmlnode_insert_data(username, js->user->node, -1); | |
504 | |
7395 | 505 jabber_iq_set_callback(iq, auth_old_cb, NULL); |
7014 | 506 |
507 jabber_iq_send(iq); | |
508 } | |
509 | |
510 static GHashTable* parse_challenge(const char *challenge) | |
511 { | |
512 GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, | |
513 g_free, g_free); | |
514 char **pairs; | |
515 int i; | |
516 | |
517 pairs = g_strsplit(challenge, ",", -1); | |
518 | |
519 for(i=0; pairs[i]; i++) { | |
520 char **keyval = g_strsplit(pairs[i], "=", 2); | |
521 if(keyval[0] && keyval[1]) { | |
522 if(keyval[1][0] == '"' && keyval[1][strlen(keyval[1])-1] == '"') | |
523 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strndup(keyval[1]+1, strlen(keyval[1])-2)); | |
524 else | |
525 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strdup(keyval[1])); | |
526 } | |
527 g_strfreev(keyval); | |
528 } | |
529 | |
530 g_strfreev(pairs); | |
531 | |
532 return ret; | |
533 } | |
534 | |
11163 | 535 static char * |
7014 | 536 generate_response_value(JabberID *jid, const char *passwd, const char *nonce, |
7267 | 537 const char *cnonce, const char *a2, const char *realm) |
7014 | 538 { |
10684
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
539 GaimCipher *cipher; |
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
540 GaimCipherContext *context; |
11137 | 541 guchar result[16]; |
10136 | 542 size_t a1len; |
7014 | 543 |
11163 | 544 gchar *a1, *convnode, *convpasswd, *ha1, *ha2, *kd, *x, *z; |
7014 | 545 |
10136 | 546 if((convnode = g_convert(jid->node, strlen(jid->node), "iso-8859-1", "utf-8", |
547 NULL, NULL, NULL)) == NULL) { | |
548 convnode = g_strdup(jid->node); | |
549 } | |
550 if((convpasswd = g_convert(passwd, strlen(passwd), "iso-8859-1", "utf-8", | |
551 NULL, NULL, NULL)) == NULL) { | |
552 convpasswd = g_strdup(passwd); | |
553 } | |
554 | |
10684
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
555 cipher = gaim_ciphers_find_cipher("md5"); |
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
556 context = gaim_cipher_context_new(cipher, NULL); |
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
557 |
10136 | 558 x = g_strdup_printf("%s:%s:%s", convnode, realm, convpasswd); |
11183 | 559 gaim_cipher_context_append(context, (const guchar *)x, strlen(x)); |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
560 gaim_cipher_context_digest(context, sizeof(result), result, NULL); |
7014 | 561 |
10136 | 562 a1 = g_strdup_printf("xxxxxxxxxxxxxxxx:%s:%s", nonce, cnonce); |
563 a1len = strlen(a1); | |
564 g_memmove(a1, result, 16); | |
7014 | 565 |
10684
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
566 gaim_cipher_context_reset(context, NULL); |
11183 | 567 gaim_cipher_context_append(context, (const guchar *)a1, a1len); |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
568 gaim_cipher_context_digest(context, sizeof(result), result, NULL); |
7014 | 569 |
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
570 ha1 = gaim_base16_encode(result, 16); |
7014 | 571 |
10684
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
572 gaim_cipher_context_reset(context, NULL); |
11183 | 573 gaim_cipher_context_append(context, (const guchar *)a2, strlen(a2)); |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
574 gaim_cipher_context_digest(context, sizeof(result), result, NULL); |
7014 | 575 |
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
576 ha2 = gaim_base16_encode(result, 16); |
7014 | 577 |
578 kd = g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1, nonce, cnonce, ha2); | |
579 | |
10684
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
580 gaim_cipher_context_reset(context, NULL); |
11183 | 581 gaim_cipher_context_append(context, (const guchar *)kd, strlen(kd)); |
10687
b256ce6b85b8
[gaim-migrate @ 12235]
Etan Reisner <pidgin@unreliablesource.net>
parents:
10684
diff
changeset
|
582 gaim_cipher_context_digest(context, sizeof(result), result, NULL); |
10684
72a5babfa8b4
[gaim-migrate @ 12231]
Luke Schierer <lschiere@pidgin.im>
parents:
10496
diff
changeset
|
583 gaim_cipher_context_destroy(context); |
7014 | 584 |
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
585 z = gaim_base16_encode(result, 16); |
7014 | 586 |
10136 | 587 g_free(convnode); |
588 g_free(convpasswd); | |
7014 | 589 g_free(x); |
590 g_free(a1); | |
591 g_free(ha1); | |
592 g_free(ha2); | |
593 g_free(kd); | |
594 | |
595 return z; | |
596 } | |
597 | |
598 void | |
599 jabber_auth_handle_challenge(JabberStream *js, xmlnode *packet) | |
600 { | |
601 | |
7703 | 602 if(js->auth_type == JABBER_AUTH_DIGEST_MD5) { |
7291 | 603 char *enc_in = xmlnode_get_data(packet); |
604 char *dec_in; | |
605 char *enc_out; | |
606 GHashTable *parts; | |
7014 | 607 |
7395 | 608 if(!enc_in) { |
7981 | 609 gaim_connection_error(js->gc, _("Invalid response from server.")); |
7395 | 610 return; |
611 } | |
612 | |
11127 | 613 dec_in = (char *)gaim_base64_decode(enc_in, NULL); |
7395 | 614 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded challenge (%d): %s\n", |
615 strlen(dec_in), dec_in); | |
7291 | 616 |
617 parts = parse_challenge(dec_in); | |
7014 | 618 |
619 | |
7291 | 620 if (g_hash_table_lookup(parts, "rspauth")) { |
621 char *rspauth = g_hash_table_lookup(parts, "rspauth"); | |
7014 | 622 |
623 | |
7291 | 624 if(rspauth && js->expected_rspauth && |
625 !strcmp(rspauth, js->expected_rspauth)) { | |
626 jabber_send_raw(js, | |
7642 | 627 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", |
628 -1); | |
7291 | 629 } else { |
630 gaim_connection_error(js->gc, _("Invalid challenge from server")); | |
631 } | |
632 g_free(js->expected_rspauth); | |
633 } else { | |
634 /* assemble a response, and send it */ | |
635 /* see RFC 2831 */ | |
636 GString *response = g_string_new(""); | |
637 char *a2; | |
638 char *auth_resp; | |
639 char *buf; | |
640 char *cnonce; | |
641 char *realm; | |
642 char *nonce; | |
7014 | 643 |
7291 | 644 /* we're actually supposed to prompt the user for a realm if |
645 * the server doesn't send one, but that really complicates things, | |
646 * so i'm not gonna worry about it until is poses a problem to | |
647 * someone, or I get really bored */ | |
648 realm = g_hash_table_lookup(parts, "realm"); | |
649 if(!realm) | |
650 realm = js->user->domain; | |
7014 | 651 |
7291 | 652 cnonce = g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL), |
653 g_random_int()); | |
654 nonce = g_hash_table_lookup(parts, "nonce"); | |
7014 | 655 |
656 | |
7291 | 657 a2 = g_strdup_printf("AUTHENTICATE:xmpp/%s", realm); |
658 auth_resp = generate_response_value(js->user, | |
10740 | 659 gaim_connection_get_password(js->gc), nonce, cnonce, a2, realm); |
7291 | 660 g_free(a2); |
661 | |
662 a2 = g_strdup_printf(":xmpp/%s", realm); | |
663 js->expected_rspauth = generate_response_value(js->user, | |
10740 | 664 gaim_connection_get_password(js->gc), nonce, cnonce, a2, realm); |
7291 | 665 g_free(a2); |
666 | |
667 | |
668 g_string_append_printf(response, "username=\"%s\"", js->user->node); | |
669 g_string_append_printf(response, ",realm=\"%s\"", realm); | |
670 g_string_append_printf(response, ",nonce=\"%s\"", nonce); | |
671 g_string_append_printf(response, ",cnonce=\"%s\"", cnonce); | |
672 g_string_append_printf(response, ",nc=00000001"); | |
673 g_string_append_printf(response, ",qop=auth"); | |
674 g_string_append_printf(response, ",digest-uri=\"xmpp/%s\"", realm); | |
675 g_string_append_printf(response, ",response=%s", auth_resp); | |
676 g_string_append_printf(response, ",charset=utf-8"); | |
677 | |
678 g_free(auth_resp); | |
679 g_free(cnonce); | |
680 | |
11137 | 681 enc_out = gaim_base64_encode((guchar *)response->str, response->len); |
7291 | 682 |
683 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded response (%d): %s\n", response->len, response->str); | |
684 | |
685 buf = g_strdup_printf("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>", enc_out); | |
686 | |
7642 | 687 jabber_send_raw(js, buf, -1); |
7291 | 688 |
689 g_free(buf); | |
690 | |
691 g_free(enc_out); | |
692 | |
693 g_string_free(response, TRUE); | |
7014 | 694 } |
7291 | 695 |
696 g_free(enc_in); | |
697 g_free(dec_in); | |
698 g_hash_table_destroy(parts); | |
7014 | 699 } |
12508 | 700 #ifdef HAVE_CYRUS_SASL |
701 else if (js->auth_type == JABBER_AUTH_CYRUS) { | |
702 char *enc_in = xmlnode_get_data(packet); | |
703 unsigned char *dec_in; | |
704 char *enc_out; | |
705 const char *c_out; | |
12543 | 706 unsigned int clen; |
707 gsize declen; | |
12508 | 708 xmlnode *response; |
709 | |
710 dec_in = gaim_base64_decode(enc_in, &declen); | |
711 | |
712 js->sasl_state = sasl_client_step(js->sasl, (char*)dec_in, declen, | |
713 NULL, &c_out, &clen); | |
714 g_free(dec_in); | |
715 if (js->sasl_state != SASL_CONTINUE && js->sasl_state != SASL_OK) { | |
716 gaim_debug_error("jabber", "Error is %d : %s\n",js->sasl_state,sasl_errdetail(js->sasl)); | |
717 gaim_connection_error(js->gc, _("SASL error")); | |
718 return; | |
719 } else { | |
720 response = xmlnode_new("response"); | |
721 xmlnode_set_attrib(response, "xmlns", "urn:ietf:params:xml:ns:xmpp-sasl"); | |
722 if (c_out) { | |
723 enc_out = gaim_base64_encode((unsigned char*)c_out, clen); | |
724 xmlnode_insert_data(response, enc_out, -1); | |
725 g_free(enc_out); | |
726 } | |
727 jabber_send(js, response); | |
728 xmlnode_free(response); | |
729 } | |
730 } | |
731 #endif | |
7014 | 732 } |
733 | |
734 void jabber_auth_handle_success(JabberStream *js, xmlnode *packet) | |
735 { | |
736 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
12508 | 737 #ifdef HAVE_CYRUS_SASL |
738 int *x; | |
739 #endif | |
7014 | 740 |
741 if(!ns || strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
7981 | 742 gaim_connection_error(js->gc, _("Invalid response from server.")); |
7014 | 743 return; |
744 } | |
745 | |
12520
d85c2bfb2ea2
[gaim-migrate @ 14832]
Richard Laager <rlaager@wiktel.com>
parents:
12508
diff
changeset
|
746 #ifdef HAVE_CYRUS_SASL |
12508 | 747 /* The SASL docs say that if the client hasn't returned OK yet, we |
748 * should try one more round against it | |
749 */ | |
750 if (js->sasl_state != SASL_OK) { | |
751 js->sasl_state = sasl_client_step(js->sasl, NULL, 0, NULL, NULL, NULL); | |
752 if (js->sasl_state != SASL_OK) { | |
753 /* This should never happen! */ | |
754 gaim_connection_error(js->gc, _("Invalid response from server.")); | |
755 } | |
756 } | |
757 /* If we've negotiated a security layer, we need to enable it */ | |
758 sasl_getprop(js->sasl, SASL_SSF, (const void **)&x); | |
759 if (*x>0) { | |
760 sasl_getprop(js->sasl, SASL_MAXOUTBUF, (const void **)&x); | |
761 js->sasl_maxbuf = *x; | |
762 } | |
763 #endif | |
764 | |
7014 | 765 jabber_stream_set_state(js, JABBER_STREAM_REINITIALIZING); |
766 } | |
767 | |
768 void jabber_auth_handle_failure(JabberStream *js, xmlnode *packet) | |
769 { | |
8401 | 770 char *msg = jabber_parse_error(js, packet); |
7014 | 771 |
8401 | 772 if(!msg) { |
7981 | 773 gaim_connection_error(js->gc, _("Invalid response from server.")); |
8401 | 774 } else { |
775 gaim_connection_error(js->gc, msg); | |
776 g_free(msg); | |
7014 | 777 } |
778 } |