2018-12-19 11:47:15 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2021-05-01 11:21:09 -07:00
|
|
|
getRemoteSupport,
|
2021-08-12 16:27:31 -07:00
|
|
|
enableRemoteSupport,
|
|
|
|
|
|
|
|
|
|
_sshInfo: sshInfo
|
2018-12-19 11:47:15 -08:00
|
|
|
};
|
|
|
|
|
|
2021-08-12 16:27:31 -07:00
|
|
|
const assert = require('assert'),
|
2019-10-22 11:08:19 -07:00
|
|
|
BoxError = require('./boxerror.js'),
|
2019-07-26 10:10:14 -07:00
|
|
|
constants = require('./constants.js'),
|
2019-12-16 14:06:55 -08:00
|
|
|
eventlog = require('./eventlog.js'),
|
2018-12-19 11:47:15 -08:00
|
|
|
path = require('path'),
|
2019-07-26 10:04:54 -07:00
|
|
|
paths = require('./paths.js'),
|
2020-06-25 11:07:49 -07:00
|
|
|
safe = require('safetydance'),
|
2019-12-16 14:06:55 -08:00
|
|
|
shell = require('./shell.js');
|
2018-12-19 11:47:15 -08:00
|
|
|
|
2019-05-21 09:44:58 -07:00
|
|
|
// the logic here is also used in the cloudron-support tool
|
2019-10-29 15:46:33 -07:00
|
|
|
const AUTHORIZED_KEYS_CMD = path.join(__dirname, 'scripts/remotesupport.sh');
|
|
|
|
|
|
|
|
|
|
function sshInfo() {
|
|
|
|
|
let filePath, user;
|
|
|
|
|
|
|
|
|
|
if (constants.TEST) {
|
|
|
|
|
filePath = path.join(paths.baseDir(), 'authorized_keys');
|
|
|
|
|
user = process.getuid();
|
|
|
|
|
} else {
|
2022-03-30 14:27:39 -07:00
|
|
|
filePath = `/home/${constants.SUPPORT_USERNAME}/.ssh/authorized_keys`;
|
|
|
|
|
user = constants.SUPPORT_USERNAME;
|
2019-10-29 15:46:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { filePath, user };
|
|
|
|
|
}
|
2018-12-19 11:47:15 -08:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
async function getRemoteSupport() {
|
|
|
|
|
const [error, stdoutResult] = await safe(shell.promises.sudo('support', [ AUTHORIZED_KEYS_CMD, 'is-enabled', sshInfo().filePath ], {}));
|
|
|
|
|
if (error) throw new BoxError(BoxError.FS_ERROR, error);
|
2018-12-19 11:47:15 -08:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
return stdoutResult.trim() === 'true';
|
2018-12-19 11:47:15 -08:00
|
|
|
}
|
|
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
async function enableRemoteSupport(enable, auditSource) {
|
2019-12-16 14:06:55 -08:00
|
|
|
assert.strictEqual(typeof enable, 'boolean');
|
|
|
|
|
assert.strictEqual(typeof auditSource, 'object');
|
2018-12-19 11:47:15 -08:00
|
|
|
|
2021-08-12 16:27:31 -07:00
|
|
|
const si = sshInfo();
|
2021-08-18 15:54:53 -07:00
|
|
|
const [error] = await safe(shell.promises.sudo('support', [ AUTHORIZED_KEYS_CMD, enable ? 'enable' : 'disable', si.filePath, si.user ], {}));
|
|
|
|
|
if (error) throw new BoxError(BoxError.FS_ERROR, error);
|
2019-12-16 14:06:55 -08:00
|
|
|
|
2021-08-18 15:54:53 -07:00
|
|
|
await eventlog.add(eventlog.ACTION_SUPPORT_SSH, auditSource, { enable });
|
2018-12-19 11:47:15 -08:00
|
|
|
}
|