Debian Codegraph @

src/bnetd/handle_d2cs.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2000,2001 Onlyer ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"
19 #include "handle_d2cs.h"
20
21 #include <cstring>
22 #include <cstdio>
23
24 #include "compat/strcasecmp.h"
25 #include "common/eventlog.h"
26 #include "common/bn_type.h"
27 #include "common/addr.h"
28 #include "common/bnethash.h"
29 #include "common/bnethashconv.h"
30 #include "common/tag.h"
31 #include "common/util.h"
32
33 #include "realm.h"
34 #include "prefs.h"
35 #include "account_wrap.h"
36 #include "game.h"
37 #include "common/setup_after.h"
38
39 namespace pvpgn
40 {
41
42 namespace bnetd
43 {
44
45 static int on_d2cs_accountloginreq(t_connection * c, t_packet const * packet);
46 static int on_d2cs_charloginreq(t_connection * c, t_packet const * packet);
47 static int on_d2cs_authreply(t_connection * c, t_packet const * packet);
48 static int on_d2cs_gameinforeply(t_connection * c, t_packet const * packet);
49
50 extern int handle_d2cs_packet(t_connection * c, t_packet const * packet)
51 {
52 if (!c) {
53 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
54 return -1;
55 }
56 if (!packet) {
57 eventlog(eventlog_level_error, __FUNCTION__, "got NULL packet");
58 return -1;
59 }
60 if (packet_get_class(packet) != packet_class_d2cs_bnetd) {
61 eventlog(eventlog_level_error, __FUNCTION__, "got bad packet class {}",
62 packet_get_class(packet));
63 return -1;
64 }
65 switch (conn_get_state(c)) {
66 case conn_state_connected:
67 switch (packet_get_type(packet)) {
68 case D2CS_BNETD_AUTHREPLY:
69 on_d2cs_authreply(c, packet);
70 break;
71 default:
72 eventlog(eventlog_level_error, __FUNCTION__,
73 "got unknown packet type {}", packet_get_type(packet));
74 break;
75 }
76 break;
77 case conn_state_loggedin:
78 switch (packet_get_type(packet)) {
79 case D2CS_BNETD_ACCOUNTLOGINREQ:
80 on_d2cs_accountloginreq(c, packet);
81 break;
82 case D2CS_BNETD_CHARLOGINREQ:
83 on_d2cs_charloginreq(c, packet);
84 break;
85 case D2CS_BNETD_GAMEINFOREPLY:
86 on_d2cs_gameinforeply(c, packet);
87 break;
88 default:
89 eventlog(eventlog_level_error, __FUNCTION__,
90 "got unknown packet type {}", packet_get_type(packet));
91 break;
92 }
93 break;
94 default:
95 eventlog(eventlog_level_error, __FUNCTION__,
96 "got unknown connection state {}", conn_get_state(c));
97 break;
98 }
99 return 0;
100 }
101
102 static int on_d2cs_authreply(t_connection * c, t_packet const * packet)
103 {
104 t_packet * rpacket;
105 unsigned int version;
106 unsigned int try_version;
107 unsigned int reply;
108 char const * realmname;
109 t_realm * realm;
110
111 if (packet_get_size(packet) < sizeof(t_d2cs_bnetd_authreply)) {
112 eventlog(eventlog_level_error, __FUNCTION__, "got bad packet size");
113 return -1;
114 }
115 if (!(realmname = packet_get_str_const(packet, sizeof(t_d2cs_bnetd_authreply), MAX_REALMNAME_LEN))) {
116 eventlog(eventlog_level_error, __FUNCTION__, "got bad realmname");
117 return -1;
118 }
119 if (!(realm = realmlist_find_realm(realmname))) {
120 realm = realmlist_find_realm_by_ip(conn_get_addr(c)); /* should not fail - checked in handle_init_packet() handle_init.c */
121 eventlog(eventlog_level_warn, __FUNCTION__, "warn: realm name mismatch {} {}", realm_get_name(realm), realmname);
122 if (!(prefs_allow_d2cs_setname())) { /* fail if allow_d2cs_setname = false */
123 eventlog(eventlog_level_error, __FUNCTION__, "d2cs not allowed to set realm name");
124 return -1;
125 }
126 if (realm_get_active(realm)) { /* fail if realm already active */
127 eventlog(eventlog_level_error, __FUNCTION__, "cannot set realm name to {} (realm already active)", realmname);
128 return -1;
129 }
130 realm_set_name(realm, realmname);
131 }
132 version = prefs_get_d2cs_version();
133 try_version = bn_int_get(packet->u.d2cs_bnetd_authreply.version);
134 if (version && version != try_version) {
135 eventlog(eventlog_level_error, __FUNCTION__, "d2cs version mismatch 0x{:X} - 0x{:X}",
136 try_version, version);
137 reply = BNETD_D2CS_AUTHREPLY_BAD_VERSION;
138 }
139 else {
140 reply = BNETD_D2CS_AUTHREPLY_SUCCEED;
141 }
142
143 if (reply == BNETD_D2CS_AUTHREPLY_SUCCEED) {
144 eventlog(eventlog_level_info, __FUNCTION__, "d2cs {} authed",
145 addr_num_to_ip_str(conn_get_addr(c)));
146 conn_set_state(c, conn_state_loggedin);
147 realm_active(realm, c);
148 }
149 else {
150 eventlog(eventlog_level_error, __FUNCTION__, "failed to auth d2cs {}",
151 addr_num_to_ip_str(conn_get_addr(c)));
152 }
153 if ((rpacket = packet_create(packet_class_d2cs_bnetd))) {
154 packet_set_size(rpacket, sizeof(t_bnetd_d2cs_authreply));
155 packet_set_type(rpacket, BNETD_D2CS_AUTHREPLY);
156 bn_int_set(&rpacket->u.bnetd_d2cs_authreply.h.seqno, 1);
157 bn_int_set(&rpacket->u.bnetd_d2cs_authreply.reply, reply);
158 conn_push_outqueue(c, rpacket);
159 packet_del_ref(rpacket);
160 }
161 return 0;
162 }
163
164 static int on_d2cs_accountloginreq(t_connection * c, t_packet const * packet)
165 {
166 unsigned int sessionkey;
167 unsigned int sessionnum;
168 unsigned int salt;
169 char const * account;
170 char const * tname;
171 t_connection * client;
172 int reply;
173 t_packet * rpacket;
174 struct
175 {
176 bn_int salt;
177 bn_int sessionkey;
178 bn_int sessionnum;
179 bn_int secret;
180 bn_int passhash[5];
181 } temp;
182 t_hash secret_hash;
183 char const * pass_str;
184 t_hash passhash;
185 t_hash try_hash;
186
187 if (packet_get_size(packet) < sizeof(t_d2cs_bnetd_accountloginreq)) {
188 eventlog(eventlog_level_error, __FUNCTION__, "got bad packet size");
189 return -1;
190 }
191 if (!(account = packet_get_str_const(packet, sizeof(t_d2cs_bnetd_accountloginreq), MAX_USERNAME_LEN))) {
192 eventlog(eventlog_level_error, __FUNCTION__, "missing or too long account name");
193 return -1;
194 }
195 sessionkey = bn_int_get(packet->u.d2cs_bnetd_accountloginreq.sessionkey);
196 sessionnum = bn_int_get(packet->u.d2cs_bnetd_accountloginreq.sessionnum);
197 salt = bn_int_get(packet->u.d2cs_bnetd_accountloginreq.seqno);
198 if (!(client = connlist_find_connection_by_sessionnum(sessionnum))) {
199 eventlog(eventlog_level_error, __FUNCTION__, "sessionnum {} not found", sessionnum);
200 reply = BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
201 }
202 else if (sessionkey != conn_get_sessionkey(client)) {
203 eventlog(eventlog_level_error, __FUNCTION__, "sessionkey {} not match", sessionkey);
204 reply = BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
205 }
206 else if (!(tname = conn_get_username(client))) {
207 eventlog(eventlog_level_error, __FUNCTION__, "got NULL username");
208 reply = BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
209 }
210 else if (strcasecmp(account, tname)) {
211 eventlog(eventlog_level_error, __FUNCTION__, "username {} not match", account);
212 reply = BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
213 }
214 else {
215 bn_int_set(&temp.salt, salt);
216 bn_int_set(&temp.sessionkey, sessionkey);
217 bn_int_set(&temp.sessionnum, sessionnum);
218 bn_int_set(&temp.secret, conn_get_secret(client));
219 pass_str = account_get_pass(conn_get_account(client));
220 if (hash_set_str(&passhash, pass_str) < 0) {
221 reply = BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
222 }
223 else {
224 hash_to_bnhash((t_hash const *)&passhash, temp.passhash);
225 bnet_hash(&secret_hash, sizeof(temp), &temp);
226 bnhash_to_hash(packet->u.d2cs_bnetd_accountloginreq.secret_hash, &try_hash);
227 if (hash_eq(try_hash, secret_hash) == 1) {
228 eventlog(eventlog_level_debug, __FUNCTION__, "user {} loggedin on d2cs",
229 account);
230 reply = BNETD_D2CS_ACCOUNTLOGINREPLY_SUCCEED;
231 }
232 else {
233 eventlog(eventlog_level_error, __FUNCTION__, "user {} hash not match",
234 account);
235 reply = BNETD_D2CS_ACCOUNTLOGINREPLY_FAILED;
236 }
237 }
238 }
239 if ((rpacket = packet_create(packet_class_d2cs_bnetd))) {
240 packet_set_size(rpacket, sizeof(t_bnetd_d2cs_accountloginreply));
241 packet_set_type(rpacket, BNETD_D2CS_ACCOUNTLOGINREPLY);
242 bn_int_set(&rpacket->u.bnetd_d2cs_accountloginreply.h.seqno,
243 bn_int_get(packet->u.d2cs_bnetd_accountloginreq.h.seqno));
244 bn_int_set(&rpacket->u.bnetd_d2cs_accountloginreply.reply, reply);
245 conn_push_outqueue(c, rpacket);
246 packet_del_ref(rpacket);
247 }
248 return 0;
249 }
250
251 #define CHAR_PORTRAIT_LEN 0x30
252 static int on_d2cs_charloginreq(t_connection * c, t_packet const * packet)
253 {
254 t_connection * client;
255 char const * charname;
256 char const * portrait;
257 char const * clienttag;
258 char * temp;
259 unsigned int sessionnum;
260 t_realm * realm;
261 char const * realmname;
262 unsigned int pos, reply;
263 t_packet * rpacket;
264
265 if (packet_get_size(packet) < sizeof(t_d2cs_bnetd_charloginreq)) {
266 eventlog(eventlog_level_error, __FUNCTION__, "got bad packet size");
267 return -1;
268 }
269 sessionnum = bn_int_get(packet->u.d2cs_bnetd_charloginreq.sessionnum);
270 pos = sizeof(t_d2cs_bnetd_charloginreq);
271 if (!(charname = packet_get_str_const(packet, pos, MAX_CHARNAME_LEN))) {
272 eventlog(eventlog_level_error, __FUNCTION__, "got bad character name");
273 return -1;
274 }
275 pos += std::strlen(charname) + 1;
276 if (!(portrait = packet_get_str_const(packet, pos, CHAR_PORTRAIT_LEN))) {
277 eventlog(eventlog_level_error, __FUNCTION__, "got bad character portrait");
278 return -1;
279 }
280 if (!(client = connlist_find_connection_by_sessionnum(sessionnum))) {
281 eventlog(eventlog_level_error, __FUNCTION__, "user {} not found", sessionnum);
282 reply = BNETD_D2CS_CHARLOGINREPLY_FAILED;
283 }
284 else if (!(clienttag = clienttag_uint_to_str(conn_get_clienttag(client)))) {
285 eventlog(eventlog_level_error, __FUNCTION__, "got NULL clienttag");
286 reply = BNETD_D2CS_CHARLOGINREPLY_FAILED;
287 }
288 else if (!(realm = conn_get_realm(client))) {
289 eventlog(eventlog_level_error, __FUNCTION__, "got NULL realm");
290 reply = BNETD_D2CS_CHARLOGINREPLY_FAILED;
291 }
292 else {
293 char revtag[8];
294
295 realmname = realm_get_name(realm);
296 temp = (char*)xmalloc(std::strlen(clienttag) + std::strlen(realmname) + 1 + std::strlen(charname) + 1 +
297 std::strlen(portrait) + 1);
298 reply = BNETD_D2CS_CHARLOGINREPLY_SUCCEED;
299 std::strcpy(revtag, clienttag);
300 strreverse(revtag);
301 std::sprintf(temp, "%4s%s,%s,%s", revtag, realmname, charname, portrait);
302 conn_set_charname(client, charname);
303 conn_set_realminfo(client, temp);
304 xfree(temp);
305 eventlog(eventlog_level_debug, __FUNCTION__,
306 "loaded portrait for character {}", charname);
307 }
308 if ((rpacket = packet_create(packet_class_d2cs_bnetd))) {
309 packet_set_size(rpacket, sizeof(t_bnetd_d2cs_charloginreply));
310 packet_set_type(rpacket, BNETD_D2CS_CHARLOGINREPLY);
311 bn_int_set(&rpacket->u.bnetd_d2cs_charloginreply.h.seqno,
312 bn_int_get(packet->u.d2cs_bnetd_charloginreq.h.seqno));
313 bn_int_set(&rpacket->u.bnetd_d2cs_charloginreply.reply, reply);
314 conn_push_outqueue(c, rpacket);
315 packet_del_ref(rpacket);
316 }
317 return 0;
318 }
319
320 extern int handle_d2cs_init(t_connection * c)
321 {
322 t_packet * packet;
323
324 if ((packet = packet_create(packet_class_d2cs_bnetd))) {
325 packet_set_size(packet, sizeof(t_bnetd_d2cs_authreq));
326 packet_set_type(packet, BNETD_D2CS_AUTHREQ);
327 bn_int_set(&packet->u.bnetd_d2cs_authreq.h.seqno, 1);
328 bn_int_set(&packet->u.bnetd_d2cs_authreq.sessionnum, conn_get_sessionnum(c));
329 conn_push_outqueue(c, packet);
330 packet_del_ref(packet);
331 }
332 eventlog(eventlog_level_info, __FUNCTION__, "sent init packet to d2cs (sessionnum={})",
333 conn_get_sessionnum(c));
334 return 0;
335 }
336
337 extern int send_d2cs_gameinforeq(t_connection * c)
338 {
339 t_packet * packet;
340 t_game * game;
341 t_realm * realm;
342
343 if (!(c))
344 {
345 eventlog(eventlog_level_error, __FUNCTION__, "got NULL conn");
346 return -1;
347 }
348
349 if (!(game = conn_get_game(c)))
350 {
351 eventlog(eventlog_level_error, __FUNCTION__, "conn had NULL game");
352 return -1;
353 }
354
355 if (!(realm = conn_get_realm(c)))
356 {
357 eventlog(eventlog_level_error, __FUNCTION__, "conn had NULL realm");
358 return -1;
359 }
360
361
362 if ((packet = packet_create(packet_class_d2cs_bnetd))) {
363 packet_set_size(packet, sizeof(t_bnetd_d2cs_gameinforeq));
364 packet_set_type(packet, BNETD_D2CS_GAMEINFOREQ);
365 bn_int_set(&packet->u.bnetd_d2cs_gameinforeq.h.seqno, 0);
366 packet_append_string(packet, game_get_name(game));
367 conn_push_outqueue(realm_get_conn(realm), packet);
368 packet_del_ref(packet);
369 }
370 return 0;
371 }
372
373 static int on_d2cs_gameinforeply(t_connection * c, t_packet const * packet)
374 {
375 t_game * game;
376 char const * gamename;
377 unsigned int difficulty;
378 t_game_difficulty diff;
379
380 if (!(c)) {
381 eventlog(eventlog_level_error, __FUNCTION__, "got NULL connection");
382 return -1;
383 }
384
385 if (!(gamename = packet_get_str_const(packet, sizeof(t_d2cs_bnetd_gameinforeply), MAX_GAMENAME_LEN)))
386 {
387 eventlog(eventlog_level_error, __FUNCTION__, "missing or too long gamename");
388 return -1;
389 }
390
391 if (!(game = gamelist_find_game(gamename, CLIENTTAG_DIABLO2DV_UINT, game_type_diablo2closed))
392 && !(game = gamelist_find_game(gamename, CLIENTTAG_DIABLO2XP_UINT, game_type_diablo2closed)))
393 {
394 eventlog(eventlog_level_error, __FUNCTION__, "reply for unknown game \"{}\"", gamename);
395 return -1;
396 }
397
398 difficulty = bn_byte_get(packet->u.d2cs_bnetd_gameinforeply.difficulty);
399
400 switch (difficulty)
401 {
402 case 0:
403 diff = game_difficulty_normal;
404 break;
405 case 1:
406 diff = game_difficulty_nightmare;
407 break;
408 case 2:
409 diff = game_difficulty_hell;
410 break;
411 default:
412 diff = game_difficulty_none;
413 }
414
415 game_set_difficulty(game, diff);
416
417 return 0;
418 }
419
420 }
421
422 }