Allow woeusbgui to modify policy so there is bigger chance of script working regardless of what happens.

Squashed commit of the following:

commit 961fd8a87c6d930aa0a72e86609f2096cf4cd00b
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Fri Jul 24 13:36:19 2020 +0200

    Forgot to revert small change

commit d1e54b4a19a5c59d912ba5bb4fd325093b86c2ee
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Fri Jul 24 13:27:55 2020 +0200

    Remove woeusbgui from installed scripts

commit 384dcb6c5a91d2a0fdc427c01474907444af4866
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Fri Jul 24 13:16:24 2020 +0200

    Remove shortcut, fix wrong path in policy

commit d643436d285e52804a9a49c67dc4961b36f67c7b
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Fri Jul 24 13:08:10 2020 +0200

    Remove dead code from `woeusbgui`

commit 66baa86c30bf3b5479aa78ea49f6d56aabeb81bf
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Fri Jul 24 13:05:09 2020 +0200

    Revert most changes

commit 047a104c19b39e0c6b549eafb65abf67fd1f1a77
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Fri Jul 24 11:37:18 2020 +0200

    Improve acquiring of path, attempt 5

commit ccc67bbe29301552be922a870210f6d40b4d23be
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Fri Jul 24 11:36:08 2020 +0200

    Update policy

commit 42c4870e7c691aa692aeee4e20425376131bf045
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Fri Jul 24 11:16:42 2020 +0200

    Improve acquiring of path, attempt 4

commit f3f934226d9f23c4f7f46134162fb282c9832636
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Thu Jul 23 23:38:03 2020 +0200

    Improve acquiring of path, attempt 3

commit 316022688ba10640748ac162a073abbc0b24fe27
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Thu Jul 23 23:24:07 2020 +0200

    Improve acquiring of path, attempt 2

commit ca39ccdc9c461fd5ef4a944ce83a97a4c5c06c59
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Thu Jul 23 23:02:53 2020 +0200

    Improve acquiring of path

commit cd9801be61196ad0e784d4f3110b0be091c5ac9c
Merge: 9c7f733 35be61b
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Thu Jul 23 19:34:42 2020 +0200

    Merge remote-tracking branch 'WoeUSB-ng-org/master' into development

commit 35be61b4f3e344a9350e8edc991987c24fd75868
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Thu Jul 23 19:26:59 2020 +0200

    Add missing import

commit 65336c3867b297338d8c5f5bdc8dd8c3cc717481
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Thu Jul 23 19:17:25 2020 +0200

    Improve acquiring of path

commit 8d34a6a21d49ef3c68692a3e1e8a0cb30acba5d5
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Thu Jul 23 17:29:52 2020 +0200

    update .gitignore

commit dcd915a95fb309085e36f5ce8e60e557fc690491
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Thu Jul 23 17:29:07 2020 +0200

    Put task of installing scripts back to pip

commit 7c235cc2f735ec0f7ba514ef03af681534bf7384
Author: Jakub Szymański <jakubmateusz@poczta.onet.pl>
Date:   Thu Jul 23 16:55:19 2020 +0200

    WoeUSB-ng can now update polkit policy

    This way this program can be ran from anywhere in system
This commit is contained in:
Jakub Szymański 2020-07-24 13:36:35 +02:00
parent 9c7f733ce4
commit d1bef63bbe
6 changed files with 87 additions and 58 deletions

2
.gitignore vendored
View File

@ -8,3 +8,5 @@ WoeUSB_ng\.egg-info/
dist/
build/
create_tarball

View File

@ -3,9 +3,11 @@
import os
import re
import sys
import stat
import shutil
import pathlib
import subprocess
from xml.dom.minidom import parseString
#: Disable message coloring when set to True, set by --no-color
no_color = False
@ -215,7 +217,8 @@ def check_uefi_ntfs_support_partition(target_device):
if re.findall("UEFI_NTFS", lsblk) != []:
print_with_color(
"Warning: Your device doesn't seems to have an UEFI:NTFS partition, UEFI booting will fail if the motherboard firmware itself doesn't support NTFS filesystem!")
"Warning: Your device doesn't seems to have an UEFI:NTFS partition, "
"UEFI booting will fail if the motherboard firmware itself doesn't support NTFS filesystem!")
print_with_color(
"Info: You may recreate disk with an UEFI:NTFS partition by using the --device creation method")
@ -251,8 +254,9 @@ def check_target_filesystem_free_space(target_fs_mountpoint, source_fs_mountpoin
if needed_space > free_space:
print_with_color("Error: Not enough free space on target partition!")
print_with_color(
"Error: We required " + get_size(
str(needed_space)) + "(" + str(needed_space) + " bytes) but '" + target_partition + "' only has " + str(free_space) + "(" + str(free_space) + " bytes).")
"Error: We required " + str(get_size(
str(needed_space))) + "(" + str(needed_space) + " bytes) but '" + target_partition + "' only has " + str(
free_space) + "(" + str(free_space) + " bytes).")
return 1
@ -294,14 +298,6 @@ def get_size(path):
return total_size
# Ok, you may asking yourself, what the f**k is this, and why is it called everywhere. Let me explain
# In python you can't just stop or kill thread, it must end its execution,
# or recognize moment where you want it to stop and politely perform euthanasia on itself.
# So, here, if gui is set, we throw exception which is going to be (hopefully) catch by GUI,
# simultaneously ending whatever script was doing meantime!
# Everyone goes to home happy and user is left with wrecked pendrive (just joking, next thing called by gui is cleanup)
def check_kill_signal():
"""
Ok, you may asking yourself, what the f**k is this, and why is it called everywhere. Let me explain
@ -314,3 +310,64 @@ def check_kill_signal():
if gui is not None:
if gui.kill:
raise sys.exit()
# noinspection DuplicatedCode
def update_policy_to_allow_for_running_gui_as_root(path):
dom = parseString(
"<?xml version=\"1.0\" ?>"
"<!DOCTYPE policyconfig PUBLIC '-//freedesktop//DTD polkit Policy Configuration 1.0//EN' "
"'http://www.freedesktop.org/software/polkit/policyconfig-1.dtd'><!-- \n"
"DOC: https://www.freedesktop.org/software/polkit/docs/latest/polkit.8.html\n"
"--><policyconfig>\n"
" <vendor>The WoeUSB Project</vendor>\n"
" <vendor_url>https://github.com/slacka/WoeUSB</vendor_url>\n"
" <icon_name>woeusbgui-icon</icon_name>\n"
"\n"
" <action id=\"com.github.slacka.woeusb.run-gui-using-pkexec\">\n"
" <description>Run `woeusb` as SuperUser</description>\n"
" <description xml:lang=\"zh_TW\">以超級使用者(SuperUser)身份執行 `woeusb`</description>\n"
" <description xml:lang=\"pl_PL\">Uruchom `woeusb` jako root</description>\n"
" \n"
" <message>Authentication is required to run `woeusb` as SuperUser.</message>\n"
" <message xml:lang=\"zh_TW\">以超級使用者(SuperUser)身份執行 `woeusb` 需要通過身份驗證。</message>\n"
" <message xml:lang=\"pl_PL\">Wymagana jest autoryzacja do uruchomienia `woeusb` jako root</message>\n"
" \n"
" <defaults>\n"
" <allow_any>auth_admin</allow_any>\n"
" <allow_inactive>auth_admin</allow_inactive>\n"
" <allow_active>auth_admin_keep</allow_active>\n"
" </defaults>\n"
" \n"
" <annotate key=\"org.freedesktop.policykit.exec.path\">/usr/local/bin/woeusbgui</annotate>\n"
" <annotate key=\"org.freedesktop.policykit.exec.allow_gui\">true</annotate>\n"
" </action>\n"
" <action id=\"com.github.slacka.woeusb.run-gui-using-pkexec-local\">\n"
" <description>Run `woeusb` as SuperUser</description>\n"
" <description xml:lang=\"zh_TW\">以超級使用者(SuperUser)身份執行 `woeusb`</description>\n"
" <description xml:lang=\"pl_PL\">Uruchom `woeusb` jako root</description>\n"
"\n"
" <message>Authentication is required to run `woeusb` as SuperUser.</message>\n"
" <message xml:lang=\"zh_TW\">以超級使用者(SuperUser)身份執行 `woeusb` 需要通過身份驗證。</message>\n"
" <message xml:lang=\"pl_PL\">Wymagana jest autoryzacja do uruchomienia `woeusb` jako root</message>\n"
"\n"
" <defaults>\n"
" <allow_any>auth_admin</allow_any>\n"
" <allow_inactive>auth_admin</allow_inactive>\n"
" <allow_active>auth_admin_keep</allow_active>\n"
" </defaults>\n"
"\n"
" <annotate key=\"org.freedesktop.policykit.exec.path\">/usr/local/bin/woeusbgui</annotate>\n"
" <annotate key=\"org.freedesktop.policykit.exec.allow_gui\">true</annotate>\n"
" </action>\n"
"</policyconfig>"
)
for action in dom.getElementsByTagName('action'):
if action.getAttribute('id') == "com.github.slacka.woeusb.run-gui-using-pkexec":
for annotate in action.getElementsByTagName('annotate'):
if annotate.getAttribute('key') == "org.freedesktop.policykit.exec.path":
annotate.childNodes[0].nodeValue = path
with open("/usr/share/polkit-1/actions/com.github.woeusb.woeusb-ng.policy", "w") as file:
file.write(dom.toxml())

View File

@ -23,12 +23,18 @@ along with WoeUSB-ng If not, see <http://www.gnu.org/licenses/>.
+"""
import subprocess
import sys
import os
import WoeUSB.gui
if os.getuid() != 0:
subprocess.run(["pkexec", os.path.realpath(__file__)])
exit(0)
else:
try:
import WoeUSB.gui
WoeUSB.gui.run()
except SystemExit:
import WoeUSB.utils
WoeUSB.utils.update_policy_to_allow_for_running_gui_as_root(__file__)
print("Policy was updated, please run this program again")
WoeUSB.gui.run()

View File

@ -1,7 +0,0 @@
#!/usr/bin/env xdg-open
[Desktop Entry]
Name=WoeUSB-ng
Exec=/usr/bin/woeusbgui
Icon=/usr/share/icons/WoeUSB-ng/icon.ico
Terminal=false
Type=Application

View File

@ -20,24 +20,6 @@ DOC: https://www.freedesktop.org/software/polkit/docs/latest/polkit.8.html
<allow_active>auth_admin_keep</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/bin/woeusbgui</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</action>
<action id="com.github.slacka.woeusb.run-gui-using-pkexec-local">
<description>Run `woeusb` as SuperUser</description>
<description xml:lang="zh_TW">以超級使用者(SuperUser)身份執行 `woeusb`</description>
<description xml:lang="pl_PL">Uruchom `woeusb` jako root</description>
<message>Authentication is required to run `woeusb` as SuperUser.</message>
<message xml:lang="zh_TW">以超級使用者(SuperUser)身份執行 `woeusb` 需要通過身份驗證。</message>
<message xml:lang="pl_PL">Wymagana jest autoryzacja do uruchomienia `woeusb` jako root</message>
<defaults>
<allow_any>auth_admin</allow_any>
<allow_inactive>auth_admin</allow_inactive>
<allow_active>auth_admin_keep</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/usr/local/bin/woeusbgui</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</action>

View File

@ -1,11 +1,10 @@
import os
import shutil
import stat
from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install
from xml.dom.minidom import parse
import shutil
import stat
import os
this_directory = os.path.abspath(os.path.dirname(__file__))
@ -17,16 +16,6 @@ def post_install():
path = '/usr/local/bin/woeusbgui' # I give up, I have no clue how to get bin path that is used by pip
shutil.copy2(this_directory + '/WoeUSB/woeusbgui', path) # I'll just hard code it until someone finds better way
dom = parse(this_directory + '/miscellaneous/com.github.woeusb.woeusb-ng.policy')
for action in dom.getElementsByTagName('action'):
if action.getAttribute('id') == "com.github.slacka.woeusb.run-gui-using-pkexec":
for annotate in action.getElementsByTagName('annotate'):
if annotate.getAttribute('key') == "org.freedesktop.policykit.exec.path":
annotate.childNodes[0].nodeValue = path
with open(this_directory + '/miscellaneous/com.github.woeusb.woeusb-ng.policy', "w") as file:
dom.writexml(file)
shutil.copy2(this_directory + '/miscellaneous/com.github.woeusb.woeusb-ng.policy', "/usr/share/polkit-1/actions")
try:
@ -36,7 +25,7 @@ def post_install():
shutil.copy2(this_directory + '/WoeUSB/data/icon.ico', '/usr/share/icons/WoeUSB-ng/icon.ico')
with open(this_directory + '/miscellaneous/WoeUSB-ng.desktop', "w") as file:
with open("/usr/share/applications/WoeUSB-ng.desktop", "w") as file:
file.write(
"""#!/usr/bin/env xdg-open
[Desktop Entry]
@ -48,8 +37,8 @@ def post_install():
"""
)
shutil.copy2(this_directory + '/miscellaneous/WoeUSB-ng.desktop', '/usr/share/applications')
os.chmod('/usr/share/applications/WoeUSB-ng.desktop', stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) # 644
os.chmod('/usr/share/applications/WoeUSB-ng.desktop',
stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IEXEC) # 755
class PostDevelopCommand(develop):