How to Execute Python Script Using pkexec

1k views Asked by At

For a project I am writing a PyQt5 widget for ifconfig on Linux. Some parts of my code require admin privilege. For example, shutting down an interface.

This is a GUI application and users must enter their sudo password using a GUI. I have searched online and found pkexec that allows an authorized user to execute program as another user. So, I wrote the .policy action file required. I have never used polkit before, I might have missed something.

I can execute my program with no errors using python3 ifconfig_Logic.py

but if I try executing my program using pkexec python3 ~/Desktop/GUI/ifconfig_Logic.py The message on the authentication window is enter image description here

Shouldn't the message be Authentication is required to run the ifconfigLogic instead of Authentication is required to install this project?

Also, I get the following error

Traceback (most recent call last):
  File "/home/grandino/Desktop/GUI/ifconfig_Logic.py", line 9, in <module>
    baseUIClass, baseUIWidget = uic.loadUiType("ifconfig.ui")
  File "/usr/lib/python3/dist-packages/PyQt5/uic/__init__.py", line 199, in loadUiType
    winfo = compiler.UICompiler().compileUi(uifile, code_string, from_imports,
  File "/usr/lib/python3/dist-packages/PyQt5/uic/Compiler/compiler.py", line 111, in compileUi
    w = self.parse(input_stream, resource_suffix)
  File "/usr/lib/python3/dist-packages/PyQt5/uic/uiparser.py", line 1020, in parse
    document = parse(filename)
  File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 1202, in parse
    tree.parse(source, parser)
  File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 584, in parse
    source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'ifconfig.ui'

ifconfig.ui is the ui file that ifconfig_Logic.py uses to import the interface. I thought this issue is beacase ifconfig.ui is not added in the policy so I added it.

Here is ifconfig_Logic.py

from PyQt5 import QtCore, QtGui, QtWidgets, QtWidgets
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QProcess, Qt
from PyQt5 import uic
from PyQt5 import QtGui


import sys

baseUIClass, baseUIWidget = uic.loadUiType("ifconfig.ui")

class ifconfig_Logic(baseUIWidget,baseUIClass):
    process = QProcess()
    send_output = pyqtSignal(str)


    def __init__(self, parent = None):
        super(ifconfig_Logic,self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint)






if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ui = ifconfig_Logic(None)

    ui.show()

    sys.exit(app.exec_())

Policy file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>



  <action id="com.ifconfigLogic">
    <description>Run ifconfigLogic</description>
   
    <message>Authentication is required to run the ifconfigLogic</message>
   
    <defaults>
      <allow_any>auth_admin_keep</allow_any>
      <allow_inactive>auth_admin_keep</allow_inactive>
      <allow_active>auth_admin_keep</allow_active>
    </defaults>
    
     <annotate key="org.freedesktop.policykit.exec.path">/home/grandino/Desktop/GUI/ifconfig</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
  </action>

</policyconfig>

How can I fix this issue? Why do files seem missing when I use pkexec?

1

There are 1 answers

0
eyllanesc On

It is recommended that you build the absolute paths instead of using relative paths.

import os.path

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
ui_filename = os.path.join(CURRENT_DIR, "ifconfig.ui")

baseUIClass, baseUIWidget = uic.loadUiType(ui_filename)