Skip to content

Commit

Permalink
Merge pull request #242 from Allen-Webb/pull_requests
Browse files Browse the repository at this point in the history
Added # line comments to the rule grammar.

Resolves: #111
  • Loading branch information
Daniel Kopeček committed Jul 24, 2018
2 parents 0ab32d7 + 1a0ac3b commit 09be6dc
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Daemon/Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ namespace usbguard
break;

case Rule::Target::Invalid:
case Rule::Target::Empty:
case Rule::Target::Unknown:
case Rule::Target::Match:
case Rule::Target::Device:
Expand Down
3 changes: 3 additions & 0 deletions src/GUI.Qt/DeviceDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void DeviceDialog::setDefaultDecision(usbguard::Rule::Target target)

case usbguard::Rule::Target::Reject:
case usbguard::Rule::Target::Unknown:
case usbguard::Rule::Target::Empty:
case usbguard::Rule::Target::Invalid:
case usbguard::Rule::Target::Match:
case usbguard::Rule::Target::Device:
Expand Down Expand Up @@ -206,6 +207,7 @@ void DeviceDialog::updateDialog()
case usbguard::Rule::Target::Match:
case usbguard::Rule::Target::Device:
case usbguard::Rule::Target::Invalid:
case usbguard::Rule::Target::Empty:
case usbguard::Rule::Target::Unknown:
default:
button = ui->reject_button;
Expand Down Expand Up @@ -237,6 +239,7 @@ void DeviceDialog::executeDefaultDecision()

case usbguard::Rule::Target::Reject:
case usbguard::Rule::Target::Unknown:
case usbguard::Rule::Target::Empty:
case usbguard::Rule::Target::Invalid:
case usbguard::Rule::Target::Device:
case usbguard::Rule::Target::Match:
Expand Down
2 changes: 2 additions & 0 deletions src/GUI.Qt/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ void MainWindow::notifyDevicePolicyChanged(const usbguard::Rule& device_rule, qu
break;

case usbguard::Rule::Target::Invalid:
case usbguard::Rule::Target::Empty:
case usbguard::Rule::Target::Match:
case usbguard::Rule::Target::Device:
case usbguard::Rule::Target::Unknown:
Expand Down Expand Up @@ -665,6 +666,7 @@ void MainWindow::commitDeviceListChanges()

case usbguard::Rule::Target::Match:
case usbguard::Rule::Target::Invalid:
case usbguard::Rule::Target::Empty:
case usbguard::Rule::Target::Unknown:
case usbguard::Rule::Target::Device:
default:
Expand Down
11 changes: 11 additions & 0 deletions src/Library/RuleParser/Actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace usbguard
{
namespace RuleParser
{
struct comment;
struct target;
struct device_id;
struct device_id_value;
Expand All @@ -49,6 +50,16 @@ namespace usbguard
template<typename Rule>
struct rule_parser_actions : tao::pegtl::nothing<Rule> {};

template<>
struct rule_parser_actions<comment> {
static void apply0(Rule& rule)
{
if (rule.getTarget() == Rule::Target::Invalid) {
rule.setTarget(Rule::Target::Empty);
}
}
};

template<>
struct rule_parser_actions<target> {
template<typename Input>
Expand Down
12 changes: 10 additions & 2 deletions src/Library/RuleParser/Grammar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,27 @@ namespace usbguard
struct target
: sor<str_allow, str_block, str_reject, str_match, str_device> {};

/*
* Comment
*/
struct comment
: seq<star<ascii::blank>, if_must<one<'#'>,
star<seq<not_at<eof>, any>>>> {};

/*
* Rule
*/
struct rule
: seq<target,
opt<plus<ascii::blank>, device_id>,
opt<plus<ascii::blank>, list<rule_attributes, plus<ascii::blank>>>> {};
opt<plus<ascii::blank>, list<rule_attributes, plus<ascii::blank>>>,
opt<comment>> {};

/*
* Grammar entry point
*/
struct rule_grammar
: until<eof, must<rule>> {};
: until<eof, must<sor<comment, rule>>> {};
} /* namespace RuleParser */
} /* namespace usbguard */

Expand Down
1 change: 1 addition & 0 deletions src/Library/UEventDeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ namespace usbguard
case Rule::Target::Match:
case Rule::Target::Device:
case Rule::Target::Unknown:
case Rule::Target::Empty:
case Rule::Target::Invalid:
default:
throw std::runtime_error("Unknown rule target in applyDevicePolicy");
Expand Down
1 change: 1 addition & 0 deletions src/Library/UMockdevDeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ namespace usbguard
case Rule::Target::Match:
case Rule::Target::Device:
case Rule::Target::Unknown:
case Rule::Target::Empty:
case Rule::Target::Invalid:
default:
throw std::runtime_error("Unknown rule target in applyDevicePolicy");
Expand Down
1 change: 1 addition & 0 deletions src/Library/public/usbguard/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ namespace usbguard

case Rule::Target::Reject:
case Rule::Target::Unknown:
case Rule::Target::Empty:
case Rule::Target::Invalid:
default:
throw std::runtime_error("Invalid device query target");
Expand Down
6 changes: 4 additions & 2 deletions src/Library/public/usbguard/Rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ namespace usbguard
Rule::operator bool() const
{
return !(getTarget() == Target::Unknown ||
getTarget() == Target::Invalid);
getTarget() == Target::Invalid ||
getTarget() == Target::Empty);
}

std::string Rule::toString(bool invalid) const
Expand Down Expand Up @@ -272,7 +273,8 @@ namespace usbguard
{ "block", Rule::Target::Block },
{ "reject", Rule::Target::Reject },
{ "match", Rule::Target::Match },
{ "device", Rule::Target::Device }
{ "device", Rule::Target::Device },
{ "", Rule::Target::Empty }
};

const std::string Rule::targetToString(const Rule::Target target)
Expand Down
3 changes: 2 additions & 1 deletion src/Library/public/usbguard/Rule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ namespace usbguard
Match = 3, /**< Special target which can be used to trigger actions. The rule wont affect the final decision. */
Unknown = 4, /**< Unknown target. Used for default constructed rules. */
Device = 5, /**< Special target which can only be used for a rule that represents a single device */
Invalid = 6
Empty = 6, /**< Special target to represent the case the parser reaches a comment only line. */
Invalid = 7
};

static const std::string targetToString(Target target);
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Rules/test-rules.bad
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ asdf
123
.:][}{}{(*#*&^!&^%
@(&(*##$
#()($#)($#
()($#)($##
())(!
!@_#)$_#)$!*@
-=-+_!@#
Expand Down
4 changes: 4 additions & 0 deletions src/Tests/Rules/test-rules.good
Original file line number Diff line number Diff line change
Expand Up @@ -733,3 +733,7 @@ reject with-interface equals { 6F:dD:1b 83:dB:96 b4:*:* } parent-hash equals { "
reject with-interface equals { E6:4c:* 4F:8F:24 A8:c1:C8 5e:eB:Fb ED:*:* A1:3d:* fA:*:* } hash "rJjT-`yo" id 26a5:0e6F
reject with-interface none-of { C8:3C:* 4b:*:* 5c:2e:4c 7f:*:* BA:F7:* d3:4D:cb } parent-hash none-of { ":m_H'h`:" } id all-of { ADc6:* 332d:* } via-port equals-ordered { "vHE?ZN;x" "Ga:kiC`N" "PlOjBwj0" } name "GQu<K$g:" if !true
reject with-interface one-of { e5:*:* 2A:*:* AC:c7:* 4B:*:* f2:*:* } if equals-ordered { true false !true false }
#
# this is a comment
reject with-interface one-of { e6:*:* } # comment about this rule
# reject with-interface one-of { e6:*:* }

0 comments on commit 09be6dc

Please sign in to comment.