Debian Codegraph @

pkg/lib/pam_user_parser.ts

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