Mercurial > pidgin.yaz
annotate src/protocols/jabber/auth.c @ 7395:b250288fa948
[gaim-migrate @ 7990]
this would be the non-working start of file transfer (the real way) for jabber
also approximately eleventy billion jabber tweaks
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Fri, 31 Oct 2003 02:43:58 +0000 |
parents | 632cee95cc5c |
children | 85cd2e71bff7 |
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 | |
132 query = xmlnode_get_child(packet, "query"); | |
133 if(js->stream_id && xmlnode_get_child(query, "digest")) { | |
134 digest = TRUE; | |
135 } else if(!xmlnode_get_child(query, "password")) { | |
136 gaim_connection_error(js->gc, | |
137 _("Server does not use any supported authentication method")); | |
138 return; | |
139 } | |
140 | |
141 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:auth"); | |
142 query = xmlnode_get_child(iq->node, "query"); | |
143 x = xmlnode_new_child(query, "username"); | |
144 xmlnode_insert_data(x, js->user->node, -1); | |
145 x = xmlnode_new_child(query, "resource"); | |
146 xmlnode_insert_data(x, js->user->resource, -1); | |
147 | |
148 if(digest) { | |
149 unsigned char hashval[20]; | |
150 char *s, h[41], *p; | |
151 int i; | |
152 | |
153 x = xmlnode_new_child(query, "digest"); | |
154 s = g_strdup_printf("%s%s", js->stream_id, pw); | |
155 shaBlock((unsigned char *)s, strlen(s), hashval); | |
156 p = h; | |
157 for(i=0; i<20; i++, p+=2) | |
158 snprintf(p, 3, "%02x", hashval[i]); | |
159 xmlnode_insert_data(x, h, -1); | |
160 g_free(s); | |
161 } else { | |
162 x = xmlnode_new_child(query, "password"); | |
163 xmlnode_insert_data(x, pw, -1); | |
164 } | |
165 | |
7395 | 166 jabber_iq_set_callback(iq, auth_old_result_cb, NULL); |
7014 | 167 |
168 jabber_iq_send(iq); | |
169 } | |
170 | |
171 void jabber_auth_start_old(JabberStream *js) | |
172 { | |
173 JabberIq *iq; | |
174 xmlnode *query, *username; | |
175 | |
176 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:auth"); | |
177 | |
178 query = xmlnode_get_child(iq->node, "query"); | |
179 username = xmlnode_new_child(query, "username"); | |
180 xmlnode_insert_data(username, js->user->node, -1); | |
181 | |
7395 | 182 jabber_iq_set_callback(iq, auth_old_cb, NULL); |
7014 | 183 |
184 jabber_iq_send(iq); | |
185 } | |
186 | |
187 static GHashTable* parse_challenge(const char *challenge) | |
188 { | |
189 GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, | |
190 g_free, g_free); | |
191 char **pairs; | |
192 int i; | |
193 | |
194 pairs = g_strsplit(challenge, ",", -1); | |
195 | |
196 for(i=0; pairs[i]; i++) { | |
197 char **keyval = g_strsplit(pairs[i], "=", 2); | |
198 if(keyval[0] && keyval[1]) { | |
199 if(keyval[1][0] == '"' && keyval[1][strlen(keyval[1])-1] == '"') | |
200 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strndup(keyval[1]+1, strlen(keyval[1])-2)); | |
201 else | |
202 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strdup(keyval[1])); | |
203 } | |
204 g_strfreev(keyval); | |
205 } | |
206 | |
207 g_strfreev(pairs); | |
208 | |
209 return ret; | |
210 } | |
211 | |
212 static unsigned char* | |
213 generate_response_value(JabberID *jid, const char *passwd, const char *nonce, | |
7267 | 214 const char *cnonce, const char *a2, const char *realm) |
7014 | 215 { |
216 md5_state_t ctx; | |
217 md5_byte_t result[16]; | |
218 | |
219 char *x, *y, *a1, *ha1, *ha2, *kd, *z; | |
220 | |
7267 | 221 x = g_strdup_printf("%s:%s:%s", jid->node, realm, passwd); |
7014 | 222 md5_init(&ctx); |
223 md5_append(&ctx, x, strlen(x)); | |
224 md5_finish(&ctx, result); | |
225 | |
226 y = g_strndup(result, 16); | |
227 | |
7395 | 228 a1 = g_strdup_printf("%s:%s:%s:%s@%s", y, nonce, cnonce, jid->node, |
229 jid->domain); | |
7014 | 230 |
231 md5_init(&ctx); | |
232 md5_append(&ctx, a1, strlen(a1)); | |
233 md5_finish(&ctx, result); | |
234 | |
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
235 ha1 = gaim_base16_encode(result, 16); |
7014 | 236 |
237 md5_init(&ctx); | |
238 md5_append(&ctx, a2, strlen(a2)); | |
239 md5_finish(&ctx, result); | |
240 | |
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
241 ha2 = gaim_base16_encode(result, 16); |
7014 | 242 |
243 kd = g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1, nonce, cnonce, ha2); | |
244 | |
245 md5_init(&ctx); | |
246 md5_append(&ctx, kd, strlen(kd)); | |
247 md5_finish(&ctx, result); | |
248 | |
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
249 z = gaim_base16_encode(result, 16); |
7014 | 250 |
251 g_free(x); | |
252 g_free(y); | |
253 g_free(a1); | |
254 g_free(ha1); | |
255 g_free(ha2); | |
256 g_free(kd); | |
257 | |
258 return z; | |
259 } | |
260 | |
261 void | |
262 jabber_auth_handle_challenge(JabberStream *js, xmlnode *packet) | |
263 { | |
264 | |
7291 | 265 if(js->auth_type == JABBER_AUTH_PLAIN) { |
266 /* XXX: implement me! */ | |
267 } else if(js->auth_type == JABBER_AUTH_DIGEST_MD5) { | |
268 char *enc_in = xmlnode_get_data(packet); | |
269 char *dec_in; | |
270 char *enc_out; | |
271 GHashTable *parts; | |
7014 | 272 |
7395 | 273 if(!enc_in) { |
274 gaim_connection_error(js->gc, _("Invalid response from server")); | |
275 return; | |
276 } | |
277 | |
7291 | 278 gaim_base64_decode(enc_in, &dec_in, NULL); |
7395 | 279 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded challenge (%d): %s\n", |
280 strlen(dec_in), dec_in); | |
7291 | 281 |
282 parts = parse_challenge(dec_in); | |
7014 | 283 |
284 | |
7291 | 285 if (g_hash_table_lookup(parts, "rspauth")) { |
286 char *rspauth = g_hash_table_lookup(parts, "rspauth"); | |
7014 | 287 |
288 | |
7291 | 289 if(rspauth && js->expected_rspauth && |
290 !strcmp(rspauth, js->expected_rspauth)) { | |
291 jabber_send_raw(js, | |
292 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />"); | |
293 } else { | |
294 gaim_connection_error(js->gc, _("Invalid challenge from server")); | |
295 } | |
296 g_free(js->expected_rspauth); | |
297 } else { | |
298 /* assemble a response, and send it */ | |
299 /* see RFC 2831 */ | |
300 GString *response = g_string_new(""); | |
301 char *a2; | |
302 char *auth_resp; | |
303 char *buf; | |
304 char *cnonce; | |
305 char *realm; | |
306 char *nonce; | |
7014 | 307 |
7291 | 308 /* we're actually supposed to prompt the user for a realm if |
309 * the server doesn't send one, but that really complicates things, | |
310 * so i'm not gonna worry about it until is poses a problem to | |
311 * someone, or I get really bored */ | |
312 realm = g_hash_table_lookup(parts, "realm"); | |
313 if(!realm) | |
314 realm = js->user->domain; | |
7014 | 315 |
7291 | 316 cnonce = g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL), |
317 g_random_int()); | |
318 nonce = g_hash_table_lookup(parts, "nonce"); | |
7014 | 319 |
320 | |
7291 | 321 a2 = g_strdup_printf("AUTHENTICATE:xmpp/%s", realm); |
322 auth_resp = generate_response_value(js->user, | |
323 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
324 g_free(a2); | |
325 | |
326 a2 = g_strdup_printf(":xmpp/%s", realm); | |
327 js->expected_rspauth = generate_response_value(js->user, | |
328 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
329 g_free(a2); | |
330 | |
331 | |
332 g_string_append_printf(response, "username=\"%s\"", js->user->node); | |
333 g_string_append_printf(response, ",realm=\"%s\"", realm); | |
334 g_string_append_printf(response, ",nonce=\"%s\"", nonce); | |
335 g_string_append_printf(response, ",cnonce=\"%s\"", cnonce); | |
336 g_string_append_printf(response, ",nc=00000001"); | |
337 g_string_append_printf(response, ",qop=auth"); | |
338 g_string_append_printf(response, ",digest-uri=\"xmpp/%s\"", realm); | |
339 g_string_append_printf(response, ",response=%s", auth_resp); | |
340 g_string_append_printf(response, ",charset=utf-8"); | |
7395 | 341 g_string_append_printf(response, ",authzid=\"%s@%s\"", |
342 js->user->node, js->user->domain); | |
7291 | 343 |
344 g_free(auth_resp); | |
345 g_free(cnonce); | |
346 | |
347 enc_out = gaim_base64_encode(response->str, response->len); | |
348 | |
349 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded response (%d): %s\n", response->len, response->str); | |
350 | |
351 buf = g_strdup_printf("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>", enc_out); | |
352 | |
353 jabber_send_raw(js, buf); | |
354 | |
355 g_free(buf); | |
356 | |
357 g_free(enc_out); | |
358 | |
359 g_string_free(response, TRUE); | |
7014 | 360 } |
7291 | 361 |
362 g_free(enc_in); | |
363 g_free(dec_in); | |
364 g_hash_table_destroy(parts); | |
7014 | 365 } |
366 } | |
367 | |
368 void jabber_auth_handle_success(JabberStream *js, xmlnode *packet) | |
369 { | |
370 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
371 | |
372 if(!ns || strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
373 gaim_connection_error(js->gc, _("Invalid response from server")); | |
374 return; | |
375 } | |
376 | |
377 jabber_stream_set_state(js, JABBER_STREAM_REINITIALIZING); | |
378 } | |
379 | |
380 void jabber_auth_handle_failure(JabberStream *js, xmlnode *packet) | |
381 { | |
382 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
383 | |
384 if(!ns) | |
385 gaim_connection_error(js->gc, _("Invalid response from server")); | |
386 else if(!strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
387 if(xmlnode_get_child(packet, "bad-protocol")) { | |
388 gaim_connection_error(js->gc, _("Bad Protocol")); | |
389 } else if(xmlnode_get_child(packet, "encryption-required")) { | |
390 js->gc->wants_to_die = TRUE; | |
391 gaim_connection_error(js->gc, _("Encryption Required")); | |
392 } else if(xmlnode_get_child(packet, "invalid-authzid")) { | |
393 js->gc->wants_to_die = TRUE; | |
394 gaim_connection_error(js->gc, _("Invalid authzid")); | |
395 } else if(xmlnode_get_child(packet, "invalid-mechanism")) { | |
396 js->gc->wants_to_die = TRUE; | |
397 gaim_connection_error(js->gc, _("Invalid Mechanism")); | |
398 } else if(xmlnode_get_child(packet, "invalid-realm")) { | |
399 gaim_connection_error(js->gc, _("Invalid Realm")); | |
400 } else if(xmlnode_get_child(packet, "mechanism-too-weak")) { | |
401 js->gc->wants_to_die = TRUE; | |
402 gaim_connection_error(js->gc, _("Mechanism Too Weak")); | |
403 } else if(xmlnode_get_child(packet, "not-authorized")) { | |
404 js->gc->wants_to_die = TRUE; | |
405 gaim_connection_error(js->gc, _("Not Authorized")); | |
406 } else if(xmlnode_get_child(packet, "temporary-auth-failure")) { | |
407 gaim_connection_error(js->gc, | |
408 _("Temporary Authentication Failure")); | |
409 } else { | |
410 gaim_connection_error(js->gc, _("Authentication Failure")); | |
411 } | |
412 } | |
413 } |