3 * Module purpose is to handle incoming and outgoing requests with client.
4 * Licensed under GNU GPL.
5 * For more information, see LICENCE in the main folder.
6 * @author Athena Dev Teams originally in login.c
7 * @author rAthena Dev Team
10 #include "../common/timer.h" //difftick
11 #include "../common/strlib.h" //safeprint
12 #include "../common/showmsg.h" //show notice
13 #include "../common/socket.h" //wfifo session
14 #include "../common/malloc.h"
15 #include "../common/utils.h"
16 #include "../common/md5calc.h"
17 #include "../common/random.h"
19 #include "ipban.h" //ipban_check
22 #include "loginclif.h"
23 #include "loginchrif.h"
28 * Transmit auth result to client.
29 * @param fd: client file desciptor link
30 * @param result: result to transmit to client, see below
32 * 2 : Someone has already logged in with this id
34 * <result>.B (SC_NOTIFY_BAN)
36 static void logclif_sent_auth_result(int fd,char result){
39 WFIFOB(fd,2) = result;
44 * Auth successful, inform client and create a temp auth_node.
45 * @param sd: player session
47 static void logclif_auth_ok(struct login_session_data* sd) {
49 uint32 ip = session[fd]->client_addr;
52 uint32 subnet_char_ip;
53 struct auth_node* node;
56 #if PACKETVER < 20170315
57 int cmd = 0x69; // AC_ACCEPT_LOGIN
61 int cmd = 0xac4; // AC_ACCEPT_LOGIN3
66 if( runflag != LOGINSERVER_ST_RUNNING ){
67 // players can only login while running
68 logclif_sent_auth_result(fd,1); // server closed
72 if( login_config.group_id_to_connect >= 0 && sd->group_id != login_config.group_id_to_connect ) {
73 ShowStatus("Connection refused: the required group id for connection is %d (account: %s, group: %d).\n", login_config.group_id_to_connect, sd->userid, sd->group_id);
74 logclif_sent_auth_result(fd,1); // server closed
76 } else if( login_config.min_group_id_to_connect >= 0 && login_config.group_id_to_connect == -1 && sd->group_id < login_config.min_group_id_to_connect ) {
77 ShowStatus("Connection refused: the minimum group id required for connection is %d (account: %s, group: %d).\n", login_config.min_group_id_to_connect, sd->userid, sd->group_id);
78 logclif_sent_auth_result(fd,1); // server closed
83 for( i = 0; i < ARRAYLENGTH(ch_server); ++i )
84 if( session_isActive(ch_server[i].fd) )
88 {// if no char-server, don't send void list of servers, just disconnect the player with proper message
89 ShowStatus("Connection refused: there is no char-server online (account: %s).\n", sd->userid);
90 logclif_sent_auth_result(fd,1); // server closed
95 struct online_login_data* data = (struct online_login_data*)idb_get(online_db, sd->account_id);
97 {// account is already marked as online!
98 if( data->char_server > -1 )
99 {// Request char servers to kick this account out. [Skotlex]
101 ShowNotice("User '%s' is already online - Rejected.\n", sd->userid);
102 WBUFW(buf,0) = 0x2734;
103 WBUFL(buf,2) = sd->account_id;
104 logchrif_sendallwos(-1, buf, 6);
105 if( data->waiting_disconnect == INVALID_TIMER )
106 data->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, login_waiting_disconnect_timer, sd->account_id, 0);
107 logclif_sent_auth_result(fd,8); // 08 = Server still recognizes your last login
111 if( data->char_server == -1 )
112 {// client has authed but did not access char-server yet
113 // wipe previous session
114 idb_remove(auth_db, sd->account_id);
115 login_remove_online_user(sd->account_id);
121 login_log(ip, sd->userid, 100, "login ok");
122 ShowStatus("Connection of the account '%s' accepted.\n", sd->userid);
124 WFIFOHEAD(fd,header+size*server_num);
126 WFIFOW(fd,2) = header+size*server_num;
127 WFIFOL(fd,4) = sd->login_id1;
128 WFIFOL(fd,8) = sd->account_id;
129 WFIFOL(fd,12) = sd->login_id2;
130 WFIFOL(fd,16) = 0; // in old version, that was for ip (not more used)
131 //memcpy(WFIFOP(fd,20), sd->lastlogin, 24); // in old version, that was for name (not more used)
132 memset(WFIFOP(fd,20), 0, 24);
133 WFIFOW(fd,44) = 0; // unknown
134 WFIFOB(fd,46) = sex_str2num(sd->sex);
135 #if PACKETVER >= 20170315
136 memset(WFIFOP(fd,47),0,17); // Unknown
138 for( i = 0, n = 0; i < ARRAYLENGTH(ch_server); ++i ) {
139 if( !session_isValid(ch_server[i].fd) )
141 subnet_char_ip = lan_subnetcheck(ip); // Advanced subnet check [LuzZza]
142 WFIFOL(fd,header+n*size) = htonl((subnet_char_ip) ? subnet_char_ip : ch_server[i].ip);
143 WFIFOW(fd,header+n*size+4) = ntows(htons(ch_server[i].port)); // [!] LE byte order here [!]
144 memcpy(WFIFOP(fd,header+n*size+6), ch_server[i].name, 20);
145 WFIFOW(fd,header+n*size+26) = ch_server[i].users;
146 WFIFOW(fd,header+n*size+28) = ch_server[i].type;
147 WFIFOW(fd,header+n*size+30) = ch_server[i].new_;
148 #if PACKETVER >= 20170315
149 memset(WFIFOP(fd, header+n*size+32), 0, 128); // Unknown
153 WFIFOSET(fd,header+size*server_num);
155 // create temporary auth entry
156 CREATE(node, struct auth_node, 1);
157 node->account_id = sd->account_id;
158 node->login_id1 = sd->login_id1;
159 node->login_id2 = sd->login_id2;
162 node->clienttype = sd->clienttype;
163 idb_put(auth_db, sd->account_id, node);
165 struct online_login_data* data;
166 // mark client as 'online'
167 data = login_add_online_user(-1, sd->account_id);
168 // schedule deletion of this node
169 data->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, login_waiting_disconnect_timer, sd->account_id, 0);
174 * Inform client that auth has failed.
175 * @param sd: player session
176 * @param result: nb (msg define in conf)
178 1 = Incorrect Password
179 2 = This ID is expired
180 3 = Rejected from Server
181 4 = You have been blocked by the GM Team
182 5 = Your Game's EXE file is not the latest version
183 6 = You are prohibited to log in until %s
184 7 = Server is jammed due to over populated
185 8 = No more accounts may be connected from this company
186 9 = MSI_REFUSE_BAN_BY_DBA
187 10 = MSI_REFUSE_EMAIL_NOT_CONFIRMED
188 11 = MSI_REFUSE_BAN_BY_GM
189 12 = MSI_REFUSE_TEMP_BAN_FOR_DBWORK
190 13 = MSI_REFUSE_SELF_LOCK
191 14 = MSI_REFUSE_NOT_PERMITTED_GROUP
192 15 = MSI_REFUSE_NOT_PERMITTED_GROUP
193 99 = This ID has been totally erased
194 100 = Login information remains at %s
195 101 = Account has been locked for a hacking investigation. Please contact the GM Team for more information
196 102 = This account has been temporarily prohibited from login due to a bug-related investigation
197 103 = This character is being deleted. Login is temporarily unavailable for the time being
198 104 = This character is being deleted. Login is temporarily unavailable for the time being
199 default = Unknown Error.
201 static void logclif_auth_failed(struct login_session_data* sd, int result) {
203 uint32 ip = session[fd]->client_addr;
205 if (login_config.log_login)
207 if(result >= 0 && result <= 15)
208 login_log(ip, sd->userid, result, msg_txt(result));
209 else if(result >= 99 && result <= 104)
210 login_log(ip, sd->userid, result, msg_txt(result-83)); //-83 offset
212 login_log(ip, sd->userid, result, msg_txt(22)); //unknow error
215 if( (result == 0 || result == 1) && login_config.dynamic_pass_failure_ban )
216 ipban_log(ip); // log failed password attempt
218 #if PACKETVER >= 20120000 /* not sure when this started */
220 WFIFOW(fd,0) = 0x83e;
221 WFIFOL(fd,2) = result;
223 memset(WFIFOP(fd,6), '\0', 20);
224 else { // 6 = You are prohibited to log in until %s
225 struct mmo_account acc;
226 AccountDB* accounts = login_get_accounts_db();
227 time_t unban_time = ( accounts->load_str(accounts, &acc, sd->userid) ) ? acc.unban_time : 0;
228 timestamp2string(WFIFOCP(fd,6), 20, unban_time, login_config.date_format);
234 WFIFOB(fd,2) = (uint8)result;
236 memset(WFIFOP(fd,3), '\0', 20);
237 else { // 6 = You are prohibited to log in until %s
238 struct mmo_account acc;
239 AccountDB* accounts = login_get_accounts_db();
240 time_t unban_time = ( accounts->load_str(accounts, &acc, sd->userid) ) ? acc.unban_time : 0;
241 timestamp2string(WFIFOCP(fd,3), 20, unban_time, login_config.date_format);
248 * Received a keepalive packet to maintain connection.
249 * 0x200 <account.userid>.24B.
250 * @param fd: fd to parse from (client fd)
251 * @return 0 not enough info transmitted, 1 success
253 static int logclif_parse_keepalive(int fd){
254 if (RFIFOREST(fd) < 26)
261 * Received a keepalive packet to maintain connection.
262 * S 0204 <md5 hash>.16B (kRO 2004-05-31aSakexe langtype 0 and 6)
263 * @param fd: fd to parse from (client fd)
264 * @return 0 not enough info transmitted, 1 success
266 static int logclif_parse_updclhash(int fd, struct login_session_data *sd){
267 if (RFIFOREST(fd) < 18)
269 sd->has_client_hash = 1;
270 memcpy(sd->client_hash, RFIFOP(fd, 2), 16);
276 * Received a connection request.
277 * @param fd: file descriptor to parse from (client)
278 * @param sd: client session
279 * @param command: packet type sent
280 * @param ip: ipv4 address (client)
281 * S 0064 <version>.L <username>.24B <password>.24B <clienttype>.B
282 * S 0277 <version>.L <username>.24B <password>.24B <clienttype>.B <ip address>.16B <adapter address>.13B
283 * S 02b0 <version>.L <username>.24B <password>.24B <clienttype>.B <ip address>.16B <adapter address>.13B <g_isGravityID>.B
284 * S 01dd <version>.L <username>.24B <password hash>.16B <clienttype>.B
285 * S 01fa <version>.L <username>.24B <password hash>.16B <clienttype>.B <?>.B(index of the connection in the clientinfo file (+10 if the command-line contains "pc"))
286 * S 027c <version>.L <username>.24B <password hash>.16B <clienttype>.B <?>.13B(junk)
287 * S 0825 <packetsize>.W <version>.L <clienttype>.B <userid>.24B <password>.27B <mac>.17B <ip>.15B <token>.(packetsize - 0x5C)B
288 * @param fd: fd to parse from (client fd)
289 * @return 0 failure, 1 success
291 static int logclif_parse_reqauth(int fd, struct login_session_data *sd, int command, char* ip){
292 size_t packet_len = RFIFOREST(fd);
294 if( (command == 0x0064 && packet_len < 55)
295 || (command == 0x0277 && packet_len < 84)
296 || (command == 0x02b0 && packet_len < 85)
297 || (command == 0x01dd && packet_len < 47)
298 || (command == 0x01fa && packet_len < 48)
299 || (command == 0x027c && packet_len < 60)
300 || (command == 0x0825 && (packet_len < 4 || packet_len < RFIFOW(fd, 2))) )
304 char username[NAME_LENGTH];
305 char password[PASSWD_LENGTH];
306 unsigned char passhash[16];
308 bool israwpass = (command==0x0064 || command==0x0277 || command==0x02b0 || command == 0x0825);
310 // Shinryo: For the time being, just use token as password.
311 if(command == 0x0825) {
312 char *accname = RFIFOCP(fd, 9);
313 char *token = RFIFOCP(fd, 0x5C);
314 size_t uAccLen = strlen(accname);
315 size_t uTokenLen = RFIFOREST(fd) - 0x5C;
317 if(uAccLen > NAME_LENGTH - 1 || uAccLen == 0 || uTokenLen > NAME_LENGTH - 1 || uTokenLen == 0)
319 logclif_auth_failed(sd, 3);
323 safestrncpy(username, accname, uAccLen + 1);
324 safestrncpy(password, token, uTokenLen + 1);
325 clienttype = RFIFOB(fd, 8);
329 safestrncpy(username, RFIFOCP(fd,6), NAME_LENGTH);
332 safestrncpy(password, RFIFOCP(fd,30), PASSWD_LENGTH);
333 clienttype = RFIFOB(fd,54);
337 memcpy(passhash, RFIFOP(fd,30), 16);
338 clienttype = RFIFOB(fd,46);
341 RFIFOSKIP(fd,RFIFOREST(fd)); // assume no other packet was sent
343 sd->clienttype = clienttype;
344 safestrncpy(sd->userid, username, NAME_LENGTH);
347 ShowStatus("Request for connection of %s (ip: %s)\n", sd->userid, ip);
348 safestrncpy(sd->passwd, password, NAME_LENGTH);
349 if( login_config.use_md5_passwds )
350 MD5_String(sd->passwd, sd->passwd);
355 ShowStatus("Request for connection (passwdenc mode) of %s (ip: %s)\n", sd->userid, ip);
356 bin2hex(sd->passwd, passhash, 16); // raw binary data here!
357 sd->passwdenc = PASSWORDENC;
360 if( sd->passwdenc != 0 && login_config.use_md5_passwds )
362 logclif_auth_failed(sd, 3); // send "rejected from server"
366 result = login_mmo_auth(sd, false);
371 logclif_auth_failed(sd, result);
377 * Client requests an md5key for his session: keys will be generated and sent back.
378 * @param fd: file descriptor to parse from (client)
379 * @param sd: client session
382 static int logclif_parse_reqkey(int fd, struct login_session_data *sd){
385 memset(sd->md5key, '\0', sizeof(sd->md5key));
386 sd->md5keylen = (uint16)(12 + rnd() % 4);
387 MD5_Salt(sd->md5keylen, sd->md5key);
389 WFIFOHEAD(fd,4 + sd->md5keylen);
390 WFIFOW(fd,0) = 0x01dc;
391 WFIFOW(fd,2) = 4 + sd->md5keylen;
392 memcpy(WFIFOP(fd,4), sd->md5key, sd->md5keylen);
393 WFIFOSET(fd,WFIFOW(fd,2));
399 * Char-server request to connect to the login-server.
400 * This is needed to exchange packets.
401 * @param fd: file descriptor to parse from (client)
402 * @param sd: client session
403 * @param ip: ipv4 address (client)
404 * @return 0 packet received too shirt, 1 success
406 static int logclif_parse_reqcharconnec(int fd, struct login_session_data *sd, char* ip){
407 if (RFIFOREST(fd) < 86)
411 char server_name[20];
418 safestrncpy(sd->userid, RFIFOCP(fd,2), NAME_LENGTH);
419 safestrncpy(sd->passwd, RFIFOCP(fd,26), NAME_LENGTH);
420 if( login_config.use_md5_passwds )
421 MD5_String(sd->passwd, sd->passwd);
423 server_ip = ntohl(RFIFOL(fd,54));
424 server_port = ntohs(RFIFOW(fd,58));
425 safestrncpy(server_name, RFIFOCP(fd,60), 20);
426 type = RFIFOW(fd,82);
427 new_ = RFIFOW(fd,84);
430 ShowInfo("Connection request of the char-server '%s' @ %u.%u.%u.%u:%u (account: '%s', ip: '%s')\n", server_name, CONVIP(server_ip), server_port, sd->userid, ip);
431 sprintf(message, "charserver - %s@%u.%u.%u.%u:%u", server_name, CONVIP(server_ip), server_port);
432 login_log(session[fd]->client_addr, sd->userid, 100, message);
434 result = login_mmo_auth(sd, true);
435 if( runflag == LOGINSERVER_ST_RUNNING &&
438 sd->account_id < ARRAYLENGTH(ch_server) &&
439 !session_isValid(ch_server[sd->account_id].fd) )
441 ShowStatus("Connection of the char-server '%s' accepted.\n", server_name);
442 safestrncpy(ch_server[sd->account_id].name, server_name, sizeof(ch_server[sd->account_id].name));
443 ch_server[sd->account_id].fd = fd;
444 ch_server[sd->account_id].ip = server_ip;
445 ch_server[sd->account_id].port = server_port;
446 ch_server[sd->account_id].users = 0;
447 ch_server[sd->account_id].type = type;
448 ch_server[sd->account_id].new_ = new_;
450 session[fd]->func_parse = logchrif_parse;
451 session[fd]->flag.server = 1;
452 realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
454 // send connection success
456 WFIFOW(fd,0) = 0x2711;
462 ShowNotice("Connection of the char-server '%s' REFUSED.\n", server_name);
464 WFIFOW(fd,0) = 0x2711;
473 * Entry point from client to log-server.
474 * Function that checks incoming command, then splits it to the correct handler.
475 * @param fd: file descriptor to parse, (link to client)
476 * @return 0=invalid session,marked for disconnection,unknow packet, banned..; 1=success
478 int logclif_parse(int fd) {
479 struct login_session_data* sd = (struct login_session_data*)session[fd]->session_data;
482 uint32 ipl = session[fd]->client_addr;
485 if( session[fd]->flag.eof )
487 ShowInfo("Closed connection from '"CL_WHITE"%s"CL_RESET"'.\n", ip);
494 // Perform ip-ban check
495 if( login_config.ipban && ipban_check(ipl) )
497 ShowStatus("Connection refused: IP isn't authorised (deny/allow, ip: %s).\n", ip);
498 login_log(ipl, "unknown", -3, "ip banned");
501 WFIFOB(fd,2) = 3; // 3 = Rejected from Server
506 // create a session for this new connection
507 CREATE(session[fd]->session_data, struct login_session_data, 1);
508 sd = (struct login_session_data*)session[fd]->session_data;
512 while( RFIFOREST(fd) >= 2 )
514 uint16 command = RFIFOW(fd,0);
519 // New alive packet: used to verify if client is always alive.
520 case 0x0200: next = logclif_parse_keepalive(fd); break;
521 // client md5 hash (binary)
522 case 0x0204: next = logclif_parse_updclhash(fd,sd); break;
523 // request client login (raw password)
524 case 0x0064: // S 0064 <version>.L <username>.24B <password>.24B <clienttype>.B
525 case 0x0277: // S 0277 <version>.L <username>.24B <password>.24B <clienttype>.B <ip address>.16B <adapter address>.13B
526 case 0x02b0: // S 02b0 <version>.L <username>.24B <password>.24B <clienttype>.B <ip address>.16B <adapter address>.13B <g_isGravityID>.B
527 // request client login (md5-hashed password)
528 case 0x01dd: // S 01dd <version>.L <username>.24B <password hash>.16B <clienttype>.B
529 case 0x01fa: // S 01fa <version>.L <username>.24B <password hash>.16B <clienttype>.B <?>.B(index of the connection in the clientinfo file (+10 if the command-line contains "pc"))
530 case 0x027c: // S 027c <version>.L <username>.24B <password hash>.16B <clienttype>.B <?>.13B(junk)
531 case 0x0825: // S 0825 <packetsize>.W <version>.L <clienttype>.B <userid>.24B <password>.27B <mac>.17B <ip>.15B <token>.(packetsize - 0x5C)B
532 next = logclif_parse_reqauth(fd, sd, command, ip);
534 // Sending request of the coding key
535 case 0x01db: next = logclif_parse_reqkey(fd, sd); break;
536 // Connection request of a char-server
537 case 0x2710: logclif_parse_reqcharconnec(fd,sd, ip); return 0; // processing will continue elsewhere
539 ShowNotice("Abnormal end of connection (ip: %s): Unknown packet 0x%x\n", ip, command);
543 if(next==0) return 0; // avoid processing of followup packets (prev was probably incomplete)
551 /// Constructor destructor
554 * Initialize the module.
555 * Launched at login-serv start, create db or other long scope variable here.
557 void do_init_loginclif(void){
562 * loginclif destructor
563 * dealloc..., function called at exit of the login-serv
565 void do_final_loginclif(void){