Mercurial > pidgin.yaz
annotate src/protocols/jabber/auth.c @ 7310:dd4b4a187171
[gaim-migrate @ 7894]
assorted jabber tweaks
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Tue, 21 Oct 2003 17:18:46 +0000 |
parents | 632cee95cc5c |
children | b250288fa948 |
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 | |
94 static void auth_old_result_cb(JabberStream *js, xmlnode *packet) | |
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 | |
125 static void auth_old_cb(JabberStream *js, xmlnode *packet) | |
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 | |
166 jabber_iq_set_callback(iq, auth_old_result_cb); | |
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 | |
182 jabber_iq_set_callback(iq, auth_old_cb); | |
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 | |
228 a1 = g_strdup_printf("%s:%s:%s:%s@%s/%s", y, nonce, cnonce, jid->node, | |
229 jid->domain, jid->resource); | |
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 |
7291 | 273 gaim_base64_decode(enc_in, &dec_in, NULL); |
274 | |
275 parts = parse_challenge(dec_in); | |
7014 | 276 |
277 | |
7291 | 278 if (g_hash_table_lookup(parts, "rspauth")) { |
279 char *rspauth = g_hash_table_lookup(parts, "rspauth"); | |
7014 | 280 |
281 | |
7291 | 282 if(rspauth && js->expected_rspauth && |
283 !strcmp(rspauth, js->expected_rspauth)) { | |
284 jabber_send_raw(js, | |
285 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />"); | |
286 } else { | |
287 gaim_connection_error(js->gc, _("Invalid challenge from server")); | |
288 } | |
289 g_free(js->expected_rspauth); | |
290 } else { | |
291 /* assemble a response, and send it */ | |
292 /* see RFC 2831 */ | |
293 GString *response = g_string_new(""); | |
294 char *a2; | |
295 char *auth_resp; | |
296 char *buf; | |
297 char *cnonce; | |
298 char *realm; | |
299 char *nonce; | |
7014 | 300 |
7291 | 301 /* we're actually supposed to prompt the user for a realm if |
302 * the server doesn't send one, but that really complicates things, | |
303 * so i'm not gonna worry about it until is poses a problem to | |
304 * someone, or I get really bored */ | |
305 realm = g_hash_table_lookup(parts, "realm"); | |
306 if(!realm) | |
307 realm = js->user->domain; | |
7014 | 308 |
7291 | 309 cnonce = g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL), |
310 g_random_int()); | |
311 nonce = g_hash_table_lookup(parts, "nonce"); | |
7014 | 312 |
313 | |
7291 | 314 a2 = g_strdup_printf("AUTHENTICATE:xmpp/%s", realm); |
315 auth_resp = generate_response_value(js->user, | |
316 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
317 g_free(a2); | |
318 | |
319 a2 = g_strdup_printf(":xmpp/%s", realm); | |
320 js->expected_rspauth = generate_response_value(js->user, | |
321 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
322 g_free(a2); | |
323 | |
324 | |
325 g_string_append_printf(response, "username=\"%s\"", js->user->node); | |
326 g_string_append_printf(response, ",realm=\"%s\"", realm); | |
327 g_string_append_printf(response, ",nonce=\"%s\"", nonce); | |
328 g_string_append_printf(response, ",cnonce=\"%s\"", cnonce); | |
329 g_string_append_printf(response, ",nc=00000001"); | |
330 g_string_append_printf(response, ",qop=auth"); | |
331 g_string_append_printf(response, ",digest-uri=\"xmpp/%s\"", realm); | |
332 g_string_append_printf(response, ",response=%s", auth_resp); | |
333 g_string_append_printf(response, ",charset=utf-8"); | |
334 g_string_append_printf(response, ",authzid=\"%s@%s/%s\"", | |
335 js->user->node, js->user->domain, js->user->resource); | |
336 | |
337 g_free(auth_resp); | |
338 g_free(cnonce); | |
339 | |
340 enc_out = gaim_base64_encode(response->str, response->len); | |
341 | |
342 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded response (%d): %s\n", response->len, response->str); | |
343 | |
344 buf = g_strdup_printf("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>", enc_out); | |
345 | |
346 jabber_send_raw(js, buf); | |
347 | |
348 g_free(buf); | |
349 | |
350 g_free(enc_out); | |
351 | |
352 g_string_free(response, TRUE); | |
7014 | 353 } |
7291 | 354 |
355 g_free(enc_in); | |
356 g_free(dec_in); | |
357 g_hash_table_destroy(parts); | |
7014 | 358 } |
359 } | |
360 | |
361 void jabber_auth_handle_success(JabberStream *js, xmlnode *packet) | |
362 { | |
363 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
364 | |
365 if(!ns || strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
366 gaim_connection_error(js->gc, _("Invalid response from server")); | |
367 return; | |
368 } | |
369 | |
370 jabber_stream_set_state(js, JABBER_STREAM_REINITIALIZING); | |
371 } | |
372 | |
373 void jabber_auth_handle_failure(JabberStream *js, xmlnode *packet) | |
374 { | |
375 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
376 | |
377 if(!ns) | |
378 gaim_connection_error(js->gc, _("Invalid response from server")); | |
379 else if(!strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
380 if(xmlnode_get_child(packet, "bad-protocol")) { | |
381 gaim_connection_error(js->gc, _("Bad Protocol")); | |
382 } else if(xmlnode_get_child(packet, "encryption-required")) { | |
383 js->gc->wants_to_die = TRUE; | |
384 gaim_connection_error(js->gc, _("Encryption Required")); | |
385 } else if(xmlnode_get_child(packet, "invalid-authzid")) { | |
386 js->gc->wants_to_die = TRUE; | |
387 gaim_connection_error(js->gc, _("Invalid authzid")); | |
388 } else if(xmlnode_get_child(packet, "invalid-mechanism")) { | |
389 js->gc->wants_to_die = TRUE; | |
390 gaim_connection_error(js->gc, _("Invalid Mechanism")); | |
391 } else if(xmlnode_get_child(packet, "invalid-realm")) { | |
392 gaim_connection_error(js->gc, _("Invalid Realm")); | |
393 } else if(xmlnode_get_child(packet, "mechanism-too-weak")) { | |
394 js->gc->wants_to_die = TRUE; | |
395 gaim_connection_error(js->gc, _("Mechanism Too Weak")); | |
396 } else if(xmlnode_get_child(packet, "not-authorized")) { | |
397 js->gc->wants_to_die = TRUE; | |
398 gaim_connection_error(js->gc, _("Not Authorized")); | |
399 } else if(xmlnode_get_child(packet, "temporary-auth-failure")) { | |
400 gaim_connection_error(js->gc, | |
401 _("Temporary Authentication Failure")); | |
402 } else { | |
403 gaim_connection_error(js->gc, _("Authentication Failure")); | |
404 } | |
405 } | |
406 } |