Files
cloudron-box/src/support.js

56 lines
1.8 KiB
JavaScript
Raw Normal View History

'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
};
2021-08-12 16:27:31 -07:00
const assert = require('assert'),
2019-10-22 11:08:19 -07:00
BoxError = require('./boxerror.js'),
constants = require('./constants.js'),
2019-12-16 14:06:55 -08:00
eventlog = require('./eventlog.js'),
path = require('path'),
2019-07-26 10:04:54 -07:00
paths = require('./paths.js'),
safe = require('safetydance'),
2019-12-16 14:06:55 -08:00
shell = require('./shell.js');
// 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 if (safe.fs.existsSync('/home/ubuntu')) { // yellowtent user won't have access to anything deeper
2019-10-29 15:46:33 -07:00
filePath = '/home/ubuntu/.ssh/authorized_keys';
user = 'ubuntu';
} else {
filePath = '/root/.ssh/authorized_keys';
2019-10-29 15:46:33 -07:00
user = 'root';
}
return { filePath, user };
}
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);
2021-08-18 15:54:53 -07:00
return stdoutResult.trim() === 'true';
}
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');
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 });
}