diff --git a/lib/agent/actions/nativepermission/index.js b/lib/agent/actions/nativepermission/index.js deleted file mode 100644 index 842bce18e..000000000 --- a/lib/agent/actions/nativepermission/index.js +++ /dev/null @@ -1,41 +0,0 @@ -const { EventEmitter } = require('events'); -const common = require('../../../common'); -const socket = require('../../socket'); -const permissionFile = require('../../../utils/permissionfile'); - -// eslint-disable-next-line camelcase -const { get_location } = require('../../providers/geo/darwin'); - -let emitter; -const logger = common.logger.prefix('osquery'); -const osName = process.platform.replace('win32', 'windows').replace('darwin', 'mac'); -const permissionFunction = 'check-location-perms'; -const done = (id, err) => { - if (!emitter) emitter = new EventEmitter(); - emitter.emit('end', id, err); -}; - -// eslint-disable-next-line consistent-return -exports.start = (id, opts, cb) => { - if (osName.localeCompare('mac') !== 0) return cb(new Error('Action only allowed on MacOS')); - // eslint-disable-next-line consistent-return - socket.writeMessage(permissionFunction, () => { - cb(); - const permissionNative = permissionFile.getData('nativeLocation'); - if (permissionNative.localeCompare('false') !== 0 && permissionNative.localeCompare('true') !== 0) { - try { - get_location((err) => { - done(id, err); - }); - } catch (ex) { - done(id, new Error(ex.message)); - } - } else { - done(id, null); - } - }); -}; - -exports.stop = () => { - logger.info('inside stop'); -}; diff --git a/lib/agent/actions/request_permission/index.js b/lib/agent/actions/request_permission/index.js new file mode 100644 index 000000000..03896e789 --- /dev/null +++ b/lib/agent/actions/request_permission/index.js @@ -0,0 +1,68 @@ +const { EventEmitter } = require('events'); +const socket = require('../../socket'); +const geo = require('../../providers/geo'); +const location = require('../../triggers/location'); +const permissionFile = require('../../../utils/permissionfile'); + +const osName = process.platform.replace('win32', 'windows').replace('darwin', 'mac'); +const permissionFunction = 'check-location-perms'; + +let emitter; +const done = (id, err) => { + if (!emitter) emitter = new EventEmitter(); + emitter.emit('end', id, err); +}; +/** + * Requests native permission on MacOS. + * + * @param {function} cb - The callback function to be executed after the permission is requested. + * @return {void} + */ +// eslint-disable-next-line consistent-return +const requestNativePermission = (cb) => { + if (osName.localeCompare('mac') !== 0) return cb(new Error('Action only allowed on MacOS')); + // eslint-disable-next-line consistent-return + socket.writeMessage(permissionFunction, () => { + const permissionNative = permissionFile.getData('nativeLocation'); + if (permissionNative.localeCompare('false') !== 0 && permissionNative.localeCompare('true') !== 0) { + try { + geo.get_location_native((err) => { + cb(err); + }); + } catch (ex) { + cb(new Error(ex.message)); + } + } else { + cb(null); + } + }); +}; +/** + * Starts the process based on the provided ID and options. + * + * @param {type} id - The ID for the process + * @param {type} opts - The options for the process + * @param {type} cb - Callback function + * @return {type} description of return value + */ +// eslint-disable-next-line consistent-return +exports.start = (id, opts, cb) => { + // eslint-disable-next-line consistent-return + cb(); + if (!opts.name) return done(id, new Error('Invalid permission name')); + switch (opts.name) { + case 'native_location': + requestNativePermission((err) => { + setTimeout(() => { + location.start(); + }, 1000 * 60 * 3); + done(id, err); + }); + break; + default: + done(id, new Error('Invalid permission name')); + } +}; + +exports.stop = () => { +}; diff --git a/lib/agent/providers/geo/index.js b/lib/agent/providers/geo/index.js index 4f82153b7..bb0b564b9 100644 --- a/lib/agent/providers/geo/index.js +++ b/lib/agent/providers/geo/index.js @@ -22,6 +22,10 @@ exports.get_location = (cb) => { hooks.trigger('get_location', cb); }; +exports.get_location_native = (cb) => { + strategies.native((err) => { cb(err); }); +}; + const strategyCallback = (err, res, cb) => { if (err) { logError(err, current);