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