-
Notifications
You must be signed in to change notification settings - Fork 4
/
grant_access_to_last_attempt.js
executable file
·91 lines (79 loc) · 3.04 KB
/
grant_access_to_last_attempt.js
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
#!/usr/bin/env node
/*
Running this script will add the last attempted login to the access list
*/
var fs = require('fs');
var spawn = require('child_process').spawn;
var split = require('split2');
var through = require('through2');
var strftime = require('strftime');
if(!fs.existsSync('/var_rw/failed_attempts')) {
console.log("It appears that there have been no failed attempts (the /var_rw/failed_attempts file doesn't exist)");
process.exit(1);
}
if(process.argv.length < 3) {
console.log("Usage: "+process.argv[1]+" <name of new user>")
process.exit(1);
}
var attempts = fs.readFileSync('/var_rw/failed_attempts', {encoding: 'utf8'}).split("\n");
var i, lastAttempt;
for(i=attempts.length-1; i >= 0; i--) {
lastAttempt = attempts[i];
if(lastAttempt.replace(/^\s+$/g, '').length > 4) {
lastAttempt = JSON.parse(lastAttempt);
if (/^less than/.test(lastAttempt.code)) {
console.error('last failed attempt was a bad read');
process.exit(1);
}
break;
}
}
askPrompts([
{ key: 'announce', msg: 'How should the IRC bot announce this user?' },
{ key: 'contactInfo', msg: 'What is their email and/or phone number?' },
{ key: 'collective', msg: 'Which collective are they a member of?' },
{ key: 'addedBy', msg: 'Who are you, the person granting access?' }
], addUser);
function askPrompts (prompts, cb) {
var current = prompts.shift();
var answers = {};
process.stdout.write(current.msg + ' ');
process.stdin.pipe(split()).pipe(through(function (buf, enc, next) {
var line = buf.toString();
if (!/\S/.test(line)) {
process.stdout.write(current.msg + ' ');
return next();
}
answers[current.key] = line;
if (prompts.length > 0) {
current = prompts.shift();
process.stdout.write(current.msg + ' ');
next();
} else cb(answers);
}));
}
function addUser (answers) {
var comment = '# ' + process.argv.slice(2).join(' ') + " | added on " + new Date() + process.env.SSH_CLIENT + process.env.TERM + " user: " + process.env.USER + "\n";
var name = process.argv.slice(2).join(' ')
var comment = '#ANNOUNCE ' + JSON.stringify(answers.announce) + ' ' + name
+ ' ' + answers.contactInfo
+ ' ' + answers.collective
+ ' | added by ' + answers.addedBy
+ ' (' + process.env.USER
+ ') on ' + strftime('%FT%T')
+ '\n';
// #ANNOUNCE "gumby" ben [email protected] / commons wg | added by jake Wed Feb 10 22:29:19 PST 2016
var entry = comment + lastAttempt.code + "\n";
try {
spawn('/bin/mount',[ '-o','remount,rw','/' ])
.stdout.on('end', function(){
fs.appendFileSync(__dirname + '/access_control_list', entry);
spawn('/usr/local/bin/roroot');
})
} catch(e) {
console.log("Error: Hm. Are you sure you have permission to write to the access_control_list file?")
process.exit(1);
}
console.log("Added: \n" + entry);
process.stdin.destroy();
}