Add minetest.check_password_entry callback
authorest31 <MTest31@outlook.com>
Mon, 30 May 2016 13:27:48 +0000 (23:27 +1000)
committerCraig Robbins <kde.psych@gmail.com>
Mon, 30 May 2016 13:28:08 +0000 (23:28 +1000)
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

doc/lua_api.txt
src/script/lua_api/l_util.cpp
src/script/lua_api/l_util.h
src/util/auth.h

index 03f2dad321abce8b44aa639b046ccbb52e2e5649..82a0acbeec06da8e65f6660600eb76e1608e43c9 100644 (file)
@@ -1951,12 +1951,21 @@ Call these functions only at load time!
 * `minetest.notify_authentication_modified(name)`
     * Should be called by the authentication handler if privileges changes.
     * To report everybody, set `name=nil`.
+* `minetest.check_password_entry(name, entry, password)`
+    * Returns true if the "db entry" for a player with name matches given
+    * password, false otherwise.
+    * The "db entry" is the usually player-individual value that is derived
+    * from the player's chosen password and stored on the server in order to allow
+    * authentication whenever the player desires to log in.
+    * Only use this function for making it possible to log in via the password from
+    * via protocols like IRC, other uses for inside the game are frowned upon.
 * `minetest.get_password_hash(name, raw_password)`
     * Convert a name-password pair to a password hash that Minetest can use.
     * The returned value alone is not a good basis for password checks based
     * on comparing the password hash in the database with the password hash
     * from the function, with an externally provided password, as the hash
     * in the db might use the new SRP verifier format.
+    * For this purpose, use minetest.check_password_entry instead.
 * `minetest.string_to_privs(str)`: returns `{priv1=true,...}`
 * `minetest.privs_to_string(privs)`: returns `"priv1,priv2,..."`
     * Convert between two privilege representations
index e90b7fbcf4d909774ec8b0f76676d048aa8fb01c..d090fc91c5a40bc84d731e78bc3e4699c069b2e6 100644 (file)
@@ -246,6 +246,35 @@ int ModApiUtil::l_get_hit_params(lua_State *L)
        return 1;
 }
 
+// check_password_entry(name, entry, password)
+int ModApiUtil::l_check_password_entry(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+       std::string name = luaL_checkstring(L, 1);
+       std::string entry = luaL_checkstring(L, 2);
+       std::string password = luaL_checkstring(L, 3);
+
+       if (base64_is_valid(entry)) {
+               std::string hash = translate_password(name, password);
+               lua_pushboolean(L, hash == entry);
+               return 1;
+       }
+
+       std::string salt;
+       std::string verifier;
+
+       if (!decode_srp_verifier_and_salt(entry, &verifier, &salt)) {
+               // invalid format
+               warningstream << "Invalid password format for " << name << std::endl;
+               lua_pushboolean(L, false);
+               return 1;
+       }
+       std::string gen_verifier = generate_srp_verifier(name, password, salt);
+
+       lua_pushboolean(L, gen_verifier == verifier);
+       return 1;
+}
+
 // get_password_hash(name, raw_password)
 int ModApiUtil::l_get_password_hash(lua_State *L)
 {
@@ -449,6 +478,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
        API_FCT(get_dig_params);
        API_FCT(get_hit_params);
 
+       API_FCT(check_password_entry);
        API_FCT(get_password_hash);
 
        API_FCT(is_yes);
index 779dbe281f7543d9908524db5362be1c4923c009..3012d55aa2a593f93d6a31cdae1e11ce4dbe718e 100644 (file)
@@ -71,6 +71,9 @@ private:
        // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
        static int l_get_hit_params(lua_State *L);
 
+       // check_password_entry(name, entry, password)
+       static int l_check_password_entry(lua_State *L);
+
        // get_password_hash(name, raw_password)
        static int l_get_password_hash(lua_State *L);
 
index 1fd6ab4533e0c9a28867377ee10921169c77a569..7cdc7d74d2b572602c902ec1521e2c5a1cea216f 100644 (file)
@@ -45,6 +45,6 @@ std::string encode_srp_verifier(const std::string &verifier,
 /// Reads the DB-formatted SRP verifier and gets the verifier
 /// and salt components.
 bool decode_srp_verifier_and_salt(const std::string &encoded,
-       std::string *salt, std::string *bytes_v);
+       std::string *verifier, std::string *salt);
 
 #endif