-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (51 loc) · 1.22 KB
/
index.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
// Requirements
var util = require('util'),
child = require('child_process');
// The namespace
var user = module.exports;
// Checks if a user exists
user.exists = function(username, done) {
// Spawn id command
// ex: $ id -u <username>
return child.spawn('id', ['-u', username])
.on('close', function(code) {
done(!code);
});
};
// Creates a user
user.create = function(username, pass, opts, done) {
// Process optional args
if (typeof pass === 'function') {
done = pass;
pass = null;
opts = [];
} else if (typeof opts === 'function') {
done = opts;
opts = [];
}
// Push on username
opts.push(username);
// Spawn useradd command
// ex: $ useradd <user>
return child.spawn('useradd', opts)
.on('close', function(code) {
if (code) {
return done('Failed to create user. Exit code: ' + code);
}
if (!pass) {
return done();
}
// Set the user password
user.passwd(username, pass, done);
});
};
// Sets a password for a user
user.passwd = function(username, pass, done) {
return child.exec(util.format('echo %s:%s | chpasswd', username, pass))
.on('close', function(code) {
if (code) {
return done('Failed to set password for user. Exit code: ' + code);
}
done();
});
};