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