Skip to content

Commit 6bd113e

Browse files
committed
set cwd to user's homedir
1 parent 73ce3a2 commit 6bd113e

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

cmd/function22/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,12 @@ func handleSSHConnection(s gliderssh.Session, account linuxuser.Account, verbose
197197

198198
sshClientEnvs = append(sshClientEnvs, sshConnectionEnvVars(s)...)
199199
if userWantsDefaultShell { // FIXME: should this be set anyway?
200-
sshClientEnvs = append(sshClientEnvs, loginEnvVars(s, account.Shell)...)
200+
sshClientEnvs = append(sshClientEnvs, loginEnvVars(account)...)
201201
}
202202

203203
//nolint:gosec // Due to nature of SSH this is a thing we must do
204204
cmd := exec.CommandContext(s.Context(), argv[0], argv[1:]...)
205+
cmd.Dir = account.Homedir
205206
cmd.Env = sshClientEnvs
206207
cmd.SysProcAttr = &syscall.SysProcAttr{
207208
Credential: &syscall.Credential{
@@ -281,11 +282,11 @@ func handleSSHConnection(s gliderssh.Session, account linuxuser.Account, verbose
281282
}
282283

283284
// https://unix.stackexchange.com/a/76356
284-
func loginEnvVars(s gliderssh.Session, shell string) []string {
285+
func loginEnvVars(account linuxuser.Account) []string {
285286
return []string{
286-
makeEnvVarStr("HOME", fmt.Sprintf("/home/%s", s.User())),
287-
makeEnvVarStr("SHELL", shell),
288-
makeEnvVarStr("USER", s.User()),
287+
makeEnvVarStr("HOME", account.Homedir),
288+
makeEnvVarStr("SHELL", account.Shell),
289+
makeEnvVarStr("USER", account.Username),
289290
}
290291
}
291292

pkg/linuxuser/shadow.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import (
1515
)
1616

1717
type Account struct {
18-
username string
18+
Username string
1919
passwordHash string
2020
Uid uint32
2121
GidPrimary uint32
2222
GidsSupplementary []uint32
23+
Homedir string
2324
Shell string
2425
}
2526

@@ -47,9 +48,9 @@ func FindByUsername(username string) (*Account, error) {
4748
itemUsername := parts[0]
4849

4950
if itemUsername == username {
50-
uid, gid, shell, err := resolveUIDAndGIDAndShellForUser(itemUsername)
51+
uid, gid, homedir, shell, err := resolveUserDetails(itemUsername)
5152
if err != nil {
52-
return nil, fmt.Errorf("resolveUIDAndGIDAndShellForUser: %w", err)
53+
return nil, fmt.Errorf("resolveUserDetails: %w", err)
5354
}
5455

5556
groups, err := resolveSupplementaryGids(itemUsername)
@@ -58,11 +59,12 @@ func FindByUsername(username string) (*Account, error) {
5859
}
5960

6061
return &Account{
61-
username: itemUsername,
62+
Username: itemUsername,
6263
passwordHash: parts[1],
6364
Uid: uid,
6465
GidPrimary: gid,
6566
GidsSupplementary: groups,
67+
Homedir: homedir,
6668
Shell: shell,
6769
}, nil
6870
}
@@ -111,10 +113,10 @@ func extractSHA512CryptSalt(account Account) ([]byte, error) {
111113
}
112114

113115
// /etc/shadow doesn't contain uid & gid, so we have to read it from a separate file
114-
func resolveUIDAndGIDAndShellForUser(username string) (uint32, uint32, string, error) {
116+
func resolveUserDetails(username string) (uint32, uint32, string, string, error) {
115117
passwdFile, err := os.Open("/etc/passwd")
116118
if err != nil {
117-
return 0, 0, "", err
119+
return 0, 0, "", "", err
118120
}
119121
defer passwdFile.Close()
120122

@@ -124,31 +126,32 @@ func resolveUIDAndGIDAndShellForUser(username string) (uint32, uint32, string, e
124126
// usbmux:x:106:46:usbmux daemon,,,:/var/lib/usbmux:/usr/sbin/nologin
125127
parts := strings.Split(passwdLines.Text(), ":")
126128
if len(parts) < 7 {
127-
return 0, 0, "", fmt.Errorf("/etc/passwd invalid parts number: %d", len(parts))
129+
return 0, 0, "", "", fmt.Errorf("/etc/passwd invalid parts number: %d", len(parts))
128130
}
129131

130132
name := parts[0]
131133
if name == username {
132134
uid, err := strconv.Atoi(parts[2])
133135
if err != nil {
134-
return 0, 0, "", err
136+
return 0, 0, "", "", err
135137
}
136138

137139
gid, err := strconv.Atoi(parts[3])
138140
if err != nil {
139-
return 0, 0, "", err
141+
return 0, 0, "", "", err
140142
}
141143

144+
homedir := parts[5]
142145
shell := parts[6]
143146

144-
return uint32(uid), uint32(gid), shell, nil
147+
return uint32(uid), uint32(gid), homedir, shell, nil
145148
}
146149
}
147150
if err := passwdLines.Err(); err != nil {
148-
return 0, 0, "", err
151+
return 0, 0, "", "", err
149152
}
150153

151-
return 0, 0, "", fmt.Errorf("user '%s' not found from /etc/passwd", username)
154+
return 0, 0, "", "", fmt.Errorf("user '%s' not found from /etc/passwd", username)
152155
}
153156

154157
func resolveSupplementaryGids(username string) ([]uint32, error) {

0 commit comments

Comments
 (0)