guest
Version:
Lets you enable or disable guest account access on Linux, Mac and Windows.
107 lines (81 loc) • 2.49 kB
JavaScript
var exec = require('child_process').exec,
sudo = require('sudoer').exec,
async = require('async'),
user = require('roster');
var bin = '/usr/bin/defaults';
/*
// these allow guests to connect to shared folders
var properties = [
'/Library/Preferences/com.apple.AppleFileServer guestAccess',
'/Library/Preferences/SystemConfiguration/com.apple.smb.server AllowGuestAccess'
];
*/
// this one is for Lion. more info here:
// http://osxdaily.com/2011/10/13/disable-guest-user-account-mac-os-x-10-7-2-login-screen/
// var lion = '/Library/Preferences/com.apple.FindMyMac HideRestartToSafari';
var properties = [
'/Library/Preferences/com.apple.loginwindow GuestEnabled'
]
var guest_user_opts = {
user : 'Guest',
full_name : 'Guest User',
user_id : 201,
group_id : 201,
guest : true
}
function create_user(cb) {
user.create(guest_user_opts, cb);
}
function toggle(enabling, cb) {
var go = function() {
var val = enabling ? 'YES' : 'NO';
var fx = properties.map(function(property) {
return function(cb) {
var args = ' write ' + property + ' -bool ' + val;
sudo(bin + args, cb);
}
})
async.parallel(fx, function(err, results) {
if (err) return cb(err);
exports.check(function(err, enabled) {
if (err) return cb(err);
// 'enabled' var could be false and 'enabling' var too
if (enabled === enabling)
return cb();
return cb(new Error('Failed to enable: ' + str))
});
})
}
user.exists(guest_user_opts.user, function(exists) {
if (enabling && !exists)
return create_user(go);
// should we remove the account as well?
// if (!enabling && exists)
// return delete_user(go);
go()
})
}
exports.check = function(cb) {
var fx = properties.map(function(property) {
return function(cb) {
var cmd = bin + ' read ' + property;
exec(cmd, cb);
}
})
user.exists(guest_user_opts.name, function(exists) {
if (exists) return cb(new Error('Guest user account not found.'))
async.parallel(fx, function(err, results) {
if (err) return cb(err);
// make sure all properties returned a 1
var integers = results.map(function(el) { return parseInt(el[0]) });
var enabled = integers.indexOf(0) === -1;
cb(null, enabled);
})
})
}
exports.enable = function(cb) {
toggle(true, cb);
}
exports.disable = function(cb) {
toggle(false, cb);
}