mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 12:25:53 +02:00
replace syscall with unix/windows packages where possible
from the 1.22 release notes: The syscall package has been frozen since Go 1.4 and was marked as deprecated in Go 1.11 [...] However, some non-deprecated functionality requires use of the syscall package [...] To avoid unnecessary complaints[...] no longer marked as deprecated. The package remains frozen to most new functionality, and new code remains encouraged to use golang.org/x/sys/unix or golang.org/x/sys/windows where possible.
This commit is contained in:
parent
429418ffc6
commit
89efed68b0
7 changed files with 28 additions and 25 deletions
|
@ -12,13 +12,13 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
"github.com/pbnjay/memory"
|
"github.com/pbnjay/memory"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/metabase"
|
"github.com/crowdsecurity/crowdsec/pkg/metabase"
|
||||||
|
@ -457,7 +457,7 @@ func (cli *cliDashboard) chownDatabase(gid string) error {
|
||||||
|
|
||||||
if stat, err := os.Stat(cfg.DbConfig.DbPath); !os.IsNotExist(err) {
|
if stat, err := os.Stat(cfg.DbConfig.DbPath); !os.IsNotExist(err) {
|
||||||
info := stat.Sys()
|
info := stat.Sys()
|
||||||
if err := os.Chown(cfg.DbConfig.DbPath, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
|
if err := os.Chown(cfg.DbConfig.DbPath, int(info.(*unix.Stat_t).Uid), intID); err != nil {
|
||||||
return fmt.Errorf("unable to chown sqlite db file '%s': %s", cfg.DbConfig.DbPath, err)
|
return fmt.Errorf("unable to chown sqlite db file '%s': %s", cfg.DbConfig.DbPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -467,7 +467,7 @@ func (cli *cliDashboard) chownDatabase(gid string) error {
|
||||||
file := cfg.DbConfig.DbPath + ext
|
file := cfg.DbConfig.DbPath + ext
|
||||||
if stat, err := os.Stat(file); !os.IsNotExist(err) {
|
if stat, err := os.Stat(file); !os.IsNotExist(err) {
|
||||||
info := stat.Sys()
|
info := stat.Sys()
|
||||||
if err := os.Chown(file, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
|
if err := os.Chown(file, int(info.(*unix.Stat_t).Uid), intID); err != nil {
|
||||||
return fmt.Errorf("unable to chown sqlite db file '%s': %s", file, err)
|
return fmt.Errorf("unable to chown sqlite db file '%s': %s", file, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -67,7 +66,7 @@ func runService(name string) error {
|
||||||
// All the calls to logging before the logger is configured are pretty much useless, but we keep them for clarity
|
// All the calls to logging before the logger is configured are pretty much useless, but we keep them for clarity
|
||||||
err := eventlog.InstallAsEventCreate("CrowdSec", eventlog.Error|eventlog.Warning|eventlog.Info)
|
err := eventlog.InstallAsEventCreate("CrowdSec", eventlog.Error|eventlog.Warning|eventlog.Info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errno, ok := err.(syscall.Errno); ok {
|
if errno, ok := err.(windows.Errno); ok {
|
||||||
if errno == windows.ERROR_ACCESS_DENIED {
|
if errno == windows.ERROR_ACCESS_DENIED {
|
||||||
log.Warnf("Access denied when installing event source, running as non-admin ?")
|
log.Warnf("Access denied when installing event source, running as non-admin ?")
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/winops/winlog"
|
"github.com/google/winops/winlog"
|
||||||
|
@ -180,7 +179,7 @@ func (w *WinEventLogSource) getEvents(out chan types.Event, t *tomb.Tomb) error
|
||||||
w.logger.Errorf("WaitForSingleObject failed: %s", err)
|
w.logger.Errorf("WaitForSingleObject failed: %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if status == syscall.WAIT_OBJECT_0 {
|
if status == windows.WAIT_OBJECT_0 {
|
||||||
renderedEvents, err := w.getXMLEvents(w.evtConfig, publisherCache, subscription, 500)
|
renderedEvents, err := w.getXMLEvents(w.evtConfig, publisherCache, subscription, 500)
|
||||||
if err == windows.ERROR_NO_MORE_ITEMS {
|
if err == windows.ERROR_NO_MORE_ITEMS {
|
||||||
windows.ResetEvent(w.evtConfig.SignalEvent)
|
windows.ResetEvent(w.evtConfig.SignalEvent)
|
||||||
|
@ -225,7 +224,7 @@ func (w *WinEventLogSource) generateConfig(query string) (*winlog.SubscribeConfi
|
||||||
return &config, fmt.Errorf("windows.CreateEvent failed: %v", err)
|
return &config, fmt.Errorf("windows.CreateEvent failed: %v", err)
|
||||||
}
|
}
|
||||||
config.Flags = wevtapi.EvtSubscribeToFutureEvents
|
config.Flags = wevtapi.EvtSubscribeToFutureEvents
|
||||||
config.Query, err = syscall.UTF16PtrFromString(query)
|
config.Query, err = windows.UTF16PtrFromString(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &config, fmt.Errorf("syscall.UTF16PtrFromString failed: %v", err)
|
return &config, fmt.Errorf("syscall.UTF16PtrFromString failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,10 +14,12 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CheckCredential(uid int, gid int) *syscall.SysProcAttr {
|
func CheckCredential(uid int, gid int) *unix.SysProcAttr {
|
||||||
return &syscall.SysProcAttr{
|
return &unix.SysProcAttr{
|
||||||
Credential: &syscall.Credential{
|
Credential: &syscall.Credential{
|
||||||
Uid: uint32(uid),
|
Uid: uint32(uid),
|
||||||
Gid: uint32(gid),
|
Gid: uint32(gid),
|
||||||
|
@ -80,7 +82,7 @@ func getPluginTypeAndSubtypeFromPath(path string) (string, string, error) {
|
||||||
return strings.Join(parts[:len(parts)-1], "-"), parts[len(parts)-1], nil
|
return strings.Join(parts[:len(parts)-1], "-"), parts[len(parts)-1], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProcessAttr(username string, groupname string) (*syscall.SysProcAttr, error) {
|
func getProcessAttr(username string, groupname string) (*unix.SysProcAttr, error) {
|
||||||
uid, err := getUID(username)
|
uid, err := getUID(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -90,7 +92,7 @@ func getProcessAttr(username string, groupname string) (*syscall.SysProcAttr, er
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &syscall.SysProcAttr{
|
return &unix.SysProcAttr{
|
||||||
Credential: &syscall.Credential{
|
Credential: &syscall.Credential{
|
||||||
Uid: uid,
|
Uid: uid,
|
||||||
Gid: gid,
|
Gid: gid,
|
||||||
|
@ -116,7 +118,7 @@ func pluginIsValid(path string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("while looking up the current uid: %w", err)
|
return fmt.Errorf("while looking up the current uid: %w", err)
|
||||||
}
|
}
|
||||||
stat := details.Sys().(*syscall.Stat_t)
|
stat := details.Sys().(*unix.Stat_t)
|
||||||
if stat.Uid != currentUID {
|
if stat.Uid != currentUID {
|
||||||
return fmt.Errorf("plugin at %s is not owned by user '%s'", path, currentUser.Username)
|
return fmt.Errorf("plugin at %s is not owned by user '%s'", path, currentUser.Username)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
advapi32 = syscall.NewLazyDLL("advapi32.dll")
|
advapi32 = windows.NewLazyDLL("advapi32.dll")
|
||||||
|
|
||||||
procGetAce = advapi32.NewProc("GetAce")
|
procGetAce = advapi32.NewProc("GetAce")
|
||||||
)
|
)
|
||||||
|
@ -149,7 +149,7 @@ func CheckPerms(path string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProcessAtr() (*syscall.SysProcAttr, error) {
|
func getProcessAtr() (*windows.SysProcAttr, error) {
|
||||||
var procToken, token windows.Token
|
var procToken, token windows.Token
|
||||||
|
|
||||||
proc := windows.CurrentProcess()
|
proc := windows.CurrentProcess()
|
||||||
|
@ -195,7 +195,7 @@ func getProcessAtr() (*syscall.SysProcAttr, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return &windows.SysProcAttr{
|
return &windows.SysProcAttr{
|
||||||
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
|
CreationFlags: windows.CREATE_NEW_PROCESS_GROUP,
|
||||||
Token: syscall.Token(token),
|
Token: syscall.Token(token),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,14 +3,15 @@
|
||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"syscall"
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetFSType(path string) (string, error) {
|
func GetFSType(path string) (string, error) {
|
||||||
var fsStat syscall.Statfs_t
|
var fsStat unix.Statfs_t
|
||||||
|
|
||||||
if err := syscall.Statfs(path, &fsStat); err != nil {
|
if err := unix.Statfs(path, &fsStat); err != nil {
|
||||||
return "", fmt.Errorf("failed to get filesystem type: %w", err)
|
return "", fmt.Errorf("failed to get filesystem type: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,16 +4,18 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"golang.org/x/sys/windows"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetFSType(path string) (string, error) {
|
func GetFSType(path string) (string, error) {
|
||||||
kernel32, err := syscall.LoadLibrary("kernel32.dll")
|
kernel32, err := windows.LoadLibrary("kernel32.dll")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer syscall.FreeLibrary(kernel32)
|
defer windows.FreeLibrary(kernel32)
|
||||||
|
|
||||||
getVolumeInformation, err := syscall.GetProcAddress(kernel32, "GetVolumeInformationW")
|
getVolumeInformation, err := windows.GetProcAddress(kernel32, "GetVolumeInformationW")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -27,7 +29,7 @@ func GetFSType(path string) (string, error) {
|
||||||
// Get the root path of the volume
|
// Get the root path of the volume
|
||||||
volumeRoot := filepath.VolumeName(absPath) + "\\"
|
volumeRoot := filepath.VolumeName(absPath) + "\\"
|
||||||
|
|
||||||
volumeRootPtr, _ := syscall.UTF16PtrFromString(volumeRoot)
|
volumeRootPtr, _ := windows.UTF16PtrFromString(volumeRoot)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
fileSystemNameBuffer = make([]uint16, 260)
|
fileSystemNameBuffer = make([]uint16, 260)
|
||||||
|
@ -49,5 +51,5 @@ func GetFSType(path string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return syscall.UTF16ToString(fileSystemNameBuffer), nil
|
return windows.UTF16ToString(fileSystemNameBuffer), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue