-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathaccess.go
136 lines (109 loc) · 3.05 KB
/
access.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package commands
import (
"errors"
"fmt"
"io"
"github.com/NHAS/reverse_ssh/internal/server/users"
"github.com/NHAS/reverse_ssh/internal/terminal"
"github.com/NHAS/reverse_ssh/internal/terminal/autocomplete"
)
type access struct {
}
func (s *access) Run(user *users.User, tty io.ReadWriter, line terminal.ParsedLine) error {
var err error
pattern, err := line.GetArgString("p")
if err != nil {
if err != terminal.ErrFlagNotSet {
return err
}
pattern, err = line.GetArgString("pattern")
if err != nil && err != terminal.ErrFlagNotSet {
return err
}
}
newOwners, err := line.GetArgString("o")
if err != nil {
if err != terminal.ErrFlagNotSet {
return err
}
newOwners, err = line.GetArgString("owners")
if err != nil && err != terminal.ErrFlagNotSet {
return err
}
}
if line.IsSet("c") || line.IsSet("current") {
newOwners = user.Username()
}
if line.IsSet("a") || line.IsSet("all") {
newOwners = ""
}
if spaceMatcher.MatchString(newOwners) {
return errors.New("new owners cannot contain spaces")
}
connections, err := user.SearchClients(pattern)
if err != nil {
return err
}
if len(connections) == 0 {
return fmt.Errorf("No clients matched '%s'", pattern)
}
if !line.IsSet("y") {
fmt.Fprintf(tty, "Modifing ownership of %d clients? [N/y] ", len(connections))
if term, ok := tty.(*terminal.Terminal); ok {
term.EnableRaw()
}
b := make([]byte, 1)
_, err := tty.Read(b)
if err != nil {
if term, ok := tty.(*terminal.Terminal); ok {
term.DisableRaw()
}
return err
}
if term, ok := tty.(*terminal.Terminal); ok {
term.DisableRaw()
}
if !(b[0] == 'y' || b[0] == 'Y') {
return fmt.Errorf("\nUser did not enter y/Y, aborting")
}
}
changes := 0
for id := range connections {
err := user.SetOwnership(id, newOwners)
if err != nil {
fmt.Fprintf(tty, "error changing ownership of %s: err %s", id, err)
continue
}
changes++
}
return fmt.Errorf("%d client owners modified", changes)
}
func (s *access) ValidArgs() map[string]string {
r := map[string]string{
"y": "Auto confirm prompt",
}
addDuplicateFlags("Clients to act on", r, "p", "pattern")
addDuplicateFlags("Set the ownership of the client, comma seperated user list", r, "o", "owners")
addDuplicateFlags("Set the ownership to only the current user", r, "c", "current")
addDuplicateFlags("Set the ownership to anyone on the server", r, "a", "all")
return r
}
func (s *access) Expect(line terminal.ParsedLine) []string {
if line.Section != nil {
switch line.Section.Value() {
case "p", "pattern":
return []string{autocomplete.RemoteId}
}
}
return nil
}
func (s *access) Help(explain bool) string {
if explain {
return "Temporarily share/unhide client connection."
}
return terminal.MakeHelpText(s.ValidArgs(),
"access [OPTIONS] -p <FILTER>",
"Change ownership of client connection, only lasts until restart of rssh server, to make permanent edit authorized_controllee_keys 'owner' option",
"Filter uses glob matching against all attributes of a target (id, public key hash, hostname, ip)",
)
}