-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpolkitex.py
executable file
·167 lines (142 loc) · 6.55 KB
/
polkitex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#! /usr/bin/env python
# Polkit Explorer
# View/Explore all entries within a Linux Polkit XML file
#
# Original release date : 20130324 (yyyymmdd)
#
# Copyright (C) 2013-2024, Kevin Cave <[email protected]>
#
# ISC License (ISC)
#
# Permission to use, copy, modify, and/or distribute this software for any purpose with
# or without fee is hereby granted, provided that the above copyright notice and this
# permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
from PySide6 import QtCore, QtWidgets
from Ui_polkitex import Ui_PolkitExplorer
from Ui_About import Ui_About
from Ui_Glossary import Ui_Glossary
from lxml import etree
from importlib import reload
import sys
reload(sys)
VERSION = 1.1
class PolkitExplorer(QtWidgets.QMainWindow, Ui_PolkitExplorer):
def __init__(self, parent=None):
super(PolkitExplorer, self).__init__(parent)
self.setupUi(self)
@QtCore.Slot()
# User wants to open a file...
def fileOpen(self):
self.open_action_file_dialog()
# User wants to quit...
def fileQuit(self):
app.quit()
# Event fires when comboBox changes (scrollwheel or click'n'select)
def actionComboBoxChanged(self):
self.actionID = str(self.actionComboBox.currentText())
self.parseAction(self.actionID)
def fileAbout(self):
self.aboutDialog = aboutPolkitExplorer()
self.aboutDialog.versionlabel.setText("Version : " + str(VERSION))
self.aboutDialog.exec()
def helpGlossary(self):
self.glossaryDialog = glossaryPolkitExplorer()
self.glossaryDialog.exec()
# Display file open dialog at the correct dir and with a filename filter for the policy files
# then fill the Actions Combobox
def open_action_file_dialog(self):
fname, _filter = QtWidgets.QFileDialog.getOpenFileName(self, 'Open Polkit file...', '/usr/share/polkit-1/actions/', '*.policy')
if fname != "":
self.parsePolKitFile(str(fname))
# fill Actions ComboBox with list of all actions for specified Policy File.
def parsePolKitFile(self, fname):
# Display the filename of the policy kit file
self.policyKitFileName.setText(fname)
#Read the selected file and create the lxml tree object...
parser = etree.XMLParser(encoding='utf-8')
self.tree = etree.parse(fname, parser)
self.root = self.tree.getroot()
self.actionComboBox.clear()
self.actionsCount = 0
# Fill the Actions combo box list with all actions from the loaded polkit file...
for actionslist in self.root.iter('action'):
actname = actionslist.get('id')
self.actionComboBox.addItem(str(actname))
self.actionsCount = self.actionsCount + 1
self.actionsCounterDisplay.display(self.actionsCount)
def parseAction(self, actionID):
description = None
for actionslist in self.root.iter('action'):
policy = actionslist.get('id')
if policy == actionID:
# Get the default permissions for the selected Action...
for action in self.root.xpath('.//action[@id = $policy]', policy=actionID):
self.anySet = 0
self.inSet = 0
self.actSet = 0
for defaults in action.findall("./defaults/"):
self.fillDefaultsTable(defaults.tag, defaults.text)
# Get the desired Description of the Action...
# Note: It's a complete PITA... some .policy XML files just have Descriptions with
# no xml:lang attributes, and some have, and one file has a Description element
# tag of "_description"! Yep, a complete PITA, all right...
for d in self.root.xpath('.//action[@id = $policy]/description[@xml:lang = $lang]', policy=policy, lang="en_GB"):
description = d.text
if description is not None:
self.polkitActionDescription.setText(description)
else:
# Okay so we didn't find a Description with an xml:lang attribute, so try to find one
# which has a "description" tag with no attributes...
for d in self.root.xpath('.//action[@id = $policy]/*[local-name()="description" or local-name()="_description"]', policy=actionID):
if d.attrib == "":
description = d.text
if description is not None:
# Found one!
self.polkitActionDescription.setText(description)
else:
# Booooo!
self.polkitActionDescription.setText("No description found - how helpfull!")
def fillDefaultsTable(self, defaultTag, defaultText):
if defaultTag == "allow_any":
self.currentAllowAnyLabel.setText(defaultText)
self.anySet = 1
elif defaultTag == "allow_inactive":
self.currentAllowInactiveLabel.setText(defaultText)
self.inSet = 1
elif defaultTag == "allow_active":
self.currentAllowActiveLabel.setText(defaultText)
self.actSet = 1
if self.anySet == 0:
self.currentAllowAnyLabel.setText("")
if self.inSet == 0:
self.currentAllowInactiveLabel.setText("")
if self.actSet == 0:
self.currentAllowActiveLabel.setText("")
class aboutPolkitExplorer(QtWidgets.QDialog, Ui_About):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self, parent)
self.aboutDialog = Ui_About()
self.setupUi(self)
def aboutClose(self):
self.close()
class glossaryPolkitExplorer(QtWidgets.QDialog, Ui_Glossary):
def __init__(self, parent=None):
QtWidgets.QDialog.__init__(self,parent)
self.glossaryDialog = Ui_Glossary()
self.setupUi(self)
def closeGlossary(self):
self.close()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = PolkitExplorer()
window.show()
sys.exit(app.exec())