2015-03-17 04:41:14 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-03-17 04:41:14 +00:00
|
|
|
// ENiGMA½
|
2015-10-22 18:22:03 +00:00
|
|
|
var Config = require('../config.js').config;
|
2015-03-23 04:52:04 +00:00
|
|
|
var baseClient = require('../client.js');
|
2015-10-19 23:21:47 +00:00
|
|
|
var Log = require('../logger.js').log;
|
2015-03-23 04:52:04 +00:00
|
|
|
var ServerModule = require('../server_module.js').ServerModule;
|
2015-10-19 23:21:47 +00:00
|
|
|
var userLogin = require('../user_login.js').userLogin;
|
2015-10-20 21:39:33 +00:00
|
|
|
var enigVersion = require('../../package.json').version;
|
2015-10-22 17:04:50 +00:00
|
|
|
var theme = require('../theme.js');
|
2015-10-20 04:33:11 +00:00
|
|
|
|
2015-03-17 04:41:14 +00:00
|
|
|
var ssh2 = require('ssh2');
|
|
|
|
var fs = require('fs');
|
2015-03-23 04:52:04 +00:00
|
|
|
var util = require('util');
|
2015-10-19 23:21:47 +00:00
|
|
|
var _ = require('lodash');
|
2015-10-21 04:37:39 +00:00
|
|
|
var assert = require('assert');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2015-10-22 06:03:18 +00:00
|
|
|
name : 'SSH',
|
|
|
|
desc : 'SSH Server',
|
|
|
|
author : 'NuSkooler',
|
|
|
|
isSecure : true,
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
2015-03-23 04:52:04 +00:00
|
|
|
exports.getModule = SSHServerModule;
|
2015-03-17 04:41:14 +00:00
|
|
|
|
2015-10-19 23:21:47 +00:00
|
|
|
function SSHClient(clientConn) {
|
2015-03-17 04:41:14 +00:00
|
|
|
baseClient.Client.apply(this, arguments);
|
|
|
|
|
2015-10-19 23:21:47 +00:00
|
|
|
//
|
|
|
|
// WARNING: Until we have emit 'ready', self.input, and self.output and
|
|
|
|
// not yet defined!
|
|
|
|
//
|
2015-03-17 04:41:14 +00:00
|
|
|
|
2015-10-19 23:21:47 +00:00
|
|
|
var self = this;
|
2015-03-17 04:41:14 +00:00
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
var loginAttempts = 0;
|
|
|
|
|
2015-10-21 22:30:32 +00:00
|
|
|
clientConn.on('authentication', function authAttempt(ctx) {
|
|
|
|
var username = ctx.username || '';
|
|
|
|
var password = ctx.password || '';
|
2015-10-22 18:22:03 +00:00
|
|
|
self.isNewUser = (Config.users.newUserNames || []).indexOf(username) > -1;
|
|
|
|
|
|
|
|
self.log.trace( { method : ctx.method, username : username, newUser : self.isNewUser }, 'SSH authentication attempt');
|
2015-10-21 22:30:32 +00:00
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
function termConnection() {
|
|
|
|
ctx.reject();
|
|
|
|
clientConn.end();
|
|
|
|
}
|
|
|
|
|
2015-10-22 18:22:03 +00:00
|
|
|
//
|
|
|
|
// If the system is open and |isNewUser| is true, the login
|
|
|
|
// sequence is hijacked in order to start the applicaiton process.
|
|
|
|
//
|
|
|
|
if(false === Config.general.closedSystem && self.isNewUser) {
|
|
|
|
return ctx.accept();
|
|
|
|
}
|
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
if(username.length > 0 && password.length > 0) {
|
|
|
|
loginAttempts += 1;
|
|
|
|
|
2015-10-21 22:30:32 +00:00
|
|
|
userLogin(self, ctx.username, ctx.password, function authResult(err) {
|
|
|
|
if(err) {
|
|
|
|
if(err.existingConn) {
|
|
|
|
// :TODO: Can we display somthing here?
|
2015-10-22 04:51:35 +00:00
|
|
|
termConnection();
|
2015-10-21 22:30:32 +00:00
|
|
|
return;
|
2015-10-22 17:04:50 +00:00
|
|
|
} else {
|
|
|
|
return ctx.reject(SSHClient.ValidAuthMethods);
|
2015-10-21 22:30:32 +00:00
|
|
|
}
|
2015-10-21 04:37:39 +00:00
|
|
|
} else {
|
2015-10-21 22:30:32 +00:00
|
|
|
ctx.accept();
|
2015-10-21 04:37:39 +00:00
|
|
|
}
|
2015-10-21 22:30:32 +00:00
|
|
|
});
|
2015-10-22 04:51:35 +00:00
|
|
|
} else {
|
|
|
|
if(-1 === SSHClient.ValidAuthMethods.indexOf(ctx.method)) {
|
|
|
|
return ctx.reject(SSHClient.ValidAuthMethods);
|
|
|
|
}
|
2015-10-19 23:21:47 +00:00
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
if(0 === username.length) {
|
|
|
|
// :TODO: can we display something here?
|
|
|
|
return ctx.reject();
|
|
|
|
}
|
2015-10-21 22:30:32 +00:00
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
var interactivePrompt = { prompt: ctx.username + '\'s password: ', echo : false };
|
|
|
|
|
|
|
|
ctx.prompt(interactivePrompt, function retryPrompt(answers) {
|
|
|
|
loginAttempts += 1;
|
|
|
|
|
|
|
|
userLogin(self, username, (answers[0] || ''), function authResult(err) {
|
|
|
|
if(err) {
|
|
|
|
if(err.existingConn) {
|
|
|
|
// :TODO: can we display something here?
|
|
|
|
termConnection();
|
2015-10-22 17:04:50 +00:00
|
|
|
} else {
|
2015-10-22 18:22:03 +00:00
|
|
|
if(loginAttempts >= Config.general.loginAttempts) {
|
2015-10-22 04:51:35 +00:00
|
|
|
termConnection();
|
|
|
|
} else {
|
2015-10-22 17:04:50 +00:00
|
|
|
var artOpts = {
|
|
|
|
client : self,
|
|
|
|
name : 'SSHPMPT.ASC',
|
|
|
|
readSauce : false,
|
|
|
|
};
|
|
|
|
theme.getThemeArt(artOpts, function gotArt(err, artInfo) {
|
|
|
|
if(err) {
|
|
|
|
interactivePrompt.prompt = 'Access denied\n' + ctx.username + '\'s password: ';
|
|
|
|
} else {
|
2015-10-22 18:22:03 +00:00
|
|
|
var newUserNameList = '"' + (Config.users.newUserNames || []).join(', ') + '"';
|
2015-10-22 17:04:50 +00:00
|
|
|
interactivePrompt.prompt =
|
|
|
|
'Access denied\n' +
|
|
|
|
artInfo.data.format( { newUserNames : newUserNameList } ) +
|
|
|
|
'\n' + ctx.username + '\'s password: ';
|
|
|
|
}
|
|
|
|
return ctx.prompt(interactivePrompt, retryPrompt);
|
|
|
|
});
|
2015-10-22 04:51:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-21 22:30:32 +00:00
|
|
|
} else {
|
2015-10-22 04:51:35 +00:00
|
|
|
ctx.accept();
|
2015-10-21 22:30:32 +00:00
|
|
|
}
|
2015-10-22 04:51:35 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-03-17 04:41:14 +00:00
|
|
|
});
|
|
|
|
|
2015-10-21 04:37:39 +00:00
|
|
|
this.updateTermInfo = function(info) {
|
|
|
|
//
|
|
|
|
// From ssh2 docs:
|
|
|
|
// "rows and cols override width and height when rows and cols are non-zero."
|
|
|
|
//
|
2015-10-21 22:30:32 +00:00
|
|
|
var termHeight;
|
|
|
|
var termWidth;
|
2015-10-21 04:37:39 +00:00
|
|
|
|
|
|
|
if(info.rows > 0 && info.cols > 0) {
|
|
|
|
termHeight = info.rows;
|
|
|
|
termWidth = info.cols;
|
|
|
|
} else if(info.width > 0 && info.height > 0) {
|
|
|
|
termHeight = info.height;
|
|
|
|
termWidth = info.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(_.isObject(self.term));
|
|
|
|
|
2015-10-21 22:30:32 +00:00
|
|
|
//
|
|
|
|
// Note that if we fail here, connect.js attempts some non-standard
|
|
|
|
// queries/etc., and ultimately will default to 80x24 if all else fails
|
|
|
|
//
|
|
|
|
if(termHeight > 0 && termWidth > 0) {
|
|
|
|
self.term.termHeight = termHeight;
|
|
|
|
self.term.termWidth = termWidth;
|
|
|
|
}
|
2015-10-21 04:37:39 +00:00
|
|
|
|
|
|
|
if(_.isString(info.term) && info.term.length > 0 && 'unknown' === self.term.termType) {
|
|
|
|
self.setTermType(info.term);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-10-21 22:30:32 +00:00
|
|
|
clientConn.once('ready', function clientReady() {
|
2015-10-19 23:21:47 +00:00
|
|
|
self.log.info('SSH authentication success');
|
2015-03-19 05:08:23 +00:00
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
clientConn.on('session', function sess(accept, reject) {
|
2015-10-20 04:33:11 +00:00
|
|
|
|
|
|
|
var session = accept();
|
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
session.on('pty', function pty(accept, reject, info) {
|
2015-10-21 04:37:39 +00:00
|
|
|
self.log.debug(info, 'SSH pty event');
|
2015-10-20 04:33:11 +00:00
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
if(_.isFunction(accept)) {
|
|
|
|
accept();
|
|
|
|
}
|
2015-10-21 22:30:32 +00:00
|
|
|
|
2015-10-21 04:37:39 +00:00
|
|
|
if(self.input) { // do we have I/O?
|
|
|
|
self.updateTermInfo(info);
|
2015-10-21 22:30:32 +00:00
|
|
|
} else {
|
|
|
|
self.cachedPtyInfo = info;
|
2015-10-21 04:37:39 +00:00
|
|
|
}
|
2015-10-20 04:33:11 +00:00
|
|
|
});
|
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
session.on('shell', function shell(accept, reject) {
|
2015-10-21 04:37:39 +00:00
|
|
|
self.log.debug('SSH shell event');
|
|
|
|
|
2015-10-20 04:33:11 +00:00
|
|
|
var channel = accept();
|
|
|
|
|
2015-10-20 21:39:33 +00:00
|
|
|
self.setInputOutput(channel.stdin, channel.stdout);
|
2015-10-20 04:33:11 +00:00
|
|
|
|
2015-10-21 04:37:39 +00:00
|
|
|
channel.stdin.on('data', function clientData(data) {
|
|
|
|
self.emit('data', data);
|
|
|
|
});
|
|
|
|
|
2015-10-21 22:30:32 +00:00
|
|
|
if(self.cachedPtyInfo) {
|
|
|
|
self.updateTermInfo(self.cachedPtyInfo);
|
|
|
|
delete self.cachedPtyInfo;
|
|
|
|
}
|
2015-10-20 04:33:11 +00:00
|
|
|
|
2015-10-21 22:30:32 +00:00
|
|
|
// we're ready!
|
2015-10-22 18:22:03 +00:00
|
|
|
var firstMenu = self.isNewUser ? Config.servers.ssh.firstMenuNewUser : Config.servers.ssh.firstMenu;
|
|
|
|
self.emit('ready', { firstMenu : firstMenu } );
|
2015-10-21 04:37:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
session.on('window-change', function windowChange(accept, reject, info) {
|
|
|
|
self.log.debug(info, 'SSH window-change event');
|
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
console.log('window-change: ' + accept)
|
|
|
|
|
2015-10-21 04:37:39 +00:00
|
|
|
self.updateTermInfo(info);
|
|
|
|
});
|
|
|
|
|
2015-03-19 05:08:23 +00:00
|
|
|
});
|
2015-03-17 04:41:14 +00:00
|
|
|
});
|
|
|
|
|
2015-10-19 23:21:47 +00:00
|
|
|
clientConn.on('end', function clientEnd() {
|
2015-10-21 22:30:32 +00:00
|
|
|
self.emit('end'); // remove client connection/tracking
|
|
|
|
});
|
|
|
|
|
|
|
|
clientConn.on('error', function connError(err) {
|
2015-10-22 04:51:35 +00:00
|
|
|
self.log.warn( { error : err.toString(), code : err.code }, 'SSH connection error');
|
2015-03-17 04:41:14 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-23 04:52:04 +00:00
|
|
|
util.inherits(SSHClient, baseClient.Client);
|
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
SSHClient.ValidAuthMethods = [ 'password', 'keyboard-interactive' ];
|
|
|
|
|
2015-03-23 04:52:04 +00:00
|
|
|
function SSHServerModule() {
|
|
|
|
ServerModule.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
util.inherits(SSHServerModule, ServerModule);
|
|
|
|
|
|
|
|
SSHServerModule.prototype.createServer = function() {
|
|
|
|
SSHServerModule.super_.prototype.createServer.call(this);
|
2015-03-17 04:41:14 +00:00
|
|
|
|
|
|
|
var serverConf = {
|
2015-10-28 02:46:30 +00:00
|
|
|
privateKey : fs.readFileSync(Config.servers.ssh.privateKeyPem),
|
|
|
|
passphrase : Config.servers.ssh.privateKeyPass,
|
2015-10-20 21:39:33 +00:00
|
|
|
ident : 'enigma-bbs-' + enigVersion + '-srv',
|
2015-10-21 04:37:39 +00:00
|
|
|
// Note that sending 'banner' breaks at least EtherTerm!
|
2015-10-19 23:21:47 +00:00
|
|
|
debug : function debugSsh(dbgLine) {
|
2015-12-08 04:45:48 +00:00
|
|
|
if(true === Config.servers.ssh.traceConnections) {
|
2015-10-20 21:39:33 +00:00
|
|
|
Log.trace('SSH: ' + dbgLine);
|
2015-10-19 23:21:47 +00:00
|
|
|
}
|
2015-10-20 04:33:11 +00:00
|
|
|
},
|
2015-03-17 04:41:14 +00:00
|
|
|
};
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-03-17 04:41:14 +00:00
|
|
|
var server = ssh2.Server(serverConf);
|
2015-03-19 05:08:23 +00:00
|
|
|
server.on('connection', function onConnection(conn, info) {
|
2015-10-19 23:21:47 +00:00
|
|
|
Log.info(info, 'New SSH connection');
|
|
|
|
|
|
|
|
var client = new SSHClient(conn);
|
2015-10-20 21:39:33 +00:00
|
|
|
|
|
|
|
this.emit('client', client, conn._sock);
|
2014-10-17 02:21:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return server;
|
2015-10-22 04:51:35 +00:00
|
|
|
};
|