Unify the CLI flag format between the server/bridge, and add help/usage information to the server.

This commit is contained in:
Ken Moore
2016-06-03 09:27:20 -04:00
parent 0c6ef4dd85
commit f65591ad3a
5 changed files with 66 additions and 22 deletions

View File

@@ -50,11 +50,11 @@ qDebug() << "Starting the bridge:";
qDebug() << " \"sysadm-bridge [-port <portnumber>]\"";
qDebug() << "CLI flags for configuring the bridge:";
qDebug() << " \"-h\" or \"help\": Show this help text";
qDebug() << " \"-import_ssl_file <nickname> <filepath>\": Loads a .crt or .key file and enables the public key for authorization access later";
qDebug() << " \"-import_ssl_pubkey <nickname> <key>\": Enables the public key for authorization access later";
qDebug() << " \"-list_ssl\": Show all known SSL keys";
qDebug() << " \"-remove_ssl <nickname>\": Removes a public key from allowing authorization access";
qDebug() << " \"-set <variable>=<value>\": Used for adjusting individual settings for the bridge";
qDebug() << " \"import_ssl_file <nickname> <filepath>\": Loads a .crt or .key file and enables the public key for authorization access later";
qDebug() << " \"import_ssl_pubkey <nickname> <key>\": Enables the public key for authorization access later";
qDebug() << " \"list_ssl\": Show all known SSL keys";
qDebug() << " \"remove_ssl <nickname>\": Removes a public key from allowing authorization access";
qDebug() << " \"set <variable>=<value>\": Used for adjusting individual settings for the bridge";
qDebug() << " - Possible variables:";
qDebug() << " \"blacklist/block_minutes\" (integer): Number of minutes a system remains on the automatic blacklist";
qDebug() << " \"blacklist/fails_to_block\" (integer): Number of times a system must fail authentication to be placed on blacklist";
@@ -92,9 +92,9 @@ int main( int argc, char ** argv )
// -------------------------
else if( (QString(argv[i])=="-port" || QString(argv[i])=="-p") && (i+1<argc)){ i++; port = QString(argv[i]).toUInt(); }
// -------------------------
else if( QString(argv[i])=="-set" && i+1<argc){ settingchange = true; }
else if( QString(argv[i])=="set" && i+1<argc){ settingchange = true; }
// -------------------------
else if( QString(argv[i])=="-import_ssl_file" && i+2<argc){
else if( QString(argv[i])=="import_ssl_file" && i+2<argc){
i++; QString id = QString(argv[i]);
i++; QFile file(argv[i]);
settingchange=true;
@@ -121,7 +121,7 @@ int main( int argc, char ** argv )
}
// -------------------------
}else if( QString(argv[i])=="-import_ssl_pubkey" && i+2<argc){
}else if( QString(argv[i])=="import_ssl_pubkey" && i+2<argc){
i++; QString id = QString(argv[i]);
i++; QByteArray byte(argv[i], strlen(argv[i]) );
QString enc_key = byte.toBase64();
@@ -131,7 +131,7 @@ int main( int argc, char ** argv )
CONFIG->setValue("RegisteredCerts/"+id+"/"+enc_key, "Date Registered: "+QDateTime::currentDateTime().toString(Qt::ISODate) );
settingchange=true;
// -------------------------
}else if( QString(argv[i])=="-list_ssl" ){
}else if( QString(argv[i])=="list_ssl" ){
qDebug() << "Known SSL Keys (base64)";
settingchange = true;
QStringList keys = QStringList(CONFIG->allKeys());//.filter("RegisteredCerts/");
@@ -139,7 +139,7 @@ int main( int argc, char ** argv )
qDebug() << keys[i].section("/",1,1) << keys[i].section("/",2,-1) << CONFIG->value(keys[i]).toString();
}
// -------------------------
}else if( QString(argv[i])=="-remove_ssl" && i+1<argc){
}else if( QString(argv[i])=="remove_ssl" && i+1<argc){
i++; QString id = QString(argv[i]);
settingchange = true;
QStringList dupkeys = CONFIG->allKeys().filter("RegisteredCerts/"+id+"/");

View File

@@ -211,7 +211,7 @@ void WebServer::SslErrors(const QList<QSslError> &list){
// - More Functions for all socket interactions
void WebServer::SocketClosed(QString ID){
qDebug() << "Socket Closed:" << ID;
qDebug() << "Socket Closed:" << ID << QDateTime::currentDateTime().toString(Qt::ISODate);
for(int i=0; i<OpenSockets.length(); i++){
if(OpenSockets[i]->ID()==ID){ delete OpenSockets.takeAt(i); break; }
}

View File

@@ -14,6 +14,7 @@
WebSocket::WebSocket(QObject *parent, QWebSocket *sock, QString ID, AuthorizationManager *auth) : QObject(parent){
SockID = ID;
isBridge = false;
connecting = false;
SockAuthToken.clear(); //nothing set initially
SOCKET = sock;
TSOCKET = 0;
@@ -42,6 +43,7 @@ WebSocket::WebSocket(QObject *parent, QSslSocket *sock, QString ID, Authorizatio
SockAuthToken.clear(); //nothing set initially
TSOCKET = sock;
SOCKET = 0;
connecting = false;
SockPeerIP = TSOCKET->peerAddress().toString();
LogManager::log(LogManager::HOST,"New Connection: "+SockPeerIP);
AUTHSYSTEM = auth;
@@ -96,7 +98,8 @@ WebSocket::WebSocket(QObject *parent, QString url, QString ID, AuthorizationMana
url.section(":",-1).toInt(&hasport); //check if the last piece of the url is a valid number
if(!hasport){ url.append(":"+QString::number(BRIDGEPORTNUMBER)); }
//Now setup/init the connection
qDebug() << "Connecting to bridge:" << url;
qDebug() << "Connecting to bridge:" << url << QDateTime::currentDateTime().toString(Qt::ISODate);
connecting = true;
SOCKET->setSslConfiguration(QSslConfiguration::defaultConfiguration());
SOCKET->open(QUrl(url));
connCheckTimer = new QTimer(this);
@@ -134,7 +137,7 @@ void WebSocket::closeConnection(){
bool WebSocket::isActive(){
bool ok = false;
if(SOCKET!=0){
ok = SOCKET->isValid();
ok = (SOCKET->isValid() || connecting);
}else if(TSOCKET!=0){
ok = TSOCKET->isValid();
}
@@ -492,6 +495,7 @@ QStringList WebSocket::JsonArrayToStringList(QJsonArray array){
// =====================
void WebSocket::checkConnection(){
if(SOCKET !=0 && !SOCKET->isValid()){
if(connecting){ SOCKET->abort(); }
emit SocketClosed(SockID);
}
else if(TSOCKET !=0 && !TSOCKET->isValid() ){
@@ -636,6 +640,7 @@ void WebSocket::SslError(const QList<QSslError> &err){ //sslErrors() signal
}
void WebSocket::startBridgeAuth(){
connecting = false; //now connected
SockPeerIP = SOCKET->peerAddress().toString();
LogManager::log(LogManager::HOST,"New Bridge Connection: "+SockPeerIP);
//qDebug() << "Init Bridge Auth...";

View File

@@ -36,6 +36,7 @@ private:
QString SockID, SockAuthToken, SockPeerIP;
AuthorizationManager *AUTHSYSTEM;
QList<EventWatcher::EVENT_TYPE> ForwardEvents;
bool connecting; //flag for whether the connection is still being established
//Data handling for bridged connections (1 connection for multiple clients)
QHash<QString, bridge_data> BRIDGE; //ID/data

View File

@@ -63,6 +63,20 @@ inline QString ReadFile(QString path){
return str;
}
void showUsage(){
qDebug() << "sysadm-binary usage:";
qDebug() << "Starting the server:";
qDebug() << " \"sysadm-binary [-rest] [-port <portnumber>]\"";
qDebug() << "CLI flags for configuring the server:";
qDebug() << " \"-h\" or \"help\": Show this help text";
qDebug() << " \"import_ssl_file <username> <filepath> <nickname> [<email>]\": Loads a .crt or .key file and enables the public key for authorization access later";
qDebug() << "Configuring server->bridge connections (websockets only):";
qDebug() << " \"bridge_list\": Show all bridges that are currently setup";
qDebug() << " \"bridge_add <nickname> <url>\": Create a new bridge connection with the given nickname";
qDebug() << " \"bridge_remove <nickname>\": Remove the bridge connection with the given nickname";
qDebug() << " \"bridge_export_key [file]\": Export the public SSL key the server uses to connect to bridges";
}
int main( int argc, char ** argv )
{
@@ -79,6 +93,7 @@ int main( int argc, char ** argv )
for(int i=1; i<argc; i++){
if( QString(argv[i])=="-rest" ){ websocket = false;}
else if( QString(argv[i])=="-p" && (i+1<argc) ){ i++; port = QString(argv[i]).toUInt(); }
else if( QString(argv[i])=="-h" || QString(argv[i]).contains("help") ){ showUsage(); return 0; }
else if( QString(argv[i]).startsWith("bridge_") ){
setonly = true;
QString opt = QString(argv[i]).section("_",1,-1);
@@ -123,16 +138,38 @@ int main( int argc, char ** argv )
qDebug() << "Unknown option:" << argv[i];
return 1;
}
}else if(QString(argv[i])=="-import_ssl_key" && i+3<argc){
}else if(QString(argv[i])=="import_ssl_file" && i+3<argc){
setonly = true;
i++; QString user(argv[i]);
i++; QByteArray key(argv[i]);
i++; QString nickname(argv[i]);
QString email;
if(i+1<argc){ i++; email = QString(argv[i]); }
//Load CLI inputs
i++; QString user(argv[i]); //username
i++; QByteArray key(argv[i]); //key file
i++; QString nickname(argv[i]); // nickname for key
QString email; if(i+1<argc){ i++; email = QString(argv[i]); } //email address
//Read the key file
QFile file(key);
if(!file.open(QIODevice::ReadOnly)){ qDebug() << "Could not open file:" << file.fileName(); }
else{
QByteArray enc_key;
if(file.fileName().endsWith(".crt")){
QSslCertificate cert(&file, QSsl::Pem);
if(!cert.isNull()){ enc_key = cert.publicKey().toPem(); }
}else if(file.fileName().endsWith(".key")){
QSslKey key( &file, QSsl::Rsa, QSsl::Pem, QSsl::PublicKey);
if(!key.isNull()){ enc_key = key.toPem(); }
}else{
qDebug() << "Error: Unknown file type (need .crt or .key file)";
}
file.close();
if(enc_key.isEmpty()){ qDebug() << "ERROR: Could not read file"; }
else{
bool ok = AuthorizationManager::RegisterCertificateInternal(user, enc_key, nickname, email);
if(ok){ qDebug() << "Key Added" << user << nickname; }
else{ qDebug() << "Could not add key"; }
}
}
//See if the key is a file instead - then read it
bool ok = true;
if(QFile::exists(key)){
/*bool ok = true;
if(QFile::exists(key)){
QFile file(key);
QByteArray pubkey;
if(file.open(QIODevice::ReadOnly)){
@@ -144,7 +181,8 @@ int main( int argc, char ** argv )
}
if(ok){ ok = AuthorizationManager::RegisterCertificateInternal(user, key, nickname, email); }
if(ok){ qDebug() << "Key Added" << user << nickname; }
else{ qDebug() << "Could not add key"; }
else{ qDebug() << "Could not add key"; } */
}else{
qDebug() << "Unknown option:" << argv[1];
return 1;