From: Enar Väikene Date: Thu, 17 Nov 2011 08:33:39 +0000 (+0200) Subject: Added command-line interface module to the PswGen application. X-Git-Url: https://vaikene.ee/gitweb/index.html?a=commitdiff_plain;h=cdc02ee6da9c883125d5cff563bdcfcc0bf2b7b8;p=evaf Added command-line interface module to the PswGen application. --- diff --git a/src/apps/PswGen/CLI/CMakeLists.txt b/src/apps/PswGen/CLI/CMakeLists.txt new file mode 100644 index 0000000..63f57a7 --- /dev/null +++ b/src/apps/PswGen/CLI/CMakeLists.txt @@ -0,0 +1,35 @@ +# Name of the target +set(TARGET PswCli) + +# Qt modules +set(QT_DONT_USE_QTGUI TRUE) +include(${QT_USE_FILE}) + +# Include files +include_directories(${eVaf_INCLUDE}) + +# Required eVaf libraries +set(eVaf_LIBRARIES CommonLib PluginsLib) + +# Source files +set(SRCS + cli.cpp +) + +# Header files for the meta-object compiler +set(MOC_HDRS + cli.h +) + +# Version info resource file for Windows builds +if(WIN32) + set(SRCS ${SRCS} version.rc) +endif(WIN32) + +qt4_wrap_cpp(MOC_SRCS ${MOC_HDRS}) + +add_library(${TARGET} SHARED ${SRCS} ${MOC_SRCS}) + +target_link_libraries(${TARGET} ${QT_LIBRARIES} ${eVaf_LIBRARIES}) + +install(TARGETS ${TARGET} DESTINATION bin) diff --git a/src/apps/PswGen/CLI/cli.cpp b/src/apps/PswGen/CLI/cli.cpp new file mode 100644 index 0000000..fbc8450 --- /dev/null +++ b/src/apps/PswGen/CLI/cli.cpp @@ -0,0 +1,216 @@ +/** + * @file PswGen/CLI/cli.cpp + * @brief Command line interface for the PswGen application + * @author Enar Vaikene + * + * Copyright (c) 2011 Enar Vaikene + * + * This file is part of the eVaf C++ cross-platform application development framework. + * + * This file can be used under the terms of the GNU General Public License + * version 3.0 as published by the Free Software Foundation and appearing in + * the file LICENSE included in the packaging of this file. Please review the + * the following information to ensure the GNU General Public License version + * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. + * + * Alternatively, this file may be used in accordance with the Commercial License + * Agreement provided with the Software. + */ + +#include "cli.h" +#include "version.h" + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#ifdef Q_OS_LINUX +# include +# include +#endif +#ifdef Q_OS_WIN32 +# include +#endif + +VER_EXPORT_VERSION_INFO() +Q_EXPORT_PLUGIN2(VER_MODULE_NAME_STR, eVaf::PswGen::CLI::Module) + + +//------------------------------------------------------------------- + +using namespace eVaf; +using namespace eVaf::PswGen::CLI; + +int const Module::DefaultPasswordLength = 16; + +Module::Module() + : Plugins::iPlugin() + , mReady(false) + , mGenerator(false) + , mStorage(false) + , mEvReady(0) +{ + setObjectName(QString("%1-Module").arg(VER_MODULE_NAME_STR)); + EVAF_INFO("%s created", qPrintable(objectName())); +} + +Module::~Module() +{ + EVAF_INFO("%s destroyed", qPrintable(objectName())); +} + +bool Module::init(QString const & args) +{ + Q_UNUSED(args); + + // Get the iGenerator interface + EVAF_TEST_X((mGenerator = evafQueryInterface("iGenerator")), "No iGenerator interface"); + + // Get the optional iStorage interface + mStorage = evafQueryInterface("iStorage"); + if (!mStorage) + EVAF_WARNING("No iStorage interface"); + + // Get the iEventQueue interface and subscribe to the 'ready' event + Common::iEventQueue * eventQueue = evafQueryInterface("iEventQueue"); + EVAF_TEST_X(eventQueue, "No iEventQueue interface"); + + // Subscribe to the 'ready' event + EVAF_TEST_X((mEvReady = eventQueue->subscribeEvent(eventQueue->queryEvent(Common::iApp::EV_READY), this)), "No 'ready' event"); + + mReady = true; + + EVAF_INFO("%s initialized", qPrintable(objectName())); + + return true; +} + +void Module::done() +{ + mReady = false; + + EVAF_INFO("%s finalized", qPrintable(objectName())); +} + +bool Module::event(QEvent * e) +{ + if (e->type() == Common::Event::eVafEvent) { + Common::Event * event = static_cast(e); + + if (event->id() == mEvReady) { + + // Generate the password + generatePassword(); + + // Quit the application + Common::iApp::instance()->quit(); + } + + return false; + } + else + return Plugins::iPlugin::event(e); +} + +QString Module::readPassword() +{ + bool noEcho = false; +#ifdef Q_OS_LINUX + termios oldt; + tcgetattr(STDIN_FILENO, &oldt); + termios newt = oldt; + newt.c_lflag &= ~ECHO; + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + noEcho = true; +#elif defined Q_OS_WIN32 + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + DWORD mode = 0; + GetConsoleMode(hStdin, &mode); + SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); + noEcho = true; +#endif + + QTextStream cin(stdin); + QString rval = cin.readLine(); + + if (noEcho) { + QTextStream cout(stdout); + cout << endl; + } + +#ifdef Q_OS_LINUX + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); +#elif defined Q_OS_WIN32 + SetConsoleMode(hStdin, mode); +#endif + + return rval; +} + +void Module::generatePassword() +{ + QString masterPassword; + QString appName; + int passwordLength = DefaultPasswordLength; + + // Process command-line arguments + QStringList args = QCoreApplication::arguments(); + for (int i = 0; i < args.size(); ++i) { + QStringList arg = args.at(i).simplified().split('='); + + if (QRegExp("-[-]?p(assword)?").exactMatch(arg.at(0)) && arg.size() > 1) + masterPassword = arg.at(1); + else if (QRegExp("-[-]?n(ame)?").exactMatch(arg.at(0)) && arg.size() > 1) + appName = arg.at(1); + else if (QRegExp("-[-]?l(ength)?").exactMatch(arg.at(0)) && arg.size() > 1) { + bool ok; + int t = arg.at(1).toInt(&ok); + if (!ok) { + EVAF_FATAL_ERROR("Invalid argument : %s", qPrintable(args.at(i))); + return; + } + passwordLength = t; + } + } + + QTextStream cin(stdin); + QTextStream cout(stdout); + + // Get missing arguments + while (masterPassword.isEmpty()) { + cout << tr("Master password : ") << flush; + masterPassword = readPassword(); + } + + while (appName.isEmpty()) { + cout << tr("Application name : ") << flush; + cin >> appName; + } + + // Get more arguments from the storage + QExplicitlySharedDataPointer data; + if (mStorage) { + data = mStorage->query(appName); + if (data) + passwordLength = data->length(); + } + + // Generate password + QString password = mGenerator->generatePassword(appName, masterPassword, passwordLength); + cout << "Generated password : " << password << endl; + + // Store arguments for this password + if (mStorage) { + if (!data) + data = new Storage::Data(appName, passwordLength); + mStorage->save(appName, data); + } +} diff --git a/src/apps/PswGen/CLI/cli.h b/src/apps/PswGen/CLI/cli.h new file mode 100644 index 0000000..9747565 --- /dev/null +++ b/src/apps/PswGen/CLI/cli.h @@ -0,0 +1,94 @@ +/** + * @file PswGen/CLI/cli.h + * @brief Command line interface for the PswGen application + * @author Enar Vaikene + * + * Copyright (c) 2011 Enar Vaikene + * + * This file is part of the eVaf C++ cross-platform application development framework. + * + * This file can be used under the terms of the GNU General Public License + * version 3.0 as published by the Free Software Foundation and appearing in + * the file LICENSE included in the packaging of this file. Please review the + * the following information to ensure the GNU General Public License version + * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. + * + * Alternatively, this file may be used in accordance with the Commercial License + * Agreement provided with the Software. + */ + +#ifndef __PSWGEN_CLI_CLI_H +# define __PSWGEN_CLI_CLI_H + +#include + +#include +#include + +class QEvent; + +namespace eVaf { +namespace PswGen { + struct iGenerator; + struct iStorage; + +/** + * Command line interface for the PswGen application. + * + * This module adds command line interface to the PswGen application. + */ +namespace CLI { + +/** + * Command line interface for the PswGen application. + */ +class Module : public Plugins::iPlugin +{ + Q_OBJECT + Q_INTERFACES(eVaf::Plugins::iPlugin) + +public: + + Module(); + + virtual ~Module(); + + virtual bool init(QString const & args); + + virtual void done(); + + virtual bool isReady() const { return mReady; } + + virtual bool event(QEvent *); + + +private: // Members + + static int const DefaultPasswordLength; + + /// Flag indicating that the module is ready + bool mReady; + + /// The iGenerator interface + eVaf::PswGen::iGenerator * mGenerator; + + /// The iStorage interface (can be null) + eVaf::PswGen::iStorage * mStorage; + + /// Ready event + uint mEvReady; + + +private: // Methods + + QString readPassword(); + + void generatePassword(); + +}; + +} // namespace eVaf::PswGen::CLI +} // namespace eVaf::PswGen +} // namespace eVaf + +#endif // cli.h diff --git a/src/apps/PswGen/CLI/lib.h b/src/apps/PswGen/CLI/lib.h new file mode 100644 index 0000000..f2a9d4d --- /dev/null +++ b/src/apps/PswGen/CLI/lib.h @@ -0,0 +1,30 @@ +/** + * @file PswGen/CLI/lib.h + * @author Enar Vaikene + * + * Copyright (c) 2011 Enar Vaikene + * + * This file is part of the eVaf C++ cross-platform application development framework. + * + * This file can be used under the terms of the GNU General Public License + * version 3.0 as published by the Free Software Foundation and appearing in + * the file LICENSE included in the packaging of this file. Please review the + * the following information to ensure the GNU General Public License version + * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. + * + * Alternatively, this file may be used in accordance with the Commercial License + * Agreement provided with the Software. + */ + +#ifndef __PSWGEN_CLI_LIB_H +# define __PSWGEN_CLI_LIB_H + +#include + +#if defined(PSWGEN_CLI_LIBRARY) +# define PSWGEN_CLI_EXPORT Q_DECL_EXPORT +#else +# define PSWGEN_CLI_EXPORT Q_DECL_IMPORT +#endif + +#endif // libgen.h diff --git a/src/apps/PswGen/CLI/version.h b/src/apps/PswGen/CLI/version.h new file mode 100644 index 0000000..2707d5a --- /dev/null +++ b/src/apps/PswGen/CLI/version.h @@ -0,0 +1,60 @@ +/** + * @file PswGen/CLI/version.h + * @brief Version information for eVaf modules + * @author Enar Vaikene + * + * Copyright (c) 2011 Enar Vaikene + * + * This file is part of the eVaf C++ cross-platform application development framework. + * + * This file can be used under the terms of the GNU General Public License + * version 3.0 as published by the Free Software Foundation and appearing in + * the file LICENSE included in the packaging of this file. Please review the + * the following information to ensure the GNU General Public License version + * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. + * + * Alternatively, this file may be used in accordance with the Commercial License + * Agreement provided with the Software. + */ + +#ifndef __PSWGEN_CLI_VERSION_H +# define __PSWGEN_CLI_VERSION_H + +#include + +/** + * Module/library version number in the form major,minor,release,build + */ +#define VER_FILE_VERSION 0,1,1,1 + +/** + * Module/library version number in the string format (shall end with \0) + */ +#define VER_FILE_VERSION_STR "0.1.1.1\0" + +/** + * Module/library name (shall end with \0) + */ +#define VER_MODULE_NAME_STR "PswCli\0" + +/** + * Module type (see version_rc.h for all the types) + */ +#define VER_MODULE_TYPE MT_GENERIC + +/** + * Module type in the string format (see version_rc for all the types) + */ +#define VER_MODULE_TYPE_STR MT_GENERIC + +/** + * Original file name for windows (shall end with \0) + */ +#define VER_ORIGINAL_FILE_NAME_STR "PswCli.dll\0" + +/** + * Description of the module/library (shall end with \0) + */ +#define VER_FILE_DESCRIPTION_STR "Comman line interface module for PswGen application.\0" + +#endif // version.h diff --git a/src/apps/PswGen/CLI/version.rc b/src/apps/PswGen/CLI/version.rc new file mode 100644 index 0000000..9cf3076 --- /dev/null +++ b/src/apps/PswGen/CLI/version.rc @@ -0,0 +1,54 @@ +/** + * @file PswGen/CLI/version.rc + * @brief Windows resource file with module/library version information. + * @author Enar Vaikene + * + * Copyright (c) 2011 Enar Vaikene + * + * This file is part of the eVaf C++ cross-platform application development framework. + * + * This file can be used under the terms of the GNU General Public License + * version 3.0 as published by the Free Software Foundation and appearing in + * the file LICENSE included in the packaging of this file. Please review the + * the following information to ensure the GNU General Public License version + * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. + * + * Alternatively, this file may be used in accordance with the Commercial License + * Agreement provided with the Software. + */ + +#include "version.h" +#include +#include + + +VS_VERSION_INFO VERSIONINFO + FILEVERSION VER_FILE_VERSION + PRODUCTVERSION VER_PRODUCT_VERSION + FILEFLAGSMASK 0x3fL +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS__WINDOWS32 + FILETYPE VFT_DLL + FILESUBTYPE 0x0L + BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904B0" + BEGIN + VALUE "CompanyName", VER_COMPANY_NAME_STR + VALUE "FileDescription", VER_FILE_DESCRIPTION_STR + VALUE "FileVersion", VER_FILE_VERSION_STR + VALUE "LegalCopyright", VER_LEGAL_COPYRIGHT_STR + VALUE "OriginalFilename", VER_ORIGINAL_FILE_NAME_STR + VALUE "ProductName", VER_PRODUCT_NAME_STR + VALUE "ProductVersion", VER_PRODUCT_VERSION_STR + VALUE "Build Date", VER_PRODUCT_DATE_STR + VALUE "Module Name", VER_MODULE_NAME_STR + VALUE "Module Type", VER_MODULE_TYPE_STR + END + END + END diff --git a/src/apps/PswGen/CMakeLists.txt b/src/apps/PswGen/CMakeLists.txt index c7c1db5..01770c2 100644 --- a/src/apps/PswGen/CMakeLists.txt +++ b/src/apps/PswGen/CMakeLists.txt @@ -1,5 +1,6 @@ set(eVaf_INCLUDE ${eVaf_INCLUDE} ${CMAKE_SOURCE_DIR}/src/apps/PswGen) add_subdirectory(GUI) +add_subdirectory(CLI) add_subdirectory(Generator) add_subdirectory(Storage)