sync/src/setuid.js

46 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-02-21 08:13:55 +00:00
var Config = require("./config");
var fs = require("fs");
var path = require("path");
2015-09-03 06:00:45 +00:00
var execSync = require("child_process").execSync;
var needPermissionsFixed = [
path.join(__dirname, "..", "chanlogs"),
path.join(__dirname, "..", "chandump"),
path.join(__dirname, "..", "google-drive-subtitles")
];
function fixPermissions(user, group) {
var uid = resolveUid(user);
var gid = resolveGid(group);
needPermissionsFixed.forEach(function (dir) {
if (fs.existsSync(dir)) {
fs.chownSync(dir, uid, gid);
}
});
}
2015-02-21 08:13:55 +00:00
function resolveUid(user) {
return parseInt(execSync('id -u ' + user), 10);
2015-09-03 06:00:45 +00:00
}
function resolveGid(group) {
return parseInt(execSync('id -g ' + group), 10);
2015-09-03 06:00:45 +00:00
}
2015-02-21 08:13:55 +00:00
if (Config.get("setuid.enabled")) {
setTimeout(function() {
try {
2015-09-03 06:00:45 +00:00
fixPermissions(Config.get("setuid.user"), Config.get("setuid.group"));
console.log("Old User ID: " + process.getuid() + ", Old Group ID: " +
process.getgid());
2015-02-21 08:13:55 +00:00
process.setgid(Config.get("setuid.group"));
process.setuid(Config.get("setuid.user"));
console.log("New User ID: " + process.getuid() + ", New Group ID: "
+ process.getgid());
2015-02-21 08:13:55 +00:00
} catch (err) {
2015-09-03 06:00:45 +00:00
console.log("Error setting uid: " + err.stack);
2015-02-21 08:13:55 +00:00
process.exit(1);
}
}, (Config.get("setuid.timeout")));
2015-02-21 08:13:55 +00:00
};