Skip to content

Commit

Permalink
util: Add CheckIfRoot
Browse files Browse the repository at this point in the history
Replace IsUserRoot with CheckIfRoot. This allows all functions to use
the same error handling when a user is not root.
  • Loading branch information
josephlr committed Oct 19, 2017
1 parent fe76f6f commit 3269bc5
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/fscrypt/commands.go
Expand Up @@ -353,7 +353,7 @@ func purgeAction(c *cli.Context) error {
}

if dropCachesFlag.Value {
if !util.IsUserRoot() {
if util.CurrentUserID() != 0 {
return newExitError(c, ErrDropCachesPerm)
}
}
Expand Down
1 change: 0 additions & 1 deletion cmd/fscrypt/errors.go
Expand Up @@ -54,7 +54,6 @@ var (
ErrSpecifyKeyFile = errors.New("no key file specified")
ErrKeyFileLength = errors.Errorf("key file must be %d bytes", metadata.InternalKeyLen)
ErrAllLoadsFailed = errors.New("could not load any protectors")
ErrMustBeRoot = errors.New("this command must be run as root")
ErrPolicyUnlocked = errors.New("this file or directory is already unlocked")
ErrBadOwners = errors.New("you do not own this directory")
ErrNotEmptyDir = errors.New("not an empty directory")
Expand Down
4 changes: 2 additions & 2 deletions cmd/fscrypt/setup.go
Expand Up @@ -31,8 +31,8 @@ import (

// createGlobalConfig creates (or overwrites) the global config file
func createGlobalConfig(w io.Writer, path string) error {
if !util.IsUserRoot() {
return ErrMustBeRoot
if err := util.CheckIfRoot(); err != nil {
return err
}

// Ask to create or replace the config file
Expand Down
2 changes: 1 addition & 1 deletion security/keyring.go
Expand Up @@ -114,7 +114,7 @@ func UserKeyringID(target *user.User, checkSession bool) (int, error) {
return 0, errors.Wrap(ErrAccessUserKeyring, err.Error())
}

if !util.IsUserRoot() {
if util.CurrentUserID() != 0 {
// Make sure the returned keyring will be accessible by checking
// that it is in the session keyring.
if checkSession && !isUserKeyringInSession(uid) {
Expand Down
2 changes: 2 additions & 0 deletions util/errors.go
Expand Up @@ -29,6 +29,8 @@ import (
)

var (
// ErrNotRoot indicates the action is restricted to the superuser.
ErrNotRoot = errors.New("only root can perform this action")
// ErrSkipIntegration indicates integration tests shouldn't be run.
ErrSkipIntegration = errors.New("skipping integration test")
)
Expand Down
8 changes: 8 additions & 0 deletions util/users.go
Expand Up @@ -48,3 +48,11 @@ func GetUser(uid int) *user.User {
func CurrentUser() *user.User {
return GetUser(CurrentUserID())
}

// CheckIfRoot returns ErrNotRoot if the current user is not the root user.
func CheckIfRoot() error {
if id := CurrentUserID(); id != 0 {
return ErrNotRoot
}
return nil
}
5 changes: 0 additions & 5 deletions util/util.go
Expand Up @@ -117,8 +117,3 @@ func AtoiOrPanic(input string) int {
}
return i
}

// IsUserRoot checks if the effective user is root.
func IsUserRoot() bool {
return CurrentUserID() == 0
}

0 comments on commit 3269bc5

Please sign in to comment.