PW from cin

This commit is contained in:
j3d1 2018-09-05 21:51:13 +02:00
parent 15d7e00c29
commit 56affbe423
9 changed files with 246 additions and 183 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "libs/libsodium"]
path = libs/libsodium
url = https://github.com/jedisct1/libsodium.git

View file

@ -5,7 +5,7 @@ DEBUG = -g
prefix=/usr/local
all: shepherd rsa
all: shepherd
shepherd: src/*.cpp src/*.h
mkdir -p bin
@ -18,6 +18,14 @@ rsa: rsa.cpp
install: bin/shepherd
install -m 0755 bin/shepherd $(prefix)/bin
.PHONY: test
test:
test/test.sh test/test.db
# remove produced files, invoke as "make clean"
clean:
rm -f bin/*
#%.o : %.cpp
# $(CC) $(CFLAGS) -c $^ -o $@

1
libs/libsodium Submodule

@ -0,0 +1 @@
Subproject commit 11eef91e4924f0a8130731125db3a88cf605805c

43
rsa.cpp
View file

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

View file

@ -1,56 +1,49 @@
#include <iostream>
#include <string>
#include <cstring>
#include "Database.h"
using namespace std;
bool Database::open(string filename)
{
if(sqlite3_open(filename.c_str(), &database) == SQLITE_OK)
return true;
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)
{
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);
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;
}
}
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);
}
sqlite3_finalize(statement);
}
string error = sqlite3_errmsg(database);
if(error != "not an error") cout << query << " " << error << endl;
string error = sqlite3_errmsg(database);
if (error != "not an error")
cout << query << " " << error << endl;
return results;
return results;
}
void Database::close()
{
sqlite3_close(database);
void Database::close() {
sqlite3_close(database);
}

View file

@ -7,47 +7,62 @@
#include <vector>
#include <sqlite3.h>
using namespace std;
class Database{
class Database {
private:
sqlite3 *database;
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;
}
};
class QueryStream : public std::ostream {
private:
class QueryBuf : public std::stringbuf {
private:
Database *m_db;
public:
QueryBuf(Database *db) {
m_db = db;
}
public:
QueryStream(Database *db) : std::ostream(new QueryBuf(db)) {}
~QueryStream() { delete rdbuf(); }
};
~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);
bool open(string filename);
vector<vector<string> > query2(string query);
void close();
void close();
vector<vector<string> > result;
QueryStream query;
QueryStream query;
Database(const std::string& filename) : query(this) {
database = NULL;
open(filename);
};
~Database(){}
Database(const std::string &filename) :
query(this) {
database = NULL;
open(filename);
};
~Database() {
}
};
#endif

View file

@ -6,7 +6,6 @@
*/
#include <iostream>
#include <regex>
#include <string>
#include "Manager.h"
@ -27,7 +26,7 @@ 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\\.\\-]+)$"))) {
"^([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;
@ -43,10 +42,16 @@ int Manager::add(string pattern, string passwd) {
}
int Manager::create(string pattern) {
cout << "12345" << endl;
db.query
<< "INSERT INTO passwd (type, user, host, passwd) VALUES('http', 'fish','test.de','12345');"
<< flush;
char charset[] = "ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
int len = strlen(charset);
string secret;
for (int i = 0; i < 20; i++) {
secret += charset[rand() % len];
}
cout << secret << endl;
add(pattern, secret);
return 0;
}
@ -65,13 +70,70 @@ int Manager::clear() {
}
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;
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 << "SELECT * FROM passwd WHERE 1";
if (string("*").compare(sm[1]) != 0)
db.query << " AND type LIKE '" << sm[1] << "'";
if (string("*").compare(sm[2]) != 0)
db.query << " AND user LIKE '" << sm[2] << "'";
if (string("*").compare(sm[2]) != 0)
db.query << " AND host LIKE '" << sm[3] << "'";
db.query << ";" << flush;
for (vector<string> row : db.result) {
cout << row.at(0) << ":" << row.at(1) << "@" << row.at(2) << "\t"
<< row.at(3) << endl;
}
return 0;
} else if (regex_match(pattern.c_str(), sm,
std::regex("^([a-zA-Z0-9]+):([a-zA-Z0-9/_\\.\\-]+)$"))) {
db.query
<< "SELECT * FROM passwd WHERE (type, user, host, passwd) VALUES('"
<< sm[1] << "', '" << sm[2] << "','" << sm[2] << "');" << flush;
for (vector<string> row : db.result) {
cout << row.at(0) << ":" << row.at(1) << "@" << row.at(2) << "\t"
<< row.at(3) << endl;
}
return 0;
} else {
cout << "fehler: " << pattern << endl;
return 1;
}
}
int Manager::del(string pattern) {
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 << "DELETE FROM passwd WHERE 1";
if (string("*").compare(sm[1]) != 0)
db.query << " AND type LIKE '" << sm[1] << "'";
if (string("*").compare(sm[2]) != 0)
db.query << " AND user LIKE '" << sm[2] << "'";
if (string("*").compare(sm[2]) != 0)
db.query << " AND host LIKE '" << sm[3] << "'";
db.query << ";" << flush;
for (vector<string> row : db.result) {
cout << row.at(0) << ":" << row.at(1) << "@" << row.at(2) << "\t"
<< row.at(3) << endl;
}
return 0;
} else if (regex_match(pattern.c_str(), sm,
std::regex("^([a-zA-Z0-9]+):([a-zA-Z0-9/_\\.\\-]+)$"))) {
db.query
<< "DELETE FROM passwd WHERE (type, user, host, passwd) VALUES('"
<< sm[1] << "', '" << sm[2] << "','" << sm[2] << "');" << flush;
for (vector<string> row : db.result) {
cout << row.at(0) << ":" << row.at(1) << "@" << row.at(2) << "\t"
<< row.at(3) << endl;
}
return 0;
} else {
cout << "fehler: " << pattern << endl;
return 1;
}
return 0;
}

View file

@ -12,16 +12,24 @@
#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);
int del(std::string pattern);
private:
Database db;
};

View file

@ -1,7 +1,6 @@
#include <sstream>
#include <iostream>
#include <regex>
#include <string>
#include <stdlib.h>
#include <getopt.h>
#include "Manager.h"
@ -16,86 +15,103 @@ constexpr unsigned int arg(const char* str, int h = 0) {
}
int main(int argc, char *argv[]) {
srand(time(NULL));
int opt, index;
string file = "passwd.db";
string file = getenv("HOME");
file += "/.shepherd/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) {
/*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;
case 'r':
//options.flags |= FLAG_REVERSE;
break;
case 'd':
//options.flags |= FLAG_DEBUG;
//if (optarg != NULL)
// options.debug_level = atoi(optarg);
case 'd':
//options.flags |= FLAG_DEBUG;
//if (optarg != NULL)
// options.debug_level = atoi(optarg);
break;
case 't':
//options.timeout = atol(optarg);
case 't':
//options.timeout = atol(optarg);
break;
case 'f':
file = std::string(optarg);
case 'f':
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);
switch (argc - optind) {
case 0:
mg.show();
case 0:
mg.show();
break;
case 1:
if (!strcmp(argv[optind], "clear")) {
mg.clear();
cerr << file << " cleared" << endl;
} else {
case 1:
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;
}
case 2:
if (!strcmp(argv[optind], "clear")) {
if (!strcmp(argv[optind + 1], "all")) {
mg.clear();
cerr << file << " cleared" << endl;
}
} else if (!strcmp(argv[optind], "gen")) {
mg.create(argv[optind + 1]);
} else if (!strcmp(argv[optind], "del")) {
mg.del(argv[optind + 1]);
} else if (!strcmp(argv[optind], "add")) {
string pw;
cout << argv[optind + 1] << ": " << flush;
cin >> pw;
mg.add(argv[optind + 1], pw);
} else {
cerr << argv[0] << " <account identifier>" << endl;
}
break;
}
exit (EXIT_SUCCESS);