-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnonsudo.js
More file actions
70 lines (59 loc) · 2.09 KB
/
nonsudo.js
File metadata and controls
70 lines (59 loc) · 2.09 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
#!/usr/bin/env node
const { exec } = require('child_process');
const { execSync } = require('child_process');
const account = process.env.ACCOUNT;
const password = process.env.PASSWORD;
const passwd = process.env.PASSWD
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);
}
try {
exec(`grep '^${account}:' /etc/passwd`, (error, stdout, stderr) => {
console.log('Checking if user exists...');
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (stdout) {
console.log('User already exists!');
process.exit(1);
} 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 -s /bin/bash ${account}"`);
console.log('User added successfully!');
console.log(account)
} catch (error) {
console.error(`useradd error: ${error}`);
return;
}
try {
execSync(`echo ${passwd} | sudo -S usermod -aG nonsudoers ${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;
}
}