From 9aafffd273a8839e27868859433054619125e68d Mon Sep 17 00:00:00 2001 From: Maxime Poulin Date: Tue, 26 Apr 2016 16:40:27 -0400 Subject: [PATCH 1/3] Fix existing identd --- src/identd.js | 17 +++++++++++------ src/plugins/irc-events/connection.js | 10 ++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/identd.js b/src/identd.js index 3d626dad..f598052a 100644 --- a/src/identd.js +++ b/src/identd.js @@ -2,26 +2,31 @@ var _ = require("lodash"); var net = require("net"); var users = {}; +var enabled = false; module.exports.start = function(port) { port = port || 113; log.info("Starting identd server on port", port); net.createServer(init).listen(port); + enabled = true; }; module.exports.hook = function(stream, user) { - var id = ""; var socket = stream.socket || stream; - socket.on("connect", function() { - var ports = _.pick(socket, "localPort", "remotePort"); - id = _.values(ports).join(", "); - users[id] = user; - }); + var ports = _.pick(socket, "localPort", "remotePort"); + var id = _.values(ports).join(", "); + + users[id] = user; + socket.on("close", function() { delete users[id]; }); }; +module.exports.isEnabled = function() { + return enabled; +}; + function init(socket) { socket.on("data", function(data) { respond(socket, data); diff --git a/src/plugins/irc-events/connection.js b/src/plugins/irc-events/connection.js index 597e1db3..a4226693 100644 --- a/src/plugins/irc-events/connection.js +++ b/src/plugins/irc-events/connection.js @@ -9,10 +9,6 @@ module.exports = function(irc, network) { text: "Network created, connecting to " + network.host + ":" + network.port + "..." })); - irc.on("raw socket connected", function() { - identd.hook(irc.connection.socket, network.username); - }); - irc.on("socket connected", function() { network.channels[0].pushMessage(client, new Msg({ text: "Connected to the network." @@ -25,6 +21,12 @@ module.exports = function(irc, network) { })); }); + if (identd.isEnabled()) { + irc.on("socket connected", function() { + identd.hook(irc.connection.socket, client.name || network.username); + }); + } + irc.on("socket error", function(err) { log.debug("IRC socket error", err); network.channels[0].pushMessage(client, new Msg({ From fabb758985f2527f6a58b36334e7148de46d5a76 Mon Sep 17 00:00:00 2001 From: Maxime Poulin Date: Tue, 26 Apr 2016 16:41:08 -0400 Subject: [PATCH 2/3] Add support for oidentd spoofing --- defaults/config.js | 12 ++++- src/clientManager.js | 7 +++ src/oidentd.js | 73 ++++++++++++++++++++++++++++ src/plugins/irc-events/connection.js | 13 +++++ src/server.js | 3 +- 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/oidentd.js diff --git a/defaults/config.js b/defaults/config.js index 5481a5cf..47387f66 100644 --- a/defaults/config.js +++ b/defaults/config.js @@ -309,5 +309,15 @@ module.exports = { // @default 113 // port: 113 - } + }, + + // + // Enable oidentd support using the specified file + // + // Example: oidentd: "~/.oidentd.conf", + // + // @type string + // @default null + // + oidentd: null, }; diff --git a/src/clientManager.js b/src/clientManager.js index 33db9474..420d4bf2 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -3,11 +3,18 @@ var fs = require("fs"); var Client = require("./client"); var mkdirp = require("mkdirp"); var Helper = require("./helper"); +var oidentd = require("./oidentd"); module.exports = ClientManager; function ClientManager() { + var config = Helper.getConfig(); + this.clients = []; + + if (typeof config.oidentd === "string") { + this.identHandler = new oidentd(config.oidentd); + } } ClientManager.prototype.findClient = function(name) { diff --git a/src/oidentd.js b/src/oidentd.js new file mode 100644 index 00000000..d8193677 --- /dev/null +++ b/src/oidentd.js @@ -0,0 +1,73 @@ +var fs = require("fs"); +var Helper = require("./helper"); + +function oidentdFile(file) { + this.file = Helper.expandHome(file); + this.connectionId = 0; + this.connections = {}; + + this.refresh(); +} + +oidentdFile.prototype = { + hookSocket: function(socket, user) { + var that = this; + var id = null; + + socket.on("connect", function() { + id = that.addSocket(socket, user); + that.refresh(); + }); + socket.on("close", function() { + that.removeConnection(id); + that.refresh(); + }); + }, + + addSocket: function(socket, user) { + var id = this.connectionId++; + this.connections[id] = {socket: socket, user: user}; + return id; + }, + + removeSocket: function(socket) { + for (var id in this.connections) { + if (this.connections[id] === socket) { + delete this.connections[id]; + break; + } + } + }, + + removeConnection: function(id) { + delete this.connections[id]; + }, + + getSockets: function() { + return this.connections; + }, + + refresh: function() { + var file = "# Warning: file generated by The Lounge: changes will be overwritten!\n"; + + function makeRule(connection) { + return "to " + connection.socket.remoteAddress + + " lport " + connection.socket.localPort + + " from " + connection.socket.localAddress + + " fport " + connection.socket.remotePort + + " { reply \"" + connection.user + "\" }\n"; + } + + for (var id in this.connections) { + file += makeRule(this.connections[id]); + } + + fs.writeFile(this.file, file, {flag: "w+"}, function(err) { + if (err) { + log.error("Failed to update oidentd file!", err); + } + }); + }, +}; + +module.exports = oidentdFile; diff --git a/src/plugins/irc-events/connection.js b/src/plugins/irc-events/connection.js index a4226693..c2114e12 100644 --- a/src/plugins/irc-events/connection.js +++ b/src/plugins/irc-events/connection.js @@ -4,6 +4,7 @@ var Msg = require("../../models/msg"); module.exports = function(irc, network) { var client = this; + var identHandler = this.manager.identHandler; network.channels[0].pushMessage(client, new Msg({ text: "Network created, connecting to " + network.host + ":" + network.port + "..." @@ -27,6 +28,18 @@ module.exports = function(irc, network) { }); } + if (identHandler) { + irc.on("socket connected", function() { + identHandler.addSocket(irc.connection.socket, client.name || network.username); + identHandler.refresh(); + }); + + irc.on("socket close", function() { + identHandler.removeSocket(irc.connection.socket); + identHandler.refresh(); + }); + } + irc.on("socket error", function(err) { log.debug("IRC socket error", err); network.channels[0].pushMessage(client, new Msg({ diff --git a/src/server.js b/src/server.js index 7fa3dffe..dd7e5a9a 100644 --- a/src/server.js +++ b/src/server.js @@ -10,9 +10,10 @@ var dns = require("dns"); var Helper = require("./helper"); var config = {}; -var manager = new ClientManager(); +var manager = null; module.exports = function(options) { + manager = new ClientManager(); config = Helper.getConfig(); config = _.extend(config, options); From 22d4f4061336027a2d55aaf5a3e4421152c06c2b Mon Sep 17 00:00:00 2001 From: Maxime Poulin Date: Tue, 26 Apr 2016 16:53:29 -0400 Subject: [PATCH 3/3] Warn the user when both ident handlers are enabled --- src/server.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/server.js b/src/server.js index dd7e5a9a..b0937058 100644 --- a/src/server.js +++ b/src/server.js @@ -41,6 +41,10 @@ module.exports = function(options) { } if ((config.identd || {}).enable) { + if (manager.identHandler) { + log.warn("Using both identd and oidentd at the same time!"); + } + require("./identd").start(config.identd.port); }