Skip to content
Snippets Groups Projects
Commit 7758379a authored by frekk's avatar frekk
Browse files

moved things around again

parent be2b8643
No related merge requests found
"use strict";
var fs = require("fs");
var cproc = require("child_process");
function getUserIds(username){
var ids = {uid:NaN, gid:NaN};
try{
ids.uid = Number.parseInt(cproc.execFileSync("id", ["-u",username]));
ids.gid = Number.parseInt(cproc.execFileSync("id", ["-g",username]));
if(ids.uid == NaN || ids.gid == NaN)
throw new Error("The `id` command returned an invalid uid or gid");
}
catch(e){
throw new Error(
"Error looking up uid and gid of \"" + username + "\": "
+ e.message
);
}
return ids;
}
function initConfig(configPath, shadowConfig){
var rawConfig, config;
// Try to open config file
console.info("Loading config from \"" + configPath + "\"");
try {rawConfig = fs.readFileSync(configPath);}
catch(e){
throw new Error(
"Could not open config file \"" + configPath + "\": " + e.message
);
}
// Try to parse config file as JSON
try {
// Make config inherit properties from shadowConfig, so it they're
// undefined in the current config file, their defaults take precedence
// from the default config file.
config = Object.assign({}, shadowConfig, JSON.parse(rawConfig));
}
catch(e){
throw new Error(
"\"" + configPath + "\" is not in a valid JSON format: "
+ e.message
);
}
// Verify options provided in config file
try {
// varPath: verify that it exists and is a directory
try {fs.readdirSync(config.varPath);}
catch(e){
throw new Error(
"varPath: the provided directory could not be opened: "
+ e.message
);
}
// user: verify that a user and group of this name exist,
// then set uid and gid for later lookup
var ids = getUserIds(config.user);
config.uid = ids.uid;
config.gid = ids.gid;
// host: verify that host is in a (somewhat) valid format
// TODO: Improve this?
if(typeof config.host != "string" || !config.host.match(
/[a-z0-9:.-]+/i
)) throw new Error("host: not a valid hostname nor IP address");
// port: verify that a valid port was specified
if(
typeof config.port != "number"
|| config.port%1 > 0 || config.port < 0 || config.port > 65536
) throw new Error("port: invalid port number specified");
}
catch(e){
throw new Error(
"Error while validating \"" + configPath + "\": " + e.message
);
}
// configPath: if the config file specifies a different path to a config
// file, then try to load this instead.
// Config files loaded later inherit from config files loaded earlier :)
if(config.configPath){
// Make sure that we don't keep re-loading the current config file
// recursively and overflow the stack.
configPath = config.configPath;
delete config.configPath
return initConfig(configPath, config);
}
return config;
}
module.exports = function main(){
try {
// Attempt to load config.json
var config = initConfig("config.json");
// Set up environment for child processes
process.env.UCCPORTAL_CONFIG = JSON.stringify(config);
process.env.NODE_PATH =
(process.env.NODE_PATH ? process.env.NODE_PATH + ":" : "")
+ process.cwd()
;
// Launch services
var nodejs = process.argv[0];
var spawnOpts = {stdio: "inherit"};
cproc.spawn(nodejs, ["core/web-service.js"], spawnOpts);
cproc.spawn(nodejs, ["core/login-service.js"], spawnOpts);
}
catch(e){
//console.error("Fatal Error: " + e.message);
process.exitCode = 1;
throw e;
}
}
"use strict";
var express = require("express");
var http = require("http");
var fs = require("fs");
var config = require("common/config");
var hsocks = require("common/http-sockets");
function main(){
var app;
// Set up express app and routes
app = express();
app.use("/api/login", hsocks.proxy(config.varPath + "/login-service.sock"));
app.get("/", function(req, res){res.redirect("/login.html");});
app.use("/", express.static("static/"));
// Start web server or crash
console.info(
"Starting web-service on host \"" + config.host
+ "\" and port " + config.port
);
try {app.listen(config.port, config.host);}
catch(e){
throw new Error("Failed to start web-service: " + e.message);
}
}
main();
"use strict";
var http = require("http");
var fs = require("fs");
var cproc = require("child_process");
var qstring = require("querystring");
var express = require("express");
var hsocks = require("common/http-sockets");
var sudo = require("common/sudo");
var config = require("common/config");
// TODO: replace with sudo implementation
// This is a stub that will likely just be replaced with
// a call to the sudo function
function testCredentials(username, password, callback){
// Validate input
if(
typeof username != "string" || !username
|| !username.match(/^[a-zA-Z0-9_]+$/)
|| typeof password != "string" || !password
|| typeof callback != "function"
) callback(false);
// Try to run sudo
// TODO: come up with a better way of verifying credentials
callback(true);
}
function main(){
var app = express();
var server;
// Set up routes
app.get("/", function(req, res){res.redirect("/login.html");});
app.post("/", function(req, res){
req.once("readable", function(){
var parsed = {};
// Read in first chunk of req, try to parse it...
try{parsed = qstring.parse(req.read().toString());}
catch(e){
res.status(400).end();
return;
}
// Validate provided username/password (or lack thereof)
testCredentials(parsed.username, parsed.password, function(valid){
if(valid) res.status(200).end();
else res.status(401).end();
});
});
});
// Create server and listening socket, ensuring only processess running
// as config.user can access the socket
var sockPath = config.varPath + "/login-service.sock";
server = hsocks.createServer(app);
process.umask(0o177); //u=rw,go=-
server.listen(sockPath);
}
main();
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment