Advertisement
Guest User

Untitled

a guest
Sep 7th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. root@674b60ad8563:/usr/lib/node_modules/Haraka/plugins/auth# ls
  2. auth_base.js  auth_bridge.js  auth_ldap.js  auth_proxy.js  auth_vpopmaild.js  flat_file.js
  3. root@674b60ad8563:/usr/lib/node_modules/Haraka/plugins/auth# cat auth_proxy.js
  4. // Proxy AUTH requests selectively by domain
  5. var sock  = require('./line_socket');
  6. var utils = require('./utils');
  7. var smtp_regexp = /^([0-9]{3})([ -])(.*)/;
  8.  
  9. exports.register = function () {
  10.     this.inherits('auth/auth_base');
  11. };
  12.  
  13. exports.hook_capabilities = function (next, connection) {
  14.     if (connection.using_tls) {
  15.         var methods = [ 'PLAIN', 'LOGIN' ];
  16.         connection.capabilities.push('AUTH ' + methods.join(' '));
  17.         connection.notes.allowed_auth_methods = methods;
  18.     }
  19.     next();
  20. };
  21.  
  22. exports.check_plain_passwd = function (connection, user, passwd, cb) {
  23.     var domain;
  24.     if ((domain = /@([^@]+)$/.exec(user))) {
  25.         domain = domain[1].toLowerCase();
  26.     }
  27.     else {
  28.         // AUTH user not in user@domain.com format
  29.         connection.logerror(this, 'AUTH user="' + user + '" error="not in required format"');
  30.         return cb(false);
  31.     }
  32.  
  33.     // Check if domain exists in configuration file
  34.     var config = this.config.get('auth_proxy.ini');
  35.     if (!config.domains[domain]) {
  36.         connection.logerror(this, 'AUTH user="' + user + '" error="domain \'' + domain + '\' is not defined"');
  37.         return cb(false);
  38.     }
  39.  
  40.     this.try_auth_proxy(connection, config.domains[domain].split(/[,; ]/), user, passwd, cb);
  41. };
  42.  
  43. exports.try_auth_proxy = function (connection, hosts, user, passwd, cb) {
  44.     if (!hosts || (hosts && !hosts.length)) return cb(false);
  45.     if (typeof hosts !== 'object') {
  46.         hosts = [ hosts ];
  47.     }
  48.  
  49.     var self = this;
  50.     var host = hosts.shift();
  51.     var methods = [];
  52.     var auth_complete = false;
  53.     var auth_success = false;
  54.     var command = 'connect';
  55.     var response = [];
  56.     var secure = false;
  57.  
  58.     var hostport = host.split(/:/);
  59.     var socket = sock.connect(((hostport[1]) ? hostport[1] : 25), hostport[0]);
  60.     connection.logdebug(self, 'attempting connection to host=' + hostport[0] + ' port=' + ((hostport[1]) ? hostport[1] : 25) + 'with password: ' + passwd + ', and username: ' + user);
  61.     socket.setTimeout(30 * 1000);
  62.     socket.on('connect', function () {
  63.     });
  64.     socket.on('close', function () {
  65.         if (!auth_complete) {
  66.             // Try next host
  67.             return self.try_auth_proxy(connection, hosts, user, passwd, cb);
  68.         }
  69.         connection.loginfo(self, 'AUTH user="' + user + '" host="' + host + '" success=' + auth_success);
  70.         return cb(auth_success);
  71.     });
  72.     socket.on('timeout', function () {
  73.         connection.logerror(self, "connection timed out");
  74.         socket.end();
  75.         // Try next host
  76.         return self.try_auth_proxy(connection, hosts, user, passwd, cb);
  77.     });
  78.     socket.on('error', function (err) {
  79.         connection.logerror(self, "connection failed to host " + host + ": " + err);
  80.         return self.try_auth_proxy(connection, hosts, user, passwd, cb);
  81.     });
  82.     socket.send_command = function (cmd, data) {
  83.         var line = cmd + (data ? (' ' + data) : '');
  84.         if (cmd === 'dot') {
  85.             line = '.';
  86.         }
  87.         connection.logprotocol(self, "C: " + line);
  88.         command = cmd.toLowerCase();
  89.         this.write(line + "\r\n");
  90.         // Clear response buffer from previous command
  91.         response = [];
  92.     };
  93.     socket.on('line', function (line) {
  94.         connection.logprotocol(self, "S: " + line);
  95.         var matches = smtp_regexp.exec(line);
  96.         if (!matches) return;
  97.  
  98.         var code = matches[1];
  99.         var cont = matches[2];
  100.         var rest = matches[3];
  101.         response.push(rest);
  102.  
  103.         if (cont !== ' ' || cont !== '-') {
  104.             // Unrecognized response.
  105.             //connection.logerror(self, "unrecognized response: " + line);
  106.             //socket.end();
  107.             return;
  108.         }
  109.  
  110.         connection.logdebug(self, 'command state: ' + command);
  111.         if (command === 'ehlo') {
  112.             if (code[0] === '5') {
  113.                 // EHLO command rejected; abort
  114.                 socket.send_command('QUIT');
  115.                 return;
  116.             }
  117.             // Parse CAPABILITIES
  118.             var i;
  119.             for (i in response) {
  120.                 if (/^STARTTLS/.test(response[i])) {
  121.                     if (secure) continue;    // silly remote, we've already upgraded
  122.                     var key = self.config.get('tls_key.pem', 'binary');
  123.                     var cert = self.config.get('tls_cert.pem', 'binary');
  124.                     // Use TLS opportunistically if we found the key and certificate
  125.                     if (key && cert) {
  126.                         this.on('secure', function () {
  127.                             secure = true;
  128.                             socket.send_command('EHLO', self.config.get('me'));
  129.                         });
  130.                         socket.send_command('STARTTLS');
  131.                         return;
  132.                     }
  133.                 }
  134.                 else if (/^AUTH /.test(response[i])) {
  135.                     // Parse supported AUTH methods
  136.                     var parse = /^AUTH (.+)$/.exec(response[i]);
  137.                     methods = parse[1].split(/\s+/);
  138.                     connection.logdebug(self, 'found supported AUTH methods: ' + methods);
  139.                     // Prefer PLAIN as it's easiest
  140.                     if (methods.indexOf('PLAIN') !== -1) {
  141.                         socket.send_command('AUTH','PLAIN ' + utils.base64("\0" + user + "\0" + passwd));
  142.                         return;
  143.                     }
  144.                     else if (methods.indexOf('LOGIN') !== -1) {
  145.                         socket.send_command('AUTH','LOGIN');
  146.                         return;
  147.                     }
  148.                     else {
  149.                         // No compatible methods; abort...
  150.                         connection.logdebug(self, 'no compatible AUTH methods');
  151.                         socket.send_command('QUIT');
  152.                         return;
  153.                     }
  154.                 }
  155.             }
  156.         }
  157.         if (command === 'auth') {
  158.             // Handle LOGIN
  159.             if (code[0] === '3' && response[0] === 'VXNlcm5hbWU6') {
  160.                 // Write to the socket directly to keep the state at 'auth'
  161.                 this.write(utils.base64(user) + "\r\n");
  162.                 response = [];
  163.                 return;
  164.             }
  165.             else if (code[0] === '3' && response[0] === 'UGFzc3dvcmQ6') {
  166.                 this.write(utils.base64(passwd) + "\r\n");
  167.                 response = [];
  168.                 return;
  169.             }
  170.             if (code[0] === '5') {
  171.                 // Initial attempt failed; strip domain and retry.
  172.                 var u;
  173.                 if ((u = /^([^@]+)@.+$/.exec(user))) {
  174.                     user = u[1];
  175.                     if (methods.indexOf('PLAIN') !== -1) {
  176.                         socket.send_command('AUTH', 'PLAIN ' + utils.base64("\0" + user + "\0" + passwd));
  177.                     }
  178.                     else if (methods.indexOf('LOGIN') !== -1) {
  179.                         socket.send_command('AUTH', 'LOGIN');
  180.                     }
  181.                     return;
  182.                 }
  183.                 else {
  184.                     // Don't attempt any other hosts
  185.                     auth_complete = true;
  186.                 }
  187.             }
  188.         }
  189.         if (/^[345]/.test(code)) {
  190.             // Got an unhandled error
  191.             connection.logdebug(self, 'error: ' + line);
  192.             socket.send_command('QUIT');
  193.             return;
  194.         }
  195.         switch (command) {
  196.             case 'starttls':
  197.                 var tls_options = { key: key, cert: cert };
  198.                 this.upgrade(tls_options);
  199.                 break;
  200.             case 'connect':
  201.                 socket.send_command('EHLO', self.config.get('me'));
  202.                 break;
  203.             case 'auth':
  204.                 // AUTH was successful
  205.                 auth_complete = true;
  206.                 auth_success = true;
  207.                 socket.send_command('QUIT');
  208.                 break;
  209.             case 'ehlo':
  210.             case 'helo':
  211.             case 'quit':
  212.                 socket.end();
  213.                 break;
  214.             default:
  215.                 throw new Error("[auth/auth_proxy] unknown command: " + command);
  216.         }
  217.     });
  218. };
  219. root@674b60ad8563:/usr/lib/node_modules/Haraka/plugins/auth#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement