Identd resolving usernames/idents correctly
authorDarren <darren@darrenwhitlen.com>
Wed, 17 Jul 2013 17:53:22 +0000 (18:53 +0100)
committerDarren <darren@darrenwhitlen.com>
Wed, 17 Jul 2013 17:53:22 +0000 (18:53 +0100)
server/identd.js
server/irc/connection.js
server/kiwi.js

index 7ad72fb3dd3c17ad634df925938ac495d0ba188d..1fc871bb0a68bcd322f3a447b41d037b2ef89fac 100644 (file)
@@ -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;
index 4239b6e6ff293e16b0eb0fb7e5c5dac076a46a36..eeb1350658d1279bc0c5d4e77080109b50574da0 100644 (file)
@@ -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
index 75b3dde24e515ac89801791274c55895e421b527..42ef9bc5a41c5d672efaf1e554eed874f97c25d7 100755 (executable)
@@ -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();
 }