This commit is contained in:
j3d1 2016-01-28 18:06:36 +01:00
parent b5efb8c4b4
commit 15d7e00c29
10 changed files with 196 additions and 325 deletions

View file

@ -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

View file

@ -1,7 +1,7 @@
#include "db2.h"
#include <iostream>
#include <string>
#include <cstring>
#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<vector<string> > Database::query2(string query)
@ -25,7 +25,7 @@ vector<vector<string> > Database::query2(string query)
while(true)
{
result = sqlite3_step(statement);
if(result == SQLITE_ROW)
{
vector<string> values;
@ -37,20 +37,20 @@ vector<vector<string> > 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);
}

77
src/Manager.cpp Normal file
View file

@ -0,0 +1,77 @@
/*
* Manager.cpp
*
* Created on: 28.01.2016
* Author: jedi
*/
#include <iostream>
#include <regex>
#include <string>
#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<string> 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<string> row : db.result) {
cout << row.at(0) << "://" << row.at(1) << "@" << row.at(2) << "\t"
<< row.at(3) << endl;
}
return 0;
}

29
src/Manager.h Normal file
View file

@ -0,0 +1,29 @@
/*
* Manager.h
*
* Created on: 28.01.2016
* Author: jedi
*/
#ifndef MANAGER_H_
#define MANAGER_H_
#include <iostream>
#include <regex>
#include <string>
#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_ */

View file

@ -1,69 +0,0 @@
#include "db.h"
#include <iostream>
#include <string>
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<vector<string> > Database::qstream
{
return query(query.str());
}*/
vector<vector<string> > Database::query(string query)
{
sqlite3_stmt *statement;
vector<vector<string> > 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<string> 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);
}

View file

@ -1,26 +0,0 @@
#ifndef __DATABASE_H__
#define __DATABASE_H__
#include <sstream>
#include <string>
#include <vector>
#include <sqlite3.h>
using namespace std;
class Database
{
public:
Database(string filename);
~Database();
bool open(string filename);
vector<vector<string> > query(string query);
void close();
private:
sqlite3 *database;
};
#endif

View file

@ -1,10 +1,9 @@
#include <sstream>
#include <iostream>
#include <regex>
#include <string>
#include <cstring>
#include <getopt.h>
#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] << " <account identifier>" << endl;
exit (EXIT_FAILURE);
}
}
default: /* '?' */
//cerr << "Unknown option" << endl;
cerr << argv[0] << " <account identifier>" << 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<vector<string> > result = db->query("SELECT type, user, host, passwd FROM passwd;");
for(vector<string> 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] << " <account identifier>" << 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] << " <account identifier>" << endl;
}
break;
}
exit (EXIT_SUCCESS);
}

View file

@ -1,139 +0,0 @@
#include <sstream>
#include <iostream>
#include <regex>
#include <string>
//#include <cstring>
#include <getopt.h>
#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] << " <account identifier>" << 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<string> 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<string> 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<string> 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] << " <account identifier>" << endl;
}
}
db.close();
}