Mercurial > pidgin
annotate src/protocols/msn/nexus.c @ 10481:bcfea6c3d5c9
[gaim-migrate @ 11769]
Patch 1093958 from Felipe Contreras. It fixes stuff.
I also made some tweaks to make valgrind a bit happier.
committer: Tailor Script <tailor@pidgin.im>
| author | Stu Tomlinson <stu@nosnilmot.com> |
|---|---|
| date | Fri, 07 Jan 2005 02:48:33 +0000 |
| parents | 2e01c503aa4f |
| children | 1a97d5e88d12 |
| rev | line source |
|---|---|
|
9198
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
1 /** |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
2 * @file nexus.c MSN Nexus functions |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
3 * |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
4 * gaim |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
5 * |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
6 * Gaim is the legal property of its developers, whose names are too numerous |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
8 * source distribution. |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
9 * |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
10 * This program is free software; you can redistribute it and/or modify |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
11 * it under the terms of the GNU General Public License as published by |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
12 * the Free Software Foundation; either version 2 of the License, or |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
13 * (at your option) any later version. |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
14 * |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
15 * This program is distributed in the hope that it will be useful, |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
18 * GNU General Public License for more details. |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
19 * |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
20 * You should have received a copy of the GNU General Public License |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
21 * along with this program; if not, write to the Free Software |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
ab6636c5a136
[gaim-migrate @ 9993]
Christian Hammond <chipx86@chipx86.com>
parents:
9193
diff
changeset
|
23 */ |
| 8171 | 24 #include "msn.h" |
| 25 #include "nexus.h" | |
| 26 #include "notification.h" | |
| 27 | |
| 28 /************************************************************************** | |
| 10481 | 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, g_str_equal, | |
| 40 g_free, g_free); | |
| 41 | |
| 42 return nexus; | |
| 43 } | |
| 44 | |
| 45 void | |
| 46 msn_nexus_destroy(MsnNexus *nexus) | |
| 47 { | |
| 48 if (nexus->login_host != NULL) | |
| 49 g_free(nexus->login_host); | |
| 50 | |
| 51 if (nexus->login_path != NULL) | |
| 52 g_free(nexus->login_path); | |
| 53 | |
| 54 if (nexus->challenge_data != NULL) | |
| 55 g_hash_table_destroy(nexus->challenge_data); | |
| 56 | |
| 57 g_free(nexus); | |
| 58 } | |
| 59 | |
| 60 /************************************************************************** | |
| 8171 | 61 * Util |
| 62 **************************************************************************/ | |
| 63 | |
| 64 static size_t | |
| 65 msn_ssl_read(GaimSslConnection *gsc, char **dest_buffer) | |
| 66 { | |
| 8925 | 67 gssize size = 0, s; |
| 8171 | 68 char *buffer = NULL; |
| 69 char temp_buf[4096]; | |
| 70 | |
| 71 while ((s = gaim_ssl_read(gsc, temp_buf, sizeof(temp_buf))) > 0) | |
| 72 { | |
| 73 buffer = g_realloc(buffer, size + s + 1); | |
| 74 | |
| 75 strncpy(buffer + size, temp_buf, s); | |
| 76 | |
| 77 buffer[size + s] = '\0'; | |
| 78 | |
| 79 size += s; | |
| 80 } | |
| 81 | |
| 82 *dest_buffer = buffer; | |
| 83 | |
| 84 return size; | |
| 85 } | |
| 86 | |
| 87 /************************************************************************** | |
| 88 * Login | |
| 89 **************************************************************************/ | |
| 90 | |
| 91 static void | |
| 92 login_error_cb(GaimSslConnection *gsc, GaimSslErrorType error, void *data) | |
| 93 { | |
| 94 MsnNexus *nexus; | |
| 95 MsnSession *session; | |
| 96 GaimAccount *account; | |
| 97 GaimConnection *gc; | |
| 98 | |
| 99 nexus = data; | |
| 100 g_return_if_fail(nexus != NULL); | |
| 101 | |
| 102 session = nexus->session; | |
| 103 g_return_if_fail(session != NULL); | |
| 104 | |
| 105 account = session->account; | |
| 106 g_return_if_fail(account != NULL); | |
| 107 | |
| 108 gc = gaim_account_get_connection(account); | |
| 109 g_return_if_fail(gc != NULL); | |
| 110 | |
| 10481 | 111 msn_session_set_error(session, MSN_ERROR_AUTH, _("Unable to connect")); |
| 8171 | 112 |
| 113 msn_nexus_destroy(nexus); | |
| 114 session->nexus = NULL; | |
| 115 } | |
| 116 | |
| 117 static void | |
| 118 login_connect_cb(gpointer data, GaimSslConnection *gsc, | |
| 119 GaimInputCondition cond) | |
| 120 { | |
| 121 MsnNexus *nexus; | |
| 122 MsnSession *session; | |
| 123 char *username, *password; | |
| 124 char *request_str; | |
| 125 char *buffer = NULL; | |
| 9966 | 126 guint32 ctint; |
| 8171 | 127 size_t s; |
| 128 | |
| 129 nexus = data; | |
| 130 g_return_if_fail(nexus != NULL); | |
| 131 | |
| 132 session = nexus->session; | |
| 133 g_return_if_fail(session != NULL); | |
| 134 | |
| 10481 | 135 msn_session_set_login_step(session, MSN_LOGIN_STEP_GET_COOKIE); |
| 136 | |
| 8171 | 137 username = |
| 138 g_strdup(gaim_url_encode(gaim_account_get_username(session->account))); | |
| 139 | |
| 140 password = | |
| 141 g_strdup(gaim_url_encode(gaim_account_get_password(session->account))); | |
| 142 | |
| 9966 | 143 ctint = strtoul((char *)g_hash_table_lookup(nexus->challenge_data, "ct"), NULL, 10) + 200; |
| 144 | |
| 8171 | 145 request_str = g_strdup_printf( |
| 146 "GET %s HTTP/1.1\r\n" | |
| 147 "Authorization: Passport1.4 OrgVerb=GET,OrgURL=%s,sign-in=%s,pwd=%s," | |
| 9966 | 148 "lc=%s,id=%s,tw=%s,fs=%s,ru=%s,ct=%" G_GUINT32_FORMAT ",kpp=%s,kv=%s,ver=%s,tpf=%s\r\n" |
| 8171 | 149 "User-Agent: MSMSGS\r\n" |
| 150 "Host: %s\r\n" | |
| 151 "Connection: Keep-Alive\r\n" | |
| 152 "Cache-Control: no-cache\r\n" | |
| 153 "\r\n", | |
| 154 nexus->login_path, | |
| 155 (char *)g_hash_table_lookup(nexus->challenge_data, "ru"), | |
| 156 username, password, | |
| 157 (char *)g_hash_table_lookup(nexus->challenge_data, "lc"), | |
| 158 (char *)g_hash_table_lookup(nexus->challenge_data, "id"), | |
| 159 (char *)g_hash_table_lookup(nexus->challenge_data, "tw"), | |
| 160 (char *)g_hash_table_lookup(nexus->challenge_data, "fs"), | |
| 161 (char *)g_hash_table_lookup(nexus->challenge_data, "ru"), | |
| 9966 | 162 ctint, |
| 8171 | 163 (char *)g_hash_table_lookup(nexus->challenge_data, "kpp"), |
| 164 (char *)g_hash_table_lookup(nexus->challenge_data, "kv"), | |
| 165 (char *)g_hash_table_lookup(nexus->challenge_data, "ver"), | |
| 166 (char *)g_hash_table_lookup(nexus->challenge_data, "tpf"), | |
| 167 nexus->login_host); | |
| 168 | |
| 10345 | 169 gaim_debug_misc("msn", "Sending: {%s}\n", request_str); |
| 8171 | 170 |
| 171 g_free(username); | |
| 172 g_free(password); | |
| 173 | |
| 174 if ((s = gaim_ssl_write(gsc, request_str, strlen(request_str))) <= 0) | |
| 175 { | |
| 176 g_free(request_str); | |
| 177 | |
| 178 return; | |
| 179 } | |
| 180 | |
| 181 g_free(request_str); | |
| 182 | |
| 183 if ((s = msn_ssl_read(gsc, &buffer)) <= 0) | |
| 184 return; | |
| 185 | |
| 186 gaim_ssl_close(gsc); | |
| 187 | |
| 10345 | 188 gaim_debug_misc("msn", "ssl buffer: {%s}", buffer); |
| 8171 | 189 |
| 190 if (strstr(buffer, "HTTP/1.1 302") != NULL) | |
| 191 { | |
| 192 /* Redirect. */ | |
| 193 char *location, *c; | |
| 194 | |
| 195 location = strstr(buffer, "Location: "); | |
| 196 if (location == NULL) | |
| 197 { | |
| 198 g_free(buffer); | |
| 199 | |
| 200 return; | |
| 201 } | |
| 202 location = strchr(location, ' ') + 1; | |
| 203 | |
| 204 if ((c = strchr(location, '\r')) != NULL) | |
| 205 *c = '\0'; | |
| 206 | |
| 207 /* Skip the http:// */ | |
| 208 if ((c = strchr(location, '/')) != NULL) | |
| 209 location = c + 2; | |
| 210 | |
| 211 if ((c = strchr(location, '/')) != NULL) | |
| 212 { | |
| 213 g_free(nexus->login_path); | |
| 214 nexus->login_path = g_strdup(c); | |
| 215 | |
| 216 *c = '\0'; | |
| 217 } | |
| 218 | |
| 219 g_free(nexus->login_host); | |
| 220 nexus->login_host = g_strdup(location); | |
| 221 | |
| 222 gaim_ssl_connect(session->account, nexus->login_host, | |
| 223 GAIM_SSL_DEFAULT_PORT, login_connect_cb, | |
| 224 login_error_cb, nexus); | |
| 225 } | |
| 226 else if (strstr(buffer, "HTTP/1.1 401 Unauthorized") != NULL) | |
| 227 { | |
| 10481 | 228 const char *error; |
| 8171 | 229 |
| 230 if ((error = strstr(buffer, "WWW-Authenticate")) != NULL) | |
| 231 { | |
| 232 if ((error = strstr(error, "cbtxt=")) != NULL) | |
| 233 { | |
| 10481 | 234 const char *c; |
| 235 char *temp; | |
| 236 | |
| 8171 | 237 error += strlen("cbtxt="); |
| 238 | |
| 239 if ((c = strchr(error, '\n')) == NULL) | |
| 240 c = error + strlen(error); | |
| 241 | |
| 242 temp = g_strndup(error, c - error); | |
| 243 error = gaim_url_decode(temp); | |
| 244 g_free(temp); | |
| 245 } | |
| 246 } | |
| 247 | |
| 10481 | 248 msn_session_set_error(session, MSN_ERROR_AUTH, error); |
| 8171 | 249 } |
| 250 else if (strstr(buffer, "HTTP/1.1 200 OK")) | |
| 251 { | |
| 252 char *base, *c; | |
| 253 char *login_params; | |
| 254 | |
| 255 #if 0 | |
| 256 /* All your base are belong to us. */ | |
| 257 base = buffer; | |
| 258 | |
| 259 /* For great cookie! */ | |
| 260 while ((base = strstr(base, "Set-Cookie: ")) != NULL) | |
| 261 { | |
| 262 base += strlen("Set-Cookie: "); | |
| 263 | |
| 264 c = strchr(base, ';'); | |
| 265 | |
| 266 session->login_cookies = | |
| 267 g_list_append(session->login_cookies, | |
| 268 g_strndup(base, c - base)); | |
| 269 } | |
| 270 #endif | |
| 271 | |
| 272 base = strstr(buffer, "Authentication-Info: "); | |
| 273 | |
|
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
8925
diff
changeset
|
274 g_return_if_fail(base != NULL); |
| 8171 | 275 |
| 276 base = strstr(base, "from-PP='"); | |
| 277 base += strlen("from-PP='"); | |
| 278 c = strchr(base, '\''); | |
| 279 | |
| 280 login_params = g_strndup(base, c - base); | |
| 281 | |
|
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
8925
diff
changeset
|
282 msn_got_login_params(session, login_params); |
| 8171 | 283 |
| 284 g_free(login_params); | |
| 285 | |
| 286 msn_nexus_destroy(nexus); | |
| 287 session->nexus = NULL; | |
| 288 } | |
| 289 | |
| 290 g_free(buffer); | |
| 291 } | |
| 292 | |
| 10481 | 293 /************************************************************************** |
| 294 * Connect | |
| 295 **************************************************************************/ | |
| 296 | |
| 8171 | 297 static void |
| 298 nexus_connect_cb(gpointer data, GaimSslConnection *gsc, | |
| 299 GaimInputCondition cond) | |
| 300 { | |
| 301 MsnNexus *nexus; | |
| 302 MsnSession *session; | |
| 303 char *request_str; | |
| 304 char *da_login; | |
| 305 char *base, *c; | |
| 306 char *buffer = NULL; | |
| 307 size_t s; | |
| 308 | |
| 309 nexus = data; | |
| 310 g_return_if_fail(nexus != NULL); | |
| 311 | |
| 312 session = nexus->session; | |
| 313 g_return_if_fail(session != NULL); | |
| 314 | |
| 10481 | 315 msn_session_set_login_step(session, MSN_LOGIN_STEP_AUTH); |
| 316 | |
| 8171 | 317 request_str = g_strdup_printf("GET /rdr/pprdr.asp\r\n\r\n"); |
| 318 | |
| 319 if ((s = gaim_ssl_write(gsc, request_str, strlen(request_str))) <= 0) | |
| 320 { | |
| 321 g_free(request_str); | |
| 322 return; | |
| 323 } | |
| 324 | |
| 325 g_free(request_str); | |
| 326 | |
| 327 /* Get the PassportURLs line. */ | |
| 328 if ((s = msn_ssl_read(gsc, &buffer)) <= 0) | |
| 329 return; | |
| 330 | |
| 331 base = strstr(buffer, "PassportURLs"); | |
| 332 | |
| 333 if (base == NULL) | |
| 334 { | |
| 335 g_free(buffer); | |
| 336 return; | |
| 337 } | |
| 338 | |
| 339 if ((da_login = strstr(base, "DALogin=")) != NULL) | |
| 340 { | |
| 341 if ((da_login = strchr(da_login, '=')) != NULL) | |
| 342 da_login++; | |
| 343 | |
| 344 if ((c = strchr(da_login, ',')) != NULL) | |
| 345 *c = '\0'; | |
| 346 | |
| 347 if ((c = strchr(da_login, '/')) != NULL) | |
| 348 { | |
| 349 nexus->login_path = g_strdup(c); | |
| 350 | |
| 351 *c = '\0'; | |
| 352 } | |
| 353 | |
| 354 nexus->login_host = g_strdup(da_login); | |
| 355 } | |
| 356 | |
| 357 g_free(buffer); | |
| 358 | |
| 359 gaim_ssl_close(gsc); | |
| 360 | |
| 361 /* Now begin the connection to the login server. */ | |
| 362 gaim_ssl_connect(session->account, nexus->login_host, | |
| 363 GAIM_SSL_DEFAULT_PORT, login_connect_cb, | |
| 364 login_error_cb, nexus); | |
| 365 } | |
| 366 | |
| 367 void | |
| 368 msn_nexus_connect(MsnNexus *nexus) | |
| 369 { | |
| 370 gaim_ssl_connect(nexus->session->account, "nexus.passport.com", | |
| 371 GAIM_SSL_DEFAULT_PORT, nexus_connect_cb, | |
| 372 login_error_cb, nexus); | |
| 373 } |
