Skip to content

Commit 27db929

Browse files
est31Zeno-
authored andcommittedMay 30, 2016
Add minetest.check_password_entry callback
Gives a convenient way to check a player's password. This entirely bypasses the SRP protocol, so should be used with great care. This function is not intended to be used in-game, but solely by external protocols, where no authentication of the minetest engine is provided, and also only for protocols, in which the user already gives the server the plaintext password. Examples for good use are the classical http form, or irc, an example for a bad use is a password change dialog inside formspec. Users should be aware that they lose the advantages of the SRP protocol if they enter their passwords for servers outside the normal entry box, like in in-game formspec menus, or through irc /msg s, This patch also fixes an auth.h mistake which has mixed up the order of params inside the decode_srp_verifier_and_salt function. Zeno-: Added errorstream message for invalid format when I committed
1 parent 4134d8a commit 27db929

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed
 

‎doc/lua_api.txt

+9
Original file line numberDiff line numberDiff line change
@@ -1951,12 +1951,21 @@ Call these functions only at load time!
19511951
* `minetest.notify_authentication_modified(name)`
19521952
* Should be called by the authentication handler if privileges changes.
19531953
* To report everybody, set `name=nil`.
1954+
* `minetest.check_password_entry(name, entry, password)`
1955+
* Returns true if the "db entry" for a player with name matches given
1956+
* password, false otherwise.
1957+
* The "db entry" is the usually player-individual value that is derived
1958+
* from the player's chosen password and stored on the server in order to allow
1959+
* authentication whenever the player desires to log in.
1960+
* Only use this function for making it possible to log in via the password from
1961+
* via protocols like IRC, other uses for inside the game are frowned upon.
19541962
* `minetest.get_password_hash(name, raw_password)`
19551963
* Convert a name-password pair to a password hash that Minetest can use.
19561964
* The returned value alone is not a good basis for password checks based
19571965
* on comparing the password hash in the database with the password hash
19581966
* from the function, with an externally provided password, as the hash
19591967
* in the db might use the new SRP verifier format.
1968+
* For this purpose, use minetest.check_password_entry instead.
19601969
* `minetest.string_to_privs(str)`: returns `{priv1=true,...}`
19611970
* `minetest.privs_to_string(privs)`: returns `"priv1,priv2,..."`
19621971
* Convert between two privilege representations

‎src/script/lua_api/l_util.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,35 @@ int ModApiUtil::l_get_hit_params(lua_State *L)
246246
return 1;
247247
}
248248

249+
// check_password_entry(name, entry, password)
250+
int ModApiUtil::l_check_password_entry(lua_State *L)
251+
{
252+
NO_MAP_LOCK_REQUIRED;
253+
std::string name = luaL_checkstring(L, 1);
254+
std::string entry = luaL_checkstring(L, 2);
255+
std::string password = luaL_checkstring(L, 3);
256+
257+
if (base64_is_valid(entry)) {
258+
std::string hash = translate_password(name, password);
259+
lua_pushboolean(L, hash == entry);
260+
return 1;
261+
}
262+
263+
std::string salt;
264+
std::string verifier;
265+
266+
if (!decode_srp_verifier_and_salt(entry, &verifier, &salt)) {
267+
// invalid format
268+
warningstream << "Invalid password format for " << name << std::endl;
269+
lua_pushboolean(L, false);
270+
return 1;
271+
}
272+
std::string gen_verifier = generate_srp_verifier(name, password, salt);
273+
274+
lua_pushboolean(L, gen_verifier == verifier);
275+
return 1;
276+
}
277+
249278
// get_password_hash(name, raw_password)
250279
int ModApiUtil::l_get_password_hash(lua_State *L)
251280
{
@@ -449,6 +478,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
449478
API_FCT(get_dig_params);
450479
API_FCT(get_hit_params);
451480

481+
API_FCT(check_password_entry);
452482
API_FCT(get_password_hash);
453483

454484
API_FCT(is_yes);

‎src/script/lua_api/l_util.h

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class ModApiUtil : public ModApiBase {
7171
// get_hit_params(groups, tool_capabilities[, time_from_last_punch])
7272
static int l_get_hit_params(lua_State *L);
7373

74+
// check_password_entry(name, entry, password)
75+
static int l_check_password_entry(lua_State *L);
76+
7477
// get_password_hash(name, raw_password)
7578
static int l_get_password_hash(lua_State *L);
7679

‎src/util/auth.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ std::string encode_srp_verifier(const std::string &verifier,
4545
/// Reads the DB-formatted SRP verifier and gets the verifier
4646
/// and salt components.
4747
bool decode_srp_verifier_and_salt(const std::string &encoded,
48-
std::string *salt, std::string *bytes_v);
48+
std::string *verifier, std::string *salt);
4949

5050
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.