summaryrefslogblamecommitdiffstats
path: root/Options.cpp
blob: c862bb8cdcf3c07f081d628ca1566758a15ef24f (plain) (tree)
1
2
3
4
5
6
7
8
9



                                 


                                                 
                                    
       



                               





























                                                             






























                                                                                              
















                                                                                                           
                                                                        























                                                                                                                         
                                                                        










































                                                                                                                 


























                                                                                                                                                        
#include "Options.h"

Options::Options() {
	char _cwd[FILENAME_MAX];
#ifdef WIN32
	GetCurrentDirectory(FILENAME_MAX, _cwd);
#else
	getcwd(_cwd, FILENAME_MAX);
#endif
	std::string cwd(_cwd);

	user = "";
	pass = "";
	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 setGenMod = false;

	std::vector<std::string> 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 == "-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 (setGenMod && pass != "")
		std::cerr << "Notice: setting generator modifiers and explicitly setting a password. Explicit password takes precedence." << std::endl;
}

std::vector<std::string> Options::loadOptionsFile(int start, int argc, char* argv[]) {
	std::vector<std::string> 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;
}