| 1 | #include "src/addpass.hh" |
| 2 | #include "src/delpass.hh" |
| 3 | #include "src/editpass.hh" |
| 4 | #include "src/genpass.hh" |
| 5 | #include "src/initpass.hh" |
| 6 | #include "src/showpass.hh" |
| 7 | #include "src/vulnpass.hh" |
| 8 | #include "src/chkpass.hh" |
| 9 | #include "src/common.hh" |
| 10 | #include "main.hh" |
| 11 | |
| 12 | #undef Status |
| 13 | #undef None |
| 14 | #include <FL/Fl.H> |
| 15 | #include <FL/Fl_Box.H> |
| 16 | #include <FL/Fl_Widget.H> |
| 17 | #include <FL/Fl_Window.H> |
| 18 | #include <FL/Fl_Select_Browser.H> |
| 19 | #include <FL/Fl_Text_Display.H> |
| 20 | #include <FL/Fl_Text_Buffer.H> |
| 21 | #include <FL/Fl_Button.H> |
| 22 | #include <FL/Fl_Check_Button.H> |
| 23 | #include <FL/Fl_Input.H> |
| 24 | #include <FL/Fl_Secret_Input.H> |
| 25 | #include <FL/Fl_Hold_Browser.H> |
| 26 | #include <FL/Fl_Copy_Surface.H> |
| 27 | #include <FL/Fl_PNG_Image.H> |
| 28 | |
| 29 | #include <dirent.h> |
| 30 | #include <sys/stat.h> |
| 31 | |
| 32 | #include <string> |
| 33 | #include <vector> |
| 34 | |
| 35 | Fl_Select_Browser *browser = nullptr; |
| 36 | Fl_Text_Display *textview = nullptr; |
| 37 | Fl_Text_Buffer *textbuf = nullptr; |
| 38 | Fl_Input *searchfield = nullptr; |
| 39 | Fl_Check_Button *hidechk = nullptr; |
| 40 | Fl_Button *showbtn = nullptr; |
| 41 | |
| 42 | Addpass a; |
| 43 | Chkpass c; |
| 44 | Delpass d; |
| 45 | Editpass e; |
| 46 | Genpass g; |
| 47 | Initpass i; |
| 48 | Showpass s; |
| 49 | Vulnpass v; |
| 50 | |
| 51 | const char *sofname = "simpas"; |
| 52 | const char *intname = "SimPas"; |
| 53 | const char *version = "1.1.1"; |
| 54 | const char *basedof = "sp-1.5.1"; |
| 55 | |
| 56 | std::vector<std::string> fullpaths; |
| 57 | std::vector<std::string> dispaths; |
| 58 | std::vector<std::string> filterpaths; |
| 59 | int browseId; |
| 60 | bool isPassHidden = true; |
| 61 | std::string realpass = ""; |
| 62 | |
| 63 | void browse(std::string &path, bool isNew) { |
| 64 | std::string cont = s.exec(path.c_str(), false); |
| 65 | realpass = cont; |
| 66 | |
| 67 | if (isNew) browseId = browser->size(); |
| 68 | browser->value(browseId); |
| 69 | |
| 70 | if (!cont.empty()) { |
| 71 | if (isPassHidden) { |
| 72 | std::string lang = Common::getlang(); |
| 73 | if (lang.compare(0, 2, "en") == 0) { |
| 74 | textbuf->text("(Hidden, please click the \"show\" button to reveal)"); |
| 75 | } else { |
| 76 | textbuf->text("(非表示、「表示」ボタンをクリックして確認して下さい)"); |
| 77 | } |
| 78 | showbtn->activate(); |
| 79 | showbtn->label((Common::getlang().compare(0, 2, "en") == 0) ? |
| 80 | "Show password" : "パスワードの表示"); |
| 81 | } else { |
| 82 | textbuf->text(cont.c_str()); |
| 83 | showbtn->label((Common::getlang().compare(0, 2, "en") == 0) ? |
| 84 | "Hide password" : "パスワードを隠す"); |
| 85 | showbtn->activate();} |
| 86 | Editpass::setFile(path); |
| 87 | Delpass::setFile(path); |
| 88 | d.btn->activate(); |
| 89 | e.btn->activate(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void hide_cb(Fl_Widget *w, void *) { |
| 94 | isPassHidden = ((Fl_Check_Button *)w)->value(); |
| 95 | int idx = browser->value(); |
| 96 | |
| 97 | if (isPassHidden && idx > 0) { |
| 98 | showbtn->activate(); |
| 99 | showbtn->label((Common::getlang().compare(0, 2, "en") == 0) ? |
| 100 | "Show password" : "パスワードの表示"); |
| 101 | } else { |
| 102 | showbtn->deactivate(); |
| 103 | showbtn->label((Common::getlang().compare(0, 2, "en") == 0) ? |
| 104 | "Hide password" : "パスワードを隠す"); |
| 105 | } |
| 106 | |
| 107 | showbtn->label((Common::getlang().compare(0, 2, "en") == 0) ? |
| 108 | "Show password" : "パスワードの表示"); |
| 109 | |
| 110 | if (idx > 0) { |
| 111 | std::string path = filterpaths[idx - 1]; |
| 112 | browse(path, false); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void clearpaths(bool isReset, std::string &path) { |
| 117 | fullpaths.clear(); |
| 118 | dispaths.clear(); |
| 119 | if (isReset) { |
| 120 | std::string mockpath = ""; |
| 121 | Editpass::setFile(mockpath); |
| 122 | Delpass::setFile(mockpath); |
| 123 | d.btn->deactivate(); |
| 124 | e.btn->deactivate(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void updatelist() { |
| 129 | browser->clear(); |
| 130 | filterpaths.clear(); |
| 131 | std::string searchtxt = searchfield->value(); |
| 132 | for (size_t i = 0; i < dispaths.size(); ++i) { |
| 133 | if (dispaths[i].find(searchtxt) != std::string::npos) { |
| 134 | browser->add(dispaths[i].c_str()); |
| 135 | filterpaths.push_back(fullpaths[i]); |
| 136 | } |
| 137 | } |
| 138 | textbuf->text(""); |
| 139 | } |
| 140 | |
| 141 | void search_cb(Fl_Widget *, void *) { |
| 142 | std::string mockpath = ""; |
| 143 | Editpass::setFile(mockpath); |
| 144 | Delpass::setFile(mockpath); |
| 145 | d.btn->deactivate(); |
| 146 | e.btn->deactivate(); |
| 147 | updatelist(); |
| 148 | } |
| 149 | |
| 150 | void copy_cb(Fl_Widget *, void *) { |
| 151 | if (!realpass.empty()) { |
| 152 | Fl::copy(realpass.c_str(), realpass.length(), 1, Fl::clipboard_plain_text); |
| 153 | } else { |
| 154 | Fl::copy("", 0, 1, Fl::clipboard_plain_text); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void browser_cb(Fl_Widget *w, void *) { |
| 159 | (void)w; |
| 160 | int idx = browser->value(); |
| 161 | if (idx == 0) return; |
| 162 | browseId = idx; |
| 163 | |
| 164 | std::string path = filterpaths[idx - 1]; |
| 165 | browse(path, false); |
| 166 | } |
| 167 | |
| 168 | void show_cb(Fl_Widget *, void *) { |
| 169 | int idx = browser->value(); |
| 170 | if (idx == 0) return; |
| 171 | std::string path = filterpaths[idx - 1]; |
| 172 | |
| 173 | if (isPassHidden) { |
| 174 | std::string cont = s.exec(path.c_str(), false); |
| 175 | realpass = cont; |
| 176 | textbuf->text(cont.c_str()); |
| 177 | |
| 178 | std::string lang = Common::getlang(); |
| 179 | if (lang.compare(0, 2, "en") == 0) { |
| 180 | showbtn->label("Hide password"); |
| 181 | } else { |
| 182 | showbtn->label("パスワードを隠す"); |
| 183 | } |
| 184 | isPassHidden = false; |
| 185 | } else { |
| 186 | std::string lang = Common::getlang(); |
| 187 | if (lang.compare(0, 2, "en") == 0) { |
| 188 | textbuf->text("(Hidden, please click the \"show\" button to reveal)"); |
| 189 | showbtn->label("Show password"); |
| 190 | } else { |
| 191 | textbuf->text("(非表示、「表示」ボタンをクリックして確認して下さい)"); |
| 192 | showbtn->label("パスワードの表示"); |
| 193 | } |
| 194 | isPassHidden = true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void scandir(const std::string &dpath, const std::string &rpath, |
| 199 | std::vector<std::string> &fpaths) { |
| 200 | DIR *dir = opendir(dpath.c_str()); |
| 201 | if (!dir) return; |
| 202 | |
| 203 | struct dirent *entry; |
| 204 | while ((entry = readdir(dir)) != nullptr) { |
| 205 | std::string name = entry->d_name; |
| 206 | if (name == "." || name == ".." || name == ".gpg-id") continue; |
| 207 | |
| 208 | std::string fpath = std::string(dpath) + "/" + name; |
| 209 | struct stat s; |
| 210 | if (stat(fpath.c_str(), &s) != 0) { |
| 211 | closedir(dir); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | if (S_ISDIR(s.st_mode)) { |
| 216 | scandir(fpath, rpath, fpaths); |
| 217 | } else if (name.find(".gpg") != std::string::npos) { |
| 218 | std::string rel = fpath.substr(rpath.size() + 1); |
| 219 | fpaths.push_back(rel); |
| 220 | fullpaths.push_back(fpath); |
| 221 | |
| 222 | std::string disname = rel.substr(0, rel.rfind(".gpg")); |
| 223 | dispaths.push_back(disname); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | closedir(dir); |
| 228 | } |
| 229 | |
| 230 | void init_cb(Fl_Widget *w, void *data) { |
| 231 | i.exec(i.gpgid->value()); |
| 232 | i.btn->deactivate(); |
| 233 | ((Initpass *)data)->cancel_cb(w, data); |
| 234 | } |
| 235 | |
| 236 | void init_dialog_cb(Fl_Widget *w, void *) { |
| 237 | (void)w; |
| 238 | std::string lang = Common::getlang(); |
| 239 | Fl_Window *dialog = new Fl_Window(450, 120, |
| 240 | (lang.compare(0, 2, "en") == 0 ? |
| 241 | "Initialize password" : "パスワードの初期設定")); |
| 242 | |
| 243 | i.gpgid = new Fl_Input(90, 20, 300, 30, |
| 244 | (lang.compare(0, 2, "en") == 0 ? "GPG secret key:" : "gpg秘密鍵:")); |
| 245 | dialog->add(i.gpgid); |
| 246 | |
| 247 | Fl_Button *startbtn = new Fl_Button(185, 70, 80, 30, |
| 248 | (lang.compare(0, 2, "en") == 0 ? "Start" : "開始")); |
| 249 | |
| 250 | startbtn->callback(init_cb, dialog); |
| 251 | |
| 252 | dialog->add(startbtn); |
| 253 | |
| 254 | dialog->end(); |
| 255 | dialog->set_modal(); |
| 256 | dialog->show(); |
| 257 | } |
| 258 | |
| 259 | void set_dark_theme() { |
| 260 | Fl::background(35, 32, 35); |
| 261 | Fl::background2(68, 59, 68); |
| 262 | Fl::foreground(252, 252, 252); |
| 263 | } |
| 264 | |
| 265 | int main(int argc, char **argv) { |
| 266 | std::string lang = Common::getlang(); |
| 267 | std::string windowtit = std::string(intname) + " " + version; |
| 268 | Fl_Window *window = new Fl_Window(790, 740, windowtit.c_str()); |
| 269 | |
| 270 | set_dark_theme(); |
| 271 | |
| 272 | #if defined(__linux) |
| 273 | const char *iconPath = "/usr/share/icons/076/512x512/simpas.png"; |
| 274 | #else |
| 275 | const char *iconPath = "/usr/local/share/icons/076/512x512/simpas.png"; |
| 276 | #endif |
| 277 | |
| 278 | Fl_PNG_Image *icon = new Fl_PNG_Image(iconPath); |
| 279 | |
| 280 | window->icon(icon); |
| 281 | |
| 282 | searchfield = new Fl_Input( |
| 283 | (lang.compare(0, 2, "en") == 0 ? 70 : 50), 10, |
| 284 | (lang.compare(0, 2, "en") == 0 ? 710 : 730), 30, |
| 285 | (lang.compare(0, 2, "en") == 0 ? "Search:" : "検索:")); |
| 286 | searchfield->callback(search_cb); |
| 287 | |
| 288 | browser = new Fl_Select_Browser(10, 50, 380, 500); |
| 289 | textview = new Fl_Text_Display(400, 50, 380, 500); |
| 290 | textbuf = new Fl_Text_Buffer(); |
| 291 | textview->buffer(textbuf); |
| 292 | |
| 293 | browser->callback(browser_cb); |
| 294 | |
| 295 | Fl_Button *copybtn = new Fl_Button(400, 600, 150, 30, |
| 296 | (lang.compare(0, 2, "en") == 0 ? "Copy password" : "パスワードのコピー")); |
| 297 | copybtn->callback(copy_cb); |
| 298 | |
| 299 | a.btn = new Fl_Button(10, 560, 150, 30, |
| 300 | (lang.compare(0, 2, "en") == 0 ? "Add password" : "パスワードの追加")); |
| 301 | a.btn->callback(a.dialog_cb); |
| 302 | |
| 303 | d.btn = new Fl_Button(10, 600, 150, 30, |
| 304 | (lang.compare(0, 2, "en") == 0 ? "Delete password" : "パスワードの削除")); |
| 305 | d.btn->deactivate(); |
| 306 | d.btn->callback(d.dialog_cb); |
| 307 | |
| 308 | e.btn = new Fl_Button(400, 560, 150, 30, |
| 309 | (lang.compare(0, 2, "en") == 0 ? "Edit password" : "パスワードの編集")); |
| 310 | e.btn->deactivate(); |
| 311 | e.btn->callback(e.dialog_cb); |
| 312 | |
| 313 | hidechk = new Fl_Check_Button(560, 560, 150, 30, |
| 314 | (lang.compare(0, 2, "en") == 0 ? "Hide password" : "パスワードを隠す")); |
| 315 | hidechk->set(); |
| 316 | hidechk->callback(hide_cb); |
| 317 | |
| 318 | showbtn = new Fl_Button(560, 600, 150, 30, |
| 319 | (lang.compare(0, 2, "en") == 0 ? "Show password" : "パスワードの表示")); |
| 320 | showbtn->deactivate(); |
| 321 | showbtn->callback(show_cb); |
| 322 | |
| 323 | g.btn = new Fl_Button(10, 640, 150, 30, |
| 324 | (lang.compare(0, 2, "en") == 0 ? "Generate password" : "パスワードの作成")); |
| 325 | g.btn->callback(g.dialog_cb); |
| 326 | |
| 327 | i.btn = new Fl_Button(10, 680, 150, 30, |
| 328 | (lang.compare(0, 2, "en") == 0 ? |
| 329 | "Initialize password" : |
| 330 | "パスワードの初期設定")); |
| 331 | |
| 332 | v.btn = new Fl_Button(170, 560, 200, 30, |
| 333 | (lang.compare(0, 2, "en") == 0 ? "Check for breach" : "漏洩されたかの確認")); |
| 334 | v.btn->callback(v.dialog_cb); |
| 335 | |
| 336 | c.btn = new Fl_Button(170, 600, 200, 30, |
| 337 | (lang.compare(0, 2, "en") == 0 ? |
| 338 | "Check for unsafe passwords" : "不安定的なパスワードの確認")); |
| 339 | c.btn->callback(c.dialog_cb); |
| 340 | |
| 341 | std::string gpgidpath = Common::getbasedir(true) + ".gpg-id"; |
| 342 | |
| 343 | struct stat buf; |
| 344 | if (stat(gpgidpath.c_str(), &buf) == 0) { |
| 345 | i.btn->deactivate(); |
| 346 | } |
| 347 | i.btn->callback(init_dialog_cb); |
| 348 | |
| 349 | std::string bothver = windowtit + " (" + std::string(basedof) + ")"; |
| 350 | Fl_Box *versionlabel = new Fl_Box(FL_NO_BOX, 620, 700, 160, 30, bothver.c_str()); |
| 351 | versionlabel->align(FL_ALIGN_RIGHT | FL_ALIGN_INSIDE); |
| 352 | |
| 353 | std::vector<std::string> fpaths; |
| 354 | std::string rdir = Common::getbasedir(false); |
| 355 | scandir(rdir, rdir, fpaths); |
| 356 | updatelist(); |
| 357 | |
| 358 | window->end(); |
| 359 | window->show(argc, argv); |
| 360 | |
| 361 | return Fl::run(); |
| 362 | } |