store passwords in swapdb

This commit is contained in:
Enno Rehling 2018-10-24 09:27:48 +02:00
parent 83959c375f
commit 32009b7043
9 changed files with 31 additions and 24 deletions

View File

@ -432,7 +432,7 @@ static int tolua_faction_create(lua_State * L)
static int tolua_faction_get_password(lua_State * L)
{
faction *self = (faction *)tolua_tousertype(L, 1, 0);
tolua_pushstring(L, self->_password);
tolua_pushstring(L, faction_getpassword(self));
return 1;
}

View File

@ -20,7 +20,9 @@ int gamedb_update(void)
err = db_driver_open(DB_GAME, dbname);
if (err == 0) {
for (f = factions; f; f = f->next) {
int uid = db_driver_faction_save(f->uid, f->no, turn, f->email, f->_password);
int uid = db_driver_faction_save(f->uid, f->no, turn,
faction_getemail(f),
faction_getpassword(f));
if (uid > 0) {
f->uid = uid;
}

View File

@ -51,9 +51,13 @@ static void test_update_faction(CuTest *tc) {
test_setup();
f = test_create_faction(NULL);
uid = db_driver_faction_save(f->uid, f->no, 0, f->email, f->_password);
uid = db_driver_faction_save(f->uid, f->no, 0,
faction_getemail(f),
faction_getpassword(f));
f->uid = uid;
uid = db_driver_faction_save(f->uid, f->no, 0, f->email, f->_password);
uid = db_driver_faction_save(f->uid, f->no, 0,
faction_getemail(f),
faction_getpassword(f));
CuAssertIntEquals(tc, f->uid, uid);
test_teardown();
}

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "calendar.h"
#include "config.h"
#include "database.h"
#include "alliance.h"
#include "ally.h"
#include "curse.h"
@ -106,7 +107,6 @@ static void free_faction(faction * f)
free(f->email);
free(f->banner);
free(f->_password);
free(f->name);
if (f->seen_factions) {
selist_free(f->seen_factions);
@ -245,7 +245,7 @@ faction *addfaction(const char *email, const char *password,
f->alliance_joindate = turn;
f->lastorders = turn;
f->_alive = true;
f->_password = NULL;
f->password_id = 0;
f->age = 0;
f->race = frace;
f->magiegebiet = 0;
@ -321,9 +321,11 @@ unit *addplayer(region * r, faction * f)
bool checkpasswd(const faction * f, const char *passwd)
{
const char *pwhash;
if (!passwd) return false;
if (f->_password && password_verify(f->_password, passwd) == VERIFY_FAIL) {
pwhash = faction_getpassword(f);
if (pwhash && password_verify(pwhash, passwd) == VERIFY_FAIL) {
log_info("password check failed: %s", factionname(f));
return false;
}
@ -603,11 +605,17 @@ void faction_setbanner(faction * self, const char *banner)
self->banner = str_strdup(banner);
}
const char *faction_getpassword(const faction *f) {
if (f->password_id > 0) {
return dbstring_load(f->password_id, NULL);
}
return NULL;
}
void faction_setpassword(faction * f, const char *pwhash)
{
assert(pwhash);
free(f->_password);
f->_password = str_strdup(pwhash);
f->password_id = dbstring_save(pwhash);
}
bool valid_race(const struct faction *f, const struct race *rc)
@ -844,7 +852,8 @@ int writepasswd(void)
for (f = factions; f; f = f->next) {
fprintf(F, "%s:%s:%s:%d\n",
itoa36(f->no), faction_getemail(f), f->_password, f->uid);
itoa36(f->no), faction_getemail(f),
faction_getpassword(f), f->uid);
}
fclose(F);
return 0;

View File

@ -21,6 +21,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "skill.h"
#include "types.h"
#include "db/driver.h"
#include <util/resolve.h>
#include <modules/score.h>
@ -72,7 +73,7 @@ extern "C" {
char *name;
char *banner;
char *email;
char *_password;
dbrow_id password_id;
int max_spelllevel;
struct spellbook *spellbook;
const struct locale *locale;
@ -154,6 +155,7 @@ extern "C" {
void faction_setemail(struct faction *self, const char *email);
void faction_setpassword(struct faction *self, const char *pwhash);
const char *faction_getpassword(const struct faction *f);
bool valid_race(const struct faction *f, const struct race *rc);
void faction_getorigin(const struct faction * f, int id, int *x, int *y);

View File

@ -931,7 +931,6 @@ static void read_password(gamedata *data, faction *f) {
else {
faction_setpassword(f, (data->version >= CRYPT_VERSION) ? name : password_hash(name, PASSWORD_DEFAULT));
}
(void)_test_read_password;
}
void _test_read_password(gamedata *data, faction *f) {
@ -939,8 +938,7 @@ void _test_read_password(gamedata *data, faction *f) {
}
static void write_password(gamedata *data, const faction *f) {
WRITE_TOK(data->store, (const char *)f->_password);
(void)_test_write_password;
WRITE_TOK(data->store, faction_getpassword(f));
}
void _test_write_password(gamedata *data, const faction *f) {

View File

@ -432,7 +432,7 @@ static void test_read_password_external(CuTest *tc) {
}
f = test_create_faction(NULL);
faction_setpassword(f, password_hash("secret", PASSWORD_DEFAULT));
CuAssertPtrNotNull(tc, f->_password);
CuAssertPtrNotNull(tc, faction_getpassword(f));
mstream_init(&data.strm);
gamedata_init(&data, &store, RELEASE_VERSION);
WRITE_TOK(data.store, "newpassword");

View File

@ -1554,14 +1554,6 @@ void prepare_report(report_context *ctx, faction *f)
rule_lighthouse_units = config_get_int("rules.lighthouse.unit_capacity", 0) != 0;
}
if (f->age <= 2) {
if ((f->flags&FFL_PWMSG) == 0) {
/* TODO: this assumes unencrypted passwords */
f->flags |= FFL_PWMSG;
ADDMSG(&f->msgs, msg_message("changepasswd", "value", f->_password));
}
}
ctx->f = f;
ctx->report_time = time(NULL);
ctx->addresses = NULL;

View File

@ -902,7 +902,7 @@ CuSuite *get_reports_suite(void)
SUITE_ADD_TEST(suite, test_region_distance);
SUITE_ADD_TEST(suite, test_region_distance_max);
SUITE_ADD_TEST(suite, test_region_distance_ql);
SUITE_ADD_TEST(suite, test_newbie_password_message);
DISABLE_TEST(suite, test_newbie_password_message);
SUITE_ADD_TEST(suite, test_prepare_report);
SUITE_ADD_TEST(suite, test_seen_neighbours);
SUITE_ADD_TEST(suite, test_seen_travelthru);