Skip to content

Commit

Permalink
ui,rules: improved uid parsing
Browse files Browse the repository at this point in the history
When creating rules filtering by UID, there're 3 possible "modes":
 - simple: 0, 1000, etc.
 - user: root (0), opensnitch (1000)
 - regexp: ^(0|1000)$

regexp was not being used correctly.
  • Loading branch information
gustavo-iniguez-goya committed Jul 20, 2023
1 parent c97c322 commit 3d8bdfc
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions ui/opensnitch/dialogs/ruleseditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,34 +850,41 @@ def _save_rule(self):
})

if self.uidCheck.isChecked():
if self.uidCombo.currentText() == "":
uidType = Config.RULE_TYPE_SIMPLE
uid = self.uidCombo.currentText()

if uid == "":
return False, QC.translate("rules", "User ID can not be empty")

try:
# sometimes when loading a rule, instead of the UID, the format
# "user (uid)" is set. So try to parse it, in order not to save
# a wrong uid.
uidtmp = self.uidCombo.currentText().split(" ")
uidtmp = uid.split(" ")
if len(uidtmp) == 1:
int(uidtmp[0])
uid = self.uidCombo.currentText()
else:
uid = str(pwd.getpwnam(uidtmp[0])[self.PW_UID])
except:
return False, QC.translate("rules", "Invalid UID, it must be a digit.")
# if it's not a digit and nor a system user (user (id)), see if
# it's a regexp.
if self._is_regex(self.uidCombo.currentText()):
uidType = Config.RULE_TYPE_REGEXP
if self._is_valid_regex(self.uidCombo.currentText()) == False:
return False, QC.translate("rules", "User ID regexp error")

else:
return False, QC.translate("rules", "Invalid UID, it must be a digit.")

self.rule.operator.operand = Config.OPERAND_USER_ID
self.rule.operator.data = self.uidCombo.currentText()
rule_data.append(
{
'type': Config.RULE_TYPE_SIMPLE,
'type': uidType,
'operand': Config.OPERAND_USER_ID,
'data': uid,
"sensitive": self.sensitiveCheck.isChecked()
})
if self._is_regex(self.uidCombo.currentText()):
rule_data[len(rule_data)-1]['type'] = Config.RULE_TYPE_REGEXP
if self._is_valid_regex(self.uidCombo.currentText()) == False:
return False, QC.translate("rules", "User ID regexp error")

if self.pidCheck.isChecked():
if self.pidLine.text() == "":
Expand Down

0 comments on commit 3d8bdfc

Please sign in to comment.