Skip to content

Commit

Permalink
Use id command for user lookup on MacOS (#384)
Browse files Browse the repository at this point in the history
When building client without CGO, user.Lookup
attempts to get user from /etc/passwd
Which doesn't have the user as MacOS uses
opendirectoryd as user directory
  • Loading branch information
mlsmaycon authored Jul 7, 2022
1 parent 7e1b20d commit ff729f6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
10 changes: 10 additions & 0 deletions client/ssh/lookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !darwin
// +build !darwin

package ssh

import "os/user"

func userNameLookup(username string) (*user.User, error) {
return user.Lookup(username)
}
47 changes: 47 additions & 0 deletions client/ssh/lookup_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build darwin
// +build darwin

package ssh

import (
"bytes"
"fmt"
"os/exec"
"os/user"
"strings"
)

func userNameLookup(username string) (*user.User, error) {
var userObject *user.User
userObject, err := user.Lookup(username)
if err != nil && err.Error() == user.UnknownUserError(username).Error() {
return idUserNameLookup(username)
} else if err != nil {
return nil, err
}

return userObject, nil
}

func idUserNameLookup(username string) (*user.User, error) {
cmd := exec.Command("id", "-P", username)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("error while retrieving user with id -P command, error: %v", err)
}
colon := ":"

if !bytes.Contains(out, []byte(username+colon)) {
return nil, fmt.Errorf("unable to find user in returned string")
}
// netbird:********:501:20::0:0:netbird:/Users/netbird:/bin/zsh
parts := strings.SplitN(string(out), colon, 10)
userObject := &user.User{
Username: parts[0],
Uid: parts[2],
Gid: parts[3],
Name: parts[7],
HomeDir: parts[8],
}
return userObject, nil
}
2 changes: 1 addition & 1 deletion client/ssh/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (srv *DefaultServer) sessionHandler(session ssh.Session) {
}
}()

localUser, err := user.Lookup(session.User())
localUser, err := userNameLookup(session.User())
if err != nil {
_, err = fmt.Fprintf(session, "remote SSH server couldn't find local user %s\n", session.User()) //nolint
err = session.Exit(1)
Expand Down

0 comments on commit ff729f6

Please sign in to comment.