Skip to content

Commit

Permalink
Fix only Regex or only Not bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobmaker55 committed Aug 16, 2023
1 parent b590ba0 commit 9585c25
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
29 changes: 14 additions & 15 deletions checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type cond struct {
Key string
Value string
After string
Regex bool
regex bool
}

// requireArgs is a convenience function that prints a warning if any required
Expand Down Expand Up @@ -106,22 +106,21 @@ func runCheck(cond cond) bool {
regex := "Regex"
condFunc := ""
negation := false
cond.Regex = false
cond.regex = false

// Ensure that condition type is a valid length
if len(cond.Type) <= len(regex) {
fail(`Condition type "` + cond.Type + `" is not long enough to be valid. Do you have a "type = 'CheckTypeHere'" for all check conditions?`)
return false
}

condFunc = cond.Type
if cond.Type[len(cond.Type)-len(not):len(cond.Type)] == not {
negation = true
condFunc = cond.Type[:len(cond.Type)-len(not)]
} else if cond.Type[len(cond.Type)-len(regex):len(cond.Type)] == regex {
cond.Regex = true
}
if cond.Type[len(cond.Type)-len(regex):len(cond.Type)] == regex {
cond.regex = true
condFunc = cond.Type[:len(cond.Type)-len(regex)]
} else {
condFunc = cond.Type
}

// Catch panic if check type doesn't exist
Expand Down Expand Up @@ -151,11 +150,11 @@ func runCheck(cond cond) bool {
func (c cond) CommandContains() (bool, error) {
c.requireArgs("Cmd", "Value")
out, err := shellCommandOutput(c.Cmd)
if c.Regex {
if err != nil {
return false, err
}
if c.regex {
outTrim := strings.TrimSpace(out)
if err != nil {
return false, err
}
return regexp.Match(c.Value, []byte(outTrim))
}
return strings.Contains(strings.TrimSpace(out), c.Value), err
Expand Down Expand Up @@ -222,14 +221,14 @@ func (c cond) FileContains() (bool, error) {
}
found := false
for _, line := range strings.Split(fileContent, "\n") {
if c.Regex {
if c.regex {
found, err = regexp.Match(c.Value, []byte(line))
if err != nil {
return false, err
}
} else {
found = strings.Contains(line, c.Value)
}
if err != nil {
return false, err
}
if found {
break
}
Expand Down
14 changes: 7 additions & 7 deletions checks_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (c cond) AutoCheckUpdatesEnabled() (bool, error) {
result, err := cond{
Path: "/etc/apt/apt.conf.d/",
Value: `(?i)^\s*APT::Periodic::Update-Package-Lists\s+"1"\s*;\s*$`,
Regex: true,
regex: true,
}.DirContains()
// If /etc/apt/ does not exist, try dnf (RHEL)
if err != nil {
Expand All @@ -27,7 +27,7 @@ func (c cond) AutoCheckUpdatesEnabled() (bool, error) {
applyUpdates, err := cond{
Path: "/etc/dnf/automatic.conf",
Value: `(?i)^\s*apply_updates\s*=\s*(1|on|yes|true)`,
Regex: true,
regex: true,
}.FileContains()
if err != nil {
return false, err
Expand Down Expand Up @@ -82,7 +82,7 @@ func (c cond) FirewallUp() (bool, error) {
result, err := cond{
Path: "/etc/ufw/ufw.conf",
Value: `^\s*ENABLED=yes\s*$`,
Regex: true,
regex: true,
}.FileContains()
if err != nil {
// If ufw.conf does not exist, check firewalld status (RHEL)
Expand All @@ -98,13 +98,13 @@ func (c cond) GuestDisabledLDM() (bool, error) {
result, err := cond{
Path: "/usr/share/lightdm/lightdm.conf.d/",
Value: guestStr,
Regex: true,
regex: true,
}.DirContains()
if !result {
return cond{
Path: "/etc/lightdm/",
Value: guestStr,
Regex: true,
regex: true,
}.DirContains()
}
return result, err
Expand Down Expand Up @@ -249,7 +249,7 @@ func (c cond) UserExists() (bool, error) {
return cond{
Path: "/etc/passwd",
Value: "^" + c.User + ":",
Regex: true,
regex: true,
}.FileContains()
}

Expand All @@ -258,6 +258,6 @@ func (c cond) UserInGroup() (bool, error) {
return cond{
Path: "/etc/group",
Value: c.Group + `[0-9a-zA-Z,:\s+]+` + c.User,
Regex: true,
regex: true,
}.FileContains()
}
3 changes: 2 additions & 1 deletion docs/regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ to score something simple, but we think it significantly increases the overall q
to each line of the input file, so currently, no multi-line regexes are currently possible.

The checks that are specifically supported are `CommandContainsRegex`, `DirContainsRegex`, and `FileContainsRegex`.
Please note that you **must** append `Regex` to the end for the check to use regular expressions.
Please note that you **must** add `Regex` for the check to use regular expressions. You can also still append `Not` to
the end to invert the condition, such as `CommandContainsRegexNot`

> We're using the Golang Regular Expression package ([documentation here](https://godocs.io/regexp)). It uses RE2
> syntax, which is also generally the same as Perl, Python, and other languages.
Expand Down
2 changes: 1 addition & 1 deletion utility_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func checkTrace() {
result, err := cond{
Path: "/proc/self/status",
Value: `^TracerPid:\s+0$`,
Regex: true,
regex: true,
}.FileContains()

// If there was an error reading the file, the user may be restricting access to /proc for the phocus binary
Expand Down

0 comments on commit 9585c25

Please sign in to comment.