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 18 : function parse_passwd_content(content: string): PasswdUserInfo[] {
24 3 : if (!content) {
25 3 : console.warn("Couldn't read /etc/passwd");
26 3 : return [];
27 3 : }
28 :
29 18 : const ret = [];
30 18 : const lines = content.split('\n');
31 :
32 18 : for (let i = 0; i < lines.length; i++) {
33 18 : if (!lines[i])
34 18 : continue;
35 18 : const column = lines[i].split(':');
36 18 : ret.push({
37 18 : name: column[0],
38 18 : password: column[1],
39 18 : uid: parseInt(column[2], 10),
40 18 : gid: parseInt(column[3], 10),
41 18 : gecos: (column[4] || '').replace(/,*$/, ''),
42 3 : home: column[5] || '',
43 3 : shell: column[6] || '',
44 18 : });
45 18 : }
46 :
47 18 : return ret;
48 18 : }
49 :
50 19 : export const etc_passwd_syntax = {
51 19 : parse: parse_passwd_content
52 19 : };
53 :
54 18 : 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 3 : content = (content || "").trim();
57 3 : if (!content) {
58 3 : console.warn("Couldn't read /etc/group");
59 3 : return [];
60 3 : }
61 :
62 18 : const ret = [];
63 18 : const lines = content.split('\n');
64 :
65 18 : for (let i = 0; i < lines.length; i++) {
66 18 : if (!lines[i])
67 18 : continue;
68 18 : const column = lines[i].split(':');
69 18 : ret.push({
70 18 : name: column[0],
71 18 : password: column[1],
72 18 : gid: parseInt(column[2], 10),
73 18 : userlist: column[3].split(','),
74 18 : });
75 18 : }
76 :
77 18 : return ret;
78 18 : }
79 :
80 19 : export const etc_group_syntax = {
81 19 : parse: parse_group_content
82 19 : };
83 :
84 18 : function parse_shells_content(content: string) {
85 3 : content = (content || "").trim();
86 3 : if (!content) {
87 3 : console.warn("Couldn't read /etc/shells");
88 3 : return [];
89 3 : }
90 :
91 18 : const lines = content.split('\n');
92 :
93 18 : return lines.filter(line => !line.includes("#") && line.trim());
94 18 : }
95 :
96 19 : export const etc_shells_syntax = {
97 19 : parse: parse_shells_content
98 19 : };
|