Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of git://github.com/NodePing/node-smtp-protocol…

… into NodePing-master
  • Loading branch information...
commit 105d5a0d1b8b48a4142ba82b96ddc1a6666c0666 2 parents 8160758 + 603e9c1
@substack authored
Showing with 95 additions and 1 deletion.
  1. +4 −1 index.js
  2. +91 −0 lib/server/proto.js
View
5 index.js
@@ -38,7 +38,10 @@ exports.connect = function () {
else if (tlsOpts) {
stream = tls.connect(port, host, tlsOpts, function () {
var pending = stream.listeners('secure').length;
- var allOk = pending === 0 ? stream.authorized : true;
+ var allOk = true;
+ if(pending === 0){
+ if(!stream.authorized && tlsOpts.rejectUnauthorized !== false) allOk = false;
+ }
if (pending === 0) done()
else {
var ack = {
View
91 lib/server/proto.js
@@ -1,6 +1,7 @@
var parser = require('./parser');
var EventEmitter = require('events').EventEmitter;
var dot = require('../dot.js').dot;
+var crypto = require("crypto");
module.exports = function (stream) {
return new Client(stream);
@@ -41,6 +42,96 @@ Client.prototype.helo = function (hostname, cb) {
this.queue.push(cb);
};
+Client.prototype.ehlo = function (hostname, cb) {
+ if (typeof hostname === 'function') {
+ cb = hostname;
+ hostname = undefined;
+ }
+ this.stream.write(
+ 'EHLO'
+ + (hostname !== undefined ? ' ' + hostname : '')
+ + '\r\n'
+ );
+ this.queue.push(cb);
+};
+
+Client.prototype.starttls = function (cb) {
+ this.stream.write(
+ 'STARTTLS'
+ + '\r\n'
+ );
+ this.queue.push(cb);
+};
+
+Client.prototype.tlsconnectionupgrade = function (hostname, cb) { // hostname is required for cert check
+ var tls = require('tls');
+ var s = tls.connect({servername:hostname, socket:this.stream}, function(error, response){
+ if(error){
+ cb('Unable to upgrade connection',451);
+ return true;
+ }
+ cb(null,220,s);
+ });
+};
+
+Client.prototype.login = function (username, password, type, cb) {
+ var self = this;
+ var supportedTypes = ['PLAIN','LOGIN','CRAM-MD5'];
+ type = (type) ? type.toUpperCase() : 'PLAIN';
+ if(supportedTypes.indexOf(type) < 0){
+ cb('Unsupported login type', 451);
+ }
+ switch(type){
+ case 'PLAIN':
+ var buf = new Buffer(username + "\0" + username + "\0" + password);
+ self.stream.write("AUTH PLAIN " + buf.toString("base64") + "\r\n");
+ self.queue.push(cb);
+ break;
+ case "LOGIN":
+ case "CRAM-MD5":
+ self.authtype = type;
+ self.username = username;
+ self.password = password;
+ self.stream.write("AUTH " + type + "\r\n");
+ self.queue.push(function(err,code,lines){
+ if(err){
+ cb(err,code,lines);
+ return true;
+ }
+ if(code != 334){
+ cb(type+' Auth Failed',code,lines);
+ return true;
+ }
+ switch (self.authtype) {
+ case "LOGIN":
+ var buf = new Buffer(self.username);
+ self.stream.write(buf.toString("base64") + "\r\n");
+ self.queue.push(function(erro,code,lines){
+ if(erro){
+ cb(erro,code,lines);
+ return true;
+ }
+ if(code != 334){
+ cb('LOGIN Auth Failed at username',code,lines);
+ return true;
+ }
+ var buf = new Buffer(self.password);
+ self.stream.write(buf.toString("base64") + "\r\n");
+ self.queue.push(cb);
+ });
+ break;
+ case "CRAM-MD5":
+ var hmac = crypto.createHmac('md5', self.password);
+ msg = (new Buffer(msg, "base64")).toString("ascii");
+ hmac.update(msg);
+ self.stream.write((new Buffer(self.username + " " + hmac.digest("hex")).toString("base64")) + "\r\n");
+ self.queue.push(cb);
+ break;
+ }
+ });
+ }
+};
+
Client.prototype.to = function (addr, ext, cb) {
if (typeof ext === 'function') {
cb = ext;
Please sign in to comment.
Something went wrong with that request. Please try again.