-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (91 loc) · 2.76 KB
/
index.js
File metadata and controls
111 lines (91 loc) · 2.76 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import process from 'node:process';
import fs, {promises as fsPromises} from 'node:fs';
import execa from 'execa';
function extractDarwin(line) {
const columns = line.split(':');
// Darwin passwd(5)
// 0 name User's login name.
// 1 password User's encrypted password.
// 2 uid User's id.
// 3 gid User's login group id.
// 4 class User's general classification (unused).
// 5 change Password change time.
// 6 expire Account expiration time.
// 7 gecos User's full name.
// 8 home_dir User's home directory.
// 9 shell User's login shell.
return {
username: columns[0],
password: columns[1],
userIdentifier: Number(columns[2]),
groupIdentifier: Number(columns[3]),
fullName: columns[7],
homeDirectory: columns[8],
shell: columns[9],
};
}
function extractLinux(line) {
const columns = line.split(':');
// Linux passwd(5):
// 0 login name
// 1 optional encrypted password
// 2 numerical user ID
// 3 numerical group ID
// 4 user name or comment field
// 5 user home directory
// 6 optional user command interpreter
return {
username: columns[0],
password: columns[1],
userIdentifier: Number(columns[2]),
groupIdentifier: Number(columns[3]),
fullName: columns[4] && columns[4].split(',')[0],
homeDirectory: columns[5],
shell: columns[6],
};
}
const extract = process.platform === 'linux' ? extractLinux : extractDarwin;
function getUser(passwd, username) {
const lines = passwd.split('\n');
const linesCount = lines.length;
let index = 0;
while (index < linesCount) {
const user = extract(lines[index++]);
if (user.username === username || user.userIdentifier === Number(username)) {
return user;
}
}
}
export async function passwdUser(username) {
if (username === undefined) {
if (typeof process.getuid !== 'function') {
// eslint-disable-next-line unicorn/prefer-type-error
throw new Error('Platform not supported');
}
username = process.getuid();
}
if (process.platform === 'linux') {
return getUser(await fsPromises.readFile('/etc/passwd', 'utf8'), username);
}
if (process.platform === 'darwin') {
const {stdout} = await execa('/usr/bin/id', ['-P', username]);
return getUser(stdout, username);
}
throw new Error('Platform not supported');
}
export function passwdUserSync(username) {
if (username === undefined) {
if (typeof process.getuid !== 'function') {
// eslint-disable-next-line unicorn/prefer-type-error
throw new Error('Platform not supported');
}
username = process.getuid();
}
if (process.platform === 'linux') {
return getUser(fs.readFileSync('/etc/passwd', 'utf8'), username);
}
if (process.platform === 'darwin') {
return getUser(execa.sync('/usr/bin/id', ['-P', username]).stdout, username);
}
throw new Error('Platform not supported');
}