go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/users/manager.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package users
     5  
     6  import (
     7  	"errors"
     8  
     9  	"go.mondoo.com/cnquery/providers/os/connection/shared"
    10  )
    11  
    12  type User struct {
    13  	ID          string
    14  	Uid         int64
    15  	Gid         int64
    16  	Sid         string
    17  	Name        string
    18  	Description string
    19  	Shell       string
    20  	Home        string
    21  	Enabled     bool
    22  }
    23  
    24  type OSUserManager interface {
    25  	Name() string
    26  	User(id string) (*User, error)
    27  	List() ([]*User, error)
    28  }
    29  
    30  func ResolveManager(conn shared.Connection) (OSUserManager, error) {
    31  	var um OSUserManager
    32  
    33  	asset := conn.Asset()
    34  	if asset == nil || asset.Platform == nil {
    35  		return nil, errors.New("cannot find OS information for users detection")
    36  	}
    37  
    38  	// check darwin before unix since darwin is also a unix
    39  	if asset.Platform.IsFamily("darwin") {
    40  		um = &OSXUserManager{conn: conn}
    41  	} else if asset.Platform.IsFamily("unix") {
    42  		um = &UnixUserManager{conn: conn}
    43  	} else if asset.Platform.IsFamily("windows") {
    44  		um = &WindowsUserManager{conn: conn}
    45  	}
    46  
    47  	if um == nil {
    48  		return nil, errors.New("could not detect suitable group manager for platform: " + asset.Platform.Name)
    49  	}
    50  
    51  	return um, nil
    52  }
    53  
    54  func findUser(users []*User, id string) (*User, error) {
    55  	// search for id
    56  	for i := range users {
    57  		user := users[i]
    58  		if user.ID == id {
    59  			return user, nil
    60  		}
    61  	}
    62  
    63  	return nil, errors.New("user> " + id + " does not exist")
    64  }