Implemented most of the protocol
[mnenc] / mnencd.hpp
1 //      mnencd.hpp
2 //      
3 //      Copyright 2010 Micke Nordin <mickewiki@gmail.com>
4 //      
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.
9 //      
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.
14 //      
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_
21 #include <string>
22 #include <iostream>
23 #include <fstream>
24 #include "php.hpp"
25 #include <vector>
26 #include <csignal>
27
28 std::string masterpasswd = "";
29
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) {
33                 str.erase(k, 1);
34         }
35         return str;
36 }
37
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]);
42         }
43         
44         return str;     
45 }
46
47 std::string make_filename(std::string user, std::string app) {
48         std::string name;
49         name = getenv("USER");
50         return "/home/" + name + "/.mnenc/" + remove_chars(app + user);
51 }
52 std::string get_password(std::string masterpasswd, std::string user, std::string app) {
53         mnenc menc = mnenc();
54         menc.genkey(masterpasswd);
55         std::string key = menc.get_key();
56         std::string enc, dec;
57         password pw = password("", "", key);
58         if(pw.from_file(make_filename(user, app))) {
59                 enc = pw.get_enc();
60                 dec = menc.decrypt(key, enc);
61         } else {
62                 dec = "failure";
63         }
64         return dec;
65 }
66
67 void put_password(std::string masterpasswd, std::string passwd, std::string user, std::string app) {
68         mnenc menc = mnenc();
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));
73 }
74
75 std::string do_something(std::string str) { //Handle requests
76
77         std::vector<std::string> request; //Incomming message stored here
78         request = explode(str, "|"); //explode request with function from php.hpp
79         
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';
88                 }
89         } 
90         
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";
97                 }
98                 
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]);
104                         std::string message;
105                         if(pw == "") {
106                                 message = "500_Internal_Server_Error\n";
107                         } else {
108                                 message = "201_Created " + pw + "\n";
109                         }
110                         return message;
111                 } 
112         }else if(request[0] == "30_Check") { //check if password file exists
113                 bool check = false;
114                 fstream file;
115                 file.open(make_filename(request[2], request[1]).c_str(), ios::in);
116                 if( file.is_open() ) {
117                         check = true;
118                 }
119                 file.close();
120                 if(masterpasswd == "") { //If master password is not yet set
121                         return "412_Precondition_Failed\n";
122                 } else {
123                         if(check) {
124                                 return "201_Created\n";
125                         } else {
126                                 return "404_Not_Found";
127                         }
128                 }
129         }
130         else {
131                 return "400_Bad_Request " + str + '\n';
132         }
133 }
134
135 std::string m_read() {
136         std::string str;
137         std::ifstream is("mnencdfifo");
138         getline(is, str);
139         is.close();
140         return str;
141 }
142
143 void m_send(std::string message) {
144         std::ofstream os("mnencdfifo");
145         os << message;
146         os.close();
147 }
148
149 void term(int sig)
150 {
151         unlink("/tmp/mnencdfifo");
152 }
153
154 #endif