From 490d36e65ac24e34e3021c2a0947384aee138c88 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 13 Apr 2016 21:05:14 -0400 Subject: Root commit for new Compass repository This is the Alpha version of ComPASS, originally developed sometime in 2014. --- Options.cpp | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 Options.cpp (limited to 'Options.cpp') diff --git a/Options.cpp b/Options.cpp new file mode 100644 index 0000000..4515307 --- /dev/null +++ b/Options.cpp @@ -0,0 +1,215 @@ +#include "Options.h" + +Options::Options() { + char _cwd[FILENAME_MAX]; + getcwd(_cwd, FILENAME_MAX); + std::string cwd(_cwd); + + user = ""; + pass = ""; + rh = ""; + pt = "3041"; + dr = cwd; + cn = -1; + ml = DEF_PASSWD_LENGTH; + ns = false; + nc = false; + nn = false; + pp = false; + cc = false; +} + +Options::~Options() { +} + +PasswordSpec Options::getPasswordSpec() { + PasswordSpec spec; + spec.ml = ml; + spec.ns = ns; + spec.nc = nc; + spec.nn = nn; + return spec; +} + +void Options::checkString(std::string str) { + if (str == "") throw 1.0; + if (str.find("\"") != std::string::npos) throw 1.0; + if (str.find("'") != std::string::npos) throw 1.0; + if (str.find(" ") != std::string::npos) throw 1.0; +} + +void Options::parseArgv(int start, int argc, char* argv[]) { + bool setPort = false; + bool setGenMod = false; + + std::vector args = loadOptionsFile(start, argc, argv); + + for (int i = 0; i < args.size(); i++) { + std::string opt(args[i]); + + if (opt == "") continue; + + if (opt == "-user") { + i++; + if (i >= args.size()) { + std::cerr << "Warning: user: value is missing." << std::endl; + i--; + continue; + } + user = std::string(args[i]); + checkString(user); + } + + else if (opt == "-pass") { + i++; + if (i >= args.size()) { + std::cerr << "Warning: pass: value is missing." << std::endl; + i--; + continue; + } + pass = std::string(args[i]); + checkString(pass); + } + + else if (opt == "-rh") { + i++; + if (i >= args.size()) { + std::cerr << "Warning: remote-host: value is missing." << std::endl; + i--; + continue; + } + rh = std::string(args[i]); + checkString(rh); + } + + else if (opt == "-pt") { + i++; + if (i >= args.size()) { + std::cerr << "Warning: port: value is missing." << std::endl; + i--; + continue; + } + pt = std::string(args[i]); + checkString(pt); + setPort = true; + } + + else if (opt == "-dr") { + i++; + if (i >= args.size()) { + std::cerr << "Warning: directory: value is missing." << std::endl; + i--; + continue; + } + dr = std::string(args[i]); + } + + else if (opt == "-cn") { + i++; + if (i >= args.size()) { + std::cerr << "Warning: credential-number: value is missing." << std::endl; + i--; + continue; + } + std::istringstream dat(std::string(args[i])); + int num; + dat >> num; + + if (dat.fail()) { + i--; + std::cerr << "Warning: trouble parsing credential number." << std::endl; + } + else { + cn = num; + + if (cn < 0) { + std::cerr << "Notice: set credential number below zero; ignoring." << std::endl; + cn = -1; + } + } + } + + else if (opt == "-ml") { + i++; + if (i >= args.size()) { + std::cerr << "Warning: max-length: value is missing." << std::endl; + i--; + continue; + } + std::istringstream dat(std::string(args[i])); + int len; + dat >> len; + + if (dat.fail()) { + i--; + std::cerr << "Warning: trouble parsing max-length." << std::endl; + } + else { + ml = len; + setGenMod = true; + + if (ml < 1) { + std::cerr << "Notice: set max-length below one; ignoring." << std::endl; + ml = DEF_PASSWD_LENGTH; + } + } + } + + else if (opt == "-ns") { + ns = true; + setGenMod = true; + } + + else if (opt == "-nc") { + nc = true; + setGenMod = true; + } + + else if (opt == "-nn") { + nn = true; + setGenMod = true; + } + + else if (opt == "-pp") + pp = true; + + else if (opt == "-cc") + cc = true; + + else + std::cerr << "Notice: unrecognized option: " << opt << std::endl; + } + + if (rh != "") + std::cout << "Notice: using remote-host: " << rh << std::endl; + + if (setPort && rh == "") + std::cerr << "Notice: setting remote port without setting remote host." << std::endl; + + if (setGenMod && pass != "") + std::cerr << "Notice: setting generator modifiers and explicitly setting a password. Explicit password takes precedence." << std::endl; +} + +std::vector Options::loadOptionsFile(int start, int argc, char* argv[]) { + std::vector args; + + std::ifstream opfile(OPTIONS_FILE); + + if (opfile.good()) { + std::cout << "Notice: using " << OPTIONS_FILE << " from working directory." << std::endl; + + while (!opfile.eof()) { + std::string str; + opfile >> str; + args.push_back(str); + } + } + + opfile.close(); + + for (int i = start; i < argc; i++) { + args.push_back(argv[i]); + } + + return args; +} -- cgit v1.2.3