forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is done by splitting the parts using syslog and syscall and then create simpler _windows.go versions.
- Loading branch information
1 parent
453e510
commit 8c8fb75
Showing
8 changed files
with
165 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func DropPrivileges() error { | ||
var err error | ||
|
||
if !_ConfigMeta.IsDefined("runoptions", "uid") { | ||
// not found, no dropping privileges but no err | ||
return nil | ||
} | ||
|
||
if !_ConfigMeta.IsDefined("runoptions", "gid") { | ||
return MsgError("GID must be specified for dropping privileges") | ||
} | ||
|
||
INFO("Switching to user: %d.%d", _Config.RunOptions.Uid, _Config.RunOptions.Gid) | ||
|
||
if err = syscall.Setgid(_Config.RunOptions.Gid); err != nil { | ||
return MsgError("setgid: %s", err.Error()) | ||
} | ||
|
||
if err = syscall.Setuid(_Config.RunOptions.Uid); err != nil { | ||
return MsgError("setuid: %s", err.Error()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
func DropPrivileges() error { | ||
var err error | ||
|
||
if !_ConfigMeta.IsDefined("runoptions", "uid") { | ||
// not found, no dropping privileges but no err | ||
return nil | ||
} | ||
|
||
return MsgError("Dropping privileges is not supported on Windows") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"runtime/debug" | ||
) | ||
type Logger struct { | ||
toSyslog bool | ||
level Priority | ||
selectors map[string]bool | ||
|
||
logger *log.Logger | ||
} | ||
|
||
type Priority int | ||
const ( | ||
// Severity. | ||
|
||
// From /usr/include/sys/syslog.h. | ||
// These are the same on Linux, BSD, and OS X. | ||
LOG_EMERG Priority = iota | ||
LOG_ALERT | ||
LOG_CRIT | ||
LOG_ERR | ||
LOG_WARNING | ||
LOG_NOTICE | ||
LOG_INFO | ||
LOG_DEBUG | ||
) | ||
|
||
var _log Logger | ||
|
||
func DEBUG(selector string, format string, v ...interface{}) { | ||
if _log.level >= LOG_DEBUG { | ||
selected := _log.selectors[selector] | ||
if !selected { | ||
return | ||
} | ||
_log.logger.Output(2, fmt.Sprintf("DBG "+format, v...)) | ||
} | ||
} | ||
|
||
func IS_DEBUG(selector string) bool { | ||
return _log.selectors[selector] | ||
} | ||
|
||
func INFO(format string, v ...interface{}) { | ||
if _log.level >= LOG_INFO { | ||
_log.logger.Output(2, fmt.Sprintf("INFO "+format, v...)) | ||
} | ||
} | ||
|
||
func WARN(format string, v ...interface{}) { | ||
if _log.level >= LOG_WARNING { | ||
_log.logger.Output(2, fmt.Sprintf("WARN "+format, v...)) | ||
} | ||
} | ||
|
||
func ERR(format string, v ...interface{}) { | ||
if _log.level >= LOG_ERR { | ||
_log.logger.Output(2, fmt.Sprintf("ERR "+format, v...)) | ||
} | ||
} | ||
|
||
func CRIT(format string, v ...interface{}) { | ||
if _log.level >= LOG_CRIT { | ||
_log.logger.Output(2, fmt.Sprintf("CRIT "+format, v...)) | ||
} | ||
} | ||
|
||
func WTF(format string, v ...interface{}) { | ||
if _log.level >= LOG_CRIT { | ||
_log.logger.Output(2, fmt.Sprintf("CRIT "+format, v...)) | ||
} | ||
|
||
// TODO: assert here when not in production mode | ||
} | ||
|
||
func RECOVER(msg string) { | ||
if r := recover(); r != nil { | ||
ERR("%s. Recovering, but please report this: %s.", msg, r) | ||
ERR("Stacktrace: %s", debug.Stack()) | ||
} | ||
} | ||
|
||
func LogInit(level Priority, prefix string, toSyslog bool, debugSelectors []string) { | ||
_log.level = level | ||
|
||
_log.selectors = make(map[string]bool) | ||
for _, selector := range debugSelectors { | ||
_log.selectors[selector] = true | ||
} | ||
|
||
_log.logger = log.New(os.Stdout, prefix, log.Lshortfile) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters