Skip to content

Commit

Permalink
Extra functionality for PermissionIs (#124)
Browse files Browse the repository at this point in the history
* more functionality added for PermissionIs check

* documentation
  • Loading branch information
Akshay-Rohatgi authored Jul 24, 2021
1 parent 5a74d8f commit b701e3f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
47 changes: 40 additions & 7 deletions cmd/checks_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,35 @@ func processCheck(check *check, checkType, arg1, arg2, arg3 string) bool {
return err == nil && !result
case "PermissionIs":
if check.Message == "" {
check.Message = "The permissions of " + arg1 + " are " + arg2

if arg2 == "octal" {
check.Message = "The octal permissions of " + arg1 + " are " + arg3
} else if arg2 == "WorldWritable" {
check.Message = arg1 + " is world writable"
} else if arg2 == "WorldReadable" {
check.Message = arg1 + " is world readable"
} else {
check.Message = "Permissions of " + arg1 + " are " + arg3
}

}
result, err := permissionIs(arg1, arg2)
result, err := permissionIs(arg1, arg2, arg3)
return err == nil && result
case "PermissionIsNot":
if check.Message == "" {
check.Message = "The permissions of " + arg1 + " are not " + arg2

if arg2 == "octal" {
check.Message = "The octal permissions of " + arg1 + " are not " + arg3
} else if arg2 == "WorldWritable" {
check.Message = arg1 + " is not world writable"
} else if arg2 == "WorldReadable" {
check.Message = arg1 + " is not world readable"
} else {
check.Message = "Permissions of " + arg1 + " are not " + arg3
}

}
result, err := permissionIs(arg1, arg2)
result, err := permissionIs(arg1, arg2, arg3)
return err == nil && !result
default:
failPrint("No check type " + checkType)
Expand Down Expand Up @@ -149,7 +169,20 @@ func autoCheckUpdatesEnabled() (bool, error) {
return fileContainsRegex("/etc/apt/apt.conf.d/20auto-upgrades", `APT::Periodic::Update-Package-Lists( |)"1";`)
}

func permissionIs(filePath, permissionToCheck string) (bool, error) {
perm, err := commandOutput(`stat -c '%a' ` + filePath)
return perm == permissionToCheck, err
// func permissionIs(filePath, permissionToCheck string) (bool, error) {
// perm, err := commandOutput(`stat -c '%a' ` + filePath)
// return perm == permissionToCheck, err
// }

func permissionIs(filePath, checkType, permissionToCheck string) (bool, error) {
if checkType == "octal" {
perm, err := commandOutput(`stat -c '%a' ` + filePath)
return perm == permissionToCheck, err
} else if checkType == "WorldWritable" {
return commandContains(`find `+filePath+` -perm -g+w -or -perm -o+w`, filePath)
} else if checkType == "WorldReadable" {
return commandContains(`find `+filePath+` -perm -o=r`, filePath)
}
// If arguments are messed up or whatever:
return false, nil
}
19 changes: 17 additions & 2 deletions docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,27 @@ type='AutoCheckUpdatesEnabled'

> Only works for standard `apt` installs.
**PermissionIs**: pass if the specified file has the octal permissions specified
**PermissionIs**: pass if the specified file has permissions specified

```
type='PermissionIs'
arg1='/etc/passwd'
arg2='644'
arg2='octal'
arg3='644'
```

```
type='PermissionIs'
arg1='/etc/passwd'
arg2='WorldWritable'
arg3='none'
```

```
type='PermissionIs'
arg1='/etc/passwd'
arg2='WorldReadable'
arg3='none'
```

<hr>
Expand Down
28 changes: 28 additions & 0 deletions docs/examples/linux-allchecks.conf
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,31 @@ arg2='644'
type='PermissionIsNot'
arg1='/etc/passwd'
arg2='777'

[[check]]
[[check.pass]]
type='PermissionIs'
arg1='/etc/passwd'
arg2='WorldWritable'
arg3='none'

[[check]]
[[check.pass]]
type='PermissionIsNot'
arg1='/etc/passwd'
arg2='WorldWritable'
arg3='none'

[[check]]
[[check.pass]]
type='PermissionIs'
arg1='/etc/passwd'
arg2='WorldReadable'
arg3='none'

[[check]]
[[check.pass]]
type='PermissionIsNot'
arg1='/etc/passwd'
arg2='WorldReadable'
arg3='none'

0 comments on commit b701e3f

Please sign in to comment.