3 // Copyright 2010 Micke Nordin <mickewiki@gmail.com>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 // MA 02110-1301, USA.
19 #ifndef _mnencd_h_included_
20 #define _mnencd_h_included_
28 std::string masterpasswd = "";
30 std::string remove_char(std::string str, char c) {
31 std::string::size_type k = 0;
32 while((k=str.find(c,k))!=str.npos) {
38 std::string remove_chars(std::string str) {
39 std::string chars = " \t\n\b\a-?+\\{[]}'*'";
40 for(int i = 0; i < (signed) chars.size(); i++) {
41 str = remove_char(str, chars[i]);
47 std::string make_filename(std::string user, std::string app) {
49 name = getenv("USER");
50 return "/home/" + name + "/.mnenc/" + remove_chars(app + user);
52 std::string get_password(std::string masterpasswd, std::string user, std::string app) {
54 menc.genkey(masterpasswd);
55 std::string key = menc.get_key();
57 password pw = password("", "", key);
58 if(pw.from_file(make_filename(user, app))) {
60 dec = menc.decrypt(key, enc);
67 void put_password(std::string masterpasswd, std::string passwd, std::string user, std::string app) {
69 menc.genkey(masterpasswd);
70 std::string key = menc.get_key();
71 password pw = password(menc.encrypt(key, passwd ), "", key);
72 pw.to_file(make_filename(user, app));
75 std::string do_something(std::string str) { //Handle requests
77 std::vector<std::string> request; //Incomming message stored here
78 request = explode(str, "|"); //explode request with function from php.hpp
80 if(request[0] == "0_Unlock") { //Unlock keyring
81 if(masterpasswd == "") { //If master password is not yet set
82 masterpasswd = request[1]; //set password
83 return "201_Created\n";
84 } else if(masterpasswd!= "") { //If master password is allready set
85 return "403_Forbidden\n";
86 } else { //If something else is wrong
87 return "400_Bad_Request " + str + '\n';
91 else if(request[0] == "10_Encrypt") { //encrypt password
92 if(masterpasswd == "") { //If master password is not yet set
93 return "412_Precondition_Failed\n";
94 } else { //If we have a master password to encrypt with
95 put_password(masterpasswd, request[3], request[2], request[1]);
96 return "201_Created\n";
99 }else if(request[0] == "20_Decrypt") { //decrypt password
100 if(masterpasswd == "") { //If master password is not yet set
101 return "412_Precondition_Failed\n";
102 } else { //If we have a master password to encrypt with
103 std::string pw = get_password(masterpasswd, request[2], request[1]);
106 message = "500_Internal_Server_Error\n";
108 message = "201_Created " + pw + "\n";
112 }else if(request[0] == "30_Check") { //check if password file exists
115 file.open(make_filename(request[2], request[1]).c_str(), ios::in);
116 if( file.is_open() ) {
120 if(masterpasswd == "") { //If master password is not yet set
121 return "412_Precondition_Failed\n";
124 return "201_Created\n";
126 return "404_Not_Found";
131 return "400_Bad_Request " + str + '\n';
135 std::string m_read() {
137 std::ifstream is("mnencdfifo");
143 void m_send(std::string message) {
144 std::ofstream os("mnencdfifo");
151 unlink("/tmp/mnencdfifo");