From b6d44410b7e0becaa272b340371699414432e0e5 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 12 Jan 2016 06:46:51 +0100 Subject: [PATCH] make the password pseudo-private to faction.c --- src/bind_faction.c | 9 +----- src/kernel/faction.c | 46 ++++++++++++++++++++-------- src/kernel/faction.h | 5 ++-- src/kernel/faction.test.c | 2 +- src/kernel/save.c | 8 ++--- src/laws.c | 63 ++------------------------------------- src/laws.h | 1 - src/laws.test.c | 4 ++- src/report.c | 5 +--- src/study.test.c | 1 + src/tests.c | 1 + 11 files changed, 51 insertions(+), 94 deletions(-) diff --git a/src/bind_faction.c b/src/bind_faction.c index 19f4c8989..1e6547fb1 100644 --- a/src/bind_faction.c +++ b/src/bind_faction.c @@ -380,13 +380,6 @@ static int tolua_faction_create(lua_State * L) return 1; } -static int tolua_faction_get_password(lua_State * L) -{ - faction *self = (faction *)tolua_tousertype(L, 1, 0); - tolua_pushstring(L, faction_getpassword(self)); - return 1; -} - static int tolua_faction_set_password(lua_State * L) { faction *self = (faction *)tolua_tousertype(L, 1, 0); @@ -561,7 +554,7 @@ void tolua_faction_open(lua_State * L) tolua_variable(L, TOLUA_CAST "heroes", tolua_faction_get_heroes, NULL); tolua_variable(L, TOLUA_CAST "maxheroes", tolua_faction_get_maxheroes, NULL); - tolua_variable(L, TOLUA_CAST "password", tolua_faction_get_password, + tolua_variable(L, TOLUA_CAST "password", NULL, tolua_faction_set_password); tolua_variable(L, TOLUA_CAST "email", tolua_faction_get_email, tolua_faction_set_email); diff --git a/src/kernel/faction.c b/src/kernel/faction.c index 3ed6ee095..56996d606 100755 --- a/src/kernel/faction.c +++ b/src/kernel/faction.c @@ -104,7 +104,7 @@ static void free_faction(faction * f) free(f->email); free(f->banner); - free(f->passw); + free(f->_password); free(f->name); if (f->seen_factions) { ql_free(f->seen_factions); @@ -251,7 +251,9 @@ faction *addfaction(const char *email, const char *password, log_warning("Invalid email address for faction %s: %s\n", itoa36(f->no), email); } + if (!password) password = itoa36(rng_int()); faction_setpassword(f, password); + ADDMSG(&f->msgs, msg_message("changepasswd", "value", password)); f->alliance_joindate = turn; f->lastorders = turn; @@ -314,8 +316,8 @@ unit *addplayer(region * r, faction * f) bool checkpasswd(const faction * f, const char *passwd) { if (!passwd) return false; - if (strcmp(f->passw, passwd)==0) return true; - if (unicode_utf8_strcasecmp(f->passw, passwd) == 0) { + if (strcmp(f->_password, passwd)==0) return true; + if (unicode_utf8_strcasecmp(f->_password, passwd) == 0) { log_warning("case-sensitive password check failed: %s", factionname(f)); return true; } @@ -565,11 +567,9 @@ void faction_setbanner(faction * self, const char *banner) void faction_setpassword(faction * f, const char *passw) { - free(f->passw); - if (passw) - f->passw = _strdup(passw); - else - f->passw = _strdup(itoa36(rng_int())); + assert(passw); + free(f->_password); + f->_password = _strdup(passw); } bool valid_race(const struct faction *f, const struct race *rc) @@ -584,11 +584,6 @@ bool valid_race(const struct faction *f, const struct race *rc) } } -const char *faction_getpassword(const faction * f) -{ - return f->passw; -} - struct alliance *f_get_alliance(const struct faction *f) { if (f->alliance && !(f->alliance->flags & ALF_NON_ALLIED)) { @@ -853,3 +848,28 @@ faction *dfindhash(int no) } return 0; } + +int writepasswd(void) +{ + FILE *F; + char zText[128]; + + sprintf(zText, "%s/passwd", basepath()); + F = fopen(zText, "w"); + if (!F) { + perror(zText); + } + else { + faction *f; + log_info("writing passwords..."); + + for (f = factions; f; f = f->next) { + fprintf(F, "%s:%s:%s:%u\n", + factionid(f), f->email, f->_password, f->subscription); + } + fclose(F); + return 0; + } + return 1; +} + diff --git a/src/kernel/faction.h b/src/kernel/faction.h index 933421da5..153a948b1 100644 --- a/src/kernel/faction.h +++ b/src/kernel/faction.h @@ -68,7 +68,7 @@ extern "C" { char *name; char *banner; char *email; - char *passw; + char *_password; int max_spelllevel; struct spellbook *spellbook; const struct locale *locale; @@ -121,6 +121,7 @@ extern "C" { struct faction *addfaction(const char *email, const char *password, const struct race *frace, const struct locale *loc, int subscription); bool checkpasswd(const faction * f, const char *passwd); + int writepasswd(void); void destroyfaction(faction ** f); bool faction_alive(const struct faction *f); @@ -152,7 +153,7 @@ extern "C" { const char *faction_getemail(const struct faction *self); void faction_setemail(struct faction *self, const char *email); - const char *faction_getpassword(const struct faction *self); + // const char *faction_getpassword(const struct faction *self); void faction_setpassword(struct faction *self, const char *password); bool valid_race(const struct faction *f, const struct race *rc); diff --git a/src/kernel/faction.test.c b/src/kernel/faction.test.c index 578c31c86..905704e83 100644 --- a/src/kernel/faction.test.c +++ b/src/kernel/faction.test.c @@ -107,7 +107,7 @@ static void test_addfaction(CuTest *tc) { CuAssertPtrEquals(tc, NULL, (void *)f->ursprung); CuAssertPtrEquals(tc, (void *)factions, (void *)f); CuAssertStrEquals(tc, "test@eressea.de", f->email); - CuAssertStrEquals(tc, "hurrdurr", f->passw); + CuAssertIntEquals(tc, true, checkpasswd(f, "hurrdurr")); CuAssertPtrEquals(tc, (void *)lang, (void *)f->locale); CuAssertIntEquals(tc, 1234, f->subscription); CuAssertIntEquals(tc, 0, f->flags); diff --git a/src/kernel/save.c b/src/kernel/save.c index de8f84490..fa8da2a7e 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -1216,7 +1216,7 @@ faction *readfaction(struct gamedata * data) } READ_STR(data->store, name, sizeof(name)); - f->passw = _strdup(name); + faction_setpassword(f, name); if (data->version < NOOVERRIDE_VERSION) { READ_STR(data->store, 0, 0); } @@ -1320,10 +1320,10 @@ void writefaction(struct gamedata *data, const faction * f) } WRITE_INT(data->store, f->alliance_joindate); - WRITE_STR(data->store, (const char *)f->name); - WRITE_STR(data->store, (const char *)f->banner); + WRITE_STR(data->store, f->name); + WRITE_STR(data->store, f->banner); WRITE_STR(data->store, f->email); - WRITE_TOK(data->store, (const char *)f->passw); + WRITE_TOK(data->store, f->_password); WRITE_TOK(data->store, locale_name(f->locale)); WRITE_INT(data->store, f->lastorders); WRITE_INT(data->store, f->age); diff --git a/src/laws.c b/src/laws.c index 904563a79..a78a3371f 100755 --- a/src/laws.c +++ b/src/laws.c @@ -733,9 +733,6 @@ void nmr_warnings(void) faction *f, *fa; #define FRIEND (HELP_GUARD|HELP_MONEY) for (f = factions; f; f = f->next) { - if (f->age <= 1) { - ADDMSG(&f->msgs, msg_message("changepasswd", "value", f->passw)); - } if (!fval(f, FFL_NOIDLEOUT) && turn > f->lastorders) { ADDMSG(&f->msgs, msg_message("nmr_warning", "")); if (turn - f->lastorders == NMRTimeout() - 1) { @@ -2168,16 +2165,13 @@ int password_cmd(unit * u, struct order *ord) } } } - free(u->faction->passw); if (!pwok) { cmistake(u, ord, 283, MSG_EVENT); - u->faction->passw = _strdup(itoa36(rng_int())); - } - else { - u->faction->passw = _strdup(pwbuf); + strlcpy(pwbuf, itoa36(rng_int()), sizeof(pwbuf)); } + faction_setpassword(u->faction, pwbuf); ADDMSG(&u->faction->msgs, msg_message("changepasswd", - "value", u->faction->passw)); + "value", pwbuf)); return 0; } @@ -4256,32 +4250,6 @@ static void maintain_buildings_1(region * r) maintain_buildings(r, false); } -/** warn about passwords that are not US ASCII. - * even though passwords are technically UTF8 strings, the server receives - * them as part of the Subject of an email when reports are requested. - * This means that we need to limit them to ASCII characters until that - * mechanism has been changed. - */ -static int warn_password(void) -{ - faction *f; - for (f = factions; f; f = f->next) { - bool pwok = true; - const char *c = f->passw; - while (*c && pwok) { - if (!isalnum((unsigned char)*c)) - pwok = false; - c++; - } - if (!pwok) { - free(f->passw); - f->passw = _strdup(itoa36(rng_int())); - ADDMSG(&f->msgs, msg_message("illegal_password", "newpass", f->passw)); - } - } - return 0; -} - void init_processor(void) { int p; @@ -4463,31 +4431,6 @@ void processorders(void) /* immer ausführen, wenn neue Sprüche dazugekommen sind, oder sich * Beschreibungen geändert haben */ update_spells(); - warn_password(); -} - -int writepasswd(void) -{ - FILE *F; - char zText[128]; - - sprintf(zText, "%s/passwd", basepath()); - F = fopen(zText, "w"); - if (!F) { - perror(zText); - } - else { - faction *f; - log_info("writing passwords..."); - - for (f = factions; f; f = f->next) { - fprintf(F, "%s:%s:%s:%u\n", - factionid(f), f->email, f->passw, f->subscription); - } - fclose(F); - return 0; - } - return 1; } void update_subscriptions(void) diff --git a/src/laws.h b/src/laws.h index e245f3c01..6c8633536 100755 --- a/src/laws.h +++ b/src/laws.h @@ -38,7 +38,6 @@ extern "C" { extern int dropouts[2]; extern int *age; - int writepasswd(void); void demographics(void); void immigration(void); void update_guards(void); diff --git a/src/laws.test.c b/src/laws.test.c index 91b04b257..5d21250cd 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -1061,6 +1061,7 @@ static void test_ally_cmd(CuTest *tc) { test_cleanup(); u = test_create_unit(test_create_faction(0), test_create_region(0, 0, 0)); f = test_create_faction(0); + u->faction->locale = lang = get_or_create_locale("de"); locale_setstring(lang, parameters[P_NOT], "NICHT"); locale_setstring(lang, parameters[P_GUARD], "BEWACHE"); @@ -1105,8 +1106,9 @@ static void test_nmr_warnings(CuTest *tc) { CuAssertIntEquals(tc, 0, f1->age); nmr_warnings(); CuAssertPtrNotNull(tc, f1->msgs); - CuAssertPtrNotNull(tc, test_find_messagetype(f1->msgs, "changepasswd")); + CuAssertPtrNotNull(tc, test_find_messagetype(f1->msgs, "nmr_warning")); CuAssertPtrNotNull(tc, f2->msgs); + CuAssertPtrNotNull(tc, f2->msgs->begin); CuAssertPtrNotNull(tc, test_find_messagetype(f2->msgs, "nmr_warning")); CuAssertPtrNotNull(tc, test_find_messagetype(f2->msgs, "nmr_warning_final")); test_cleanup(); diff --git a/src/report.c b/src/report.c index 7b04e020f..f22cb6049 100644 --- a/src/report.c +++ b/src/report.c @@ -1427,7 +1427,7 @@ report_template(const char *filename, report_context * ctx, const char *charset) newline(out); newline(out); - sprintf(buf, "%s %s \"%s\"", LOC(f->locale, "ERESSEA"), factionid(f), f->passw); + sprintf(buf, "%s %s \"password\"", LOC(f->locale, "ERESSEA"), factionid(f)); rps_nowrap(out, buf); newline(out); newline(out); @@ -2112,9 +2112,6 @@ const char *charset) if (f->age <= 2) { const char *s; - RENDER(f, buf, sizeof(buf), ("newbie_password", "password", f->passw)); - newline(out); - centre(out, buf, true); s = locale_getstring(f->locale, "newbie_info_1"); if (s) { newline(out); diff --git a/src/study.test.c b/src/study.test.c index 4b30ae842..6b9d97787 100644 --- a/src/study.test.c +++ b/src/study.test.c @@ -45,6 +45,7 @@ static void setup_study(study_fixture *fix, skill_t sk) { fix->teachers[1] = test_create_unit(f, r); assert(fix->teachers[1]); fix->teachers[1]->thisorder = create_order(K_TEACH, f->locale, "%s", itoa36(fix->u->no)); + test_clear_messages(f); } static void test_study_no_teacher(CuTest *tc) { diff --git a/src/tests.c b/src/tests.c index c5f98502e..6fef425d0 100644 --- a/src/tests.c +++ b/src/tests.c @@ -64,6 +64,7 @@ struct region *test_create_region(int x, int y, const terrain_type *terrain) struct faction *test_create_faction(const struct race *rc) { faction *f = addfaction("nobody@eressea.de", NULL, rc ? rc : test_create_race("human"), default_locale, 0); + test_clear_messages(f); return f; }