Mercurial > pidgin
annotate src/protocols/msn/nexus.c @ 10261:d4e9ff2edc4e
[gaim-migrate @ 11405]
This should fix segfault bug 1072604. Oops.
committer: Tailor Script <tailor@pidgin.im>
| author | Tim Ringenbach <marv@pidgin.im> |
|---|---|
| date | Thu, 25 Nov 2004 18:35:26 +0000 |
| parents | b15c177895b9 |
| children | 2e01c503aa4f |
| 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 /************************************************************************** | |
| 29 * Util | |
| 30 **************************************************************************/ | |
| 31 | |
| 32 static size_t | |
| 33 msn_ssl_read(GaimSslConnection *gsc, char **dest_buffer) | |
| 34 { | |
| 8925 | 35 gssize size = 0, s; |
| 8171 | 36 char *buffer = NULL; |
| 37 char temp_buf[4096]; | |
| 38 | |
| 39 while ((s = gaim_ssl_read(gsc, temp_buf, sizeof(temp_buf))) > 0) | |
| 40 { | |
| 41 buffer = g_realloc(buffer, size + s + 1); | |
| 42 | |
| 43 strncpy(buffer + size, temp_buf, s); | |
| 44 | |
| 45 buffer[size + s] = '\0'; | |
| 46 | |
| 47 size += s; | |
| 48 } | |
| 49 | |
| 50 *dest_buffer = buffer; | |
| 51 | |
| 52 return size; | |
| 53 } | |
| 54 | |
| 55 /************************************************************************** | |
| 56 * Login | |
| 57 **************************************************************************/ | |
| 58 | |
| 59 static void | |
| 60 login_error_cb(GaimSslConnection *gsc, GaimSslErrorType error, void *data) | |
| 61 { | |
| 62 MsnNexus *nexus; | |
| 63 MsnSession *session; | |
| 64 GaimAccount *account; | |
| 65 GaimConnection *gc; | |
| 66 | |
| 67 nexus = data; | |
| 68 g_return_if_fail(nexus != NULL); | |
| 69 | |
| 70 session = nexus->session; | |
| 71 g_return_if_fail(session != NULL); | |
| 72 | |
| 73 account = session->account; | |
| 74 g_return_if_fail(account != NULL); | |
| 75 | |
| 76 gc = gaim_account_get_connection(account); | |
| 77 g_return_if_fail(gc != NULL); | |
| 78 | |
| 79 gaim_connection_error(gc, _("Unable to connect to server")); | |
| 80 | |
| 81 msn_nexus_destroy(nexus); | |
| 82 session->nexus = NULL; | |
| 83 } | |
| 84 | |
| 85 static void | |
| 86 login_connect_cb(gpointer data, GaimSslConnection *gsc, | |
| 87 GaimInputCondition cond) | |
| 88 { | |
| 89 MsnNexus *nexus; | |
| 90 MsnSession *session; | |
| 91 char *username, *password; | |
| 92 char *request_str; | |
| 93 char *buffer = NULL; | |
| 9966 | 94 guint32 ctint; |
| 8171 | 95 size_t s; |
| 96 | |
| 97 nexus = data; | |
| 98 g_return_if_fail(nexus != NULL); | |
| 99 | |
| 100 session = nexus->session; | |
| 101 g_return_if_fail(session != NULL); | |
| 102 | |
| 103 username = | |
| 104 g_strdup(gaim_url_encode(gaim_account_get_username(session->account))); | |
| 105 | |
| 106 password = | |
| 107 g_strdup(gaim_url_encode(gaim_account_get_password(session->account))); | |
| 108 | |
| 9966 | 109 ctint = strtoul((char *)g_hash_table_lookup(nexus->challenge_data, "ct"), NULL, 10) + 200; |
| 110 | |
| 8171 | 111 request_str = g_strdup_printf( |
| 112 "GET %s HTTP/1.1\r\n" | |
| 113 "Authorization: Passport1.4 OrgVerb=GET,OrgURL=%s,sign-in=%s,pwd=%s," | |
| 9966 | 114 "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 | 115 "User-Agent: MSMSGS\r\n" |
| 116 "Host: %s\r\n" | |
| 117 "Connection: Keep-Alive\r\n" | |
| 118 "Cache-Control: no-cache\r\n" | |
| 119 "\r\n", | |
| 120 nexus->login_path, | |
| 121 (char *)g_hash_table_lookup(nexus->challenge_data, "ru"), | |
| 122 username, password, | |
| 123 (char *)g_hash_table_lookup(nexus->challenge_data, "lc"), | |
| 124 (char *)g_hash_table_lookup(nexus->challenge_data, "id"), | |
| 125 (char *)g_hash_table_lookup(nexus->challenge_data, "tw"), | |
| 126 (char *)g_hash_table_lookup(nexus->challenge_data, "fs"), | |
| 127 (char *)g_hash_table_lookup(nexus->challenge_data, "ru"), | |
| 9966 | 128 ctint, |
| 8171 | 129 (char *)g_hash_table_lookup(nexus->challenge_data, "kpp"), |
| 130 (char *)g_hash_table_lookup(nexus->challenge_data, "kv"), | |
| 131 (char *)g_hash_table_lookup(nexus->challenge_data, "ver"), | |
| 132 (char *)g_hash_table_lookup(nexus->challenge_data, "tpf"), | |
| 133 nexus->login_host); | |
| 134 | |
| 135 gaim_debug(GAIM_DEBUG_MISC, "msn", "Sending: {%s}\n", request_str); | |
| 136 | |
| 137 g_free(username); | |
| 138 g_free(password); | |
| 139 | |
| 140 if ((s = gaim_ssl_write(gsc, request_str, strlen(request_str))) <= 0) | |
| 141 { | |
| 142 g_free(request_str); | |
| 143 | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 g_free(request_str); | |
| 148 | |
| 149 if ((s = msn_ssl_read(gsc, &buffer)) <= 0) | |
| 150 return; | |
| 151 | |
| 152 gaim_ssl_close(gsc); | |
| 153 | |
| 154 gaim_debug(GAIM_DEBUG_MISC, "msn", "ssl buffer: {%s}", buffer); | |
| 155 | |
| 156 if (strstr(buffer, "HTTP/1.1 302") != NULL) | |
| 157 { | |
| 158 /* Redirect. */ | |
| 159 char *location, *c; | |
| 160 | |
| 161 location = strstr(buffer, "Location: "); | |
| 162 if (location == NULL) | |
| 163 { | |
| 164 g_free(buffer); | |
| 165 | |
| 166 return; | |
| 167 } | |
| 168 location = strchr(location, ' ') + 1; | |
| 169 | |
| 170 if ((c = strchr(location, '\r')) != NULL) | |
| 171 *c = '\0'; | |
| 172 | |
| 173 /* Skip the http:// */ | |
| 174 if ((c = strchr(location, '/')) != NULL) | |
| 175 location = c + 2; | |
| 176 | |
| 177 if ((c = strchr(location, '/')) != NULL) | |
| 178 { | |
| 179 g_free(nexus->login_path); | |
| 180 nexus->login_path = g_strdup(c); | |
| 181 | |
| 182 *c = '\0'; | |
| 183 } | |
| 184 | |
| 185 g_free(nexus->login_host); | |
| 186 nexus->login_host = g_strdup(location); | |
| 187 | |
| 188 gaim_ssl_connect(session->account, nexus->login_host, | |
| 189 GAIM_SSL_DEFAULT_PORT, login_connect_cb, | |
| 190 login_error_cb, nexus); | |
| 191 } | |
| 192 else if (strstr(buffer, "HTTP/1.1 401 Unauthorized") != NULL) | |
| 193 { | |
| 194 GaimConnection *gc; | |
| 195 const char *error, *c; | |
| 196 char *temp; | |
| 197 | |
| 198 if ((error = strstr(buffer, "WWW-Authenticate")) != NULL) | |
| 199 { | |
| 200 if ((error = strstr(error, "cbtxt=")) != NULL) | |
| 201 { | |
| 202 error += strlen("cbtxt="); | |
| 203 | |
| 204 if ((c = strchr(error, '\n')) == NULL) | |
| 205 c = error + strlen(error); | |
| 206 | |
| 207 temp = g_strndup(error, c - error); | |
| 208 error = gaim_url_decode(temp); | |
| 209 g_free(temp); | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 gc = gaim_account_get_connection(session->account); | |
| 214 | |
| 215 if (error == NULL) | |
| 216 { | |
| 217 gaim_connection_error(gc, | |
| 218 _("Unknown error when attempting to authorize with " | |
| 219 "MSN login server.")); | |
| 220 } | |
| 221 else | |
| 222 gaim_connection_error(gc, error); | |
| 223 } | |
| 224 else if (strstr(buffer, "HTTP/1.1 200 OK")) | |
| 225 { | |
| 226 char *base, *c; | |
| 227 char *login_params; | |
| 228 | |
| 229 #if 0 | |
| 230 /* All your base are belong to us. */ | |
| 231 base = buffer; | |
| 232 | |
| 233 /* For great cookie! */ | |
| 234 while ((base = strstr(base, "Set-Cookie: ")) != NULL) | |
| 235 { | |
| 236 base += strlen("Set-Cookie: "); | |
| 237 | |
| 238 c = strchr(base, ';'); | |
| 239 | |
| 240 session->login_cookies = | |
| 241 g_list_append(session->login_cookies, | |
| 242 g_strndup(base, c - base)); | |
| 243 } | |
| 244 #endif | |
| 245 | |
| 246 base = strstr(buffer, "Authentication-Info: "); | |
| 247 | |
|
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
8925
diff
changeset
|
248 g_return_if_fail(base != NULL); |
| 8171 | 249 |
| 250 base = strstr(base, "from-PP='"); | |
| 251 base += strlen("from-PP='"); | |
| 252 c = strchr(base, '\''); | |
| 253 | |
| 254 login_params = g_strndup(base, c - base); | |
| 255 | |
|
9193
502707ca1836
[gaim-migrate @ 9988]
Christian Hammond <chipx86@chipx86.com>
parents:
8925
diff
changeset
|
256 msn_got_login_params(session, login_params); |
| 8171 | 257 |
| 258 g_free(login_params); | |
| 259 | |
| 260 msn_nexus_destroy(nexus); | |
| 261 session->nexus = NULL; | |
| 262 } | |
| 263 | |
| 264 g_free(buffer); | |
| 265 } | |
| 266 | |
| 267 static void | |
| 268 nexus_connect_cb(gpointer data, GaimSslConnection *gsc, | |
| 269 GaimInputCondition cond) | |
| 270 { | |
| 271 MsnNexus *nexus; | |
| 272 MsnSession *session; | |
| 273 char *request_str; | |
| 274 char *da_login; | |
| 275 char *base, *c; | |
| 276 char *buffer = NULL; | |
| 277 size_t s; | |
| 278 | |
| 279 nexus = data; | |
| 280 g_return_if_fail(nexus != NULL); | |
| 281 | |
| 282 session = nexus->session; | |
| 283 g_return_if_fail(session != NULL); | |
| 284 | |
| 285 request_str = g_strdup_printf("GET /rdr/pprdr.asp\r\n\r\n"); | |
| 286 | |
| 287 if ((s = gaim_ssl_write(gsc, request_str, strlen(request_str))) <= 0) | |
| 288 { | |
| 289 g_free(request_str); | |
| 290 return; | |
| 291 } | |
| 292 | |
| 293 g_free(request_str); | |
| 294 | |
| 295 /* Get the PassportURLs line. */ | |
| 296 if ((s = msn_ssl_read(gsc, &buffer)) <= 0) | |
| 297 return; | |
| 298 | |
| 299 base = strstr(buffer, "PassportURLs"); | |
| 300 | |
| 301 if (base == NULL) | |
| 302 { | |
| 303 g_free(buffer); | |
| 304 return; | |
| 305 } | |
| 306 | |
| 307 if ((da_login = strstr(base, "DALogin=")) != NULL) | |
| 308 { | |
| 309 if ((da_login = strchr(da_login, '=')) != NULL) | |
| 310 da_login++; | |
| 311 | |
| 312 if ((c = strchr(da_login, ',')) != NULL) | |
| 313 *c = '\0'; | |
| 314 | |
| 315 if ((c = strchr(da_login, '/')) != NULL) | |
| 316 { | |
| 317 nexus->login_path = g_strdup(c); | |
| 318 | |
| 319 *c = '\0'; | |
| 320 } | |
| 321 | |
| 322 nexus->login_host = g_strdup(da_login); | |
| 323 } | |
| 324 | |
| 325 g_free(buffer); | |
| 326 | |
| 327 gaim_ssl_close(gsc); | |
| 328 | |
| 329 /* Now begin the connection to the login server. */ | |
| 330 gaim_ssl_connect(session->account, nexus->login_host, | |
| 331 GAIM_SSL_DEFAULT_PORT, login_connect_cb, | |
| 332 login_error_cb, nexus); | |
| 333 } | |
| 334 | |
| 335 /************************************************************************** | |
| 336 * Nexus | |
| 337 **************************************************************************/ | |
| 338 | |
| 339 MsnNexus * | |
| 340 msn_nexus_new(MsnSession *session) | |
| 341 { | |
| 342 MsnNexus *nexus; | |
| 343 | |
| 344 nexus = g_new0(MsnNexus, 1); | |
| 345 nexus->session = session; | |
| 346 nexus->challenge_data = g_hash_table_new_full(g_str_hash, g_str_equal, | |
| 347 g_free, g_free); | |
| 348 | |
| 349 return nexus; | |
| 350 } | |
| 351 | |
| 352 void | |
| 353 msn_nexus_destroy(MsnNexus *nexus) | |
| 354 { | |
| 355 if (nexus->login_host != NULL) | |
| 356 g_free(nexus->login_host); | |
| 357 | |
| 358 if (nexus->login_path != NULL) | |
| 359 g_free(nexus->login_path); | |
| 360 | |
| 361 if (nexus->challenge_data != NULL) | |
| 362 g_hash_table_destroy(nexus->challenge_data); | |
| 363 | |
| 364 g_free(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 } |
