| 1 | #include "common.hh" |
| 2 | #include "showpass.hh" |
| 3 | #include "../main.hh" |
| 4 | #include "otppass.hh" |
| 5 | |
| 6 | #include <gpgme++/decryptionresult.h> |
| 7 | |
| 8 | #undef Status |
| 9 | #undef None |
| 10 | #include <FL/Fl.H> |
| 11 | #include <FL/fl_ask.H> |
| 12 | #include <FL/Fl_Text_Buffer.H> |
| 13 | #include <FL/Fl_Text_Display.H> |
| 14 | |
| 15 | void Showpass::otpupdate_cb(void *o) { |
| 16 | Showpass *show = static_cast<Showpass *>(o); |
| 17 | Otppass otp; |
| 18 | |
| 19 | if (!show->otpSav.empty()) { |
| 20 | std::string dec = otp.exec(show->otpSav); |
| 21 | |
| 22 | realpass = dec; |
| 23 | if (!isPassHidden) { |
| 24 | textbuf->text(dec.c_str()); |
| 25 | textview->redraw(); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | Fl::repeat_timeout(1.0, otpupdate_cb, o); |
| 30 | } |
| 31 | |
| 32 | std::string Showpass::exec(const char *file, bool stfu) { |
| 33 | std::string lang = Common::getlang(); |
| 34 | |
| 35 | try { |
| 36 | std::setlocale(LC_ALL, ""); |
| 37 | GpgME::initializeLibrary(); |
| 38 | |
| 39 | gpg_error_t err = gpg_err_init(); |
| 40 | if (err) { |
| 41 | if (!stfu) { |
| 42 | std::string ero = (lang.compare(0, 2, "en") == 0 ? |
| 43 | "Failed to generate GPGME" : |
| 44 | "GPGエラーの設置に失敗"); |
| 45 | fl_alert("%s: %s", ero.c_str(), gpg_strerror(err)); |
| 46 | } |
| 47 | return ""; |
| 48 | } |
| 49 | |
| 50 | std::unique_ptr<GpgME::Context> ctx = |
| 51 | GpgME::Context::create(GpgME::Protocol::OpenPGP); |
| 52 | if (!ctx) { |
| 53 | if (!stfu) { |
| 54 | std::string ero = (lang.compare(0, 2, "en") == 0 ? |
| 55 | "Failed to create GPGME context" : |
| 56 | "GPGMEコンテキストの作成に失敗"); |
| 57 | fl_alert("%s", ero.c_str()); |
| 58 | } |
| 59 | return ""; |
| 60 | } |
| 61 | |
| 62 | if (ctx->protocol() != GpgME::Protocol::OpenPGP) { |
| 63 | if (!stfu) { |
| 64 | std::string ero = (lang.compare(0, 2, "en") == 0 ? |
| 65 | "Failed to set OpenPGP protocol" : |
| 66 | "OpenPGPプロトコールの設置に失敗"); |
| 67 | fl_alert("%s", ero.c_str()); |
| 68 | } |
| 69 | return ""; |
| 70 | } |
| 71 | |
| 72 | std::ifstream gpgfile(file, std::ios::binary); |
| 73 | if (!gpgfile.is_open()) { |
| 74 | if (!stfu) { |
| 75 | std::string ero = (lang.compare(0, 2, "en") == 0 ? |
| 76 | "Unable to open the specified file" : |
| 77 | "指定されたファイルを開けません"); |
| 78 | fl_alert("%s", ero.c_str()); |
| 79 | } |
| 80 | return ""; |
| 81 | } |
| 82 | |
| 83 | GpgME::Data in(file); |
| 84 | GpgME::Data out; |
| 85 | |
| 86 | GpgME::DecryptionResult res = ctx->decrypt(in, out); |
| 87 | if (res.error()) { |
| 88 | if (!stfu) { |
| 89 | std::string ero = (lang.compare(0, 2, "en") == 0 ? |
| 90 | "Failed to decrypt" : |
| 91 | "復号化に失敗"); |
| 92 | fl_alert("%s: %s", ero.c_str(), res.error().asString()); |
| 93 | } |
| 94 | return ""; |
| 95 | } |
| 96 | |
| 97 | std::string dec; |
| 98 | char buf[512]; |
| 99 | ssize_t read_bytes; |
| 100 | |
| 101 | out.seek(0, SEEK_SET); |
| 102 | |
| 103 | while ((read_bytes = out.read(buf, sizeof(buf))) > 0) { |
| 104 | dec.append(buf, read_bytes); |
| 105 | } |
| 106 | |
| 107 | if (dec.rfind("otpauth://", 0) == 0 && !stfu) { |
| 108 | Otppass o; |
| 109 | otpSav = dec; |
| 110 | |
| 111 | Fl::remove_timeout(otpupdate_cb, this); |
| 112 | Fl::add_timeout(1.0, otpupdate_cb, this); |
| 113 | |
| 114 | return o.exec(dec); |
| 115 | } |
| 116 | |
| 117 | Fl::remove_timeout(otpupdate_cb, this); |
| 118 | otpSav = ""; |
| 119 | return dec; |
| 120 | } catch (const std::exception &e) { |
| 121 | if (!stfu) { |
| 122 | std::string err = (lang.compare(0, 2, "en") == 0 ? |
| 123 | "Error" : |
| 124 | "エラー"); |
| 125 | fl_alert("%s: %s", err.c_str(), e.what()); |
| 126 | } |
| 127 | return ""; |
| 128 | } |
| 129 | } |