summaryrefslogtreecommitdiffstats
path: root/servers/auth_server_lin/src/auth_conn.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'servers/auth_server_lin/src/auth_conn.cpp')
-rw-r--r--servers/auth_server_lin/src/auth_conn.cpp127
1 files changed, 74 insertions, 53 deletions
diff --git a/servers/auth_server_lin/src/auth_conn.cpp b/servers/auth_server_lin/src/auth_conn.cpp
index e531099..14c234f 100644
--- a/servers/auth_server_lin/src/auth_conn.cpp
+++ b/servers/auth_server_lin/src/auth_conn.cpp
@@ -26,6 +26,15 @@
#include "auth_conn.h"
+#define ABORT_SOCKET(s) s->close(); \
+ tqApp->processEvents(); \
+ while (s->state() == TQSocket::Closing) { \
+ tqApp->processEvents(); \
+ } \
+ s->disconnect(); \
+ delete s; \
+ s = NULL;
+
/* exception handling */
struct exit_exception {
int c;
@@ -38,7 +47,7 @@ struct exit_exception {
instance of this class.
*/
AuthSocket::AuthSocket(int sock, TQObject *parent, const char *name) :
- TDEKerberosServerSocket(parent, name), m_criticalSection(0), m_stationID(-1), m_config(static_cast<AuthServer*>(parent)->m_config), m_database(NULL), m_databaseStationsCursor(NULL),
+ TDEKerberosServerSocket(parent, name), m_criticalSection(0), m_stationID(-1), m_bound(false), m_config(static_cast<AuthServer*>(parent)->m_config), m_database(NULL), m_databaseStationsCursor(NULL),
m_databaseServicesCursor(NULL), m_databaseServiceTypesCursor(NULL), m_databasePermissionsCursor(NULL), m_databaseActivityCursor(NULL)
{
@@ -69,11 +78,6 @@ AuthSocket::~AuthSocket() {
if (m_databaseActivityCursor) {
delete m_databaseActivityCursor;
}
-
- if (m_database) {
- m_database->close();
- delete m_database;
- }
}
void AuthSocket::close() {
@@ -86,11 +90,13 @@ void AuthSocket::close() {
void AuthSocket::connectionClosedHandler() {
printf("[DEBUG] Connection from %s closed\n\r", m_remoteHost.ascii());
- // Update database
- m_databaseActivityCursor->select(TQString("station='%1' AND username='%2' AND realmname='%3'").arg(m_stationID).arg(m_authenticatedUserName).arg(m_authenticatedRealmName));
- if (m_databaseActivityCursor->next()) {
- m_databaseActivityCursor->primeDelete();
- m_databaseActivityCursor->del(true);
+ if (m_bound) {
+ // Update database
+ m_databaseActivityCursor->select(TQString("station='%1' AND username='%2' AND realmname='%3'").arg(m_stationID).arg(m_authenticatedUserName).arg(m_authenticatedRealmName));
+ if (m_databaseActivityCursor->next()) {
+ m_databaseActivityCursor->primeDelete();
+ m_databaseActivityCursor->del(true);
+ }
}
if (m_criticalSection > 0) {
@@ -115,8 +121,6 @@ int AuthSocket::initiateKerberosHandshake() {
}
int AuthSocket::enterCommandLoop() {
- bool bound = false;
-
m_criticalSection++;
try {
TQString command;
@@ -196,7 +200,7 @@ int AuthSocket::enterCommandLoop() {
ds << TQString("ERRUNAVAL");
}
else {
- bound = true;
+ m_bound = true;
// Update database
TQSqlRecord *buffer = m_databaseActivityCursor->primeInsert();
@@ -219,7 +223,7 @@ int AuthSocket::enterCommandLoop() {
m_stationID = m_databaseActivityCursor->value("station").toInt();
}
- if (bound == true) {
+ if (m_bound == true) {
ds << TQString("ERRINVCMD");
}
@@ -284,6 +288,53 @@ int AuthSocket::connectToDatabase() {
return -2;
}
+ m_database = TQSqlDatabase::database();
+ if (!m_database) {
+ printf("[ERROR] Database was not constructed by the application\n\r"); fflush(stdout);
+ return -1;
+ }
+
+ m_databaseStationsCursor = new TQSqlCursor("stations", TRUE, m_database);
+ m_databaseServicesCursor = new TQSqlCursor("services", TRUE, m_database);
+ m_databaseServiceTypesCursor = new TQSqlCursor("servicetypes", TRUE, m_database);
+ m_databasePermissionsCursor = new TQSqlCursor("permissions", TRUE, m_database);
+ m_databaseActivityCursor = new TQSqlCursor("activity", TRUE, m_database);
+
+ return 0;
+}
+
+/*
+ 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 ), m_database(NULL) {
+
+ m_config = new KSimpleConfig("remotefpga_authserver.conf", false);
+
+ if (connectToDatabase() != 0) {
+ exit(1);
+ }
+
+ if ( !ok() ) {
+ printf("[ERROR] Failed to bind to port 4004\n\r");
+ exit(1);
+ }
+
+ printf("[INFO] Server started on port 4004\n\r"); fflush(stdout);
+}
+
+AuthServer::~AuthServer() {
+ if (m_database) {
+ TQSqlDatabase::removeDatabase(m_database);
+ m_database = NULL;
+ }
+
+ delete m_config;
+}
+
+int AuthServer::connectToDatabase() {
m_config->setGroup("Database");
m_database = TQSqlDatabase::addDatabase(m_config->readEntry("driver"));
@@ -294,7 +345,7 @@ int AuthSocket::connectToDatabase() {
if(!m_database->open()) {
printf("[ERROR] Failed to connect to control database on server '%s' [%s]\n\r", m_database->hostName().ascii(), m_database->lastError().text().ascii()); fflush(stdout);
- delete m_database;
+ TQSqlDatabase::removeDatabase(m_database);
m_database = NULL;
return -1;
}
@@ -302,7 +353,7 @@ int AuthSocket::connectToDatabase() {
if (!m_database->tables().contains("stations")) {
m_database->close();
printf("[ERROR] Control database '%s' on '%s' does not contain the required 'stations' table\n\r", m_database->databaseName().ascii(), m_database->hostName().ascii()); fflush(stdout);
- delete m_database;
+ TQSqlDatabase::removeDatabase(m_database);
m_database = NULL;
return -1;
}
@@ -310,7 +361,7 @@ int AuthSocket::connectToDatabase() {
if (!m_database->tables().contains("services")) {
m_database->close();
printf("[ERROR] Control database '%s' on '%s' does not contain the required 'services' table\n\r", m_database->databaseName().ascii(), m_database->hostName().ascii()); fflush(stdout);
- delete m_database;
+ TQSqlDatabase::removeDatabase(m_database);
m_database = NULL;
return -1;
}
@@ -318,7 +369,7 @@ int AuthSocket::connectToDatabase() {
if (!m_database->tables().contains("servicetypes")) {
m_database->close();
printf("[ERROR] Control database '%s' on '%s' does not contain the required 'servicetypes' table\n\r", m_database->databaseName().ascii(), m_database->hostName().ascii()); fflush(stdout);
- delete m_database;
+ TQSqlDatabase::removeDatabase(m_database);
m_database = NULL;
return -1;
}
@@ -326,7 +377,7 @@ int AuthSocket::connectToDatabase() {
if (!m_database->tables().contains("permissions")) {
m_database->close();
printf("[ERROR] Control database '%s' on '%s' does not contain the required 'permissions' table\n\r", m_database->databaseName().ascii(), m_database->hostName().ascii()); fflush(stdout);
- delete m_database;
+ TQSqlDatabase::removeDatabase(m_database);
m_database = NULL;
return -1;
}
@@ -334,51 +385,21 @@ int AuthSocket::connectToDatabase() {
if (!m_database->tables().contains("activity")) {
m_database->close();
printf("[ERROR] Control database '%s' on '%s' does not contain the required 'activity' table\n\r", m_database->databaseName().ascii(), m_database->hostName().ascii()); fflush(stdout);
- delete m_database;
+ TQSqlDatabase::removeDatabase(m_database);
m_database = NULL;
return -1;
}
- m_databaseStationsCursor = new TQSqlCursor("stations");
- m_databaseServicesCursor = new TQSqlCursor("services");
- m_databaseServiceTypesCursor = new TQSqlCursor("servicetypes");
- m_databasePermissionsCursor = new TQSqlCursor("permissions");
- m_databaseActivityCursor = new TQSqlCursor("activity");
-
return 0;
}
-/*
- 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 ) {
-
- m_config = new KSimpleConfig("remotefpga_authserver.conf", false);
-
- if ( !ok() ) {
- printf("[ERROR] Failed to bind to port 4004\n\r");
- exit(1);
- }
-
- printf("[INFO] Server started on port 4004\n\r"); fflush(stdout);
-}
-
-AuthServer::~AuthServer() {
- delete m_config;
-}
-
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) {
- printf("[DEBUG] Connection from %s closed due to Kerberos failure\n\r", s->m_remoteHost.ascii());
- s->close();
- delete s;
- s = NULL;
+ printf("[DEBUG] Connection from %s closed due to Kerberos failure\n\r", s->m_remoteHost.ascii()); fflush(stdout);
+ ABORT_SOCKET(s)
return;
}
else {