diff --git a/lib/agent/actions/wipe/index.js b/lib/agent/actions/wipe/index.js index 9d519fb21..89582eded 100644 --- a/lib/agent/actions/wipe/index.js +++ b/lib/agent/actions/wipe/index.js @@ -86,8 +86,7 @@ exports.start = function(opts, cb) { // run it as another process, using impersonation (to avoid permission errors) var spawn = function() { var wipe_opts = [ - '-device-key', keys.get().device.toString(), - '-token', opts.token, + '-token', opts.token, exports.cloud.toString(), exports.directories.toString() ]; @@ -105,13 +104,11 @@ exports.start = function(opts, cb) { if (typeof child == 'function') { // only for windows os_wipe.paths.directories = exports.directories; - wipe.fetch_dirs(items, exports.directories, exports.cloud, (err, dirs_to_wipe) => { + wipe.fetch_dirs(items, exports.directories, exports.cloud, function(err, dirs_to_wipe) { var opts = { + dirs: dirs_to_wipe, token: token, - key: keys.get().device.toString(), - options: { - dirs: dirs_to_wipe - } + key: keys.get().device.toString() }; child('wipe', opts, function(err) { @@ -124,7 +121,7 @@ exports.start = function(opts, cb) { child.stdout.on('data', function(str) { var lines = str.toString().split(/\n/); lines.forEach(function(line) { - if (line.toString().match('Removing file')) { + if (line.toString().match('Removing directory')) { logger.warn(line.trim()); removed++; } else if (line.toString().match('Error while removing dir')) { @@ -148,14 +145,14 @@ exports.start = function(opts, cb) { } var finished = function(err, service) { - logger.warn('Process finished! ' + (service ? '' : removed + ' file(s) removed.')); + logger.warn('Process finished! ' + (service ? '' : removed + ' dir(s) removed.')); if (!emitter) return; if (service) return emitter.emit('end', err); // if no files were removed, treat that as an error if (!err && removed == 0) { - emitter.emit('end', new Error('No files were removed.')); + emitter.emit('end', new Error('No dirs were removed.')); } else { emitter.emit('end', err, { files_removed: removed }); } @@ -164,7 +161,6 @@ exports.start = function(opts, cb) { emitter = new Emitter; cb(null, emitter); fs.existsSync(node_bin) ? spawn() : finished(new Error('Node binary not present')); - if (!opts.token) return finished(new Error("Security Token necessary")) } diff --git a/lib/agent/actions/wipe/linux/wipe-linux b/lib/agent/actions/wipe/linux/wipe-linux index c4a5dd6b2..e49f538ce 100755 Binary files a/lib/agent/actions/wipe/linux/wipe-linux and b/lib/agent/actions/wipe/linux/wipe-linux differ diff --git a/lib/agent/actions/wipe/mac/wipe-osx b/lib/agent/actions/wipe/mac/wipe-osx index c8f780a18..7e1f24abe 100755 Binary files a/lib/agent/actions/wipe/mac/wipe-osx and b/lib/agent/actions/wipe/mac/wipe-osx differ diff --git a/lib/agent/actions/wipe/windows/wipe-win.exe b/lib/agent/actions/wipe/windows/wipe-win.exe old mode 100755 new mode 100644 index 30ffdcd49..a4d8efc67 Binary files a/lib/agent/actions/wipe/windows/wipe-win.exe and b/lib/agent/actions/wipe/windows/wipe-win.exe differ diff --git a/lib/agent/actions/wipe/wipe.js b/lib/agent/actions/wipe/wipe.js index ac7d4786b..6064c708e 100755 --- a/lib/agent/actions/wipe/wipe.js +++ b/lib/agent/actions/wipe/wipe.js @@ -171,9 +171,15 @@ exports.fetch_dirs = function(items, to_erase, to_kill, cb) { } exports.wipeout = function(cb) { - var cmd = secure_wipe_cmd + '"' + dirs_to_wipe.join(',') + '" ' + credentials.join(' '); - exec(cmd, (err, stdout) => { - return cb(err, stdout) + var output = ''; + dirs_to_wipe.forEach((dir, index) => { + var cmd = secure_wipe_cmd + '"' + dir + '" ' + credentials.join(' '); + + exec(cmd, (err, stdout) => { + output += stdout; + if (index == dirs_to_wipe.length - 1) + return cb(err, output) + }) }) } diff --git a/lib/agent/triggers/hostname/index.js b/lib/agent/triggers/hostname/index.js index 5284c28a5..2d2e88306 100644 --- a/lib/agent/triggers/hostname/index.js +++ b/lib/agent/triggers/hostname/index.js @@ -32,7 +32,6 @@ var check_hostname = () => { } get_current_hostname() - .catch(error => { return done(new Error('Unable to get device current hostname')) }) .then(current_name => { device_keys.exist('hostname', (err, stored) => { if (err) return done(new Error('Error checking stored hostname')); @@ -54,12 +53,12 @@ var check_hostname = () => { } else device_keys.store('hostname', current_name, (err) => { done(err) }); }); }) + .catch(error => { return done(new Error('Unable to get device current hostname')) }) } exports.start = (opts, cb) => { triggers.watch('hostname') - .catch(error => { return cb(error); }) .then((hostname) => { hostname.on('state_changed', (info) => { check_hostname(); @@ -69,6 +68,7 @@ exports.start = (opts, cb) => { check_hostname(); }) + .catch(error => { return cb(error); }) }; exports.stop = () => { diff --git a/lib/agent/triggers/power/index.js b/lib/agent/triggers/power/index.js index 8e3558fd5..2727dd946 100644 --- a/lib/agent/triggers/power/index.js +++ b/lib/agent/triggers/power/index.js @@ -29,7 +29,6 @@ var check_battery_status = () => { exports.start = (opts, cb) => { triggers.watch('power') - .catch(error => { return cb(error); }) .then((power) => { power.on('state_changed', (info) => { setTimeout(check_battery_status, 1500); @@ -43,6 +42,7 @@ exports.start = (opts, cb) => { emitter = new Emitter(); cb(null, emitter); }) + .catch(error => { return cb(error); }) }; exports.stop = () => { diff --git a/lib/agent/utils/custom-dirs.js b/lib/agent/utils/custom-dirs.js index 72c6e4368..f5af39c1d 100644 --- a/lib/agent/utils/custom-dirs.js +++ b/lib/agent/utils/custom-dirs.js @@ -33,7 +33,7 @@ exports.validateCustomDirs = (dirs, should_be_home) => { let directories = [], cloud = {}; - dirs = dirs.split(','); + dirs = dirs.split(/[\n,]+/); dirs.forEach((dir, index) => { dirs[index] = dir.trim(); diff --git a/lib/conf/cli.js b/lib/conf/cli.js index c413a711b..7ed4e06e6 100644 --- a/lib/conf/cli.js +++ b/lib/conf/cli.js @@ -29,6 +29,7 @@ common.program.logfile = system.tempfile_path(logfile); var argv = process.argv.splice(3), Operetta = require('./utils/operetta').Operetta, + exec = require('child_process').exec, cli = new Operetta(argv, 'config'); var account = require('./account'), @@ -46,6 +47,30 @@ var via_npm = function() { return !!(process.env.npm_package_version || process.env.npm_lifecycle_script); } +var restart_client = () => { + var client_pid = function(cb) { + var process_id; + if (os_name == 'windows') { + process_id = `for /f "tokens=2 delims=," %F in ('tasklist /nh /fi "imagename eq node.exe" /fo csv') do @echo %~F`; + } else { + var awk = os_name == 'mac' ? 2 : 1 + process_id = "ps -u prey | grep prx | awk '{print $'" + awk + "'}'"; + } + + exec(process_id, function(err, pid) { + if (err) return cb("Error getting client process id:" + err) + pid = pid.toString().split("\r\n")[0]; + cb(null, pid) + }) + } + + client_pid(function(err, pid) { + if (err || !pid) return; + var restart_cmd = os_name == 'windows' ? 'taskkill /F /PID ' : 'kill -9 '; + exec(restart_cmd + pid); + }) +} + var start = function(cb) { var run = function(scope, command) { @@ -75,6 +100,10 @@ var start = function(cb) { cmd.parameters(['-e', '--email'], 'Email') cmd.parameters(['-p', '--password'], 'Password') run_if_writable(cmd, account.authorize); + + setTimeout(() => { // Wait for the key to be set + restart_client(); + }, 2000) }); sub.command('verify', 'Verifies API & Device keys, optionally saving them to config.', function(cmd) { diff --git a/lib/system/windows/index.js b/lib/system/windows/index.js index 98e75ca18..d37aabfc6 100644 --- a/lib/system/windows/index.js +++ b/lib/system/windows/index.js @@ -16,9 +16,7 @@ var path = require('path'), spawn = cp.spawn, os_name = process.platform.replace('win32', 'windows'); -var LOCALHOST_SERVICE = 'http://127.0.0.1:7739', - OPEN_TIMEOUT = 1000 * 60 * 60; - +var LOCALHOST_ACTION = 'http://127.0.0.1:7739/action'; exports.monitoring_service_go = false; // add windows bin path to env @@ -77,7 +75,7 @@ exports.scan_networks = function(cb) { exports.check_service = function(data, cb) { if (exports.monitoring_service_go) return cb(null, data); - needle.get(LOCALHOST_SERVICE + '/action', function(err, resp) { + needle.get(LOCALHOST_ACTION, function(err, resp) { if (err) return cb(err, data); exports.monitoring_service_go = true; return cb(null, data); @@ -89,14 +87,10 @@ exports.run_as_admin = function(command, opts, cb) { action: command, key: opts.key, token: opts.token, - opts: opts.options - }, - options = { - json: true, - open_timeout: OPEN_TIMEOUT + opts: opts.dirs }; - needle.post(LOCALHOST_SERVICE + '/action', body, options, function(err, resp, body) { + needle.post(LOCALHOST_ACTION, body, { json : true }, function(err, resp, body) { if (err) return cb(err); return cb(null); });