Gets the home directory and shell from /etc/passwd for the current user.
()
| 73 | |
| 74 | // Gets the home directory and shell from /etc/passwd for the current user. |
| 75 | func 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 | /* |
| 103 | Gets 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. |
no test coverage detected