From 6928a1b3e89e4a8302c8519dc2da0807fbb4d87f Mon Sep 17 00:00:00 2001 From: "aliriza.keskin" Date: Mon, 14 Aug 2023 15:04:13 +0000 Subject: [PATCH] gpg logic improvements --- src/operations/utility/key.vala | 17 +++++++++++++++++ src/util/file.vala | 6 ++++++ src/util/gpg.vala | 25 ++++++++++++++++++++++++- src/ymp.vala | 2 ++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100755 src/operations/utility/key.vala diff --git a/src/operations/utility/key.vala b/src/operations/utility/key.vala new file mode 100755 index 0000000..3146c05 --- /dev/null +++ b/src/operations/utility/key.vala @@ -0,0 +1,17 @@ +public int key_main (string[] args) { + if(get_bool("add")){ + foreach(string arg in args){ + add_gpg_key(arg); + } + } + return 0; +} +void key_init () { + operation op = new operation (); + op.help = new helpmsg (); + op.callback.connect (key_main); + op.names = {_ ("key"), "key"}; + op.help.name = _ ("key"); + op.help.description = _ ("Gpg key operations."); + add_operation (op); +} diff --git a/src/util/file.vala b/src/util/file.vala index f034229..088d6e1 100755 --- a/src/util/file.vala +++ b/src/util/file.vala @@ -215,6 +215,12 @@ public void copy_file (string src, string desc) { File file2 = File.new_for_path (desc); create_dir (sdirname (desc)); int64 sync_bytes = 0; + if("://" in src){ + if(!fetch(src, desc)){ + error_add (_ ("Failed to fetch file: %s => %s").printf (src, desc)); + } + return; + } if (isfile (desc)) { remove_file (desc); } diff --git a/src/util/gpg.vala b/src/util/gpg.vala index 3d4b3e5..e3aa3c4 100755 --- a/src/util/gpg.vala +++ b/src/util/gpg.vala @@ -28,7 +28,30 @@ public bool verify_file (string path) { if (!isfile (path)) { return false; } - return 0 == run ("sh -c \"gpg --verify %s'\" 2>/dev/null".printf (path + ".gpg'")); + string gpgdir = get_storage()+"/gpg/"; + foreach(string file in listdir(gpgdir)) { + if(!endswith(file,".gpg")){ + continue; + } + string[] args = {"gpg","--homedir", gpgdir, "--trust-model", "always", "--no-default-keyring", "--keyring", gpgdir+"%s".printf(file), "--quiet" ,"--verify", path+".gpg"}; + int status = run_args (args); + if(status == 0){ + return true; + } + + } + return false; +} + +public void add_gpg_key(string path){ + if(endswith(path,".asc")){ + string target = get_storage()+"/gpg/"+sbasename(path); + copy_file(path, target); + run_args({"gpg", "--dearmor", target}); + move_file(target+".gpg",get_storage()+"/gpg/"+calculate_md5sum(target+".gpg")+".gpg"); + remove_file(target); + } + } //DOC: `void sign_elf (string path):` diff --git a/src/ymp.vala b/src/ymp.vala index ffa546a..67af6b1 100755 --- a/src/ymp.vala +++ b/src/ymp.vala @@ -228,6 +228,7 @@ private void directories_init () { create_dir (get_storage () + "/metadata/"); create_dir (get_storage () + "/files/"); create_dir (get_storage () + "/links/"); + create_dir (get_storage () + "/gpg/"); create_dir (get_storage () + "/sources.list.d/"); create_dir (get_storage () + "/quarantine/"); if (!isexists (get_storage () + "/sources.list")) { @@ -239,6 +240,7 @@ private void directories_init () { Posix.chown (path, 0, 0); } } + GLib.FileUtils.chmod (get_storage () + "/gpg/", 0700); } private bool ymp_activated = false;