cmd intrface

This commit is contained in:
j3d1 2016-01-28 15:35:43 +01:00
parent b7682b0eb6
commit 17f820284a
9 changed files with 517 additions and 0 deletions

14
src/Validator.cpp Normal file
View file

@ -0,0 +1,14 @@
/*
* Validator.cpp
*
* Created on: 28.01.2016
* Author: jedi
*/
#include "Validator.h"
Validator::Validator() {
// TODO Auto-generated constructor stub
}

16
src/Validator.h Normal file
View file

@ -0,0 +1,16 @@
/*
* Validator.h
*
* Created on: 28.01.2016
* Author: jedi
*/
#ifndef VALIDATOR_H_
#define VALIDATOR_H_
class Validator {
public:
Validator();
};
#endif /* VALIDATOR_H_ */

69
src/db.cpp Normal file
View file

@ -0,0 +1,69 @@
#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);
}

26
src/db.h Normal file
View file

@ -0,0 +1,26 @@
#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

56
src/db2.cpp Normal file
View file

@ -0,0 +1,56 @@
#include "db2.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
bool Database::open(string filename)
{
if(sqlite3_open(filename.c_str(), &database) == SQLITE_OK)
return true;
return false;
}
vector<vector<string> > Database::query2(string query)
{
sqlite3_stmt *statement;
vector<vector<string> > results;
if(!strcmp(query.c_str(),"")) return 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);
}

54
src/db2.h Normal file
View file

@ -0,0 +1,54 @@
#ifndef __DATABASE_H__
#define __DATABASE_H__
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <sqlite3.h>
using namespace std;
class Database{
private:
sqlite3 *database;
class QueryStream : public std::ostream {
private:
class QueryBuf : public std::stringbuf {
private:
Database *m_db;
public:
QueryBuf(Database *db) { m_db = db;}
~QueryBuf() { pubsync(); }
int sync() {
m_db->result = m_db->query2(str());
str("");
return 0;
}
};
public:
QueryStream(Database *db) : std::ostream(new QueryBuf(db)) {}
~QueryStream() { delete rdbuf(); }
};
public:
bool open(string filename);
vector<vector<string> > query2(string query);
void close();
vector<vector<string> > result;
QueryStream query;
Database(const std::string& filename) : query(this) {
database = NULL;
open(filename);
};
~Database(){}
};
#endif

100
src/pwmgr.cpp Normal file
View file

@ -0,0 +1,100 @@
#include <sstream>
#include <iostream>
#include <string>
#include <cstring>
#include <getopt.h>
#include "db.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 = 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();
}

43
src/rsa.cpp Normal file
View file

@ -0,0 +1,43 @@
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string>
#include <openssl/aes.h>
using namespace std;
static const unsigned char key[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};
int main()
{
unsigned char text[]="hello world!";
unsigned char enc_out[80];
unsigned char dec_out[80];
AES_KEY enc_key, dec_key;
AES_set_encrypt_key(key, 128, &enc_key);
AES_encrypt(text, enc_out, &enc_key);
AES_set_decrypt_key(key,128,&dec_key);
AES_decrypt(enc_out, dec_out, &dec_key);
int i;
printf("original:\t");
for(i=0;*(text+i)!=0x00;i++)
printf("%X ",*(text+i));
printf("\nencrypted:\t");
for(i=0;*(enc_out+i)!=0x00;i++)
printf("%X ",*(enc_out+i));
printf("\ndecrypted:\t");
for(i=0;*(dec_out+i)!=0x00;i++)
printf("%X ",*(dec_out+i));
printf("\n");
return 0;
}

139
src/test.cpp Normal file
View file

@ -0,0 +1,139 @@
#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();
}