Line data Source code
1 : /*
2 : * Copyright (C) 2013 Red Hat, Inc.
3 : * SPDX-License-Identifier: LGPL-2.1-or-later
4 : */
5 :
6 : export interface PamCommon {
7 : name: string,
8 : password: string,
9 : gid: number,
10 : }
11 :
12 : export interface PasswdUserInfo extends PamCommon {
13 : uid: number,
14 : gecos: string,
15 : home: string,
16 : shell: string,
17 : }
18 :
19 : export interface EtcGroupInfo extends PamCommon {
20 : userlist: string[],
21 : }
22 :
23 19 : function parse_passwd_content(content: string): PasswdUserInfo[] {
24 4 : if (!content) {
25 4 : console.warn("Couldn't read /etc/passwd");
26 4 : return [];
27 4 : }
28 :
29 19 : const ret = [];
30 19 : const lines = content.split('\n');
31 :
32 19 : for (let i = 0; i < lines.length; i++) {
33 19 : if (!lines[i])
34 19 : continue;
35 19 : const column = lines[i].split(':');
36 19 : ret.push({
37 19 : name: column[0],
38 19 : password: column[1],
39 19 : uid: parseInt(column[2], 10),
40 19 : gid: parseInt(column[3], 10),
41 19 : gecos: (column[4] || '').replace(/,*$/, ''),
42 4 : home: column[5] || '',
43 4 : shell: column[6] || '',
44 19 : });
45 19 : }
46 :
47 19 : return ret;
48 19 : }
49 :
50 19 : export const etc_passwd_syntax = {
51 19 : parse: parse_passwd_content
52 19 : };
53 :
54 19 : function parse_group_content(content: string): EtcGroupInfo[] {
55 : // /etc/group file is used to set only secondary groups of users. The primary group is saved in /etc/passwd-
56 4 : content = (content || "").trim();
57 4 : if (!content) {
58 4 : console.warn("Couldn't read /etc/group");
59 4 : return [];
60 4 : }
61 :
62 19 : const ret = [];
63 19 : const lines = content.split('\n');
64 :
65 19 : for (let i = 0; i < lines.length; i++) {
66 19 : if (!lines[i])
67 19 : continue;
68 19 : const column = lines[i].split(':');
69 19 : ret.push({
70 19 : name: column[0],
71 19 : password: column[1],
72 19 : gid: parseInt(column[2], 10),
73 19 : userlist: column[3].split(','),
74 19 : });
75 19 : }
76 :
77 19 : return ret;
78 19 : }
79 :
80 19 : export const etc_group_syntax = {
81 19 : parse: parse_group_content
82 19 : };
83 :
84 19 : function parse_shells_content(content: string) {
85 4 : content = (content || "").trim();
86 4 : if (!content) {
87 4 : console.warn("Couldn't read /etc/shells");
88 4 : return [];
89 4 : }
90 :
91 19 : const lines = content.split('\n');
92 :
93 19 : return lines.filter(line => !line.includes("#") && line.trim());
94 19 : }
95 :
96 19 : export const etc_shells_syntax = {
97 19 : parse: parse_shells_content
98 19 : };
|