suwako / SimPas1-Unix アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
SimPas1-Unix / src / chkpass.cc
8.5 KB、279行 生 Blame 履歴
1#include "common.hh"
2#include "chkpass.hh"
3#include "../main.hh"
4#include "showpass.hh"
5
6#include <FL/Fl.H>
7#include <FL/Fl_Window.H>
8#include <FL/fl_ask.H>
9#include <FL/Fl_Box.H>
10#include <FL/Fl_Button.H>
11#include <FL/Fl_Text_Display.H>
12#include <FL/Fl_Text_Buffer.H>
13#include <FL/Fl_Scrollbar.H>
14
15#include <string>
16#include <thread>
17#include <algorithm>
18
19Chkpass chk;
20std::vector<std::string> foundRes;
21std::vector<std::string> duppath;
22std::vector<std::string> duppasswd;
23
24struct InputData {
25 Fl_Radio_Button *all;
26 Fl_Radio_Button *len;
27 Fl_Radio_Button *chr;
28 Fl_Radio_Button *dup;
29 Fl_Window *dialog;
30};
31
32void Chkpass::lenPass(const std::string &path, const std::string &pass,
33 const std::string &lang) {
34 if (pass.length() > 0 && pass.length() < minimumlen) {
35 std::string res = "【E】";
36 res += (lang.compare(0, 2, "en") == 0 ?
37 "The password \"" + path + "\" is too short, minimum length should be " +
38 std::to_string(minimumlen) + " characters, recommended is " +
39 std::to_string(recommendlen) + " characters.":
40 "パスワード「" + path + "」は短すぎます。最短パスワードの長さは" +
41 std::to_string(minimumlen) + "文字ですが、勧めが" +
42 std::to_string(recommendlen) + "文字です。");
43
44 foundRes.push_back(res);
45 weaklencount++;
46 vulncount++;
47 } else if (pass.length() >= minimumlen && pass.length() < recommendlen) {
48 std::string res = "【W】";
49 res += (lang.compare(0, 2, "en") == 0 ?
50 "The password \"" + path + "\" is long enough, but for optimal security, " +
51 std::to_string(recommendlen) + " characters is recommended.\n" :
52 "パスワード「" + path + "」の長さは十分ですが、最強のセキュリティには" +
53 std::to_string(recommendlen) + "文字が勧めします。\n");
54
55 foundRes.push_back(res);
56 }
57}
58
59void Chkpass::charPass(const std::string &path, const std::string &pass,
60 const std::string &lang) {
61 bool isUpper = false;
62 bool isLower = false;
63 bool isDigit = false;
64 bool isSpecial = false;
65
66 for (char ch : pass) {
67 if (std::isupper(static_cast<unsigned char>(ch))) isUpper = true;
68 if (std::islower(static_cast<unsigned char>(ch))) isLower = true;
69 if (std::isdigit(static_cast<unsigned char>(ch))) isDigit = true;
70 if (std::find(spchar.begin(), spchar.end(), ch) != spchar.end()) isSpecial = true;
71 }
72
73 if (!isUpper || !isLower || !isDigit || !isSpecial) {
74 std::string res = "【E】";
75 res += (lang.compare(0, 2, "en") == 0 ?
76 "The password \"" + path + "\" is too weak! A strong password contains " +
77 "at least 1 uppercase, 1 lowercase, 1 digit, and 1 special character." :
78 "パスワード「" + path + "」は弱すぎます!強いパスワードは最大1大文字、" +
79 "1小文字、1数字、及び1記号の文字が含みます。");
80
81 foundRes.push_back(res);
82 weakcharcount++;
83 vulncount++;
84 }
85}
86
87void Chkpass::dupPass(const std::string &path, const std::string &pass,
88 const std::string &lang) {
89 for (std::size_t k = 0; k < duppasswd.size(); k++) {
90 if (pass.compare(duppasswd[k]) == 0) {
91 std::string res = "【E】";
92 res += (lang.compare(0, 2, "en") == 0 ?
93 "The password \"" + path + "\" is the same as \"" + duppath[k] + "\". " +
94 "For security, please keep passwords unique!":
95 "パスワード「" + path + "」は「" + duppath[k] + "」と一致しています。" +
96 "セキュリティの為、各パスワードはユニークにする様にして下さい!");
97
98 foundRes.push_back(res);
99 duppasscount++;
100 vulncount++;
101 }
102 }
103
104 duppath.push_back(path);
105 duppasswd.push_back(pass);
106}
107
108bool Chkpass::exec() {
109 std::string lang = Common::getlang();
110
111 // パスワードをスキャンして
112 Showpass show;
113
114 for (const auto &dispath : dispaths) {
115 std::string fullpath = Common::getbasedir(true) + dispath + ".gpg";
116 std::string pass = show.exec(fullpath.c_str(), true);
117 if (pass.empty()) continue;
118 if (pass.rfind("otpauth://totp/", 0) == 0) continue;
119
120 if (isAll) {
121 lenPass(dispath, pass, lang);
122 charPass(dispath, pass, lang);
123 dupPass(dispath, pass, lang);
124 } else if (isLen) {
125 lenPass(dispath, pass, lang);
126 } else if (isChar) {
127 charPass(dispath, pass, lang);
128 } else if (isDup) {
129 dupPass(dispath, pass, lang);
130 }
131 }
132
133 return true;
134}
135
136void Chkpass::showRes() {
137 std::string lang = Common::getlang();
138
139 std::string res;
140 if (lang.compare(0, 2, "en") == 0) {
141 res = "Weak passwords:\n";
142 } else {
143 res = "不安定なパスワード:\n";
144 }
145
146 for (const auto &path : foundRes) {
147 res += path + "\n";
148 }
149
150 if (lang.compare(0, 2, "en") == 0) {
151 res += "Short password count: " + std::to_string(weaklencount) + "\n";
152 res += "Weak password count: " + std::to_string(weakcharcount) + "\n";
153 res += "Duplicate password count: " + std::to_string(duppasscount) + "\n";
154 res += "Total: " + std::to_string(duppath.size()) + "\n";
155 res += "It's advised to change any of the";
156 res += "weak passwords as soon as possible!";
157 } else {
158 res += "短いパスワード数: " + std::to_string(weaklencount) + "\n";
159 res += "弱いパスワード数: " + std::to_string(weakcharcount) + "\n";
160 res += "同じパスワード数: " + std::to_string(duppasscount) + "\n";
161 res += "合計: " + std::to_string(duppath.size()) + "\n";
162 res += "不安定なパスワードは出来るだけ早く変更する事をお勧めします!";
163 }
164
165 Fl_Window *win = new Fl_Window(500, 440, lang.compare(0, 2, "en") == 0 ?
166 "Results" : "結果");
167 Fl_Text_Display *display = new Fl_Text_Display(10, 10, 480, 380);
168 Fl_Text_Buffer *textbuf = new Fl_Text_Buffer();
169
170 textbuf->text(res.c_str());
171 display->buffer(textbuf);
172 display->scrollbar_width(15);
173 win->resizable(display);
174
175 Fl_Button *okBtn = new Fl_Button(210, 400, 80, 30, "OK");
176 okBtn->callback([](Fl_Widget *widget, void *win) {
177 (void)widget;
178 reinterpret_cast<Fl_Window*>(win)->hide();
179 }, win);
180
181 win->add(okBtn);
182
183 win->end();
184 win->show();
185}
186
187void Chkpass::chk_cb(Fl_Widget *, void *data) {
188 InputData *inputs = (InputData *)data;
189 std::string lang = Common::getlang();
190
191 if (inputs) {
192 isAll = inputs->all->value();
193 isLen = inputs->len->value();
194 isChar = inputs->chr->value();
195 isDup = inputs->dup->value();
196 }
197
198 Fl_Window *dialog = new Fl_Window(400, 50,
199 (lang.compare(0, 2, "en") == 0 ?
200 "Checking for weak password" : "不安的なパスワードの確認中"));
201
202 Fl_Box *box = new Fl_Box(10, 10, 380, 20,
203 (lang.compare(0, 2, "en") == 0 ?
204 "Checking, please wait for a while..." :
205 "確認中。暫くお待ち下さい・・・"));
206
207 dialog->add(box);
208
209 dialog->end();
210 dialog->set_modal();
211 dialog->show();
212
213 std::thread checker([dialog]() {
214 chk.exec();
215
216 Fl::lock();
217 dialog->hide();
218 chk.showRes();
219 Fl::unlock();
220 });
221
222 checker.detach();
223}
224
225void Chkpass::dialog_cb(Fl_Widget *w, void *) {
226 (void)w;
227
228 std::string lang = Common::getlang();
229
230 Fl_Window *dialog = new Fl_Window(390, 145,
231 (lang.compare(0, 2, "en") == 0 ?
232 "Check for weak password" : "不安的なパスワードの確認"));
233
234 Fl_Radio_Button *all = new Fl_Radio_Button(10, 10, 180, 30,
235 (lang.compare(0, 2, "en") == 0) ? "Everything" : "全部");
236 Fl_Radio_Button *len = new Fl_Radio_Button(10, 50, 180, 30,
237 (lang.compare(0, 2, "en") == 0) ? "Length" : "長さ");
238 Fl_Radio_Button *chr = new Fl_Radio_Button(200, 10, 180, 30,
239 (lang.compare(0, 2, "en") == 0) ? "Strength" : "強さ");
240 Fl_Radio_Button *dup = new Fl_Radio_Button(200, 50, 180, 30,
241 (lang.compare(0, 2, "en") == 0) ? "Duplicate" : "服数度");
242
243 dialog->add(all);
244 dialog->add(len);
245 dialog->add(chr);
246 dialog->add(dup);
247
248 InputData *inputs = new InputData();
249 inputs->all = all;
250 inputs->len = len;
251 inputs->chr = chr;
252 inputs->dup = dup;
253 inputs->dialog = dialog;
254
255 Fl_Button *okbtn = new Fl_Button(105, 100, 80, 30, "OK");
256 Fl_Button *cancelbtn = new Fl_Button(205, 100, 80, 30,
257 (lang.compare(0, 2, "en") == 0 ? "Cancel" : "キャンセル"));
258
259 okbtn->callback(static_ok_cb, inputs);
260 cancelbtn->callback(static_cancel_cb, dialog);
261
262 dialog->add(okbtn);
263 dialog->add(cancelbtn);
264
265 dialog->end();
266 dialog->set_modal();
267 dialog->show();
268}
269
270void Chkpass::static_ok_cb(Fl_Widget *w, void *data) {
271 (void)w;
272 InputData *inputs = (InputData *)data;
273
274 chk.chk_cb(nullptr, inputs);
275}
276
277void Chkpass::static_cancel_cb(Fl_Widget *w, void *data) {
278 ((Chkpass *)data)->cancel_cb(w, data);
279}