-
-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathworkingdir.go
More file actions
107 lines (84 loc) · 1.98 KB
/
workingdir.go
File metadata and controls
107 lines (84 loc) · 1.98 KB
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
package main
import (
"bufio"
"fmt"
"os"
"path"
"regexp"
"strings"
"github.com/tg123/sshpiper/libplugin"
)
type workingdir struct {
Path string
NoCheckPerm bool
Strict bool
}
// Base username validation on Debians default: https://sources.debian.net/src/adduser/3.113%2Bnmu3/adduser.conf/#L85
// -> NAME_REGEX="^[a-z][-a-z0-9_]*\$"
// The length is limited to 32 characters. See man 8 useradd: https://linux.die.net/man/8/useradd
var usernameRule *regexp.Regexp = regexp.MustCompile("^[a-z_][-a-z0-9_]{0,31}$")
const (
userAuthorizedKeysFile = "authorized_keys"
userKeyFile = "id_rsa"
userUpstreamFile = "sshpiper_upstream"
userKnownHosts = "known_hosts"
)
func isUsernameSecure(user string) bool {
return usernameRule.MatchString(user)
}
func (w *workingdir) checkPerm(file string) error {
filename := path.Join(w.Path, file)
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
if w.NoCheckPerm {
return nil
}
if fi.Mode().Perm()&0o077 != 0 {
return fmt.Errorf("%v's perm is too open", filename)
}
return nil
}
func (w *workingdir) fullpath(file string) string {
return path.Join(w.Path, file)
}
func (w *workingdir) Readfile(file string) ([]byte, error) {
if err := w.checkPerm(file); err != nil {
return nil, err
}
return os.ReadFile(w.fullpath(file))
}
func (w *workingdir) Exists(file string) bool {
info, err := os.Stat(w.fullpath(file))
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// TODO refactor this
func parseUpstreamFile(data string) (host string, user string, err error) {
r := bufio.NewReader(strings.NewReader(data))
for {
host, err = r.ReadString('\n')
if err != nil {
break
}
host = strings.TrimSpace(host)
if host != "" && host[0] != '#' {
break
}
}
t := strings.SplitN(host, "@", 2)
if len(t) > 1 {
user = t[0]
host = t[1]
}
_, _, err = libplugin.SplitHostPortForSSH(host)
return
}