From cd87e77c1952c0b831c3eee5f3556ccd3797bb70 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sun, 10 Jan 2010 16:21:40 +0100 Subject: [PATCH] Implemented most of the protocol --- client.sh | 4 +-- mnencd.cpp | 21 +++++++++---- mnencd.hpp | 89 +++++++++++++++++++++++++++++++++++++++++++++--------- password.cpp | 2 +- protocol_v_0.txt | 25 ++++++++------- 5 files changed, 106 insertions(+), 35 deletions(-) diff --git a/client.sh b/client.sh index 6a8c2ed..62fbbfe 100755 --- a/client.sh +++ b/client.sh @@ -1,4 +1,4 @@ #/bin/bash -echo "$1" > /home/micke/.fifo/fife -cat /home/micke/.fifo/fife +echo "$1" > /tmp/mnencdfifo +cat /tmp/mnencdfifo diff --git a/mnencd.cpp b/mnencd.cpp index 68be9a5..2573b47 100644 --- a/mnencd.cpp +++ b/mnencd.cpp @@ -26,7 +26,7 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ - +#include #include #include #include @@ -68,9 +68,17 @@ int main(int argc, char** argv) { /* Log the failure */ exit(EXIT_FAILURE); } - + std::string name, dirname; + name = getenv("USER"); + dirname = "/home/" + name + "/.mnenc/"; + if(mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IRWXO ) < 0) { + if(errno != EEXIST) { + exit(EXIT_FAILURE); + } + } + /* Change the current working directory */ - if ((chdir("/home/micke/.fifo")) < 0) { + if ((chdir("/tmp/")) < 0) { /* Log the failure */ exit(EXIT_FAILURE); } @@ -81,9 +89,11 @@ int main(int argc, char** argv) { close(STDERR_FILENO); /* Daemon-specific initialization goes here */ - - mkfifo("./fife", 0777); + mkfifo("./mnencdfifo", 0777); + signal(SIGTERM, term); // register a SIGTERM handler + //raise(SIGTERM); // will cause term() to run + /* The Big Loop */ while (1) { std::string request = m_read(); @@ -91,6 +101,5 @@ int main(int argc, char** argv) { m_send(reply); sleep(1); /* wait 1 second*/ } - unlink("fife"); exit(EXIT_SUCCESS); } diff --git a/mnencd.hpp b/mnencd.hpp index 50065c8..4eb4bb2 100644 --- a/mnencd.hpp +++ b/mnencd.hpp @@ -23,6 +23,8 @@ #include #include "php.hpp" #include +#include + 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 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 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 diff --git a/password.cpp b/password.cpp index 3125e05..5820762 100644 --- a/password.cpp +++ b/password.cpp @@ -55,7 +55,7 @@ bool password::from_file(std::string filename) { keyfile.close(); worked = true; } - key = k; + encryptedpw = k; return worked; } diff --git a/protocol_v_0.txt b/protocol_v_0.txt index 1339010..3c10050 100644 --- a/protocol_v_0.txt +++ b/protocol_v_0.txt @@ -6,33 +6,36 @@ Protocol: 201_Created 400_Bad_Request 403_Forbidden -500_Internal_Server_Error -503_Service_Unavailable +500_Internal_Server_Error - not implemented +503_Service_Unavailable - not implemented =Encrypt password= 10_Encrypt||| ==Reply== 201_Created -400_Bad_Request -500_Internal_Server_Error -503_Service_Unavailable +400_Bad_Request - not implemented +412_Precondition_Failed +500_Internal_Server_Error - not implemented +503_Service_Unavailable - not implemented =Decrypt password= 20_Decrypt|| ==Reply== -200_OK | -400_Bad_Request +200_OK| +400_Bad_Request - not implemented +412_Precondition_Failed 500_Internal_Server_Error -503_Service_Unavailable +503_Service_Unavailable - not implemented =Check if password exists= 30_Check|| ==Reply== 201_Created -400_Bad_Request +400_Bad_Request - not implemented 404_Not_Found -500_Internal_Server_Error -503_Service_Unavailable +412_Precondition_Failed +500_Internal_Server_Error - not implemented +503_Service_Unavailable - not implemented -- 1.7.9.5