comparison libpurple/protocols/qq/login_logout.c @ 15373:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15372:f79e0f4df793 15373:5fe8042783c1
1 /**
2 * @file login_logout.c
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
25 #include "debug.h"
26 #include "internal.h"
27 #include "server.h"
28
29 #include "buddy_info.h"
30 #include "buddy_list.h"
31 #include "buddy_status.h"
32 #include "char_conv.h"
33 #include "crypt.h"
34 #include "group.h"
35 #include "header_info.h"
36 #include "login_logout.h"
37 #include "packet_parse.h"
38 #include "qq.h"
39 #include "qq_proxy.h"
40 #include "send_core.h"
41 #include "utils.h"
42
43 #define QQ_LOGIN_DATA_LENGTH 416
44 #define QQ_LOGIN_REPLY_OK_PACKET_LEN 139
45 #define QQ_LOGIN_REPLY_REDIRECT_PACKET_LEN 11
46
47 #define QQ_REQUEST_LOGIN_TOKEN_REPLY_OK 0x00
48
49 #define QQ_LOGIN_REPLY_OK 0x00
50 #define QQ_LOGIN_REPLY_REDIRECT 0x01
51 #define QQ_LOGIN_REPLY_PWD_ERROR 0x05
52 #define QQ_LOGIN_REPLY_MISC_ERROR 0xff /* defined by myself */
53
54 /* for QQ 2003iii 0117, fixed value */
55 /* static const guint8 login_23_51[29] = {
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0xbf, 0x14, 0x11, 0x20,
58 0x03, 0x9d, 0xb2, 0xe6, 0xb3, 0x11, 0xb7, 0x13,
59 0x95, 0x67, 0xda, 0x2c, 0x01
60 }; */
61
62 /* for QQ 2003iii 0304, fixed value */
63 /*
64 static const guint8 login_23_51[29] = {
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x9a, 0x93, 0xfe, 0x85,
67 0xd3, 0xd9, 0x2a, 0x41, 0xc8, 0x0d, 0xff, 0xb6,
68 0x40, 0xb8, 0xac, 0x32, 0x01
69 };
70 */
71
72 /* for QQ 2005? copy from lumaqq */
73 static const gint8 login_23_51[29] = {
74 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, -122,
76 -52, 76, 53, 44, -45, 115, 108, 20, -10, -10,
77 -81, -61, -6, 51, -92, 1
78 };
79
80 static const gint8 login_53_68[16] = {
81 -115, -117, -6, -20, -43, 82, 23, 74, -122, -7,
82 -89, 117, -26, 50, -47, 109
83 };
84
85 static const gint8 login_100_bytes[100] = {
86 64,
87 11, 4, 2, 0, 1, 0, 0, 0, 0, 0,
88 3, 9, 0, 0, 0, 0, 0, 0, 0, 1,
89 -23, 3, 1, 0, 0, 0, 0, 0, 1, -13,
90 3, 0, 0, 0, 0, 0, 0, 1, -19, 3,
91 0, 0, 0, 0, 0, 0, 1, -20, 3, 0,
92 0, 0, 0, 0, 0, 3, 5, 0, 0, 0,
93 0, 0, 0, 0, 3, 7, 0, 0, 0, 0,
94 0, 0, 0, 1, -18, 3, 0, 0, 0, 0,
95 0, 0, 1, -17, 3, 0, 0, 0, 0, 0,
96 0, 1, -21, 3, 0, 0, 0, 0, 0
97 };
98
99 /* fixed value, not affected by version, or mac address */
100 /*
101 static const guint8 login_53_68[16] = {
102 0x82, 0x2a, 0x91, 0xfd, 0xa5, 0xca, 0x67, 0x4c,
103 0xac, 0x81, 0x1f, 0x6f, 0x52, 0x05, 0xa7, 0xbf
104 };
105 */
106
107
108 typedef struct _qq_login_reply_ok qq_login_reply_ok_packet;
109 typedef struct _qq_login_reply_redirect qq_login_reply_redirect_packet;
110
111 struct _qq_login_reply_ok {
112 guint8 result;
113 guint8 *session_key;
114 guint32 uid;
115 guint8 client_ip[4]; /* those detected by server */
116 guint16 client_port;
117 guint8 server_ip[4];
118 guint16 server_port;
119 time_t login_time;
120 guint8 unknown1[26];
121 guint8 unknown_server1_ip[4];
122 guint16 unknown_server1_port;
123 guint8 unknown_server2_ip[4];
124 guint16 unknown_server2_port;
125 guint16 unknown2; /* 0x0001 */
126 guint16 unknown3; /* 0x0000 */
127 guint8 unknown4[32];
128 guint8 unknown5[12];
129 guint8 last_client_ip[4];
130 time_t last_login_time;
131 guint8 unknown6[8];
132 };
133
134 struct _qq_login_reply_redirect {
135 guint8 result;
136 guint32 uid;
137 guint8 new_server_ip[4];
138 guint16 new_server_port;
139 };
140
141 extern gint /* defined in send_core.c */
142 _create_packet_head_seq(guint8 *buf,
143 guint8 **cursor, GaimConnection *gc, guint16 cmd, gboolean is_auto_seq, guint16 *seq);
144 extern gint /* defined in send_core.c */
145 _qq_send_packet(GaimConnection *gc, guint8 *buf, gint len, guint16 cmd);
146
147 /* It is fixed to 16 bytes 0x01 for QQ2003,
148 * Any value works (or a random 16 bytes string) */
149 static guint8 *_gen_login_key(void)
150 {
151 return (guint8 *) g_strnfill(QQ_KEY_LENGTH, 0x01);
152 }
153
154 /* process login reply which says OK */
155 static gint _qq_process_login_ok(GaimConnection *gc, guint8 *data, gint len)
156 {
157 gint bytes;
158 guint8 *cursor;
159 qq_data *qd;
160 qq_login_reply_ok_packet lrop;
161
162 qd = (qq_data *) gc->proto_data;
163 cursor = data;
164 bytes = 0;
165
166 /* 000-000: reply code */
167 bytes += read_packet_b(data, &cursor, len, &lrop.result);
168 /* 001-016: session key */
169 lrop.session_key = g_memdup(cursor, QQ_KEY_LENGTH);
170 cursor += QQ_KEY_LENGTH;
171 bytes += QQ_KEY_LENGTH;
172 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Get session_key done\n");
173 /* 017-020: login uid */
174 bytes += read_packet_dw(data, &cursor, len, &lrop.uid);
175 /* 021-024: server detected user public IP */
176 bytes += read_packet_data(data, &cursor, len, (guint8 *) &lrop.client_ip, 4);
177 /* 025-026: server detected user port */
178 bytes += read_packet_w(data, &cursor, len, &lrop.client_port);
179 /* 027-030: server detected itself ip 127.0.0.1 ? */
180 bytes += read_packet_data(data, &cursor, len, (guint8 *) &lrop.server_ip, 4);
181 /* 031-032: server listening port */
182 bytes += read_packet_w(data, &cursor, len, &lrop.server_port);
183 /* 033-036: login time for current session */
184 bytes += read_packet_dw(data, &cursor, len, (guint32 *) &lrop.login_time);
185 /* 037-062: 26 bytes, unknown */
186 bytes += read_packet_data(data, &cursor, len, (guint8 *) &lrop.unknown1, 26);
187 /* 063-066: unknown server1 ip address */
188 bytes += read_packet_data(data, &cursor, len, (guint8 *) &lrop.unknown_server1_ip, 4);
189 /* 067-068: unknown server1 port */
190 bytes += read_packet_w(data, &cursor, len, &lrop.unknown_server1_port);
191 /* 069-072: unknown server2 ip address */
192 bytes += read_packet_data(data, &cursor, len, (guint8 *) &lrop.unknown_server2_ip, 4);
193 /* 073-074: unknown server2 port */
194 bytes += read_packet_w(data, &cursor, len, &lrop.unknown_server2_port);
195 /* 075-076: 2 bytes unknown */
196 bytes += read_packet_w(data, &cursor, len, &lrop.unknown2);
197 /* 077-078: 2 bytes unknown */
198 bytes += read_packet_w(data, &cursor, len, &lrop.unknown3);
199 /* 079-110: 32 bytes unknown */
200 bytes += read_packet_data(data, &cursor, len, (guint8 *) &lrop.unknown4, 32);
201 /* 111-122: 12 bytes unknown */
202 bytes += read_packet_data(data, &cursor, len, (guint8 *) &lrop.unknown5, 12);
203 /* 123-126: login IP of last session */
204 bytes += read_packet_data(data, &cursor, len, (guint8 *) &lrop.last_client_ip, 4);
205 /* 127-130: login time of last session */
206 bytes += read_packet_dw(data, &cursor, len, (guint32 *) &lrop.last_login_time);
207 /* 131-138: 8 bytes unknown */
208 bytes += read_packet_data(data, &cursor, len, (guint8 *) &lrop.unknown6, 8);
209
210 if (bytes != QQ_LOGIN_REPLY_OK_PACKET_LEN) { /* fail parsing login info */
211 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
212 "Fail parsing login info, expect %d bytes, read %d bytes\n",
213 QQ_LOGIN_REPLY_OK_PACKET_LEN, bytes);
214 } /* but we still go on as login OK */
215
216 qd->session_key = lrop.session_key;
217 qd->session_md5 = _gen_session_md5(qd->uid, qd->session_key);
218 qd->my_ip = gen_ip_str(lrop.client_ip);
219 qd->my_port = lrop.client_port;
220 qd->login_time = lrop.login_time;
221 qd->last_login_time = lrop.last_login_time;
222 qd->last_login_ip = gen_ip_str(lrop.last_client_ip);
223
224 gaim_connection_set_state(gc, GAIM_CONNECTED);
225 qd->logged_in = TRUE; /* must be defined after sev_finish_login */
226
227 /* now initiate QQ Qun, do it first as it may take longer to finish */
228 qq_group_init(gc);
229
230 /* Now goes on updating my icon/nickname, not showing info_window */
231 qd->modifying_face = FALSE;
232 qq_send_packet_get_info(gc, qd->uid, FALSE);
233 /* grab my level */
234 qq_send_packet_get_level(gc, qd->uid);
235
236 qq_send_packet_change_status(gc);
237
238 /* refresh buddies */
239 qq_send_packet_get_buddies_list(gc, QQ_FRIENDS_LIST_POSITION_START);
240 /* refresh groups */
241 qq_send_packet_get_all_list_with_group(gc, QQ_FRIENDS_LIST_POSITION_START);
242
243 return QQ_LOGIN_REPLY_OK;
244 }
245
246 /* process login reply packet which includes redirected new server address */
247 static gint _qq_process_login_redirect(GaimConnection *gc, guint8 *data, gint len)
248 {
249 gint bytes, ret;
250 guint8 *cursor;
251 gchar *new_server_str;
252 qq_data *qd;
253 qq_login_reply_redirect_packet lrrp;
254
255 qd = (qq_data *) gc->proto_data;
256 cursor = data;
257 bytes = 0;
258 /* 000-000: reply code */
259 bytes += read_packet_b(data, &cursor, len, &lrrp.result);
260 /* 001-004: login uid */
261 bytes += read_packet_dw(data, &cursor, len, &lrrp.uid);
262 /* 005-008: redirected new server IP */
263 bytes += read_packet_data(data, &cursor, len, lrrp.new_server_ip, 4);
264 /* 009-010: redirected new server port */
265 bytes += read_packet_w(data, &cursor, len, &lrrp.new_server_port);
266
267 if (bytes != QQ_LOGIN_REPLY_REDIRECT_PACKET_LEN) {
268 gaim_debug(GAIM_DEBUG_ERROR, "QQ",
269 "Fail parsing login redirect packet, expect %d bytes, read %d bytes\n",
270 QQ_LOGIN_REPLY_REDIRECT_PACKET_LEN, bytes);
271 ret = QQ_LOGIN_REPLY_MISC_ERROR;
272 } else { /* start new connection */
273 new_server_str = gen_ip_str(lrrp.new_server_ip);
274 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
275 "Redirected to new server: %s:%d\n", new_server_str, lrrp.new_server_port);
276 qq_connect(gc->account, new_server_str, lrrp.new_server_port, qd->use_tcp, TRUE);
277 g_free(new_server_str);
278 ret = QQ_LOGIN_REPLY_REDIRECT;
279 }
280
281 return ret;
282 }
283
284 /* process login reply which says wrong password */
285 static gint _qq_process_login_wrong_pwd(GaimConnection *gc, guint8 *data, gint len)
286 {
287 gchar *server_reply, *server_reply_utf8;
288 server_reply = g_new0(gchar, len);
289 g_memmove(server_reply, data + 1, len - 1);
290 server_reply_utf8 = qq_to_utf8(server_reply, QQ_CHARSET_DEFAULT);
291 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Wrong password, server msg in UTF8: %s\n", server_reply_utf8);
292 g_free(server_reply);
293 g_free(server_reply_utf8);
294
295 return QQ_LOGIN_REPLY_PWD_ERROR;
296 }
297
298 /* request before login */
299 void qq_send_packet_request_login_token(GaimConnection *gc)
300 {
301 qq_data *qd;
302 guint8 *buf, *cursor;
303 guint16 seq_ret;
304 gint bytes;
305
306 qd = (qq_data *) gc->proto_data;
307 buf = g_newa(guint8, MAX_PACKET_SIZE);
308
309 cursor = buf;
310 bytes = 0;
311 bytes += _create_packet_head_seq(buf, &cursor, gc, QQ_CMD_REQUEST_LOGIN_TOKEN, TRUE, &seq_ret);
312 bytes += create_packet_dw(buf, &cursor, qd->uid);
313 bytes += create_packet_b(buf, &cursor, 0);
314 bytes += create_packet_b(buf, &cursor, QQ_PACKET_TAIL);
315
316 if (bytes == (cursor - buf)) /* packet creation OK */
317 _qq_send_packet(gc, buf, bytes, QQ_CMD_REQUEST_LOGIN_TOKEN);
318 else
319 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Fail create request login token packet\n");
320 }
321
322 /* send login packet to QQ server */
323 static void qq_send_packet_login(GaimConnection *gc, guint8 token_length, guint8 *token)
324 {
325 qq_data *qd;
326 guint8 *buf, *cursor, *raw_data, *encrypted_data;
327 guint16 seq_ret;
328 gint encrypted_len, bytes;
329 gint pos;
330
331 qd = (qq_data *) gc->proto_data;
332 buf = g_newa(guint8, MAX_PACKET_SIZE);
333 raw_data = g_newa(guint8, QQ_LOGIN_DATA_LENGTH);
334 encrypted_data = g_newa(guint8, QQ_LOGIN_DATA_LENGTH + 16); /* 16 bytes more */
335 qd->inikey = _gen_login_key();
336
337 /* now generate the encrypted data
338 * 000-015 use pwkey as key to encrypt empty string */
339 qq_crypt(ENCRYPT, (guint8 *) "", 0, qd->pwkey, raw_data, &encrypted_len);
340 /* 016-016 */
341 raw_data[16] = 0x00;
342 /* 017-020, used to be IP, now zero */
343 *((guint32 *) (raw_data + 17)) = 0x00000000;
344 /* 021-022, used to be port, now zero */
345 *((guint16 *) (raw_data + 21)) = 0x0000;
346 /* 023-051, fixed value, unknown */
347 g_memmove(raw_data + 23, login_23_51, 29);
348 /* 052-052, login mode */
349 raw_data[52] = qd->login_mode;
350 /* 053-068, fixed value, maybe related to per machine */
351 g_memmove(raw_data + 53, login_53_68, 16);
352
353 /* 069, login token length */
354 raw_data[69] = token_length;
355 pos = 70;
356 /* 070-093, login token, normally 24 bytes */
357 g_memmove(raw_data + pos, token, token_length);
358 pos += token_length;
359 /* 100 bytes unknown */
360 g_memmove(raw_data + pos, login_100_bytes, 100);
361 pos += 100;
362 /* all zero left */
363 memset(raw_data+pos, 0, QQ_LOGIN_DATA_LENGTH - pos);
364
365 qq_crypt(ENCRYPT, raw_data, QQ_LOGIN_DATA_LENGTH, qd->inikey, encrypted_data, &encrypted_len);
366
367 cursor = buf;
368 bytes = 0;
369 bytes += _create_packet_head_seq(buf, &cursor, gc, QQ_CMD_LOGIN, TRUE, &seq_ret);
370 bytes += create_packet_dw(buf, &cursor, qd->uid);
371 bytes += create_packet_data(buf, &cursor, qd->inikey, QQ_KEY_LENGTH);
372 bytes += create_packet_data(buf, &cursor, encrypted_data, encrypted_len);
373 bytes += create_packet_b(buf, &cursor, QQ_PACKET_TAIL);
374
375 if (bytes == (cursor - buf)) /* packet creation OK */
376 _qq_send_packet(gc, buf, bytes, QQ_CMD_LOGIN);
377 else
378 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Fail create login packet\n");
379 }
380
381 void qq_process_request_login_token_reply(guint8 *buf, gint buf_len, GaimConnection *gc)
382 {
383 qq_data *qd;
384 gchar *hex_dump;
385
386 g_return_if_fail(buf != NULL && buf_len != 0);
387
388 qd = (qq_data *) gc->proto_data;
389
390 if (buf[0] == QQ_REQUEST_LOGIN_TOKEN_REPLY_OK) {
391 if (buf[1] != buf_len-2) {
392 gaim_debug(GAIM_DEBUG_INFO, "QQ",
393 "Malformed login token reply packet. Packet specifies length of %d, actual length is %d\n", buf[1], buf_len-2);
394 gaim_debug(GAIM_DEBUG_INFO, "QQ",
395 "Attempting to proceed with the actual packet length.\n");
396 }
397 hex_dump = hex_dump_to_str(buf+2, buf_len-2);
398 gaim_debug(GAIM_DEBUG_INFO, "QQ",
399 "<<< got a token with %d bytes -> [default] decrypt and dump\n%s", buf_len-2, hex_dump);
400 qq_send_packet_login(gc, buf_len-2, buf+2);
401 } else {
402 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Unknown request login token reply code : %d\n", buf[0]);
403 hex_dump = hex_dump_to_str(buf, buf_len);
404 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
405 ">>> %d bytes -> [default] decrypt and dump\n%s",
406 buf_len, hex_dump);
407 try_dump_as_gbk(buf, buf_len);
408 gaim_connection_error(gc, _("Request login token error!"));
409 }
410 g_free(hex_dump);
411 }
412
413 /* send logout packets to QQ server */
414 void qq_send_packet_logout(GaimConnection *gc)
415 {
416 gint i;
417 qq_data *qd;
418
419 qd = (qq_data *) gc->proto_data;
420 for (i = 0; i < 4; i++)
421 qq_send_cmd(gc, QQ_CMD_LOGOUT, FALSE, 0xffff, FALSE, qd->pwkey, QQ_KEY_LENGTH);
422
423 qd->logged_in = FALSE; /* update login status AFTER sending logout packets */
424 }
425
426 /* process the login reply packet */
427 void qq_process_login_reply(guint8 *buf, gint buf_len, GaimConnection *gc)
428 {
429 gint len, ret, bytes;
430 guint8 *data;
431 qq_data *qd;
432 gchar *hex_dump;
433
434 g_return_if_fail(buf != NULL && buf_len != 0);
435
436 qd = (qq_data *) gc->proto_data;
437 len = buf_len;
438 data = g_newa(guint8, len);
439
440 if (qq_crypt(DECRYPT, buf, buf_len, qd->pwkey, data, &len)) {
441 /* should be able to decrypt with pwkey */
442 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Decrypt login reply packet with pwkey, %d bytes\n", len);
443 if (data[0] == QQ_LOGIN_REPLY_OK) {
444 ret = _qq_process_login_ok(gc, data, len);
445 } else {
446 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Unknown login reply code : %d\n", data[0]);
447 ret = QQ_LOGIN_REPLY_MISC_ERROR;
448 }
449 } else { /* decrypt with pwkey error */
450 len = buf_len; /* reset len, decrypt will fail if len is too short */
451 if (qq_crypt(DECRYPT, buf, buf_len, qd->inikey, data, &len)) {
452 /* decrypt ok with inipwd, it might be password error */
453 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
454 "Decrypt login reply packet with inikey, %d bytes\n", len);
455 bytes = 0;
456 switch (data[0]) {
457 case QQ_LOGIN_REPLY_REDIRECT:
458 ret = _qq_process_login_redirect(gc, data, len);
459 break;
460 case QQ_LOGIN_REPLY_PWD_ERROR:
461 ret = _qq_process_login_wrong_pwd(gc, data, len);
462 break;
463 default:
464 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Unknown reply code: %d\n", data[0]);
465 hex_dump = hex_dump_to_str(data, len);
466 gaim_debug(GAIM_DEBUG_WARNING, "QQ",
467 ">>> %d bytes -> [default] decrypt and dump\n%s",
468 buf_len, hex_dump);
469 g_free(hex_dump);
470 try_dump_as_gbk(data, len);
471
472 ret = QQ_LOGIN_REPLY_MISC_ERROR;
473 }
474 } else { /* no idea how to decrypt */
475 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "No idea how to decrypt login reply\n");
476 ret = QQ_LOGIN_REPLY_MISC_ERROR;
477 }
478 }
479
480 switch (ret) {
481 case QQ_LOGIN_REPLY_PWD_ERROR:
482 gc->wants_to_die = TRUE;
483 gaim_connection_error(gc, _("Incorrect password."));
484 break;
485 case QQ_LOGIN_REPLY_MISC_ERROR:
486 gaim_connection_error(gc, _("Unable to login, check debug log"));
487 break;
488 case QQ_LOGIN_REPLY_OK:
489 gaim_debug(GAIM_DEBUG_INFO, "QQ", "Login replys OK, everything is fine\n");
490 break;
491 case QQ_LOGIN_REPLY_REDIRECT:
492 /* the redirect has been done in _qq_process_login_reply */
493 break;
494 default:{;
495 }
496 }
497 }