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