2017-04-25 22:49:31 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-09-27 19:32:35 +00:00
|
|
|
const log = require('./log');
|
2019-07-26 15:05:49 +00:00
|
|
|
const config = require('./config');
|
2017-04-25 22:49:31 +00:00
|
|
|
|
2018-12-26 03:38:02 +00:00
|
|
|
const fs = require('fs-extra-promise');
|
2017-04-25 22:49:31 +00:00
|
|
|
|
|
|
|
const tryRequire = require('try-require');
|
|
|
|
const posix = tryRequire('posix');
|
|
|
|
|
2019-03-24 12:27:56 +00:00
|
|
|
// process.getuid and process.getgid are not supported on Windows
|
|
|
|
process.getuid = process.getuid || (() => 100);
|
|
|
|
process.getgid = process.getuid || (() => 100);
|
|
|
|
|
2018-04-02 09:58:32 +00:00
|
|
|
function _getConfigUidGid(userKey, groupKey, defaultUid, defaultGid) {
|
2017-04-27 22:25:05 +00:00
|
|
|
let uid = defaultUid;
|
|
|
|
let gid = defaultGid;
|
2017-04-25 22:49:31 +00:00
|
|
|
|
2017-04-27 20:35:53 +00:00
|
|
|
if (posix) {
|
|
|
|
try {
|
2018-04-02 09:58:32 +00:00
|
|
|
if (config[userKey]) {
|
|
|
|
uid = posix.getpwnam(config[userKey]).uid;
|
2017-04-27 20:35:53 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2018-04-02 09:58:32 +00:00
|
|
|
log.info('PrivilegeHelpers', 'Failed to resolve user id "%s"', config[userKey]);
|
2017-04-27 20:35:53 +00:00
|
|
|
}
|
2017-04-25 22:49:31 +00:00
|
|
|
|
2017-04-27 20:35:53 +00:00
|
|
|
try {
|
2018-04-02 09:58:32 +00:00
|
|
|
if (config[groupKey]) {
|
|
|
|
gid = posix.getpwnam(config[groupKey]).gid;
|
2017-04-27 20:35:53 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2018-04-02 09:58:32 +00:00
|
|
|
log.info('PrivilegeHelpers', 'Failed to resolve group id "%s"', config[groupKey]);
|
2017-04-27 20:35:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.info('PrivilegeHelpers', 'Posix module not installed. Cannot resolve uid/gid');
|
2017-04-25 22:49:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 20:35:53 +00:00
|
|
|
return { uid, gid };
|
|
|
|
}
|
2017-04-25 22:49:31 +00:00
|
|
|
|
2017-04-27 20:35:53 +00:00
|
|
|
function getConfigUidGid() {
|
2018-04-02 09:58:32 +00:00
|
|
|
return _getConfigUidGid('user', 'group', process.getuid(), process.getgid());
|
2017-04-27 20:35:53 +00:00
|
|
|
}
|
2017-04-25 22:49:31 +00:00
|
|
|
|
2017-04-27 20:35:53 +00:00
|
|
|
function getConfigROUidGid() {
|
2018-04-02 09:58:32 +00:00
|
|
|
const rwIds = getConfigUidGid();
|
|
|
|
return _getConfigUidGid('roUser', 'roGroup', rwIds.uid, rwIds.gid);
|
2017-04-25 22:49:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-27 20:35:53 +00:00
|
|
|
function ensureMailtrainOwner(file, callback) {
|
|
|
|
const ids = getConfigUidGid();
|
2019-04-22 09:41:37 +00:00
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
fs.chown(file, ids.uid, ids.gid, callback);
|
|
|
|
} else {
|
|
|
|
return fs.chownAsync(file, ids.uid, ids.gid);
|
|
|
|
}
|
2017-04-27 20:35:53 +00:00
|
|
|
}
|
2017-04-25 22:49:31 +00:00
|
|
|
|
2019-04-22 09:41:37 +00:00
|
|
|
async function ensureMailtrainDir(dir) {
|
2019-04-22 13:41:39 +00:00
|
|
|
await fs.ensureDirAsync(dir);
|
2019-04-22 09:41:37 +00:00
|
|
|
await ensureMailtrainOwner(dir);
|
2018-12-26 03:38:02 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 22:49:31 +00:00
|
|
|
function dropRootPrivileges() {
|
|
|
|
if (config.group) {
|
|
|
|
try {
|
|
|
|
process.setgid(config.group);
|
|
|
|
log.info('PrivilegeHelpers', 'Changed group to "%s" (%s)', config.group, process.getgid());
|
|
|
|
} catch (E) {
|
|
|
|
log.info('PrivilegeHelpers', 'Failed to change group to "%s" (%s)', config.group, E.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.user) {
|
|
|
|
try {
|
|
|
|
process.setuid(config.user);
|
|
|
|
log.info('PrivilegeHelpers', 'Changed user to "%s" (%s)', config.user, process.getuid());
|
|
|
|
} catch (E) {
|
|
|
|
log.info('PrivilegeHelpers', 'Failed to change user to "%s" (%s)', config.user, E.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
dropRootPrivileges,
|
|
|
|
ensureMailtrainOwner,
|
2018-12-26 03:38:02 +00:00
|
|
|
ensureMailtrainDir,
|
2017-04-27 20:35:53 +00:00
|
|
|
getConfigUidGid,
|
|
|
|
getConfigROUidGid
|
2017-04-25 22:49:31 +00:00
|
|
|
};
|