From 90e1f8e672baee2eb8f51c44e58ac1fd80f495d1 Mon Sep 17 00:00:00 2001 From: tobast Date: Mon, 19 Dec 2011 00:20:16 +0100 Subject: [PATCH] Added function getConfigPath(), returning the config file path for the defined system. --- ConfigRead.cpp | 53 +++++++++++++++++++++++++++++++++++ ConfigRead.h | 13 ++++++++- Makefile | 8 ++++-- data.h | 12 ++------ func.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++--- func.h | 15 ++++++++++ genkey.py | 10 +++++++ main.cpp | 7 +++++ 8 files changed, 176 insertions(+), 17 deletions(-) create mode 100755 genkey.py diff --git a/ConfigRead.cpp b/ConfigRead.cpp index 271bccd..097f5da 100644 --- a/ConfigRead.cpp +++ b/ConfigRead.cpp @@ -33,4 +33,57 @@ #include "ConfigRead.h" +using namespace std; + +ConfigRead::ConfigRead(string filepath) +{ + ifstream infile(filepath.c_str()); + if(!infile) + throw -1; + + string filecontent=unxor(infile); + + user=filecontent; + user.erase(user.begin()+user.find("\n"), user.end()); + pass=filecontent; + pass.erase(pass.begin(), pass.begin()+pass.find("\n")); + pass.erase(pass.begin()+pass.find("\n"), pass.end()); // erases everything on lines after. + + infile.close(); +} + +bool ConfigRead::writeConf(std::string user, std::string pass, std::string filepath) +{ + ofstream outfile(filepath.c_str(), ios_base::out | ios_base::trunc); + if(!outfile) + return false; + + string key=ENCODING_KEY, input=user+"\n"+pass; + + for(unsigned i=0, j=0; j=key.size()) + i=0; + char c=input[j]^key[i]; + outfile.put(c); + } + + return true; +} + +std::string ConfigRead::unxor(ifstream& infile) +{ + string key=ENCODING_KEY, output=""; + + for(unsigned i=0; !infile.eof(); i++) + { + if(i>=key.size()) + i=0; + char c=infile.get(); + if(infile.good()) + output+= c ^ key[i]; + } + + return output; +} diff --git a/ConfigRead.h b/ConfigRead.h index 292d844..ffc64f6 100644 --- a/ConfigRead.h +++ b/ConfigRead.h @@ -42,7 +42,18 @@ class ConfigRead { public: ConfigRead(std::string filepath=DEFAULT_CONFIGPATH); -} + static bool writeConf(std::string user, std::string pass, std::string filepath=DEFAULT_CONFIGPATH); + + std::string getUser() const { return user; } + std::string getPass() const { return pass; } + + protected: //meth + std::string unxor(std::ifstream& infile); + + protected: + std::string user; + std::string pass; +}; #endif//DEF_CONFIGREAD diff --git a/Makefile b/Makefile index 8e7fd50..e1b9705 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,25 @@ # Makefile for pastebincl (command-line pastebin) by BASTIAN Théophile (aka Tobast) +ENC_KEY=`./genkey.py` + CXX=g++ CXXFLAGS=-Wall -Werror -O2 CXXLIBS=-lcurl TARGET=pastebincl -OBJS=CurlPost.o func.o main.o ParameterRead.o +OBJS=CurlPost.o func.o main.o ParameterRead.o ConfigRead.o all: $(TARGET) $(TARGET): $(OBJS) $(CXX) $(CXXLIBS) $^ $(CXXFLAGS) -o $@ +ConfigRead.o: ConfigRead.cpp + $(CXX) $(CXXLIBS) -c $< $(CXXFLAGS) -o $@ -DENCODING_KEY=\"${ENC_KEY}\" + %.o: %.cpp $(CXX) $(CXXLIBS) -c $< $(CXXFLAGS) -o $@ - .PHONY: clean clean: diff --git a/data.h b/data.h index 526f3dc..4b351a6 100644 --- a/data.h +++ b/data.h @@ -49,18 +49,10 @@ #define PASTEBIN_SUBMIT_URL "http://pastebin.com/api/api_post.php" -#ifdef UNIX - #define DEFAULT_CONFIGPATH "~/.pastebinclrc" -#endif -#ifdef WINDOWS - #define DEFAULT_CONFIGPATH "%APPLICATION_DATA%/.pastebinclrc" -#endif -#ifndef DEFAULT_CONFIGPATH - #define DEFAULT_CONFIGPATH "" -#endif +#define DEFAULT_CONFIGPATH ".pastebinclrc" enum FlagParameter { - P_NAME=1, P_SYNTAX=2, P_PRIVATE=4, P_EXPIRE=8, P_GUEST=16, P_HELP=32, P_VERSION=64, P_LICENCE=128 + P_NAME=1, P_SYNTAX=2, P_PRIVATE=4, P_EXPIRE=8, P_GUEST=16, P_HELP=32, P_VERSION=64, P_LICENCE=128, P_USERGEN=256 }; #define SOFT_VERSION "0.1 INDEV" diff --git a/func.cpp b/func.cpp index 3b96e14..9690133 100644 --- a/func.cpp +++ b/func.cpp @@ -62,6 +62,11 @@ bool checkParameterRead(ParameterRead& pr, unsigned& flags) flags=P_HELP; return true; } + if(pr.isSet("usergen")) + { + flags=P_USERGEN; + return true; + } if(pr.isSet("v") || pr.isSet("version")) { flags=P_VERSION; @@ -114,6 +119,25 @@ void showVersion() << "Error report: " << std::endl << std::endl; } +bool userGen() +{ + std::string user="", pass=""; + std::cout << "Enter your nickname: \t"; + std::cin >> user; + std::cin.ignore(); + + std::cout << "Enter your password (willn't be displayed, type it blindly ; 128 char max): \t"; + echoInput(false); + char passc[128]; + std::cin.getline(passc, 128); + pass=passc; + echoInput(true); + + std::cout << std::endl; + + return ConfigRead::writeConf(user, pass); +} + CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::string pasteContent) { CurlPost* request=new CurlPost(PASTEBIN_SUBMIT_URL); @@ -127,7 +151,7 @@ CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::stri } else request->setPostData("name", DEFAULT_NAME); - + if(flags & P_SYNTAX) { if(pr.isSet("s")) @@ -150,7 +174,7 @@ CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::stri } else request->setPostData("api_paste_expire_date", DEFAULT_EXPIRE); - + if(!(flags & P_GUEST)) { std::string usercode=getUserCode(); @@ -161,7 +185,7 @@ CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::stri request->setPostData("api_paste_code", pasteContent); request->setPostData("api_dev_key", DEVELOPPER_CODE); request->setPostData("api_option", "paste"); - + return request; } @@ -196,7 +220,50 @@ std::string getPasteContent() break; } } - + return outstr; } +inline std::string getConfigPath() +{ + std::string outpath; + +#if defined(UNIX) + outpath=getpwuid(getuid())->pw_dir; +#elif defined(WINDOwS) + outpath="C:/Documents and Settings/"; + + char username[100]; + DWORD nUsername=sizeof(username); + if(!GetUserName(username, &nUsername)) + outpath+="All Users"; + else + outpath+=username; + outpath+="/Application Data/"; +#endif + + outpath+=DEFAULT_CONFIGPATH; + + return outpath; +} + +void echoInput(bool echo) +{ +#ifdef UNIX + struct termios settings; + tcgetattr( STDIN_FILENO, &settings ); + settings.c_lflag = echo + ? (settings.c_lflag | ECHO ) + : (settings.c_lflag & ~(ECHO)); + tcsetattr( STDIN_FILENO, TCSANOW, &settings ); +#elif defined(WINDOWS) + DWORD mode; + HANDLE hConIn = GetStdHandle( STD_INPUT_HANDLE ); + GetConsoleMode( hConIn, &mode ); + mode = echo + ? (mode | ENABLE_ECHO_INPUT ) + : (mode & ~(ENABLE_ECHO_INPUT)); + SetConsoleMode( hConIn, mode ); +#endif +} + diff --git a/func.h b/func.h index 86ab3be..e3ca77c 100644 --- a/func.h +++ b/func.h @@ -38,6 +38,16 @@ #include #include "CurlPost.h" #include "data.h" +#include "ConfigRead.h" + +#ifdef UNIX + #include + #include + #include + #include +#elif defined(WINDOWS) + #include +#endif bool initParameterRead(ParameterRead& pr, unsigned& flags); bool checkParameterRead(ParameterRead& pr, unsigned& flags); @@ -46,10 +56,15 @@ void showLicence(); void showHelp(); void showVersion(); +bool userGen(); + CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::string pasteContent); std::string getUserCode(); std::string getPasteContent(); +inline std::string getConfigPath(); + +void echoInput(bool echo=true); #endif//DEF_FUNCTION diff --git a/genkey.py b/genkey.py new file mode 100755 index 0000000..cf3de1e --- /dev/null +++ b/genkey.py @@ -0,0 +1,10 @@ +#!/usr/bin/python +import hashlib +import random + + +randseed=random.random(); +randstr="(0DeD |3`/ _-"+str(randseed)+"-_ T08as7"; +key=hashlib.sha512(randstr.encode('utf-8')).hexdigest(); + +print(key); diff --git a/main.cpp b/main.cpp index 19f6bb5..438bdf3 100644 --- a/main.cpp +++ b/main.cpp @@ -57,6 +57,13 @@ int main(int argc, char** argv) showHelp(); else if(parameterFlags & P_VERSION) showVersion(); + else if(parameterFlags & P_USERGEN) + { + if(userGen()) + std::cout << "Configuration file generated!" << std::endl; + else + std::cerr << "An error occured while writing configuration file. FAIL." << std::endl; + } else { // Here we start real stuff!