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" #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: public:
ConfigRead(std::string filepath=DEFAULT_CONFIGPATH); 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 #endif//DEF_CONFIGREAD

View file

@ -1,21 +1,25 @@
# Makefile for pastebincl (command-line pastebin) by BASTIAN Théophile (aka Tobast) # Makefile for pastebincl (command-line pastebin) by BASTIAN Théophile (aka Tobast)
ENC_KEY=`./genkey.py`
CXX=g++ CXX=g++
CXXFLAGS=-Wall -Werror -O2 CXXFLAGS=-Wall -Werror -O2
CXXLIBS=-lcurl CXXLIBS=-lcurl
TARGET=pastebincl TARGET=pastebincl
OBJS=CurlPost.o func.o main.o ParameterRead.o OBJS=CurlPost.o func.o main.o ParameterRead.o ConfigRead.o
all: $(TARGET) all: $(TARGET)
$(TARGET): $(OBJS) $(TARGET): $(OBJS)
$(CXX) $(CXXLIBS) $^ $(CXXFLAGS) -o $@ $(CXX) $(CXXLIBS) $^ $(CXXFLAGS) -o $@
ConfigRead.o: ConfigRead.cpp
$(CXX) $(CXXLIBS) -c $< $(CXXFLAGS) -o $@ -DENCODING_KEY=\"${ENC_KEY}\"
%.o: %.cpp %.o: %.cpp
$(CXX) $(CXXLIBS) -c $< $(CXXFLAGS) -o $@ $(CXX) $(CXXLIBS) -c $< $(CXXFLAGS) -o $@
.PHONY: clean .PHONY: clean
clean: clean:

12
data.h
View file

@ -49,18 +49,10 @@
#define PASTEBIN_SUBMIT_URL "http://pastebin.com/api/api_post.php" #define PASTEBIN_SUBMIT_URL "http://pastebin.com/api/api_post.php"
#ifdef UNIX #define DEFAULT_CONFIGPATH ".pastebinclrc"
#define DEFAULT_CONFIGPATH "~/.pastebinclrc"
#endif
#ifdef WINDOWS
#define DEFAULT_CONFIGPATH "%APPLICATION_DATA%/.pastebinclrc"
#endif
#ifndef DEFAULT_CONFIGPATH
#define DEFAULT_CONFIGPATH ""
#endif
enum FlagParameter { 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" #define SOFT_VERSION "0.1 INDEV"

View file

@ -62,6 +62,11 @@ bool checkParameterRead(ParameterRead& pr, unsigned& flags)
flags=P_HELP; flags=P_HELP;
return true; return true;
} }
if(pr.isSet("usergen"))
{
flags=P_USERGEN;
return true;
}
if(pr.isSet("v") || pr.isSet("version")) if(pr.isSet("v") || pr.isSet("version"))
{ {
flags=P_VERSION; flags=P_VERSION;
@ -114,6 +119,25 @@ void showVersion()
<< "Error report: <error-report@tobast.fr>" << std::endl << std::endl; << "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* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::string pasteContent)
{ {
CurlPost* request=new CurlPost(PASTEBIN_SUBMIT_URL); CurlPost* request=new CurlPost(PASTEBIN_SUBMIT_URL);
@ -127,7 +151,7 @@ CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::stri
} }
else else
request->setPostData("name", DEFAULT_NAME); request->setPostData("name", DEFAULT_NAME);
if(flags & P_SYNTAX) if(flags & P_SYNTAX)
{ {
if(pr.isSet("s")) if(pr.isSet("s"))
@ -150,7 +174,7 @@ CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::stri
} }
else else
request->setPostData("api_paste_expire_date", DEFAULT_EXPIRE); request->setPostData("api_paste_expire_date", DEFAULT_EXPIRE);
if(!(flags & P_GUEST)) if(!(flags & P_GUEST))
{ {
std::string usercode=getUserCode(); 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_paste_code", pasteContent);
request->setPostData("api_dev_key", DEVELOPPER_CODE); request->setPostData("api_dev_key", DEVELOPPER_CODE);
request->setPostData("api_option", "paste"); request->setPostData("api_option", "paste");
return request; return request;
} }
@ -196,7 +220,50 @@ std::string getPasteContent()
break; break;
} }
} }
return outstr; 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 <iostream>
#include "CurlPost.h" #include "CurlPost.h"
#include "data.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 initParameterRead(ParameterRead& pr, unsigned& flags);
bool checkParameterRead(ParameterRead& pr, unsigned& flags); bool checkParameterRead(ParameterRead& pr, unsigned& flags);
@ -46,10 +56,15 @@ void showLicence();
void showHelp(); void showHelp();
void showVersion(); void showVersion();
bool userGen();
CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::string pasteContent); CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr, std::string pasteContent);
std::string getUserCode(); std::string getUserCode();
std::string getPasteContent(); std::string getPasteContent();
inline std::string getConfigPath();
void echoInput(bool echo=true);
#endif//DEF_FUNCTION #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(); showHelp();
else if(parameterFlags & P_VERSION) else if(parameterFlags & P_VERSION)
showVersion(); 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 else
{ {
// Here we start real stuff! // Here we start real stuff!