MCPcopy
hub / github.com/wavetermdev/waveterm / ParsePasswd

Function ParsePasswd

pkg/util/pamparse/pamparse.go:75–100  ·  view source on GitHub ↗

Gets the home directory and shell from /etc/passwd for the current user.

()

Source from the content-addressed store, hash-verified

73
74// Gets the home directory and shell from /etc/passwd for the current user.
75func ParsePasswd() (*PamParseOpts, error) {
76 file, err := os.Open("/etc/passwd")
77 if err != nil {
78 return nil, err
79 }
80 defer file.Close()
81 userPrefix := fmt.Sprintf("%s:", os.Getenv("USER"))
82 scanner := bufio.NewScanner(file)
83 for scanner.Scan() {
84 line := scanner.Text()
85 if strings.HasPrefix(line, userPrefix) {
86 parts := strings.Split(line, ":")
87 if len(parts) < 7 {
88 return nil, fmt.Errorf("invalid passwd entry: insufficient fields")
89 }
90 return &PamParseOpts{
91 Home: parts[5],
92 Shell: parts[6],
93 }, nil
94 }
95 }
96 if err := scanner.Err(); err != nil {
97 return nil, fmt.Errorf("error reading passwd file: %w", err)
98 }
99 return nil, nil
100}
101
102/*
103Gets the home directory and shell from /etc/passwd for the current user and returns a map of environment variables from /etc/security/pam_env.conf or ~/.pam_environment.

Callers 2

ParseEnvironmentConfFileFunction · 0.85
ParsePasswdSafeFunction · 0.85

Calls 4

OpenMethod · 0.80
ScanMethod · 0.80
ErrMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected