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