-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from TheBestLL/master
兼容windows user表 还有ssh协议公钥查询表
- Loading branch information
Showing
8 changed files
with
275 additions
and
11 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 |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
.DS_Store | ||
*.log | ||
dist/ | ||
vendor/ | ||
.vscode/ |
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,42 @@ | ||
//go:build linux | ||
|
||
package tables | ||
|
||
import ( | ||
"github.com/dean2021/sysql/extend/tables/system" | ||
"github.com/dean2021/sysql/table" | ||
) | ||
|
||
func init() { | ||
t := &SSHKeysTable{} | ||
err := table.Register(t, t.Name()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
type SSHKeysTable struct{} | ||
|
||
func (p *SSHKeysTable) Name() string { | ||
return "ssh_keys" | ||
} | ||
|
||
func (p *SSHKeysTable) Columns() table.TableColumns { | ||
return table.TableColumns{ | ||
{Name: "uid", Type: table.BIGINT_TYPE, Options: table.INDEX}, | ||
{Name: "path", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "username", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "file_name", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "file_size", Type: table.BIGINT_TYPE, Options: table.DEFAULT}, | ||
{Name: "mod_time", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "key", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
} | ||
} | ||
|
||
func (p *SSHKeysTable) Generate(context *table.QueryContext) (table.TableRows, error) { | ||
list, err := system.GenSSHKeys(context) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return list, 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,65 @@ | ||
//go:build linux | ||
|
||
package system | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/dean2021/sysql/table" | ||
) | ||
|
||
var authorizedKeyFileNames = []string{"authorized_keys", "authorized_keys2"} | ||
|
||
func GenSSHKeys(context *table.QueryContext) (table.TableRows, error) { | ||
var results table.TableRows | ||
users, err := getUsers() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, u := range users { | ||
for _, name := range authorizedKeyFileNames { | ||
//判断是否存在.ssh文件 | ||
path := filepath.Join(u.Directory, ".ssh", name) | ||
stat, err := os.Stat(path) | ||
if err != nil { | ||
continue | ||
} | ||
keys, err := getPublicKeys(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, key := range keys { | ||
results = append(results, table.TableRow{ | ||
"uid": u.Uid, | ||
"path": path, | ||
"username": u.Username, | ||
"file_name": name, | ||
"file_size": stat.Size(), | ||
"mod_time": stat.ModTime().Format("2006-01-02 15:04:05"), | ||
"key": key, | ||
}) | ||
} | ||
} | ||
} | ||
return results, nil | ||
} | ||
|
||
func getPublicKeys(path string) ([]string, error) { | ||
//遍历authorized_keys文件 | ||
fi, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer fi.Close() | ||
var lines []string | ||
scanner := bufio.NewScanner(fi) | ||
for scanner.Scan() { | ||
lines = append(lines, scanner.Text()) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
return lines, 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
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,65 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package system | ||
|
||
import ( | ||
"github.com/dean2021/sysql/table" | ||
"github.com/yusufpapurcu/wmi" | ||
) | ||
|
||
type Win32UserAccount struct { | ||
AccountType int64 `json:"accountType"` | ||
Caption string `json:"caption"` | ||
Description string `json:"description"` | ||
Disabled bool `json:"disabled"` | ||
Domain string `json:"domain"` | ||
FullName string `json:"fullName"` | ||
InstallDate string `json:"installDate"` | ||
LocalAccount bool `json:"localAccount"` | ||
Lockout bool `json:"lockout"` | ||
PasswordChangeable bool `json:"passwordChangeable"` | ||
PasswordExpires bool `json:"passwordExpires"` | ||
PasswordRequired bool `json:"passwordRequired"` | ||
Name string `json:"name"` | ||
SID string `json:"sid"` | ||
SIDType int64 `json:"sidType"` | ||
Status string `json:"status"` | ||
} | ||
|
||
func getWin32UserAccount() ([]Win32UserAccount, error) { | ||
var s []Win32UserAccount | ||
err := wmi.Query("SELECT * FROM Win32_UserAccount WHERE LocalAccount=True", &s) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s, nil | ||
} | ||
func GenUsers(context *table.QueryContext) (table.TableRows, error) { | ||
var results table.TableRows | ||
accounts, err := getWin32UserAccount() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, a := range accounts { | ||
results = append(results, table.TableRow{ | ||
"accountType": a.AccountType, | ||
"caption": a.Caption, | ||
"description": a.Description, | ||
"disabled": a.Disabled, | ||
"domain": a.Domain, | ||
"fullName": a.FullName, | ||
"installDate": a.InstallDate, | ||
"localAccount": a.LocalAccount, | ||
"lockout": a.Lockout, | ||
"passwordChangeable": a.PasswordChangeable, | ||
"passwordExpires": a.PasswordExpires, | ||
"passwordRequired": a.PasswordRequired, | ||
"name": a.Name, | ||
"sid": a.SID, | ||
"sidType": a.SIDType, | ||
"status": a.Status, | ||
}) | ||
} | ||
return results, 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
//go:build linux | ||
|
||
package tables | ||
|
||
import ( | ||
|
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,52 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package tables | ||
|
||
import ( | ||
"github.com/dean2021/sysql/extend/tables/system" | ||
"github.com/dean2021/sysql/table" | ||
) | ||
|
||
func init() { | ||
t := &UsersTable{} | ||
err := table.Register(t, t.Name()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
type UsersTable struct{} | ||
|
||
func (p *UsersTable) Name() string { | ||
return "users" | ||
} | ||
|
||
func (p *UsersTable) Columns() table.TableColumns { | ||
return table.TableColumns{ | ||
{Name: "sid", Type: table.TEXT_TYPE, Options: table.INDEX}, | ||
{Name: "name", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "accountType", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, | ||
{Name: "caption", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "description", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "disabled", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer | ||
{Name: "domain", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "fullName", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "installDate", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
{Name: "localAccount", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer | ||
{Name: "lockout", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer | ||
{Name: "passwordChangeable", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer | ||
{Name: "passwordExpires", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer | ||
{Name: "passwordRequired", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, // Assuming boolean is represented as integer | ||
{Name: "sidType", Type: table.INTEGER_TYPE, Options: table.DEFAULT}, | ||
{Name: "status", Type: table.TEXT_TYPE, Options: table.DEFAULT}, | ||
} | ||
} | ||
|
||
func (p *UsersTable) Generate(context *table.QueryContext) (table.TableRows, error) { | ||
list, err := system.GenUsers(context) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return list, nil | ||
} |