14192
|
1 /**
|
|
2 * @file nexus.c MSN Nexus functions
|
|
3 *
|
|
4 * gaim
|
|
5 *
|
|
6 * Gaim is the legal property of its developers, whose names are too numerous
|
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
8 * source distribution.
|
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 */
|
|
24 #include "msn.h"
|
|
25 #include "nexus.h"
|
|
26 #include "notification.h"
|
|
27
|
|
28 /**************************************************************************
|
|
29 * Main
|
|
30 **************************************************************************/
|
|
31
|
|
32 MsnNexus *
|
|
33 msn_nexus_new(MsnSession *session)
|
|
34 {
|
|
35 MsnNexus *nexus;
|
|
36
|
|
37 nexus = g_new0(MsnNexus, 1);
|
|
38 nexus->session = session;
|
|
39 nexus->challenge_data = g_hash_table_new_full(g_str_hash,
|
|
40 g_str_equal, g_free, g_free);
|
|
41
|
|
42 return nexus;
|
|
43 }
|
|
44
|
|
45 void
|
|
46 msn_nexus_destroy(MsnNexus *nexus)
|
|
47 {
|
14249
|
48 if (nexus->gsc)
|
|
49 gaim_ssl_close(nexus->gsc);
|
|
50
|
14192
|
51 g_free(nexus->login_host);
|
|
52
|
|
53 g_free(nexus->login_path);
|
|
54
|
|
55 if (nexus->challenge_data != NULL)
|
|
56 g_hash_table_destroy(nexus->challenge_data);
|
|
57
|
|
58 if (nexus->input_handler > 0)
|
|
59 gaim_input_remove(nexus->input_handler);
|
|
60 g_free(nexus->write_buf);
|
|
61 g_free(nexus->read_buf);
|
|
62
|
|
63 g_free(nexus);
|
|
64 }
|
|
65
|
|
66 /**************************************************************************
|
|
67 * Util
|
|
68 **************************************************************************/
|
|
69
|
|
70 static gssize
|
|
71 msn_ssl_read(MsnNexus *nexus)
|
|
72 {
|
|
73 gssize len;
|
|
74 char temp_buf[4096];
|
|
75
|
|
76 if ((len = gaim_ssl_read(nexus->gsc, temp_buf,
|
|
77 sizeof(temp_buf))) > 0)
|
|
78 {
|
|
79 nexus->read_buf = g_realloc(nexus->read_buf,
|
|
80 nexus->read_len + len + 1);
|
|
81 strncpy(nexus->read_buf + nexus->read_len, temp_buf, len);
|
|
82 nexus->read_len += len;
|
|
83 nexus->read_buf[nexus->read_len] = '\0';
|
|
84 }
|
|
85
|
|
86 return len;
|
|
87 }
|
|
88
|
|
89 static void
|
|
90 nexus_write_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
91 {
|
|
92 MsnNexus *nexus = data;
|
|
93 int len, total_len;
|
|
94
|
|
95 total_len = strlen(nexus->write_buf);
|
|
96
|
|
97 len = gaim_ssl_write(nexus->gsc,
|
|
98 nexus->write_buf + nexus->written_len,
|
|
99 total_len - nexus->written_len);
|
|
100
|
|
101 if (len < 0 && errno == EAGAIN)
|
|
102 return;
|
|
103 else if (len <= 0) {
|
|
104 gaim_input_remove(nexus->input_handler);
|
14249
|
105 nexus->input_handler = 0;
|
14192
|
106 /* TODO: notify of the error */
|
|
107 return;
|
|
108 }
|
|
109 nexus->written_len += len;
|
|
110
|
|
111 if (nexus->written_len < total_len)
|
|
112 return;
|
|
113
|
|
114 gaim_input_remove(nexus->input_handler);
|
14249
|
115 nexus->input_handler = 0;
|
14192
|
116
|
|
117 g_free(nexus->write_buf);
|
|
118 nexus->write_buf = NULL;
|
|
119 nexus->written_len = 0;
|
|
120
|
|
121 nexus->written_cb(nexus, source, 0);
|
|
122 }
|
|
123
|
|
124 /**************************************************************************
|
|
125 * Login
|
|
126 **************************************************************************/
|
|
127
|
|
128 static void
|
|
129 login_connect_cb(gpointer data, GaimSslConnection *gsc,
|
|
130 GaimInputCondition cond);
|
|
131
|
|
132 static void
|
|
133 login_error_cb(GaimSslConnection *gsc, GaimSslErrorType error, void *data)
|
|
134 {
|
|
135 MsnNexus *nexus;
|
|
136 MsnSession *session;
|
|
137
|
|
138 nexus = data;
|
|
139 g_return_if_fail(nexus != NULL);
|
|
140
|
14249
|
141 nexus->gsc = NULL;
|
|
142
|
14192
|
143 session = nexus->session;
|
|
144 g_return_if_fail(session != NULL);
|
|
145
|
|
146 msn_session_set_error(session, MSN_ERROR_AUTH, _("Unable to connect"));
|
|
147 /* the above line will result in nexus being destroyed, so we don't want
|
|
148 * to destroy it here, or we'd crash */
|
|
149 }
|
|
150
|
|
151 static void
|
|
152 nexus_login_written_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
153 {
|
|
154 MsnNexus *nexus = data;
|
|
155 MsnSession *session;
|
|
156 int len;
|
|
157
|
|
158 session = nexus->session;
|
|
159 g_return_if_fail(session != NULL);
|
|
160
|
14249
|
161 if (nexus->input_handler == 0)
|
|
162 //TODO: Use gaim_ssl_input_add()?
|
14192
|
163 nexus->input_handler = gaim_input_add(nexus->gsc->fd,
|
|
164 GAIM_INPUT_READ, nexus_login_written_cb, nexus);
|
|
165
|
|
166
|
|
167 len = msn_ssl_read(nexus);
|
|
168
|
|
169 if (len < 0 && errno == EAGAIN)
|
|
170 return;
|
|
171 else if (len < 0) {
|
|
172 gaim_input_remove(nexus->input_handler);
|
14249
|
173 nexus->input_handler = 0;
|
14192
|
174 g_free(nexus->read_buf);
|
|
175 nexus->read_buf = NULL;
|
|
176 nexus->read_len = 0;
|
|
177 /* TODO: error handling */
|
|
178 return;
|
|
179 }
|
|
180
|
|
181 if (g_strstr_len(nexus->read_buf, nexus->read_len,
|
|
182 "\r\n\r\n") == NULL)
|
|
183 return;
|
|
184
|
|
185 gaim_input_remove(nexus->input_handler);
|
14249
|
186 nexus->input_handler = 0;
|
14192
|
187
|
|
188 gaim_ssl_close(nexus->gsc);
|
|
189 nexus->gsc = NULL;
|
|
190
|
|
191 gaim_debug_misc("msn", "ssl buffer: {%s}", nexus->read_buf);
|
|
192
|
|
193 if (strstr(nexus->read_buf, "HTTP/1.1 302") != NULL)
|
|
194 {
|
|
195 /* Redirect. */
|
|
196 char *location, *c;
|
|
197
|
|
198 location = strstr(nexus->read_buf, "Location: ");
|
|
199 if (location == NULL)
|
|
200 {
|
|
201 g_free(nexus->read_buf);
|
|
202 nexus->read_buf = NULL;
|
|
203 nexus->read_len = 0;
|
|
204
|
|
205 return;
|
|
206 }
|
|
207 location = strchr(location, ' ') + 1;
|
|
208
|
|
209 if ((c = strchr(location, '\r')) != NULL)
|
|
210 *c = '\0';
|
|
211
|
|
212 /* Skip the http:// */
|
|
213 if ((c = strchr(location, '/')) != NULL)
|
|
214 location = c + 2;
|
|
215
|
|
216 if ((c = strchr(location, '/')) != NULL)
|
|
217 {
|
|
218 g_free(nexus->login_path);
|
|
219 nexus->login_path = g_strdup(c);
|
|
220
|
|
221 *c = '\0';
|
|
222 }
|
|
223
|
|
224 g_free(nexus->login_host);
|
|
225 nexus->login_host = g_strdup(location);
|
|
226
|
14249
|
227 nexus->gsc = gaim_ssl_connect(session->account,
|
|
228 nexus->login_host, GAIM_SSL_DEFAULT_PORT,
|
|
229 login_connect_cb, login_error_cb, nexus);
|
14192
|
230 }
|
|
231 else if (strstr(nexus->read_buf, "HTTP/1.1 401 Unauthorized") != NULL)
|
|
232 {
|
|
233 const char *error;
|
|
234
|
|
235 if ((error = strstr(nexus->read_buf, "WWW-Authenticate")) != NULL)
|
|
236 {
|
|
237 if ((error = strstr(error, "cbtxt=")) != NULL)
|
|
238 {
|
|
239 const char *c;
|
|
240 char *temp;
|
|
241
|
|
242 error += strlen("cbtxt=");
|
|
243
|
|
244 if ((c = strchr(error, '\n')) == NULL)
|
|
245 c = error + strlen(error);
|
|
246
|
|
247 temp = g_strndup(error, c - error);
|
|
248 error = gaim_url_decode(temp);
|
|
249 g_free(temp);
|
|
250 }
|
|
251 }
|
|
252
|
|
253 msn_session_set_error(session, MSN_ERROR_AUTH, error);
|
|
254 }
|
|
255 else if (strstr(nexus->read_buf, "HTTP/1.1 200 OK"))
|
|
256 {
|
|
257 char *base, *c;
|
|
258 char *login_params;
|
|
259
|
|
260 #if 0
|
|
261 /* All your base are belong to us. */
|
|
262 base = buffer;
|
|
263
|
|
264 /* For great cookie! */
|
|
265 while ((base = strstr(base, "Set-Cookie: ")) != NULL)
|
|
266 {
|
|
267 base += strlen("Set-Cookie: ");
|
|
268
|
|
269 c = strchr(base, ';');
|
|
270
|
|
271 session->login_cookies =
|
|
272 g_list_append(session->login_cookies,
|
|
273 g_strndup(base, c - base));
|
|
274 }
|
|
275 #endif
|
|
276
|
|
277 base = strstr(nexus->read_buf, "Authentication-Info: ");
|
|
278
|
|
279 g_return_if_fail(base != NULL);
|
|
280
|
|
281 base = strstr(base, "from-PP='");
|
|
282 base += strlen("from-PP='");
|
|
283 c = strchr(base, '\'');
|
|
284
|
|
285 login_params = g_strndup(base, c - base);
|
|
286
|
|
287 msn_got_login_params(session, login_params);
|
|
288
|
|
289 g_free(login_params);
|
|
290
|
|
291 msn_nexus_destroy(nexus);
|
|
292 session->nexus = NULL;
|
|
293 return;
|
|
294 }
|
|
295
|
|
296 g_free(nexus->read_buf);
|
|
297 nexus->read_buf = NULL;
|
|
298 nexus->read_len = 0;
|
|
299
|
|
300 }
|
|
301
|
|
302 /* this guards against missing hash entries */
|
|
303 static char *
|
|
304 nexus_challenge_data_lookup(GHashTable *challenge_data, const char *key)
|
|
305 {
|
|
306 char *entry;
|
|
307
|
|
308 return (entry = (char *)g_hash_table_lookup(challenge_data, key)) ?
|
|
309 entry : "(null)";
|
|
310 }
|
|
311
|
|
312 void
|
|
313 login_connect_cb(gpointer data, GaimSslConnection *gsc,
|
|
314 GaimInputCondition cond)
|
|
315 {
|
|
316 MsnNexus *nexus;
|
|
317 MsnSession *session;
|
|
318 char *username, *password;
|
|
319 char *request_str, *head, *tail;
|
|
320 char *buffer = NULL;
|
|
321 guint32 ctint;
|
|
322
|
|
323 nexus = data;
|
|
324 g_return_if_fail(nexus != NULL);
|
|
325
|
|
326 session = nexus->session;
|
|
327 g_return_if_fail(session != NULL);
|
|
328
|
|
329 msn_session_set_login_step(session, MSN_LOGIN_STEP_GET_COOKIE);
|
|
330
|
|
331 username =
|
|
332 g_strdup(gaim_url_encode(gaim_account_get_username(session->account)));
|
|
333
|
|
334 password =
|
|
335 g_strdup(gaim_url_encode(gaim_connection_get_password(session->account->gc)));
|
|
336
|
|
337 ctint = strtoul((char *)g_hash_table_lookup(nexus->challenge_data, "ct"), NULL, 10) + 200;
|
|
338
|
|
339 head = g_strdup_printf(
|
|
340 "GET %s HTTP/1.1\r\n"
|
|
341 "Authorization: Passport1.4 OrgVerb=GET,OrgURL=%s,sign-in=%s",
|
|
342 nexus->login_path,
|
|
343 (char *)g_hash_table_lookup(nexus->challenge_data, "ru"),
|
|
344 username);
|
|
345
|
|
346 tail = g_strdup_printf(
|
|
347 "lc=%s,id=%s,tw=%s,fs=%s,ru=%s,ct=%" G_GUINT32_FORMAT ",kpp=%s,kv=%s,ver=%s,tpf=%s\r\n"
|
|
348 "User-Agent: MSMSGS\r\n"
|
|
349 "Host: %s\r\n"
|
|
350 "Connection: Keep-Alive\r\n"
|
|
351 "Cache-Control: no-cache\r\n",
|
|
352 nexus_challenge_data_lookup(nexus->challenge_data, "lc"),
|
|
353 nexus_challenge_data_lookup(nexus->challenge_data, "id"),
|
|
354 nexus_challenge_data_lookup(nexus->challenge_data, "tw"),
|
|
355 nexus_challenge_data_lookup(nexus->challenge_data, "fs"),
|
|
356 nexus_challenge_data_lookup(nexus->challenge_data, "ru"),
|
|
357 ctint,
|
|
358 nexus_challenge_data_lookup(nexus->challenge_data, "kpp"),
|
|
359 nexus_challenge_data_lookup(nexus->challenge_data, "kv"),
|
|
360 nexus_challenge_data_lookup(nexus->challenge_data, "ver"),
|
|
361 nexus_challenge_data_lookup(nexus->challenge_data, "tpf"),
|
|
362 nexus->login_host);
|
|
363
|
|
364 buffer = g_strdup_printf("%s,pwd=XXXXXXXX,%s\r\n", head, tail);
|
|
365 request_str = g_strdup_printf("%s,pwd=%s,%s\r\n", head, password, tail);
|
|
366
|
|
367 gaim_debug_misc("msn", "Sending: {%s}\n", buffer);
|
|
368
|
|
369 g_free(buffer);
|
|
370 g_free(head);
|
|
371 g_free(tail);
|
|
372 g_free(username);
|
|
373 g_free(password);
|
|
374
|
|
375 nexus->write_buf = request_str;
|
|
376 nexus->written_len = 0;
|
|
377
|
|
378 nexus->read_len = 0;
|
|
379
|
|
380 nexus->written_cb = nexus_login_written_cb;
|
|
381
|
|
382 nexus->input_handler = gaim_input_add(gsc->fd, GAIM_INPUT_WRITE,
|
|
383 nexus_write_cb, nexus);
|
|
384
|
|
385 nexus_write_cb(nexus, gsc->fd, GAIM_INPUT_WRITE);
|
|
386
|
|
387 return;
|
|
388
|
|
389
|
|
390 }
|
|
391
|
|
392 static void
|
|
393 nexus_connect_written_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
394 {
|
|
395 MsnNexus *nexus = data;
|
|
396 int len;
|
|
397 char *da_login;
|
|
398 char *base, *c;
|
|
399
|
14249
|
400 if (nexus->input_handler == 0)
|
|
401 //TODO: Use gaim_ssl_input_add()?
|
14192
|
402 nexus->input_handler = gaim_input_add(nexus->gsc->fd,
|
|
403 GAIM_INPUT_READ, nexus_connect_written_cb, nexus);
|
|
404
|
|
405 /* Get the PassportURLs line. */
|
|
406 len = msn_ssl_read(nexus);
|
|
407
|
|
408 if (len < 0 && errno == EAGAIN)
|
|
409 return;
|
|
410 else if (len < 0) {
|
|
411 gaim_input_remove(nexus->input_handler);
|
14249
|
412 nexus->input_handler = 0;
|
14192
|
413 g_free(nexus->read_buf);
|
|
414 nexus->read_buf = NULL;
|
|
415 nexus->read_len = 0;
|
|
416 /* TODO: error handling */
|
|
417 return;
|
|
418 }
|
|
419
|
|
420 if (g_strstr_len(nexus->read_buf, nexus->read_len,
|
|
421 "\r\n\r\n") == NULL)
|
|
422 return;
|
|
423
|
|
424 gaim_input_remove(nexus->input_handler);
|
14249
|
425 nexus->input_handler = 0;
|
14192
|
426
|
|
427 base = strstr(nexus->read_buf, "PassportURLs");
|
|
428
|
|
429 if (base == NULL)
|
|
430 {
|
|
431 g_free(nexus->read_buf);
|
|
432 nexus->read_buf = NULL;
|
|
433 nexus->read_len = 0;
|
|
434 return;
|
|
435 }
|
|
436
|
|
437 if ((da_login = strstr(base, "DALogin=")) != NULL)
|
|
438 {
|
|
439 /* skip over "DALogin=" */
|
|
440 da_login += 8;
|
|
441
|
|
442 if ((c = strchr(da_login, ',')) != NULL)
|
|
443 *c = '\0';
|
|
444
|
|
445 if ((c = strchr(da_login, '/')) != NULL)
|
|
446 {
|
|
447 nexus->login_path = g_strdup(c);
|
|
448 *c = '\0';
|
|
449 }
|
|
450
|
|
451 nexus->login_host = g_strdup(da_login);
|
|
452 }
|
|
453
|
|
454 g_free(nexus->read_buf);
|
|
455 nexus->read_buf = NULL;
|
|
456 nexus->read_len = 0;
|
|
457
|
|
458 gaim_ssl_close(nexus->gsc);
|
|
459
|
|
460 /* Now begin the connection to the login server. */
|
14249
|
461 nexus->gsc = gaim_ssl_connect(nexus->session->account,
|
|
462 nexus->login_host, GAIM_SSL_DEFAULT_PORT,
|
|
463 login_connect_cb, login_error_cb, nexus);
|
14192
|
464 }
|
|
465
|
|
466
|
|
467 /**************************************************************************
|
|
468 * Connect
|
|
469 **************************************************************************/
|
|
470
|
|
471 static void
|
|
472 nexus_connect_cb(gpointer data, GaimSslConnection *gsc,
|
|
473 GaimInputCondition cond)
|
|
474 {
|
|
475 MsnNexus *nexus;
|
|
476 MsnSession *session;
|
|
477
|
|
478 nexus = data;
|
|
479 g_return_if_fail(nexus != NULL);
|
|
480
|
|
481 session = nexus->session;
|
|
482 g_return_if_fail(session != NULL);
|
|
483
|
|
484 msn_session_set_login_step(session, MSN_LOGIN_STEP_AUTH);
|
|
485
|
|
486 nexus->write_buf = g_strdup("GET /rdr/pprdr.asp\r\n\r\n");
|
|
487 nexus->written_len = 0;
|
|
488
|
|
489 nexus->read_len = 0;
|
|
490
|
|
491 nexus->written_cb = nexus_connect_written_cb;
|
|
492
|
|
493 nexus->input_handler = gaim_input_add(gsc->fd, GAIM_INPUT_WRITE,
|
|
494 nexus_write_cb, nexus);
|
|
495
|
|
496 nexus_write_cb(nexus, gsc->fd, GAIM_INPUT_WRITE);
|
|
497 }
|
|
498
|
|
499 void
|
|
500 msn_nexus_connect(MsnNexus *nexus)
|
|
501 {
|
14249
|
502 nexus->gsc = gaim_ssl_connect(nexus->session->account,
|
|
503 "nexus.passport.com", GAIM_SSL_DEFAULT_PORT,
|
|
504 nexus_connect_cb, login_error_cb, nexus);
|
14192
|
505 }
|