Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "JSInterface_Lobby.h"
21 :
22 : #include "gui/GUIManager.h"
23 : #include "lib/code_annotation.h"
24 : #include "lib/code_generation.h"
25 : #include "lib/config2.h"
26 : #include "lib/debug.h"
27 : #include "lib/utf8.h"
28 : #include "lobby/XmppClient.h"
29 : #include "network/NetServer.h"
30 : #include "ps/CLogger.h"
31 : #include "ps/CStr.h"
32 : #include "ps/Util.h"
33 : #include "scriptinterface/FunctionWrapper.h"
34 : #include "scriptinterface/Exceptions.h"
35 : #include "third_party/encryption/pkcs5_pbkdf2.h"
36 :
37 : #include <js/RootingAPI.h>
38 : #include <js/TypeDecls.h>
39 : #include <js/Value.h>
40 : #include <sodium.h>
41 : #include <stdexcept>
42 : #include <string>
43 :
44 : namespace JS { class CallArgs; }
45 : namespace Script { class Interface; }
46 :
47 : namespace JSI_Lobby
48 : {
49 0 : bool HasXmppClient()
50 : {
51 0 : return g_XmppClient;
52 : }
53 :
54 : /**
55 : * Whether the given nickname is a buddy of the current user.
56 : *
57 : * @param nick The nickname to look up, compared case-insensitively
58 : */
59 0 : static bool IsLobbyBuddy([[maybe_unused]] const std::string& nick)
60 : {
61 : #if CONFIG2_LOBBY
62 0 : return g_XmppClient && g_XmppClient->IsBuddy(nick);
63 : #else
64 : // Without the lobby there is no roster to look the nickname up in.
65 : return false;
66 : #endif
67 : }
68 :
69 0 : void SetRankedGame(bool isRanked)
70 : {
71 0 : g_rankedGame = isRanked;
72 0 : }
73 :
74 : #if CONFIG2_LOBBY
75 :
76 0 : void StartXmppClient(const std::wstring& username, const std::wstring& password, const std::wstring& room,
77 : const std::wstring& nick, int historyRequestSize)
78 : {
79 0 : if (g_XmppClient)
80 0 : throw std::logic_error{"Cannot call StartXmppClient with an already initialized XmppClient!"};
81 :
82 0 : g_XmppClient =
83 : new XmppClient{
84 0 : &g_GUI->GetScriptInterface(),
85 0 : utf8_from_wstring(username),
86 0 : utf8_from_wstring(password),
87 0 : utf8_from_wstring(room),
88 0 : utf8_from_wstring(nick),
89 0 : historyRequestSize};
90 :
91 0 : g_rankedGame = true;
92 0 : }
93 :
94 0 : void StartRegisterXmppClient(const std::wstring& username, const std::wstring& password)
95 : {
96 0 : if (g_XmppClient)
97 : {
98 0 : throw std::logic_error{
99 0 : "Cannot call StartRegisterXmppClient with an already initialized XmppClient!"};
100 : }
101 :
102 0 : g_XmppClient =
103 : new XmppClient{
104 0 : &g_GUI->GetScriptInterface(),
105 0 : utf8_from_wstring(username),
106 0 : utf8_from_wstring(password),
107 0 : std::string(),
108 0 : std::string(),
109 : 0,
110 0 : true};
111 0 : }
112 :
113 0 : void StopXmppClient()
114 : {
115 0 : if (!g_XmppClient)
116 0 : throw std::logic_error{"Cannot call StopXmppClient without an initialized XmppClient!"};
117 :
118 0 : SAFE_DELETE(g_XmppClient);
119 0 : g_rankedGame = false;
120 0 : }
121 :
122 : ////////////////////////////////////////////////
123 : ////////////////////////////////////////////////
124 :
125 0 : XmppClient* XmppGetter(const Script::Request&, JS::CallArgs&)
126 : {
127 0 : if (!g_XmppClient)
128 : {
129 0 : LOGERROR("Cannot use XMPPClient functions without an initialized XmppClient!");
130 0 : return nullptr;
131 : }
132 : return g_XmppClient;
133 : }
134 :
135 0 : void SendRegisterGame(const Script::Interface& scriptInterface, JS::HandleValue data)
136 : {
137 0 : if (!g_XmppClient)
138 0 : throw std::logic_error{"Cannot call SendRegisterGame without an initialized XmppClient!"};
139 :
140 : // Prevent JS mods to register matches in the lobby that were started with lobby authentication disabled
141 0 : if (!g_NetServer || !g_NetServer->UseLobbyAuth())
142 : {
143 0 : LOGERROR("Registering games in the lobby requires lobby authentication to be enabled!");
144 0 : return;
145 : }
146 :
147 0 : g_XmppClient->SendIqRegisterGame(scriptInterface, data);
148 : }
149 :
150 : // Unlike other functions, this one just returns Undefined if XmppClient isn't initialised.
151 0 : JS::Value GuiPollNewMessages(const Script::Interface& scriptInterface)
152 : {
153 0 : if (!g_XmppClient)
154 0 : return JS::UndefinedValue();
155 :
156 0 : return g_XmppClient->GuiPollNewMessages(scriptInterface);
157 : }
158 :
159 :
160 : // Non-public secure PBKDF2 hash function with salting and 1,337 iterations
161 : //
162 : // TODO: We should use libsodium's crypto_pwhash instead of this. The first reason is that
163 : // libsodium doesn't propose a bare PBKDF2 hash in its API and it's too bad to rely on custom
164 : // code when we have a fully-fledged library available; the second reason is that Argon2 (the
165 : // default algorithm for crypto_pwhash) is better than what we use (and it's the default one
166 : // in the lib for a reason).
167 : // However changing the hashing method should be planned carefully, by trying to login with a
168 : // password hashed the old way, and, if successful, updating the password in the database using
169 : // the new hashing method. Dropping the old hashing code can only be done either by giving users
170 : // a way to reset their password, or by keeping track of successful password updates and dropping
171 : // old unused accounts after some time.
172 0 : std::string EncryptPassword(const std::string& password, const std::string& username)
173 : {
174 0 : ENSURE(sodium_init() >= 0);
175 :
176 0 : const int DIGESTSIZE = crypto_hash_sha256_BYTES;
177 0 : const int ITERATIONS = 1337;
178 :
179 0 : cassert(DIGESTSIZE == 32);
180 :
181 0 : static const unsigned char salt_base[DIGESTSIZE] = {
182 : 244, 243, 249, 244, 32, 33, 34, 35, 10, 11, 12, 13, 14, 15, 16, 17,
183 : 18, 19, 20, 32, 33, 244, 224, 127, 129, 130, 140, 153, 133, 123, 234, 123 };
184 :
185 : // initialize the salt buffer
186 0 : unsigned char salt_buffer[DIGESTSIZE] = {0};
187 0 : crypto_hash_sha256_state state;
188 0 : crypto_hash_sha256_init(&state);
189 :
190 0 : crypto_hash_sha256_update(&state, salt_base, sizeof(salt_base));
191 0 : crypto_hash_sha256_update(&state, (unsigned char*)username.c_str(), username.length());
192 :
193 0 : crypto_hash_sha256_final(&state, salt_buffer);
194 :
195 : // PBKDF2 to create the buffer
196 0 : unsigned char encrypted[DIGESTSIZE];
197 0 : pbkdf2(encrypted, (unsigned char*)password.c_str(), password.length(), salt_buffer, DIGESTSIZE, ITERATIONS);
198 :
199 0 : return CStr(Hexify(encrypted, DIGESTSIZE)).UpperCase();
200 : }
201 :
202 : #endif
203 :
204 26 : void RegisterScriptFunctions(const Script::Request& rq)
205 : {
206 : // Lobby functions
207 26 : Script::Function::Register<&HasXmppClient>(rq, "HasXmppClient");
208 26 : Script::Function::Register<&IsLobbyBuddy>(rq, "IsLobbyBuddy");
209 26 : Script::Function::Register<&SetRankedGame>(rq, "SetRankedGame");
210 : #if CONFIG2_LOBBY // Allow the lobby to be disabled
211 26 : Script::Function::Register<&StartXmppClient>(rq, "StartXmppClient");
212 26 : Script::Function::Register<&StartRegisterXmppClient>(rq, "StartRegisterXmppClient");
213 26 : Script::Function::Register<&StopXmppClient>(rq, "StopXmppClient");
214 :
215 : #define REGISTER_XMPP(func, name) \
216 : Script::Function::Register<&XmppClient::func, &XmppGetter>(rq, name)
217 :
218 26 : REGISTER_XMPP(connect, "ConnectXmppClient");
219 26 : REGISTER_XMPP(disconnect, "DisconnectXmppClient");
220 26 : REGISTER_XMPP(isConnected, "IsXmppClientConnected");
221 26 : REGISTER_XMPP(SendIqGetBoardList, "SendGetBoardList");
222 26 : REGISTER_XMPP(SendIqGetProfile, "SendGetProfile");
223 26 : REGISTER_XMPP(SendIqGameReport, "SendGameReport");
224 26 : Script::Function::Register<&SendRegisterGame>(rq, "SendRegisterGame");
225 26 : REGISTER_XMPP(SendIqUnregisterGame, "SendUnregisterGame");
226 26 : REGISTER_XMPP(SendIqChangeStateGame, "SendChangeStateGame");
227 26 : REGISTER_XMPP(GUIGetPlayerList, "GetPlayerList");
228 26 : REGISTER_XMPP(GUIGetGameList, "GetGameList");
229 26 : REGISTER_XMPP(GUIGetBoardList, "GetBoardList");
230 26 : REGISTER_XMPP(GUIGetProfile, "GetProfile");
231 :
232 26 : Script::Function::Register<&GuiPollNewMessages>(rq, "LobbyGuiPollNewMessages");
233 26 : REGISTER_XMPP(GuiPollHistoricMessages, "LobbyGuiPollHistoricMessages");
234 26 : REGISTER_XMPP(GuiPollHasPlayerListUpdate, "LobbyGuiPollHasPlayerListUpdate");
235 26 : REGISTER_XMPP(SendMUCMessage, "LobbySendMessage");
236 26 : REGISTER_XMPP(SetPresence, "LobbySetPlayerPresence");
237 26 : REGISTER_XMPP(SetNick, "LobbySetNick");
238 26 : REGISTER_XMPP(GetNick, "LobbyGetNick");
239 26 : REGISTER_XMPP(GetJID, "LobbyGetJID");
240 26 : REGISTER_XMPP(GetUsername, "LobbyGetUsername");
241 26 : REGISTER_XMPP(ChangePassword, "LobbyChangePassword");
242 26 : REGISTER_XMPP(kick, "LobbyKick");
243 26 : REGISTER_XMPP(ban, "LobbyBan");
244 26 : REGISTER_XMPP(GetPresence, "LobbyGetPlayerPresence");
245 26 : REGISTER_XMPP(GetRole, "LobbyGetPlayerRole");
246 26 : REGISTER_XMPP(GetRating, "LobbyGetPlayerRating");
247 26 : REGISTER_XMPP(GetSubject, "LobbyGetRoomSubject");
248 :
249 : // Roster
250 26 : REGISTER_XMPP(AddBuddy, "LobbyAddBuddy");
251 26 : REGISTER_XMPP(RemoveBuddy, "LobbyRemoveBuddy");
252 : #undef REGISTER_XMPP
253 :
254 26 : Script::Function::Register<&EncryptPassword>(rq, "EncryptPassword");
255 : #endif // CONFIG2_LOBBY
256 26 : }
257 : }
|