fleet/orbit/pkg/table/common/common_darwin.go
Lucas Manuel Rodriguez d4a1b4d218
Add CIS checks for 2.9.X and add pmset table to fleetd (#9470)
#9253

- ~[ ] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.~
- ~[ ] Documented any API changes (docs/Using-Fleet/REST-API.md or
docs/Contributing/API-for-contributors.md)~
- ~[ ] Documented any permissions changes~
- ~[ ] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)~
- ~[ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for
new osquery data ingestion features.~
- [X] Added/updated tests
- [X] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [X] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- ~[ ] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).~

---------

Co-authored-by: Sharon Katz <121527325+sharon-fdm@users.noreply.github.com>
2023-02-08 13:08:17 -03:00

57 lines
1.7 KiB
Go

//go:build darwin
// +build darwin
package common
import (
"errors"
"fmt"
"os"
"strings"
"syscall"
"github.com/antchfx/xmlquery"
)
// GetConsoleUidGid gets the uid and gid of the current (or more accurately, most recently logged
// in) *console* user. In most scenarios this should be the currently logged in user on the system.
// Note that getting the current user of the Orbit process is typically going to return root and we
// need the underlying user.
func GetConsoleUidGid() (uid uint32, gid uint32, err error) {
info, err := os.Stat("/dev/console")
if err != nil {
return 0, 0, err
}
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return 0, 0, fmt.Errorf("unexpected type %T", info.Sys())
}
return stat.Uid, stat.Gid, nil
}
// GetValFromXMLWithTags Will look for a sequence of tags and will get the following nested value as string
// In the following xml example the function will return "5" if called with parentTag = "someParentTag", tag = "someTag", tagValue = "someValue", valType = "integer"
// <someParentTag>
//
// <someTag>someValue</someTag>
// <integer>5</integer>
//
// </someParentTag>
func GetValFromXMLWithTags(xml string, parentTag string, tag string, tagValue string, valType string) (maxFailedAttempts string, err error) {
doc, err := xmlquery.Parse(strings.NewReader(xml))
if err != nil {
return "", errors.New("can't parse xml")
}
for _, channel := range xmlquery.Find(doc, "//"+parentTag) {
if n := channel.SelectElement(tag); n != nil {
if n.InnerText() != tagValue {
continue
}
}
if n := channel.SelectElement(valType); n != nil {
return n.InnerText(), nil
}
}
return "", errors.New("can't find requested value")
}