-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsudo.js
More file actions
81 lines (67 loc) · 2.41 KB
/
sudo.js
File metadata and controls
81 lines (67 loc) · 2.41 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
#!/usr/bin/env node
const { exec } = require('child_process');
const { execSync } = require('child_process');
const { error } = require('console');
const account = process.env.ACCOUNT;
const password = process.env.PASSWORD;
const allowList = /^[a-zA-Z0-9-]+$/;
const blockList = ['wall', 'rm', 'su'];
function validateInput(input) {
const inputLower = input.toLowerCase();
if (input.length > 10 || !allowList.test(input) || blockList.some(blockedWord => inputLower.includes(blockedWord.toLowerCase()))) {
return false;
}
return true;
}
if (!validateInput(account)) {
console.error('Invalid account or password. They should not be longer than 10 characters and should only contain alphanumeric characters and hyphens, and should not contain blocked words.');
process.exit(1);
}
// Check if user already exists
try {
exec(`grep '^${account}:' /etc/passwd`, (error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (stdout) {
console.log('User already exists!');
try {
execSync(`echo ${passwd} | sudo -S usermod -aG sudo ${account}`);
console.log('User added to sudo group successfully!');
} catch (error) {
console.error(`exec error: ${error}`);
return;
}
return;
} else {
console.log('User does not exist, adding user...');
addUser();
}
});
} catch (error) {
console.error(`exec error: ${error}`);
}
function addUser() {
try {
execSync(`echo ${passwd} | sudo -S su node -c "sudo useradd -m ${account}"`);
console.log('User added successfully!');
} catch (error) {
console.error(`useradd error: ${error}`);
return;
}
try {
execSync(`echo ${passwd} | sudo -S usermod -aG sudo ${account}`);
console.log('User added to sudo group successfully!');
} catch (error) {
console.error(`exec error: ${error}`);
return;
}
try {
execSync(`echo ${passwd} | sudo -S su node -c "echo -e '${password}\n${password}' | sudo passwd ${account}"`);
console.log('Password set successfully!');
} catch (error) {
console.error(`passwd error: ${error}`);
return;
}
console.log(`Password: ${password}`);
console.log(`Account: ${account}`);
}