139 lines
4.6 KiB
C++
139 lines
4.6 KiB
C++
#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();
|
|
}
|
|
|