updating with fix for desktop/laptop use
[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 user, std::string app) {
53         mnenc menc;
54         std::string dec;
55         if(menc.from_file(make_filename(user, app))) {
56                 dec = menc.get_dec();
57         } else {
58                 dec = "failure";
59         }
60         return dec;
61 }
62
63 void put_password(std::string passwd, std::string user, std::string app) {
64         mnenc micke; //Declare a encryptor/decryptor
65         micke.encrypt(passwd); //Encrypt the password Blahonga
66         micke.to_file(make_filename(user, app)); //Save to file
67
68 }
69
70 std::string open_connection(std::string str) {
71         return "mhejj";
72 }
73
74
75 std::string do_something(std::string str) { //Handle requests for secure pipe
76     pid_t proc = fork();
77     std::string reply = "";
78     if( proc == 0 ) { // child
79         reply = open_connection( str );
80     } else if( proc > 0 ) { // parent
81     } else { // fork failed
82     }
83     
84     return reply;
85 }
86
87 std::string do_something_else(std::string str) { //Handle requests
88
89         std::vector<std::string> request; //Incomming message stored here
90         request = explode(str, "|"); //explode request with function from php.hpp
91         
92         if(request[0] == "0_Unlock") { //Unlock keyring
93                 if(masterpasswd == "")  { //If master password is not yet set
94                         masterpasswd = request[1]; //set password
95                         return "201_Created\n";
96                 } else if(masterpasswd!= "") { //If master password is allready set
97                         return "403_Forbidden\n";
98                 } else { //If something else is wrong
99                         return "400_Bad_Request " + str + '\n';
100                 }
101         } 
102         
103         else if(request[0] == "10_Encrypt") { //encrypt password
104                 if(masterpasswd == "") { //If master password is not yet set
105                         return "412_Precondition_Failed\n";
106                 } else { //If we have a master password to encrypt with
107                         put_password(request[3], request[2], request[1]);
108                         return "201_Created\n";
109                 }
110                 
111         }else if(request[0] == "20_Decrypt") { //decrypt password
112                 if(masterpasswd == "") { //If master password is not yet set
113                         return "412_Precondition_Failed\n";
114                 } else { //If we have a master password to encrypt with
115                         std::string pw = get_password(request[2], request[1]);
116                         std::string message;
117                         if(pw == "") {
118                                 message = "500_Internal_Server_Error\n";
119                         } else {
120                                 message = "201_Created " + pw + "\n";
121                         }
122                         return message;
123                 } 
124         }else if(request[0] == "30_Check") { //check if password file exists
125                 bool check = false;
126                 fstream file;
127                 file.open(make_filename(request[2], request[1]).c_str(), ios::in);
128                 if( file.is_open() ) {
129                         check = true;
130                 }
131                 file.close();
132                 if(masterpasswd == "") { //If master password is not yet set
133                         return "412_Precondition_Failed\n";
134                 } else {
135                         if(check) {
136                                 return "201_Created\n";
137                         } else {
138                                 return "404_Not_Found";
139                         }
140                 }
141         }
142         else {
143                 return "400_Bad_Request " + str + '\n';
144         }
145 }
146
147 std::string m_read() {
148         std::string str;
149         std::ifstream is("mnencdfifo");
150         getline(is, str);
151         is.close();
152         return str;
153 }
154
155 void m_send(std::string message) {
156         std::ofstream os("mnencdfifo");
157         os << message;
158         os.close();
159 }
160
161 void term(int sig)
162 {
163         unlink("/tmp/mnencdfifo");
164 }
165
166 #endif