Commit 3df4d9dc authored by fred's avatar fred
Browse files

zerogw route config file

parent ca7c878b
This diff is collapsed.
This diff is collapsed.
/**@file
* @brief CFtpServer - Custom compilation
*
* @author Mail :: thebrowser@gmail.com
*
* @copyright Copyright (C) 2007 Julien Poumailloux
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* 4. If you like this software, a fee would be appreciated, particularly if
* you use this software in a commercial product, but is not required.
*/
// Compilation Options !:
/**
* @def CFTPSERVER_ENABLE_EXTRACMD
* Enable or disable extra commands support.
* Default: Disabled.
*/
//#define CFTPSERVER_ENABLE_EXTRACMD
/**
* @def CFTPSERVER_ENABLE_EVENTS
* Enable or disable events.
*/
#define CFTPSERVER_ENABLE_EVENTS
/**
* @def CFTPSERVER_ENABLE_ZLIB
* Enable or disable data transfer compression support using the Zlib library.
*/
#define CFTPSERVER_ENABLE_ZLIB
/**
* @def CFTPSERVER_ENABLE_IPV6
* Enable or disable IPV6 support.
* @note Not yet implemented.
*/
//#define CFTPSERVER_ENABLE_IPV6 to be implemented.
/**
* @def __USE_FILE_OFFSET64
* Enable or disable large file support under Win32.
* @note Under Linux and other stuff, just add -D_FILE_OFFSET_BITS=64 to the compilation line.
*/
#ifdef WIN32
#define __USE_FILE_OFFSET64
#endif
/**@file
* @brief CFtpServer - Internal configuration
*
* @author Mail :: thebrowser@gmail.com
*
* @copyright Copyright (C) 2007 Julien Poumailloux
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
* 4. If you like this software, a fee would be appreciated, particularly if
* you use this software in a commercial product, but is not required.
*/
/*
* MODIFIED by frederic@logier.org for NCS
* REMOVED useless win32 directive
*/
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#define MAX_PATH PATH_MAX
#ifndef O_BINARY
#define O_BINARY 0
#endif
#define CloseSocket(x) (shutdown((x), SHUT_RDWR), close(x))
#ifndef MSG_NOSIGNAL
#ifdef SO_NOSIGPIPE
#define MSG_NOSIGNAL SO_NOSIGPIPE
#else
#define MSG_NOSIGNAL 0
#endif
#endif
// Internal settings. Do not modify!
#define CFTPSERVER_REPLY_MAX_LEN (MAX_PATH + 64)
#define CFTPSERVER_LIST_MAX_LINE_LEN (MAX_PATH + 57)
/****************************************************************************
** ncs is the backend's server of nodecast
** Copyright (C) 2010-2013 Frédéric Logier <frederic@logier.org>
**
** https://github.com/nodecast/ncs
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "main.h"
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
params ftp_params;
ftp_params.base_directory="/tmp";
ftp_params.server_port=2121;
Service *service;
service = new Service(ftp_params);
return a.exec();
}
#ifndef MAIN_H
#define MAIN_H
#include <QSettings>
#include <QxtCore/QxtCommandOptions>
#include <qstring.h>
#include <qtextstream.h>
#include <qsocketnotifier.h>
#include <QxtJSON>
//#include <QxtNetwork>
#include <QDateTime>
#include <iostream>
#include <unistd.h>
#include <QFile>
#include <QThread>
#include <QTextStream>
#include "service.h"
class Io : public QObject
{
Q_OBJECT
public:
Io();
~Io();
private slots:
void readStdin();
private:
QFile *io_log;
QSocketNotifier* notifier;
QTextStream* input;
signals:
void parseData(QString s);
};
class NCWhandler : public QObject
{
Q_OBJECT
public:
NCWhandler();
~NCWhandler();
public slots:
void push_payload(QString s);
void writeStdout(QString s);
signals:
void emit_payload(QVariant payload);
private:
QFile *handler_log;
QTextStream* output;
};
#endif // MAIN_H
/****************************************************************************
** ncs is the backend's server of nodecast
** Copyright (C) 2010-2013 Frédéric Logier <frederic@logier.org>
**
** https://github.com/nodecast/ncs
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "nodeftp.h"
void OnServerEvent( int Event )
{
switch( Event )
{
case CFtpServer::START_LISTENING:
printf("* Server is listening !\r\n");
break;
case CFtpServer::START_ACCEPTING:
printf("* Server is accepting incoming connexions !\r\n");
break;
case CFtpServer::STOP_LISTENING:
printf("* Server stopped listening !\r\n");
break;
case CFtpServer::STOP_ACCEPTING:
printf("* Server stopped accepting incoming connexions !\r\n");
break;
case CFtpServer::MEM_ERROR:
printf("* Warning, the CFtpServer class could not allocate memory !\r\n");
break;
case CFtpServer::THREAD_ERROR:
printf("* Warning, the CFtpServer class could not create a thread !\r\n");
break;
case CFtpServer::ZLIB_VERSION_ERROR:
printf("* Warning, the Zlib header version differs from the Zlib library version !\r\n");
break;
case CFtpServer::ZLIB_STREAM_ERROR:
printf("* Warning, error during compressing/decompressing data !\r\n");
break;
}
}
void OnUserEvent( int Event, CFtpServer::CUserEntry *pUser, void *pArg )
{
switch( Event )
{
case CFtpServer::NEW_USER:
printf("* A new user has been created:\r\n"
"\tLogin: %s\r\n" "\tPassword: %s\r\n" "\tStart directory: %s\r\n",
pUser->GetLogin(), pUser->GetPassword(), pUser->GetStartDirectory() );
break;
case CFtpServer::DELETE_USER:
printf("* \"%s\"user is being deleted: \r\n", pUser->GetLogin() );
break;
}
}
void OnClientEvent( int Event, CFtpServer::CClientEntry *pClient, void *pArg )
{
switch( Event )
{
case CFtpServer::NEW_CLIENT:
printf( "* A new client has been created:\r\n"
"\tClient IP: [%s]\r\n\tServer IP: [%s]\r\n",
inet_ntoa( *pClient->GetIP() ), inet_ntoa( *pClient->GetServerIP() ) );
break;
case CFtpServer::DELETE_CLIENT:
printf( "* A client is being deleted.\r\n" );
break;
case CFtpServer::CLIENT_AUTH:
printf( "* A client has logged-in as \"%s\".\r\n", pClient->GetUser()->GetLogin() );
break;
case CFtpServer::CLIENT_SOFTWARE:
printf( "* A client has proceed the CLNT FTP command: %s.\r\n", (char*) pArg );
break;
case CFtpServer::CLIENT_DISCONNECT:
printf( "* A client has disconnected.\r\n" );
break;
case CFtpServer::CLIENT_UPLOAD:
printf( "* A client logged-on as \"%s\" is uploading a file: \"%s\"\r\n",
pClient->GetUser()->GetLogin(), (char*)pArg );
break;
case CFtpServer::CLIENT_DOWNLOAD:
printf( "* A client logged-on as \"%s\" is downloading a file: \"%s\"\r\n",
pClient->GetUser()->GetLogin(), (char*)pArg );
break;
case CFtpServer::CLIENT_LIST:
printf( "* A client logged-on as \"%s\" is listing a directory: \"%s\"\r\n",
pClient->GetUser()->GetLogin(), (char*)pArg );
break;
case CFtpServer::CLIENT_CHANGE_DIR:
printf( "* A client logged-on as \"%s\" has changed its working directory:\r\n"
"\tFull path: \"%s\"\r\n\tWorking directory: \"%s\"\r\n",
pClient->GetUser()->GetLogin(), (char*)pArg, pClient->GetWorkingDirectory() );
break;
case CFtpServer::RECVD_CMD_LINE:
printf( "* Received: %s (%s)> %s\r\n",
pClient->GetUser() ? pClient->GetUser()->GetLogin() : "(Not logged in)",
inet_ntoa( *pClient->GetIP() ),
(char*) pArg );
break;
case CFtpServer::SEND_REPLY:
printf( "* Sent: %s (%s)> %s\r\n",
pClient->GetUser() ? pClient->GetUser()->GetLogin() : "(Not logged in)",
inet_ntoa( *pClient->GetIP() ),
(char*) pArg );
break;
case CFtpServer::TOO_MANY_PASS_TRIES:
printf( "* Too many pass tries for (%s)\r\n",
inet_ntoa( *pClient->GetIP() ) );
break;
}
}
bool Nodeftp::ncs_auth(QString email, QString &token, QString &directory)
{
/* QCryptographicHash cipher( QCryptographicHash::Sha1 );
cipher.addData(password.simplified().toAscii());
QByteArray password_hash = cipher.result();
qDebug() << "password_hash : " << password_hash.toHex();
password_hashed = QString::fromLatin1(password_hash.toHex());
*/
BSONObj bauth = BSON("email" << email.toStdString());
//BSONObj l_user = mongodb_->Find("users", bauth);
BSONObj l_user;
if (l_user.nFields() == 0)
{
qDebug() << "Nodeftp::ncs_auth auth failed !";
return false;
}
token = QString::fromStdString(l_user.getFieldDotted("ftp.token").str());
directory = QString::fromStdString(l_user.getFieldDotted("ftp.directory").str());
return true;
}
Nodeftp::Nodeftp(QString a_directory, int port) : m_directory(a_directory), m_port(port)
{
//mongodb_ = Mongodb::getInstance ();
FtpServer = new CFtpServer();
#ifdef CFTPSERVER_ENABLE_EVENTS
FtpServer->SetServerCallback( OnServerEvent );
FtpServer->SetUserCallback( OnUserEvent );
FtpServer->SetClientCallback( OnClientEvent );
#endif
FtpServer->SetMaxPasswordTries( 3 );
FtpServer->SetNoLoginTimeout( 45 ); // seconds
FtpServer->SetNoTransferTimeout( 90 ); // seconds
FtpServer->SetListeningPort( port );
//FtpServer->SetDataPortRange( 100, 900 ); // data TCP-Port range = [100-999]
FtpServer->SetDataPortRange( 1025, 1900 ); // data TCP-Port range = [100-999]
FtpServer->SetCheckPassDelay( 500 ); // milliseconds. Bruteforcing protection.
#ifdef CFTPSERVER_ENABLE_ZLIB
FtpServer->EnableModeZ( true );
#endif
}
void Nodeftp::add_ftp_user(QString email)
{
qDebug() << "Nodeftp::add_user : " << email;
// check auth
QString token, directory;
if (ncs_auth(email, token, directory))
{
// create user's directory
QString userdirectory = m_directory + "/ftp/" + directory;
if (!QDir(userdirectory).exists()) QDir().mkdir(userdirectory);
CFtpServer::CUserEntry *pUser = FtpServer->AddUser( email.toAscii(), token.toAscii(), userdirectory.toAscii() );
if( pUser )
{
printf( "-User successfully created ! :)\r\n" );
pUser->SetMaxNumberOfClient( 5 ); // 0 Unlimited
/* pUser->SetPrivileges( CFtpServer::READFILE | CFtpServer::WRITEFILE |
CFtpServer::LIST | CFtpServer::DELETEFILE | CFtpServer::CREATEDIR |
CFtpServer::DELETEDIR );
*/
pUser->SetPrivileges(CFtpServer::WRITEFILE | CFtpServer::READFILE | CFtpServer::LIST);
#ifdef CFTPSERVER_ENABLE_EXTRACMD // See "CFtpServer/config.h". not defined by default
pUser->SetExtraCommand( CFtpServer::ExtraCmd_EXEC );
// Security Warning ! Only here for example.
// the last command allow the user to call the 'system()' C function!
#endif
}
else qErrnoWarning( "-Unable to create pUser" );
}
else qErrnoWarning( "-Failed to create user" );
}
void Nodeftp::ftp_init()
{
// If you only want to listen on the TCP Loopback interface,
// replace 'INNADDR_ANY' by 'inet_addr("127.0.0.1")'.
if( FtpServer->StartListening( INADDR_ANY, m_port ) ) {
qDebug( "FTP : Server is listening !" );
if( FtpServer->StartAccepting() ) {
qDebug( "FTP : Server successfuly started !" );
populate();
for( ;; )
sleep(1);
} else
qDebug( "FTP : Unable to accept incoming connections" );
FtpServer->StopListening();
} else
qErrnoWarning( "FTP : Unable to listen" );
}
void Nodeftp::populate()
{
BSONObj filter = BSON("ftp.activated" << true);
//QList <BSONObj> users_list = mongodb_->FindAll("users", filter);
QList <BSONObj> users_list;
foreach (BSONObj l_user, users_list)
{
add_ftp_user(QString::fromStdString(l_user.getField("email").str()));
}
}
Nodeftp::~Nodeftp()
{
qErrnoWarning( "FTP Exiting" );
delete FtpServer;
}
/****************************************************************************
** ncs is the backend's server of nodecast
** Copyright (C) 2010-2013 Frédéric Logier <frederic@logier.org>
**
** https://github.com/nodecast/ncs
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#ifndef NODEFTP_H
#define NODEFTP_H
#include "CFtpServer.h"
#include <QObject>
#include <QDir>
#include <QCryptographicHash>
class Nodeftp : public QObject
{
Q_OBJECT
public:
Nodeftp(QString a_directory, int port);
~Nodeftp();
private:
bool ncs_auth(QString login, QString &token, QString &directory);
void populate();
QString m_directory;
int m_port;
CFtpServer *FtpServer;
//Mongodb *mongodb_;
public slots:
void ftp_init();
void add_ftp_user(QString email);
//void remove_user(QString username);
};
#endif // NODEFTP_H
#-------------------------------------------------
#
# Project created by QtCreator 2013-03-27T17:38:36
#
#-------------------------------------------------
QMAKE_CXXFLAGS += -D_FILE_OFFSET_BITS=64
QT += core \
network \
xml
QT -= gui
TARGET = nodeftp
CONFIG += console
CONFIG -= app_bundle
CONFIG += qxt
QXT += core
TEMPLATE = app
SOURCES += main.cpp \
nodeftp.cpp \
CFtpServer.cpp \
service.cpp
HEADERS += \
nodeftp.h \
CFtpServerGlobal.h \
CFtpServerConfig.h \
CFtpServer.h \
service.h \
main.h
This diff is collapsed.
/****************************************************************************
** ncs is the backend's server of nodecast
** Copyright (C) 2010-2013 Frédéric Logier <frederic@logier.org>
**
** https://github.com/nodecast/ncs
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "service.h"
Service::Service(params ftp_params, QObject *parent) : m_ftp_params(ftp_params), QObject(parent)
{
m_nodeftp = NULL;
}
Service::~Service()
{
qDebug() << "delete ftp";
delete(m_nodeftp);
}