suwako / SimPas1-Unix アーカイブ

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

master
ブランチ master
SimPas1-Unix / src / addpass.cc
8.8 KB、307行 生 Blame 履歴
1#include "common.hh"
2#include "addpass.hh"
3#include "../main.hh"
4
5#include <gpgme++/context.h>
6#include <gpgme++/data.h>
7#include <gpgme++/encryptionresult.h>
8
9#undef None
10#include <FL/Fl_Window.H>
11#include <FL/fl_ask.H>
12
13#include <fstream>
14
15#include <unistd.h>
16
17Addpass add;
18
19struct InputData {
20 Fl_Input *txtin;
21 Fl_Secret_Input *pass1;
22 Fl_Secret_Input *pass2;
23 Fl_Window *dialog;
24};
25
26bool Addpass::exec(const std::string &file, const std::string &pass, bool isEdit) {
27 std::string lang = Common::getlang();
28
29 std::string basedir = Common::getbasedir(true);
30 std::string ext = ".gpg";
31
32 std::string gpgoutfile = (isEdit ? file : basedir + file + ext);
33
34 if (access(gpgoutfile.c_str(), F_OK) != -1) {
35 std::string err = (lang.compare(0, 2, "en") == 0 ?
36 "Password already exist." :
37 "パスワードが既に存在しています。");
38 fl_alert("%s", err.c_str());
39 return false;
40 }
41
42 try {
43 // GPGMEライブラリを設置
44 std::setlocale(LC_ALL, "");
45 GpgME::initializeLibrary();
46 GpgME::setDefaultLocale(LC_CTYPE, std::setlocale(LC_CTYPE, NULL));
47
48 GpgME::Error err;
49 if (err) {
50 std::string ero = (lang.compare(0, 2, "en") == 0 ?
51 "Failed to generate GPGME" :
52 "GPGエラーの設置に失敗");
53 fl_alert("%s: %s", ero.c_str(), gpg_strerror(err.code()));
54 return false;
55 }
56
57 // GPGMEを創作
58 std::unique_ptr<GpgME::Context> ctx =
59 GpgME::Context::create(GpgME::Protocol::OpenPGP);
60
61 // GPGMEは非対話的モードに設定
62 err = ctx->setPinentryMode(GpgME::Context::PinentryMode::PinentryLoopback);
63 if (err) {
64 std::string ero = (lang.compare(0, 2, "en") == 0 ?
65 "Failed to set pinentry mode" :
66 "pinentryモードを設定に失敗");
67 fl_alert("%s: %s", ero.c_str(), gpg_strerror(err.code()));
68 return false;
69 }
70
71 // パスワードからデータオブジェクトを創作
72 GpgME::Data in(pass.c_str(), strlen(pass.c_str()), false);
73 GpgME::Data out;
74
75 // 鍵を受け取る
76 std::string keypath = basedir + ".gpg-id";
77 std::ifstream keyfile(keypath, std::ios::binary);
78 if (!keyfile.is_open()) {
79 std::string ero = (lang.compare(0, 2, "en") == 0 ?
80 "Failed to open .gpg-id file" :
81 ".gpg-idファイルを開くに失敗");
82 fl_alert("%s", ero.c_str());
83 return false;
84 }
85
86 std::string keyid;
87 std::string line;
88 while (std::getline(keyfile, line)) {
89 line.erase(line.find_last_not_of(" \n\r\t") + 1);
90 if (!line.empty()) keyid = line;
91 }
92
93 keyfile.close();
94
95 if (keyid.empty()) {
96 std::string ero = (lang.compare(0, 2, "en") == 0 ?
97 "The .gpg-id file is empty or invalid" :
98 ".gpg-idファイルは空か無効です");
99 fl_alert("%s", ero.c_str());
100 return false;
101 }
102
103 GpgME::Key key = ctx->key(keyid.c_str(), err);
104 if (key.isNull()) {
105 std::string ero = (lang.compare(0, 2, "en") == 0 ?
106 "Failed to get key" :
107 "鍵を受取に失敗");
108 fl_alert("%s", ero.c_str());
109 return false;
110 }
111
112 // 暗号化
113 std::vector<GpgME::Key> keys = {key};
114
115 GpgME::EncryptionResult res =
116 ctx->encrypt(keys, in, out, GpgME::Context::AlwaysTrust);
117 if (res.error()) {
118 std::string ero = (lang.compare(0, 2, "en") == 0 ?
119 "Failed to encrypt" :
120 "暗号化に失敗");
121 fl_alert("%s: %s", ero.c_str(), res.error().asString());
122 return false;
123 }
124
125 // ディレクトリを創作
126 std::string dirpath = (isEdit ? file : basedir + file);
127 auto lastsla = dirpath.find_last_of('/');
128 if (lastsla != std::string::npos) {
129 dirpath = dirpath.substr(0, lastsla);
130
131 try {
132 Common common;
133 common.mkdir_r(dirpath, 0755);
134 } catch (const std::runtime_error &e) {
135 std::string ero = (lang.compare(0, 2, "en") == 0 ?
136 "Failed to create directory" :
137 "ディレクトリを創作に失敗");
138 fl_alert("%s: %s", ero.c_str(), e.what());
139 return false;
140 }
141 }
142
143 // 暗号化したファイルを開く
144 std::ofstream gpgpath(gpgoutfile, std::ios::binary);
145 if (!gpgpath.is_open()) {
146 std::string ero = (lang.compare(0, 2, "en") == 0 ?
147 "Failed to write file '" + gpgoutfile + "'." :
148 "「" + gpgoutfile + "」ファイルを書き込めません。");
149 fl_alert("%s", ero.c_str());
150 return false;
151 }
152
153 // データが保存したかどうか確認
154 ssize_t encrypted_data_size = out.seek(0, SEEK_END);
155 if (encrypted_data_size <= 0) {
156 std::string ero = (lang.compare(0, 2, "en") == 0 ?
157 "Failed to store the data" :
158 "データを保存に失敗");
159 fl_alert("%s", ero.c_str());
160 return false;
161 }
162
163 // 復号化したパスワードを表示する
164 out.seek(0, SEEK_SET);
165
166 char buffer[512];
167 ssize_t read_bytes;
168 while ((read_bytes = out.read(buffer, sizeof(buffer))) > 0) {
169 gpgpath.write(buffer, read_bytes);
170 }
171
172 gpgpath.close();
173
174 if (gpgpath.fail()) {
175 std::string ero = (lang.compare(0, 2, "en") == 0 ?
176 "Failed to write encrypted data to file" :
177 "暗号化データを書き込めません。");
178 fl_alert("%s", ero.c_str());
179 return false;
180 }
181 } catch (const std::exception &e) {
182 std::string err = (lang.compare(0, 2, "en") == 0 ?
183 "Error" :
184 "エラー");
185 fl_alert("%s: %s", err.c_str(), e.what());
186 return false;
187 }
188
189 if (isEdit) return true;
190
191 std::string msg = (lang.compare(0, 2, "en") == 0 ?
192 "The password got saved." :
193 "パスワードを保存出来ました");
194 fl_alert("%s", msg.c_str());
195
196 return true;
197}
198
199void Addpass::add_cb(Fl_Widget *, void *data) {
200 InputData *inputs = (InputData *)data;
201 std::string lang = Common::getlang();
202
203 if (inputs) {
204 file = inputs->txtin->value();
205 if (file.empty()) {
206 std::string err =
207 (lang.compare(0, 2, "en") == 0 ?
208 "Please fill in the path." :
209 "パスをご入力下さい。");
210 fl_alert("%s", err.c_str());
211 return;
212 }
213 inputpass1 = inputs->pass1->value();
214 if (inputpass1.empty()) {
215 std::string err =
216 (lang.compare(0, 2, "en") == 0 ?
217 "Please fill in the password." :
218 "パスワードをご入力下さい。");
219 fl_alert("%s", err.c_str());
220 return;
221 }
222 inputpass2 = inputs->pass2->value();
223 if (inputpass2.empty()) {
224 std::string err =
225 (lang.compare(0, 2, "en") == 0 ?
226 "Please fill in the password (confirm)." :
227 "パスワード (確認)をご入力下さい。");
228 fl_alert("%s", err.c_str());
229 return;
230 }
231
232 if (inputpass1 != inputpass2) {
233 std::string err =
234 (lang.compare(0, 2, "en") == 0 ?
235 "Password does not match." :
236 "パスワードが一致していません。");
237 fl_alert("%s", err.c_str());
238 return;
239 }
240
241 if (exec(file, inputpass1, false)) {
242 inputs->dialog->hide();
243 }
244 } else {
245 std::string err =
246 (lang.compare(0, 2, "en") == 0 ?
247 "Please fill in all the fields." :
248 "全てのフィールドをご入力下さい。");
249 fl_alert("%s", err.c_str());
250 }
251}
252
253void Addpass::dialog_cb(Fl_Widget *w, void *) {
254 (void)w;
255 std::string lang = Common::getlang();
256 Fl_Window *dialog = new Fl_Window(400, 200,
257 (lang.compare(0, 2, "en") == 0 ? "Add password" : "パスワードの追加"));
258
259 Fl_Input *txtin = new Fl_Input(150, 20, 180, 30,
260 (lang.compare(0, 2, "en") == 0 ? "Path:" : "パス:"));
261 dialog->add(txtin);
262
263 Fl_Secret_Input *pass1 = new Fl_Secret_Input(150, 60, 180, 30,
264 (lang.compare(0, 2, "en") == 0 ? "Password:" : "パスワード:"));
265 Fl_Secret_Input *pass2 = new Fl_Secret_Input(150, 100, 180, 30,
266 (lang.compare(0, 2, "en") == 0 ? "Password (confirm):" : "パスワード (確認):"));
267 dialog->add(pass1);
268 dialog->add(pass2);
269
270 InputData *inputs = new InputData();
271 inputs->txtin = txtin;
272 inputs->pass1 = pass1;
273 inputs->pass2 = pass2;
274 inputs->dialog = dialog;
275
276 Fl_Button *okbtn = new Fl_Button(60, 150, 80, 30, "OK");
277 Fl_Button *cancelbtn = new Fl_Button(160, 150, 80, 30,
278 (lang.compare(0, 2, "en") == 0 ? "Cancel" : "キャンセル"));
279
280 okbtn->callback(static_ok_cb, inputs);
281 cancelbtn->callback(static_cancel_cb, dialog);
282
283 dialog->add(okbtn);
284 dialog->add(cancelbtn);
285
286 dialog->end();
287 dialog->set_modal();
288 dialog->show();
289}
290
291void Addpass::static_ok_cb(Fl_Widget *w, void *data) {
292 (void)w;
293 InputData *inputs = (InputData *)data;
294
295 add.add_cb(nullptr, inputs);
296 std::vector<std::string> fpaths;
297 std::string rdir = Common::getbasedir(false);
298 std::string curpath = rdir + "/" + inputs->txtin->value() + ".gpg";
299 clearpaths(false, curpath);
300 scandir(rdir, rdir, fpaths);
301 updatelist();
302 browse(curpath, true);
303}
304
305void Addpass::static_cancel_cb(Fl_Widget *w, void *data) {
306 ((Addpass *)data)->cancel_cb(w, data);
307}