Skip to content

Commit

Permalink
Merge branch 'master' into bumps-version-1.9.17
Browse files Browse the repository at this point in the history
  • Loading branch information
javo authored Jan 26, 2022
2 parents 57c8478 + e9ee76d commit b81d00e
Show file tree
Hide file tree
Showing 28 changed files with 3,632 additions and 112 deletions.
2 changes: 1 addition & 1 deletion lib/agent/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var start = function(type, id, name, opts, cb){
logger.info('Failed: ' + name + ' -> ' + err.message)
if (type == 'action') {
hooks.trigger('error', err);
return hooks.trigger('action', 'failed', name, err);
return hooks.trigger('action', 'failed', id, name, opts, err);
}
return hooks.trigger('error', err);
}
Expand Down
15 changes: 13 additions & 2 deletions lib/agent/actions/alarm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ var fs = require('fs'),
os_name = process.platform.replace('darwin', 'mac').replace('win32', 'windows'),
commands = require('./' + os_name),
Emitter = require('events').EventEmitter,
system = require('./../../common').system;
common = require('./../../common'),
logger = common.logger.prefix('actions'),
system = common.system;

var child,
emitter,
raise_interval;

exports.start = function(id, options, cb) {

var error,
self = this,
returned = 0;
Expand All @@ -38,6 +39,16 @@ exports.start = function(id, options, cb) {
}

var raise_volume = function(cb) {
if (os_name == 'linux'){
const unmuteRaiseCommands = commands.raise_volume.split('&&');
if(unmuteRaiseCommands.length < 2) return;
system.run_as_logged_user(unmuteRaiseCommands[0], [], (err) => {
if (err)
logger.info(`ERROR while raising volume: ${err}`);
system.run_as_logged_user(unmuteRaiseCommands[1], [], cb);
});
return;
}
exec(commands.raise_volume, cb);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/agent/actions/alarm/linux.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exports.play = 'mpg123';
exports.raise_volume = 'pactl set-sink-mute 0 0 && pactl set-sink-volume 0 65536';
exports.raise_volume = 'pactl set-sink-mute @DEFAULT_SINK@ 0&&pactl set-sink-volume @DEFAULT_SINK@ 100%';
9 changes: 9 additions & 0 deletions lib/agent/actions/factoryreset/bin/factory-reset.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$namespaceName = "root\cimv2\mdm\dmmap"
$className = "MDM_RemoteWipe"
$methodName = "doWipeMethod"
$session = New-CimSession
$params = New-Object Microsoft.Management.Infrastructure.CimMethodParametersCollection
$param = [Microsoft.Management.Infrastructure.CimMethodParameter]::Create("param", "", "String", "In")
$params.Add($param)
$instance = Get-CimInstance -Namespace $namespaceName -ClassName $className -Filter "ParentID='./Vendor/MSFT' and InstanceID='RemoteWipe'"
$session.InvokeMethod($namespaceName, $instance, $methodName, $params)
108 changes: 108 additions & 0 deletions lib/agent/actions/factoryreset/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var os = require('os'),
path = require('path'),
join = path.join,
Emitter = require('events').EventEmitter,
common = require('../../common'),
logger = common.logger.prefix('factoryreset'),
token = require('./../../token'),
system = require('./../../../system'),
errors = require('./../../errors').status;

var emitter,
action,
node_bin = join(system.paths.current, 'bin', 'node'),
file_factory_reset = join(system.paths.current, 'lib', 'agent','actions','factoryreset','bin','factory-reset.ps1');


var time_execution = () => {
var now = new Date();
now.setMinutes(now.getMinutes() + 2); //add two minuts
now = new Date(now);
datetext = now.toTimeString();
var time = datetext.split(' ')[0];
return time;

}

exports.start = function(id, opts, cb) {
var os_name = os.platform().replace('darwin', 'mac').replace('win32', 'windows');

if (os_name != 'windows'){
let error = new Error('Action only allowed on Windows 1O');
error.code = 3;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error);
}

var opts = opts || {};
if (!opts || id == undefined || opts.token == undefined || opts.target == undefined){
let error = new Error('The factory reset data is not valid');
error.code = 2;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error);
}

var finished = function(err, out) {
logger.info('Factory Reset Process initialized!');
var output = null;

if (!err) {
output = {};
if (!out) return emitter.emit('end', id);

if(out && out.error){
output.data = 1;//error
output.message = out.message;
logger.warn("Error executing Factory reset : " + out.message );
}
else{
output.data = 0;// factory reset ok
output.message = "OK";
}

}
if (!emitter) return;
return emitter.emit('end', id, err, output);
}

token.post_token({action : opts.target, token : opts.token, id : opts.messageID },(err) => {

console.log("error token->" , err)
if (err) {
let error = err;
error.code = 5;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error);
}

var data = {
key: "device-key",
token: "token",
logged: false,
dirs : [file_factory_reset, time_execution(), process.arch]
}

action = 'factory-reset';

emitter = new Emitter;
cb(null, emitter);

system.spawn_as_admin_user(node_bin, data, function(err, child) {
if(err){
logger.info('Error executing Factory Reset :' + JSON.stringify(err));
}
if (typeof child == 'function') { // only for windows
child(action, data, finished);
} else {
let error = new Error('Admin service not available');
error.code = 4;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error);
}
})
})
}

exports.stop = function(){
emitter = null;
}
Binary file added lib/agent/actions/fullwipe/bin/dskwipe.exe
Binary file not shown.
99 changes: 99 additions & 0 deletions lib/agent/actions/fullwipe/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
var os = require('os'),
path = require('path'),
join = path.join,
Emitter = require('events').EventEmitter,
common = require('../../common'),
logger = common.logger.prefix('fullwipe'),
token = require('./../../token'),
system = require('./../../../system'),
errors = require('./../../errors').status;

var emitter,
action,
node_bin = join(system.paths.current, 'bin', 'node');

exports.timeout = 2 * 60 * 1000;

exports.start = function(id, opts, cb) {
var os_name = os.platform().replace('darwin', 'mac').replace('win32', 'windows');
if (os_name != 'windows'){
let error = new Error('Action only allowed on Windows 1O');
error.code = 3;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error)
}

var opts = opts || {};
if (!opts || id == undefined || opts.token == undefined || opts.target == undefined){
let error = new Error('The factory reset data is not valid');
error.code = 2;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error);
}

var finished = function(err, out) {
logger.info('Full Wipe Process initialized!');
var output = null;

if (!err) {
output = {};
if (!out) return emitter.emit('end', id);

if(out && out.error){
output.data = 1;//error Fullwipe
output.message = out.message;
logger.warn("Error executing FullWipe: " + out.message );
err = new Error('Admin service Error');
err.code = 4;
err.name = errors.find( x => x.status_code == err.code).message;
}
else{
output.data= 0;// full wipe ok
output.message = "OK";
}
}
if (!emitter) return;
return emitter.emit('end', id, err, output);
}

token.post_token({action : opts.target, token : opts.token, id : opts.messageID },(err) => {

if (err) {
let error = err;
error.code = 5;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error);
}

var data = {
key: "device-key",
token: "token",
logged: false,
dirs : []
}

action = 'full-wipe';

emitter = new Emitter;
cb(null, emitter);

system.spawn_as_admin_user(node_bin, data, function(err, child) {

if(err){
logger.info('Error executing Full Wipe :' + JSON.stringify(err));
}
if (typeof child == 'function') { // only for windows
child(action, data, finished);
} else {
let error = new Error('Admin service not available');
error.code = 4;
error.name = errors.find( x => x.status_code == error.code).message;
return cb(error);
}
})
})
}

exports.stop = function(){
emitter = null;
}
3 changes: 2 additions & 1 deletion lib/agent/actions/geofencing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ function refresh_geofences(id, opts, cb) {
fetch_geofences(function(err, res) {
if (err || !res || !res.body) {
emitter.emit('end', id, err);
return cb(new Error("Unable to get geofences from control panel"));
if (cb && typeof(cb) == 'function') return cb(new Error("Unable to get geofences from control panel"));
else return null;
}

var geofences = res.body;
Expand Down
53 changes: 28 additions & 25 deletions lib/agent/actions/logretrieval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//////////////////////////////////////////

var fs = require('fs'),
os = require('os'),
join = require('path').join,
async = require('async'),
archiver = require('archiver'),
Expand All @@ -16,8 +17,11 @@ var fs = require('fs'),
common = require('./../../../common'),
logger = common.logger.prefix('logretrieval'),
paths = common.system.paths,
os_name = process.platform.replace('win32', 'windows').replace('darwin', 'mac'),
keys = require('./../../plugins/control-panel/api/keys');

exports.tmpdir = os_name == 'windows' ? process.env.WINDIR + '\\Temp' : '/tmp';

var config = common.config,
protocol = config.get('control-panel.protocol'),
host = config.get('control-panel.host'),
Expand All @@ -34,6 +38,8 @@ let CONFIG_PATH,
COMMANDS_PATH,
CONF_PATH,
ROTATED_PATH,
WINSVC_LOG,
WINSVC_UPDATER,
LOGS_ZIP_PATH;

var collect_files = (output_file, cb) => {
Expand All @@ -54,10 +60,14 @@ var collect_files = (output_file, cb) => {

let files = [
{ path: CONF_PATH, name: 'prey.conf' },
{ path: LOG_PATH, name: 'prey.log' },
{ path: COMMANDS_PATH, name: 'commands.db' },
{ path: ROTATED_PATH, name: 'prey.log.1.gz' }
]
{ path: LOG_PATH, name: 'prey.log' },
{ path: COMMANDS_PATH, name: 'commands.db' },
{ path: ROTATED_PATH, name: 'prey.log.1.gz' },
...(os.platform() == 'win32' ? [ // Only for windows add admin service logs
{ path: WINSVC_LOG, name: 'winsvc.log' },
{ path: WINSVC_UPDATER, name: 'updater.log' },
]: [])
];

var array = [];

Expand Down Expand Up @@ -122,38 +132,31 @@ exports.upload_zip = (file_path, bytes, cb) => {

logger.info("Log file succesfuly uploaded!");
cb(null);
})
});

})
});
}

var done = (err) => {
var done = (id, err) => {
if (err) logger.info(err);

em.emit('end', err);
em.emit('end', id, err);
}

exports.start = function(id, options, cb) {
CONFIG_PATH = paths.config;
LOG_PATH = paths.log_file;
COMMANDS_PATH = join(CONFIG_PATH, 'commands.db');
CONF_PATH = join(CONFIG_PATH, 'prey.conf');
ROTATED_PATH = join(CONFIG_PATH, 'prey.log.1.gz');
LOGS_ZIP_PATH = join(CONFIG_PATH, 'logs.zip');

// Delete previous zip file if exists
let zip_exists = fs.existsSync(LOGS_ZIP_PATH);
if (zip_exists) fs.unlinkSync(LOGS_ZIP_PATH);
CONFIG_PATH = paths.config;
LOG_PATH = paths.log_file;
COMMANDS_PATH = join(CONFIG_PATH, 'commands.db');
CONF_PATH = join(CONFIG_PATH, 'prey.conf');
ROTATED_PATH = join(CONFIG_PATH, 'prey.log.1.gz');
WINSVC_LOG = join(CONFIG_PATH, 'winsvc.log');
WINSVC_UPDATER = join(CONFIG_PATH, 'updater.log');
LOGS_ZIP_PATH = join(exports.tmpdir, 'logs.zip');

collect_files(LOGS_ZIP_PATH, (err, bytes) => {
if (err) return done(err);
if (err) return done(id, err);

exports.upload_zip(LOGS_ZIP_PATH, bytes, (err) => {
if (err) return done(err);

fs.unlink(LOGS_ZIP_PATH, (err) => {
return done(err);
});
return done(id, err);
});
});

Expand Down
Loading

0 comments on commit b81d00e

Please sign in to comment.