From cc3c5d041d1c68efca5b2959bf38ddd7c668a40a Mon Sep 17 00:00:00 2001 From: Darren Date: Wed, 17 Jul 2013 18:53:22 +0100 Subject: [PATCH] Identd resolving usernames/idents correctly --- server/identd.js | 28 ++++++++++++++++++---------- server/irc/connection.js | 14 ++++++++++++++ server/kiwi.js | 23 ++++++++++++++++++++--- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/server/identd.js b/server/identd.js index 7ad72fb..1fc871b 100644 --- a/server/identd.js +++ b/server/identd.js @@ -4,12 +4,15 @@ var IdentdServer = module.exports = function(opts) { var that = this; + var default_user_id = 'kiwi', + default_system_id = 'UNIX-KiwiIRC'; + // Option defaults opts = opts || {}; opts.bind_addr = opts.bind_addr || '0.0.0.0'; opts.bind_port = opts.bind_port || 113; - opts.system_id = opts.system_id || 'UNIX-KiwiIRC'; - opts.user_id = opts.user_id || 'kiwi'; + opts.system_id = opts.system_id || default_system_id; + opts.user_id = opts.user_id || default_user_id; var server = net.createServer(function(socket) { @@ -20,13 +23,18 @@ var IdentdServer = module.exports = function(opts) { buffer += data.toString(); - // Wait until we have a full line of data before processing it - if (buffer.indexOf('\n') === -1) - return; + // If we exceeed 512 bytes, presume a flood and disconnect + if (buffer.length < 512) { + + // Wait until we have a full line of data before processing it + if (buffer.indexOf('\n') === -1) + return; + + // Get the first line of data and process it for a rsponse + data_line = buffer.split('\n')[0]; + response = that.processLine(data_line); - // Get the first line of data and process it for a rsponse - data_line = buffer.split('\n')[0]; - response = that.processLine(data_line); + } // Close down the socket while sending the response socket.removeAllListeners(); @@ -72,7 +80,7 @@ var IdentdServer = module.exports = function(opts) { return; if (typeof opts.user_id === 'function') { - user = opts.user_id(port_here, port_there).toString(); + user = opts.user_id(port_here, port_there).toString() || default_user_id; } else { user = opts.user_id.toString(); } @@ -80,7 +88,7 @@ var IdentdServer = module.exports = function(opts) { if (typeof opts.system_id === 'function') { system = opts.system_id(port_here, port_there).toString(); } else { - system = opts.system_id.toString(); + system = opts.system_id.toString() || default_system_id } return port_here.toString() + ' , ' + port_there.toString() + ' : USERID : ' + system + ' : ' + user; diff --git a/server/irc/connection.js b/server/irc/connection.js index 4239b6e..eeb1350 100644 --- a/server/irc/connection.js +++ b/server/irc/connection.js @@ -196,6 +196,13 @@ IrcConnection.prototype.connect = function () { // Apply the socket listeners that.socket.on(socket_connect_event_name, function () { that.connected = true; + + // Make note of the port numbers for any identd lookups + // Nodejs < 0.9.6 has no socket.localPort so check this first + if (this.localPort) { + global.clients.port_pairs[this.localPort.toString() + '_' + this.remotePort.toString()] = that; + } + socketConnectHandler.call(that); }); @@ -209,6 +216,13 @@ IrcConnection.prototype.connect = function () { that.socket.on('close', function (had_error) { that.connected = false; + + // Remove this socket form the identd lookup + // Nodejs < 0.9.6 has no socket.localPort so check this first + if (this.localPort) { + delete global.clients.port_pairs[this.localPort.toString() + '_' + this.remotePort.toString()]; + } + that.emit('close'); // Close the whole socket down diff --git a/server/kiwi.js b/server/kiwi.js index 75b3dde..42ef9bc 100755 --- a/server/kiwi.js +++ b/server/kiwi.js @@ -83,6 +83,10 @@ global.clients = { clients: Object.create(null), addresses: Object.create(null), + // Local and foriegn port pairs for identd lookups + // {'65483_6667': client_obj, '54356_6697': client_obj} + port_pairs: {}, + add: function (client) { this.clients[client.hash] = client; if (typeof this.addresses[client.real_address] === 'undefined') { @@ -147,10 +151,23 @@ global.servers = { * Identd server */ if (global.config.identd && global.config.identd.enabled) { - new Identd({ + var identd_resolve_user = function(port_here, port_there) { + var key = port_here.toString() + '_' + port_there.toString(); + + if (typeof global.clients.port_pairs[key] == 'undefined') { + return; + } + + return global.clients.port_pairs[key].username; + }; + + var identd_server = new Identd({ bind_addr: global.config.identd.address, - bind_port: global.config.identd.port - }).start(); + bind_port: global.config.identd.port, + user_id: identd_resolve_user + }); + + identd_server.start(); } -- 2.25.1