summaryrefslogtreecommitdiffstats
path: root/pkg/lib/pam_user_parser.js
blob: 278f7106a59f1fbf528273bc36cd8ac380b23773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * This file is part of Cockpit.
 *
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * Cockpit is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * Cockpit is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
 */

function parse_passwd_content(content) {
    if (!content) {
        console.warn("Couldn't read /etc/passwd");
        return [];
    }

    const ret = [];
    const lines = content.split('\n');

    for (let i = 0; i < lines.length; i++) {
        if (!lines[i])
            continue;
        const column = lines[i].split(':');
        ret.push({
            name: column[0],
            password: column[1],
            uid: parseInt(column[2], 10),
            gid: parseInt(column[3], 10),
            gecos: (column[4] || '').replace(/,*$/, ''),
            home: column[5] || '',
            shell: column[6] || '',
        });
    }

    return ret;
}

export const etc_passwd_syntax = {
    parse: parse_passwd_content
};

function parse_group_content(content) {
    // /etc/group file is used to set only secondary groups of users. The primary group is saved in /etc/passwd-
    content = (content || "").trim();
    if (!content) {
        console.warn("Couldn't read /etc/group");
        return [];
    }

    const ret = [];
    const lines = content.split('\n');

    for (let i = 0; i < lines.length; i++) {
        if (!lines[i])
            continue;
        const column = lines[i].split(':');
        ret.push({
            name: column[0],
            password: column[1],
            gid: parseInt(column[2], 10),
            userlist: column[3].split(','),
        });
    }

    return ret;
}

export const etc_group_syntax = {
    parse: parse_group_content
};

function parse_shells_content(content) {
    content = (content || "").trim();
    if (!content) {
        console.warn("Couldn't read /etc/shells");
        return [];
    }

    const lines = content.split('\n');

    return lines.filter(line => !line.includes("#") && line.trim());
}

export const etc_shells_syntax = {
    parse: parse_shells_content
};