CyTube/src/setuid.js
Lolcow Admin 29c0df4fcc Fix uid variable name duplication/ambiguity
`uid` is used twice, where it should be `uid` and `gid`, resulting in an attempted execution of something like `id -g 1500` rather than `id -g syncgroup`. These variable names are already confusing due to the nature of the functions, so I made it clear they're strings rather than numeric IDs.
2015-12-11 00:20:40 -05:00

46 lines
1.4 KiB
JavaScript

var Config = require("./config");
var fs = require("fs");
var path = require("path");
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);
}
});
}
function resolveUid(user) {
return parseInt(execSync('id -u ' + user), 10);
}
function resolveGid(group) {
return parseInt(execSync('id -g ' + group), 10);
}
if (Config.get("setuid.enabled")) {
setTimeout(function() {
try {
fixPermissions(Config.get("setuid.user"), Config.get("setuid.group"));
console.log("Old User ID: " + process.getuid() + ", Old Group ID: " +
process.getgid());
process.setgid(Config.get("setuid.group"));
process.setuid(Config.get("setuid.user"));
console.log("New User ID: " + process.getuid() + ", New Group ID: "
+ process.getgid());
} catch (err) {
console.log("Error setting uid: " + err.stack);
process.exit(1);
}
}, (Config.get("setuid.timeout")));
};