-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathrecover.cc
348 lines (295 loc) · 8.29 KB
/
recover.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
extern "C" {
#include "bbs.h"
#include "daemons.h"
}
#ifdef USE_VERIFYDB_ACCOUNT_RECOVERY
# ifndef USE_VERIFYDB
# error "USE_VERIFYDB_ACCOUNT_RECOVERY requires USE_VERIFYDB"
# endif
#include <optional>
#include <string>
#include "verifydb.h"
#include "verifydb.fbs.h"
namespace {
constexpr int kMaxErrCnt = 3;
constexpr size_t kCodeLen = 30;
struct UserHandle {
std::string userid;
int64_t generation;
};
class AccountRecovery {
public:
void Run();
private:
std::optional<UserHandle> user_;
std::string email_;
std::vector<std::string> all_emails_;
int y_ = 2;
bool LoadUser(const char *userid);
bool LoadVerifyDbEmail();
void InputUserEmail();
void EmailCodeChallenge();
void ResetPasswd();
static std::optional<std::string> NormalizeEmail(const std::string &email);
static bool SendRecoveryCode(const std::string &email,
const std::string &code);
static bool SetPasswd(const UserHandle &user, const char *hashed_passwd);
static void LogToSecurity(const UserHandle &user, const std::string &email);
static void NotifyUser(const std::string &userid, const std::string &email);
static std::string GenCode(size_t len);
static void UserErrorExit();
};
bool AccountRecovery::LoadUser(const char *userid) {
if (!is_validuserid(userid))
return false;
userec_t u = {};
if (!getuser(userid, &u))
return false;
// Double check.
if (!is_validuserid(u.userid))
return false;
UserHandle user;
user.userid = u.userid;
user.generation = u.firstlogin;
user_ = user;
#ifdef USEREC_EMAIL_IS_CONTACT
auto email = NormalizeEmail(u.email);
if (email)
all_emails_.push_back(email.value());
#endif
return true;
}
bool AccountRecovery::LoadVerifyDbEmail() {
Bytes buf;
const VerifyDb::GetReply *reply;
if (!verifydb_getuser(user_->userid.c_str(), user_->generation, &buf, &reply))
return false;
if (reply->entry()) {
for (const auto *ent : *reply->entry()) {
if (ent->vmethod() == VMETHOD_EMAIL && ent->vkey() != nullptr) {
auto email = NormalizeEmail(ent->vkey()->str());
if (email)
all_emails_.push_back(email.value());
}
}
}
return true;
}
// static
std::optional<std::string>
AccountRecovery::NormalizeEmail(const std::string &email) {
auto out = email;
for (auto& c : out)
c = std::tolower(c);
auto idx = out.find('@');
if (idx == std::string::npos)
return std::nullopt;
if (idx != out.rfind('@'))
return std::nullopt;
return out;
}
// static
bool AccountRecovery::SendRecoveryCode(const std::string &email,
const std::string &code) {
std::string subject;
subject.append(" " BBSNAME " - 重設密碼認證碼 [ ");
subject.append(code);
subject.append(" ] @ IP ");
subject.append(fromhost);
bsmtp("etc/recovermail", subject.c_str(), email.c_str(), "non-exist");
return true;
}
// static
bool AccountRecovery::SetPasswd(const UserHandle &user,
const char *hashed_passwd) {
userec_t u = {};
int unum = getuser(user.userid.c_str(), &u);
if (unum <= 0)
return false;
// Check userid and generation again.
if (std::string(u.userid) != user.userid || u.firstlogin != user.generation)
return false;
strlcpy(u.passwd, hashed_passwd, sizeof(u.passwd));
return 0 == passwd_sync_update(unum, &u);
}
// static
void AccountRecovery::LogToSecurity(const UserHandle &user,
const std::string &email) {
std::string title = "重設密碼: ";
title.append(user.userid);
title.append(" (以信箱認證身份)");
std::string msg;
msg.append("userid: ");
msg.append(user.userid);
msg.append("\nfirstlogin: ");
msg.append(std::to_string(user.generation));
msg.append("\nemail: ");
msg.append(email);
msg.append("\nip: ");
msg.append(fromhost);
msg.append("\n");
post_msg(BN_SECURITY, title.c_str(), msg.c_str(), "[系統安全局]");
}
// static
std::string AccountRecovery::GenCode(size_t len) {
std::string s(len, '\0');
random_text_code(&s[0], s.size());
return s;
}
// static
void AccountRecovery::UserErrorExit() {
vmsg("錯誤次數過多,請重新操作。");
exit(0);
}
void AccountRecovery::InputUserEmail() {
#ifndef USEREC_EMAIL_IS_CONTACT
mvprints(y_, 0, "目前僅能取回使用 Email 註冊之帳號。");
#endif
y_ += 2;
// Input userid.
char userid[IDLEN + 1] = {};
int errcnt = 0;
while (1) {
getdata_buf(y_, 0, "欲取回帳號ID:", userid, sizeof(userid), DOECHO);
if (LoadUser(userid))
break;
if (++errcnt >= kMaxErrCnt)
UserErrorExit();
mvprints(y_ + 1, 0, "無此 ID,請重新輸入。");
userid[0] = '\0';
}
y_++;
// Input email.
char email[EMAILSZ] = {};
errcnt = 0;
while (1) {
getdata_buf(y_, 0, "認證信箱:", email, sizeof(email), DOECHO);
strip_blank(email, email);
str_lower(email, email);
if (*email)
break;
if (++errcnt >= kMaxErrCnt)
UserErrorExit();
mvprints(y_ + 1, 0, "請重新輸入。");
email[0] = '\0';
}
email_ = email;
y_++;
move(y_, 0);
clrtoeol(); // There might be error message at this line.
if (!LoadVerifyDbEmail()) {
vmsg("系統錯誤,請稍候再試。");
exit(0);
}
}
void AccountRecovery::EmailCodeChallenge() {
// Generate code.
auto code = GenCode(kCodeLen);
y_++;
mvprints(y_, 0, "系統處理中...");
doupdate();
// Check and send recovery code. Don't exit if email doesn't match, so user
// can't guess email.
bool email_matches = false;
for (const auto &email : all_emails_) {
if (email == email_)
email_matches = true;
}
if (email_matches) {
// We only want to send email if the user input the matching address.
// Silently fail if email is not matching, so that user cannot guess other
// user's email address.
SendRecoveryCode(email_, code);
}
// Add a random 5-10s delay to prevent timing oracle.
usleep(5000000 + random() % 5000000);
mvprints(y_++, 0, "若您輸入的資料正確,系統已將認證碼寄送至您的信箱。");
// Input code.
char incode[kCodeLen + 1] = {};
int errcnt = 0;
while (1) {
getdata_buf(y_, 0, "請收信後輸入認證碼:", incode, sizeof(incode), DOECHO);
if (email_matches && code == std::string(incode))
break;
if (++errcnt >= kMaxErrCnt)
UserErrorExit();
mvprints(y_ + 1, 0, "認證碼錯誤!認證碼共有 %d 字元。", (int)kCodeLen);
incode[0] = '\0';
}
y_++;
move(y_, 0);
clrtoeol(); // There might be error message at this line.
// Some paranoid checkings, we are about to let user reset their password.
if (code.size() != kCodeLen || code != std::string(incode)) {
assert(false);
exit(1);
}
}
void AccountRecovery::ResetPasswd() {
// Input new password.
const char *hashed = nullptr;
y_++;
int errcnt = 0;
while (1) {
if (errcnt++ >= kMaxErrCnt * 2)
UserErrorExit();
char pass[PASS_INPUT_LEN + 1] = {};
char confirm[PASS_INPUT_LEN + 1] = {};
move(y_, 0);
clrtobot();
if (!getdata(y_, 0, "請設定新密碼:", pass, sizeof(pass), PASSECHO) ||
strlen(pass) < 4) {
vmsg("請重新輸入,密碼必須至少四字元");
continue;
}
move(y_ + 1, 0);
outs("請注意設定密碼只有前八個字元有效,超過的將自動忽略。");
getdata(y_ + 2, 0, "請檢查新密碼:", confirm, sizeof(confirm), PASSECHO);
if (strcmp(pass, confirm)) {
vmsg("新密碼確認失敗, 請重新輸入");
continue;
}
// Hash password.
hashed = genpasswd(pass);
memset(pass, 0, sizeof(pass));
memset(confirm, 0, sizeof(confirm));
break;
}
assert(hashed);
if (!SetPasswd(user_.value(), hashed)) {
vmsg("密碼重設失敗,請稍候再試。");
return;
}
LogToSecurity(user_.value(), email_);
for (const auto &email : all_emails_) {
notify_password_change(user_->userid.c_str(), email.c_str());
}
// Log to user security.
{
char logfn[PATHLEN];
sethomefile(logfn, user_->userid.c_str(), FN_USERSECURITY);
log_filef(logfn, LOG_CREAT, "%s %s (ResetPasswd) Email: %s\n",
Cdatelite(&now), fromhost, email_.c_str());
}
vmsg("密碼重設完成,請以新密碼登入。");
}
void AccountRecovery::Run() {
vs_hdr("取回帳號");
InputUserEmail();
EmailCodeChallenge();
ResetPasswd();
}
} // namespace
void recover_account() {
#ifdef USE_REMOTE_CAPTCHA
const char *errmsg = remote_captcha();
if (errmsg) {
vmsg(errmsg);
exit(1);
}
#endif
AccountRecovery ac;
ac.Run();
exit(0);
}
#endif