diff --git a/Makefile b/Makefile index 45ca51b..464c9a6 100644 --- a/Makefile +++ b/Makefile @@ -5,19 +5,15 @@ DEBUG = -g prefix=/usr/local -all: shepherd rsa test +all: shepherd rsa -shepherd: src/pwmgr.cpp src/db.cpp src/db.h +shepherd: src/*.cpp src/*.h mkdir -p bin - $(CC) $(CFLAGS) src/pwmgr.cpp src/db.cpp -o bin/shepherd -lsqlite3 + $(CC) $(CFLAGS) src/*.cpp -o bin/shepherd -lsqlite3 -test: src/test.cpp src/db2.cpp src/db2.h +rsa: rsa.cpp mkdir -p bin - $(CC) $(CFLAGS) src/test.cpp src/db2.cpp -o bin/test -lsqlite3 - -rsa: src/rsa.cpp - mkdir -p bin - $(CC) $(CFLAGS) src/rsa.cpp -o bin/rsa -lssl -lcrypto + $(CC) $(CFLAGS) rsa.cpp -o bin/rsa -lssl -lcrypto install: bin/shepherd install -m 0755 bin/shepherd $(prefix)/bin diff --git a/src/rsa.cpp b/rsa.cpp similarity index 100% rename from src/rsa.cpp rename to rsa.cpp diff --git a/src/db2.cpp b/src/Database.cpp similarity index 89% rename from src/db2.cpp rename to src/Database.cpp index 93ad977..ba9d94d 100644 --- a/src/db2.cpp +++ b/src/Database.cpp @@ -1,7 +1,7 @@ -#include "db2.h" #include #include #include +#include "Database.h" using namespace std; @@ -9,8 +9,8 @@ bool Database::open(string filename) { if(sqlite3_open(filename.c_str(), &database) == SQLITE_OK) return true; - - return false; + + return false; } vector > Database::query2(string query) @@ -25,7 +25,7 @@ vector > Database::query2(string query) while(true) { result = sqlite3_step(statement); - + if(result == SQLITE_ROW) { vector values; @@ -37,20 +37,20 @@ vector > Database::query2(string query) } else { - break; + break; } } - + sqlite3_finalize(statement); } - + string error = sqlite3_errmsg(database); if(error != "not an error") cout << query << " " << error << endl; - - return results; + + return results; } void Database::close() { - sqlite3_close(database); + sqlite3_close(database); } diff --git a/src/db2.h b/src/Database.h similarity index 100% rename from src/db2.h rename to src/Database.h diff --git a/src/Manager.cpp b/src/Manager.cpp new file mode 100644 index 0000000..d3ae2fd --- /dev/null +++ b/src/Manager.cpp @@ -0,0 +1,77 @@ +/* + * Manager.cpp + * + * Created on: 28.01.2016 + * Author: jedi + */ +#include +#include +#include + +#include "Manager.h" + +using namespace std; + +Manager::Manager(string file) : + db(file) { + db.query + << "CREATE TABLE IF NOT EXISTS passwd (type varchar(20),user varchar(255),host varchar(255),passwd varchar(512));" + << flush; +} + +Manager::~Manager() { + db.close(); +} + +int Manager::add(string pattern, string passwd) { + std::cmatch sm; + if (regex_match(pattern.c_str(), sm, + std::regex( + "^([a-zA-Z0-9]+):([a-zA-Z0-9/_\\.\\-]+)@([a-zA-Z0-9\\.\\-]+)$"))) { + db.query << "INSERT INTO passwd (type, user, host, passwd) VALUES('" + << sm[1] << "', '" << sm[2] << "','" << sm[3] << "','" << passwd + << "');" << flush; + } else if (regex_match(pattern.c_str(), sm, + std::regex("^([a-zA-Z0-9]+):([a-zA-Z0-9/_\\.\\-]+)$"))) { + db.query << "INSERT INTO passwd (type, user, host, passwd) VALUES('" + << sm[1] << "', '" << sm[2] << "','" << sm[2] << "','" << passwd + << "');" << flush; + } else { + cerr << "invalid pattern: " << pattern << endl; + } + return 0; +} + +int Manager::create(string pattern) { + cout << "12345" << endl; + db.query + << "INSERT INTO passwd (type, user, host, passwd) VALUES('http', 'fish','test.de','12345');" + << flush; + return 0; +} + +int Manager::show() { + db.query << "SELECT type, user, host, passwd FROM passwd;" << flush; + for (vector row : db.result) { + cout << row.at(0) << ":" << row.at(1) << "@" << row.at(2) << "\t" + << row.at(3) << endl; + } + return 0; +} + +int Manager::clear() { + db.query << "DELETE FROM passwd;" << flush; + return 0; +} + +int Manager::get(string pattern) { + db.query + << "SELECT type, user, host, passwd FROM passwd WHERE type='http' AND user='blub';" + << flush; + for (vector row : db.result) { + cout << row.at(0) << "://" << row.at(1) << "@" << row.at(2) << "\t" + << row.at(3) << endl; + } + return 0; +} + diff --git a/src/Manager.h b/src/Manager.h new file mode 100644 index 0000000..cef2bf3 --- /dev/null +++ b/src/Manager.h @@ -0,0 +1,29 @@ +/* + * Manager.h + * + * Created on: 28.01.2016 + * Author: jedi + */ +#ifndef MANAGER_H_ +#define MANAGER_H_ + +#include +#include +#include +#include "Database.h" + + +class Manager { +public: + Manager(std::string); + ~Manager(); + int add(std::string pattern, std::string passwd); + int create(std::string pattern); + int show(); + int clear(); + int get(std::string pattern); +private: + Database db; +}; + +#endif /* MANAGER_H_ */ diff --git a/src/db.cpp b/src/db.cpp deleted file mode 100644 index 9641e9b..0000000 --- a/src/db.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "db.h" -#include -#include - -using namespace std; - -Database::Database(string filename) -{ - database = NULL; - open(filename); -} - -Database::~Database() -{ -} - -bool Database::open(string filename) -{ - if(sqlite3_open(filename.c_str(), &database) == SQLITE_OK) - return true; - - return false; -} - -/*vector > Database::qstream -{ - return query(query.str()); -}*/ - -vector > Database::query(string query) -{ - sqlite3_stmt *statement; - vector > results; - if(sqlite3_prepare_v2(database, query.c_str(), -1, &statement, 0) == SQLITE_OK) - { - int cols = sqlite3_column_count(statement); - int result = 0; - while(true) - { - result = sqlite3_step(statement); - - if(result == SQLITE_ROW) - { - vector values; - for(int col = 0; col < cols; col++) - { - values.push_back((char*)sqlite3_column_text(statement, col)); - } - results.push_back(values); - } - else - { - break; - } - } - - sqlite3_finalize(statement); - } - - string error = sqlite3_errmsg(database); - if(error != "not an error") cout << query << " " << error << endl; - - return results; -} - -void Database::close() -{ - sqlite3_close(database); -} diff --git a/src/db.h b/src/db.h deleted file mode 100644 index 5ff3703..0000000 --- a/src/db.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef __DATABASE_H__ -#define __DATABASE_H__ - -#include -#include -#include -#include - -using namespace std; - -class Database -{ -public: - Database(string filename); - ~Database(); - - bool open(string filename); - vector > query(string query); - void close(); - -private: - sqlite3 *database; -}; - -#endif - diff --git a/src/pwmgr.cpp b/src/pwmgr.cpp index 571b46e..d1a4d31 100644 --- a/src/pwmgr.cpp +++ b/src/pwmgr.cpp @@ -1,10 +1,9 @@ #include #include +#include #include -#include #include -#include "db.h" - +#include "Manager.h" #define no_argument 0 #define required_argument 1 @@ -12,89 +11,93 @@ using namespace std; - constexpr unsigned int arg(const char* str, int h = 0) { - return !str[h] ? 5381 : (arg(str, h + 1) * 33) ^ str[h]; + return !str[h] ? 5381 : (arg(str, h + 1) * 33) ^ str[h]; } int main(int argc, char *argv[]) { - int opt, index; + int opt, index; + string file = "passwd.db"; - const struct option longopts[] = { { "version", no_argument, 0, 'v' }, { - "help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, { - "permanent", no_argument, 0, 's' }, { "debug", optional_argument, 0, - 'd' }, { "password", required_argument, 0, 'p' }, { "user", - required_argument, 0, 'u' }, { "interface", required_argument, 0, 'i' }, { - "header", required_argument, 0, 'b' }, { "hex", required_argument, - 0, 'x' }, { "file", required_argument, 0, 'f' }, { "timeout", - required_argument, 0, 't' }, { "wait", - required_argument, 0, 'w' }, { 0, 0, 0, 0 }, }; + const struct option longopts[] = { { "version", no_argument, 0, 'v' }, { + "help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, { + "permanent", no_argument, 0, 's' }, { "debug", optional_argument, 0, + 'd' }, { "password", required_argument, 0, 'p' }, { "user", + required_argument, 0, 'u' }, { "interface", required_argument, 0, + 'i' }, { "header", required_argument, 0, 'b' }, { "hex", + required_argument, 0, 'x' }, { "file", required_argument, 0, 'f' }, + { "timeout", required_argument, 0, 't' }, { "wait", + required_argument, 0, 'w' }, { 0, 0, 0, 0 }, }; + while ((opt = getopt_long(argc, argv, "bhrvswxp:u:i:f:t:d::", longopts, + &index)) != -1) { + switch (opt) { - while ((opt = getopt_long(argc, argv, "bhrvswxp:u:i:f:t:d::", longopts, - &index)) != -1) { - switch (opt) { + /*case 'h': + fprintf(stderr, VERSION); + fprintf(stderr, USAGE, argv[0]); + fprintf(stderr, HELP); + exit(EXIT_SUCCESS); + break; - /*case 'h': - fprintf(stderr, VERSION); - fprintf(stderr, USAGE, argv[0]); - fprintf(stderr, HELP); - exit(EXIT_SUCCESS); - break; + case 'v': + fprintf(stderr, VERSION); + exit(EXIT_SUCCESS); + break;*/ - case 'v': - fprintf(stderr, VERSION); - exit(EXIT_SUCCESS); - break;*/ + case 'r': + //options.flags |= FLAG_REVERSE; + break; - case 'r': - //options.flags |= FLAG_REVERSE; - break; + case 'd': + //options.flags |= FLAG_DEBUG; + //if (optarg != NULL) + // options.debug_level = atoi(optarg); + break; - case 'd': - //options.flags |= FLAG_DEBUG; - //if (optarg != NULL) - // options.debug_level = atoi(optarg); - break; + case 't': + //options.timeout = atol(optarg); + break; - case 't': - //options.timeout = atol(optarg); - break; + case 'f': + file = std::string(optarg); + break; - case 'f': - //options.file = std::string(optarg); - break; + default: /* '?' */ + //cerr << "Unknown option" << endl; + //cerr << argv[0] << " " << endl; + exit (EXIT_FAILURE); + } + } - default: /* '?' */ - //cerr << "Unknown option" << endl; - cerr << argv[0] << " " << endl; - exit(EXIT_FAILURE); - } - } + Manager mg(file); - Database *db = new Database("passwd.db"); - db->query("CREATE TABLE IF NOT EXISTS passwd (type varchar(20),user varchar(255),host varchar(255),passwd varchar(512));"); - - if(argc==1){ - - vector > result = db->query("SELECT type, user, host, passwd FROM passwd;"); - for(vector row : result) - { - cout << row.at(0) << "://" << row.at(1) << "@" << row.at(2) << "\t" << row.at(3) << endl; - } - - }else if(argc==2){ - if(strcmp(argv[1],"add")){ - db->query("INSERT INTO passwd (type, user, host, passwd) VALUES('http', 'blub','test.de','12345');"); - } - }else if(argc>2){ - if(strcmp(argv[1],"add")){ - db->query("INSERT INTO passwd (type, user, host, passwd) VALUES('http', 'blub','test.de','12345');"); - }else{ - cerr << argv[0] << " " << endl; - } - } - - db->close(); + switch (argc - optind) { + case 0: + mg.show(); + break; + case 1: + if (!strcmp(argv[optind], "clear")) { + mg.clear(); + cerr << file << " cleared" << endl; + } else { + mg.get(argv[optind]); + } + break; + case 2: + if (!strcmp(argv[optind], "new")) { + mg.create(argv[optind+1]); + } + break; + case 3: + if (!strcmp(argv[optind], "add")) { + mg.add(argv[optind+1], argv[optind+2]); + } else { + cerr << argv[0] << " " << endl; + } + break; + } + exit (EXIT_SUCCESS); } + diff --git a/src/test.cpp b/src/test.cpp deleted file mode 100644 index 972e2de..0000000 --- a/src/test.cpp +++ /dev/null @@ -1,139 +0,0 @@ -#include -#include -#include -#include -//#include -#include -#include "db2.h" - -#define no_argument 0 -#define required_argument 1 -#define optional_argument 2 - -using namespace std; - -constexpr unsigned int arg(const char* str, int h = 0) { - return !str[h] ? 5381 : (arg(str, h + 1) * 33) ^ str[h]; -} - -int main(int argc, char *argv[]) { - int opt, index; - - const struct option longopts[] = { { "version", no_argument, 0, 'v' }, { - "help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, { - "permanent", no_argument, 0, 's' }, { "debug", optional_argument, 0, - 'd' }, { "password", required_argument, 0, 'p' }, { "user", - required_argument, 0, 'u' }, { "interface", required_argument, 0, - 'i' }, { "header", required_argument, 0, 'b' }, { "hex", - required_argument, 0, 'x' }, { "file", required_argument, 0, 'f' }, - { "timeout", required_argument, 0, 't' }, { "wait", - required_argument, 0, 'w' }, { 0, 0, 0, 0 }, }; - - while ((opt = getopt_long(argc, argv, "bhrvswxp:u:i:f:t:d::", longopts, - &index)) != -1) { - switch (opt) { - - /*case 'h': - fprintf(stderr, VERSION); - fprintf(stderr, USAGE, argv[0]); - fprintf(stderr, HELP); - exit(EXIT_SUCCESS); - break; - - case 'v': - fprintf(stderr, VERSION); - exit(EXIT_SUCCESS); - break;*/ - - case 'r': - //options.flags |= FLAG_REVERSE; - break; - - case 'd': - //options.flags |= FLAG_DEBUG; - //if (optarg != NULL) - // options.debug_level = atoi(optarg); - break; - - case 't': - //options.timeout = atol(optarg); - break; - - case 'f': - //options.file = std::string(optarg); - break; - - default: /* '?' */ - //cerr << "Unknown option" << endl; - cerr << argv[0] << " " << endl; - exit (EXIT_FAILURE); - } - } - - Database db("passwd.db"); - db.query - << "CREATE TABLE IF NOT EXISTS passwd (type varchar(20),user varchar(255),host varchar(255),passwd varchar(512));" - << flush; - - if (argc == 1) { - - db.query << "SELECT type, user, host, passwd FROM passwd;" << flush; - for (vector row : db.result) { - cout << row.at(0) << "://" << row.at(1) << "@" << row.at(2) << "\t" - << row.at(3) << endl; - } - - } else if (argc == 2) { - if (!strcmp(argv[1], "new")) { - cout << "12345" << endl; - db.query - << "INSERT INTO passwd (type, user, host, passwd) VALUES('http', 'fish','test.de','12345');" - << flush; - } else if (!strcmp(argv[1], "clear")) { - db.query << "DELETE FROM passwd;" << flush; - } else { - db.query - << "SELECT type, user, host, passwd FROM passwd WHERE type='http' AND user='blub';" - << flush; - for (vector row : db.result) { - cout << row.at(0) << "://" << row.at(1) << "@" << row.at(2) - << "\t" << row.at(3) << endl; - } - } - } else if (argc == 3) { - - if (!strcmp(argv[1], "new")) { - cout << "12345" << endl; - db.query - << "INSERT INTO passwd (type, user, host, passwd) VALUES('http', 'fish','test.de','12345');" - << flush; - } else if (!strcmp(argv[1], "clear")) { - db.query << "DELETE FROM passwd;" << flush; - } else { - db.query - << "SELECT type, user, host, passwd FROM passwd WHERE type='http' AND user='blub';" - << flush; - for (vector row : db.result) { - cout << row.at(0) << "://" << row.at(1) << "@" << row.at(2) - << "\t" << row.at(3) << endl; - } - } - } else if (argc == 4) { - if (!strcmp(argv[1], "add")) { - std::cmatch sm; - if (regex_match(argv[2], sm, - std::regex("^([a-z0-9]+):([a-zA-Z0-9_\\.\\-]+)@([a-z0-9\\.\\-]+)$"))) { - db.query - << "INSERT INTO passwd (type, user, host, passwd) VALUES('" - << sm[1] << "', '" << sm[2] << "','" << sm[3] << "','" - << argv[3] << "');" << flush; - } else { - cerr << "invalid pattern" << endl; - } - } else { - cerr << argv[0] << " " << endl; - } - } - db.close(); -} -