Added function getConfigPath(), returning the config file path for the defined system.

This commit is contained in:
tobast 2011-12-19 00:20:16 +01:00
parent e4f65d1b25
commit 90e1f8e672
8 changed files with 176 additions and 17 deletions

View file

@ -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<input.size(); i++, j++)
{
if(i>=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;
}

View file

@ -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

View file

@ -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:

12
data.h
View file

@ -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"

View file

@ -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: <error-report@tobast.fr>" << 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
}

15
func.h
View file

@ -38,6 +38,16 @@
#include <iostream>
#include "CurlPost.h"
#include "data.h"
#include "ConfigRead.h"
#ifdef UNIX
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#elif defined(WINDOWS)
#include <windows.h>
#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

10
genkey.py Executable file
View file

@ -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);

View file

@ -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!