go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/_motor/providers/os/os.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package os 5 6 import ( 7 "io" 8 "io/fs" 9 "os" 10 "regexp" 11 "time" 12 13 "go.mondoo.com/cnquery/motor/providers" 14 15 "github.com/spf13/afero" 16 ) 17 18 type OperatingSystemProvider interface { 19 providers.Instance 20 // RunCommand executes a command on the target system 21 RunCommand(command string) (*Command, error) 22 // FileInfo returns file permissions and ownership 23 FileInfo(path string) (FileInfoDetails, error) 24 // FS provides access to the file system of the target system 25 FS() afero.Fs 26 } 27 28 type CommandRunner interface { 29 RunCommand(command string) (*Command, error) 30 } 31 32 type PerfStats struct { 33 Start time.Time `json:"start"` 34 Duration time.Duration `json:"duration"` 35 } 36 37 type Command struct { 38 Command string 39 Stats PerfStats 40 Stdout io.ReadWriter 41 Stderr io.ReadWriter 42 ExitStatus int 43 } 44 45 type FileInfo struct { 46 FName string 47 FSize int64 48 FIsDir bool 49 FModTime time.Time 50 FMode os.FileMode 51 Uid int64 52 Gid int64 53 } 54 55 func (f *FileInfo) Name() string { 56 return f.FName 57 } 58 59 func (f *FileInfo) Size() int64 { 60 return f.FSize 61 } 62 63 func (f *FileInfo) Mode() os.FileMode { 64 return f.FMode 65 } 66 67 func (f *FileInfo) ModTime() time.Time { 68 return f.FModTime 69 } 70 71 func (f *FileInfo) IsDir() bool { 72 return f.FIsDir 73 } 74 75 func (f *FileInfo) Sys() interface{} { 76 return f 77 } 78 79 type FileModeDetails struct { 80 os.FileMode 81 } 82 83 func (mode FileModeDetails) UserReadable() bool { 84 return uint32(mode.FileMode)&0o0400 != 0 85 } 86 87 func (mode FileModeDetails) UserWriteable() bool { 88 return uint32(mode.FileMode)&0o0200 != 0 89 } 90 91 func (mode FileModeDetails) UserExecutable() bool { 92 return uint32(mode.FileMode)&0o0100 != 0 93 } 94 95 func (mode FileModeDetails) GroupReadable() bool { 96 return uint32(mode.FileMode)&0o0040 != 0 97 } 98 99 func (mode FileModeDetails) GroupWriteable() bool { 100 return uint32(mode.FileMode)&0o0020 != 0 101 } 102 103 func (mode FileModeDetails) GroupExecutable() bool { 104 return uint32(mode.FileMode)&0o0010 != 0 105 } 106 107 func (mode FileModeDetails) OtherReadable() bool { 108 return uint32(mode.FileMode)&0o0004 != 0 109 } 110 111 func (mode FileModeDetails) OtherWriteable() bool { 112 return uint32(mode.FileMode)&0o0002 != 0 113 } 114 115 func (mode FileModeDetails) OtherExecutable() bool { 116 return uint32(mode.FileMode)&0o0001 != 0 117 } 118 119 func (mode FileModeDetails) Suid() bool { 120 return mode.FileMode&fs.ModeSetuid != 0 121 } 122 123 func (mode FileModeDetails) Sgid() bool { 124 return mode.FileMode&fs.ModeSetgid != 0 125 } 126 127 func (mode FileModeDetails) Sticky() bool { 128 return mode.FileMode&fs.ModeSticky != 0 129 } 130 131 func (mode FileModeDetails) UnixMode() uint32 { 132 m := mode.FileMode & 0o777 133 134 if mode.IsDir() { 135 } 136 137 if (mode.FileMode & fs.ModeSetuid) != 0 { 138 m |= 0o4000 139 } 140 141 if (mode.FileMode & fs.ModeSetgid) != 0 { 142 m |= 0o2000 143 } 144 145 if (mode.FileMode & fs.ModeSticky) != 0 { 146 m |= 0o1000 147 } 148 149 return uint32(m) 150 } 151 152 type FileInfoDetails struct { 153 Size int64 154 Mode FileModeDetails 155 Uid int64 156 Gid int64 157 } 158 159 type FileSearch interface { 160 Find(from string, r *regexp.Regexp, typ string) ([]string, error) 161 }