2016-12-17 09:51:33 +00:00
|
|
|
"use strict";
|
|
|
|
|
2018-06-15 20:31:06 +00:00
|
|
|
const log = require("./log");
|
2016-12-17 09:51:33 +00:00
|
|
|
const fs = require("fs");
|
|
|
|
const net = require("net");
|
2018-03-02 18:28:54 +00:00
|
|
|
const colors = require("chalk");
|
2016-12-17 09:51:33 +00:00
|
|
|
const Helper = require("./helper");
|
|
|
|
|
|
|
|
class Identification {
|
|
|
|
constructor(startedCallback) {
|
|
|
|
this.connectionId = 0;
|
|
|
|
this.connections = new Map();
|
|
|
|
|
|
|
|
if (typeof Helper.config.oidentd === "string") {
|
|
|
|
this.oidentdFile = Helper.expandHome(Helper.config.oidentd);
|
|
|
|
log.info(`Oidentd file: ${colors.green(this.oidentdFile)}`);
|
|
|
|
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Helper.config.identd.enable) {
|
|
|
|
if (this.oidentdFile) {
|
|
|
|
log.warn("Using both identd and oidentd at the same time, this is most likely not intended.");
|
|
|
|
}
|
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
const server = net.createServer(this.serverConnection.bind(this));
|
2016-12-17 09:51:33 +00:00
|
|
|
server.listen({
|
|
|
|
port: Helper.config.identd.port || 113,
|
|
|
|
host: Helper.config.bind || Helper.config.host,
|
|
|
|
}, () => {
|
2018-01-11 11:33:36 +00:00
|
|
|
const address = server.address();
|
2016-12-17 09:51:33 +00:00
|
|
|
log.info(`Identd server available on ${colors.green(address.address + ":" + address.port)}`);
|
|
|
|
|
2017-03-17 20:19:08 +00:00
|
|
|
startedCallback(this);
|
2016-12-17 09:51:33 +00:00
|
|
|
});
|
|
|
|
} else {
|
2017-03-17 20:19:08 +00:00
|
|
|
startedCallback(this);
|
2016-12-17 09:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serverConnection(socket) {
|
2017-04-08 12:34:31 +00:00
|
|
|
socket.on("data", (data) => {
|
2016-12-17 09:51:33 +00:00
|
|
|
this.respondToIdent(socket, data);
|
|
|
|
socket.end();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
respondToIdent(socket, data) {
|
|
|
|
data = data.toString().split(",");
|
|
|
|
|
2018-05-15 13:04:05 +00:00
|
|
|
const lport = parseInt(data[0], 10) || 0;
|
|
|
|
const fport = parseInt(data[1], 10) || 0;
|
2016-12-17 09:51:33 +00:00
|
|
|
|
|
|
|
if (lport < 1 || fport < 1 || lport > 65535 || fport > 65535) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-11 11:33:36 +00:00
|
|
|
for (const connection of this.connections.values()) {
|
2016-12-17 09:51:33 +00:00
|
|
|
if (connection.socket.remoteAddress === socket.remoteAddress
|
|
|
|
&& connection.socket.remotePort === fport
|
|
|
|
&& connection.socket.localPort === lport
|
|
|
|
&& connection.socket.localAddress === socket.localAddress) {
|
|
|
|
return socket.write(`${lport}, ${fport} : USERID : UNIX : ${connection.user}\r\n`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.write(`${lport}, ${fport} : ERROR : NO-USER\r\n`);
|
|
|
|
}
|
|
|
|
|
|
|
|
addSocket(socket, user) {
|
|
|
|
const id = ++this.connectionId;
|
|
|
|
|
2018-03-05 00:59:16 +00:00
|
|
|
this.connections.set(id, {socket, user});
|
2016-12-17 09:51:33 +00:00
|
|
|
|
|
|
|
if (this.oidentdFile) {
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeSocket(id) {
|
|
|
|
this.connections.delete(id);
|
|
|
|
|
|
|
|
if (this.oidentdFile) {
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
let file = "# Warning: file generated by The Lounge: changes will be overwritten!\n";
|
|
|
|
|
|
|
|
this.connections.forEach((connection) => {
|
2017-11-26 22:34:28 +00:00
|
|
|
file += `to ${connection.socket.remoteAddress}`
|
|
|
|
+ ` fport ${connection.socket.remotePort}`
|
2018-03-22 11:44:43 +00:00
|
|
|
+ ` from ${connection.socket.localAddress}`
|
|
|
|
+ ` lport ${connection.socket.localPort}`
|
2017-11-26 22:34:28 +00:00
|
|
|
+ ` { reply "${connection.user}" }\n`;
|
2016-12-17 09:51:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeFile(this.oidentdFile, file, {flag: "w+"}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
log.error("Failed to update oidentd file!", err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Identification;
|