|
|
|
/*
|
|
|
|
* Remote Laboratory Authentication Server
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* (c) 2012 Timothy Pearson
|
|
|
|
* Raptor Engineering
|
|
|
|
* http://www.raptorengineeringinc.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "auth_conn.h"
|
|
|
|
|
|
|
|
/* exception handling */
|
|
|
|
struct exit_exception {
|
|
|
|
int c;
|
|
|
|
exit_exception(int c):c(c) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
The AuthSocket class provides a socket that is connected with a client.
|
|
|
|
For every client that connects to the server, the server creates a new
|
|
|
|
instance of this class.
|
|
|
|
*/
|
|
|
|
AuthSocket::AuthSocket(int sock, TQObject *parent, const char *name) :
|
|
|
|
TDEKerberosServerSocket( parent, name ), m_criticalSection(0) {
|
|
|
|
|
|
|
|
setServiceName("remotefpga");
|
|
|
|
|
|
|
|
line = 0;
|
|
|
|
connect(this, SIGNAL(connectionClosed()), SLOT(connectionClosedHandler()));
|
|
|
|
setSocket( sock );
|
|
|
|
}
|
|
|
|
|
|
|
|
AuthSocket::~AuthSocket() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthSocket::close() {
|
|
|
|
TDEKerberosServerSocket::close();
|
|
|
|
connectionClosedHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthSocket::connectionClosedHandler() {
|
|
|
|
printf("[DEBUG] Connection from %s closed\n\r", m_remoteHost.ascii());
|
|
|
|
if (m_criticalSection > 0) {
|
|
|
|
throw exit_exception(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AuthSocket::initiateKerberosHandshake() {
|
|
|
|
if (setUsingKerberos(true) == 0) {
|
|
|
|
TQ_UINT32 magicnum = MAGIC_NUMBER;
|
|
|
|
TQ_UINT32 protover = PROTOCOL_VERSION;
|
|
|
|
|
|
|
|
TQDataStream ds(this);
|
|
|
|
ds << magicnum;
|
|
|
|
ds << protover;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int AuthSocket::enterCommandLoop() {
|
|
|
|
m_criticalSection++;
|
|
|
|
try {
|
|
|
|
TQString command;
|
|
|
|
TQDataStream ds(this);
|
|
|
|
|
|
|
|
while (state() == TQSocket::Connected) {
|
|
|
|
ds >> command;
|
|
|
|
printf("[RAJA DEBUG 500.0] Got command %s\n\r", command.ascii()); fflush(stdout);
|
|
|
|
if (command == "LIST") {
|
|
|
|
// Send list of available servers...
|
|
|
|
// RAJA FIXME
|
|
|
|
StationList slist;
|
|
|
|
ds << slist;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ds << "ERRINVCMD";
|
|
|
|
}
|
|
|
|
tqApp->processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_criticalSection--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
m_criticalSection--;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
The AuthServer class handles new connections to the server. For every
|
|
|
|
client that connects, it creates a new AuthSocket -- that instance is now
|
|
|
|
responsible for the communication with that client.
|
|
|
|
*/
|
|
|
|
AuthServer::AuthServer(TQObject* parent) :
|
|
|
|
TQServerSocket( 4004, 1, parent ) {
|
|
|
|
|
|
|
|
if ( !ok() ) {
|
|
|
|
printf("[ERROR] Failed to bind to port 4004\n\r");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AuthServer::~AuthServer() {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
void AuthServer::newConnection(int socket) {
|
|
|
|
AuthSocket *s = new AuthSocket(socket, this);
|
|
|
|
s->m_remoteHost = s->peerAddress().toString();
|
|
|
|
printf("[DEBUG] New connection from %s\n\r", s->m_remoteHost.ascii());
|
|
|
|
if (s->initiateKerberosHandshake() != 0) {
|
|
|
|
s->close();
|
|
|
|
delete s;
|
|
|
|
s = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
connect(s, SIGNAL(connectionClosed()), s, SLOT(deleteLater()));
|
|
|
|
emit newConnect(s);
|
|
|
|
s->enterCommandLoop();
|
|
|
|
}
|
|
|
|
}
|