Debian Codegraph @

src/bnetd/handle_bnet.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1998 Mark Baysinger ([email protected])
3 * Copyright (C) 1998,1999,2000,2001 Ross Combs ([email protected])
4 * Copyright (C) 1999,2000 Rob Crittenden ([email protected])
5 * Copyright (C) 2000,2001 Marco Ziech ([email protected])
6 * Copyright (C) 2003 Dizzy
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 #include "common/setup_before.h"
24 #include "handle_bnet.h"
25
26 #include <cctype>
27 #include <cerrno>
28 #include <cinttypes>
29 #include <cstdint>
30 #include <cstring>
31 #include <fstream>
32 #include <limits>
33 #include <sstream>
34 #include <tuple>
35
36 #include "compat/strcasecmp.h"
37 #include "compat/strncasecmp.h"
38 #include "common/packet.h"
39 #include "common/eventlog.h"
40 #include "common/tag.h"
41 #include "common/bn_type.h"
42 #include "common/addr.h"
43 #include "common/bnettime.h"
44 #include "common/trans.h"
45 #include "common/list.h"
46 #include "common/bnethash.h"
47 #include "common/bnethashconv.h"
48 #include "common/proginfo.h"
49 #include "common/util.h"
50 #include "common/bnetsrp3.h"
51 #include "common/xstring.h"
52
53 #include "handlers.h"
54 #include "connection.h"
55 #include "prefs.h"
56 #include "versioncheck.h"
57 #include "handle_anongame.h"
58 #include "account.h"
59 #include "account_wrap.h"
60 #include "clan.h"
61 #include "channel.h"
62 #include "game.h"
63 #include "game_conv.h"
64 #include "ladder.h"
65 #include "watch.h"
66 #include "realm.h"
67 #include "adbanner.h"
68 #include "character.h"
69 #include "command.h"
70 #include "tick.h"
71 #include "message.h"
72 #include "file.h"
73 #include "news.h"
74 #include "team.h"
75 #include "server.h"
76 #include "friends.h"
77 #include "autoupdate.h"
78 #include "anongame.h"
79 #include "i18n.h"
80 #ifdef WIN32_GUI
81 #include <win32/winmain.h>
82 #endif
83 #include "common/setup_after.h"
84 #ifdef WITH_LUA
85 #include "luainterface.h"
86 #endif
87 namespace pvpgn
88 {
89
90 namespace bnetd
91 {
92
93 extern int last_news;
94 extern int first_news;
95
96 /* handlers prototypes */
97 static int _client_unknown_1b(t_connection * c, t_packet const *const packet);
98 static int _client_compinfo1(t_connection * c, t_packet const *const packet);
99 static int _client_compinfo2(t_connection * c, t_packet const *const packet);
100 static int _client_countryinfo1(t_connection * c, t_packet const *const packet);
101 static int _client_auth_info(t_connection * c, t_packet const *const packet);
102 static int _client_unknown2b(t_connection * c, t_packet const *const packet);
103 static int _client_progident(t_connection * c, t_packet const *const packet);
104 static int _client_createaccountw3(t_connection * c, t_packet const *const packet);
105 static int _client_createacctreq1(t_connection * c, t_packet const *const packet);
106 static int _client_createacctreq2(t_connection * c, t_packet const *const packet);
107 static int _client_changepassreq(t_connection * c, t_packet const *const packet);
108 static int _client_echoreply(t_connection * c, t_packet const *const packet);
109 static int _client_authreq1(t_connection * c, t_packet const *const packet);
110 static int _client_authreq109(t_connection * c, t_packet const *const packet);
111 static int _client_regsnoopreply(t_connection * c, t_packet const *const packet);
112 static int _client_iconreq(t_connection * c, t_packet const *const packet);
113 static int _client_cdkey(t_connection * c, t_packet const *const packet);
114 static int _client_cdkey2(t_connection * c, t_packet const *const packet);
115 static int _client_cdkey3(t_connection * c, t_packet const *const packet);
116 static int _client_udpok(t_connection * c, t_packet const *const packet);
117 static int _client_fileinforeq(t_connection * c, t_packet const *const packet);
118 static int _client_statsreq(t_connection * c, t_packet const *const packet);
119 static int _client_loginreq1(t_connection * c, t_packet const *const packet);
120 static int _client_loginreq2(t_connection * c, t_packet const *const packet);
121 static int _client_loginreqw3(t_connection * c, t_packet const *const packet);
122 static int _client_passchangereq(t_connection * c, t_packet const *const packet);
123 static int _client_passchangeproofreq(t_connection * c, t_packet const *const packet);
124 static int _client_pingreq(t_connection * c, t_packet const *const packet);
125 static int _client_logonproofreq(t_connection * c, t_packet const *const packet);
126 static int _client_changegameport(t_connection * c, t_packet const *const packet);
127 static int _client_friendslistreq(t_connection * c, t_packet const *const packet);
128 static int _client_friendinforeq(t_connection * c, t_packet const *const packet);
129 static int _client_atfriendscreen(t_connection * c, t_packet const *const packet);
130 static int _client_atinvitefriend(t_connection * c, t_packet const *const packet);
131 static int _client_atacceptinvite(t_connection * c, t_packet const *const packet);
132 static int _client_atacceptdeclineinvite(t_connection * c, t_packet const *const packet);
133 static int _client_motdw3(t_connection * c, t_packet const *const packet);
134 static int _client_realmlistreq(t_connection * c, t_packet const *const packet);
135 static int _client_realmlistreq110(t_connection * c, t_packet const *const packet);
136 static int _client_profilereq(t_connection * c, t_packet const *const packet);
137 static int _client_realmjoinreq109(t_connection * c, t_packet const *const packet);
138 static int _client_unknown39(t_connection * c, t_packet const *const packet);
139 static int _client_charlistreq(t_connection * c, t_packet const *const packet);
140 static int _client_adreq(t_connection * c, t_packet const *const packet);
141 static int _client_adack(t_connection * c, t_packet const *const packet);
142 static int _client_adclick(t_connection * c, t_packet const *const packet);
143 static int _client_adclick2(t_connection * c, t_packet const *const packet);
144 static int _client_readmemory(t_connection * c, t_packet const *const packet);
145 static int _client_statsupdate(t_connection * c, t_packet const *const packet);
146 static int _client_playerinforeq(t_connection * c, t_packet const *const packet);
147 static int _client_progident2(t_connection * c, t_packet const *const packet);
148 static int _client_joinchannel(t_connection * c, t_packet const *const packet);
149 static int _client_message(t_connection * c, t_packet const *const packet);
150 static int _client_gamelistreq(t_connection * c, t_packet const *const packet);
151 static int _client_joingame(t_connection * c, t_packet const *const packet);
152 static int _client_startgame1(t_connection * c, t_packet const *const packet);
153 static int _client_startgame3(t_connection * c, t_packet const *const packet);
154 static int _client_startgame4(t_connection * c, t_packet const *const packet);
155 static int _client_closegame(t_connection * c, t_packet const *const packet);
156 static int _client_gamereport(t_connection * c, t_packet const *const packet);
157 static int _client_leavechannel(t_connection * c, t_packet const *const packet);
158 static int _client_ladderreq(t_connection * c, t_packet const *const packet);
159 static int _client_laddersearchreq(t_connection * c, t_packet const *const packet);
160 static int _client_mapauthreq1(t_connection * c, t_packet const *const packet);
161 static int _client_mapauthreq2(t_connection * c, t_packet const *const packet);
162 static int _client_changeclient(t_connection * c, t_packet const *const packet);
163 static int _client_clanmemberlistreq(t_connection * c, t_packet const *const packet);
164 static int _client_clan_motdreq(t_connection * c, t_packet const *const packet);
165 static int _client_clan_motdchg(t_connection * c, t_packet const *const packet);
166 static int _client_clan_createreq(t_connection * c, t_packet const *const packet);
167 static int _client_clan_createinvitereq(t_connection * c, t_packet const *const packet);
168 static int _client_clan_createinvitereply(t_connection * c, t_packet const *const packet);
169 static int _client_clan_disbandreq(t_connection * c, t_packet const *const packet);
170 static int _client_clanmember_rankupdatereq(t_connection * c, t_packet const *const packet);
171 static int _client_clanmember_removereq(t_connection * c, t_packet const *const packet);
172 static int _client_clan_membernewchiefreq(t_connection * c, t_packet const *const packet);
173 static int _client_clan_invitereq(t_connection * c, t_packet const *const packet);
174 static int _client_clan_invitereply(t_connection * c, t_packet const *const packet);
175 static int _client_crashdump(t_connection * c, t_packet const *const packet);
176 static int _client_setemailreply(t_connection * c, t_packet const *const packet);
177 static int _client_changeemailreq(t_connection * c, t_packet const *const packet);
178 static int _client_getpasswordreq(t_connection * c, t_packet const *const packet);
179 static int _client_claninforeq(t_connection * c, t_packet const *const packet);
180 static int _client_extrawork(t_connection * c, t_packet const *const packet);
181
182 /* connection state connected handler table */
183 static const t_htable_row bnet_htable_con[] = {
184 { CLIENT_UNKNOWN_1B, _client_unknown_1b },
185 { CLIENT_COMPINFO1, _client_compinfo1 },
186 { CLIENT_COMPINFO2, _client_compinfo2 },
187 { CLIENT_COUNTRYINFO1, _client_countryinfo1 },
188 { CLIENT_AUTH_INFO, _client_auth_info },
189 { CLIENT_UNKNOWN_2B, _client_unknown2b },
190 { CLIENT_PROGIDENT, _client_progident },
191 { CLIENT_CLOSEGAME, NULL },
192 { CLIENT_CREATEACCOUNT_W3, _client_createaccountw3 },
193 { CLIENT_CREATEACCTREQ1, _client_createacctreq1 },
194 { CLIENT_CREATEACCTREQ2, _client_createacctreq2 },
195 { CLIENT_CHANGEPASSREQ, _client_changepassreq },
196 { CLIENT_ECHOREPLY, _client_echoreply },
197 { CLIENT_AUTHREQ1, _client_authreq1 },
198 { CLIENT_AUTHREQ_109, _client_authreq109 },
199 { CLIENT_REGSNOOPREPLY, _client_regsnoopreply },
200 { CLIENT_ICONREQ, _client_iconreq },
201 { CLIENT_CDKEY, _client_cdkey },
202 { CLIENT_CDKEY2, _client_cdkey2 },
203 { CLIENT_CDKEY3, _client_cdkey3 },
204 { CLIENT_UDPOK, _client_udpok },
205 { CLIENT_FILEINFOREQ, _client_fileinforeq },
206 { CLIENT_STATSREQ, _client_statsreq },
207 { CLIENT_PINGREQ, _client_pingreq },
208 { CLIENT_LOGINREQ1, _client_loginreq1 },
209 { CLIENT_LOGINREQ2, _client_loginreq2 },
210 { CLIENT_LOGINREQ_W3, _client_loginreqw3 },
211 { CLIENT_PASSCHANGEREQ, _client_passchangereq },
212 { CLIENT_PASSCHANGEPROOFREQ, _client_passchangeproofreq },
213 { CLIENT_LOGONPROOFREQ, _client_logonproofreq },
214 { CLIENT_CHANGECLIENT, _client_changeclient },
215 { CLIENT_GETPASSWORDREQ, _client_getpasswordreq },
216 { CLIENT_CHANGEEMAILREQ, _client_changeemailreq },
217 { CLIENT_CRASHDUMP, _client_crashdump },
218 { -1, NULL }
219 };
220
221 /* connection state loggedin handlers */
222 static const t_htable_row bnet_htable_log[] = {
223 { CLIENT_CHANGEGAMEPORT, _client_changegameport },
224 { CLIENT_FRIENDSLISTREQ, _client_friendslistreq },
225 { CLIENT_FRIENDINFOREQ, _client_friendinforeq },
226 { CLIENT_ARRANGEDTEAM_FRIENDSCREEN, _client_atfriendscreen },
227 { CLIENT_ARRANGEDTEAM_INVITE_FRIEND, _client_atinvitefriend },
228 { CLIENT_ARRANGEDTEAM_ACCEPT_INVITE, _client_atacceptinvite },
229 { CLIENT_ARRANGEDTEAM_ACCEPT_DECLINE_INVITE, _client_atacceptdeclineinvite },
230 /* anongame packet (44ff) handled in handle_anongame.c */
231 { CLIENT_FINDANONGAME, handle_anongame_packet },
232 { CLIENT_FILEINFOREQ, _client_fileinforeq },
233 { CLIENT_MOTD_W3, _client_motdw3 },
234 { CLIENT_REALMLISTREQ, _client_realmlistreq },
235 { CLIENT_REALMLISTREQ_110, _client_realmlistreq110 },
236 { CLIENT_PROFILEREQ, _client_profilereq },
237 { CLIENT_REALMJOINREQ_109, _client_realmjoinreq109 },
238 { CLIENT_UNKNOWN_37, _client_charlistreq },
239 { CLIENT_UNKNOWN_39, _client_unknown39 },
240 { CLIENT_ECHOREPLY, _client_echoreply },
241 { CLIENT_PINGREQ, _client_pingreq },
242 { CLIENT_ADREQ, _client_adreq },
243 { CLIENT_ADACK, _client_adack },
244 { CLIENT_ADCLICK, _client_adclick },
245 { CLIENT_ADCLICK2, _client_adclick2 },
246 { CLIENT_READMEMORY, _client_readmemory },
247 { CLIENT_STATSREQ, _client_statsreq },
248 { CLIENT_STATSUPDATE, _client_statsupdate },
249 { CLIENT_PLAYERINFOREQ, _client_playerinforeq },
250 { CLIENT_PROGIDENT2, _client_progident2 },
251 { CLIENT_JOINCHANNEL, _client_joinchannel },
252 { CLIENT_MESSAGE, _client_message },
253 { CLIENT_GAMELISTREQ, _client_gamelistreq },
254 { CLIENT_JOIN_GAME, _client_joingame },
255 { CLIENT_STARTGAME1, _client_startgame1 },
256 { CLIENT_STARTGAME3, _client_startgame3 },
257 { CLIENT_STARTGAME4, _client_startgame4 },
258 { CLIENT_CLOSEGAME, _client_closegame },
259 { CLIENT_CLOSEGAME2, _client_closegame },
260 { CLIENT_GAME_REPORT, _client_gamereport },
261 { CLIENT_LEAVECHANNEL, _client_leavechannel },
262 { CLIENT_LADDERREQ, _client_ladderreq },
263 { CLIENT_LADDERSEARCHREQ, _client_laddersearchreq },
264 { CLIENT_MAPAUTHREQ1, _client_mapauthreq1 },
265 { CLIENT_MAPAUTHREQ2, _client_mapauthreq2 },
266 { CLIENT_CLAN_DISBANDREQ, _client_clan_disbandreq },
267 { CLIENT_CLANMEMBERLIST_REQ, _client_clanmemberlistreq },
268 { CLIENT_CLAN_MOTDCHG, _client_clan_motdchg },
269 { CLIENT_CLAN_MOTDREQ, _client_clan_motdreq },
270 { CLIENT_CLAN_CREATEREQ, _client_clan_createreq },
271 { CLIENT_CLAN_CREATEINVITEREQ, _client_clan_createinvitereq },
272 { CLIENT_CLAN_CREATEINVITEREPLY, _client_clan_createinvitereply },
273 { CLIENT_CLANMEMBER_RANKUPDATE_REQ, _client_clanmember_rankupdatereq },
274 { CLIENT_CLANMEMBER_REMOVE_REQ, _client_clanmember_removereq },
275 { CLIENT_CLAN_MEMBERNEWCHIEFREQ, _client_clan_membernewchiefreq },
276 { CLIENT_CLAN_INVITEREQ, _client_clan_invitereq },
277 { CLIENT_CLAN_INVITEREPLY, _client_clan_invitereply },
278 { CLIENT_CRASHDUMP, _client_crashdump },
279 { CLIENT_SETEMAILREPLY, _client_setemailreply },
280 { CLIENT_CLANINFOREQ, _client_claninforeq },
281 { CLIENT_EXTRAWORK, _client_extrawork },
282 { CLIENT_NULL, NULL },
283 { -1, NULL }
284 };
285
286 /* main handler function */
287 static int handle(const t_htable_row * htable, int type, t_connection * c, t_packet const *const packet);
288
289 extern int handle_bnet_packet(t_connection * c, t_packet const *const packet)
290 {
291 if (!c) {
292 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL connection", conn_get_socket(c));
293 return -1;
294 }
295 if (!packet) {
296 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got NULL packet", conn_get_socket(c));
297 return -1;
298 }
299 if (packet_get_class(packet) != packet_class_bnet) {
300 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad packet (class {})", conn_get_socket(c), (int)packet_get_class(packet));
301 return -1;
302 }
303
304 switch (conn_get_state(c)) {
305 case conn_state_connected:
306 switch (handle(bnet_htable_con, packet_get_type(packet), c, packet)) {
307 case 1:
308 eventlog(eventlog_level_error, __FUNCTION__, "[{}] unknown (unlogged in) bnet packet type 0x{:04x}, len {}", conn_get_socket(c), packet_get_type(packet), packet_get_size(packet));
309 break;
310 case -1:
311 eventlog(eventlog_level_error, __FUNCTION__, "[{}] (unlogged in) got error handling packet type 0x{:04x}, len {}", conn_get_socket(c), packet_get_type(packet), packet_get_size(packet));
312 break;
313 };
314 break;
315
316 case conn_state_loggedin:
317 switch (handle(bnet_htable_log, packet_get_type(packet), c, packet)) {
318 case 1:
319 eventlog(eventlog_level_error, __FUNCTION__, "[{}] unknown (logged in) bnet packet type 0x{:04x}, len {}", conn_get_socket(c), packet_get_type(packet), packet_get_size(packet));
320 break;
321 case -1:
322 eventlog(eventlog_level_error, __FUNCTION__, "[{}] (logged in) got error handling packet type 0x{:04x}, len {}", conn_get_socket(c), packet_get_type(packet), packet_get_size(packet));
323 break;
324 };
325 break;
326
327 case conn_state_untrusted:
328 eventlog(eventlog_level_error, __FUNCTION__, "[{}] unknown (untrusted) bnet packet type 0x{:04x}, len {}", conn_get_socket(c), packet_get_type(packet), packet_get_size(packet));
329 break;
330
331 default:
332 eventlog(eventlog_level_error, __FUNCTION__, "[{}] invalid login state {}", conn_get_socket(c), conn_get_state(c));
333 };
334
335 return 0;
336 }
337
338 static int handle(const t_htable_row * htable, int type, t_connection * c, t_packet const *const packet)
339 {
340 t_htable_row const *p;
341 int res = 1;
342
343 for (p = htable; p->type != -1; p++)
344 if (p->type == type) {
345 res = 0;
346 if (p->handler != NULL)
347 res = p->handler(c, packet);
348 if (res != 2)
349 break; /* return 2 means we want to continue parsing */
350 }
351
352 return res;
353 }
354
355 /* handlers for bnet packets */
356 static int _client_unknown_1b(t_connection * c, t_packet const *const packet)
357 {
358 if (packet_get_size(packet) < sizeof(t_client_unknown_1b)) {
359 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad UNKNOWN_1B packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_unknown_1b), packet_get_size(packet));
360 return -1;
361 }
362
363 {
364 unsigned int newip;
365 unsigned short newport;
366
367 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] UNKNOWN_1B unknown1=0x{:04hx}", conn_get_socket(c), bn_short_get(packet->u.client_unknown_1b.unknown1));
368 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] UNKNOWN_1B unknown2=0x{:08x}", conn_get_socket(c), bn_int_get(packet->u.client_unknown_1b.unknown2));
369 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] UNKNOWN_1B unknown3=0x{:08x}", conn_get_socket(c), bn_int_get(packet->u.client_unknown_1b.unknown3));
370
371 newip = bn_int_nget(packet->u.client_unknown_1b.ip);
372 newport = bn_short_nget(packet->u.client_unknown_1b.port);
373
374 eventlog(eventlog_level_info, __FUNCTION__, "[{}] UNKNOWN_1B set new UDP address to {}", conn_get_socket(c), addr_num_to_addr_str(newip, newport));
375 conn_set_game_addr(c, newip);
376 conn_set_game_port(c, newport);
377 }
378 return 0;
379 }
380
381 static int _client_compinfo1(t_connection * c, t_packet const *const packet)
382 {
383 t_packet *rpacket;
384
385 if (packet_get_size(packet) < sizeof(t_client_compinfo1)) {
386 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COMPINFO1 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_compinfo1), packet_get_size(packet));
387 return -1;
388 }
389
390 {
391 char const *host;
392 char const *user;
393
394 if (!(host = packet_get_str_const(packet, sizeof(t_client_compinfo1), MAX_WINHOST_STR))) {
395 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COMPINFO1 packet (missing or too long host)", conn_get_socket(c));
396 return -1;
397 }
398 if (!(user = packet_get_str_const(packet, sizeof(t_client_compinfo1)+std::strlen(host) + 1, MAX_WINUSER_STR))) {
399 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COMPINFO1 packet (missing or too long user)", conn_get_socket(c));
400 return -1;
401 }
402
403 conn_set_host(c, host);
404 conn_set_user(c, user);
405 }
406
407 if ((rpacket = packet_create(packet_class_bnet))) {
408 packet_set_size(rpacket, sizeof(t_server_compreply));
409 packet_set_type(rpacket, SERVER_COMPREPLY);
410 bn_int_set(&rpacket->u.server_compreply.reg_version, SERVER_COMPREPLY_REG_VERSION);
411 bn_int_set(&rpacket->u.server_compreply.reg_auth, SERVER_COMPREPLY_REG_AUTH);
412 bn_int_set(&rpacket->u.server_compreply.client_id, SERVER_COMPREPLY_CLIENT_ID);
413 bn_int_set(&rpacket->u.server_compreply.client_token, SERVER_COMPREPLY_CLIENT_TOKEN);
414 conn_push_outqueue(c, rpacket);
415 packet_del_ref(rpacket);
416 }
417 if ((rpacket = packet_create(packet_class_bnet))) {
418 packet_set_size(rpacket, sizeof(t_server_sessionkey1));
419 packet_set_type(rpacket, SERVER_SESSIONKEY1);
420 bn_int_set(&rpacket->u.server_sessionkey1.sessionkey, conn_get_sessionkey(c));
421 conn_push_outqueue(c, rpacket);
422 packet_del_ref(rpacket);
423 }
424 return 0;
425 }
426
427 static int _client_compinfo2(t_connection * c, t_packet const *const packet)
428 {
429 t_packet *rpacket;
430
431 if (packet_get_size(packet) < sizeof(t_client_compinfo2)) {
432 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COMPINFO2 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_compinfo2), packet_get_size(packet));
433 return -1;
434 }
435
436 {
437 char const *host;
438 char const *user;
439
440 if (!(host = packet_get_str_const(packet, sizeof(t_client_compinfo2), MAX_WINHOST_STR))) {
441 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COMPINFO2 packet (missing or too long host)", conn_get_socket(c));
442 return -1;
443 }
444 if (!(user = packet_get_str_const(packet, sizeof(t_client_compinfo2)+std::strlen(host) + 1, MAX_WINUSER_STR))) {
445 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COMPINFO2 packet (missing or too long user)", conn_get_socket(c));
446 return -1;
447 }
448
449 conn_set_host(c, host);
450 conn_set_user(c, user);
451 }
452
453 if ((rpacket = packet_create(packet_class_bnet))) {
454 packet_set_size(rpacket, sizeof(t_server_compreply));
455 packet_set_type(rpacket, SERVER_COMPREPLY);
456 bn_int_set(&rpacket->u.server_compreply.reg_version, SERVER_COMPREPLY_REG_VERSION);
457 bn_int_set(&rpacket->u.server_compreply.reg_auth, SERVER_COMPREPLY_REG_AUTH);
458 bn_int_set(&rpacket->u.server_compreply.client_id, SERVER_COMPREPLY_CLIENT_ID);
459 bn_int_set(&rpacket->u.server_compreply.client_token, SERVER_COMPREPLY_CLIENT_TOKEN);
460 conn_push_outqueue(c, rpacket);
461 packet_del_ref(rpacket);
462 }
463
464 if ((rpacket = packet_create(packet_class_bnet))) {
465 packet_set_size(rpacket, sizeof(t_server_sessionkey2));
466 packet_set_type(rpacket, SERVER_SESSIONKEY2);
467 bn_int_set(&rpacket->u.server_sessionkey2.sessionnum, conn_get_sessionnum(c));
468 bn_int_set(&rpacket->u.server_sessionkey2.sessionkey, conn_get_sessionkey(c));
469 conn_push_outqueue(c, rpacket);
470 packet_del_ref(rpacket);
471 }
472
473 return 0;
474 }
475
476 static int _client_countryinfo1(t_connection * c, t_packet const *const packet)
477 {
478 if (packet_get_size(packet) < sizeof(t_client_countryinfo1)) {
479 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COUNTRYINFO1 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_countryinfo1), packet_get_size(packet));
480 return -1;
481 }
482 {
483 char const *langstr;
484 char const *countrycode;
485 char const *country;
486 unsigned int tzbias;
487
488 if (!(langstr = packet_get_str_const(packet, sizeof(t_client_countryinfo1), MAX_LANG_STR))) {
489 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COUNTRYINFO1 packet (missing or too long langstr)", conn_get_socket(c));
490 return -1;
491 }
492
493 if (!(countrycode = packet_get_str_const(packet, sizeof(t_client_countryinfo1)+std::strlen(langstr) + 1, MAX_COUNTRYCODE_STR))) {
494 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COUNTRYINFO1 packet (missing or too long countrycode)", conn_get_socket(c));
495 return -1;
496 }
497
498 if (!(country = packet_get_str_const(packet, sizeof(t_client_countryinfo1)+std::strlen(langstr) + 1 + std::strlen(countrycode) + 1, MAX_COUNTRY_STR))) {
499 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COUNTRYINFO1 packet (missing or too long country)", conn_get_socket(c));
500 return -1;
501 }
502
503 if (!(packet_get_str_const(packet, sizeof(t_client_countryinfo1)+std::strlen(langstr) + 1 + std::strlen(countrycode) + 1 + std::strlen(country) + 1, MAX_COUNTRYNAME_STR))) {
504 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad COUNTRYINFO1 packet (missing or too long countryname)", conn_get_socket(c));
505 return -1;
506 }
507
508 tzbias = bn_int_get(packet->u.client_countryinfo1.bias);
509 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] COUNTRYINFO1 packet from tzbias=0x{:04x} langstr={} countrycode={} country={}", conn_get_socket(c), tzbias, langstr, countrycode, country);
510 conn_set_country(c, country);
511 conn_set_tzbias(c, uint32_to_int(tzbias));
512 }
513 return 0;
514 }
515
516 static int _client_auth_info(t_connection * c, t_packet const *const packet)
517 {
518 t_packet *rpacket;
519
520 if (packet_get_size(packet) < sizeof(t_client_auth_info)) {
521 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTH_INFO packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_auth_info), packet_get_size(packet));
522 return -1;
523 }
524
525 {
526 char const *langstr;
527 char const *countryname;
528 unsigned int tzbias;
529 char archtag_str[5];
530 char clienttag_str[5];
531 char gamelang_str[5];
532
533 if (!(langstr = packet_get_str_const(packet, sizeof(t_client_auth_info), MAX_LANG_STR))) {
534 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTH_INFO packet (missing or too long langstr)", conn_get_socket(c));
535 return -1;
536 }
537
538 if (!(countryname = packet_get_str_const(packet, sizeof(t_client_auth_info)+std::strlen(langstr) + 1, MAX_COUNTRYNAME_STR))) {
539 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTH_INFO packet (missing or too long countryname)", conn_get_socket(c));
540 return -1;
541 }
542
543 /* check if it's an allowed client type */
544 if (tag_check_in_list(bn_int_get(packet->u.client_auth_info.clienttag), prefs_get_allowed_clients())) {
545 conn_set_state(c, conn_state_destroy);
546 return 0;
547 }
548
549 tzbias = bn_int_get(packet->u.client_auth_info.bias);
550
551 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] AUTH_INFO packet {{ protocol={:#02x}, platform={}, product={}, versionid={:#02x}, language={}, localip={:#04x}, tzbias={:04x}, locale={}, language={}, country={}.{} }}",
552 conn_get_socket(c),
553 bn_int_get(packet->u.client_auth_info.protocol),
554 tag_uint_to_str(archtag_str, bn_int_get(packet->u.client_auth_info.archtag)),
555 tag_uint_to_str(clienttag_str, bn_int_get(packet->u.client_auth_info.clienttag)),
556 bn_int_get(packet->u.client_auth_info.versionid),
557 tag_uint_to_str(gamelang_str, bn_int_get(packet->u.client_auth_info.gamelang)),
558 bn_int_get(packet->u.client_auth_info.localip),
559 tzbias,
560 bn_int_get(packet->u.client_auth_info.lcid),
561 bn_int_get(packet->u.client_auth_info.langid),
562 langstr,
563 countryname);
564
565 conn_set_country(c, langstr); /* FIXME: This isn't right. We want USA not ENU (English-US) */
566 conn_set_tzbias(c, uint32_to_int(tzbias));
567 conn_set_versionid(c, bn_int_get(packet->u.client_auth_info.versionid));
568 conn_set_archtag(c, bn_int_get(packet->u.client_auth_info.archtag));
569 conn_set_clienttag(c, bn_int_get(packet->u.client_auth_info.clienttag));
570 conn_set_gamelang(c, bn_int_get(packet->u.client_auth_info.gamelang));
571
572 /* First, send an ECHO_REQ */
573
574 if ((rpacket = packet_create(packet_class_bnet))) {
575 packet_set_size(rpacket, sizeof(t_server_echoreq));
576 packet_set_type(rpacket, SERVER_ECHOREQ);
577 bn_int_set(&rpacket->u.server_echoreq.ticks, get_ticks());
578 conn_push_outqueue(c, rpacket);
579 packet_del_ref(rpacket);
580 }
581
582 if ((rpacket = packet_create(packet_class_bnet)))
583 {
584 packet_set_size(rpacket, sizeof(t_server_authreq_109));
585 packet_set_type(rpacket, SERVER_AUTHREQ_109);
586
587 // Logon type
588 if ((conn_get_clienttag(c) == CLIENTTAG_WARCRAFT3_UINT))
589 bn_int_set(&rpacket->u.server_authreq_109.logontype, SERVER_AUTHREQ_109_LOGONTYPE_W3);
590 else if ((conn_get_clienttag(c) == CLIENTTAG_WAR3XP_UINT))
591 bn_int_set(&rpacket->u.server_authreq_109.logontype, SERVER_AUTHREQ_109_LOGONTYPE_W3XP);
592 else
593 bn_int_set(&rpacket->u.server_authreq_109.logontype, SERVER_AUTHREQ_109_LOGONTYPE);
594
595
596 // Session
597 bn_int_set(&rpacket->u.server_authreq_109.sessionkey, conn_get_sessionkey(c));
598 bn_int_set(&rpacket->u.server_authreq_109.sessionnum, conn_get_sessionnum(c));
599
600
601 // CheckRevision
602 std::tuple<std::string, std::string> checkrevision = select_checkrevision(bn_int_get(packet->u.client_auth_info.archtag), bn_int_get(packet->u.client_auth_info.clienttag), bn_int_get(packet->u.client_auth_info.versionid));
603 file_to_mod_time(c, std::get<0>(checkrevision).c_str(), &rpacket->u.server_authreq_109.timestamp); // Checkrevision file timestamp
604 packet_append_string(rpacket, std::get<0>(checkrevision).c_str()); // CheckRevision filename
605 packet_append_string(rpacket, std::get<1>(checkrevision).c_str()); // CheckRevision equation
606 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] selected \"{}\" \"{}\"", conn_get_socket(c), std::get<0>(checkrevision), std::get<1>(checkrevision));
607
608
609 // WarCraft 3 Server Signature
610 if ((conn_get_clienttag(c) == CLIENTTAG_WARCRAFT3_UINT)
611 || (conn_get_clienttag(c) == CLIENTTAG_WAR3XP_UINT))
612 {
613 char padding[128];
614 std::memset(padding, 0, 128);
615 packet_append_data(rpacket, padding, 128);
616 }
617
618 conn_push_outqueue(c, rpacket);
619 packet_del_ref(rpacket);
620 }
621 }
622 return 0;
623 }
624
625 static int _client_unknown2b(t_connection * c, t_packet const *const packet)
626 {
627 if (packet_get_size(packet) < sizeof(t_client_unknown_2b)) {
628 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad UNKNOWN_2B packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_unknown_2b), packet_get_size(packet));
629 return -1;
630 }
631 return 0;
632 }
633
634 static int _client_progident(t_connection * c, t_packet const *const packet)
635 {
636 t_packet *rpacket;
637
638 if (packet_get_size(packet) < sizeof(t_client_progident)) {
639 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad PROGIDENT packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_progident), packet_get_size(packet));
640 return -1;
641 }
642
643 if (tag_check_in_list(bn_int_get(packet->u.client_progident.clienttag), prefs_get_allowed_clients())) {
644 conn_set_state(c, conn_state_destroy);
645 return 0;
646 }
647
648 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] CLIENT_PROGIDENT archtag=0x{:08x} clienttag=0x{:08x} versionid=0x{:08x} unknown1=0x{:08x}", conn_get_socket(c), bn_int_get(packet->u.client_progident.archtag), bn_int_get(packet->u.client_progident.clienttag), bn_int_get(packet->u.client_progident.versionid), bn_int_get(packet->u.client_progident.unknown1));
649
650 conn_set_archtag(c, bn_int_get(packet->u.client_progident.archtag));
651 conn_set_clienttag(c, bn_int_get(packet->u.client_progident.clienttag));
652 conn_set_versionid(c, bn_int_get(packet->u.client_progident.versionid));
653
654 if ((rpacket = packet_create(packet_class_bnet)))
655 {
656 packet_set_size(rpacket, sizeof(t_server_authreq1));
657 packet_set_type(rpacket, SERVER_AUTHREQ1);
658
659
660 // CheckRevision
661 std::tuple<std::string, std::string> checkrevision = select_checkrevision(bn_int_get(packet->u.client_progident.archtag), bn_int_get(packet->u.client_progident.clienttag), bn_int_get(packet->u.client_progident.versionid));
662 file_to_mod_time(c, std::get<0>(checkrevision).c_str(), &rpacket->u.server_authreq1.timestamp); // Checkrevision file timestamp
663 packet_append_string(rpacket, std::get<0>(checkrevision).c_str()); // CheckRevision filename
664 packet_append_string(rpacket, std::get<1>(checkrevision).c_str()); // CheckRevision equation
665 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] selected \"{}\" \"{}\"", conn_get_socket(c), std::get<0>(checkrevision), std::get<1>(checkrevision));
666
667
668 conn_push_outqueue(c, rpacket);
669 packet_del_ref(rpacket);
670 }
671
672 return 0;
673 }
674
675 static int _client_createaccountw3(t_connection * c, t_packet const *const packet)
676 {
677 char const *username;
678 char const *plainpass;
679 t_hash sc_hash;
680 unsigned int i;
681 const char *account_salt;
682 const char *account_verifier;
683 t_account *account;
684 bool presume_plainpass = true;
685
686 if (packet_get_size(packet) < sizeof(t_client_createaccount_w3)) {
687 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CREATEACCOUNT_W3 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_createaccount_w3), packet_get_size(packet));
688 return -1;
689 }
690
691 username = packet_get_str_const(packet, sizeof(t_client_createaccount_w3), UNCHECKED_NAME_STR);
692 if (!username) {
693 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CREATEACCOUNT_W3 (missing or too long username)", conn_get_socket(c));
694 return -1;
695 }
696
697 /* PELISH: We are testing if username missing and if packetsize is good
698 so we does not need to test if salt and verifier are present */
699 account_salt = (const char *)packet_get_data_const(packet, offsetof(t_client_createaccount_w3, salt), 32);
700 account_verifier = (const char *)packet_get_data_const(packet, offsetof(t_client_createaccount_w3, password_verifier), 32);
701
702 for (i = 0; i<16; i++){
703 int value = (unsigned char)account_verifier[i];
704 if (value == 0)
705 break;
706 presume_plainpass &= (std::isprint(value)>0);
707 }
708
709 for (i = 16; (i < 32) && presume_plainpass; i++){
710 int value = account_verifier[i];
711 presume_plainpass &= (value == 0);
712 }
713
714 if (presume_plainpass)
715 eventlog(eventlog_level_debug, __FUNCTION__, "This looks more like a plaintext password than like a verifier");
716
717 plainpass = packet_get_str_const(packet, offsetof(t_client_createaccount_w3, password_verifier), 16);
718 if (presume_plainpass && !plainpass) {
719 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CREATEACCOUNT_W3 (missing password)", conn_get_socket(c));
720 return -1;
721 }
722
723 t_packet* const rpacket = packet_create(packet_class_bnet);
724 if (!rpacket)
725 return -1;
726
727 packet_set_size(rpacket, sizeof(t_server_createaccount_w3));
728 packet_set_type(rpacket, SERVER_CREATEACCOUNT_W3);
729
730 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] new account requested for \"{}\"", conn_get_socket(c), username);
731
732 if (prefs_get_allow_new_accounts() == 0)
733 {
734 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account not created (disabled)", conn_get_socket(c));
735 bn_int_set(&rpacket->u.server_createaccount_w3.result, SERVER_CREATEACCOUNT_W3_RESULT_EXIST);
736 conn_push_outqueue(c, rpacket);
737 packet_del_ref(rpacket);
738 return 0;
739 }
740
741 if (account_check_name(username) < 0)
742 {
743 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account not created (invalid symbols)", conn_get_socket(c));
744 bn_int_set(&rpacket->u.server_createaccount_w3.result, SERVER_CREATEACCOUNT_W3_RESULT_INVALID);
745 conn_push_outqueue(c, rpacket);
746 packet_del_ref(rpacket);
747 return 0;
748 }
749
750 char lpass[20] = {};
751 if (plainpass)
752 {
753 /* convert plaintext password to lowercase for sc etc. */
754 std::snprintf(lpass, sizeof lpass, "%s", plainpass);
755 strtolower(lpass);
756 }
757
758 //set password hash for sc etc.
759 bnet_hash(&sc_hash, std::strlen(lpass), lpass);
760 if (!(account = accountlist_create_account(username, hash_get_str(sc_hash))))
761 {
762 bn_int_set(&rpacket->u.server_createaccount_w3.result, SERVER_CREATEACCOUNT_W3_RESULT_EXIST);
763 }
764 else
765 {
766 if (presume_plainpass)
767 {
768 BigInt salt = BigInt((unsigned char*)account_salt, 32, 4, false);
769 BnetSRP3 srp3 = BnetSRP3(username, plainpass);
770 srp3.setSalt(salt);
771 BigInt verifier = srp3.getVerifier();
772 account_verifier = (const char*)verifier.getData(32, 4, false);
773 }
774 else {
775 //NEED TO TAG ACCOUNT AS "SC PASS BROKEN"
776 }
777 account_set_salt(account, account_salt);
778 account_set_verifier(account, account_verifier);
779 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account created", conn_get_socket(c));
780 bn_int_set(&rpacket->u.server_createaccount_w3.result, SERVER_CREATEACCOUNT_W3_RESULT_OK);
781 }
782
783 conn_push_outqueue(c, rpacket);
784 packet_del_ref(rpacket);
785
786 return 0;
787 }
788
789 static int _client_createacctreq1(t_connection * c, t_packet const *const packet)
790 {
791 t_packet *rpacket;
792 char const *username;
793 t_hash newpasshash1;
794
795 if (packet_get_size(packet) < sizeof(t_client_createacctreq1)) {
796 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CREATEACCTREQ1 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_createacctreq1), packet_get_size(packet));
797 return -1;
798 }
799
800 if (!(username = packet_get_str_const(packet, sizeof(t_client_createacctreq1), UNCHECKED_NAME_STR))) {
801 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CREATEACCTREQ1 (missing or too long username)", conn_get_socket(c));
802 return -1;
803 }
804
805 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] new account requested for \"{}\"", conn_get_socket(c), username);
806
807 rpacket = packet_create(packet_class_bnet);
808 if (!rpacket)
809 return -1;
810 packet_set_size(rpacket, sizeof(t_server_createacctreply1));
811 packet_set_type(rpacket, SERVER_CREATEACCTREPLY1);
812
813 if (prefs_get_allow_new_accounts() == 0) {
814 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account not created (disabled)", conn_get_socket(c));
815 bn_int_set(&rpacket->u.server_createacctreply1.result, SERVER_CREATEACCTREPLY1_RESULT_NO);
816 goto out;
817 }
818
819 bnhash_to_hash(packet->u.client_createacctreq1.password_hash1, &newpasshash1);
820 if (!accountlist_create_account(username, hash_get_str(newpasshash1))) {
821 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account not created (failed)", conn_get_socket(c));
822 bn_int_set(&rpacket->u.server_createacctreply1.result, SERVER_CREATEACCTREPLY1_RESULT_NO);
823 goto out;
824 }
825
826 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account created", conn_get_socket(c));
827 bn_int_set(&rpacket->u.server_createacctreply1.result, SERVER_CREATEACCTREPLY1_RESULT_OK);
828
829 out:
830 conn_push_outqueue(c, rpacket);
831 packet_del_ref(rpacket);
832
833 return 0;
834 }
835
836 static int _client_createacctreq2(t_connection * c, t_packet const *const packet)
837 {
838 t_packet *rpacket;
839 char const *username;
840 t_hash newpasshash1;
841
842 if (packet_get_size(packet) < sizeof(t_client_createacctreq2)) {
843 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_CREATEACCTREQ2 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_createacctreq2), packet_get_size(packet));
844 return -1;
845 }
846
847 username = packet_get_str_const(packet, sizeof(t_client_createacctreq2), UNCHECKED_NAME_STR);
848 if (!username) {
849 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CREATEACCTREQ2 (missing or too long username)", conn_get_socket(c));
850 return -1;
851 }
852
853 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] new account requested for \"{}\"", conn_get_socket(c), username);
854
855 rpacket = packet_create(packet_class_bnet);
856 if (!rpacket)
857 return -1;
858 packet_set_size(rpacket, sizeof(t_server_createacctreply2));
859 packet_set_type(rpacket, SERVER_CREATEACCTREPLY2);
860
861 if (prefs_get_allow_new_accounts() == 0) {
862 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account not created (disabled)", conn_get_socket(c));
863 bn_int_set(&rpacket->u.server_createacctreply2.result, SERVER_CREATEACCTREPLY2_RESULT_EXIST);
864 goto out;
865 }
866
867 if (account_check_name(username) < 0) {
868 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account not created (invalid symbols)", conn_get_socket(c));
869 bn_int_set(&rpacket->u.server_createaccount_w3.result, SERVER_CREATEACCTREPLY2_RESULT_INVALID);
870 goto out;
871 }
872
873 bnhash_to_hash(packet->u.client_createacctreq2.password_hash1, &newpasshash1);
874 if (!accountlist_create_account(username, hash_get_str(newpasshash1))) {
875 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account not created (failed)", conn_get_socket(c));
876 bn_int_set(&rpacket->u.server_createacctreply2.result, SERVER_CREATEACCTREPLY2_RESULT_EXIST); /* FIXME: return reason for failure */
877 goto out;
878 }
879
880 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] account created", conn_get_socket(c));
881 bn_int_set(&rpacket->u.server_createacctreply2.result, SERVER_CREATEACCTREPLY2_RESULT_OK);
882
883 out:
884 conn_push_outqueue(c, rpacket);
885 packet_del_ref(rpacket);
886
887 return 0;
888 }
889
890 static int _client_changepassreq(t_connection * c, t_packet const *const packet)
891 {
892 t_packet *rpacket;
893
894 if (packet_get_size(packet) < sizeof(t_client_changepassreq)) {
895 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CHANGEPASSREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_changepassreq), packet_get_size(packet));
896 return -1;
897 }
898
899 {
900 char const *username;
901 t_account *account;
902
903 if (!(username = packet_get_str_const(packet, sizeof(t_client_changepassreq), UNCHECKED_NAME_STR))) {
904 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CHANGEPASSREQ (missing or too long username)", conn_get_socket(c));
905 return -1;
906 }
907
908 eventlog(eventlog_level_info, __FUNCTION__, "[{}] password change requested for \"{}\"", conn_get_socket(c), username);
909
910 if (!(rpacket = packet_create(packet_class_bnet)))
911 return -1;
912 packet_set_size(rpacket, sizeof(t_server_changepassack));
913 packet_set_type(rpacket, SERVER_CHANGEPASSACK);
914
915 /* fail if logged in or no account */
916 if (connlist_find_connection_by_accountname(username) || !(account = accountlist_find_account(username))) {
917 eventlog(eventlog_level_info, __FUNCTION__, "[{}] password change for \"{}\" refused (no such account)", conn_get_socket(c), username);
918 bn_int_set(&rpacket->u.server_changepassack.message, SERVER_CHANGEPASSACK_MESSAGE_FAIL);
919 }
920 else if (account_get_auth_changepass(account) == 0) { /* default to true */
921 eventlog(eventlog_level_info, __FUNCTION__, "[{}] password change for \"{}\" refused (no change access)", conn_get_socket(c), username);
922 bn_int_set(&rpacket->u.server_changepassack.message, SERVER_CHANGEPASSACK_MESSAGE_FAIL);
923 }
924 else if (conn_get_sessionkey(c) != bn_int_get(packet->u.client_changepassreq.sessionkey)) {
925 eventlog(eventlog_level_error, __FUNCTION__, "[{}] password change for \"{}\" refused (expected session key 0x{:08x}, got 0x{:08x})", conn_get_socket(c), username, conn_get_sessionkey(c), bn_int_get(packet->u.client_changepassreq.sessionkey));
926 bn_int_set(&rpacket->u.server_changepassack.message, SERVER_CHANGEPASSACK_MESSAGE_FAIL);
927 }
928 else {
929 struct {
930 bn_int ticks;
931 bn_int sessionkey;
932 bn_int passhash1[5];
933 } temp;
934 char const *oldstrhash1;
935 t_hash oldpasshash1;
936 t_hash oldpasshash2;
937 t_hash trypasshash2;
938 t_hash newpasshash1;
939
940
941 if ((oldstrhash1 = account_get_pass(account))) {
942 bn_int_set(&temp.ticks, bn_int_get(packet->u.client_changepassreq.ticks));
943 bn_int_set(&temp.sessionkey, bn_int_get(packet->u.client_changepassreq.sessionkey));
944 if (hash_set_str(&oldpasshash1, oldstrhash1) < 0) {
945 bnhash_to_hash(packet->u.client_changepassreq.newpassword_hash1, &newpasshash1);
946 account_set_pass(account, hash_get_str(newpasshash1));
947 eventlog(eventlog_level_info, __FUNCTION__, "[{}] password change for \"{}\" successful (bad previous password)", conn_get_socket(c), account_get_name(account));
948 bn_int_set(&rpacket->u.server_changepassack.message, SERVER_CHANGEPASSACK_MESSAGE_SUCCESS);
949 }
950 else {
951 hash_to_bnhash((t_hash const *)&oldpasshash1, temp.passhash1); /* avoid warning */
952 bnet_hash(&oldpasshash2, sizeof(temp), &temp); /* do the double hash */
953 bnhash_to_hash(packet->u.client_changepassreq.oldpassword_hash2, &trypasshash2);
954
955 if (hash_eq(trypasshash2, oldpasshash2) == 1) {
956 bnhash_to_hash(packet->u.client_changepassreq.newpassword_hash1, &newpasshash1);
957 account_set_pass(account, hash_get_str(newpasshash1));
958 eventlog(eventlog_level_info, __FUNCTION__, "[{}] password change for \"{}\" successful (previous password)", conn_get_socket(c), account_get_name(account));
959 bn_int_set(&rpacket->u.server_changepassack.message, SERVER_CHANGEPASSACK_MESSAGE_SUCCESS);
960 }
961 else {
962 eventlog(eventlog_level_info, __FUNCTION__, "[{}] password change for \"{}\" refused (wrong password)", conn_get_socket(c), account_get_name(account));
963 conn_increment_passfail_count(c);
964 bn_int_set(&rpacket->u.server_changepassack.message, SERVER_CHANGEPASSACK_MESSAGE_FAIL);
965 }
966 }
967 }
968 else {
969 bnhash_to_hash(packet->u.client_changepassreq.newpassword_hash1, &newpasshash1);
970 account_set_pass(account, hash_get_str(newpasshash1));
971 eventlog(eventlog_level_info, __FUNCTION__, "[{}] password change for \"{}\" successful (no previous password)", conn_get_socket(c), account_get_name(account));
972 bn_int_set(&rpacket->u.server_changepassack.message, SERVER_CHANGEPASSACK_MESSAGE_SUCCESS);
973 }
974 }
975
976 conn_push_outqueue(c, rpacket);
977 packet_del_ref(rpacket);
978
979 }
980
981 return 0;
982 }
983
984 static int _client_echoreply(t_connection * c, t_packet const *const packet)
985 {
986 if (packet_get_size(packet) < sizeof(t_client_echoreply)) {
987 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad ECHOREPLY packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_echoreply), packet_get_size(packet));
988 return -1;
989 }
990
991 {
992 unsigned int now;
993 unsigned int then;
994
995 now = get_ticks();
996 then = bn_int_get(packet->u.client_echoreply.ticks);
997 if (!now || !then || now < then)
998 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] bad timing in echo reply: now={} then={}", conn_get_socket(c), now, then);
999 else
1000 conn_set_latency(c, now - then);
1001 }
1002
1003 return 0;
1004 }
1005
1006 static int _client_authreq1(t_connection * c, t_packet const *const packet)
1007 {
1008 eventlog(eventlog_level_trace, __FUNCTION__, "[{}] received AUTHREQ1(0x07) packet", conn_get_socket(c));
1009
1010 if (packet_get_size(packet) < sizeof(t_client_authreq1))
1011 {
1012 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTHREQ1 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_authreq1), packet_get_size(packet));
1013 return -1;
1014 }
1015
1016 auto send_failed_packet = [](t_connection *c)
1017 {
1018 t_packet *rpacket = packet_create(packet_class_bnet);
1019 if (rpacket)
1020 {
1021 packet_set_size(rpacket, sizeof(t_server_authreply1));
1022 packet_set_type(rpacket, SERVER_AUTHREPLY1);
1023 conn_set_state(c, conn_state_untrusted);
1024
1025 bn_int_set(&rpacket->u.server_authreply1.message, SERVER_AUTHREPLY1_MESSAGE_BADVERSION);
1026 packet_append_string(rpacket, "");
1027 packet_append_string(rpacket, ""); // undocumented extra null terminator
1028
1029 conn_push_outqueue(c, rpacket);
1030
1031 packet_del_ref(rpacket);
1032 }
1033 };
1034
1035 // The following if statements are sanity checks
1036 // The client should have already sent this information in a previous packet and is resending it again in this packet
1037 if (bn_int_get(packet->u.client_authreq1.archtag) != conn_get_archtag(c))
1038 {
1039 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTHREQ1 (mismatch architecture)", conn_get_socket(c));
1040 send_failed_packet(c);
1041 return -1;
1042 }
1043
1044 if (bn_int_get(packet->u.client_authreq1.clienttag) != conn_get_clienttag(c))
1045 {
1046 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTHREQ1 (mismatch client tag)", conn_get_socket(c));
1047 send_failed_packet(c);
1048 return -1;
1049 }
1050
1051 if (bn_int_get(packet->u.client_authreq1.versionid) != conn_get_versionid(c))
1052 {
1053 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTHREQ1 (mismatch version ID)", conn_get_socket(c));
1054 send_failed_packet(c);
1055 return -1;
1056 }
1057
1058
1059 const char *exeinfo = packet_get_str_const(packet, sizeof(t_client_authreq1), MAX_EXEINFO_STR);
1060 if (exeinfo)
1061 {
1062 conn_set_clientexe(c, exeinfo);
1063 }
1064 else
1065 {
1066 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTHREQ1 (missing or too long exeinfo)", conn_get_socket(c));
1067 send_failed_packet(c);
1068 return 0;
1069 }
1070
1071 std::string version_string = vernum_to_verstr(bn_int_get(packet->u.client_authreq1.gameversion));
1072 conn_set_clientver(c, version_string.c_str());
1073 eventlog(eventlog_level_info, __FUNCTION__, "[{}] CLIENT_AUTHREQ1 archtag=0x{:08x} clienttag=0x{:08x} verstr={} exeinfo=\"{}\" versionid=0x{:08x} gameversion=0x{:08x} checksum=0x{:08x}", conn_get_socket(c), bn_int_get(packet->u.client_authreq1.archtag), bn_int_get(packet->u.client_authreq1.clienttag), version_string, exeinfo, conn_get_versionid(c), conn_get_gameversion(c), conn_get_checksum(c));
1074
1075 conn_set_versionid(c, bn_int_get(packet->u.client_authreq1.versionid));
1076 conn_set_checksum(c, bn_int_get(packet->u.client_authreq1.checksum));
1077 conn_set_gameversion(c, bn_int_get(packet->u.client_authreq1.gameversion));
1078
1079
1080 const VersionCheck* vc = select_versioncheck(conn_get_archtag(c), conn_get_clienttag(c), conn_get_versionid(c), conn_get_gameversion(c), conn_get_checksum(c));
1081 conn_set_versioncheck(c, vc);
1082 if (vc)
1083 {
1084 eventlog(eventlog_level_info, __FUNCTION__, "[{}] client matches versiontag \"{}\"", conn_get_socket(c), conn_get_versioncheck(c)->get_version_tag());
1085 }
1086 else
1087 {
1088 if (prefs_get_allow_unknown_version())
1089 {
1090 eventlog(eventlog_level_info, __FUNCTION__, "[{}] skipping versioncheck because allow_unknown_version is true", conn_get_socket(c));
1091 }
1092 else
1093 {
1094 eventlog(eventlog_level_info, __FUNCTION__, "[{}] client failed versioncheck", conn_get_socket(c));
1095 send_failed_packet(c);
1096 return 0;
1097 }
1098 }
1099
1100
1101 t_packet *rpacket = packet_create(packet_class_bnet);
1102 if (rpacket)
1103 {
1104 packet_set_size(rpacket, sizeof(t_server_authreply1));
1105 packet_set_type(rpacket, SERVER_AUTHREPLY1);
1106
1107 char *mpqfilename = nullptr;
1108 if (vc)
1109 {
1110 mpqfilename = autoupdate_check(conn_get_archtag(c), conn_get_clienttag(c), conn_get_gamelang(c), vc->get_version_tag().c_str(), nullptr);
1111 }
1112
1113 // Only handle updates when there is an update file available.
1114 if (mpqfilename)
1115 {
1116 eventlog(eventlog_level_info, __FUNCTION__, "[{}] an upgrade for version {} is available \"{}\"", conn_get_socket(c), conn_get_versioncheck(c)->get_version_tag(), mpqfilename);
1117 bn_int_set(&rpacket->u.server_authreply1.message, SERVER_AUTHREPLY1_MESSAGE_UPDATE);
1118 packet_append_string(rpacket, mpqfilename);
1119
1120 xfree(static_cast<void *>(mpqfilename));
1121 }
1122 else
1123 {
1124 eventlog(eventlog_level_info, __FUNCTION__, "[{}] no upgrade is available", conn_get_socket(c));
1125 }
1126
1127 bn_int_set(&rpacket->u.server_authreply1.message, SERVER_AUTHREPLY1_MESSAGE_OK);
1128 packet_append_string(rpacket, "");
1129 packet_append_string(rpacket, ""); // FIXME: what's the second string for?
1130
1131 conn_push_outqueue(c, rpacket);
1132
1133 packet_del_ref(rpacket);
1134 }
1135
1136 return 0;
1137 }
1138
1139 static int _client_authreq109(t_connection * c, t_packet const *const packet)
1140 {
1141 if (packet_get_size(packet) < sizeof(t_client_authreq_109))
1142 {
1143 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTHREQ_109 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_authreq_109), packet_get_size(packet));
1144 return 0;
1145 }
1146
1147 auto send_failed_packet = [](t_connection *c)
1148 {
1149 t_packet *rpacket = packet_create(packet_class_bnet);
1150 if (rpacket)
1151 {
1152 packet_set_size(rpacket, sizeof(t_server_authreply_109));
1153 packet_set_type(rpacket, SERVER_AUTHREPLY_109);
1154 conn_set_state(c, conn_state_untrusted);
1155
1156 bn_int_set(&rpacket->u.server_authreply_109.message, SERVER_AUTHREPLY_109_MESSAGE_BADVERSION);
1157 packet_append_string(rpacket, "");
1158
1159 conn_push_outqueue(c, rpacket);
1160
1161 packet_del_ref(rpacket);
1162 }
1163 };
1164
1165
1166 std::uint32_t count = bn_int_get(packet->u.client_authreq_109.cdkey_number);
1167 std::size_t position = sizeof(t_client_authreq_109) + (count * sizeof(t_cdkey_info));
1168
1169 const char *const exeinfo = packet_get_str_const(packet, position, MAX_EXEINFO_STR);
1170 if (exeinfo)
1171 {
1172 conn_set_clientexe(c, exeinfo);
1173 }
1174 else
1175 {
1176 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTHREQ_109 (missing or too long exeinfo)", conn_get_socket(c));
1177 send_failed_packet(c);
1178 return 0;
1179 }
1180
1181 position += std::strlen(exeinfo) + 1;
1182
1183 const char *const owner = packet_get_str_const(packet, position, MAX_OWNER_STR);
1184 if (owner)
1185 {
1186 conn_set_owner(c, owner);
1187 }
1188 else
1189 {
1190 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad AUTHREQ_109 (missing or too long owner)", conn_get_socket(c));
1191 conn_set_owner(c, "");
1192 }
1193
1194 conn_set_checksum(c, bn_int_get(packet->u.client_authreq_109.checksum));
1195 conn_set_gameversion(c, bn_int_get(packet->u.client_authreq_109.gameversion));
1196 std::string version = vernum_to_verstr(bn_int_get(packet->u.client_authreq_109.gameversion));
1197 conn_set_clientver(c, version.c_str());
1198
1199 eventlog(eventlog_level_info, __FUNCTION__, "[{}] CLIENT_AUTHREQ_109 ticks=0x{:08x}, verstr={} exeinfo=\"{}\" versionid=0x{:08x} gameversion=0x{:08x} checksum=0x{:08x}", conn_get_socket(c), bn_int_get(packet->u.client_authreq_109.ticks), version, exeinfo, conn_get_versionid(c), conn_get_gameversion(c), conn_get_checksum(c));
1200
1201 t_packet *rpacket;
1202 if ((rpacket = packet_create(packet_class_bnet)))
1203 {
1204 packet_set_size(rpacket, sizeof(t_server_authreply_109));
1205 packet_set_type(rpacket, SERVER_AUTHREPLY_109);
1206
1207
1208 const VersionCheck* vc = select_versioncheck(conn_get_archtag(c), conn_get_clienttag(c), conn_get_versionid(c), conn_get_gameversion(c), conn_get_checksum(c));
1209 conn_set_versioncheck(c, vc);
1210 if (vc)
1211 {
1212 eventlog(eventlog_level_info, __FUNCTION__, "[{}] client matches versiontag \"{}\"", conn_get_socket(c), conn_get_versioncheck(c)->get_version_tag());
1213 }
1214 else
1215 {
1216 if (prefs_get_allow_unknown_version())
1217 {
1218 eventlog(eventlog_level_info, __FUNCTION__, "[{}] skipping versioncheck because allow_unknown_version is true", conn_get_socket(c));
1219 }
1220 else
1221 {
1222 eventlog(eventlog_level_info, __FUNCTION__, "[{}] client failed versioncheck", conn_get_socket(c));
1223 packet_del_ref(rpacket);
1224 send_failed_packet(c);
1225 return 0;
1226 }
1227 }
1228
1229
1230 char *mpqfilename = nullptr;
1231 if (vc)
1232 {
1233 mpqfilename = autoupdate_check(conn_get_archtag(c), conn_get_clienttag(c), conn_get_gamelang(c), conn_get_versioncheck(c)->get_version_tag().c_str(), NULL);
1234 }
1235
1236 // Only handle updates when there is an update file available.
1237 if (mpqfilename)
1238 {
1239 eventlog(eventlog_level_info, __FUNCTION__, "[{}] an upgrade for {} is available \"{}\"", conn_get_socket(c), conn_get_versioncheck(c)->get_version_tag(), mpqfilename);
1240 bn_int_set(&rpacket->u.server_authreply_109.message, SERVER_AUTHREPLY_109_MESSAGE_UPDATE);
1241 packet_append_string(rpacket, mpqfilename);
1242
1243 xfree(static_cast<void *>(mpqfilename));
1244 }
1245 else
1246 {
1247 eventlog(eventlog_level_info, __FUNCTION__, "[{}] no upgrade is available", conn_get_socket(c));
1248 }
1249
1250 bn_int_set(&rpacket->u.server_authreply_109.message, SERVER_AUTHREPLY_109_MESSAGE_OK);
1251 packet_append_string(rpacket, "");
1252
1253 conn_push_outqueue(c, rpacket);
1254
1255 packet_del_ref(rpacket);
1256 }
1257
1258 return 0;
1259 }
1260
1261 static int _client_regsnoopreply(t_connection * c, t_packet const *const packet)
1262 {
1263 if (packet_get_size(packet) < sizeof(t_client_regsnoopreply)) {
1264 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad REGSNOOPREPLY packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_regsnoopreply), packet_get_size(packet));
1265 return -1;
1266 }
1267 return 0;
1268 }
1269
1270 static int _client_iconreq(t_connection * c, t_packet const *const packet)
1271 {
1272 t_packet *rpacket;
1273
1274 if (packet_get_size(packet) < sizeof(t_client_iconreq)) {
1275 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad ICONREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_iconreq), packet_get_size(packet));
1276 return -1;
1277 }
1278
1279 if ((rpacket = packet_create(packet_class_bnet))) {
1280 packet_set_size(rpacket, sizeof(t_server_iconreply));
1281 packet_set_type(rpacket, SERVER_ICONREPLY);
1282 file_to_mod_time(c, prefs_get_iconfile(), &rpacket->u.server_iconreply.timestamp);
1283
1284 /* battle.net sends different file on iconreq for WAR3 and W3XP [Omega] */
1285 if ((conn_get_clienttag(c) == CLIENTTAG_WARCRAFT3_UINT) || (conn_get_clienttag(c) == CLIENTTAG_WAR3XP_UINT))
1286 packet_append_string(rpacket, prefs_get_war3_iconfile());
1287 /* battle.net still sends "icons.bni" to sc/bw clients
1288 * clients request icons_STAR.bni seperatly */
1289 /* else if (std::strcmp(conn_get_clienttag(c),CLIENTTAG_STARCRAFT)==0)
1290 packet_append_string(rpacket,prefs_get_star_iconfile());
1291 else if (std::strcmp(conn_get_clienttag(c),CLIENTTAG_BROODWARS)==0)
1292 packet_append_string(rpacket,prefs_get_star_iconfile());
1293 */
1294 else
1295 packet_append_string(rpacket, prefs_get_iconfile());
1296
1297 conn_push_outqueue(c, rpacket);
1298 packet_del_ref(rpacket);
1299 }
1300
1301 return 0;
1302 }
1303
1304 static int _client_cdkey(t_connection * c, t_packet const *const packet)
1305 {
1306 t_packet *rpacket;
1307
1308 if (packet_get_size(packet) < sizeof(t_client_cdkey)) {
1309 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CDKEY packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_cdkey), packet_get_size(packet));
1310 return -1;
1311 }
1312
1313 {
1314 char const *cdkey;
1315 char const *owner;
1316
1317 if (!(cdkey = packet_get_str_const(packet, sizeof(t_client_cdkey), MAX_CDKEY_STR))) {
1318 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CDKEY packet (missing or too long cdkey)", conn_get_socket(c));
1319 return -1;
1320 }
1321 if (!(owner = packet_get_str_const(packet, sizeof(t_client_cdkey)+std::strlen(cdkey) + 1, MAX_OWNER_STR))) {
1322 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CDKEY packet (missing or too long owner)", conn_get_socket(c));
1323 return -1;
1324 }
1325
1326 conn_set_cdkey(c, cdkey);
1327 conn_set_owner(c, owner);
1328
1329 if ((rpacket = packet_create(packet_class_bnet))) {
1330 packet_set_size(rpacket, sizeof(t_server_cdkeyreply));
1331 packet_set_type(rpacket, SERVER_CDKEYREPLY);
1332 bn_int_set(&rpacket->u.server_cdkeyreply.message, SERVER_CDKEYREPLY_MESSAGE_OK);
1333 packet_append_string(rpacket, owner);
1334 conn_push_outqueue(c, rpacket);
1335 packet_del_ref(rpacket);
1336 }
1337 }
1338 #if 0 /* Blizzard used this to track down pirates, should only be accepted by old clients */
1339 if ((rpacket = packet_create(packet_class_bnet))) {
1340 packet_set_size(rpacket, sizeof(t_server_regsnoopreq));
1341 packet_set_type(rpacket, SERVER_REGSNOOPREQ);
1342 bn_int_set(&rpacket->u.server_regsnoopreq.unknown1, SERVER_REGSNOOPREQ_UNKNOWN1); /* sequence num */
1343 bn_int_set(&rpacket->u.server_regsnoopreq.hkey, SERVER_REGSNOOPREQ_HKEY_CURRENT_USER);
1344 packet_append_string(rpacket, SERVER_REGSNOOPREQ_REGKEY);
1345 packet_append_string(rpacket, SERVER_REGSNOOPREQ_REGVALNAME);
1346 conn_push_outqueue(c, rpacket);
1347 packet_del_ref(rpacket);
1348 }
1349 #endif
1350 return 0;
1351 }
1352
1353 static int _client_cdkey2(t_connection * c, t_packet const *const packet)
1354 {
1355 t_packet *rpacket;
1356
1357 if (packet_get_size(packet) < sizeof(t_client_cdkey2)) {
1358 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CDKEY2 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_cdkey2), packet_get_size(packet));
1359 return -1;
1360 }
1361
1362 {
1363 char const *owner;
1364
1365 if (!(owner = packet_get_str_const(packet, sizeof(t_client_cdkey2), MAX_OWNER_STR))) {
1366 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CDKEY2 packet (missing or too long owner)", conn_get_socket(c));
1367 return -1;
1368 }
1369
1370 conn_set_owner(c, owner);
1371
1372 if ((rpacket = packet_create(packet_class_bnet))) {
1373 packet_set_size(rpacket, sizeof(t_server_cdkeyreply2));
1374 packet_set_type(rpacket, SERVER_CDKEYREPLY2);
1375 bn_int_set(&rpacket->u.server_cdkeyreply2.message, SERVER_CDKEYREPLY2_MESSAGE_OK);
1376 packet_append_string(rpacket, owner);
1377 conn_push_outqueue(c, rpacket);
1378 packet_del_ref(rpacket);
1379 }
1380 }
1381
1382 return 0;
1383 }
1384
1385 static int _client_cdkey3(t_connection * c, t_packet const *const packet)
1386 {
1387 t_packet *rpacket;
1388
1389 if (packet_get_size(packet) < sizeof(t_client_cdkey3)) {
1390 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CDKEY3 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_cdkey2), packet_get_size(packet));
1391 return -1;
1392 }
1393
1394 {
1395 char const *owner;
1396
1397 if (!(owner = packet_get_str_const(packet, sizeof(t_client_cdkey3), MAX_OWNER_STR))) {
1398 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CDKEY3 packet (missing or too long owner)", conn_get_socket(c));
1399 return -1;
1400 }
1401
1402 conn_set_owner(c, owner);
1403
1404 if ((rpacket = packet_create(packet_class_bnet))) {
1405 packet_set_size(rpacket, sizeof(t_server_cdkeyreply3));
1406 packet_set_type(rpacket, SERVER_CDKEYREPLY3);
1407 bn_int_set(&rpacket->u.server_cdkeyreply3.message, SERVER_CDKEYREPLY3_MESSAGE_OK);
1408 packet_append_string(rpacket, ""); /* FIXME: owner, message, ??? */
1409 conn_push_outqueue(c, rpacket);
1410 packet_del_ref(rpacket);
1411 }
1412 }
1413
1414 return 0;
1415 }
1416
1417 static int _client_udpok(t_connection * c, t_packet const *const packet)
1418 {
1419 if (packet_get_size(packet) < sizeof(t_client_udpok)) {
1420 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad UDPOK packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_udpok), packet_get_size(packet));
1421 return -1;
1422 }
1423 /* we could check the contents but there really isn't any point */
1424 conn_set_udpok(c);
1425
1426 return 0;
1427 }
1428
1429 static int _client_fileinforeq(t_connection * c, t_packet const *const packet)
1430 {
1431 t_packet *rpacket;
1432
1433 if (packet_get_size(packet) < sizeof(t_client_fileinforeq)) {
1434 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad FILEINFOREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_fileinforeq), packet_get_size(packet));
1435 return -1;
1436 }
1437
1438 {
1439 char const *filename;
1440
1441 if (!(filename = packet_get_str_const(packet, sizeof(t_client_fileinforeq), MAX_FILENAME_STR))) {
1442 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad FILEINFOREQ packet (missing or too long tosfile)", conn_get_socket(c));
1443 return -1;
1444 }
1445 eventlog(eventlog_level_info, __FUNCTION__, "[{}] file requested: \"{}\" - type = 0x{:02x}", conn_get_socket(c), filename, bn_int_get(packet->u.client_fileinforeq.type));
1446
1447 /* TODO: if type is TOSFILE make bnetd to send default tosfile if selected is not found */
1448 if ((rpacket = packet_create(packet_class_bnet))) {
1449 packet_set_size(rpacket, sizeof(t_server_fileinforeply));
1450 packet_set_type(rpacket, SERVER_FILEINFOREPLY);
1451 bn_int_set(&rpacket->u.server_fileinforeply.type, bn_int_get(packet->u.client_fileinforeq.type));
1452 bn_int_set(&rpacket->u.server_fileinforeply.unknown2, bn_int_get(packet->u.client_fileinforeq.unknown2));
1453 /* Note from Sherpya:
1454 * timestamp -> 0x852b7d00 - 0x01c0e863 b.net send this (bn_int),
1455 * I suppose is not a long
1456 * if bnserver-D2DV is bad diablo 2 crashes
1457 * timestamp doesn't work correctly and starcraft
1458 * needs name in client locale or displays hostname
1459 */
1460 file_to_mod_time(c, filename, &rpacket->u.server_fileinforeply.timestamp);
1461 packet_append_string(rpacket, filename);
1462 conn_push_outqueue(c, rpacket);
1463 packet_del_ref(rpacket);
1464 }
1465 }
1466
1467 return 0;
1468 }
1469
1470 static const char *_attribute_req(t_account * reqacc, t_account * myacc, const char *key)
1471 {
1472 const char *result = "";
1473 const char *tval;
1474
1475 if (!reqacc)
1476 goto out;
1477 if (reqacc != myacc && !strncasecmp(key, "BNET", 4))
1478 goto out;
1479
1480 tval = account_get_strattr(reqacc, key);
1481 if (tval)
1482 result = tval;
1483
1484 out:
1485 return result;
1486 }
1487
1488 static int _client_statsreq(t_connection * c, t_packet const *const packet)
1489 {
1490 t_packet *rpacket;
1491 char const *name;
1492 char const *key;
1493 unsigned int name_count;
1494 unsigned int key_count;
1495 unsigned int i, j;
1496 unsigned int name_off;
1497 unsigned int keys_off;
1498 unsigned int key_off;
1499 t_account *reqacc, *myacc;
1500
1501 if (packet_get_size(packet) < sizeof(t_client_statsreq)) {
1502 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STATSREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_statsreq), packet_get_size(packet));
1503 return -1;
1504 }
1505
1506 name_count = bn_int_get(packet->u.client_statsreq.name_count);
1507 key_count = bn_int_get(packet->u.client_statsreq.key_count);
1508
1509 for (i = 0, name_off = sizeof(t_client_statsreq); i < name_count && (name = packet_get_str_const(packet, name_off, UNCHECKED_NAME_STR)); i++, name_off += std::strlen(name) + 1);
1510
1511 if (i < name_count) {
1512 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STATSREQ packet (only {} names of {})", conn_get_socket(c), i, name_count);
1513 return -1;
1514 }
1515 keys_off = name_off;
1516
1517 if (!(rpacket = packet_create(packet_class_bnet)))
1518 return -1;
1519
1520 packet_set_size(rpacket, sizeof(t_server_statsreply));
1521 packet_set_type(rpacket, SERVER_STATSREPLY);
1522 bn_int_set(&rpacket->u.server_statsreply.name_count, name_count);
1523 bn_int_set(&rpacket->u.server_statsreply.key_count, key_count);
1524 bn_int_set(&rpacket->u.server_statsreply.requestid, bn_int_get(packet->u.client_statsreq.requestid));
1525
1526 myacc = conn_get_account(c);
1527
1528 for (i = 0, name_off = sizeof(t_client_statsreq); i < name_count && (name = packet_get_str_const(packet, name_off, UNCHECKED_NAME_STR)); i++, name_off += std::strlen(name) + 1) {
1529 reqacc = accountlist_find_account(name);
1530 if (!reqacc)
1531 reqacc = myacc;
1532
1533 for (j = 0, key_off = keys_off; j < key_count && (key = packet_get_str_const(packet, key_off, MAX_ATTRKEY_STR)); j++, key_off += std::strlen(key) + 1) {
1534 if (*key == '\0')
1535 continue;
1536 packet_append_string(rpacket, _attribute_req(reqacc, myacc, key));
1537 }
1538 }
1539
1540 conn_push_outqueue(c, rpacket);
1541 packet_del_ref(rpacket);
1542
1543 return 0;
1544 }
1545
1546 static int _client_loginreq1(t_connection * c, t_packet const *const packet)
1547 {
1548 if (packet_get_size(packet) < sizeof(t_client_loginreq1)) {
1549 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad LOGINREQ1 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_loginreq1), packet_get_size(packet));
1550 return -1;
1551 }
1552
1553 {
1554 char const *username;
1555 t_account *account;
1556
1557 if (!(username = packet_get_str_const(packet, sizeof(t_client_loginreq1), MAX_USERNAME_LEN))) {
1558 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad LOGINREQ1 (missing or too long username)", conn_get_socket(c));
1559 return -1;
1560 }
1561
1562 t_packet* const rpacket = packet_create(packet_class_bnet);
1563 if (!rpacket)
1564 {
1565 return -1;
1566 }
1567
1568 packet_set_size(rpacket, sizeof(t_server_loginreply1));
1569 packet_set_type(rpacket, SERVER_LOGINREPLY1);
1570
1571 // too many logins? [added by NonReal]
1572 if (prefs_get_max_concurrent_logins() > 0)
1573 {
1574 if (prefs_get_max_concurrent_logins() <= connlist_login_get_length())
1575 {
1576 eventlog(eventlog_level_error, __FUNCTION__, "[{}] login denied, too many concurrent logins. max: {}. current: {}.", conn_get_socket(c), prefs_get_max_concurrent_logins(), connlist_login_get_length());
1577 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_FAIL);
1578 conn_push_outqueue(c, rpacket);
1579 packet_del_ref(rpacket);
1580 return -1;
1581 }
1582 }
1583
1584 /* fail if no account */
1585 if (!(account = accountlist_find_account(username))) {
1586 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (no such account)", conn_get_socket(c), username);
1587 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_FAIL);
1588 }
1589 else
1590 /* already logged in */
1591 if (connlist_find_connection_by_account(account) && prefs_get_kick_old_login() == 0) {
1592 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (already logged in)", conn_get_socket(c), username);
1593 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_FAIL);
1594 }
1595 else if (account_get_auth_bnetlogin(account) == 0) { /* default to true */
1596 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (no bnet access)", conn_get_socket(c), username);
1597 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_FAIL);
1598 }
1599 else if (account_get_auth_lock(account) == 1) { /* default to false */
1600 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (this account is locked)", conn_get_socket(c), username);
1601 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_FAIL);
1602 }
1603 else if (conn_get_sessionkey(c) != bn_int_get(packet->u.client_loginreq1.sessionkey)) {
1604 eventlog(eventlog_level_error, __FUNCTION__, "[{}] login for \"{}\" refused (expected session key 0x{:08x}, got 0x{:08x})", conn_get_socket(c), username, conn_get_sessionkey(c), bn_int_get(packet->u.client_loginreq1.sessionkey));
1605 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_FAIL);
1606 }
1607 else {
1608 struct {
1609 bn_int ticks;
1610 bn_int sessionkey;
1611 bn_int passhash1[5];
1612 } temp;
1613 char const *oldstrhash1;
1614 t_hash oldpasshash1;
1615 t_hash oldpasshash2;
1616 t_hash trypasshash2;
1617
1618 if ((oldstrhash1 = account_get_pass(account))) {
1619 bn_int_set(&temp.ticks, bn_int_get(packet->u.client_loginreq1.ticks));
1620 bn_int_set(&temp.sessionkey, bn_int_get(packet->u.client_loginreq1.sessionkey));
1621 if (hash_set_str(&oldpasshash1, oldstrhash1) < 0) {
1622 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (corrupted passhash1?)", conn_get_socket(c), username);
1623 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_FAIL);
1624 }
1625 else {
1626 hash_to_bnhash((t_hash const *)&oldpasshash1, temp.passhash1); /* avoid warning */
1627
1628 bnet_hash(&oldpasshash2, sizeof(temp), &temp); /* do the double hash */
1629 bnhash_to_hash(packet->u.client_loginreq1.password_hash2, &trypasshash2);
1630
1631 if (hash_eq(trypasshash2, oldpasshash2) == 1) {
1632 conn_login(c, account, username);
1633 eventlog(eventlog_level_info, __FUNCTION__, "[{}] \"{}\" logged in (correct password)", conn_get_socket(c), username);
1634 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_SUCCESS);
1635 #ifdef WITH_LUA
1636 if (lua_handle_user(c, NULL, NULL, luaevent_user_login) == 1)
1637 {
1638 // feature to break login from Lua
1639 conn_set_state(c, conn_state_destroy);
1640 return -1;
1641 }
1642 #endif
1643 #ifdef WIN32_GUI
1644 guiOnUpdateUserList();
1645 #endif
1646 }
1647 else {
1648 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (wrong password)", conn_get_socket(c), username);
1649 conn_increment_passfail_count(c);
1650 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_FAIL);
1651 }
1652 }
1653 }
1654 else {
1655 conn_login(c, account, username);
1656 eventlog(eventlog_level_info, __FUNCTION__, "[{}] \"{}\" logged in (no password)", conn_get_socket(c), username);
1657 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY1_MESSAGE_SUCCESS);
1658 #ifdef WIN32_GUI
1659 guiOnUpdateUserList();
1660 #endif
1661 }
1662 }
1663 conn_push_outqueue(c, rpacket);
1664 packet_del_ref(rpacket);
1665 }
1666
1667 return 0;
1668 }
1669
1670 void client_init_email(t_connection * c, t_account * account)
1671 {
1672 t_packet *packet;
1673 char const *email;
1674
1675 if (!c || !account)
1676 return;
1677 if (!(email = account_get_email(account))) {
1678 if ((packet = packet_create(packet_class_bnet))) {
1679 packet_set_size(packet, sizeof(t_server_setemailreq));
1680 packet_set_type(packet, SERVER_SETEMAILREQ);
1681 conn_push_outqueue(c, packet);
1682 packet_del_ref(packet);
1683 }
1684 }
1685 return;
1686 }
1687
1688 static int _client_loginreq2(t_connection * c, t_packet const *const packet)
1689 {
1690 t_packet *rpacket;
1691 int success = 0;
1692
1693 if (packet_get_size(packet) < sizeof(t_client_loginreq2)) {
1694 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad LOGINREQ2 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_loginreq2), packet_get_size(packet));
1695 return -1;
1696 }
1697
1698 {
1699 char const *username;
1700 t_account *account;
1701 char supports_locked_reply = 0;
1702 t_clienttag clienttag = conn_get_clienttag(c);
1703
1704 if (clienttag == CLIENTTAG_STARCRAFT_UINT || clienttag == CLIENTTAG_BROODWARS_UINT || clienttag == CLIENTTAG_SHAREWARE_UINT ||
1705 clienttag == CLIENTTAG_DIABLORTL_UINT || clienttag == CLIENTTAG_DIABLOSHR_UINT || clienttag == CLIENTTAG_WARCIIBNE_UINT ||
1706 clienttag == CLIENTTAG_DIABLO2DV_UINT || clienttag == CLIENTTAG_STARJAPAN_UINT || clienttag == CLIENTTAG_DIABLO2ST_UINT ||
1707 clienttag == CLIENTTAG_DIABLO2XP_UINT || clienttag == CLIENTTAG_WARCRAFT3_UINT || clienttag == CLIENTTAG_WAR3XP_UINT )
1708 {
1709 if (conn_get_versionid(c) >= 0x0000000b)
1710 supports_locked_reply = 1;
1711 }
1712
1713 if (!(username = packet_get_str_const(packet, sizeof(t_client_loginreq2), MAX_USERNAME_LEN))) {
1714 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad LOGINREQ2 (missing or too long username)", conn_get_socket(c));
1715 return -1;
1716 }
1717
1718 if (!(rpacket = packet_create(packet_class_bnet)))
1719 return -1;
1720 packet_set_size(rpacket, sizeof(t_server_loginreply2));
1721 packet_set_type(rpacket, SERVER_LOGINREPLY2);
1722
1723 // too many logins? [added by NonReal]
1724 if (prefs_get_max_concurrent_logins() > 0) {
1725 if (prefs_get_max_concurrent_logins() <= connlist_login_get_length()) {
1726 eventlog(eventlog_level_error, __FUNCTION__, "[{}] login denied, too many concurrent logins. max: {}. current: {}.", conn_get_socket(c), prefs_get_max_concurrent_logins(), connlist_login_get_length());
1727 if (supports_locked_reply) {
1728 bn_int_set(&rpacket->u.server_loginreply2.message, SERVER_LOGINREPLY2_MESSAGE_LOCKED);
1729 packet_append_string(rpacket, "Too many concurrent logins. Try again later.");
1730 }
1731 else {
1732 bn_int_set(&rpacket->u.server_loginreply2.message, SERVER_LOGINREPLY2_MESSAGE_BADPASS);
1733 }
1734
1735 packet_del_ref(rpacket);
1736 return -1;
1737 }
1738 }
1739
1740 /* fail if no account */
1741 if (!(account = accountlist_find_account(username))) {
1742 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (no such account)", conn_get_socket(c), username);
1743 bn_int_set(&rpacket->u.server_loginreply2.message, SERVER_LOGINREPLY2_MESSAGE_NONEXIST);
1744 }
1745 /* already logged in */
1746 else if (connlist_find_connection_by_account(account) && prefs_get_kick_old_login() == 0) {
1747 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (already logged in)", conn_get_socket(c), username);
1748 if (supports_locked_reply) {
1749 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY2_MESSAGE_LOCKED);
1750 packet_append_string(rpacket, "This account is already logged in.");
1751 }
1752 else {
1753 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY2_MESSAGE_BADPASS);
1754 }
1755 }
1756 else if (account_get_auth_bnetlogin(account) == 0) { /* default to true */
1757 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (no bnet access)", conn_get_socket(c), username);
1758 if (supports_locked_reply) {
1759 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY2_MESSAGE_LOCKED);
1760 packet_append_string(rpacket, "This account is barred from bnet access.");
1761 }
1762 else {
1763 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY2_MESSAGE_BADPASS);
1764 }
1765 }
1766 else if (account_get_auth_lock(account) == 1) { /* default to false */
1767 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (this account is locked)", conn_get_socket(c), username);
1768 if (supports_locked_reply)
1769 {
1770 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY2_MESSAGE_LOCKED);
1771 std::string msgtemp = localize(c, "This account has been locked");
1772 msgtemp += account_get_locktext(c, account, true);
1773 packet_append_string(rpacket, msgtemp.c_str());
1774 }
1775 else {
1776 bn_int_set(&rpacket->u.server_loginreply1.message, SERVER_LOGINREPLY2_MESSAGE_BADPASS);
1777 }
1778 }
1779 else if (conn_get_sessionkey(c) != bn_int_get(packet->u.client_loginreq2.sessionkey)) {
1780 eventlog(eventlog_level_error, __FUNCTION__, "[{}] login for \"{}\" refused (expected session key 0x{:08x}, got 0x{:08x})", conn_get_socket(c), username, conn_get_sessionkey(c), bn_int_get(packet->u.client_loginreq2.sessionkey));
1781 bn_int_set(&rpacket->u.server_loginreply2.message, SERVER_LOGINREPLY2_MESSAGE_BADPASS);
1782 }
1783 else {
1784 struct {
1785 bn_int ticks;
1786 bn_int sessionkey;
1787 bn_int passhash1[5];
1788 } temp;
1789 char const *oldstrhash1;
1790 t_hash oldpasshash1;
1791 t_hash oldpasshash2;
1792 t_hash trypasshash2;
1793
1794 if ((oldstrhash1 = account_get_pass(account))) {
1795 bn_int_set(&temp.ticks, bn_int_get(packet->u.client_loginreq2.ticks));
1796 bn_int_set(&temp.sessionkey, bn_int_get(packet->u.client_loginreq2.sessionkey));
1797 if (hash_set_str(&oldpasshash1, oldstrhash1) < 0) {
1798 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (corrupted passhash1?)", conn_get_socket(c), username);
1799 bn_int_set(&rpacket->u.server_loginreply2.message, SERVER_LOGINREPLY2_MESSAGE_BADPASS);
1800 }
1801 else {
1802 hash_to_bnhash((t_hash const *)&oldpasshash1, temp.passhash1); /* avoid warning */
1803
1804 bnet_hash(&oldpasshash2, sizeof(temp), &temp); /* do the double hash */
1805 bnhash_to_hash(packet->u.client_loginreq2.password_hash2, &trypasshash2);
1806
1807 if (hash_eq(trypasshash2, oldpasshash2) == 1) {
1808 conn_login(c, account, username);
1809 eventlog(eventlog_level_info, __FUNCTION__, "[{}] \"{}\" logged in (correct password)", conn_get_socket(c), username);
1810 bn_int_set(&rpacket->u.server_loginreply2.message, SERVER_LOGINREPLY2_MESSAGE_SUCCESS);
1811 success = 1;
1812 }
1813 else {
1814 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (wrong password)", conn_get_socket(c), username);
1815 conn_increment_passfail_count(c);
1816 bn_int_set(&rpacket->u.server_loginreply2.message, SERVER_LOGINREPLY2_MESSAGE_BADPASS);
1817 }
1818 }
1819 }
1820 else {
1821 conn_login(c, account, username);
1822 eventlog(eventlog_level_info, __FUNCTION__, "[{}] \"{}\" logged in (no password)", conn_get_socket(c), username);
1823 bn_int_set(&rpacket->u.server_loginreply2.message, SERVER_LOGINREPLY2_MESSAGE_SUCCESS);
1824 success = 1;
1825 }
1826 }
1827 if (success && account) {
1828
1829 #ifdef WITH_LUA
1830 if (lua_handle_user(c, NULL, NULL, luaevent_user_login) == 1)
1831 {
1832 // feature to break login from Lua
1833 conn_set_state(c, conn_state_destroy);
1834 packet_del_ref(rpacket);
1835 return -1;
1836 }
1837 #endif
1838
1839 #ifdef WIN32_GUI
1840 guiOnUpdateUserList();
1841 #endif
1842 client_init_email(c, account);
1843 }
1844
1845 conn_push_outqueue(c, rpacket);
1846 packet_del_ref(rpacket);
1847 }
1848
1849 return 0;
1850 }
1851
1852 static int _client_loginreqw3(t_connection * c, t_packet const *const packet)
1853 {
1854 t_packet *rpacket;
1855
1856 if (packet_get_size(packet) < sizeof(t_client_loginreq_w3)) {
1857 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_LOGINREQ_W3 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_loginreq_w3), packet_get_size(packet));
1858 return -1;
1859 }
1860
1861 {
1862 char const *username;
1863 t_account *account;
1864 char const *account_salt;
1865 char const *account_verifier;
1866 const char *conn_client_public_key;
1867 int i;
1868
1869 /* PELISH: Does not need to check conn_client_public_key != NULL because we testing packet size */
1870 conn_client_public_key = (char *)packet_get_data_const(packet, offsetof(t_client_loginreq_w3, client_public_key), 32);
1871
1872
1873 if (!(username = packet_get_str_const(packet, sizeof(t_client_loginreq_w3), MAX_USERNAME_LEN))) {
1874 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_LOGINREQ_W3 (missing or too long username)", conn_get_socket(c));
1875 return -1;
1876 }
1877
1878 if (!(rpacket = packet_create(packet_class_bnet)))
1879 return -1;
1880 packet_set_size(rpacket, sizeof(t_server_loginreply_w3));
1881 packet_set_type(rpacket, SERVER_LOGINREPLY_W3);
1882
1883 for (i = 0; i < 32; i++)
1884 bn_byte_set(&rpacket->u.server_loginreply_w3.salt[i], 0);
1885
1886 for (i = 0; i < 32; i++)
1887 bn_byte_set(&rpacket->u.server_loginreply_w3.server_public_key[i], 0);
1888
1889 {
1890 /* too many logins? */
1891 if (prefs_get_max_concurrent_logins() > 0 && prefs_get_max_concurrent_logins() <= connlist_login_get_length()) {
1892 eventlog(eventlog_level_error, __FUNCTION__, "[{}] login denied, too many concurrent logins. max: {}. current: {}.", conn_get_socket(c), prefs_get_max_concurrent_logins(), connlist_login_get_length());
1893 bn_int_set(&rpacket->u.server_loginreply_w3.message, SERVER_LOGINREPLY_W3_MESSAGE_BADACCT);
1894 }
1895 else
1896 /* fail if no account */
1897 if (!(account = accountlist_find_account(username))) {
1898 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) login for \"{}\" refused (no such account)", conn_get_socket(c), username);
1899 bn_int_set(&rpacket->u.server_loginreply_w3.message, SERVER_LOGINREPLY_W3_MESSAGE_BADACCT);
1900 }
1901 else
1902 /* already logged in */
1903 if (connlist_find_connection_by_account(account) && prefs_get_kick_old_login() == 0) {
1904 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) login for \"{}\" refused (already logged in)", conn_get_socket(c), username);
1905 bn_int_set(&rpacket->u.server_loginreply_w3.message, SERVER_LOGINREPLY_W3_MESSAGE_ALREADY);
1906 }
1907 else if (account_get_auth_bnetlogin(account) == 0) { /* default to true */
1908 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) login for \"{}\" refused (no bnet access)", conn_get_socket(c), username);
1909 bn_int_set(&rpacket->u.server_loginreply_w3.message, SERVER_LOGINREPLY_W3_MESSAGE_BADACCT);
1910 }
1911 else if (!(account_salt = account_get_salt(account))) {
1912 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) \"{}\" passed account check (even though account has no salt)", conn_get_socket(c), username);
1913 conn_set_loggeduser(c, username);
1914 bn_int_set(&rpacket->u.server_loginreply_w3.message, SERVER_LOGINREPLY_W3_MESSAGE_SUCCESS);
1915 }
1916 else if (!(account_verifier = account_get_verifier(account))) {
1917 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) \"{}\" passed account check (even though account has no verifier)", conn_get_socket(c), username);
1918 conn_set_loggeduser(c, username);
1919 bn_int_set(&rpacket->u.server_loginreply_w3.message, SERVER_LOGINREPLY_W3_MESSAGE_SUCCESS);
1920 xfree((void*)account_salt);
1921 }
1922 else {
1923
1924 for (i = 0; i < 32; i++){
1925 bn_byte_set(&rpacket->u.server_loginreply_w3.salt[i], account_salt[i]);
1926 }
1927
1928 BigInt salt = BigInt((unsigned char*)account_salt, 32, 4, false);
1929 BigInt verifier = BigInt((unsigned char*)account_verifier, 32, 1, false);
1930 BnetSRP3 srp3 = BnetSRP3(username, salt);
1931
1932 BigInt client_public_key = BigInt((unsigned char*)conn_client_public_key, 32, 1, false);
1933 BigInt server_public_key = srp3.getServerSessionPublicKey(verifier);
1934
1935 server_public_key.getData((unsigned char*)&rpacket->u.server_loginreply_w3.server_public_key, 32, 4, false);
1936
1937 BigInt hashed_server_secret_ = srp3.getHashedServerSecret(client_public_key, verifier);
1938 BigInt client_proof = srp3.getClientPasswordProof(client_public_key, server_public_key, hashed_server_secret_);
1939 BigInt server_proof = srp3.getServerPasswordProof(client_public_key, client_proof, hashed_server_secret_);
1940
1941 char * conn_client_proof = (char*)client_proof.getData(20, 4, false);
1942 char * conn_server_proof = (char*)server_proof.getData(20, 4, false);
1943
1944 conn_set_client_proof(c, conn_client_proof);
1945 conn_set_server_proof(c, conn_server_proof);
1946
1947 xfree((void*)account_verifier);
1948 xfree((void*)account_salt);
1949 xfree(conn_client_proof);
1950 xfree(conn_server_proof);
1951
1952 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) \"{}\" passed account check", conn_get_socket(c), username);
1953 conn_set_loggeduser(c, username);
1954 bn_int_set(&rpacket->u.server_loginreply_w3.message, SERVER_LOGINREPLY_W3_MESSAGE_SUCCESS);
1955 }
1956 }
1957
1958 conn_push_outqueue(c, rpacket);
1959 packet_del_ref(rpacket);
1960
1961 }
1962
1963 return 0;
1964 }
1965
1966 static int _client_passchangereq(t_connection * c, t_packet const *const packet)
1967 {
1968 t_packet *rpacket;
1969
1970 if (packet_get_size(packet) < sizeof(t_client_passchangereq)) {
1971 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_PASSCHANGEREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_passchangereq), packet_get_size(packet));
1972 return -1;
1973 }
1974
1975 {
1976 char const *username;
1977 t_account *account;
1978 char const *account_salt;
1979 char const *account_verifier;
1980 const char *conn_client_public_key;
1981 int i;
1982
1983 /* PELISH: Does not need to check conn_client_public_key != NULL because we testing packet size */
1984 conn_client_public_key = (char *)packet_get_data_const(packet, offsetof(t_client_loginreq_w3, client_public_key), 32);
1985
1986
1987 if (!(username = packet_get_str_const(packet, sizeof(t_client_passchangereq), MAX_USERNAME_LEN))) {
1988 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_PASSCHANGEREQ (missing or too long username)", conn_get_socket(c));
1989 return -1;
1990 }
1991
1992 if (!(rpacket = packet_create(packet_class_bnet)))
1993 return -1;
1994 packet_set_size(rpacket, sizeof(t_server_passchangereply));
1995 packet_set_type(rpacket, SERVER_PASSCHANGEREPLY);
1996
1997 for (i = 0; i < 32; i++)
1998 bn_byte_set(&rpacket->u.server_loginreply_w3.salt[i], 0);
1999
2000 for (i = 0; i < 32; i++)
2001 bn_byte_set(&rpacket->u.server_loginreply_w3.server_public_key[i], 0);
2002
2003 {
2004 bn_int_set(&rpacket->u.server_passchangereply.message, SERVER_PASSCHANGEREPLY_MESSAGE_REJECT);
2005
2006 /* fail if no account */
2007 if (!(account = accountlist_find_account(username))) {
2008 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) passchange for \"{}\" refused (no such account)", conn_get_socket(c), username);
2009 }
2010 else
2011 /* fail if bnetlogin is disallowed */
2012 if (account_get_auth_bnetlogin(account) == 0) { /* default to true */
2013 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) passchange for \"{}\" refused (no bnet access)", conn_get_socket(c), username);
2014 }
2015 else
2016 /* fail if no salt */
2017 if ((account_salt = account_get_salt(account)) == NULL) {
2018 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) passchange for \"{}\" refused (no SALT)", conn_get_socket(c), username);
2019 }
2020 else
2021 /* fail if no verifier */
2022 if ((account_verifier = account_get_verifier(account)) == NULL) {
2023 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) passchange for \"{}\" refused (no VERIFIER)", conn_get_socket(c), username);
2024 xfree((void*)account_salt);
2025 }
2026 else {
2027
2028 for (i = 0; i < 32; i++){
2029 bn_byte_set(&rpacket->u.server_passchangereply.salt[i], account_salt[i]);
2030 }
2031
2032 BigInt salt = BigInt((unsigned char*)account_salt, 32, 4, false);
2033 BigInt verifier = BigInt((unsigned char*)account_verifier, 32, 1, false);
2034 BnetSRP3 srp3 = BnetSRP3(username, salt);
2035
2036 BigInt client_public_key = BigInt((unsigned char*)conn_client_public_key, 32, 1, false);
2037 BigInt server_public_key = srp3.getServerSessionPublicKey(verifier);
2038
2039 server_public_key.getData((unsigned char*)&rpacket->u.server_loginreply_w3.server_public_key, 32, 4, false);
2040
2041 BigInt hashed_server_secret_ = srp3.getHashedServerSecret(client_public_key, verifier);
2042 BigInt client_proof = srp3.getClientPasswordProof(client_public_key, server_public_key, hashed_server_secret_);
2043 BigInt server_proof = srp3.getServerPasswordProof(client_public_key, client_proof, hashed_server_secret_);
2044
2045 char * conn_client_proof = (char*)client_proof.getData(20, 4, false);
2046 char * conn_server_proof = (char*)server_proof.getData(20, 4, false);
2047
2048 conn_set_client_proof(c, conn_client_proof);
2049 conn_set_server_proof(c, conn_server_proof);
2050
2051 xfree((void*)account_verifier);
2052 xfree((void*)account_salt);
2053 xfree(conn_client_proof);
2054 xfree(conn_server_proof);
2055
2056 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) \"{}\" passed account passchange check", conn_get_socket(c), username);
2057 conn_set_loggeduser(c, username);
2058 bn_int_set(&rpacket->u.server_passchangereply.message, SERVER_PASSCHANGEREPLY_MESSAGE_ACCEPT);
2059 }
2060 }
2061
2062 conn_push_outqueue(c, rpacket);
2063 packet_del_ref(rpacket);
2064
2065 }
2066
2067 return 0;
2068 }
2069
2070 static int _client_passchangeproofreq(t_connection * c, t_packet const *const packet)
2071 {
2072 t_packet *rpacket;
2073
2074 if (packet_get_size(packet) < sizeof(t_client_passchangeproofreq)) {
2075 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad PASSCHANGEPROOFREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_passchangeproofreq), packet_get_size(packet));
2076 return -1;
2077 }
2078
2079 {
2080 char const *username;
2081 t_account *account;
2082 const char *salt;
2083 const char *verifier;
2084
2085 eventlog(eventlog_level_info, __FUNCTION__, "[{}] passchange proof requested", conn_get_socket(c));
2086
2087 if (!(rpacket = packet_create(packet_class_bnet)))
2088 return -1;
2089 packet_set_size(rpacket, sizeof(t_server_passchangeproofreply));
2090 packet_set_type(rpacket, SERVER_PASSCHANGEPROOFREPLY);
2091
2092 std::memset(&rpacket->u.server_passchangeproofreply.server_password_proof, 0, sizeof(rpacket->u.server_passchangeproofreply.server_password_proof));
2093
2094 bn_int_set(&rpacket->u.server_logonproofreply.response, SERVER_PASSCHANGEPROOFREPLY_RESPONSE_BADPASS);
2095
2096 if (!(username = conn_get_loggeduser(c))) {
2097 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) got NULL username, 0x56ff before 0x55ff?", conn_get_socket(c));
2098 }
2099 else if (!(account = accountlist_find_account(username))) {
2100 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) passchange in 0x56ff for \"{}\" refused (no such account)", conn_get_socket(c), username);
2101 }
2102 else if (account_get_auth_lock(account) == 1) { /* default to false */
2103 eventlog(eventlog_level_info, __FUNCTION__, "[{}] passchange for \"{}\" refused (this account is locked)", conn_get_socket(c), username);
2104 }
2105 else {
2106 int i;
2107 const char * client_password_proof;
2108
2109 if (!(client_password_proof = (const char*)packet_get_data_const(packet, offsetof(t_client_passchangeproofreq, client_password_proof), 20))) {
2110 eventlog(eventlog_level_error, __FUNCTION__, "[{}] (W3) got bad PASSCHANGEPROOFREQ packet (missing hash)", conn_get_socket(c));
2111 packet_del_ref(rpacket);
2112 return -1;
2113 }
2114
2115 if (std::memcmp(client_password_proof, conn_get_client_proof(c), 20) == 0) {
2116 const char * server_proof = conn_get_server_proof(c);
2117
2118 for (i = 0; i < 20; i++){
2119 bn_byte_set(&rpacket->u.server_passchangeproofreply.server_password_proof[i], server_proof[i]);
2120 }
2121
2122 salt = (const char *)packet_get_data_const(packet, offsetof(t_client_passchangeproofreq, salt), 32);
2123 verifier = (const char *)packet_get_data_const(packet, offsetof(t_client_passchangeproofreq, password_verifier), 32);
2124 account_set_salt(account, salt);
2125 account_set_verifier(account, verifier);
2126
2127 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) \"{}\" successful passchange (right client password proof)", conn_get_socket(c), username);
2128 bn_int_set(&rpacket->u.server_passchangeproofreply.response, SERVER_PASSCHANGEPROOFREPLY_RESPONSE_OK);
2129 }
2130 else {
2131 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) got wrong client password proof for \"{}\"", conn_get_socket(c), username);
2132 conn_increment_passfail_count(c);
2133 }
2134 }
2135 conn_push_outqueue(c, rpacket);
2136 packet_del_ref(rpacket);
2137 conn_set_client_proof(c, NULL);
2138 conn_set_server_proof(c, NULL);
2139 }
2140
2141 return 0;
2142 }
2143
2144 static int _client_pingreq(t_connection * c, t_packet const *const packet)
2145 {
2146 t_packet *rpacket;
2147
2148 if (packet_get_size(packet) < sizeof(t_client_pingreq)) {
2149 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad PINGREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_pingreq), packet_get_size(packet));
2150 return -1;
2151 }
2152
2153 if ((rpacket = packet_create(packet_class_bnet))) {
2154 packet_set_size(rpacket, sizeof(t_server_pingreply));
2155 packet_set_type(rpacket, SERVER_PINGREPLY);
2156 conn_push_outqueue(c, rpacket);
2157 packet_del_ref(rpacket);
2158 }
2159
2160 return 0;
2161 }
2162
2163 static int _client_logonproofreq(t_connection * c, t_packet const *const packet)
2164 {
2165 t_packet *rpacket;
2166
2167 if (packet_get_size(packet) < sizeof(t_client_logonproofreq)) {
2168 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad LOGONPROOFREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_logonproofreq), packet_get_size(packet));
2169 return -1;
2170 }
2171
2172 {
2173 char const *username;
2174 t_account *account;
2175
2176 eventlog(eventlog_level_info, __FUNCTION__, "[{}] logon proof requested", conn_get_socket(c));
2177
2178 if (!(rpacket = packet_create(packet_class_bnet)))
2179 return -1;
2180 packet_set_size(rpacket, sizeof(t_server_logonproofreply));
2181 packet_set_type(rpacket, SERVER_LOGONPROOFREPLY);
2182
2183 std::memset(&rpacket->u.server_logonproofreply.server_password_proof, 0, sizeof(rpacket->u.server_logonproofreply.server_password_proof));
2184
2185 bn_int_set(&rpacket->u.server_logonproofreply.response, SERVER_LOGONPROOFREPLY_RESPONSE_BADPASS);
2186
2187 if (!(username = conn_get_loggeduser(c))) {
2188 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) got NULL username, 0x54ff before 0x53ff?", conn_get_socket(c));
2189 }
2190 else if (!(account = accountlist_find_account(username))) {
2191 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) login in 0x54ff for \"{}\" refused (no such account)", conn_get_socket(c), username);
2192 }
2193 else if (account_get_auth_lock(account) == 1) { /* default to false */
2194 eventlog(eventlog_level_info, __FUNCTION__, "[{}] login for \"{}\" refused (this account is locked)", conn_get_socket(c), username);
2195 bn_int_set(&rpacket->u.server_logonproofreply.response, SERVER_LOGONPROOFREPLY_RESPONSE_CUSTOM);
2196 std::string msgtemp = localize(c, "This account has been locked");
2197 msgtemp += account_get_locktext(c, account, true);
2198 packet_append_string(rpacket, msgtemp.c_str());
2199 }
2200 else {
2201 t_hash serverhash;
2202 t_hash clienthash;
2203 int i;
2204 const char * client_password_proof;
2205
2206 /* PELISH: This can not occur - We already tested packet size which must be wrong firstly.
2207 Also pvpgn will crash when will dereferencing NULL pointer (so we cant got this errorlog message)
2208 I vote for deleting this "if" */
2209 if (!(client_password_proof = (const char*)packet_get_data_const(packet, offsetof(t_client_logonproofreq, client_password_proof), 20))) {
2210 eventlog(eventlog_level_error, __FUNCTION__, "[{}] (W3) got bad LOGONPROOFREQ packet (missing hash)", conn_get_socket(c));
2211 packet_del_ref(rpacket);
2212 return -1;
2213 }
2214
2215 clienthash[0] = bn_int_get(packet->u.client_logonproofreq.client_password_proof[0]);
2216 clienthash[1] = bn_int_get(packet->u.client_logonproofreq.client_password_proof[4]);
2217 clienthash[2] = bn_int_get(packet->u.client_logonproofreq.client_password_proof[8]);
2218 clienthash[3] = bn_int_get(packet->u.client_logonproofreq.client_password_proof[12]);
2219 clienthash[4] = bn_int_get(packet->u.client_logonproofreq.client_password_proof[16]);
2220
2221 hash_set_str(&serverhash, account_get_pass(account));
2222
2223 if (conn_get_client_proof(c) && std::memcmp(client_password_proof, conn_get_client_proof(c), 20) == 0) {
2224 const char * server_proof = conn_get_server_proof(c);
2225
2226 for (i = 0; i < 20; i++){
2227 bn_byte_set(&rpacket->u.server_logonproofreply.server_password_proof[i], server_proof[i]);
2228 }
2229
2230 conn_login(c, account, username);
2231 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) \"{}\" logged in (right client password proof)", conn_get_socket(c), username);
2232 if ((conn_get_versionid(c) >= 0x0000000D) && (account_get_email(account) == NULL))
2233 bn_int_set(&rpacket->u.server_logonproofreply.response, SERVER_LOGONPROOFREPLY_RESPONSE_EMAIL);
2234 else
2235 bn_int_set(&rpacket->u.server_logonproofreply.response, SERVER_LOGONPROOFREPLY_RESPONSE_OK);
2236 // by amadeo updates the userlist
2237 #ifdef WIN32_GUI
2238 guiOnUpdateUserList();
2239 #endif
2240 }
2241 else if (hash_eq(clienthash, serverhash)) {
2242
2243 conn_login(c, account, username);
2244 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) \"{}\" logged in (right password)", conn_get_socket(c), username);
2245 if ((conn_get_versionid(c) >= 0x0000000D) && (account_get_email(account) == NULL))
2246 bn_int_set(&rpacket->u.server_logonproofreply.response, SERVER_LOGONPROOFREPLY_RESPONSE_EMAIL);
2247 else
2248 bn_int_set(&rpacket->u.server_logonproofreply.response, SERVER_LOGONPROOFREPLY_RESPONSE_OK);
2249 #ifdef WITH_LUA
2250 if (lua_handle_user(c, NULL, NULL, luaevent_user_login) == 1)
2251 {
2252 // feature to break login from Lua
2253 conn_set_state(c, conn_state_destroy);
2254 packet_del_ref(rpacket);
2255 return -1;
2256 }
2257 #endif
2258 // by amadeo updates the userlist
2259 #ifdef WIN32_GUI
2260 guiOnUpdateUserList();
2261 #endif
2262
2263 }
2264 else {
2265 eventlog(eventlog_level_info, __FUNCTION__, "[{}] (W3) got wrong client password proof for \"{}\"", conn_get_socket(c), username);
2266 conn_increment_passfail_count(c);
2267 }
2268 }
2269 conn_push_outqueue(c, rpacket);
2270 packet_del_ref(rpacket);
2271 conn_set_client_proof(c, NULL);
2272 conn_set_server_proof(c, NULL);
2273 }
2274 clan_send_status_window(c);
2275
2276 return 0;
2277 }
2278
2279 static int _client_changegameport(t_connection * c, t_packet const *const packet)
2280 {
2281 if (packet_get_size(packet) < sizeof(t_client_changegameport)) {
2282 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad changegameport packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_changegameport), packet_get_size(packet));
2283 return -1;
2284 }
2285 {
2286 unsigned short port = bn_short_get(packet->u.client_changegameport.port);
2287 if (port < 1024) {
2288 eventlog(eventlog_level_error, __FUNCTION__, "[{}] invalid port in changegameport packet: {}", conn_get_socket(c), (int)port);
2289 return -1;
2290 }
2291
2292 conn_set_game_port(c, port);
2293 }
2294
2295 return 0;
2296 }
2297
2298 static int _client_friendslistreq(t_connection * c, t_packet const *const packet)
2299 {
2300 t_packet *rpacket;
2301
2302 if (packet_get_size(packet) < sizeof(t_client_friendslistreq)) {
2303 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad FRIENDSLISTREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_friendslistreq), packet_get_size(packet));
2304 return -1;
2305 }
2306 {
2307 int frienduid;
2308 t_list *flist;
2309 t_friend *fr;
2310 t_account *account = conn_get_account(c);
2311 int i;
2312 int n = account_get_friendcount(account);
2313 int friendcount = 0;
2314 t_server_friendslistreply_status status;
2315 t_connection *dest_c;
2316 t_game *game;
2317 t_channel *channel;
2318 char stat;
2319
2320 if (!(rpacket = packet_create(packet_class_bnet)))
2321 return -1;
2322
2323 packet_set_size(rpacket, sizeof(t_server_friendslistreply));
2324 packet_set_type(rpacket, SERVER_FRIENDSLISTREPLY);
2325
2326 if ((flist = account_get_friends(account)) == NULL)
2327 {
2328 packet_del_ref(rpacket);
2329 return -1;
2330 }
2331
2332 for (i = 0; i < n; i++) {
2333 frienduid = account_get_friend(account, i);
2334 if ((fr = friendlist_find_uid(flist, frienduid)) == NULL)
2335 continue;
2336 packet_append_string(rpacket, account_get_name(friend_get_account(fr)));
2337 game = NULL;
2338 channel = NULL;
2339
2340 if (!(dest_c = connlist_find_connection_by_uid(frienduid))) {
2341 bn_byte_set(&status.location, FRIENDSTATUS_OFFLINE);
2342 bn_byte_set(&status.status, 0);
2343 bn_int_set(&status.clienttag, 0);
2344 }
2345 else {
2346 bn_int_set(&status.clienttag, conn_get_clienttag(dest_c));
2347 stat = 0;
2348 if ((friend_get_mutual(fr)))
2349 stat |= FRIEND_TYPE_MUTUAL;
2350 if ((conn_get_dndstr(dest_c)))
2351 stat |= FRIEND_TYPE_DND;
2352 if ((conn_get_awaystr(dest_c)))
2353 stat |= FRIEND_TYPE_AWAY;
2354 bn_byte_set(&status.status, stat);
2355 if ((game = conn_get_game(dest_c))) {
2356 if (game_get_flag(game) != game_flag_private)
2357 bn_byte_set(&status.location, FRIENDSTATUS_PUBLIC_GAME);
2358 else
2359 bn_byte_set(&status.location, FRIENDSTATUS_PRIVATE_GAME);
2360 }
2361 else if ((channel = conn_get_channel(dest_c))) {
2362 bn_byte_set(&status.location, FRIENDSTATUS_CHAT);
2363 }
2364 else {
2365 bn_byte_set(&status.location, FRIENDSTATUS_ONLINE);
2366 }
2367 }
2368
2369 packet_append_data(rpacket, &status, sizeof(status));
2370
2371 if (game)
2372 packet_append_string(rpacket, game_get_name(game));
2373 else if (channel)
2374 packet_append_string(rpacket, channel_get_name(channel));
2375 else
2376 packet_append_string(rpacket, "");
2377
2378 friendcount++;
2379 }
2380
2381 bn_byte_set(&rpacket->u.server_friendslistreply.friendcount, friendcount);
2382
2383 conn_push_outqueue(c, rpacket);
2384 packet_del_ref(rpacket);
2385 }
2386
2387 return 0;
2388 }
2389
2390 static int _client_friendinforeq(t_connection * c, t_packet const *const packet)
2391 {
2392 t_packet *rpacket;
2393
2394 if (packet_get_size(packet) < sizeof(t_client_friendinforeq)) {
2395 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad FRIENDINFOREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_friendinforeq), packet_get_size(packet));
2396 return -1;
2397 }
2398
2399 {
2400 t_connection const *dest_c;
2401 t_game const *game;
2402 t_channel const *channel;
2403 t_account *account = conn_get_account(c);
2404 int frienduid;
2405 t_friend *fr;
2406 t_list *flist;
2407 int n = account_get_friendcount(account);
2408 char type;
2409
2410 if (n == 0)
2411 return 0;
2412
2413 if (bn_byte_get(packet->u.client_friendinforeq.friendnum) > n) {
2414 eventlog(eventlog_level_error, __FUNCTION__, "[{}] bad friend number in FRIENDINFOREQ packet", conn_get_socket(c));
2415 return -1;
2416 }
2417
2418 if (!(rpacket = packet_create(packet_class_bnet)))
2419 return -1;
2420
2421 packet_set_size(rpacket, sizeof(t_server_friendinforeply));
2422 packet_set_type(rpacket, SERVER_FRIENDINFOREPLY);
2423
2424 frienduid = account_get_friend(account, bn_byte_get(packet->u.client_friendinforeq.friendnum));
2425 if (frienduid < 0) {
2426 eventlog(eventlog_level_error, __FUNCTION__, "[{}] friend number {} not found", conn_get_socket(c), (int)bn_byte_get(packet->u.client_friendinforeq.friendnum));
2427 return -1;
2428 }
2429
2430 bn_byte_set(&rpacket->u.server_friendinforeply.friendnum, bn_byte_get(packet->u.client_friendinforeq.friendnum));
2431
2432 flist = account_get_friends(account);
2433 fr = friendlist_find_uid(flist, frienduid);
2434
2435 if (fr == NULL || (dest_c = connlist_find_connection_by_account(friend_get_account(fr))) == NULL) {
2436 bn_byte_set(&rpacket->u.server_friendinforeply.type, FRIEND_TYPE_NON_MUTUAL);
2437 bn_byte_set(&rpacket->u.server_friendinforeply.status, FRIENDSTATUS_OFFLINE);
2438 bn_int_set(&rpacket->u.server_friendinforeply.clienttag, 0);
2439 packet_append_string(rpacket, "");
2440 conn_push_outqueue(c, rpacket);
2441 packet_del_ref(rpacket);
2442 return 0;
2443 }
2444
2445 type = FRIEND_TYPE_NON_MUTUAL;
2446 if (friend_get_mutual(fr))
2447 type |= FRIEND_TYPE_MUTUAL;
2448 if ((conn_get_dndstr(dest_c)))
2449 type |= FRIEND_TYPE_DND;
2450 if ((conn_get_awaystr(dest_c)))
2451 type |= FRIEND_TYPE_AWAY;
2452 bn_byte_set(&rpacket->u.server_friendinforeply.type, type);
2453 if ((game = conn_get_game(dest_c))) {
2454 if (game_get_flag(game) != game_flag_private)
2455 bn_byte_set(&rpacket->u.server_friendinforeply.status, FRIENDSTATUS_PUBLIC_GAME);
2456 else
2457 bn_byte_set(&rpacket->u.server_friendinforeply.status, FRIENDSTATUS_PRIVATE_GAME);
2458 packet_append_string(rpacket, game_get_name(game));
2459 }
2460 else if ((channel = conn_get_channel(dest_c))) {
2461 bn_byte_set(&rpacket->u.server_friendinforeply.status, FRIENDSTATUS_CHAT);
2462 packet_append_string(rpacket, channel_get_name(channel));
2463 }
2464 else {
2465 bn_byte_set(&rpacket->u.server_friendinforeply.status, FRIENDSTATUS_ONLINE);
2466 packet_append_string(rpacket, "");
2467 }
2468
2469 bn_int_set(&rpacket->u.server_friendinforeply.clienttag, conn_get_clienttag(dest_c));
2470
2471 conn_push_outqueue(c, rpacket);
2472 packet_del_ref(rpacket);
2473
2474 }
2475
2476 return 0;
2477 }
2478
2479 static int _client_atfriendscreen(t_connection * c, t_packet const *const packet)
2480 {
2481 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got CLIENT_ARRANGEDTEAM_FRIENDSCREEN packet", conn_get_socket(c));
2482
2483 const char *my_username = conn_get_username(c);
2484 if (!my_username)
2485 {
2486 return -1;
2487 }
2488
2489 t_channel *my_channel = conn_get_channel(c);
2490 int my_channel_is_public = 1;
2491 if (my_channel)
2492 {
2493 my_channel_is_public = channel_get_flags(my_channel) & channel_flags_public;
2494 }
2495
2496
2497 t_packet *rpacket = packet_create(packet_class_bnet);
2498 if (!rpacket)
2499 {
2500 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not create friendscreen server packet", conn_get_socket(c));
2501 return -1;
2502 }
2503 packet_set_size(rpacket, sizeof(t_server_arrangedteam_friendscreen));
2504 packet_set_type(rpacket, SERVER_ARRANGEDTEAM_FRIENDSCREEN);
2505
2506
2507 std::uint8_t available_players = 0;
2508
2509 // begin search for mutual and available friends
2510 t_list *my_friend_list = account_get_friends(conn_get_account(c));
2511 t_elem *entry;
2512 LIST_TRAVERSE(my_friend_list, entry)
2513 {
2514 t_friend *_friend = static_cast<t_friend *>(elem_get_data(entry));
2515 if (!_friend)
2516 {
2517 eventlog(eventlog_level_error, __FUNCTION__, "found NULL entry in list");
2518 continue;
2519 }
2520
2521 if (available_players == std::numeric_limits<decltype(available_players)>::max())
2522 {
2523 eventlog(eventlog_level_info, __FUNCTION__, "Reached maximum amount of available players to send in packet");
2524 break;
2525 }
2526
2527 // skip non-mutual friends
2528 if (friend_get_mutual(_friend) == FRIEND_NOTMUTUAL)
2529 {
2530 continue;
2531 }
2532
2533 t_account *friend_account = friend_get_account(_friend);
2534 t_connection *friend_connection = connlist_find_connection_by_account(friend_account);
2535 // if user is offline, then continue to next friend
2536 if (!friend_connection)
2537 {
2538 continue;
2539 }
2540
2541 const std::string my_version_tag = conn_get_versioncheck(c) ? conn_get_versioncheck(c)->get_version_tag() : "";
2542 const std::string friend_version_tag = conn_get_versioncheck(friend_connection) ? conn_get_versioncheck(friend_connection)->get_version_tag() : "";
2543 // friend is using another game or is on a different version
2544 if (my_version_tag != friend_version_tag)
2545 {
2546 continue;
2547 }
2548
2549 // friend has Do Not Disturb mode enabled
2550 if (conn_get_dndstr(friend_connection))
2551 {
2552 continue;
2553 }
2554
2555 // friend is away
2556 if (conn_get_awaystr(friend_connection))
2557 {
2558 continue;
2559 }
2560
2561 // friend is not in a channel
2562 t_channel *friend_channel = conn_get_channel(friend_connection);
2563 if (!friend_channel)
2564 {
2565 continue;
2566 }
2567
2568 // // don't list YET if in same private channel
2569 if (!my_channel_is_public && (friend_channel == my_channel))
2570 {
2571 continue;
2572 }
2573
2574 const char *friend_name = account_get_name(friend_account);
2575 eventlog(eventlog_level_trace, __FUNCTION__, "Friend {} is available for an AT Game.", friend_name);
2576 available_players += 1;
2577 packet_append_string(rpacket, friend_name);
2578 }
2579
2580
2581 // now list matching users in same private chan
2582 if (!my_channel_is_public)
2583 {
2584 for (t_connection *user_connection = channel_get_first(my_channel); user_connection; user_connection = channel_get_next())
2585 {
2586 if (available_players == std::numeric_limits<decltype(available_players)>::max())
2587 {
2588 eventlog(eventlog_level_info, __FUNCTION__, "Reached maximum amount of available players to send in packet");
2589 break;
2590 }
2591
2592 // skip self
2593 if (user_connection == c)
2594 {
2595 continue;
2596 }
2597
2598 const std::string my_version_tag = conn_get_versioncheck(c) ? conn_get_versioncheck(c)->get_version_tag() : "";
2599 const std::string user_version_tag = conn_get_versioncheck(user_connection) ? conn_get_versioncheck(user_connection)->get_version_tag() : "";
2600 // user is using another game or is on a different version
2601 if (my_version_tag != user_version_tag)
2602 {
2603 continue;
2604 }
2605
2606 // user is dnd
2607 if (conn_get_dndstr(user_connection))
2608 {
2609 continue;
2610 }
2611
2612 // user is away
2613 if (conn_get_awaystr(user_connection))
2614 {
2615 continue;
2616 }
2617
2618 const char *username = account_get_name(conn_get_account(user_connection));
2619 if (!username)
2620 {
2621 continue;
2622 }
2623
2624 eventlog(eventlog_level_trace, __FUNCTION__, "user {} in private channel {} is available for an AT Game.", username, channel_get_name(my_channel));
2625 available_players += 1;
2626 packet_append_string(rpacket, username);
2627 }
2628 }
2629
2630 if (available_players == 0)
2631 {
2632 eventlog(eventlog_level_info, __FUNCTION__, "No available players");
2633 }
2634
2635 bn_byte_set(&rpacket->u.server_arrangedteam_friendscreen.f_count, available_players);
2636
2637 conn_push_outqueue(c, rpacket);
2638 packet_del_ref(rpacket);
2639
2640 return 0;
2641 }
2642
2643 static int _client_atinvitefriend(t_connection * c, t_packet const *const packet)
2644 {
2645 t_packet *rpacket;
2646 t_clienttag ctag;
2647
2648 if (packet_get_size(packet) < sizeof(t_client_arrangedteam_invite_friend)) {
2649 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad ARRANGEDTEAM_INVITE_FRIEND packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_arrangedteam_invite_friend), packet_get_size(packet));
2650 return -1;
2651 }
2652
2653 ctag = conn_get_clienttag(c);
2654
2655 {
2656 int count_to_invite, count, id;
2657 char const *invited_usernames[8];
2658 t_account *members[MAX_TEAMSIZE];
2659 int i, n, offset, teammemcount;
2660 t_connection *dest_c;
2661 t_team *team;
2662 unsigned int teamid;
2663
2664 count_to_invite = bn_byte_get(packet->u.client_arrangedteam_invite_friend.numfriends);
2665 count = bn_int_get(packet->u.client_arrangedteam_invite_friend.count);
2666 id = bn_int_get(packet->u.client_arrangedteam_invite_friend.id);
2667 teammemcount = count_to_invite + 1;
2668
2669 if ((count_to_invite < 1) || (count_to_invite > 3)) {
2670 eventlog(eventlog_level_error, __FUNCTION__, "got invalid number of users to invite to game");
2671 return -1;
2672 }
2673
2674 eventlog(eventlog_level_info, __FUNCTION__, "[{}] got ARRANGEDTEAM INVITE packet for {} invitees", conn_get_socket(c), count_to_invite);
2675
2676 offset = sizeof(t_client_arrangedteam_invite_friend);
2677
2678 for (i = 0; i < count_to_invite; i++) {
2679 if (!(invited_usernames[i] = packet_get_str_const(packet, offset, MAX_USERNAME_LEN))) {
2680 eventlog(eventlog_level_error, "handle_bnet", "Could not get username from invite packet");
2681 return -1;
2682 }
2683 else {
2684 offset += std::strlen(invited_usernames[i]) + 1;
2685 eventlog(eventlog_level_debug, "handle_bnet", "Added user {} to invite array.", invited_usernames[i]);
2686 }
2687 }
2688
2689 members[0] = conn_get_account(c);
2690 for (i = 1; i < MAX_TEAMSIZE; i++) {
2691 if ((i < teammemcount)) {
2692 if (!(members[i] = accountlist_find_account(invited_usernames[i - 1]))) {
2693 eventlog(eventlog_level_error, __FUNCTION__, "got invitation for non-existant user \"{}\"", invited_usernames[i - 1]);
2694 return -1;
2695 }
2696 }
2697 else
2698 members[i] = NULL;
2699 }
2700
2701
2702 if (!(team = account_find_team_by_accounts(members[0], members, ctag))) {
2703 team = create_team(members, ctag); //no need to free on return -1 because it's already in teamlist
2704
2705 eventlog(eventlog_level_trace, __FUNCTION__, "this team has never played before, creating new team");
2706 }
2707 else {
2708 eventlog(eventlog_level_trace, __FUNCTION__, "this team has already played before");
2709 }
2710
2711 teamid = team_get_teamid(team);
2712 account_set_currentatteam(conn_get_account(c), team_get_teamid(team));
2713
2714
2715 //Create the packet to send to each of the users you wanted to invite
2716 conn_part_channel(c);
2717
2718 for (i = 0; i < teammemcount; i++) {
2719
2720 if (!(dest_c = account_get_conn(team_get_member(team, i))))
2721 continue;
2722
2723 if ((dest_c == c))
2724 continue;
2725
2726 if (!(rpacket = packet_create(packet_class_bnet)))
2727 return -1;
2728
2729 packet_set_size(rpacket, sizeof(t_server_arrangedteam_send_invite));
2730 packet_set_type(rpacket, SERVER_ARRANGEDTEAM_SEND_INVITE);
2731
2732 bn_int_set(&rpacket->u.server_arrangedteam_send_invite.count, count);
2733 bn_int_set(&rpacket->u.server_arrangedteam_send_invite.id, id);
2734 { /* trans support */
2735 unsigned short port = conn_get_game_port(c);
2736 unsigned int addr = conn_get_addr(c);
2737
2738 trans_net(conn_get_addr(dest_c), &addr, &port);
2739
2740 bn_int_nset(&rpacket->u.server_arrangedteam_send_invite.inviterip, addr);
2741 bn_short_set(&rpacket->u.server_arrangedteam_send_invite.port, port);
2742 }
2743 bn_byte_set(&rpacket->u.server_arrangedteam_send_invite.numfriends, count_to_invite);
2744
2745 for (n = 0; n < teammemcount; n++) {
2746 if (n != i)
2747 packet_append_string(rpacket, account_get_name(team_get_member(team, n)));
2748 }
2749
2750 //now send packet
2751 conn_push_outqueue(dest_c, rpacket);
2752 packet_del_ref(rpacket);
2753
2754 account_set_currentatteam(conn_get_account(dest_c), teamid);
2755 }
2756
2757 //now send a ACK to the inviter
2758 if (!(rpacket = packet_create(packet_class_bnet)))
2759 return -1;
2760 packet_set_size(rpacket, sizeof(t_server_arrangedteam_invite_friend_ack));
2761 packet_set_type(rpacket, SERVER_ARRANGEDTEAM_INVITE_FRIEND_ACK);
2762
2763 bn_int_set(&rpacket->u.server_arrangedteam_invite_friend_ack.count, count);
2764 bn_int_set(&rpacket->u.server_arrangedteam_invite_friend_ack.id, id);
2765 bn_int_set(&rpacket->u.server_arrangedteam_invite_friend_ack.timestamp, now);
2766 bn_byte_set(&rpacket->u.server_arrangedteam_invite_friend_ack.teamsize, count_to_invite + 1);
2767
2768 /*
2769 * five int's to fill
2770 * fill with uid's of all teammembers, including the inviter
2771 * and the rest with FFFFFFFF
2772 * to be used when sever recieves anongame search
2773 * [Omega]
2774 */
2775 for (i = 0; i < 5; i++) {
2776
2777 if (i < teammemcount) {
2778 bn_int_set(&rpacket->u.server_arrangedteam_invite_friend_ack.info[i], team_get_memberuid(team, i));
2779 }
2780 else { /* fill rest with FFFFFFFF */
2781 bn_int_set(&rpacket->u.server_arrangedteam_invite_friend_ack.info[i], 0xFFFFFFFF);
2782 }
2783 }
2784
2785 conn_push_outqueue(c, rpacket);
2786 packet_del_ref(rpacket);
2787
2788 }
2789
2790 return 0;
2791 }
2792
2793 static int _client_atacceptdeclineinvite(t_connection * c, t_packet const *const packet)
2794 {
2795 t_packet *rpacket;
2796 t_clienttag ctag;
2797
2798 if (packet_get_size(packet) < sizeof(t_client_arrangedteam_accept_decline_invite)) {
2799 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad ARRANGEDTEAM_ACCEPT_DECLINE_INVITE packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_arrangedteam_accept_decline_invite), packet_get_size(packet));
2800 return -1;
2801 }
2802
2803 ctag = conn_get_clienttag(c);
2804
2805 {
2806 char const *inviter;
2807 t_connection *dest_c;
2808
2809 //if user declined the invitation then
2810 if (bn_int_get(packet->u.client_arrangedteam_accept_decline_invite.option) == CLIENT_ARRANGEDTEAM_DECLINE) {
2811 inviter = packet_get_str_const(packet, sizeof(t_client_arrangedteam_accept_decline_invite), MAX_USERNAME_LEN);
2812 dest_c = connlist_find_connection_by_accountname(inviter);
2813
2814 eventlog(eventlog_level_info, "handle_bnet", "{} declined a arranged team game with {}", conn_get_username(c), inviter);
2815
2816 if (!(rpacket = packet_create(packet_class_bnet)))
2817 return -1;
2818 packet_set_size(rpacket, sizeof(t_server_arrangedteam_member_decline));
2819 packet_set_type(rpacket, SERVER_ARRANGEDTEAM_MEMBER_DECLINE);
2820
2821 bn_int_set(&rpacket->u.server_arrangedteam_member_decline.count, bn_int_get(packet->u.client_arrangedteam_accept_decline_invite.count));
2822 bn_int_set(&rpacket->u.server_arrangedteam_member_decline.action, SERVER_ARRANGEDTEAM_DECLINE);
2823 packet_append_string(rpacket, conn_get_username(c));
2824
2825 conn_push_outqueue(dest_c, rpacket);
2826 packet_del_ref(rpacket);
2827 }
2828 }
2829
2830 return 0;
2831 }
2832
2833 static int _client_atacceptinvite(t_connection * c, t_packet const *const packet)
2834 {
2835 // t_packet * rpacket;
2836
2837 if (packet_get_size(packet) < sizeof(t_client_arrangedteam_accept_invite)) {
2838 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad ARRANGEDTEAM_ACCEPT_INVITE packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_arrangedteam_accept_invite), packet_get_size(packet));
2839 return -1;
2840 }
2841 /* conn_set_channel(c, "Arranged Teams"); */
2842 return 0;
2843 }
2844
2845 typedef struct {
2846 t_connection *c;
2847 unsigned lnews;
2848 unsigned fnews;
2849 } t_motd_data;
2850
2851 static int _news_cb(std::time_t date, t_lstr * lstr, void *data)
2852 {
2853 t_packet *rpacket;
2854 t_motd_data *motdd = (t_motd_data *)data;
2855
2856 if (date < motdd->lnews)
2857 return -1; /* exit traversing */
2858
2859 rpacket = packet_create(packet_class_bnet);
2860 if (!rpacket)
2861 return -1;
2862
2863 packet_set_size(rpacket, sizeof(t_server_motd_w3));
2864 packet_set_type(rpacket, SERVER_MOTD_W3);
2865
2866 bn_byte_set(&rpacket->u.server_motd_w3.msgtype, SERVER_MOTD_W3_MSGTYPE);
2867 bn_int_set(&rpacket->u.server_motd_w3.curr_time, now);
2868
2869 bn_int_set(&rpacket->u.server_motd_w3.first_news_time, motdd->fnews);
2870 bn_int_set(&rpacket->u.server_motd_w3.timestamp, date);
2871 bn_int_set(&rpacket->u.server_motd_w3.timestamp2, date);
2872
2873 /* Append news to packet, we used the already cached len in the lstr */
2874 packet_append_lstr(rpacket, lstr);
2875
2876 /* Send news packet */
2877 conn_push_outqueue(motdd->c, rpacket);
2878 packet_del_ref(rpacket);
2879
2880 return 0;
2881 }
2882
2883 // motd for warcraft 3 (http://img21.imageshack.us/img21/1808/j2py.png)
2884 static int _client_motdw3(t_connection * c, t_packet const *const packet)
2885 {
2886 t_packet *rpacket;
2887 t_clienttag ctag;
2888 t_motd_data motdd;
2889
2890 if (packet_get_size(packet) < sizeof(t_client_motd_w3)) {
2891 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_MOTD_W3 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_motd_w3), packet_get_size(packet));
2892 return -1;
2893 }
2894
2895 ctag = conn_get_clienttag(c);
2896 /* if in a game, remove user from his game */
2897 if (conn_get_game(c) != NULL)
2898 conn_set_game(c, NULL, NULL, NULL, game_type_none, 0);
2899
2900 /* News */
2901 motdd.lnews = bn_int_get(packet->u.client_motd_w3.last_news_time);
2902 motdd.fnews = news_get_firstnews();
2903 motdd.c = c;
2904
2905 eventlog(eventlog_level_trace, __FUNCTION__, "lastnews() {} news_time {}", news_get_lastnews(), motdd.lnews);
2906 news_traverse(_news_cb, &motdd);
2907
2908 /* Welcome Message */
2909 rpacket = packet_create(packet_class_bnet);
2910 if (!rpacket)
2911 return -1;
2912
2913 packet_set_size(rpacket, sizeof(t_server_motd_w3));
2914 packet_set_type(rpacket, SERVER_MOTD_W3);
2915
2916 //bn_int_set(&rpacket->u.server_motd_w3.ticks,get_ticks());
2917 bn_byte_set(&rpacket->u.server_motd_w3.msgtype, SERVER_MOTD_W3_MSGTYPE);
2918 bn_int_set(&rpacket->u.server_motd_w3.curr_time, now);
2919 bn_int_set(&rpacket->u.server_motd_w3.first_news_time, motdd.fnews);
2920 bn_int_set(&rpacket->u.server_motd_w3.timestamp, motdd.fnews + 1);
2921 bn_int_set(&rpacket->u.server_motd_w3.timestamp2, SERVER_MOTD_W3_WELCOME);
2922
2923
2924 // read text from bnmotd_w3.txt
2925 {
2926 fmt::MemoryWriter serverinfo;
2927
2928 std::string filename = i18n_filename(prefs_get_motdw3file(), conn_get_gamelang_localized(c));
2929 std::FILE* fp = std::fopen(filename.c_str(), "r");
2930 if (fp)
2931 {
2932 while (char* buff = file_get_line(fp))
2933 {
2934 char* line = message_format_line(c, buff);
2935 serverinfo << (line + 1) << '\n';
2936 xfree((void*)line);
2937 }
2938
2939 if (std::fclose(fp) == EOF)
2940 {
2941 eventlog(eventlog_level_error, __FUNCTION__, "could not close motdw3 file \"{}\" after reading (std::fopen: {})", filename, std::strerror(errno));
2942 }
2943 }
2944 packet_append_string(rpacket, serverinfo.c_str());
2945 }
2946
2947 conn_push_outqueue(c, rpacket);
2948 packet_del_ref(rpacket);
2949
2950 return 0;
2951 }
2952
2953 static int _client_realmlistreq(t_connection * c, t_packet const *const packet)
2954 {
2955 t_packet *rpacket;
2956
2957 if (packet_get_size(packet) < sizeof(t_client_realmlistreq)) {
2958 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad REALMLISTREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_realmlistreq), packet_get_size(packet));
2959 return -1;
2960 }
2961
2962 if ((rpacket = packet_create(packet_class_bnet))) {
2963 t_elem const *curr;
2964 t_realm const *realm;
2965 t_server_realmlistreply_data realmdata;
2966 unsigned int count;
2967
2968 packet_set_size(rpacket, sizeof(t_server_realmlistreply));
2969 packet_set_type(rpacket, SERVER_REALMLISTREPLY);
2970 bn_int_set(&rpacket->u.server_realmlistreply.unknown1, SERVER_REALMLISTREPLY_UNKNOWN1);
2971 count = 0;
2972 LIST_TRAVERSE_CONST(realmlist(), curr) {
2973 realm = (t_realm*)elem_get_data(curr);
2974 if (!realm_get_active(realm))
2975 continue;
2976 bn_int_set(&realmdata.unknown3, SERVER_REALMLISTREPLY_DATA_UNKNOWN3);
2977 bn_int_set(&realmdata.unknown4, SERVER_REALMLISTREPLY_DATA_UNKNOWN4);
2978 bn_int_set(&realmdata.unknown5, SERVER_REALMLISTREPLY_DATA_UNKNOWN5);
2979 bn_int_set(&realmdata.unknown6, SERVER_REALMLISTREPLY_DATA_UNKNOWN6);
2980 bn_int_set(&realmdata.unknown7, SERVER_REALMLISTREPLY_DATA_UNKNOWN7);
2981 bn_int_set(&realmdata.unknown8, SERVER_REALMLISTREPLY_DATA_UNKNOWN8);
2982 bn_int_set(&realmdata.unknown9, SERVER_REALMLISTREPLY_DATA_UNKNOWN9);
2983 packet_append_data(rpacket, &realmdata, sizeof(realmdata));
2984 packet_append_string(rpacket, realm_get_name(realm));
2985 packet_append_string(rpacket, realm_get_description(realm));
2986 count++;
2987 }
2988 bn_int_set(&rpacket->u.server_realmlistreply.count, count);
2989 conn_push_outqueue(c, rpacket);
2990 packet_del_ref(rpacket);
2991 }
2992
2993 return 0;
2994 }
2995
2996 static int _client_realmlistreq110(t_connection * c, t_packet const *const packet)
2997 {
2998 t_packet *rpacket;
2999
3000 if (packet_get_size(packet) < sizeof(t_client_realmlistreq_110)) {
3001 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad REALMLISTREQ_110 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_realmlistreq), packet_get_size(packet));
3002 return -1;
3003 }
3004
3005 if ((rpacket = packet_create(packet_class_bnet))) {
3006 t_elem const *curr;
3007 t_realm const *realm;
3008 t_server_realmlistreply_110_data realmdata;
3009 unsigned int count;
3010
3011 packet_set_size(rpacket, sizeof(t_server_realmlistreply_110));
3012 packet_set_type(rpacket, SERVER_REALMLISTREPLY_110);
3013 bn_int_set(&rpacket->u.server_realmlistreply_110.unknown1, SERVER_REALMLISTREPLY_110_UNKNOWN1);
3014 count = 0;
3015 LIST_TRAVERSE_CONST(realmlist(), curr) {
3016 realm = (t_realm*)elem_get_data(curr);
3017 if (!realm_get_active(realm))
3018 continue;
3019 bn_int_set(&realmdata.unknown1, SERVER_REALMLISTREPLY_110_DATA_UNKNOWN1);
3020 packet_append_data(rpacket, &realmdata, sizeof(realmdata));
3021 packet_append_string(rpacket, realm_get_name(realm));
3022 packet_append_string(rpacket, realm_get_description(realm));
3023 count++;
3024 }
3025 bn_int_set(&rpacket->u.server_realmlistreply_110.count, count);
3026 conn_push_outqueue(c, rpacket);
3027 packet_del_ref(rpacket);
3028 }
3029
3030 return 0;
3031 }
3032
3033 static int _client_claninforeq(t_connection * c, t_packet const *const packet)
3034 {
3035 t_packet *rpacket;
3036 int count;
3037 char const *username;
3038 t_account *account;
3039 t_clanmember *clanmember;
3040 t_clan *clan;
3041 t_clantag clantag1, clantag2;
3042
3043 if (packet_get_size(packet) < sizeof(t_client_claninforeq)) {
3044 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLANINFOREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_claninforeq), packet_get_size(packet));
3045 return -1;
3046 }
3047
3048 count = bn_int_get(packet->u.client_claninforeq.count);
3049 clantag1 = bn_int_get(packet->u.client_claninforeq.clantag);
3050 clan = NULL;
3051
3052 if (!(username = packet_get_str_const(packet, sizeof(t_client_claninforeq), MAX_USERNAME_LEN))) {
3053 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLANINFOREQ (missing or too long username)", conn_get_socket(c));
3054 return -1;
3055 }
3056
3057 if (!(account = accountlist_find_account(username))) {
3058 eventlog(eventlog_level_error, __FUNCTION__, "requested claninfo for non-existant account");
3059 return -1;
3060 }
3061
3062 if ((clanmember = account_get_clanmember(account)) && (clan = clanmember_get_clan(clanmember)))
3063 clantag2 = clan_get_clantag(clan);
3064 else
3065 clantag2 = 0;
3066
3067 if ((rpacket = packet_create(packet_class_bnet))) {
3068 packet_set_size(rpacket, sizeof(t_server_claninforeply));
3069 packet_set_type(rpacket, SERVER_CLANINFOREPLY);
3070 bn_int_set(&rpacket->u.server_profilereply.count, count);
3071 if (clantag1 == clantag2) {
3072 t_bnettime bn_time;
3073 bn_long ltime;
3074
3075 bn_byte_set(&rpacket->u.server_claninforeply.fail, 0);
3076
3077 packet_append_string(rpacket, clan_get_name(clan));
3078 char status = clanmember_get_status(clanmember);
3079 packet_append_data(rpacket, &status, 1);
3080 std::time_t temp = clanmember_get_join_time(clanmember);
3081 bn_time = time_to_bnettime(temp, 0);
3082 bn_time = bnettime_add_tzbias(bn_time, -conn_get_tzbias(c));
3083 bnettime_to_bn_long(bn_time, &ltime);
3084 packet_append_data(rpacket, &ltime, 8);
3085 }
3086 else
3087 bn_byte_set(&rpacket->u.server_claninforeply.fail, 1);
3088
3089
3090 conn_push_outqueue(c, rpacket);
3091 packet_del_ref(rpacket);
3092 }
3093
3094 return 0;
3095 }
3096
3097 static int _client_profilereq(t_connection * c, t_packet const *const packet)
3098 {
3099 t_packet *rpacket;
3100 int count;
3101 char const *username;
3102 t_account *account;
3103 t_clanmember *clanmember;
3104 t_clan *clan;
3105 bn_int clanTAG;
3106
3107 if (packet_get_size(packet) < sizeof(t_client_profilereq)) {
3108 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad PROFILEREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_profilereq), packet_get_size(packet));
3109 return -1;
3110 }
3111
3112 count = bn_int_get(packet->u.client_profilereq.count);
3113
3114 if (!(username = packet_get_str_const(packet, sizeof(t_client_profilereq), MAX_USERNAME_LEN))) {
3115 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad PROFILEREQ (missing or too long username)", conn_get_socket(c));
3116 return -1;
3117 }
3118
3119 if (!(account = accountlist_find_account(username))) {
3120 eventlog(eventlog_level_error, __FUNCTION__, "requested profile for non-existant account");
3121 return -1;
3122 }
3123 if ((rpacket = packet_create(packet_class_bnet))) {
3124 packet_set_size(rpacket, sizeof(t_server_profilereply));
3125 packet_set_type(rpacket, SERVER_PROFILEREPLY);
3126 bn_int_set(&rpacket->u.server_profilereply.count, count);
3127 bn_byte_set(&rpacket->u.server_profilereply.fail, 0);
3128 packet_append_string(rpacket, account_get_desc(account).c_str());
3129 packet_append_string(rpacket, account_get_loc(account).c_str());
3130 if ((clanmember = account_get_clanmember(account)) && (clan = clanmember_get_clan(clanmember)))
3131 bn_int_set(&clanTAG, clan_get_clantag(clan));
3132 else
3133 bn_int_set(&clanTAG, 0);
3134 packet_append_data(rpacket, clanTAG, 4);
3135
3136 conn_push_outqueue(c, rpacket);
3137 packet_del_ref(rpacket);
3138 }
3139
3140 return 0;
3141 }
3142
3143
3144 static int _client_realmjoinreq109(t_connection * c, t_packet const *const packet)
3145 {
3146 t_packet *rpacket;
3147
3148 if (packet_get_size(packet) < sizeof(t_client_realmjoinreq_109)) {
3149 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad REALMJOINREQ_109 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_realmjoinreq_109), packet_get_size(packet));
3150 return -1;
3151 }
3152
3153 {
3154 char const *realmname;
3155 t_realm *realm;
3156
3157 if (!(realmname = packet_get_str_const(packet, sizeof(t_client_realmjoinreq_109), MAX_REALMNAME_LEN))) {
3158 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad REALMJOINREQ_109 (missing or too long realmname)", conn_get_socket(c));
3159 return -1;
3160 }
3161
3162 if ((realm = realmlist_find_realm(realmname))) {
3163 unsigned int salt;
3164 struct {
3165 bn_int salt;
3166 bn_int sessionkey;
3167 bn_int sessionnum;
3168 bn_int secret;
3169 bn_int passhash[5];
3170 } temp;
3171 char const *pass_str;
3172 t_hash secret_hash;
3173 t_hash passhash;
3174 t_realm *prev_realm;
3175
3176 /* FIXME: should we only set this after they log in to the realm server? */
3177 prev_realm = conn_get_realm(c);
3178 if (prev_realm) {
3179 if (prev_realm != realm) {
3180 realm_add_player_number(realm, 1);
3181 realm_add_player_number(prev_realm, -1);
3182 conn_set_realm(c, realm);
3183 }
3184 }
3185 else {
3186 realm_add_player_number(realm, 1);
3187 conn_set_realm(c, realm);
3188 }
3189
3190 if ((pass_str = account_get_pass(conn_get_account(c)))) {
3191 if (hash_set_str(&passhash, pass_str) == 0) {
3192 hash_to_bnhash((t_hash const *)&passhash, temp.passhash);
3193 salt = bn_int_get(packet->u.client_realmjoinreq_109.seqno);
3194 bn_int_set(&temp.salt, salt);
3195 bn_int_set(&temp.sessionkey, conn_get_sessionkey(c));
3196 bn_int_set(&temp.sessionnum, conn_get_sessionnum(c));
3197 bn_int_set(&temp.secret, conn_get_secret(c));
3198 bnet_hash(&secret_hash, sizeof(temp), &temp);
3199
3200 if ((rpacket = packet_create(packet_class_bnet))) {
3201 packet_set_size(rpacket, sizeof(t_server_realmjoinreply_109));
3202 packet_set_type(rpacket, SERVER_REALMJOINREPLY_109);
3203 bn_int_set(&rpacket->u.server_realmjoinreply_109.seqno, salt);
3204 bn_int_set(&rpacket->u.server_realmjoinreply_109.u1, 0x0);
3205 bn_short_set(&rpacket->u.server_realmjoinreply_109.u3, 0x0); /* reg auth */
3206 bn_int_set(&rpacket->u.server_realmjoinreply_109.bncs_addr1, 0x0);
3207 bn_int_set(&rpacket->u.server_realmjoinreply_109.sessionnum, conn_get_sessionnum(c));
3208 { /* trans support */
3209 unsigned int addr = realm_get_ip(realm);
3210 unsigned short port = realm_get_port(realm);
3211
3212 trans_net(conn_get_addr(c), &addr, &port);
3213
3214 bn_int_nset(&rpacket->u.server_realmjoinreply_109.addr, addr);
3215 bn_short_nset(&rpacket->u.server_realmjoinreply_109.port, port);
3216 }
3217 bn_int_set(&rpacket->u.server_realmjoinreply_109.sessionkey, conn_get_sessionkey(c));
3218 bn_int_set(&rpacket->u.server_realmjoinreply_109.u5, 0);
3219 bn_int_set(&rpacket->u.server_realmjoinreply_109.u6, 0);
3220 bn_int_set(&rpacket->u.server_realmjoinreply_109.bncs_addr2, 0);
3221 bn_int_set(&rpacket->u.server_realmjoinreply_109.u7, 0);
3222 bn_int_set(&rpacket->u.server_realmjoinreply_109.versionid, conn_get_versionid(c));
3223 bn_int_set(&rpacket->u.server_realmjoinreply_109.clienttag, conn_get_clienttag(c));
3224 hash_to_bnhash((t_hash const *)&secret_hash, rpacket->u.server_realmjoinreply_109.secret_hash); /* avoid warning */
3225 packet_append_string(rpacket, conn_get_username(c));
3226 conn_push_outqueue(c, rpacket);
3227 packet_del_ref(rpacket);
3228 }
3229 return 0;
3230 }
3231 else
3232 eventlog(eventlog_level_info, __FUNCTION__, "[{}] realm join for \"{}\" failed (unable to hash password)", conn_get_socket(c), conn_get_loggeduser(c));
3233 }
3234 else {
3235 eventlog(eventlog_level_info, __FUNCTION__, "[{}] realm join for \"{}\" failed (no password)", conn_get_socket(c), conn_get_loggeduser(c));
3236 }
3237 }
3238 else
3239 eventlog(eventlog_level_error, __FUNCTION__, "[{}] could not find active realm \"{}\"", conn_get_socket(c), realmname);
3240
3241 if ((rpacket = packet_create(packet_class_bnet))) {
3242 packet_set_size(rpacket, sizeof(t_server_realmjoinreply_109));
3243 packet_set_type(rpacket, SERVER_REALMJOINREPLY_109);
3244 bn_int_set(&rpacket->u.server_realmjoinreply_109.seqno, bn_int_get(packet->u.client_realmjoinreq_109.seqno));
3245 bn_int_set(&rpacket->u.server_realmjoinreply_109.u1, 0);
3246 bn_int_set(&rpacket->u.server_realmjoinreply_109.sessionnum, 0);
3247 bn_short_set(&rpacket->u.server_realmjoinreply_109.u3, 0);
3248 bn_int_nset(&rpacket->u.server_realmjoinreply_109.addr, 0);
3249 bn_short_nset(&rpacket->u.server_realmjoinreply_109.port, 0);
3250 bn_int_set(&rpacket->u.server_realmjoinreply_109.sessionkey, 0);
3251 bn_int_set(&rpacket->u.server_realmjoinreply_109.u5, 0);
3252 bn_int_set(&rpacket->u.server_realmjoinreply_109.u6, 0);
3253 bn_int_set(&rpacket->u.server_realmjoinreply_109.u7, 0);
3254 bn_int_set(&rpacket->u.server_realmjoinreply_109.bncs_addr1, 0);
3255 bn_int_set(&rpacket->u.server_realmjoinreply_109.bncs_addr2, 0);
3256 bn_int_set(&rpacket->u.server_realmjoinreply_109.versionid, 0);
3257 bn_int_set(&rpacket->u.server_realmjoinreply_109.clienttag, 0);
3258 bn_int_set(&rpacket->u.server_realmjoinreply_109.secret_hash[0], 0);
3259 bn_int_set(&rpacket->u.server_realmjoinreply_109.secret_hash[1], 0);
3260 bn_int_set(&rpacket->u.server_realmjoinreply_109.secret_hash[2], 0);
3261 bn_int_set(&rpacket->u.server_realmjoinreply_109.secret_hash[3], 0);
3262 bn_int_set(&rpacket->u.server_realmjoinreply_109.secret_hash[4], 0);
3263 packet_append_string(rpacket, "");
3264 conn_push_outqueue(c, rpacket);
3265 packet_del_ref(rpacket);
3266 }
3267 }
3268
3269 return 0;
3270 }
3271
3272 static int _client_charlistreq(t_connection * c, t_packet const *const packet)
3273 {
3274 t_packet *rpacket;
3275
3276 if (packet_get_size(packet) < sizeof(t_client_unknown_37)) {
3277 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad UNKNOWN_37 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_unknown_37), packet_get_size(packet));
3278 return -1;
3279 }
3280 /*
3281 0x0070: 83 80 ff ff ff ff ff 2f t,taran,......./
3282 0x0080: ff ff ff ff ff ff ff ff ff ff 03 ff ff ff ff ff ................
3283 0x0090: ff ff ff ff ff ff ff ff ff ff ff 07 80 80 80 80 ................
3284 0x00a0: ff ff ff 00
3285 */
3286 if ((rpacket = packet_create(packet_class_bnet))) {
3287 char const *charlist;
3288 char *temp;
3289
3290 packet_set_size(rpacket, sizeof(t_server_unknown_37));
3291 packet_set_type(rpacket, SERVER_UNKNOWN_37);
3292 bn_int_set(&rpacket->u.server_unknown_37.unknown1, SERVER_UNKNOWN_37_UNKNOWN1);
3293 bn_int_set(&rpacket->u.server_unknown_37.unknown2, SERVER_UNKNOWN_37_UNKNOWN2);
3294
3295 if (!(charlist = account_get_closed_characterlist(conn_get_account(c), conn_get_clienttag(c), realm_get_name(conn_get_realm(c))))) {
3296 bn_int_set(&rpacket->u.server_unknown_37.count, 0);
3297 conn_push_outqueue(c, rpacket);
3298 packet_del_ref(rpacket);
3299 return 0;
3300 }
3301 temp = xstrdup(charlist);
3302
3303 {
3304 char const *tok1;
3305 char const *tok2;
3306 t_character const *ch;
3307 unsigned int count;
3308
3309 count = 0;
3310 tok1 = (char const *)std::strtok(temp, ","); /* std::strtok modifies the string it is passed */
3311 tok2 = std::strtok(NULL, ",");
3312 while (tok1) {
3313 if (!tok2) {
3314 eventlog(eventlog_level_error, __FUNCTION__, "[{}] account \"{}\" has bad character list \"{}\"", conn_get_socket(c), conn_get_username(c), temp);
3315 break;
3316 }
3317
3318 if ((ch = characterlist_find_character(tok1, tok2))) {
3319 packet_append_ntstring(rpacket, character_get_realmname(ch));
3320 packet_append_ntstring(rpacket, ",");
3321 packet_append_string(rpacket, character_get_name(ch));
3322 packet_append_string(rpacket, character_get_playerinfo(ch));
3323 packet_append_string(rpacket, character_get_guildname(ch));
3324 count++;
3325 }
3326 else
3327 eventlog(eventlog_level_error, __FUNCTION__, "[{}] character \"{}\" is missing", conn_get_socket(c), tok2);
3328 tok1 = std::strtok(NULL, ",");
3329 tok2 = std::strtok(NULL, ",");
3330 }
3331 xfree(temp);
3332
3333 bn_int_set(&rpacket->u.server_unknown_37.count, count);
3334 conn_push_outqueue(c, rpacket);
3335 packet_del_ref(rpacket);
3336 }
3337 }
3338
3339 return 0;
3340 }
3341
3342 static int _client_unknown39(t_connection * c, t_packet const *const packet)
3343 {
3344 if (packet_get_size(packet) < sizeof(t_client_unknown_39)) {
3345 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad UNKNOWN_39 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_unknown_39), packet_get_size(packet));
3346 return -1;
3347 }
3348 return 0;
3349 }
3350
3351 static int _client_adreq(t_connection * c, t_packet const *const packet)
3352 {
3353 if (packet_get_size(packet) < sizeof(t_client_adreq))
3354 {
3355 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad ADREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_adreq), packet_get_size(packet));
3356 return -1;
3357 }
3358
3359 /*
3360 eventlog(eventlog_level_debug, __FUNCTION__, "[%d] SID_CHECKAD { %d %d %d %d }", conn_get_socket(c), bn_int_get(packet->u.client_adreq.archtag),
3361 bn_int_get(packet->u.client_adreq.clienttag), bn_int_get(packet->u.client_adreq.prev_adid), bn_int_get(packet->u.client_adreq.ticks));
3362 */
3363
3364 const AdBanner* ad = AdBannerList.pick(conn_get_clienttag(c), conn_get_gamelang(c), bn_int_get(packet->u.client_adreq.prev_adid));
3365 if (!ad)
3366 {
3367 return 0;
3368 }
3369
3370 t_packet* const rpacket = packet_create(packet_class_bnet);
3371 if (!rpacket)
3372 {
3373 eventlog(eventlog_level_error, __FUNCTION__, "Could not create a packet");
3374 return -1;
3375 }
3376 packet_set_size(rpacket, sizeof(t_server_adreply));
3377 packet_set_type(rpacket, SERVER_ADREPLY);
3378 bn_int_set(&rpacket->u.server_adreply.adid, ad->get_id());
3379 bn_int_set(&rpacket->u.server_adreply.extensiontag, ad->get_extension_tag());
3380 file_to_mod_time(c, ad->get_filename().c_str(), &rpacket->u.server_adreply.timestamp);
3381 packet_append_string(rpacket, ad->get_filename().c_str());
3382 packet_append_string(rpacket, ad->get_url().c_str());
3383 conn_push_outqueue(c, rpacket);
3384 packet_del_ref(rpacket);
3385
3386 return 0;
3387 }
3388
3389 static int _client_adack(t_connection * c, t_packet const *const packet)
3390 {
3391 if (packet_get_size(packet) < sizeof(t_client_adack)) {
3392 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad ADACK packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_adack), packet_get_size(packet));
3393 return -1;
3394 }
3395
3396 /*
3397 {
3398 char const * tname;
3399
3400 eventlog(eventlog_level_info,__FUNCTION__,"[%d] ad acknowledgement for adid 0x%04x from \"%s\"",conn_get_socket(c),bn_int_get(packet->u.client_adack.adid),(tname = conn_get_chatname(c)));
3401 conn_unget_chatname(c,tname);
3402 }
3403 */
3404 return 0;
3405 }
3406
3407 static int _client_adclick(t_connection * c, t_packet const *const packet)
3408 {
3409 if (packet_get_size(packet) < sizeof(t_client_adclick)) {
3410 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad ADCLICK packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_adclick), packet_get_size(packet));
3411 return -1;
3412 }
3413
3414 eventlog(eventlog_level_info, __FUNCTION__, "[{}] ad click for adid 0x{:04x} from \"{}\"", conn_get_socket(c), bn_int_get(packet->u.client_adclick.adid), conn_get_username(c));
3415
3416 return 0;
3417 }
3418
3419 static int _client_adclick2(t_connection * c, t_packet const *const packet)
3420 {
3421 if (packet_get_size(packet) < sizeof(t_client_adclick2))
3422 {
3423 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad ADCLICK2 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_adclick2), packet_get_size(packet));
3424 return -1;
3425 }
3426
3427 eventlog(eventlog_level_trace, __FUNCTION__, "[{}] ad click2 for adid 0x{:04x} from \"{}\"", conn_get_socket(c), bn_int_get(packet->u.client_adclick2.adid), conn_get_username(c));
3428
3429 const AdBanner* const ad = AdBannerList.find(conn_get_clienttag(c), conn_get_gamelang(c), bn_int_get(packet->u.client_adclick2.adid));
3430 if (!ad)
3431 {
3432 return 0;
3433 }
3434
3435 t_packet* const rpacket = packet_create(packet_class_bnet);
3436 if (!rpacket)
3437 {
3438 eventlog(eventlog_level_error, __FUNCTION__, "Could not create a packet");
3439 return -1;
3440 }
3441 packet_set_size(rpacket, sizeof(t_server_adclickreply2));
3442 packet_set_type(rpacket, SERVER_ADCLICKREPLY2);
3443 bn_int_set(&rpacket->u.server_adclickreply2.adid, ad->get_id());
3444 packet_append_string(rpacket, ad->get_url().c_str());
3445 conn_push_outqueue(c, rpacket);
3446 packet_del_ref(rpacket);
3447
3448 return 0;
3449 }
3450
3451 static int _client_readmemory(t_connection * c, t_packet const *const packet)
3452 {
3453 unsigned int size, offset, request_id;
3454
3455 if (packet_get_size(packet) < sizeof(t_client_readmemory)) {
3456 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad READMEMORY packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_readmemory), packet_get_size(packet));
3457 return -1;
3458 }
3459
3460 request_id = bn_int_get(packet->u.client_readmemory.request_id);
3461
3462 size = (unsigned int)packet_get_size(packet);
3463 offset = sizeof(t_client_readmemory);
3464
3465 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] Received READMEMORY packet with Request ID: {} and Memory size: {}", conn_get_socket(c), request_id, size - offset);
3466
3467 #ifdef WITH_LUA
3468 std::vector<int> _data;
3469 for (int i = offset; i < size; i++)
3470 {
3471 _data.push_back(packet->u.data[i]);
3472 }
3473 lua_handle_client_readmemory(c, request_id, _data);
3474 #endif
3475
3476 return 0;
3477 }
3478
3479 static int _client_statsupdate(t_connection * c, t_packet const *const packet)
3480 {
3481 if (packet_get_size(packet) < sizeof(t_client_statsupdate)) {
3482 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STATSUPDATE packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_statsupdate), packet_get_size(packet));
3483 return -1;
3484 }
3485
3486 {
3487 char const *name;
3488 char const *key;
3489 char const *val;
3490 unsigned int name_count;
3491 unsigned int key_count;
3492 unsigned int i, j;
3493 unsigned int name_off;
3494 unsigned int keys_off;
3495 unsigned int key_off;
3496 unsigned int vals_off;
3497 unsigned int val_off;
3498 t_account *account;
3499
3500 name_count = bn_int_get(packet->u.client_statsupdate.name_count);
3501 key_count = bn_int_get(packet->u.client_statsupdate.key_count);
3502
3503 if (name_count != 1)
3504 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] got suspicious STATSUPDATE packet (name_count={})", conn_get_socket(c), name_count);
3505
3506 for (i = 0, name_off = sizeof(t_client_statsupdate); i < name_count && (name = packet_get_str_const(packet, name_off, UNCHECKED_NAME_STR)); i++, name_off += std::strlen(name) + 1);
3507 if (i < name_count) {
3508 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STATSUPDATE packet (only {} names of {})", conn_get_socket(c), i, name_count);
3509 return -1;
3510 }
3511 keys_off = name_off;
3512
3513 for (i = 0, key_off = keys_off; i < key_count && (key = packet_get_str_const(packet, key_off, MAX_ATTRKEY_STR)); i++, key_off += std::strlen(key) + 1);
3514 if (i < key_count) {
3515 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STATSUPDATE packet (only {} keys of {})", conn_get_socket(c), i, key_count);
3516 return -1;
3517 }
3518 vals_off = key_off;
3519
3520 if ((account = conn_get_account(c))) {
3521 if (account_get_auth_changeprofile(account) == 0) { /* default to true */
3522 eventlog(eventlog_level_error, __FUNCTION__, "[{}] stats update for \"{}\" refused (no change profile access)", conn_get_socket(c), conn_get_username(c));
3523 return -1;
3524 }
3525 eventlog(eventlog_level_info, __FUNCTION__, "[{}] updating player profile for \"{}\"", conn_get_socket(c), conn_get_username(c));
3526
3527 for (i = 0, name_off = sizeof(t_client_statsupdate); i < name_count && (name = packet_get_str_const(packet, name_off, UNCHECKED_NAME_STR)); i++, name_off += std::strlen(name) + 1)
3528 for (j = 0, key_off = keys_off, val_off = vals_off; j < key_count && (key = packet_get_str_const(packet, key_off, MAX_ATTRKEY_STR)) && (val = packet_get_str_const(packet, val_off, MAX_ATTRVAL_STR)); j++, key_off += std::strlen(key) + 1, val_off += std::strlen(val) + 1)
3529 if (std::strlen(key) < 9 || strncasecmp(key, "profile\\", 8) != 0)
3530 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got STATSUPDATE with suspicious key \"{}\" value \"{}\"", conn_get_socket(c), key, val);
3531 else
3532 account_set_strattr(account, key, val);
3533 }
3534 }
3535
3536 return 0;
3537 }
3538
3539 static int _client_playerinforeq(t_connection * c, t_packet const *const packet)
3540 {
3541 t_packet *rpacket;
3542
3543 if (packet_get_size(packet) < sizeof(t_client_playerinforeq)) {
3544 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad PLAYERINFOREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_playerinforeq), packet_get_size(packet));
3545 return -1;
3546 }
3547
3548 {
3549 char const *username;
3550 char const *info;
3551 t_account *account;
3552
3553 if (!(username = packet_get_str_const(packet, sizeof(t_client_playerinforeq), MAX_USERNAME_LEN))) {
3554 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad PLAYERINFOREQ (missing or too long username)", conn_get_socket(c));
3555 return -1;
3556 }
3557 if (!(info = packet_get_str_const(packet, sizeof(t_client_playerinforeq)+std::strlen(username) + 1, MAX_PLAYERINFO_STR))) {
3558 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad PLAYERINFOREQ (missing or too long info)", conn_get_socket(c));
3559 return -1;
3560 }
3561
3562 if (info[0] != '\0')
3563 conn_set_playerinfo(c, info);
3564 if (!username[0])
3565 username = conn_get_loggeduser(c);
3566
3567 account = conn_get_account(c);
3568
3569 if (!(rpacket = packet_create(packet_class_bnet)))
3570 return -1;
3571 packet_set_size(rpacket, sizeof(t_server_playerinforeply));
3572 packet_set_type(rpacket, SERVER_PLAYERINFOREPLY);
3573
3574 if (account) {
3575 packet_append_string(rpacket, username);
3576 packet_append_string(rpacket, conn_get_playerinfo(c));
3577 packet_append_string(rpacket, username);
3578 }
3579 else {
3580 packet_append_string(rpacket, "");
3581 packet_append_string(rpacket, "");
3582 packet_append_string(rpacket, "");
3583 }
3584 conn_push_outqueue(c, rpacket);
3585 packet_del_ref(rpacket);
3586 }
3587
3588 return 0;
3589 }
3590
3591 static int _client_progident2(t_connection * c, t_packet const *const packet)
3592 {
3593 t_packet *rpacket;
3594
3595 if (packet_get_size(packet) < sizeof(t_client_progident2)) {
3596 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad PROGIDENT2 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_progident2), packet_get_size(packet));
3597 return -1;
3598 }
3599
3600 /* d2 uses this packet with clienttag = 0 to request the channel list */
3601 if (bn_int_get(packet->u.client_progident2.clienttag)) {
3602 if (tag_check_in_list(bn_int_get(packet->u.client_progident2.clienttag), prefs_get_allowed_clients())) {
3603 conn_set_state(c, conn_state_destroy);
3604 return 0;
3605 }
3606 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] CLIENT_PROGIDENT2 clienttag=0x{:08x}", conn_get_socket(c), bn_int_get(packet->u.client_progident2.clienttag));
3607
3608 /* Hmm... no archtag. Hope we get it in CLIENT_AUTHREQ1 (but we won't if we use the shortcut) */
3609
3610 conn_set_clienttag(c, bn_int_get(packet->u.client_progident2.clienttag));
3611 }
3612
3613 if ((rpacket = packet_create(packet_class_bnet))) {
3614 packet_set_size(rpacket, sizeof(t_server_channellist));
3615 packet_set_type(rpacket, SERVER_CHANNELLIST);
3616 {
3617 t_channel *ch;
3618 t_elem const *curr;
3619
3620 LIST_TRAVERSE_CONST(channellist(), curr) {
3621 ch = (t_channel*)elem_get_data(curr);
3622 if ((!(channel_get_flags(ch) & channel_flags_clan)) && (!prefs_get_hide_temp_channels() || channel_get_permanent(ch)) && (!channel_get_clienttag(ch) || channel_get_clienttag(ch) == conn_get_clienttag(c)) && (!(channel_get_flags(ch) & channel_flags_thevoid)) && // don't display theVoid in channel list
3623 ((channel_get_max(ch) != 0) || ((channel_get_max(ch) == 0) && (account_is_operator_or_admin(conn_get_account(c), channel_get_name(ch)) == 1)))) // don't display restricted channel for no admins/ops
3624 packet_append_string(rpacket, channel_get_name(ch));
3625 }
3626 }
3627 packet_append_string(rpacket, "");
3628 conn_push_outqueue(c, rpacket);
3629 packet_del_ref(rpacket);
3630 }
3631
3632 return 0;
3633 }
3634
3635 static int _client_joinchannel(t_connection * c, t_packet const *const packet)
3636 {
3637 t_account *account;
3638 char const *cname;
3639 int found = 1;
3640 t_clan *user_clan;
3641 t_clantag clantag;
3642 std::uint32_t clienttag;
3643 t_channel *channel;
3644
3645 if (packet_get_size(packet) < sizeof(t_client_joinchannel)) {
3646 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad JOINCHANNEL packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_joinchannel), packet_get_size(packet));
3647 return -1;
3648 }
3649
3650 account = conn_get_account(c);
3651
3652 if (!(cname = packet_get_str_const(packet, sizeof(t_client_joinchannel), MAX_CHANNELNAME_LEN))) {
3653 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad JOINCHANNEL (missing or too long cname)", conn_get_socket(c));
3654 return -1;
3655 }
3656
3657 if ((channel = conn_get_channel(c)) && (strcasecmp(channel_get_name(channel), cname) == 0))
3658 return 0; //we are already in this channel
3659
3660 std::string tmpstr;
3661 clienttag = conn_get_clienttag(c);
3662 if ((clienttag == CLIENTTAG_WARCRAFT3_UINT) || (clienttag == CLIENTTAG_WAR3XP_UINT)) {
3663 conn_update_w3_playerinfo(c);
3664 switch (bn_int_get(packet->u.client_joinchannel.channelflag)) {
3665 case CLIENT_JOINCHANNEL_NORMAL:
3666 eventlog(eventlog_level_info, __FUNCTION__, "[{}] CLIENT_JOINCHANNEL_NORMAL channel \"{}\"", conn_get_socket(c), cname);
3667
3668 if (prefs_get_ask_new_channel() && (!(channellist_find_channel_by_name(cname, conn_get_country(c), realm_get_name(conn_get_realm(c)))))) {
3669 found = 0;
3670 eventlog(eventlog_level_info, __FUNCTION__, "[{}] didn't find channel \"{}\" to join", conn_get_socket(c), cname);
3671 message_send_text(c, message_type_channeldoesnotexist, c, cname);
3672 }
3673 break;
3674 case CLIENT_JOINCHANNEL_GENERIC:
3675
3676 if ((user_clan = account_get_clan(account)) && (clantag = clan_get_clantag(user_clan)))
3677 {
3678 std::ostringstream ostr;
3679 ostr << "Clan " << clantag_to_str(clantag);
3680 tmpstr = ostr.str();
3681 cname = tmpstr.c_str();
3682 }
3683 eventlog(eventlog_level_info, __FUNCTION__, "[{}] CLIENT_JOINCHANNEL_GENERIC channel \"{}\"", conn_get_socket(c), cname);
3684
3685 /* don't have to do anything here */
3686 break;
3687 case CLIENT_JOINCHANNEL_CREATE:
3688 eventlog(eventlog_level_info, __FUNCTION__, "[{}] CLIENT_JOINCHANNEL_CREATE channel \"{}\"", conn_get_socket(c), cname);
3689 eventlog(eventlog_level_info, __FUNCTION__, "[{}] CLIENT_JOINCHANNEL_CREATE channel \"{}\"", conn_get_socket(c), cname);
3690 /* don't have to do anything here */
3691 break;
3692 }
3693
3694 if (found && conn_set_channel(c, cname) < 0)
3695 conn_set_channel(c, CHANNEL_NAME_BANNED); /* should not fail */
3696 }
3697 else {
3698
3699 // not W3
3700 if (conn_set_channel(c, cname) < 0)
3701 conn_set_channel(c, CHANNEL_NAME_BANNED); /* should not fail */
3702 }
3703 // here we set channel flags on user
3704 channel_set_userflags(c);
3705
3706 return 0;
3707 }
3708
3709 static int _client_message(t_connection * c, t_packet const *const packet)
3710 {
3711 if (packet_get_size(packet) < sizeof(t_client_message)) {
3712 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad MESSAGE packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_message), packet_get_size(packet));
3713 return -1;
3714 }
3715
3716 {
3717 char const *text;
3718 t_channel const *channel;
3719
3720 if (!(text = packet_get_str_const(packet, sizeof(t_client_message), MAX_MESSAGE_LEN))) {
3721 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad MESSAGE (missing or too long text)", conn_get_socket(c));
3722 return -1;
3723 }
3724
3725 conn_set_idletime(c);
3726
3727 if ((channel = conn_get_channel(c)))
3728 channel_message_log(channel, c, 1, text);
3729 /* we don't log game commands currently */
3730
3731
3732
3733 if (text[0] == '/')
3734 handle_command(c, text);
3735 else if (channel && !conn_quota_exceeded(c, text))
3736 channel_message_send(channel, message_type_talk, c, text);
3737 /* else discard */
3738 }
3739
3740 return 0;
3741 }
3742
3743 static int _glist_cb(t_game * game, void *data)
3744 {
3745 struct glist_cbdata *cbdata = (struct glist_cbdata*)data;
3746 char clienttag_str[5];
3747 t_server_gamelistreply_game glgame;
3748 unsigned int addr;
3749 unsigned short port;
3750 bn_int game_spacer = { 1, 0, 0, 0 };
3751
3752 cbdata->tcount++;
3753 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] considering listing game=\"{}\", pass=\"{}\" clienttag=\"{}\" gtype={}", conn_get_socket(cbdata->c), game_get_name(game), game_get_pass(game), tag_uint_to_str(clienttag_str, game_get_clienttag(game)), (int)game_get_type(game));
3754
3755 if (prefs_get_hide_pass_games() && game_get_flag(game) == game_flag_private) {
3756 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] not listing because game is passworded or has private flag", conn_get_socket(cbdata->c));
3757 return 0;
3758 }
3759 if (prefs_get_hide_started_games() && game_get_status(game) != game_status_open) {
3760 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] not listing because game is not open", conn_get_socket(cbdata->c));
3761 return 0;
3762 }
3763 if (game_get_clienttag(game) != conn_get_clienttag(cbdata->c)) {
3764 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] not listing because game is for a different client", conn_get_socket(cbdata->c));
3765 return 0;
3766 }
3767 if (cbdata->gtype != game_type_all && game_get_type(game) != cbdata->gtype) {
3768 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] not listing because game is wrong type", conn_get_socket(cbdata->c));
3769 return 0;
3770 }
3771 if (conn_get_versioncheck(cbdata->c) &&
3772 conn_get_versioncheck(game_get_owner(game)) &&
3773 conn_get_versioncheck(cbdata->c)->get_version_tag() != conn_get_versioncheck(game_get_owner(game))->get_version_tag())
3774 {
3775 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] not listing because game is wrong versiontag", conn_get_socket(cbdata->c));
3776 return 0;
3777 }
3778
3779 bn_short_set(&glgame.gametype, gtype_to_bngtype(game_get_type(game)));
3780 bn_short_set(&glgame.unknown1, SERVER_GAMELISTREPLY_GAME_UNKNOWN1);
3781 bn_short_set(&glgame.unknown3, SERVER_GAMELISTREPLY_GAME_UNKNOWN3);
3782 addr = game_get_addr(game);
3783 port = game_get_port(game);
3784 trans_net(conn_get_addr(cbdata->c), &addr, &port);
3785 bn_short_nset(&glgame.port, port);
3786 bn_int_nset(&glgame.game_ip, addr);
3787 bn_int_set(&glgame.unknown4, SERVER_GAMELISTREPLY_GAME_UNKNOWN4);
3788 bn_int_set(&glgame.unknown5, SERVER_GAMELISTREPLY_GAME_UNKNOWN5);
3789 switch (game_get_status(game)) {
3790 case game_status_started:
3791 bn_int_set(&glgame.status, SERVER_GAMELISTREPLY_GAME_STATUS_STARTED);
3792 break;
3793 case game_status_full:
3794 bn_int_set(&glgame.status, SERVER_GAMELISTREPLY_GAME_STATUS_FULL);
3795 break;
3796 case game_status_open:
3797 bn_int_set(&glgame.status, SERVER_GAMELISTREPLY_GAME_STATUS_OPEN);
3798 break;
3799 case game_status_done:
3800 bn_int_set(&glgame.status, SERVER_GAMELISTREPLY_GAME_STATUS_DONE);
3801 break;
3802 default:
3803 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] game \"{}\" has bad status={}", conn_get_socket(cbdata->c), game_get_name(game), (int)game_get_status(game));
3804 bn_int_set(&glgame.status, 0);
3805 }
3806 bn_int_set(&glgame.unknown6, SERVER_GAMELISTREPLY_GAME_UNKNOWN6);
3807
3808 if (packet_get_size(cbdata->rpacket) + sizeof(glgame)+std::strlen(game_get_name(game)) + 1 + std::strlen(game_get_pass(game)) + 1 + std::strlen(game_get_info(game)) + 1 > MAX_PACKET_SIZE) {
3809 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] out of room for games", conn_get_socket(cbdata->c));
3810 return -1; /* no more room */
3811 }
3812
3813 if (cbdata->counter) {
3814 packet_append_data(cbdata->rpacket, &game_spacer, sizeof(game_spacer));
3815 }
3816
3817 packet_append_data(cbdata->rpacket, &glgame, sizeof(glgame));
3818 packet_append_string(cbdata->rpacket, game_get_name(game));
3819 packet_append_string(cbdata->rpacket, game_get_pass(game));
3820 packet_append_string(cbdata->rpacket, game_get_info(game));
3821 cbdata->counter++;
3822
3823 return 0;
3824 }
3825
3826 static int _client_gamelistreq(t_connection * c, t_packet const *const packet)
3827 {
3828 t_packet *rpacket;
3829 char const *gamename;
3830 char const *gamepass;
3831 unsigned short bngtype;
3832 t_game_type gtype;
3833 t_clienttag clienttag;
3834 t_game *game;
3835 t_server_gamelistreply_game glgame;
3836 unsigned int addr;
3837 unsigned short port;
3838 char clienttag_str[5];
3839
3840 if (packet_get_size(packet) < sizeof(t_client_gamelistreq)) {
3841 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad GAMELISTREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_gamelistreq), packet_get_size(packet));
3842 return -1;
3843 }
3844
3845 if (!(gamename = packet_get_str_const(packet, sizeof(t_client_gamelistreq), MAX_GAMENAME_LEN))) {
3846 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad GAMELISTREQ (missing or too long gamename)", conn_get_socket(c));
3847 return -1;
3848 }
3849
3850 if (!(gamepass = packet_get_str_const(packet, sizeof(t_client_gamelistreq)+std::strlen(gamename) + 1, MAX_GAMEPASS_LEN))) {
3851 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad GAMELISTREQ (missing or too long password)", conn_get_socket(c));
3852 return -1;
3853 }
3854
3855 bngtype = bn_short_get(packet->u.client_gamelistreq.gametype);
3856 clienttag = conn_get_clienttag(c);
3857 gtype = bngreqtype_to_gtype(clienttag, bngtype);
3858 if (!(rpacket = packet_create(packet_class_bnet)))
3859 return -1;
3860 packet_set_size(rpacket, sizeof(t_server_gamelistreply));
3861 packet_set_type(rpacket, SERVER_GAMELISTREPLY);
3862
3863 bn_int_set(&rpacket->u.server_gamelistreply.sstatus, 0);
3864
3865 /* specific game requested? */
3866 if (gamename[0] != '\0') {
3867 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] GAMELISTREPLY looking for specific game tag=\"{}\" bngtype=0x{:08x} gtype={} name=\"{}\" pass=\"{}\"", conn_get_socket(c), tag_uint_to_str(clienttag_str, clienttag), bngtype, (int)gtype, gamename, gamepass);
3868 if ((game = gamelist_find_game(gamename, clienttag, gtype))) {
3869 /* game found but first we need to make sure everything is OK */
3870 bn_int_set(&rpacket->u.server_gamelistreply.gamecount, 0);
3871 switch (game_get_status(game)) {
3872 case game_status_started:
3873 bn_int_set(&rpacket->u.server_gamelistreply.sstatus, SERVER_GAMELISTREPLY_GAME_SSTATUS_STARTED);
3874 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] GAMELISTREPLY found but started", conn_get_socket(c));
3875 break;
3876 case game_status_full:
3877 bn_int_set(&rpacket->u.server_gamelistreply.sstatus, SERVER_GAMELISTREPLY_GAME_SSTATUS_FULL);
3878 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] GAMELISTREPLY found but full", conn_get_socket(c));
3879 break;
3880 case game_status_done:
3881 bn_int_set(&rpacket->u.server_gamelistreply.sstatus, SERVER_GAMELISTREPLY_GAME_SSTATUS_NOTFOUND);
3882 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] GAMELISTREPLY found but done", conn_get_socket(c));
3883 break;
3884 case game_status_open:
3885 case game_status_loaded:
3886 if (std::strcmp(gamepass, game_get_pass(game))) { /* passworded game must match password in request */
3887 bn_int_set(&rpacket->u.server_gamelistreply.sstatus, SERVER_GAMELISTREPLY_GAME_SSTATUS_PASS);
3888 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] GAMELISTREPLY found but is password protected and wrong password given", conn_get_socket(c));
3889 break;
3890 }
3891
3892 if (game_get_status(game) == game_status_loaded) {
3893 bn_int_set(&rpacket->u.server_gamelistreply.sstatus, SERVER_GAMELISTREPLY_GAME_SSTATUS_LOADED);
3894 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] GAMELISTREPLY found loaded game", conn_get_socket(c));
3895 }
3896
3897 /* everything seems fine, lets reply with the found game */
3898 bn_int_set(&glgame.status, SERVER_GAMELISTREPLY_GAME_STATUS_OPEN);
3899 bn_short_set(&glgame.gametype, gtype_to_bngtype(game_get_type(game)));
3900 bn_short_set(&glgame.unknown1, SERVER_GAMELISTREPLY_GAME_UNKNOWN1);
3901 bn_short_set(&glgame.unknown3, SERVER_GAMELISTREPLY_GAME_UNKNOWN3);
3902 addr = game_get_addr(game);
3903 port = game_get_port(game);
3904 trans_net(conn_get_addr(c), &addr, &port);
3905 bn_short_nset(&glgame.port, port);
3906 bn_int_nset(&glgame.game_ip, addr);
3907 bn_int_set(&glgame.unknown4, SERVER_GAMELISTREPLY_GAME_UNKNOWN4);
3908 bn_int_set(&glgame.unknown5, SERVER_GAMELISTREPLY_GAME_UNKNOWN5);
3909 bn_int_set(&glgame.unknown6, SERVER_GAMELISTREPLY_GAME_UNKNOWN6);
3910
3911 packet_append_data(rpacket, &glgame, sizeof(glgame));
3912 packet_append_string(rpacket, game_get_name(game));
3913 packet_append_string(rpacket, game_get_pass(game));
3914 packet_append_string(rpacket, game_get_info(game));
3915 bn_int_set(&rpacket->u.server_gamelistreply.gamecount, 1);
3916 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] GAMELISTREPLY specific game found", conn_get_socket(c));
3917 break;
3918 default:
3919 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] game \"{}\" has bad status {}", conn_get_socket(c), game_get_name(game), game_get_status(game));
3920 }
3921 }
3922 else {
3923 bn_int_set(&rpacket->u.server_gamelistreply.gamecount, 0);
3924 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] GAMELISTREPLY specific game doesn't seem to exist", conn_get_socket(c));
3925 }
3926 }
3927 else { /* list all public games of this type */
3928 struct glist_cbdata cbdata;
3929
3930 if (gtype == game_type_all)
3931 eventlog(eventlog_level_debug, __FUNCTION__, "GAMELISTREPLY looking for public games tag=\"{}\" bngtype=0x{:08x} gtype=all", tag_uint_to_str(clienttag_str, clienttag), bngtype);
3932 else
3933 eventlog(eventlog_level_debug, __FUNCTION__, "GAMELISTREPLY looking for public games tag=\"{}\" bngtype=0x{:08x} gtype={}", tag_uint_to_str(clienttag_str, clienttag), bngtype, (int)gtype);
3934
3935 cbdata.counter = 0;
3936 cbdata.tcount = 0;
3937 cbdata.c = c;
3938 cbdata.gtype = gtype;
3939 cbdata.rpacket = rpacket;
3940 gamelist_traverse(_glist_cb, &cbdata, gamelist_source_joinbutton);
3941
3942 bn_int_set(&rpacket->u.server_gamelistreply.gamecount, cbdata.counter);
3943 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] GAMELISTREPLY sent {} of {} games", conn_get_socket(c), cbdata.counter, cbdata.tcount);
3944 }
3945
3946 conn_push_outqueue(c, rpacket);
3947 packet_del_ref(rpacket);
3948
3949 return 0;
3950 }
3951
3952 static int _client_joingame(t_connection * c, t_packet const *const packet)
3953 {
3954 char const *gamename;
3955 char const *gamepass;
3956 t_game *game;
3957 t_game_type gtype;
3958
3959 if (packet_get_size(packet) < sizeof(t_client_join_game)) {
3960 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad JOIN_GAME packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_join_game), packet_get_size(packet));
3961 return -1;
3962 }
3963
3964 if (!(gamename = packet_get_str_const(packet, sizeof(t_client_join_game), MAX_GAMENAME_LEN))) {
3965 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_JOIN_GAME (missing or too long gamename)", conn_get_socket(c));
3966 return -1;
3967 }
3968
3969 if (!(gamepass = packet_get_str_const(packet, sizeof(t_client_join_game)+std::strlen(gamename) + 1, MAX_GAMEPASS_LEN))) {
3970 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_JOIN_GAME packet (missing or too long gamepass)", conn_get_socket(c));
3971 return -1;
3972 }
3973
3974 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] trying to join game \"{}\" pass=\"{}\"", conn_get_socket(c), gamename, gamepass);
3975
3976 if (conn_get_joingamewhisper_ack(c) == 0) {
3977 watchlist->dispatch(conn_get_account(c), gamename, conn_get_clienttag(c), Watch::ET_joingame);
3978 conn_set_joingamewhisper_ack(c, 1); /* 1 = already whispered. We reset this each time user joins a channel */
3979 clanmember_on_change_status_by_connection(c);
3980 }
3981
3982 if (conn_get_channel(c))
3983 conn_part_channel(c);
3984
3985 if (!std::strcmp(gamename, "BNet") && !handle_anongame_join(c)) {
3986 gtype = game_type_anongame;
3987 gamename = NULL;
3988 return 0; /* tmp: do not record any anongames as yet */
3989 }
3990 else {
3991 if (!(game = gamelist_find_game_available(gamename, conn_get_clienttag(c), game_type_all))) {
3992 eventlog(eventlog_level_info, __FUNCTION__, "[{}] unable to find game \"{}\" for user to join", conn_get_socket(c), gamename);
3993 return 0;
3994 }
3995 gtype = game_get_type(game);
3996 gamename = game_get_name(game);
3997 if ((gtype == game_type_ladder && account_get_auth_joinladdergame(conn_get_account(c)) == 0) || /* default to true */
3998 (gtype != game_type_ladder && account_get_auth_joinnormalgame(conn_get_account(c)) == 0)) { /* default to true */
3999 eventlog(eventlog_level_info, __FUNCTION__, "[{}] game join for \"{}\" to \"{}\" refused (no authority)", conn_get_socket(c), conn_get_username(c), gamename);
4000 /* If the user is not in a game, then map authorization
4001 will fail and keep them from playing. */
4002 return 0;
4003 }
4004 }
4005
4006 if (conn_set_game(c, gamename, gamepass, "", gtype, STARTVER_UNKNOWN) < 0)
4007 eventlog(eventlog_level_info, __FUNCTION__, "[{}] \"{}\" joined game \"{}\", but could not be recorded on server", conn_get_socket(c), conn_get_username(c), gamename);
4008 else
4009 eventlog(eventlog_level_info, __FUNCTION__, "[{}] \"{}\" joined game \"{}\"", conn_get_socket(c), conn_get_username(c), gamename);
4010
4011 #ifdef WITH_LUA
4012 lua_handle_game(game, c, luaevent_game_userjoin);
4013 #endif
4014
4015 return 0;
4016 }
4017
4018 static int _client_startgame1(t_connection * c, t_packet const *const packet)
4019 {
4020 t_packet *rpacket;
4021
4022 if (packet_get_size(packet) < sizeof(t_client_startgame1)) {
4023 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME1 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_startgame1), packet_get_size(packet));
4024 return -1;
4025 }
4026
4027 {
4028 char const *gamename;
4029 char const *gamepass;
4030 char const *gameinfo;
4031 unsigned short bngtype;
4032 unsigned int status;
4033 t_game *currgame;
4034
4035 if (!(gamename = packet_get_str_const(packet, sizeof(t_client_startgame1), MAX_GAMENAME_LEN))) {
4036 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME1 packet (missing or too long gamename)", conn_get_socket(c));
4037 return -1;
4038 }
4039 if (!(gamepass = packet_get_str_const(packet, sizeof(t_client_startgame1)+std::strlen(gamename) + 1, MAX_GAMEPASS_LEN))) {
4040 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME1 packet (missing or too long gamepass)", conn_get_socket(c));
4041 return -1;
4042 }
4043 if (!(gameinfo = packet_get_str_const(packet, sizeof(t_client_startgame1)+std::strlen(gamename) + 1 + std::strlen(gamepass) + 1, MAX_GAMEINFO_LEN))) {
4044 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME1 packet (missing or too long gameinfo)", conn_get_socket(c));
4045 return -1;
4046 }
4047 if (conn_get_joingamewhisper_ack(c) == 0) {
4048 if (watchlist->dispatch(conn_get_account(c), gamename, conn_get_clienttag(c), Watch::ET_joingame) == 0)
4049 eventlog(eventlog_level_info, "handle_bnet", "Told Mutual Friends your in game {}", gamename);
4050
4051 conn_set_joingamewhisper_ack(c, 1); //1 = already whispered. We reset this each time user joins a channel
4052 }
4053
4054
4055 bngtype = bn_short_get(packet->u.client_startgame1.gametype);
4056 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got startgame1 status for game \"{}\" is 0x{:08x} (gametype = 0x{:04x})", conn_get_socket(c), gamename, bn_int_get(packet->u.client_startgame1.status), bngtype);
4057 status = bn_int_get(packet->u.client_startgame1.status) & CLIENT_STARTGAME1_STATUSMASK;
4058
4059 if ((currgame = conn_get_game(c))) {
4060 switch (status) {
4061 case CLIENT_STARTGAME1_STATUS_STARTED:
4062 game_set_status(currgame, game_status_started);
4063 break;
4064 case CLIENT_STARTGAME1_STATUS_FULL:
4065 game_set_status(currgame, game_status_full);
4066 break;
4067 case CLIENT_STARTGAME1_STATUS_OPEN:
4068 game_set_status(currgame, game_status_open);
4069 break;
4070 case CLIENT_STARTGAME1_STATUS_DONE:
4071 game_set_status(currgame, game_status_done);
4072 eventlog(eventlog_level_info, __FUNCTION__, "[{}] game \"{}\" is finished", conn_get_socket(c), gamename);
4073 break;
4074 }
4075 }
4076 else if (status != CLIENT_STARTGAME1_STATUS_DONE) {
4077 t_game_type gtype;
4078
4079 gtype = bngtype_to_gtype(conn_get_clienttag(c), bngtype);
4080 if ((gtype == game_type_ladder && account_get_auth_createladdergame(conn_get_account(c)) == 0) || /* default to true */
4081 (gtype != game_type_ladder && account_get_auth_createnormalgame(conn_get_account(c)) == 0)) /* default to true */
4082 eventlog(eventlog_level_info, __FUNCTION__, "[{}] game start for \"{}\" refused (no authority)", conn_get_socket(c), conn_get_username(c));
4083 else
4084 conn_set_game(c, gamename, gamepass, gameinfo, gtype, STARTVER_GW1);
4085
4086 if ((rpacket = packet_create(packet_class_bnet))) {
4087 packet_set_size(rpacket, sizeof(t_server_startgame1_ack));
4088 packet_set_type(rpacket, SERVER_STARTGAME1_ACK);
4089
4090 if (conn_get_game(c))
4091 bn_int_set(&rpacket->u.server_startgame1_ack.reply, SERVER_STARTGAME1_ACK_OK);
4092 else
4093 bn_int_set(&rpacket->u.server_startgame1_ack.reply, SERVER_STARTGAME1_ACK_NO);
4094
4095 conn_push_outqueue(c, rpacket);
4096 packet_del_ref(rpacket);
4097 }
4098 }
4099 else
4100 eventlog(eventlog_level_info, __FUNCTION__, "[{}] client tried to set game status DONE to destroyed game", conn_get_socket(c));
4101 }
4102
4103 return 0;
4104 }
4105
4106 static int _client_startgame3(t_connection * c, t_packet const *const packet)
4107 {
4108 t_packet *rpacket;
4109
4110 if (packet_get_size(packet) < sizeof(t_client_startgame3)) {
4111 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME3 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_startgame3), packet_get_size(packet));
4112 return -1;
4113 }
4114
4115 {
4116 char const *gamename;
4117 char const *gamepass;
4118 char const *gameinfo;
4119 unsigned short bngtype;
4120 unsigned int status;
4121 t_game *currgame;
4122
4123 if (!(gamename = packet_get_str_const(packet, sizeof(t_client_startgame3), MAX_GAMENAME_LEN))) {
4124 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME3 packet (missing or too long gamename)", conn_get_socket(c));
4125 return -1;
4126 }
4127 if (!(gamepass = packet_get_str_const(packet, sizeof(t_client_startgame3)+std::strlen(gamename) + 1, MAX_GAMEPASS_LEN))) {
4128 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME3 packet (missing or too long gamepass)", conn_get_socket(c));
4129 return -1;
4130 }
4131 if (!(gameinfo = packet_get_str_const(packet, sizeof(t_client_startgame3)+std::strlen(gamename) + 1 + std::strlen(gamepass) + 1, MAX_GAMEINFO_LEN))) {
4132 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME3 packet (missing or too long gameinfo)", conn_get_socket(c));
4133 return -1;
4134 }
4135 if (conn_get_joingamewhisper_ack(c) == 0) {
4136 if (watchlist->dispatch(conn_get_account(c), gamename, conn_get_clienttag(c), Watch::ET_joingame) == 0)
4137 eventlog(eventlog_level_info, "handle_bnet", "Told Mutual Friends your in game {}", gamename);
4138
4139 conn_set_joingamewhisper_ack(c, 1); //1 = already whispered. We reset this each time user joins a channel
4140 }
4141 bngtype = bn_short_get(packet->u.client_startgame3.gametype);
4142 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got startgame3 status for game \"{}\" is 0x{:08x} (gametype = 0x{:04x})", conn_get_socket(c), gamename, bn_int_get(packet->u.client_startgame3.status), bngtype);
4143 status = bn_int_get(packet->u.client_startgame3.status) & CLIENT_STARTGAME3_STATUSMASK;
4144
4145 if ((currgame = conn_get_game(c))) {
4146 switch (status) {
4147 case CLIENT_STARTGAME3_STATUS_STARTED:
4148 game_set_status(currgame, game_status_started);
4149 break;
4150 case CLIENT_STARTGAME3_STATUS_FULL:
4151 game_set_status(currgame, game_status_full);
4152 break;
4153 case CLIENT_STARTGAME3_STATUS_OPEN1:
4154 case CLIENT_STARTGAME3_STATUS_OPEN:
4155 game_set_status(currgame, game_status_open);
4156 break;
4157 case CLIENT_STARTGAME3_STATUS_DONE:
4158 game_set_status(currgame, game_status_done);
4159 eventlog(eventlog_level_info, __FUNCTION__, "[{}] game \"{}\" is finished", conn_get_socket(c), gamename);
4160 break;
4161 }
4162 }
4163 else if (status != CLIENT_STARTGAME3_STATUS_DONE) {
4164 t_game_type gtype;
4165
4166 gtype = bngtype_to_gtype(conn_get_clienttag(c), bngtype);
4167 if ((gtype == game_type_ladder && account_get_auth_createladdergame(conn_get_account(c)) == 0) || (gtype != game_type_ladder && account_get_auth_createnormalgame(conn_get_account(c)) == 0))
4168 eventlog(eventlog_level_info, __FUNCTION__, "[{}] game start for \"{}\" refused (no authority)", conn_get_socket(c), conn_get_username(c));
4169 else
4170 conn_set_game(c, gamename, gamepass, gameinfo, gtype, STARTVER_GW3);
4171
4172 if ((rpacket = packet_create(packet_class_bnet))) {
4173 packet_set_size(rpacket, sizeof(t_server_startgame3_ack));
4174 packet_set_type(rpacket, SERVER_STARTGAME3_ACK);
4175
4176 if (conn_get_game(c))
4177 bn_int_set(&rpacket->u.server_startgame3_ack.reply, SERVER_STARTGAME3_ACK_OK);
4178 else
4179 bn_int_set(&rpacket->u.server_startgame3_ack.reply, SERVER_STARTGAME3_ACK_NO);
4180
4181 conn_push_outqueue(c, rpacket);
4182 packet_del_ref(rpacket);
4183 }
4184 }
4185 else
4186 eventlog(eventlog_level_info, __FUNCTION__, "[{}] client tried to set game status DONE to destroyed game", conn_get_socket(c));
4187 }
4188
4189 return 0;
4190 }
4191
4192 static int _client_startgame4(t_connection * c, t_packet const *const packet)
4193 {
4194 t_packet *rpacket;
4195
4196 if (packet_get_size(packet) < sizeof(t_client_startgame4)) {
4197 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME4 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_startgame4), packet_get_size(packet));
4198 return -1;
4199 }
4200
4201 if (conn_get_clienttag(c) == CLIENTTAG_STARCRAFT_UINT || conn_get_clienttag(c) == CLIENTTAG_BROODWARS_UINT)
4202 {
4203 // FIXME: (HarpyWar) Protection from hack attempt
4204 // Large map name size will cause crash Starcraft client for user who select an item in game list ("Join" area)
4205 // It occurs when the packet size of packet 0x0c in length interval 161-164
4206 // https://github.com/pvpgn/pvpgn-server/issues/159
4207 if (packet_get_size(packet) > 160)
4208 {
4209 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got abnormal STARTGAME4 packet length (got {} bytes, hack attempt?)", conn_get_socket(c), packet_get_size(packet));
4210 return -1;
4211 }
4212 }
4213
4214 // Quick hack to make W3 part channels when creating a game
4215 if (conn_get_channel(c))
4216 conn_part_channel(c);
4217
4218 {
4219 char const *gamename;
4220 char const *gamepass;
4221 char const *gameinfo;
4222 unsigned short bngtype;
4223 unsigned int status;
4224 unsigned int flag;
4225 unsigned short option;
4226 t_game *currgame;
4227
4228 if (!(gamename = packet_get_str_const(packet, sizeof(t_client_startgame4), MAX_GAMENAME_LEN))) {
4229 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME4 packet (missing or too long gamename)", conn_get_socket(c));
4230 return -1;
4231 }
4232 if (!(gamepass = packet_get_str_const(packet, sizeof(t_client_startgame4)+std::strlen(gamename) + 1, MAX_GAMEPASS_LEN))) {
4233 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME4 packet (missing or too long gamepass)", conn_get_socket(c));
4234 return -1;
4235 }
4236 if (!(gameinfo = packet_get_str_const(packet, sizeof(t_client_startgame4)+std::strlen(gamename) + 1 + std::strlen(gamepass) + 1, MAX_GAMEINFO_LEN))) {
4237 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad STARTGAME4 packet (missing or too long gameinfo)", conn_get_socket(c));
4238 return -1;
4239 }
4240 if (conn_get_joingamewhisper_ack(c) == 0) {
4241 if (watchlist->dispatch(conn_get_account(c), gamename, conn_get_clienttag(c), Watch::ET_joingame) == 0)
4242 eventlog(eventlog_level_info, "handle_bnet", "Told Mutual Friends your in game {}", gamename);
4243
4244 conn_set_joingamewhisper_ack(c, 1); //1 = already whispered. We reset this each time user joins a channel
4245 }
4246 bngtype = bn_short_get(packet->u.client_startgame4.gametype);
4247 option = bn_short_get(packet->u.client_startgame4.option);
4248 status = bn_int_get(packet->u.client_startgame4.status);
4249 flag = bn_short_get(packet->u.client_startgame4.flag);
4250
4251 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got startgame4 status for game \"{}\" is 0x{:08x} (gametype=0x{:04x} option=0x{:04x}, flag=0x{:04x})", conn_get_socket(c), gamename, status, bngtype, option, flag);
4252
4253 if ((currgame = conn_get_game(c))) {
4254 if ((status & CLIENT_STARTGAME4_STATUSMASK_OPEN_VALID) == status) {
4255 if (status & CLIENT_STARTGAME4_STATUS_START)
4256 game_set_status(currgame, game_status_started);
4257 else if (status & CLIENT_STARTGAME4_STATUS_FULL)
4258 game_set_status(currgame, game_status_full);
4259 else
4260 game_set_status(currgame, game_status_open);
4261 }
4262 else {
4263 eventlog(eventlog_level_error, __FUNCTION__, "[{}] unknown startgame4 status {} (clienttag: {})", conn_get_socket(c), status, clienttag_uint_to_str(conn_get_clienttag(c)));
4264 }
4265
4266 }
4267 else if ((status & CLIENT_STARTGAME4_STATUSMASK_INIT_VALID) == status) {
4268 /*valid creation status would be:
4269 0x00, 0x01, 0x02, 0x03, 0x10, 0x11, 0x12, 0x13, 0x80, 0x81, 0x82, 0x83 */
4270
4271 t_game_type gtype;
4272 bool allow_create_custom = false;
4273 t_game *game;
4274
4275 gtype = bngtype_to_gtype(conn_get_clienttag(c), bngtype);
4276 if ((gtype == game_type_ladder && account_get_auth_createladdergame(conn_get_account(c)) == 0) || (gtype != game_type_ladder && account_get_auth_createnormalgame(conn_get_account(c)) == 0))
4277 eventlog(eventlog_level_info, __FUNCTION__, "[{}] game start for \"{}\" refused (no authority)", conn_get_socket(c), conn_get_username(c));
4278 else
4279 {
4280 //find is there any existing game with same name and allow the host to create game
4281 // with same name only when another game is already started or already done
4282 if ((!(game = gamelist_find_game_available(gamename, conn_get_clienttag(c), game_type_all))) &&
4283 (conn_set_game(c, gamename, gamepass, gameinfo, gtype, STARTVER_GW4) == 0))
4284 {
4285 game_set_option(conn_get_game(c), bngoption_to_goption(conn_get_clienttag(c), gtype, option));
4286 if (status & CLIENT_STARTGAME4_STATUS_PRIVATE)
4287 game_set_flag(conn_get_game(c), game_flag_private);
4288 if (status & CLIENT_STARTGAME4_STATUS_FULL)
4289 game_set_status(conn_get_game(c), game_status_full);
4290 if (bngtype == CLIENT_GAMELISTREQ_LOADED) /* PELISH: seems strange but it is really needed for loaded games */
4291 game_set_status(conn_get_game(c), game_status_loaded);
4292 //FIXME: still need special handling for status disc-is-loss and replay
4293
4294 }
4295 }
4296 }
4297 else
4298 eventlog(eventlog_level_info, __FUNCTION__, "[{}] client tried to set game status 0x{:x} to unexistent game (clienttag: {})", conn_get_socket(c), status, clienttag_uint_to_str(conn_get_clienttag(c)));
4299 }
4300
4301 if ((rpacket = packet_create(packet_class_bnet))) {
4302 packet_set_size(rpacket, sizeof(t_server_startgame4_ack));
4303 packet_set_type(rpacket, SERVER_STARTGAME4_ACK);
4304
4305 if (conn_get_game(c))
4306 bn_int_set(&rpacket->u.server_startgame4_ack.reply, SERVER_STARTGAME4_ACK_OK);
4307 else
4308 bn_int_set(&rpacket->u.server_startgame4_ack.reply, SERVER_STARTGAME4_ACK_NO);
4309 conn_push_outqueue(c, rpacket);
4310 packet_del_ref(rpacket);
4311 }
4312
4313 /* First, send an ECHO_REQ */
4314 if ((rpacket = packet_create(packet_class_bnet))) {
4315 packet_set_size(rpacket, sizeof(t_server_echoreq));
4316 packet_set_type(rpacket, SERVER_ECHOREQ);
4317 bn_int_set(&rpacket->u.server_echoreq.ticks, get_ticks());
4318 conn_push_outqueue(c, rpacket);
4319 packet_del_ref(rpacket);
4320 }
4321
4322 return 0;
4323 }
4324
4325 static int _client_closegame(t_connection * c, t_packet const *const packet)
4326 {
4327 t_game *game;
4328
4329 eventlog(eventlog_level_info, __FUNCTION__, "[{}] client closing game", conn_get_socket(c));
4330 if (packet_get_type(packet) == CLIENT_CLOSEGAME2 || ((conn_get_clienttag(c) != CLIENTTAG_WARCRAFT3_UINT) && (conn_get_clienttag(c) != CLIENTTAG_WAR3XP_UINT)))
4331 conn_set_game(c, NULL, NULL, NULL, game_type_none, 0);
4332 else if ((game = conn_get_game(c)))
4333 game_set_status(game, game_status_started);
4334
4335 return 0;
4336 }
4337
4338 static int _client_gamereport(t_connection * c, t_packet const *const packet)
4339 {
4340 if (packet_get_size(packet) < sizeof(t_client_game_report)) {
4341 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad GAME_REPORT packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_game_report), packet_get_size(packet));
4342 return -1;
4343 }
4344
4345 {
4346 t_account *my_account;
4347 t_account *other_account;
4348 t_game *game;
4349 unsigned int player_count;
4350 unsigned int i, s;
4351 t_client_game_report_result const *result_data;
4352 unsigned int result_off;
4353 t_game_result result;
4354 char const *player;
4355 unsigned int player_off;
4356 t_game_result *results;
4357
4358 player_count = bn_int_get(packet->u.client_gamerep.count);
4359
4360 if (!(game = conn_get_game(c))) {
4361 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got GAME_REPORT when not in a game for user \"{}\"", conn_get_socket(c), conn_get_username(c));
4362 return -1;
4363 }
4364
4365 eventlog(eventlog_level_info, __FUNCTION__, "[{}] CLIENT_GAME_REPORT: {} ({} players)", conn_get_socket(c), conn_get_username(c), player_count);
4366 my_account = conn_get_account(c);
4367
4368 results = (t_game_result*)xmalloc(sizeof(t_game_result)* game_get_count(game));
4369
4370 for (i = 0; i < game_get_count(game); i++)
4371 results[i] = game_result_none;
4372
4373 for (i = 0, result_off = sizeof(t_client_game_report), player_off = sizeof(t_client_game_report)+player_count * sizeof(t_client_game_report_result); i < player_count; i++, result_off += sizeof(t_client_game_report_result), player_off += std::strlen(player) + 1) {
4374 /* PELISH: Fixme - Can this crash server (NULL pointer dereferencing)?? */
4375 if (!(result_data = (const t_client_game_report_result*)packet_get_data_const(packet, result_off, sizeof(t_client_game_report_result)))) {
4376 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got corrupt GAME_REPORT packet (missing results {}-{})", conn_get_socket(c), i + 1, player_count);
4377 break;
4378 }
4379 if (!(player = packet_get_str_const(packet, player_off, MAX_USERNAME_LEN))) {
4380 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got corrupt GAME_REPORT packet (missing players {}-{})", conn_get_socket(c), i + 1, player_count);
4381 break;
4382 }
4383
4384 if (player[0] == '\0') /* empty slots have empty player name */
4385 continue;
4386
4387 if (i >= game_get_count(game)) {
4388 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got more results than the game had players - ignoring extra results", conn_get_socket(c));
4389 break;
4390 }
4391
4392 if (!(other_account = accountlist_find_account(player))) {
4393 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got GAME_REPORT with unknown player \"{}\"", conn_get_socket(c), player);
4394 break;
4395 }
4396
4397 // as player position in game structure and in game report might differ,
4398 // search for right position
4399 for (s = 0; s < game_get_count(game); s++)
4400 {
4401 if (game_get_player(game, s) == other_account) break;
4402 }
4403
4404 if (s < game_get_count(game))
4405 {
4406 result = bngresult_to_gresult(bn_int_get(result_data->result));
4407 results[s] = result;
4408 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] got player {} (\"{}\") result {}", conn_get_socket(c), i, player, game_result_get_str(result));
4409 }
4410 else
4411 {
4412 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got GAME_REPORT for non-participating player \"{}\"", conn_get_socket(c), player);
4413 }
4414
4415
4416 }
4417
4418 if (i == player_count) { /* if everything checked out... */
4419 char const *head;
4420 char const *body;
4421
4422 if (!(head = packet_get_str_const(packet, player_off, MAX_GAMEREP_HEAD_STR)))
4423 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got GAME_REPORT with missing or too long report head", conn_get_socket(c));
4424 else {
4425 player_off += std::strlen(head) + 1;
4426 if (!(body = packet_get_str_const(packet, player_off, MAX_GAMEREP_BODY_STR)))
4427 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got GAME_REPORT with missing or too ling report body", conn_get_socket(c));
4428 else
4429 game_set_report(game, my_account, head, body);
4430 }
4431 }
4432
4433 if (game_set_reported_results(game, my_account, results) < 0)
4434 xfree((void *)results);
4435
4436 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] finished parsing result... now leaving game", conn_get_socket(c));
4437 conn_set_game(c, NULL, NULL, NULL, game_type_none, 0);
4438 }
4439
4440 return 0;
4441 }
4442
4443 static int _client_leavechannel(t_connection * c, t_packet const *const packet)
4444 {
4445 /* If this user in a channel, notify everyone that the user has left */
4446 if (conn_get_channel(c))
4447 conn_part_channel(c);
4448 return 0;
4449 }
4450
4451 static int _client_ladderreq(t_connection * c, t_packet const *const packet)
4452 {
4453 t_packet *rpacket;
4454
4455
4456 if (packet_get_size(packet) < sizeof(t_client_ladderreq)) {
4457 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad LADDERREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_ladderreq), packet_get_size(packet));
4458 return -1;
4459 }
4460
4461 {
4462 t_ladder_entry entry;
4463 unsigned int i;
4464 unsigned int type;
4465 unsigned int start;
4466 unsigned int count;
4467 unsigned int idnum;
4468 t_account *account;
4469 t_clienttag clienttag;
4470 char const *timestr;
4471 t_bnettime bt;
4472 t_ladder_id id;
4473 t_ladder_sort sort;
4474 bool error = false;
4475
4476 clienttag = conn_get_clienttag(c);
4477
4478 type = bn_int_get(packet->u.client_ladderreq.type);
4479 start = bn_int_get(packet->u.client_ladderreq.startplace);
4480 count = bn_int_get(packet->u.client_ladderreq.count);
4481 idnum = bn_int_get(packet->u.client_ladderreq.id);
4482
4483 /* eventlog(eventlog_level_debug,__FUNCTION__,"got LADDERREQ type=%u start=%u count=%u id=%u",type,start,count,id); */
4484
4485 switch (idnum) {
4486 case CLIENT_LADDERREQ_ID_STANDARD:
4487 id = ladder_id_normal;
4488 break;
4489 case CLIENT_LADDERREQ_ID_IRONMAN:
4490 id = ladder_id_ironman;
4491 break;
4492 default:
4493 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got unknown ladder ladderreq.id=0x{:08x}", conn_get_socket(c), idnum);
4494 id = ladder_id_normal;
4495 }
4496
4497 if (!(rpacket = packet_create(packet_class_bnet)))
4498 return -1;
4499 packet_set_size(rpacket, sizeof(t_server_ladderreply));
4500 packet_set_type(rpacket, SERVER_LADDERREPLY);
4501
4502 bn_int_set(&rpacket->u.server_ladderreply.clienttag, clienttag);
4503 bn_int_set(&rpacket->u.server_ladderreply.id, idnum);
4504 bn_int_set(&rpacket->u.server_ladderreply.type, type);
4505 bn_int_set(&rpacket->u.server_ladderreply.startplace, start);
4506 bn_int_set(&rpacket->u.server_ladderreply.count, count);
4507
4508 switch (type){
4509 case CLIENT_LADDERREQ_TYPE_HIGHESTRATED:
4510 sort = ladder_sort_highestrated;
4511 break;
4512 case CLIENT_LADDERREQ_TYPE_MOSTWINS:
4513 sort = ladder_sort_mostwins;
4514 break;
4515 case CLIENT_LADDERREQ_TYPE_MOSTGAMES:
4516 sort = ladder_sort_mostgames;
4517 break;
4518 default:
4519 sort = ladder_sort_default;
4520 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got unknown value for ladderreq.type={}", conn_get_socket(c), type);
4521 error = true;
4522 }
4523
4524 LadderList* ladderList_active = NULL;
4525 LadderList* ladderList_current = NULL;
4526
4527 if (!error)
4528 {
4529 ladderList_active = ladders.getLadderList(LadderKey(id, clienttag, sort, ladder_time_active));
4530 ladderList_current = ladders.getLadderList(LadderKey(id, clienttag, sort, ladder_time_current));
4531 }
4532 if (!ladderList_active || ladderList_current)
4533 error = true;
4534
4535 for (i = start; i < start + count; i++) {
4536
4537
4538 const LadderReferencedObject* referencedObject = NULL;
4539 account = NULL;
4540 if (!error){
4541
4542 if (!(referencedObject = ladderList_active->getReferencedObject(i + 1)))
4543 referencedObject = ladderList_current->getReferencedObject(i + 1);
4544 }
4545
4546 if ((referencedObject) && (account = referencedObject->getAccount()))
4547 {
4548 bn_int_set(&entry.active.wins, account_get_ladder_active_wins(account, clienttag, id));
4549 bn_int_set(&entry.active.loss, account_get_ladder_active_losses(account, clienttag, id));
4550 bn_int_set(&entry.active.disconnect, account_get_ladder_active_disconnects(account, clienttag, id));
4551 bn_int_set(&entry.active.rating, account_get_ladder_active_rating(account, clienttag, id));
4552 bn_int_set(&entry.active.rank, account_get_ladder_active_rank(account, clienttag, id) - 1);
4553 if (!(timestr = account_get_ladder_active_last_time(account, clienttag, id)))
4554 timestr = BNETD_LADDER_DEFAULT_TIME;
4555 bnettime_set_str(&bt, timestr);
4556 bnettime_to_bn_long(bt, &entry.lastgame_active);
4557
4558 bn_int_set(&entry.current.wins, account_get_ladder_wins(account, clienttag, id));
4559 bn_int_set(&entry.current.loss, account_get_ladder_losses(account, clienttag, id));
4560 bn_int_set(&entry.current.disconnect, account_get_ladder_disconnects(account, clienttag, id));
4561 bn_int_set(&entry.current.rating, account_get_ladder_rating(account, clienttag, id));
4562 bn_int_set(&entry.current.rank, account_get_ladder_rank(account, clienttag, id) - 1);
4563 if (!(timestr = account_get_ladder_last_time(account, clienttag, id)))
4564 timestr = BNETD_LADDER_DEFAULT_TIME;
4565 bnettime_set_str(&bt, timestr);
4566 bnettime_to_bn_long(bt, &entry.lastgame_current);
4567 }
4568 else {
4569 bn_int_set(&entry.active.wins, 0);
4570 bn_int_set(&entry.active.loss, 0);
4571 bn_int_set(&entry.active.disconnect, 0);
4572 bn_int_set(&entry.active.rating, 0);
4573 bn_int_set(&entry.active.rank, 0);
4574 bn_long_set_a_b(&entry.lastgame_active, 0, 0);
4575
4576 bn_int_set(&entry.current.wins, 0);
4577 bn_int_set(&entry.current.loss, 0);
4578 bn_int_set(&entry.current.disconnect, 0);
4579 bn_int_set(&entry.current.rating, 0);
4580 bn_int_set(&entry.current.rank, 0);
4581 bn_long_set_a_b(&entry.lastgame_current, 0, 0);
4582 }
4583
4584 bn_int_set(&entry.ttest[0], i); // rank
4585 bn_int_set(&entry.ttest[1], 0); //
4586 bn_int_set(&entry.ttest[2], 0); //
4587 if (account)
4588 bn_int_set(&entry.ttest[3], account_get_ladder_high_rating(account, clienttag, id));
4589 else
4590 bn_int_set(&entry.ttest[3], 0);
4591 bn_int_set(&entry.ttest[4], 0); //
4592 bn_int_set(&entry.ttest[5], 0); //
4593
4594 packet_append_data(rpacket, &entry, sizeof(entry));
4595
4596 if (account)
4597 packet_append_string(rpacket, account_get_name(account));
4598 else
4599 packet_append_string(rpacket, " "); /* use a space so the client won't show the user's own account when double-clicked on */
4600 }
4601
4602 conn_push_outqueue(c, rpacket);
4603 packet_del_ref(rpacket);
4604 }
4605
4606 return 0;
4607 }
4608
4609 static int _client_laddersearchreq(t_connection * c, t_packet const *const packet)
4610 {
4611 t_packet *rpacket;
4612
4613 if (packet_get_size(packet) < sizeof(t_client_laddersearchreq)) {
4614 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad LADDERSEARCHREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_laddersearchreq), packet_get_size(packet));
4615 return -1;
4616 }
4617
4618 {
4619 char const *playername;
4620 t_account *account;
4621 unsigned int idnum;
4622 unsigned int type;
4623 unsigned int rank; /* starts at zero */
4624 t_ladder_id id;
4625 t_ladder_sort sort;
4626 t_clienttag ctag = conn_get_clienttag(c);
4627
4628 idnum = bn_int_get(packet->u.client_laddersearchreq.id);
4629
4630 switch (idnum) {
4631 case CLIENT_LADDERREQ_ID_STANDARD:
4632 id = ladder_id_normal;
4633 break;
4634 case CLIENT_LADDERREQ_ID_IRONMAN:
4635 id = ladder_id_ironman;
4636 break;
4637 default:
4638 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got unknown ladder laddersearchreq.id=0x{:08x}", conn_get_socket(c), idnum);
4639 id = ladder_id_normal;
4640 }
4641
4642 type = bn_int_get(packet->u.client_laddersearchreq.type);
4643 switch (type) {
4644 case CLIENT_LADDERSEARCHREQ_TYPE_HIGHESTRATED:
4645 sort = ladder_sort_highestrated;
4646 break;
4647 case CLIENT_LADDERSEARCHREQ_TYPE_MOSTWINS:
4648 sort = ladder_sort_mostwins;
4649 break;
4650 case CLIENT_LADDERSEARCHREQ_TYPE_MOSTGAMES:
4651 sort = ladder_sort_mostgames;
4652 break;
4653 default:
4654 sort = ladder_sort_default;
4655 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got unknown ladder search type {}", conn_get_socket(c), type);
4656 }
4657
4658 if (!(playername = packet_get_str_const(packet, sizeof(t_client_laddersearchreq), MAX_USERNAME_LEN))) {
4659 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad LADDERSEARCHREQ packet (missing or too long playername)", conn_get_socket(c));
4660 return -1;
4661 }
4662
4663 if (!(account = accountlist_find_account(playername)))
4664 rank = SERVER_LADDERSEARCHREPLY_RANK_NONE;
4665 else {
4666 LadderList * ladderList_active = ladders.getLadderList(LadderKey(id, ctag, sort, ladder_time_active));
4667 LadderList * ladderList_current = ladders.getLadderList(LadderKey(id, ctag, sort, ladder_time_current));
4668 unsigned int uid = account_get_uid(account);
4669 switch (type) {
4670 case CLIENT_LADDERSEARCHREQ_TYPE_HIGHESTRATED:
4671 case CLIENT_LADDERSEARCHREQ_TYPE_MOSTWINS:
4672 case CLIENT_LADDERSEARCHREQ_TYPE_MOSTGAMES:
4673 if (!(rank = ladderList_active->getRank(uid)))
4674 {
4675 if (!(rank = ladderList_current->getRank(uid)) || ladderList_active->getReferencedObject(rank))
4676 rank = 0;
4677 }
4678 break;
4679 default:
4680 rank = 0;
4681 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got unknown ladder search type {}", conn_get_socket(c), bn_int_get(packet->u.client_laddersearchreq.type));
4682 }
4683
4684 if (rank == 0)
4685 rank = SERVER_LADDERSEARCHREPLY_RANK_NONE;
4686 else
4687 rank--;
4688 }
4689
4690 if (!(rpacket = packet_create(packet_class_bnet)))
4691 return -1;
4692 packet_set_size(rpacket, sizeof(t_server_laddersearchreply));
4693 packet_set_type(rpacket, SERVER_LADDERSEARCHREPLY);
4694 bn_int_set(&rpacket->u.server_laddersearchreply.rank, rank);
4695 conn_push_outqueue(c, rpacket);
4696 packet_del_ref(rpacket);
4697 }
4698
4699 return 0;
4700 }
4701
4702 static int _client_mapauthreq1(t_connection * c, t_packet const *const packet)
4703 {
4704 t_packet *rpacket;
4705
4706 if (packet_get_size(packet) < sizeof(t_client_mapauthreq1)) {
4707 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad MAPAUTHREQ1 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_mapauthreq1), packet_get_size(packet));
4708 return -1;
4709 }
4710
4711 {
4712 char const *mapname;
4713 t_game *game;
4714
4715 if (!(mapname = packet_get_str_const(packet, sizeof(t_client_mapauthreq1), MAP_NAME_LEN))) {
4716 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad MAPAUTHREQ1 packet (missing or too long mapname)", conn_get_socket(c));
4717 return -1;
4718 }
4719
4720 game = conn_get_game(c);
4721
4722 if (game) {
4723 eventlog(eventlog_level_info, __FUNCTION__, "[{}] map auth requested for map \"{}\" gametype \"{}\"", conn_get_socket(c), mapname, game_type_get_str(game_get_type(game)));
4724 game_set_mapname(game, mapname);
4725 }
4726 else
4727 eventlog(eventlog_level_info, __FUNCTION__, "[{}] map auth requested when not in a game", conn_get_socket(c));
4728
4729 if ((rpacket = packet_create(packet_class_bnet))) {
4730 unsigned int val;
4731
4732 if (!game) {
4733 val = SERVER_MAPAUTHREPLY1_NO;
4734 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] map authorization denied (not in a game)", conn_get_socket(c));
4735 }
4736 else if (strcasecmp(game_get_mapname(game), mapname) != 0) {
4737 val = SERVER_MAPAUTHREPLY1_NO;
4738 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] map authorization denied (map name \"{}\" does not match game map name \"{}\")", conn_get_socket(c), mapname, game_get_mapname(game));
4739 }
4740 else {
4741 game_set_status(game, game_status_started);
4742
4743 if (game_get_type(game) == game_type_ladder) {
4744 val = SERVER_MAPAUTHREPLY1_LADDER_OK;
4745 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] giving map ladder authorization (in a ladder game)", conn_get_socket(c));
4746 }
4747 else if (ladder_check_map(game_get_mapname(game), game_get_maptype(game), conn_get_clienttag(c))) {
4748 val = SERVER_MAPAUTHREPLY1_LADDER_OK;
4749 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] giving map ladder authorization (is a ladder map)", conn_get_socket(c));
4750 }
4751 else {
4752 val = SERVER_MAPAUTHREPLY1_OK;
4753 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] giving map normal authorization", conn_get_socket(c));
4754 }
4755 }
4756
4757 packet_set_size(rpacket, sizeof(t_server_mapauthreply1));
4758 packet_set_type(rpacket, SERVER_MAPAUTHREPLY1);
4759 bn_int_set(&rpacket->u.server_mapauthreply1.response, val);
4760 conn_push_outqueue(c, rpacket);
4761 packet_del_ref(rpacket);
4762 }
4763 }
4764
4765 return 0;
4766 }
4767
4768 static int _client_mapauthreq2(t_connection * c, t_packet const *const packet)
4769 {
4770 t_packet *rpacket;
4771
4772 if (packet_get_size(packet) < sizeof(t_client_mapauthreq2)) {
4773 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad MAPAUTHREQ2 packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_mapauthreq2), packet_get_size(packet));
4774 return -1;
4775 }
4776
4777 {
4778 char const *mapname;
4779 t_game *game;
4780
4781 if (!(mapname = packet_get_str_const(packet, sizeof(t_client_mapauthreq2), MAP_NAME_LEN))) {
4782 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad MAPAUTHREQ2 packet (missing or too long mapname)", conn_get_socket(c));
4783 return -1;
4784 }
4785
4786 game = conn_get_game(c);
4787
4788 if (game) {
4789 eventlog(eventlog_level_info, __FUNCTION__, "[{}] map auth requested for map \"{}\" gametype \"{}\"", conn_get_socket(c), mapname, game_type_get_str(game_get_type(game)));
4790 game_set_mapname(game, mapname);
4791 }
4792 else
4793 eventlog(eventlog_level_info, __FUNCTION__, "[{}] map auth requested when not in a game", conn_get_socket(c));
4794
4795 if ((rpacket = packet_create(packet_class_bnet))) {
4796 unsigned int val;
4797
4798 if (!game) {
4799 val = SERVER_MAPAUTHREPLY2_NO;
4800 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] map authorization denied (not in a game)", conn_get_socket(c));
4801 }
4802 else if (strcasecmp(game_get_mapname(game), mapname) != 0) {
4803 val = SERVER_MAPAUTHREPLY2_NO;
4804 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] map authorization denied (map name \"{}\" does not match game map name \"{}\")", conn_get_socket(c), mapname, game_get_mapname(game));
4805 }
4806 else {
4807 game_set_status(game, game_status_started);
4808
4809 if (game_get_type(game) == game_type_ladder) {
4810 val = SERVER_MAPAUTHREPLY2_LADDER_OK;
4811 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] giving map ladder authorization (in a ladder game)", conn_get_socket(c));
4812 }
4813 else if (ladder_check_map(game_get_mapname(game), game_get_maptype(game), conn_get_clienttag(c))) {
4814 val = SERVER_MAPAUTHREPLY2_LADDER_OK;
4815 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] giving map ladder authorization (is a ladder map)", conn_get_socket(c));
4816 }
4817 else {
4818 val = SERVER_MAPAUTHREPLY2_OK;
4819 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] giving map normal authorization", conn_get_socket(c));
4820 }
4821 }
4822
4823 packet_set_size(rpacket, sizeof(t_server_mapauthreply2));
4824 packet_set_type(rpacket, SERVER_MAPAUTHREPLY2);
4825 bn_int_set(&rpacket->u.server_mapauthreply2.response, val);
4826 conn_push_outqueue(c, rpacket);
4827 packet_del_ref(rpacket);
4828 }
4829 }
4830
4831 return 0;
4832 }
4833
4834 static int _client_changeclient(t_connection * c, t_packet const *const packet)
4835 {
4836 if (packet_get_size(packet) < sizeof(t_client_changeclient))
4837 {
4838 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_CHANGECLIENT packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_changeclient), packet_get_size(packet));
4839 return -1;
4840 }
4841
4842 if (conn_get_clienttag(c) != CLIENTTAG_WAR3XP_UINT
4843 || bn_int_get(packet->u.client_changeclient.clienttag) != CLIENTTAG_WARCRAFT3_UINT)
4844 {
4845 eventlog(eventlog_level_error, __FUNCTION__, "[{}] invalid attempt to change client from {X} to {X}", conn_get_socket(c), conn_get_clienttag(c), bn_int_get(packet->u.client_changeclient.clienttag));
4846 conn_set_state(c, conn_state_destroy);
4847 return -1;
4848 }
4849
4850 conn_set_clienttag(c, bn_int_get(packet->u.client_changeclient.clienttag));
4851
4852 eventlog(eventlog_level_info, __FUNCTION__, "[{}] changed client to {X}", conn_get_socket(c), bn_int_get(packet->u.client_changeclient.clienttag));
4853
4854 return 0;
4855 }
4856
4857 static int _client_clanmemberlistreq(t_connection * c, t_packet const *const packet)
4858 {
4859 if (packet_get_size(packet) < sizeof(t_client_clanmemberlist_req)) {
4860 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLANMEMBERLIST_REQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clanmemberlist_req), packet_get_size(packet));
4861 return -1;
4862 }
4863
4864 clan_send_memberlist(c, packet);
4865 return 0;
4866 }
4867
4868 static int _client_clan_motdreq(t_connection * c, t_packet const *const packet)
4869 {
4870 if (packet_get_size(packet) < sizeof(t_client_clan_motdreq)) {
4871 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_MOTDREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clan_motdreq), packet_get_size(packet));
4872 return -1;
4873 }
4874
4875 clan_send_motd_reply(c, packet);
4876 return 0;
4877 }
4878
4879 static int _client_clan_motdchg(t_connection * c, t_packet const *const packet)
4880 {
4881 if (packet_get_size(packet) < sizeof(t_client_clan_motdreq)) {
4882 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_MOTDCHGREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clan_motdreq), packet_get_size(packet));
4883 return -1;
4884 }
4885
4886 clan_save_motd_chg(c, packet);
4887 return 0;
4888 }
4889
4890 static int _client_clan_disbandreq(t_connection * c, t_packet const *const packet)
4891 {
4892 t_packet *rpacket;
4893
4894 if (packet_get_size(packet) < sizeof(t_client_clan_disbandreq)) {
4895 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_DISBANDREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clan_disbandreq), packet_get_size(packet));
4896 return -1;
4897 }
4898
4899 if ((rpacket = packet_create(packet_class_bnet))) {
4900 t_clan *clan;
4901 t_clanmember *member;
4902 t_account *account;
4903
4904 packet_set_size(rpacket, sizeof(t_server_clan_disbandreply));
4905 packet_set_type(rpacket, SERVER_CLAN_DISBANDREPLY);
4906 bn_int_set(&rpacket->u.server_clan_disbandreply.count, bn_int_get(packet->u.client_clan_disbandreq.count));
4907
4908 if (!((account = conn_get_account(c)) && (clan = account_get_clan(account)) && (member = account_get_clanmember(account)) && (clanmember_get_status(member) >= CLAN_CHIEFTAIN))) {
4909 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] got suspicious CLAN_DISBANDREQ packet (request without required privileges)", conn_get_socket(c));
4910 bn_byte_set(&rpacket->u.server_clan_disbandreply.result, CLAN_RESPONSE_NOT_AUTHORIZED);
4911 conn_push_outqueue(c, rpacket);
4912 packet_del_ref(rpacket);
4913 }
4914 else if ((clanlist_remove_clan(clan) == 0) && (clan_remove(clan_get_clantag(clan)) == 0)) {
4915 bn_byte_set(&rpacket->u.server_clan_disbandreply.result, CLAN_RESPONSE_SUCCESS);
4916 clan_close_status_window_on_disband(clan);
4917 clan_send_packet_to_online_members(clan, rpacket);
4918 packet_del_ref(rpacket);
4919 clan_destroy(clan);
4920 }
4921 else {
4922 bn_byte_set(&rpacket->u.server_clan_disbandreply.result, CLAN_RESPONSE_FAIL);
4923 conn_push_outqueue(c, rpacket);
4924 packet_del_ref(rpacket);
4925 }
4926 }
4927
4928 return 0;
4929 }
4930
4931 static int _client_clan_createreq(t_connection * c, t_packet const *const packet)
4932 {
4933 if (packet_get_size(packet) < sizeof(t_client_clan_createreq)) {
4934 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_INFOREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clan_createreq), packet_get_size(packet));
4935 return -1;
4936 }
4937
4938 clan_get_possible_member(c, packet);
4939
4940 return 0;
4941 }
4942
4943 static int _client_clan_createinvitereq(t_connection * c, t_packet const *const packet)
4944 {
4945 t_packet *rpacket;
4946 unsigned size;
4947 const char *clanname;
4948 const char *username;
4949 t_clantag clantag;
4950 unsigned offset;
4951 t_clan *clan;
4952
4953 if ((size = packet_get_size(packet)) < sizeof(t_client_clan_createinvitereq)) {
4954 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_CREATEINVITEREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clan_createinvitereq), packet_get_size(packet));
4955 return -1;
4956 }
4957 offset = sizeof(t_client_clan_createinvitereq);
4958
4959 if (!(clanname = packet_get_str_const(packet, offset, CLAN_NAME_MAX))) {
4960 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_CREATEINVITEREQ packet (missing clanname)", conn_get_socket(c));
4961 return -1;
4962 }
4963 offset += (std::strlen(clanname) + 1);
4964
4965 if (packet_get_size(packet) < offset + 4) {
4966 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_CREATEINVITEREQ packet (missing clantag)", conn_get_socket(c));
4967 return -1;
4968 }
4969 clantag = *((int *)packet_get_data_const(packet, offset, 4));
4970 offset += 4;
4971
4972 if ((rpacket = packet_create(packet_class_bnet))) {
4973 if ((clan = clan_create(conn_get_account(c), clantag, clanname, NULL)) && clanlist_add_clan(clan)) {
4974 char membercount;
4975 if (packet_get_size(packet) < offset + 1) {
4976 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_CREATEINVITEREQ packet (missing membercount)", conn_get_socket(c));
4977 packet_del_ref(rpacket);
4978 return -1;
4979 }
4980 membercount = *((char *)packet_get_data_const(packet, offset, 1));
4981 clan_set_created(clan, -membercount); /* FIXME: We should also check if membercount == count of names on end of this packet */
4982 packet_set_size(rpacket, sizeof(t_server_clan_createinvitereq));
4983 packet_set_type(rpacket, SERVER_CLAN_CREATEINVITEREQ);
4984 bn_int_set(&rpacket->u.server_clan_createinvitereq.count, bn_int_get(packet->u.client_clan_createinvitereq.count));
4985 bn_int_set(&rpacket->u.server_clan_createinvitereq.clantag, clantag);
4986 packet_append_string(rpacket, clanname);
4987 packet_append_string(rpacket, conn_get_username(c));
4988 packet_append_data(rpacket, packet_get_data_const(packet, offset, size - offset), size - offset); /* Pelish: we will send bad packet if we got bad packet... */
4989 offset++;
4990 do {
4991 username = packet_get_str_const(packet, offset, MAX_USERNAME_LEN);
4992 if (username) {
4993 t_connection *conn;
4994 offset += (std::strlen(username) + 1);
4995 if ((conn = connlist_find_connection_by_accountname(username)) != NULL) {
4996 t_clanmember *clanmember;
4997 if (prefs_get_clan_newer_time() > 0) {
4998 clanmember = clan_add_member(clan, conn_get_account(conn), CLAN_NEW);
4999 clanmember_set_fullmember(clanmember, 1); /* FIXME: do only this here and no clan_add_member() */
5000 }
5001 else {
5002 clanmember = clan_add_member(clan, conn_get_account(conn), CLAN_PEON);
5003 clanmember_set_fullmember(clanmember, 1); /* FIXME: do only this here and no clan_add_member() */
5004 }
5005 conn_push_outqueue(conn, rpacket);
5006 }
5007 }
5008 } while (username && (offset < size));
5009 }
5010 else {
5011 packet_set_size(rpacket, sizeof(t_server_clan_createinvitereply));
5012 packet_set_type(rpacket, SERVER_CLAN_CREATEINVITEREPLY);
5013 bn_int_set(&rpacket->u.server_clan_createinvitereply.count, bn_int_get(packet->u.client_clan_createinvitereply.count));
5014 bn_byte_set(&rpacket->u.server_clan_createinvitereply.status, 0);
5015 }
5016 packet_del_ref(rpacket);
5017 }
5018
5019 return 0;
5020 }
5021
5022 static int _client_clan_createinvitereply(t_connection * c, t_packet const *const packet)
5023 {
5024 t_packet *rpacket;
5025 t_connection *conn;
5026 t_clan *clan;
5027 const char *username;
5028 char status;
5029
5030 if (packet_get_size(packet) < sizeof(t_client_clan_createinvitereply)) {
5031 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_CREATEINVITEREPLY packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clan_createinvitereq), packet_get_size(packet));
5032 return -1;
5033 }
5034 std::size_t offset = sizeof(t_client_clan_createinvitereply);
5035 username = packet_get_str_const(packet, offset, MAX_USERNAME_LEN);
5036 if (!username)
5037 {
5038 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_CREATEINVITEREPLY packet (bad username)", conn_get_socket(c));
5039 return -1;
5040 }
5041 offset += (std::strlen(username) + 1);
5042 if (packet_get_size(packet) < offset + 1) {
5043 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_CREATEINVITEREPLY packet (mising status)", conn_get_socket(c));
5044 return -1;
5045 }
5046 status = *((char *)packet_get_data_const(packet, offset, 1));
5047 if ((conn = connlist_find_connection_by_accountname(username)) == NULL)
5048 return -1;
5049 if ((clan = account_get_creating_clan(conn_get_account(conn))) == NULL)
5050 return -1;
5051 if ((status != CLAN_RESPONSE_ACCEPT) && (rpacket = packet_create(packet_class_bnet))) {
5052 packet_set_size(rpacket, sizeof(t_server_clan_createinvitereply));
5053 packet_set_type(rpacket, SERVER_CLAN_CREATEINVITEREPLY);
5054 bn_int_set(&rpacket->u.server_clan_createinvitereply.count, bn_int_get(packet->u.client_clan_createinvitereply.count));
5055 bn_byte_set(&rpacket->u.server_clan_createinvitereply.status, status);
5056 packet_append_string(rpacket, conn_get_username(c));
5057 conn_push_outqueue(conn, rpacket);
5058 packet_del_ref(rpacket);
5059 if (clan) {
5060 clanlist_remove_clan(clan);
5061 clan_destroy(clan);
5062 }
5063 }
5064 else {
5065 int created = clan_get_created(clan);
5066 if (created > 0) {
5067 eventlog(eventlog_level_error, __FUNCTION__, "clan {} has already been created", clan_get_name(clan));
5068 return 0;
5069 }
5070 created++;
5071 if ((created >= 0) && (rpacket = packet_create(packet_class_bnet))) {
5072 clan_set_created(clan, 1);
5073 clan_set_creation_time(clan, std::time(NULL));
5074 packet_set_size(rpacket, sizeof(t_server_clan_createinvitereply));
5075 packet_set_type(rpacket, SERVER_CLAN_CREATEINVITEREPLY);
5076 bn_int_set(&rpacket->u.server_clan_createinvitereply.count, bn_int_get(packet->u.client_clan_createinvitereply.count));
5077 bn_byte_set(&rpacket->u.server_clan_createinvitereply.status, CLAN_RESPONSE_SUCCESS);
5078 packet_append_string(rpacket, "");
5079 conn_push_outqueue(conn, rpacket);
5080 packet_del_ref(rpacket);
5081 clan_send_status_window_on_create(clan);
5082 clan_save(clan);
5083 }
5084 else
5085 clan_set_created(clan, created);
5086 }
5087 return 0;
5088 }
5089
5090 static int _client_clanmember_rankupdatereq(t_connection * c, t_packet const *const packet)
5091 {
5092 t_packet *rpacket;
5093
5094 if (packet_get_size(packet) < sizeof(t_client_clanmember_rankupdate_req)) {
5095 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLANMEMBER_RANKUPDATE_REQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clanmember_rankupdate_req), packet_get_size(packet));
5096 return -1;
5097 }
5098
5099 if ((rpacket = packet_create(packet_class_bnet)) != NULL)
5100 {
5101 std::size_t offset = sizeof(t_client_clanmember_rankupdate_req);
5102 char status;
5103 t_clan *clan;
5104 t_clanmember *dest_member;
5105 t_clanmember *member;
5106 t_account *account;
5107
5108 packet_set_size(rpacket, sizeof(t_server_clanmember_rankupdate_reply));
5109 packet_set_type(rpacket, SERVER_CLANMEMBER_RANKUPDATE_REPLY);
5110 bn_int_set(&rpacket->u.server_clanmember_rankupdate_reply.count,
5111 bn_int_get(packet->u.client_clanmember_rankupdate_req.count));
5112 const char* const username = packet_get_str_const(packet, offset, MAX_USERNAME_LEN);
5113 if (!username)
5114 {
5115 eventlog(eventlog_level_error, __FUNCTION__, "[{}] Could not retrieve username from CLANMEMBER_RANKUPDATE_REQ packet", conn_get_socket(c));
5116 packet_del_ref(rpacket);
5117 return -1;
5118 }
5119
5120 offset += (std::strlen(username) + 1);
5121 if (packet_get_size(packet) < offset + 1)
5122 {
5123 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLANMEMBER_RANKUPDATE_REQ packet (mising status)", conn_get_socket(c));
5124 packet_del_ref(rpacket);
5125 return -1;
5126 }
5127 status = *((char *)packet_get_data_const(packet, offset, 1));
5128
5129 account = (conn_get_account(c));
5130
5131 if ((clan = account_get_clan(account)) && (member = clan_find_member(clan, account))
5132 && (dest_member = clan_find_member_by_name(clan, username)) && (member != dest_member)) {
5133 if ((status < CLAN_PEON) || (status > CLAN_SHAMAN)) {
5134 /* PELISH: CLAN_NEW can not be promoted to anything
5135 * and also noone can be promoted to CLAN_CHIEFTAIN */
5136 DEBUG1("trying to change to bad status {}", status);
5137 bn_byte_set(&rpacket->u.server_clanmember_rankupdate_reply.result, SERVER_CLANMEMBER_RANKUPDATE_FAILED);
5138 }
5139 else if ((((clanmember_get_status(member) == CLAN_SHAMAN) && (status < CLAN_SHAMAN) && (clanmember_get_status(dest_member) < CLAN_SHAMAN)) ||
5140 (clanmember_get_status(member) == CLAN_CHIEFTAIN)) &&
5141 (clanmember_set_status(dest_member, status) == 0)) {
5142 bn_byte_set(&rpacket->u.server_clanmember_rankupdate_reply.result, SERVER_CLANMEMBER_RANKUPDATE_SUCCESS);
5143 clanmember_on_change_status(dest_member);
5144 }
5145 else {
5146 bn_byte_set(&rpacket->u.server_clanmember_rankupdate_reply.result, SERVER_CLANMEMBER_RANKUPDATE_FAILED);
5147 }
5148 }
5149 else {
5150 bn_byte_set(&rpacket->u.server_clanmember_rankupdate_reply.result, SERVER_CLANMEMBER_RANKUPDATE_FAILED);
5151 }
5152 conn_push_outqueue(c, rpacket);
5153 packet_del_ref(rpacket);
5154 }
5155
5156 return 0;
5157 }
5158
5159 static int _client_clanmember_removereq(t_connection * c, t_packet const *const packet)
5160 {
5161 t_packet *rpacket;
5162
5163 if (packet_get_size(packet) < sizeof(t_client_clanmember_remove_req)) {
5164 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLANMEMBER_REMOVE_REQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clanmember_remove_req), packet_get_size(packet));
5165 return -1;
5166 }
5167
5168 if ((rpacket = packet_create(packet_class_bnet)) != NULL) {
5169 t_account *acc;
5170 t_clan *clan;
5171 const char *username;
5172 t_clanmember *member;
5173 t_connection *dest_conn;
5174 packet_set_size(rpacket, sizeof(t_server_clanmember_remove_reply));
5175 packet_set_type(rpacket, SERVER_CLANMEMBER_REMOVE_REPLY);
5176 bn_int_set(&rpacket->u.server_clanmember_remove_reply.count,
5177 bn_int_get(packet->u.client_clanmember_remove_req.count));
5178 username = packet_get_str_const(packet, sizeof(t_client_clanmember_remove_req), MAX_USERNAME_LEN);
5179 bn_byte_set(&rpacket->u.server_clanmember_remove_reply.result,
5180 SERVER_CLANMEMBER_REMOVE_FAILED); // initially presume it failed
5181
5182 if ((acc = conn_get_account(c)) && (clan = account_get_clan(acc)) && (member = clan_find_member_by_name(clan, username))) {
5183 dest_conn = clanmember_get_conn(member);
5184 if (clan_remove_member(clan, member) == 0) {
5185 t_packet *rpacket2;
5186 if (dest_conn) {
5187 clan_close_status_window(dest_conn);
5188 conn_update_w3_playerinfo(dest_conn);
5189 channel_rejoin(dest_conn);
5190 }
5191 if ((rpacket2 = packet_create(packet_class_bnet)) != NULL) {
5192 packet_set_size(rpacket2, sizeof(t_server_clanmember_removed_notify));
5193 packet_set_type(rpacket2, SERVER_CLANMEMBER_REMOVED_NOTIFY);
5194 packet_append_string(rpacket2, username);
5195 clan_send_packet_to_online_members(clan, rpacket2);
5196 packet_del_ref(rpacket2);
5197 }
5198 bn_byte_set(&rpacket->u.server_clanmember_remove_reply.result,
5199 SERVER_CLANMEMBER_REMOVE_SUCCESS);
5200 }
5201 }
5202 conn_push_outqueue(c, rpacket);
5203 packet_del_ref(rpacket);
5204 }
5205
5206 return 0;
5207 }
5208
5209 static int _client_clan_membernewchiefreq(t_connection * c, t_packet const *const packet)
5210 {
5211 t_packet *rpacket;
5212
5213 if (packet_get_size(packet) < sizeof(t_client_clan_membernewchiefreq)) {
5214 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLIENT_CLAN_MEMBERNEWCHIEFREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clan_createreq), packet_get_size(packet));
5215 return -1;
5216 }
5217
5218 if ((rpacket = packet_create(packet_class_bnet)) != NULL) {
5219 t_account *acc;
5220 t_clan *clan;
5221 t_clanmember *oldmember;
5222 t_clanmember *newmember;
5223 const char *username;
5224 packet_set_size(rpacket, sizeof(t_server_clan_membernewchiefreply));
5225 packet_set_type(rpacket, SERVER_CLAN_MEMBERNEWCHIEFREPLY);
5226 bn_int_set(&rpacket->u.server_clan_membernewchiefreply.count, bn_int_get(packet->u.client_clan_membernewchiefreq.count));
5227 username = packet_get_str_const(packet, sizeof(t_client_clan_membernewchiefreq), MAX_USERNAME_LEN);
5228 if ((acc = conn_get_account(c)) && (oldmember = account_get_clanmember(acc)) && (clanmember_get_status(oldmember) == CLAN_CHIEFTAIN) && (clan = clanmember_get_clan(oldmember)) && (newmember = clan_find_member_by_name(clan, username)) && (clanmember_set_status(oldmember, CLAN_GRUNT) == 0) && (clanmember_set_status(newmember, CLAN_CHIEFTAIN) == 0)) {
5229 clanmember_on_change_status(oldmember);
5230 clanmember_on_change_status(newmember);
5231 bn_byte_set(&rpacket->u.server_clan_membernewchiefreply.result, SERVER_CLAN_MEMBERNEWCHIEFREPLY_SUCCESS);
5232 clan_send_packet_to_online_members(clan, rpacket);
5233 packet_del_ref(rpacket);
5234 }
5235 else {
5236 bn_byte_set(&rpacket->u.server_clan_membernewchiefreply.result, SERVER_CLAN_MEMBERNEWCHIEFREPLY_FAILED);
5237 conn_push_outqueue(c, rpacket);
5238 packet_del_ref(rpacket);
5239 }
5240 }
5241
5242 return 0;
5243 }
5244
5245 static int _client_clan_invitereq(t_connection * c, t_packet const *const packet)
5246 {
5247 t_packet *rpacket;
5248 t_account *account;
5249 t_clan *clan;
5250 t_clanmember *member;
5251 t_clantag clantag;
5252 const char *username;
5253 t_connection *conn;
5254 t_account *conn_account;
5255 char response_code;
5256
5257 if (packet_get_size(packet) < sizeof(t_client_clan_invitereq)) {
5258 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_INVITEREQ packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clan_invitereq), packet_get_size(packet));
5259 return -1;
5260 }
5261
5262 if (!(username = packet_get_str_const(packet, sizeof(t_client_clan_invitereq), MAX_USERNAME_LEN))) {
5263 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_INVITEREQ packet (missing or too long username)", conn_get_socket(c));
5264 return -1;
5265 }
5266
5267 if (rpacket = packet_create(packet_class_bnet)) {
5268
5269 // user not authorized
5270 if (!((account = conn_get_account(c)) &&
5271 (clan = account_get_clan(account)) &&
5272 (member = account_get_clanmember(account)) &&
5273 (clanmember_get_status(member) >= CLAN_SHAMAN) &&
5274 (clantag = clan_get_clantag(clan)))) {
5275 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] got suspicious CLAN_INVITEREQ packet (request without required privileges)", conn_get_socket(c));
5276 response_code = CLAN_RESPONSE_NOT_AUTHORIZED;
5277 }
5278 else {
5279
5280 // target user not online
5281 if (!((conn = connlist_find_connection_by_accountname(username)) &&
5282 (conn_account = conn_get_account(conn)))) {
5283 response_code = CLAN_RESPONSE_NOT_FOUND;
5284
5285 // target user already in a clan or creating a clan
5286 }
5287 else if (account_get_clanmember_forced(conn_get_account(conn))) {
5288 response_code = CLAN_RESPONSE_NOT_FOUND;
5289
5290 // clan allready ful
5291 }
5292 else if (clan_get_member_count(clan) >= prefs_get_clan_max_members()) {
5293 response_code = CLAN_RESPONSE_CLAN_FULL;
5294
5295 // valid invitereq
5296 }
5297 else {
5298 if (prefs_get_clan_newer_time() > 0) {
5299 clan_add_member(clan, conn_account, CLAN_NEW);
5300 }
5301 else {
5302 clan_add_member(clan, conn_account, CLAN_PEON);
5303 }
5304 packet_set_size(rpacket, sizeof(t_server_clan_invitereq));
5305 packet_set_type(rpacket, SERVER_CLAN_INVITEREQ);
5306 bn_int_set(&rpacket->u.server_clan_invitereq.count, bn_int_get(packet->u.client_clan_invitereq.count));
5307 bn_int_set(&rpacket->u.server_clan_invitereq.clantag, clantag);
5308 packet_append_string(rpacket, clan_get_name(clan));
5309 packet_append_string(rpacket, conn_get_username(c));
5310 conn_push_outqueue(conn, rpacket);
5311 packet_del_ref(rpacket);
5312 return 0;
5313 }
5314 }
5315
5316 packet_set_size(rpacket, sizeof(t_server_clan_invitereply));
5317 packet_set_type(rpacket, SERVER_CLAN_INVITEREPLY);
5318 bn_byte_set(&rpacket->u.server_clan_invitereply.result, response_code);
5319 bn_int_set(&rpacket->u.server_clan_invitereply.count, bn_int_get(packet->u.client_clan_invitereq.count));
5320
5321 conn_push_outqueue(c, rpacket);
5322 packet_del_ref(rpacket);
5323 }
5324
5325 return 0;
5326 }
5327
5328 static int _client_clan_invitereply(t_connection * c, t_packet const *const packet)
5329 {
5330 t_packet *rpacket;
5331 t_account *acc;
5332 t_clan *clan;
5333 t_clanmember *member;
5334 const char *username;
5335 t_connection *conn;
5336 t_account *conn_account;
5337 t_clan *conn_clan;
5338 t_clanmember *conn_member;
5339 char status;
5340
5341 if (packet_get_size(packet) < sizeof(t_client_clan_invitereply)) {
5342 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_INVITEREPLY packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_clan_createreq), packet_get_size(packet));
5343 return -1;
5344 }
5345 std::size_t offset = sizeof(t_client_clan_invitereply);
5346 if (!(username = packet_get_str_const(packet, offset, MAX_USERNAME_LEN))) {
5347 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_INVITEREPLY packet (missing username)", conn_get_socket(c));
5348 return -1;
5349 }
5350 offset += (std::strlen(username) + 1);
5351 if (packet_get_size(packet) < offset + 1) {
5352 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad CLAN_INVITEREPLY packet (mising status)", conn_get_socket(c));
5353 return -1;
5354 }
5355 status = *((char *)packet_get_data_const(packet, offset, 1));
5356
5357 // reply without prior request
5358 if (!((acc = conn_get_account(c)) &&
5359 (member = account_get_clanmember_forced(acc)) &&
5360 (clan = clanmember_get_clan(member)) &&
5361 (clanmember_get_fullmember(member) == 0))) {
5362 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] got suspicious CLAN_INVITEREPLY packet (reply without prior request)", conn_get_socket(c));
5363 return -1;
5364
5365 // invalid inviter
5366 }
5367 else if (!((conn = connlist_find_connection_by_accountname(username)) &&
5368 (conn_account = conn_get_account(conn)) &&
5369 (conn_clan = account_get_clan(conn_account)) &&
5370 (conn_member = account_get_clanmember(conn_account)) &&
5371 (clanmember_get_status(conn_member) >= CLAN_SHAMAN) &&
5372 (clan_get_clantag(clan) == clan_get_clantag(conn_clan)))) {
5373 eventlog(eventlog_level_warn, __FUNCTION__, "[{}] got suspicious CLAN_INVITEREPLY packet (invalid inviter)", conn_get_socket(c));
5374 return -1;
5375 }
5376
5377 if (rpacket = packet_create(packet_class_bnet)) {
5378 packet_set_size(rpacket, sizeof(t_server_clan_invitereply));
5379 packet_set_type(rpacket, SERVER_CLAN_INVITEREPLY);
5380 bn_int_set(&rpacket->u.server_clan_invitereply.count, bn_int_get(packet->u.client_clan_invitereply.count));
5381
5382 if (status != CLAN_RESPONSE_ACCEPT) {
5383 clan_remove_member(clan, member);
5384 bn_byte_set(&rpacket->u.server_clan_invitereply.result, status);
5385 }
5386 else {
5387 if (clan_get_member_count(clan) >= prefs_get_clan_max_members()) {
5388 clan_remove_member(clan, member);
5389 bn_byte_set(&rpacket->u.server_clan_invitereply.result, CLAN_RESPONSE_CLAN_FULL);
5390 }
5391 else
5392 {
5393 clanmember_set_fullmember(member, 1);
5394 if (conn_get_channel(c))
5395 {
5396 conn_update_w3_playerinfo(c);
5397 channel_set_userflags(c);
5398
5399 std::string channelname("Clan " + std::string(clantag_to_str(clan_get_clantag(clan))));
5400 if (conn_set_channel(c, channelname.c_str()) < 0)
5401 {
5402 conn_set_channel(c, CHANNEL_NAME_BANNED); /* should not fail */
5403 }
5404 clanmember_set_online(c);
5405 }
5406 clan_send_status_window(c);
5407 bn_byte_set(&rpacket->u.server_clan_invitereply.result, CLAN_RESPONSE_SUCCESS);
5408 }
5409 }
5410 }
5411
5412 conn_push_outqueue(conn, rpacket);
5413 packet_del_ref(rpacket);
5414
5415 return 0;
5416 }
5417
5418 static int _client_crashdump(t_connection * c, t_packet const *const packet)
5419 {
5420 return 0;
5421 }
5422
5423 static int _client_setemailreply(t_connection * c, t_packet const *const packet)
5424 {
5425 char const *email;
5426 t_account *account;
5427
5428 if (!(email = packet_get_str_const(packet, sizeof(t_client_setemailreply), MAX_EMAIL_STR))) {
5429 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad SETEMAILREPLY packet", conn_get_socket(c));
5430 return -1;
5431 }
5432 if (!(account = conn_get_account(c))) {
5433 eventlog(eventlog_level_error, __FUNCTION__, "got NULL account for connection in setemail request");
5434 return -1;
5435 }
5436 if (account_get_email(account)) {
5437 eventlog(eventlog_level_error, __FUNCTION__, "[{}] account \"{}\" already have email set, ignore set email", conn_get_socket(c), account_get_name(account));
5438 return 0;
5439 }
5440 if (account_set_email(account, email) < 0) {
5441 eventlog(eventlog_level_error, __FUNCTION__, "[{}] failed to init account \"{}\" email to \"{}\"", conn_get_socket(c), account_get_name(account), email);
5442 return 0;
5443 }
5444 else
5445 eventlog(eventlog_level_info, __FUNCTION__, "[{}] init account \"{}\" email to \"{}\"", conn_get_socket(c), account_get_name(account), email);
5446 return 0;
5447 }
5448
5449 static int _client_changeemailreq(t_connection * c, t_packet const *const packet)
5450 {
5451 char const *oldaddr;
5452 char const *newaddr;
5453 char const *username;
5454 char const *email;
5455 t_account *account;
5456 int pos;
5457
5458 pos = sizeof(t_client_changeemailreq);
5459 if (!(username = packet_get_str_const(packet, pos, MAX_USERNAME_LEN))) {
5460 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad username in CHANGEEMAILREQ packet", conn_get_socket(c));
5461 return -1;
5462 }
5463 pos += (std::strlen(username) + 1);
5464 if (!(oldaddr = packet_get_str_const(packet, pos, MAX_EMAIL_STR))) {
5465 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad old email in CHANGEEMAILREQ packet", conn_get_socket(c));
5466 return -1;
5467 }
5468 pos += (std::strlen(oldaddr) + 1);
5469 if (!(newaddr = packet_get_str_const(packet, pos, MAX_EMAIL_STR))) {
5470 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad new email in CHANGEEMAILREQ packet", conn_get_socket(c));
5471 return -1;
5472 }
5473 if (!(account = accountlist_find_account(username))) {
5474 eventlog(eventlog_level_info, __FUNCTION__, "[{}] change email for \"{}\" refused (no such account)", conn_get_socket(c), username);
5475 return 0;
5476 }
5477 if (!(email = account_get_email(account)) || !email[0]) {
5478 eventlog(eventlog_level_info, __FUNCTION__, "[{}] account \"{}\" do not have email set, ignore changing", conn_get_socket(c), account_get_name(account));
5479 return 0;
5480 }
5481 if (strcasecmp(email, oldaddr)) {
5482 eventlog(eventlog_level_error, __FUNCTION__, "[{}] account \"{}\" email mismatch, ignore changing", conn_get_socket(c), account_get_name(account));
5483 return 0;
5484 }
5485 if (account_set_email(account, newaddr) < 0) {
5486 eventlog(eventlog_level_error, __FUNCTION__, "[{}] failed to change account \"{}\" email to \"{}\"", conn_get_socket(c), account_get_name(account), newaddr);
5487 return 0;
5488 }
5489 else
5490 eventlog(eventlog_level_info, __FUNCTION__, "[{}] change account \"{}\" email to \"{}\"", conn_get_socket(c), account_get_name(account), newaddr);
5491 return 0;
5492 }
5493
5494 static int _client_getpasswordreq(t_connection * c, t_packet const *const packet)
5495 {
5496 char const *username;
5497 char const *try_email;
5498 char const *email;
5499 t_account *account;
5500 int pos;
5501
5502 pos = sizeof(t_client_getpasswordreq);
5503 if (!(username = packet_get_str_const(packet, pos, MAX_USERNAME_LEN))) {
5504 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad username in GETPASSWORDREQ packet", conn_get_socket(c));
5505 return -1;
5506 }
5507 pos += (std::strlen(username) + 1);
5508 if (!(try_email = packet_get_str_const(packet, pos, MAX_EMAIL_STR))) {
5509 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad email in GETPASSWORDREQ packet", conn_get_socket(c));
5510 return -1;
5511 }
5512 if (!(account = accountlist_find_account(username))) {
5513 eventlog(eventlog_level_info, __FUNCTION__, "[{}] get password for \"{}\" refused (no such account)", conn_get_socket(c), username);
5514 return 0;
5515 }
5516 if (!(email = account_get_email(account)) || !email[0]) {
5517 eventlog(eventlog_level_error, __FUNCTION__, "[{}] account \"{}\" do not have email set, ignore get password", conn_get_socket(c), account_get_name(account));
5518 return 0;
5519 }
5520 if (strcasecmp(email, try_email)) {
5521 eventlog(eventlog_level_error, __FUNCTION__, "[{}] account \"{}\" email mismatch, ignore get password", conn_get_socket(c), account_get_name(account));
5522 return 0;
5523 }
5524 /* TODO: send mail to user with the real password or changed password!?
5525 * (as we cannot get the real password back, we should only change the password) --Soar */
5526 eventlog(eventlog_level_info, __FUNCTION__, "[{}] get password for account \"{}\" to email \"{}\"", conn_get_socket(c), account_get_name(account), email);
5527 return 0;
5528 }
5529
5530
5531
5532 static int _client_extrawork(t_connection * c, t_packet const *const packet)
5533 {
5534 if (packet_get_size(packet) < sizeof(t_client_extrawork)) {
5535 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad EXTRAWORK packet (expected {} bytes, got {})", conn_get_socket(c), sizeof(t_client_extrawork), packet_get_size(packet));
5536 return -1;
5537 }
5538 {
5539 short gametype;
5540 short length;
5541 const char * data;
5542 std::string data_s;
5543
5544 gametype = bn_short_get(packet->u.client_extrawork.gametype);
5545 length = bn_short_get(packet->u.client_extrawork.length);
5546
5547 if (!(data = (const char *)packet_get_raw_data_const(packet, sizeof(t_client_extrawork)))) {
5548 eventlog(eventlog_level_error, __FUNCTION__, "[{}] got bad EXTRAWORK packet (missing or too long data)", conn_get_socket(c));
5549 return -1;
5550 }
5551 // extract substring with given length
5552 data_s = std::string(data).substr(0, length);
5553 eventlog(eventlog_level_debug, __FUNCTION__, "[{}] Received EXTRAWORK packet with GameType: {} and Length: {} ({})", conn_get_socket(c), gametype, length, data_s.c_str());
5554
5555 #ifdef WITH_LUA
5556 lua_handle_client_extrawork(c, gametype, length, data_s.c_str());
5557 #endif
5558 }
5559 return 0;
5560 }
5561
5562 }
5563
5564 }