Parses a file in the format of /etc/security/pam_env.conf or ~/.pam_environment. Accepts a path to the file and returns a map of environment variables.
(path string, opts *PamParseOpts)
| 39 | |
| 40 | // Parses a file in the format of /etc/security/pam_env.conf or ~/.pam_environment. Accepts a path to the file and returns a map of environment variables. |
| 41 | func ParseEnvironmentConfFile(path string, opts *PamParseOpts) (map[string]string, error) { |
| 42 | rtn := make(map[string]string) |
| 43 | file, err := os.Open(path) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | defer file.Close() |
| 48 | if opts == nil { |
| 49 | opts, err = ParsePasswd() |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | } |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | scanner := bufio.NewScanner(file) |
| 58 | for scanner.Scan() { |
| 59 | line := scanner.Text() |
| 60 | key, val := parseEnvironmentConfLine(line) |
| 61 | |
| 62 | // Fall back to ParseEnvironmentLine if ParseEnvironmentConfLine fails |
| 63 | if key == "" { |
| 64 | key, val = parseEnvironmentLine(line) |
| 65 | if key == "" { |
| 66 | continue |
| 67 | } |
| 68 | } |
| 69 | rtn[key] = replaceHomeAndShell(val, opts.Home, opts.Shell) |
| 70 | } |
| 71 | return rtn, nil |
| 72 | } |
| 73 | |
| 74 | // Gets the home directory and shell from /etc/passwd for the current user. |
| 75 | func ParsePasswd() (*PamParseOpts, error) { |