#include <string>
#include <iostream>
#include <fstream>
+#include "php.hpp"
+#include <vector>
+#include <csignal>
+
+std::string masterpasswd = "";
std::string remove_char(std::string str, char c) {
std::string::size_type k = 0;
}
std::string make_filename(std::string user, std::string app) {
- return remove_chars(app + user);
+ std::string name;
+ name = getenv("USER");
+ return "/home/" + name + "/.mnenc/" + remove_chars(app + user);
}
-std::string get_password(std::string masterpasswd, std::string user, std::string app) {
- mnenc menc = mnenc();
- menc.genkey(masterpasswd);
- std::string key = menc.get_key();
- password pw = password("", "", key);
- pw.from_file(make_filename(user, app));
- return menc.decrypt(key, pw.get_enc());
+std::string get_password(std::string user, std::string app) {
+ mnenc menc;
+ std::string dec;
+ if(menc.from_file(make_filename(user, app))) {
+ dec = menc.get_dec();
+ } else {
+ dec = "failure";
+ }
+ return dec;
}
-void put_password(std::string masterpasswd, std::string passwd, std::string user, std::string app) {
- mnenc menc = mnenc();
- menc.genkey(masterpasswd);
- std::string key = menc.get_key();
- password pw = password(menc.encrypt(key, passwd ), "", key);
- pw.to_file(make_filename(user, app));
+void put_password(std::string passwd, std::string user, std::string app) {
+ mnenc micke; //Declare a encryptor/decryptor
+ micke.encrypt(passwd); //Encrypt the password Blahonga
+ micke.to_file(make_filename(user, app)); //Save to file
+
}
-std::string do_something(std::string str) {
- if(str[0] == '0') {
- return "201_Created\n";
- } else {
+std::string open_connection(std::string str) {
+ return "mhejj";
+}
+
+
+std::string do_something(std::string str) { //Handle requests for secure pipe
+ pid_t proc = fork();
+ std::string reply = "";
+ if( proc == 0 ) { // child
+ reply = open_connection( str );
+ } else if( proc > 0 ) { // parent
+ } else { // fork failed
+ }
+
+ return reply;
+}
+
+std::string do_something_else(std::string str) { //Handle requests
+
+ std::vector<std::string> request; //Incomming message stored here
+ request = explode(str, "|"); //explode request with function from php.hpp
+
+ if(request[0] == "0_Unlock") { //Unlock keyring
+ if(masterpasswd == "") { //If master password is not yet set
+ masterpasswd = request[1]; //set password
+ return "201_Created\n";
+ } else if(masterpasswd!= "") { //If master password is allready set
+ return "403_Forbidden\n";
+ } else { //If something else is wrong
+ return "400_Bad_Request " + str + '\n';
+ }
+ }
+
+ else if(request[0] == "10_Encrypt") { //encrypt password
+ if(masterpasswd == "") { //If master password is not yet set
+ return "412_Precondition_Failed\n";
+ } else { //If we have a master password to encrypt with
+ put_password(request[3], request[2], request[1]);
+ return "201_Created\n";
+ }
+
+ }else if(request[0] == "20_Decrypt") { //decrypt password
+ if(masterpasswd == "") { //If master password is not yet set
+ return "412_Precondition_Failed\n";
+ } else { //If we have a master password to encrypt with
+ std::string pw = get_password(request[2], request[1]);
+ std::string message;
+ if(pw == "") {
+ message = "500_Internal_Server_Error\n";
+ } else {
+ message = "201_Created " + pw + "\n";
+ }
+ return message;
+ }
+ }else if(request[0] == "30_Check") { //check if password file exists
+ bool check = false;
+ fstream file;
+ file.open(make_filename(request[2], request[1]).c_str(), ios::in);
+ if( file.is_open() ) {
+ check = true;
+ }
+ file.close();
+ if(masterpasswd == "") { //If master password is not yet set
+ return "412_Precondition_Failed\n";
+ } else {
+ if(check) {
+ return "201_Created\n";
+ } else {
+ return "404_Not_Found";
+ }
+ }
+ }
+ else {
return "400_Bad_Request " + str + '\n';
}
}
std::string m_read() {
std::string str;
- std::ifstream is("fife");
+ std::ifstream is("mnencdfifo");
getline(is, str);
is.close();
return str;
}
void m_send(std::string message) {
- std::ofstream os("fife");
+ std::ofstream os("mnencdfifo");
os << message;
os.close();
}
+
+void term(int sig)
+{
+ unlink("/tmp/mnencdfifo");
+}
+
#endif