Implemented most of the protocol
[mnenc] / mnencd.hpp
index 50065c8..4eb4bb2 100644 (file)
@@ -23,6 +23,8 @@
 #include <fstream>
 #include "php.hpp"
 #include <vector>
+#include <csignal>
+
 std::string masterpasswd = "";
 
 std::string remove_char(std::string str, char c) {
@@ -43,15 +45,23 @@ std::string remove_chars(std::string str) {
 }
 
 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();
+       std::string enc, dec;
        password pw = password("", "", key);
-       pw.from_file(make_filename(user, app));
-       return menc.decrypt(key, pw.get_enc());
+       if(pw.from_file(make_filename(user, app))) {
+               enc = pw.get_enc();
+               dec = menc.decrypt(key, enc);
+       } else {
+               dec = "failure";
+       }
+       return dec;
 }
 
 void put_password(std::string masterpasswd, std::string passwd, std::string user, std::string app) {
@@ -62,34 +72,83 @@ void put_password(std::string masterpasswd, std::string passwd, std::string user
        pw.to_file(make_filename(user, app));
 }
 
-std::string do_something(std::string str) {
-       std::vector<std::string> request;
-       request = explode(str, "|");
-       if(request[0][0] == '0') {
-               if(masterpasswd == "")  {
-                       masterpasswd = request[1];
+std::string do_something(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!= "") {
-                       return "403 Forbidden\n";
-               } else {
+               } 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 {
+       } 
+       
+       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(masterpasswd, 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(masterpasswd, 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